// If you have PHP you can set the post values like this
var postState = '';
var postCountry = '';

// State table
//
// To edit the list, just delete a line or add a line. Order is important.
// The order displayed here is the order it appears on the drop down.
//
var state = '\
US:AK:Alaska|\
US:AL:Alabama|\
US:AR:Arkansas|\
US:AS:American Samoa|\
US:AZ:Arizona|\
US:CA:California|\
US:CO:Colorado|\
US:CT:Connecticut|\
US:DC:D.C.|\
US:DE:Delaware|\
US:FL:Florida|\
US:FM:Micronesia|\
US:GA:Georgia|\
US:GU:Guam|\
US:HI:Hawaii|\
US:IA:Iowa|\
US:ID:Idaho|\
US:IL:Illinois|\
US:IN:Indiana|\
US:KS:Kansas|\
US:KY:Kentucky|\
US:LA:Louisiana|\
US:MA:Massachusetts|\
US:MD:Maryland|\
US:ME:Maine|\
US:MH:Marshall Islands|\
US:MI:Michigan|\
US:MN:Minnesota|\
US:MO:Missouri|\
US:MP:Marianas|\
US:MS:Mississippi|\
US:MT:Montana|\
US:NC:North Carolina|\
US:ND:North Dakota|\
US:NE:Nebraska|\
US:NH:New Hampshire|\
US:NJ:New Jersey|\
US:NM:New Mexico|\
US:NV:Nevada|\
US:NY:New York|\
US:OH:Ohio|\
US:OK:Oklahoma|\
US:OR:Oregon|\
US:PA:Pennsylvania|\
US:PR:Puerto Rico|\
US:PW:Palau|\
US:RI:Rhode Island|\
US:SC:South Carolina|\
US:SD:South Dakota|\
US:TN:Tennessee|\
US:TX:Texas|\
US:UT:Utah|\
US:VA:Virginia|\
US:VI:Virgin Islands|\
US:VT:Vermont|\
US:WA:Washington|\
US:WI:Wisconsin|\
US:WV:West Virginia|\
US:WY:Wyoming|\
US:AA:Military Americas|\
US:AE:Military Europe/ME/Canada|\
US:AP:Military Pacific|\
CA:AB:Alberta|\
CA:MB:Manitoba|\
CA:AB:Alberta|\
CA:BC:British Columbia|\
CA:MB:Manitoba|\
CA:NB:New Brunswick|\
CA:NL:Newfoundland and Labrador|\
CA:NS:Nova Scotia|\
CA:NT:Northwest Territories|\
CA:NU:Nunavut|\
CA:ON:Ontario|\
CA:PE:Prince Edward Island|\
CA:PQ:Quebec|\
CA:SK:Saskatchewan|\
CA:YT:Yukon Territory|\
';

// Country data table
//
// To edit the list, just delete a line or add a line. Order is important.
// The order displayed here is the order it appears on the drop down.
//
var country = '\
CA:Canada|\
US:United States\
';


var shipping = '\
CA:Canada|\
US:United States\
';


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) {
  if ( postCountry != '' ) {
    defaultCountry = postCountry;
  }
  var countryLineArray = country.split('|');  // Split into lines
  var selObj = document.getElementById('countrySelect');
  selObj.options[0] = new Option('Select Country','');
  selObj.selectedIndex = 0;
  for (var loop = 0; loop < countryLineArray.length; loop++) {
    lineArray = countryLineArray[loop].split(':');
    countryCode  = TrimString(lineArray[0]);
    countryName  = TrimString(lineArray[1]);
    if ( countryCode != '' ) {
      selObj.options[loop + 1] = new Option(countryName, countryCode);
    }
    if ( defaultCountry == countryCode ) {
      selObj.selectedIndex = loop + 1;
    }
  }
}

function populateState() {
  var selObj = document.getElementById('stateSelect');
  var foundState = false;
  // 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 State','');
    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 (document.getElementById('countrySelect').value == countryCode && countryCode != '' ) {
    // If it's a input element, change it to a select
      if ( selObj.type == 'text' ) {
        parentObj = document.getElementById('stateSelect').parentNode;
        parentObj.removeChild(selObj);
        var inputSel = document.createElement("SELECT");
        inputSel.setAttribute("name","state");
        inputSel.setAttribute("id","stateSelect");
        parentObj.appendChild(inputSel) ;
        selObj = document.getElementById('stateSelect');
        selObj.options[0] = new Option('Select State','');
        selObj.selectedIndex = 0;
      }
      if ( stateCode != '' ) {
        selObj.options[optionCntr] = new Option(stateName, stateCode);
      }
      // See if it's selected from a previous post
//      if ( stateCode == postState && countryCode == postCountry ) {
      if ( stateCode == postState) {
        selObj.selectedIndex = optionCntr;
      }
      foundState = true;
      optionCntr++
    }
  }
  // If the country has no states, change the select to a text box
  if ( ! foundState ) {
    parentObj = document.getElementById('stateSelect').parentNode;
    parentObj.removeChild(selObj);
  // Create the Input Field
    var inputEl = document.createElement("INPUT");
    inputEl.setAttribute("id", "stateSelect");
    inputEl.setAttribute("type", "text");
    inputEl.setAttribute("name", "state");
    inputEl.setAttribute("size", 20);
    inputEl.setAttribute("value", postState);
    parentObj.appendChild(inputEl) ;
  }

  var selCnt = document.getElementById('countrySelect');
  //var elSel = document.getElementById('sachin');
  //if (elSel.options[0].value=='SS_CHARGES' && selCnt.selectedIndex=='1') {
  if (selCnt.selectedIndex=='1') {
  	//elSel.remove(0);
	var radauth = document.getElementById('rd_an');
	if(radauth)
	{
		radauth.disabled = false;
		radauth.checked = true;
	}
	document.getElementById('paymentmethod').style.display = '';
	
	var radgoo = document.getElementById('rd_gc');
		radgoo.disabled = true;
	
	var radebill = document.getElementById('rd_eb');
		radebill.disabled = true;
  }
  
	//if (elSel.options[0].value=='FEDEX_CHARGES' && selCnt.selectedIndex=='2') {
	if (selCnt.selectedIndex=='2') {
		var radauth = document.getElementById('rd_an');
		if ( radauth ) { radauth.disabled = false; }
		var radgoo = document.getElementById('rd_gc');
		if ( radgoo ) { radgoo.disabled = false; }
			//radgoo.checked = true;
		var radebill = document.getElementById('rd_eb');
		if ( radebill ) {	radebill.disabled = false; }
		//var elSel = document.getElementById('sachin');
		/*if (elSel) {
			if (elSel.selectedIndex >= 0) {
				var elOptNew = document.createElement('option');
				elOptNew.text = 'Super Saver Shipping 7-10 Business Days FREE';
				elOptNew.value = 'SS_CHARGES';
				var elOptOld = elSel.options[elSel.selectedIndex];  
				try {
					elSel.add(elOptNew, elOptOld); // standards compliant; doesn't work in IE
					}
				catch(ex) {
					elSel.add(elOptNew, elSel.selectedIndex); // IE only
					}
			}
		elSel.options[0].selected=true;
		}*/
	}
}



function populateStateUpdate(defaultState) {
	postState = defaultState;
  var selObj = document.getElementById('stateSelect');
  var foundState = false;
  // 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 State','');
   // 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 (document.getElementById('countrySelect').value == countryCode && countryCode != '' ) {
    // If it's a input element, change it to a select
      if ( selObj.type == 'text' ) {
        parentObj = document.getElementById('stateSelect').parentNode;
        parentObj.removeChild(selObj);
        var inputSel = document.createElement("SELECT");
        inputSel.setAttribute("name","state");
        inputSel.setAttribute("id","stateSelect");
        parentObj.appendChild(inputSel) ;
        selObj = document.getElementById('stateSelect');
        selObj.options[0] = new Option('Select State','');
        
        //selObj.selectedIndex = 0;
           if ( defaultState == stateCode ) {
	         selObj.selectedIndex = loop + 1;
	    }

      }
      if ( stateCode != '' ) {
        selObj.options[optionCntr] = new Option(stateName, stateCode);
      }
      // See if it's selected from a previous post
//      if ( stateCode == postState && countryCode == postCountry ) {
      if ( stateCode == postState) {
        selObj.selectedIndex = optionCntr;
      }

      if ( defaultState == stateCode ) {
	 selObj.selectedIndex = loop + 1;
      }
  
      foundState = true;
      optionCntr++
    }
  }
  // If the country has no states, change the select to a text box
  if ( ! foundState ) {
    parentObj = document.getElementById('stateSelect').parentNode;
    parentObj.removeChild(selObj);
  // Create the Input Field
    var inputEl = document.createElement("INPUT");
    inputEl.setAttribute("id", "stateSelect");
    inputEl.setAttribute("type", "text");
    inputEl.setAttribute("name", "state");
    inputEl.setAttribute("size", 20);
    inputEl.setAttribute("value", postState);
    parentObj.appendChild(inputEl) ;
  }

  var selCnt = document.getElementById('countrySelect');
  //var elSel = document.getElementById('sachin');
  //if (elSel.options[0].value=='SS_CHARGES' && selCnt.selectedIndex=='1') {
  	//elSel.remove(0);
  //}
  
  /*if (elSel.options[0].value=='FEDEX_CHARGES' && selCnt.selectedIndex=='2') {
	var elSel = document.getElementById('sachin');
  	if (elSel.selectedIndex >= 0) {
    	var elOptNew = document.createElement('option');
    	elOptNew.text = 'Super Saver Shipping 7-10 Business Days FREE';
    	elOptNew.value = 'SS_CHARGES';
    	var elOptOld = elSel.options[elSel.selectedIndex];  
    	try {
      		elSel.add(elOptNew, elOptOld); // standards compliant; doesn't work in IE
    	}
    	catch(ex) {
      	elSel.add(elOptNew, elSel.selectedIndex); // IE only
    	}
  	}
  }
  elSel.options[0].selected=true;*/
}


function initCountry(country) {
  populateCountry(country);
  populateState();
}


// New function. This function is used in cart_chout.php file in case when user has logged in but having 
// incomplete personal information.

function initCountryUpdate(country, defaultState) {
  populateCountry(country);
  populateStateUpdate(defaultState);
}
