
//detect browser type - NS or IE
var isNS = (document.layers) ? true : false;
var isIE = (document.all) ? true : false;

//US value=1, Canada value=2
var arrUSAStatesValues = new Array(
" ", "AL","AK","AZ","AR","CA","CO","CT",
"DC","DE","FL","GA","HI","ID","IL",
"IN","IA","KS","KY","LA","ME","MD",
"MA","MI","MN","MS","MO","MT","NE",
"NV","NH","NJ","NM","NY","NC","ND",
"OH","OK","OR","PA","RI","SC","SD",
"TN","TX","UI","VT","VA","WA","WV",
"WI","WY","XX");

var arrUSAStatesText = new Array(
"[select a state]", "Alabama","Alaska","Arizona","Arkansas",
"California","Colorado","Connecticut","D.C.",
"Delaware","Florida","Georgia","Hawaii","Idaho",
"Illinois","Indiana","Iowa","Kansas","Kentucky",
"Louisiana","Maine","Maryland","Massachusetts",
"Michigan","Minnesota","Mississippi","Missouri",
"Montana","Nebraska","Nevada","New Hampshire",
"New Jersey","New Mexico","New York","North Carolina",
"North Dakota","Ohio","Oklahoma","Oregon","Pennsylvania",
"Rhode Island","South Carolina","South Dakota","Tennessee",
"Texas","Utah","Vermont","Virginia","Washington",
"West Virginia","Wisconsin","Wyoming","Other");

var arrCANADAStatesValues = new Array(" ", "AB","BC","MB","NB","NF","NT","NS","NU","ON","PE","QC","SK","YT");

var arrCANADAStatesText = new Array(
"[select a province]","Alberta","British Columbia","Manitoba",
"New Brunswick","Newfoundland","Northwest Territories",
"Nova Scotia","Nunavut","Ontario","Prince Edward Island",
"Quebec","Saskatchewan","Yukon");

function CheckCountryOther(selectedCountry, otherText){
	if (selectedCountry=="00"){
		// Other was selected
		document.form.country_other.value = "";
		document.form.country_other.disabled = false;
	}
	else {
		// Other was NOT selected
		document.form.country_other.value = otherText;
		document.form.country_other.disabled = true;
	}
}

function PopulateStates(selectedCountry, otherText){

	if (otherText == null) otherText = "";
	
	//first clear out the existing selectbox and textbox values
	document.form.state_textbox.value = "";
	document.form.state_dropdown.length = 1;

	if (isIE){
		//easy
		document.form.state_dropdown.options[0] = null;
	}else {	//isNS or some other archaic browser...
		//NS4 doesn't seem to like above trick - instead just blank out every existing option...
		for (n=0;n<document.form.state_dropdown.options.length;n++){
			document.form.state_dropdown.options[n] = new Option("","");
		}
	}

	if ( (selectedCountry=="US") || (selectedCountry=="CA"))
	{
		document.form.country_other.value = otherText;
		document.form.country_other.disabled = true;

		//USA or CAN was selected, populate dropdown and disable textbox
		myState = otherText;
		myIndex = 0;
		
		if (selectedCountry=="US")
		{
			for (i=0; i < arrUSAStatesValues.length;i++)
			{
				document.form.state_dropdown.options[i] = new Option( arrUSAStatesText[i], arrUSAStatesValues[i]);
				if (myState == arrUSAStatesText[i] || myState == arrUSAStatesValues[i]) myIndex = i;
			}
		}
		else
		{
			for (i=0; i < arrCANADAStatesValues.length;i++)
			{
				document.form.state_dropdown.options[i] = new Option(arrCANADAStatesText[i],arrCANADAStatesValues[i]);
				if (myState == arrCANADAStatesText[i] || myState == arrCANADAStatesValues[i]) myIndex = i;
			}
		}
			
		document.form.state_dropdown.selectedIndex= myIndex;
		document.form.state_dropdown.disabled = false;		
		document.form.state_textbox.value = document.form.state_dropdown.options[document.form.state_dropdown.selectedIndex].value;
		document.form.state_textbox.disabled = true;
	}
	else	
	{
		//Country other than USA and CANADA selected, default dropdown to Other and enable textbox
		document.form.state_dropdown.options[0] = new Option(" ", " ");
		document.form.state_dropdown.options[0].selected = true;
		document.form.state_dropdown.disabled = true;
		
		document.form.state_textbox.disabled = false;
		document.form.state_textbox.value = document.form.state.value;
		
  		CheckCountryOther(selectedCountry, otherText);
	}

	//always set hidden form variable state equal to state_textbox
  	document.form.state.value = document.form.state_textbox.value;
}

function PopulateStateTextbox(){
	var selectedCountry = document.form.country.options[document.form.country.selectedIndex].value;
	if (selectedCountry=="US" || selectedCountry=="CA"){
		document.form.state_textbox.value = document.form.state_dropdown.options[document.form.state_dropdown.selectedIndex].value;
		document.form.state_textbox.disabled = true;
	}

	//always set hidden form variable state equal to state_textbox
  	document.form.state.value = document.form.state_textbox.value;
}

function CheckDropdownUsability(){
	var selectedCountry = document.form.country.options[document.form.country.selectedIndex].value;
	if (isNS && (selectedCountry=="US" || selectedCountry=="CA") ){
		document.form.state_textbox.blur();
	}

}
function CheckTextboxUsability(){
	var selectedCountry = document.form.country.options[document.form.country.selectedIndex].value;
	if (isNS && (selectedCountry=="US" || selectedCountry=="CA") ){
		document.form.state_dropdown.blur();
	}

}

function VerifyState(){
  document.form.state.value = document.form.state_textbox.value;
}

function trim(str) {
		while (str.substring(0,1) == ' ') str = str.substring(1);
		while (str.substring(str.length-1,str.length) == ' ') str = str.substring(0,str.length-1);
		return(str);
}

function getStateName(sValue, cValue){
	var i=0;
	if (cValue=="1"){	//USA
		for (i=0;i<arrUSAStatesValues.length;i++){
			if (sValue == arrUSAStatesValues[i]){
				return(arrUSAStatesText[i]);
			}
		}
	}else if (cValue=="2"){ //CANADA
		for (i=0;i<arrCANADAStatesValues.length;i++){
			if (sValue == arrCANADAStatesValues[i]){
				return(arrCANADAStatesText[i]);
			}
		}
	}else {
		return("");
	}
}

function RePopulateStates(stateValue,selectedCountry){
	//first clear out the existing selectbox and textbox values
	if (isIE){
		//easy
		document.form.state_dropdown.options[0] = null;
	}else {	//isNS or some other archaic browser...
		//NS4 doesn't seem to like above trick - instead just blank out every existing option...
		for (n=0;n<document.form.state_dropdown.options.length;n++){
			document.form.state_dropdown.options[n] = new Option("","");
		}
	}

	if (selectedCountry=="US"){
		//USA was selected, populate dropdown and disable textbox
		for (i=0; i < arrUSAStatesValues.length;i++){
			document.form.state_dropdown.options[i] = new Option(arrUSAStatesText[i],arrUSAStatesValues[i]);
			if (document.form.state_dropdown.options[i].value == stateValue){
				document.form.state_dropdown.options[i].selected = true;
				document.form.state_textbox.value = document.form.state_dropdown.options[document.form.state_dropdown.selectedIndex].value;
			}
			document.form.state_dropdown.disabled = false;
			document.form.state_textbox.disabled = true;
		}
	}else
	if (selectedCountry=="CA"){
			//CANADA was selected, populate dropdown and disable textbox
		for (i=0; i < arrCANADAStatesValues.length;i++){
			document.form.state_dropdown.options[i] = new Option(arrCANADAStatesText[i],arrCANADAStatesValues[i]);
			if (document.form.state_dropdown.options[i].value == stateValue){
				document.form.state_dropdown.options[i].selected = true;
				document.form.state_textbox.value = document.form.state_dropdown.options[document.form.state_dropdown.selectedIndex].value;
			}
			document.form.state_dropdown.disabled = false;
			document.form.state_textbox.disabled = true;
		}
	}else	{//Country other than USA and CANADA selected, default dropdown to Other and enable textbox
		document.form.state_dropdown.options[0] = new Option("Other","XX");
		document.form.state_dropdown.options[0].selected = true;
		document.form.state_dropdown.disabled = true;
		document.form.state_textbox.disabled = false;
	}

	//always set hidden form variable state equal to state_textbox
	document.form.state.value = document.form.state_textbox.value;
}

