var USArray =  new Array(
				"(Alabama,240)",
				"(Alaska,241)",
				"(Arizona,242)",
				"(Arkansas,243)",
				"(California,244)",
				"(Colorado,245)",
				"(Connecticut,246)",
				"(Delaware,247)",
				"(District Of Columbia,248)",
				"(Florida,249)",
				"(Georgia,250)",
				"(Hawaii,251)",
				"(Idaho,252)",
				"(Illinois,253)",
				"(Indiana,254)",
				"(Iowa,255)",
				"(Kansas,256)",
				"(Kentucky,257)",
				"(Louisiana,258)",
				"(Maine,259)",
				"(Maryland,260)",
				"(Massachusetts,261)",
				"(Michigan,262)",
				"(Minnesota,263)",
				"(Mississippi,264)",
				"(Missouri,265)",
				"(Montana,266)",
				"(Nebraska,267)",
				"(Nevada,268)",
				"(New Hampshire,269)",
				"(New Jersey,270)",
				"(New Mexico,271)",
				"(New York,272)",
				"(North Carolina,273)",
				"(North Dakota,274)",
				"(Ohio,275)",
				"(Oklahoma,276)",
				"(Oregon,277)",
				"(Pennsylvania,278)",
				"(Rhode Island,279)",
				"(South Carolina,280)",
				"(South Dakota,281)",
				"(Tennessee,282)",
				"(Texas,283)",
				"(Utah,284)",
				"(Vermont,285)",
				"(Virginia,286)",
				"(Washington,287)",
				"(West Virginia,288)",
				"(Wisconsin,289)",
				"(Wyoming,290)");
		

/*********************************************************************
Array Of States Of Canada
-------------------------
The IDs are from the table 'Region' in the database.
The Region Ids' in 'Address' table, should get updated with these Ids
**********************************************************************/
var CAArray =  new Array(
				"(Alberta,291)",
				"(British Columbia,292)",
				"(Manitoba,293)",
				"(New Brunswick,294)",
				"(Newfoundland,295)",
				"(Northwest Territories,296)",
				"(Nova Scotia,297)",
				"(Ontario,298)",
				"(Prince Edward Island,299)",
				"(Quebec,300)",
				"(Saskatchewan,301)",
				"(Yukon,302)",
				"(Nunavut,335)");		
		
		
/*********************************************************************
Array Of States Of Mexico
-------------------------
The IDs are from the table 'Region' in the database.
The Region Ids' in 'Address' table, should get updated with these Ids
**********************************************************************/
var MXArray =  new Array(
				"(Distrito Federal,303)",
				"(Aguascalientes,304)",
				"(Baja California,305)", 
				"(Baja California Sur,306)", 
				"(Campeche,307)", 
				"(Chiapas,308)", 
				"(Chihuahua,309)", 
				"(Coahuila de Zaragoza,310)",
				"(Colima,311)", 
				"(Durango,312)", 
				"(Guanajuato,313)", 
				"(Guerrero,314)", 
				"(Hidalgo,315)", 
				"(Jalisco,316)", 
				"(México,317)", 
				"(Michoacán de Ocampo,318)", 
				"(Morelos,319)", 
				"(Nayarit,320)", 
				"(Nuevo León,321)", 
				"(Oaxaca,322)", 
				"(Puebla,323)", 
				"(Querétaro de Arteaga,324)", 
				"(Quintana Roo,325)", 
				"(San Luis Potosí,326)", 
				"(Sinaloa,327)", 
				"(Sonora,328)", 
				"(Tabasco,329)", 
				"(Tamaulipas,330)", 
				"(Tlaxcala,331)", 
				"(Veracruz-Llave,332)", 
				"(Yucatán,333)", 
				"(Zacatecas,334)");		

/*****************************************************************************
**********************END OF STATES ARRAY*************************************
*****************************************************************************/

function htmlSelectCountryOnChange()
{

	this.regionSelectorProperties.htmlInputHiddenCountry.value = this.regionSelectorProperties.htmlSelectCountry.options[this.regionSelectorProperties.htmlSelectCountry.selectedIndex].value

	//This variable should be changed when more countries with states are included
	var sStatesAbbr	= "US-CA-MX";
	var sSelectState;

	var sStateName;
	var sStateId;

	this.regionSelectorProperties.htmlSelectState.options.length = 0;
	
	var selArrayVal = "OO";
		
	// Check for the selected country value and set the right value to form the array.
	//Add countries to switch statement, whenever states are added to a country.
	switch(this.regionSelectorProperties.htmlInputHiddenCountry.value)
	{
		case "225":			
			selArrayVal = "US";
			break;
		case "38":
			selArrayVal = "CA";
			break;
		case "140":
			selArrayVal = "MX";
			break;
	}
	
	
	//Check if the selected country has states:
	if (sStatesAbbr.indexOf(selArrayVal)!=-1)
	{

		//this.regionSelectorProperties.htmlSelectState.disabled = false;
		
		var selectedArray = eval(selArrayVal + "Array");
		
		
		
		while (selectedArray.length < this.regionSelectorProperties.htmlSelectState.options.length)
		{
			this.regionSelectorProperties.htmlSelectState.options[(this.regionSelectorProperties.htmlSelectState.options.length - 1)] = null;
		}
		
		sStatesArray	= selectedArray;
		if( selArrayVal == "CA" )//Canada
		{
			sSelectState		= new Option('Select A Province', 0);
			this.regionSelectorProperties.spnState.innerHTML = "Province:";
		}
		else
		{
			sSelectState		= new Option('Select A State', 0);
			this.regionSelectorProperties.spnState.innerHTML = "State:";
		}
		
		this.regionSelectorProperties.htmlSelectState.options[this.regionSelectorProperties.htmlSelectState.options.length]	= sSelectState;
		for (var state=0; state < sStatesArray.length; state++)
		{
			sStateName	= sStatesArray[state].substring( 1, sStatesArray[state].indexOf(',') )
			sStateId	= sStatesArray[state].substring( sStatesArray[state].indexOf(',')+1, sStatesArray[state].indexOf(')') )
			sState		= new Option(sStateName, sStateId);
			this.regionSelectorProperties.htmlSelectState.options[this.regionSelectorProperties.htmlSelectState.options.length] = sState;
		}
		
		var stateValue = this.regionSelectorProperties.htmlInputHiddenState.value;
		var optionsLength = this.regionSelectorProperties.htmlSelectState.options.length - 1;
		var selectedStateId = this.regionSelectorProperties.htmlSelectState.options[optionsLength].value;


		for(var i = 0; i < optionsLength; i++)
		//while(selectedStateId.indexOf(stateValue) == -1)
		//while(stateValue != '0' && stateValue != stateOptionValue)
		{			
			selectedStateId = this.regionSelectorProperties.htmlSelectState.options[i].value;
			//alert('stateValue == selectedStateId: ' + stateValue  + '==' + selectedStateId + (stateValue  == selectedStateId) );
			if (stateValue == selectedStateId)
			{
				this.regionSelectorProperties.htmlSelectState.options[i].selected = true;
			}
		}
		
		//alert('stateValue: ' + stateValue + " | this.regionSelectorProperties.htmlSelectState.selectedIndex" + this.regionSelectorProperties.htmlSelectState.selectedIndex);
	
	}
	else
	{
		//this.regionSelectorProperties.htmlSelectState.disabled = true;
	}	
	//alert('Country changed!\n Country - ' + this.regionSelectorProperties.htmlInputHiddenCountry.value + '\nState - ' + this.regionSelectorProperties.htmlInputHiddenState.value);
}



function htmlSelectStateOnChange()
{	
	var oldvalue = this.regionSelectorProperties.htmlInputHiddenState.value;
	this.regionSelectorProperties.htmlInputHiddenState.value = this.regionSelectorProperties.htmlSelectState.options[this.regionSelectorProperties.htmlSelectState.selectedIndex].value;
	//alert(oldvalue + ":" + 	this.regionSelectorProperties.htmlInputHiddenState.value);
}
function RegionSelector_Init()
{
	for ( var i = 0; i < RegionSelector_RegionSelectorArray.length; i++ ) 
	{
		var regionSelectorProperties = RegionSelector_RegionSelectorArray[i];
/*		//alert(
			'regionSelectorProperties.ID:'						+ regionSelectorProperties.ID + '\n' +
			'regionSelectorProperties.htmlInputHiddenCountry:'	+ regionSelectorProperties.htmlInputHiddenCountry + '\n' +
			'regionSelectorProperties.htmlInputHiddenState:'	+ regionSelectorProperties.htmlInputHiddenState + '\n' +
			'regionSelectorProperties.spnCountry:'				+ regionSelectorProperties.spnCountry + '\n' +
			'regionSelectorProperties.spnState:'				+ regionSelectorProperties.spnState + '\n' +
			'regionSelectorProperties.htmlSelectState:'			+ regionSelectorProperties.htmlSelectState + '\n' +
			'regionSelectorProperties.htmlSelectCountry:'		+ regionSelectorProperties.htmlSelectCountry + '\n');
*/		
		regionSelectorProperties.initComplete = false;
		regionSelectorProperties.ID						= document.getElementById(regionSelectorProperties.ID);
		regionSelectorProperties.htmlInputHiddenCountry	= document.getElementById(regionSelectorProperties.htmlInputHiddenCountry);		
		regionSelectorProperties.htmlInputHiddenState	= document.getElementById(regionSelectorProperties.htmlInputHiddenState);
		regionSelectorProperties.spnCountry				= document.getElementById(regionSelectorProperties.spnCountry);
		regionSelectorProperties.spnState				= document.getElementById(regionSelectorProperties.spnState);
		regionSelectorProperties.htmlSelectState		= document.getElementById(regionSelectorProperties.htmlSelectState);
		regionSelectorProperties.htmlSelectCountry		= document.getElementById(regionSelectorProperties.htmlSelectCountry);
		
		
		var originalStateId = regionSelectorProperties.htmlInputHiddenState.value;
		
		regionSelectorProperties.htmlSelectCountry.regionSelectorProperties = regionSelectorProperties;
		regionSelectorProperties.htmlSelectCountry.onchange = htmlSelectCountryOnChange;
		
		regionSelectorProperties.htmlSelectState.regionSelectorProperties = regionSelectorProperties;
		regionSelectorProperties.htmlSelectState.onchange = htmlSelectStateOnChange;
		 
		if (regionSelectorProperties.htmlInputHiddenCountry.value == '')
			regionSelectorProperties.htmlInputHiddenCountry.value = 0;
		if (regionSelectorProperties.htmlInputHiddenState.value == '')
			regionSelectorProperties.htmlInputHiddenState.value = 0;		
		
		var countryValue = regionSelectorProperties.htmlInputHiddenCountry.value;
		var optionsLength = regionSelectorProperties.htmlSelectCountry.options.length - 1;
		var selectedCountryId = regionSelectorProperties.htmlSelectCountry.options[optionsLength].value;
		
		while(countryValue != selectedCountryId)
		{
			optionsLength--;
			selectedCountryId = regionSelectorProperties.htmlSelectCountry.options[optionsLength].value;
		}
		regionSelectorProperties.htmlSelectCountry.options[optionsLength].selected	= true;	
		
		regionSelectorProperties.htmlSelectCountry.onchange();
		regionSelectorProperties.initComplete = true;
	}	
}

//alert('Country.js script parsed OK!');
