function updateStateName(stateSelectBox){
	frm = stateSelectBox.form;
	frm.stateprovince.value = stateSelectBox.options[stateSelectBox.selectedIndex].text;
}
function updateCountryName(countrySelectBox){
	frm = countrySelectBox.form;
	frm.country.value = countrySelectBox.options[countrySelectBox.selectedIndex].text;
}

HTTPRequest = function(){
	var xmlhttp = null;
	try{
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");

	} catch (_e){
		try{
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");

		} catch (_E){


		}
	}
	if (!xmlhttp && typeof XMLHttpRequest != "undefined"){
		try{
			xmlhttp = new XMLHttpRequest();

		} catch (e){
			xmlhttp = false;
		}

	}
	return xmlhttp;

}

function selectCountry(){
	hasError = false;
	selectBox = document.getElementById("countryid");
	if (!selectBox.type && selectBox.type != 'select'){
		alert(typeof(selectBox));
		return;
	}
	
	try {
		var http = new HTTPRequest();
		url = "aja_state_lookup.cfm?countryid="+selectBox.options[selectBox.selectedIndex].value;
		http.open("GET", url, true);
		http.onreadystatechange = function(){ handleHttpResponse(http);}
		http.send(null);
	} catch (e){
		alert('An error occured retrieving the State/Province/Prefecture list.\n\nPlease try again.');
		selectBox.selectedIndex = 0;
	}
	

}

function setPhonePrefix(){
	hasError = false;
	selectBox = document.getElementById("countryid");
	if (!selectBox.type && selectBox.type != 'select'){
		alert(typeof(selectBox));
		return;
	}
	
	try {
		var http = new HTTPRequest();
		url = "aja_isdn_lookup.cfm?countryid="+selectBox.options[selectBox.selectedIndex].value;
		http.open("GET", url, true);
		http.onreadystatechange = function(){ handleISDNHttpResponse(http);}
		http.send(null);
	} catch (e){
		alert('An error occured retrieving the State/Province/Prefecture list.\n\nPlease try again.');
		selectBox.selectedIndex = 0;
	}
	

}



var defaultOption = new Option("Please Select a Country");
var loadingOption = new Option("Loading");
var noStatesOption = new Option("Not Applicable","0");


function handleHttpResponse(http){
	try {

			if (http.readyState == 4){
				options = parseResponse(trim(http.responseText));
				if (options.length > 0){
					setStateOptions(options);
				} else {
					setStateOptions(noStatesOption);
				}
			} else {
				setStateOptions(loadingOption);

			}
			setSelectedState(currentlySelectedStateID);

		} catch (e){
			if (http.readyState == 4){
				alert('An error occured retrieving the State/Province/Prefecture list.\n\nPlease try again.');
				countryBox = document.getElementById("countryid");
				countryBox.selectedIndex = 0;
				setStateOptions(defaultOption);
			}
	}
}

function handleISDNHttpResponse(http){
	try {

			if (http.readyState == 4){
				isdn = trim(http.responseText)+' ';
				//document.getElementById("phone").value = isdn;
			}

		} catch (e){
			if (http.readyState == 4){
				alert('An error occured retrieving the State/Province/Prefecture list.\n\nPlease try again.');
				countryBox = document.getElementById("countryid");
				countryBox.selectedIndex = 0;
				setStateOptions(defaultOption);
			}
	}
}

function setSelectedState(state_id){
	selectBox = document.getElementById("state_province_id");
	for(i=0;i<selectBox.options.length;i++){
		if (selectBox.options[i].value == state_id){
			selectBox.options[i].selected = true;
			break;
		}
	}
	updateStateName(selectBox);
	
}

function setStateOptions(options){
	selectBox = document.getElementById("state_province_id");
	selectBox.selectedIndex = 0;
	if (options.length && typeof(options) == "object"){
		selectBox.options.length=options.length;

		for (i=0;i<options.length;i++){
			selectBox.options[i] = options[i];
		}
	} else if (typeof(options) == "object"){
		selectBox.options.length=1;
		selectBox.options[0] = options;

	} else {
		selectBox.options.length=1;
		selectBox.options[0] = defaultOption;
	}

}

function cleanOptions(selectBox, startIndex){
	if (startIndex > selectBox.options.length){
		alert('index higher than total');
		return;
	}
	for(i=startIndex;i<selectBox.options.length;i++){
		selectBox.options[i] = null;
	}

}
function trim(str){
	return str.replace(/(^\s*)|(\s*$)/g, "");
}

function parseResponse(response){

	vals = response.split("\n");
	options = new Array();
	for(i = 0;i<vals.length;i++){
		if (trim(vals[i]).length == 0){
			continue;
		}
		line = vals[i].split('|');
		if (line.length != 2){
			continue;
		}
		options[options.length] = new Option(line[1], line[0]);
	}
	return options;
}
