var state_array = new Array();

function initialize_state_country(StateFormControl, CountryFormControl) {
	var counter = 0;
	var counter2;
	var state_length;
	var country_length;
	var country_number = -1;
	var state_array_number = 0;
	var state_selected;
	var country_selected;
	var selected = "0";
	var country_code_array = new Array();
	var country_text_array = new Array();
	var country_counter = 0;

	state_length = StateFormControl.length;
	country_length = CountryFormControl.length;
	state_selected = StateFormControl.selectedIndex;
	country_selected = CountryFormControl.selectedIndex;

	
	 // Make an array of states and countries
	 for (counter = 0; counter < country_length; counter++) {
	     state_array[counter] = new Array();
	 	  country_code_array[counter] = new Array(CountryFormControl.options[counter].value);
		  country_text_array[counter] = new Array(CountryFormControl.options[counter].text);
		  country_counter++
	 }
	
	 // Make an array of states
	  for (counter = 0; counter < state_length; counter++) {
	     if ((StateFormControl.options[counter].value == "0000") || (StateFormControl.options[counter].value == "") ) {
	         for (counter2 = 1; counter2 < country_length; counter2++) {
	             if (StateFormControl.options[counter].text == CountryFormControl.options[counter2].text) {
	                 country_number = counter2;
	             }
	         }
	         state_array_number = 0;
	     } else {
	         if (counter == state_selected) {
	             selected = "1";
	         } else {
	             selected = "0";
	         }
	
	         if (country_number == -1) { country_number = 0 }
	         state_array[country_number][state_array_number] = new Array(StateFormControl.options[counter].value, StateFormControl.options[counter].text, StateFormControl.options[counter].selected);
	         state_array_number++;
	     }
	 }
	 
	 CountryFormControl.options[country_selected].selected = 1;
	 alter_state_list("PageLoad", StateFormControl, CountryFormControl);
	    
	 // Alter the country list 
	 CountryFormControl.length = 0;
	 for (counter = 0; counter < country_counter; counter++) {
		CountryFormControl.options[counter] = new Option(country_text_array[counter], country_code_array[counter]);
	 }
	 CountryFormControl.options[country_selected].selected = 1;
}
 

// Change the state list
function alter_state_list(amode, StateFormControl, CountryFormControl) {
     // See which Country is selected
     var counter = 0;
     var counter3 = 0;
     var country_length;
     var country_selected;
     var country_selected = 0;
     var state_has_been_selected = 0;
     var statecode_array = new Array();
        
     country_length = CountryFormControl.length;
     for (counter = 0; counter < country_length; counter++) {
         if (CountryFormControl.options[counter].selected) {
             country_selected = counter;
         }
     }
     // Alter the state list
	  StateFormControl.length = 0;
	  StateFormControl.options[0] = new Option("                                 ","");
		for (counter = 0; counter < state_array[country_selected].length; counter++) {
		   StateFormControl.options[counter + 1] = new Option(state_array[country_selected][counter][1], state_array[country_selected][counter][0], 0);
		   if (amode == "PageLoad") {
				if (state_array[country_selected][counter][2] == 1) {
					StateFormControl.options[counter + 1].selected = 1;
					state_has_been_selected = 1;
				} else {
					StateFormControl.options[counter + 1].selected = 0;
				}
			}
					
			if (state_has_been_selected == 0) {
				//default to blank
				StateFormControl.options[0].selected = 1;
			} else {
				StateFormControl.options[0].selected = 0;
			}
		}
}
