var stateArray = new Array();

function wddxGetElement1(str,elementName) {
	var re = new RegExp('<'+elementName+'>([\\s\\S]*?)<\/'+elementName+'>','i');
	var arrFound = re.exec(str);
	if (arrFound && (arrFound.length>1)) {
		if (arrFound[1].length > 0) {
			//document.getElementById("httpMsg").innerHTML = "<br>" + arrFound[1] + "<br><br>";
			if (arrFound[1].substr(0,6) != "Result") {
				if (document.getElementById("httpMsg")) document.getElementById("httpMsg").innerHTML = "<br>" + arrFound[1] + "<br><br>";
			}
		}
		return arrFound[1];
	}
	else return "Error Occured. Please contact Site Support.";
}

function httpRequest1(url) {
	// creates XMLHttpRequest object
	if (window.XMLHttpRequest) req = new XMLHttpRequest();
	else if (window.ActiveXObject) req = new ActiveXObject("Microsoft.XMLHTTP");
	// request URL
	req.open("GET", url, false);
	req.send(null);
	return req.responseText;
}	

function getStatesListFromSAP() {
	// US States
	var url = '/scripts/getStates.cfc?method=GetStates&Country=US';
	response = wddxGetElement1(httpRequest1(url),'string');
	if (response.substr(0,7) != "Result:") {
		alert("Error retrieving States for United States: " + response);
		return "";
	} else {
		response = response.substr(7,response.length);
		var tmpArray = response.split("***");
		for (var i=0; i<tmpArray.length-1; i++) {
			var tmpArray1 = tmpArray[i].split(";;;");
			stateArray[i]= new Array("USA", trim(tmpArray1[0]), trim(tmpArray1[1]));
		}
	}
	
	//Canada States
	var url = '/scripts/getStates.cfc?method=GetStates&Country=CA';
	response = wddxGetElement1(httpRequest1(url),'string');
	if (response.substr(0,7) != "Result:") {
		alert("Error retrieving States for Canada: " + response);
		return "";
	} else {
		response = response.substr(7,response.length);
		
		var tmpArray = response.split("***");
		index = stateArray.length;
		for (var i=0; i<tmpArray.length-1; i++) {
			var tmpArray1 = tmpArray[i].split(";;;");
			stateArray[index++]= new Array("CAN", trim(tmpArray1[0]), trim(tmpArray1[1]));
		}
	}
}

function SetStates(selCountry, stateCtrl) {
	stateControl = document.getElementById(stateCtrl);
	selectedCountryID = selCountry;
	while( stateControl.options.length ) stateControl.options[0] = null;
	option = new Option("Select a State", "");
	stateControl.options[stateControl.length] = option;
	for(index = 0; index < stateArray.length; index++) {
		if(stateArray[index][0]==selectedCountryID) {
			option = new Option(stateArray[index][2],stateArray[index][1]);
			stateControl.options[stateControl.length] = option;
		}
	}
	return true;
}

function SetSelectedItemForChooser(theControlName, theSessionVar) {
	theControl = document.getElementById(theControlName);
	//alert(theControlName + " -- " + theControl.length + " -- " + theSessionVar + " -- " + theControl.options[2].value);
	for(index=0;index<theControl.length;index++) {
		if(theControl.options[index].value==theSessionVar) {
			theControl.selectedIndex = index;
		}	
	}
}

function trim(str) {
	return str.replace(/^\s*|\s*$/g,"");
}
