﻿
/* This script and many more are available free online at
The JavaScript Source :: http://javascript.internet.com
Created by: Down Home Consulting :: http://downhomeconsulting.com */

/*
Country State Drop Downs v1.0.
(c) Copyright 2005 Down Home Consulting, Inc.
www.DownHomeConsulting.com

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. The software is provided "as is", without warranty of any kind, express or implied, including but not limited to the warranties of merchantability, itness for a particular purpose and noninfringement. in no event shall the authors or copyright holders be liable for any claim, damages or other liability, whether in an action of contract, tort or otherwise, arising from, out of or in connection with the software or the use or other dealings in the software.

*/


function TrimString(sInString) {
  if ( sInString ) {
    sInString = sInString.replace( /^\s+/g, "" );// strip leading
    return sInString.replace( /\s+$/g, "" );// strip trailing
  }
}

// Populates the country selected with the counties from the country list
function populateCountry(defaultCountry, countryFormName) {
  if ( postCountry != '' ) {
    defaultCountry = postCountry;
  }
  var countryLineArray = country.split('|');  // Split into lines
  var selObj = eval('document.forms[0].' + countryFormName);
  selObj.selectedIndex = 0;
  for (var loop = 0; loop < countryLineArray.length-1; loop++) {
    lineArray = countryLineArray[loop].split(':');
    countryCode  = TrimString(lineArray[0]);
    countryName  = TrimString(lineArray[1]);
    if ( countryCode != '' ) {
      //selObj.options[loop + 1] = new Option(countryName, countryCode);
      selObj.options[loop] = new Option(countryName, countryCode);
    }
    if ( defaultCountry == countryCode ) {
      selObj.selectedIndex = loop + 1;
    }
  }
}

function populateState(countryFormName, stateFormName) {
    //var selObj = document.getElementById('stateSelect');
    var selObj = eval('document.forms[0].' + stateFormName);
    var objCountry = eval('document.forms[0].' + countryFormName);
    // Empty options just in case new drop down is shorter
    if ( selObj.type == 'select-one' ) {
        for (var i = 0; i < selObj.options.length; i++) {
          selObj.options[i] = null;
        }
        selObj.options.length=null;
        selObj.options[0] = new Option('Select','');
        selObj.selectedIndex = 0;
    }
    // Populate the drop down with states from the selected country
    var stateLineArray = state.split("|");  // Split into lines
    var optionCntr = 1;
    for (var loop = 0; loop < stateLineArray.length; loop++) {
        lineArray = stateLineArray[loop].split(":");
        countryCode  = TrimString(lineArray[0]);
        stateCode    = TrimString(lineArray[1]);
        stateName    = TrimString(lineArray[2]);
        if (objCountry.value == countryCode && countryCode != '' ) {
          if ( stateCode != '' ) {
            selObj.options[optionCntr] = new Option(stateName, stateCode);
          }
          // See if it's selected from a previous post
          if ( stateCode == postState && countryCode == postCountry ) {
            selObj.selectedIndex = optionCntr;
          }
          optionCntr++
        }
    }
 
}


function populateStateOnLoad(countryFormName, stateFormName, countryIn, stateIn) {
    var selObj = eval('document.forms[0].' + stateFormName);
    var objCountry = eval('document.forms[0].' + countryFormName);


    for (var j = 0; j < objCountry.options.length; j++) {
    	if (objCountry.options[j].value == countryIn ){
          objCountry.selectedIndex = j;
    	}
    }
    
    
    // Populate the drop down with states from the selected country
    var stateLineArray = state.split("|");  // Split into lines
    var optionCntr = 1;
    for (var loop = 0; loop < stateLineArray.length; loop++) {
        lineArray = stateLineArray[loop].split(":");
        countryCode  = TrimString(lineArray[0]);
        stateCode    = TrimString(lineArray[1]);
        stateName    = TrimString(lineArray[2]);
        if (countryCode == countryIn && countryIn != '' ) {
          if ( stateCode != '' ) {
            selObj.options[optionCntr] = new Option(stateName, stateCode);
          }
          // See if it's selected from a previous post
          if ( stateCode == stateIn  && countryCode == countryIn) {
            selObj.selectedIndex = optionCntr;
          }
          optionCntr++
        }
    }
    
 
}

function initCountry(country, countryFormName, stateFormName) {
  populateCountry(country, countryFormName);
  populateState(countryFormName, stateFormName);
}
