/************************************************************************************************************
Acknowledge:
Alf Magne Kalleland, 2006
Owner of DHTMLgoodies.com
************************************************************************************************************/		
var ajax = new sack();

function getStateList(sel)
{
	var countryCode = sel.options[sel.selectedIndex].value;
	var stateSelect = document.getElementById('r_state');
	var citySelect = document.getElementById('r_city');
	
	
	var optgroups = stateSelect.getElementsByTagName("optgroup"); //get optgroups (this is for UK only)
	
	stateSelect.innerHTML = null; // Empty state select box
	citySelect.innerHTML = null; // Empty state select box
	citySelect.options[0] = new Option(initialSelectionText, initialSelectionValue);
	
	//loop thru optgroups to remove (UK only)
	for (i=optgroups.length-1; i>=0; i--) {
		stateSelect.removeChild(optgroups[i]);
	}
	
	if(countryCode.length>0){
		ajax.requestFile = '/extensions/data/getStates.cfm?countryCode='+encodeURIComponent(countryCode)+'&initialSelectionText='+initialSelectionText+'&initialSelectionValue='+initialSelectionValue+'&languageID='+languageID;	// Specifying which file to get
		ajax.onLoading = whenLoadingState; // Disable State dropdown and add 'Loading...' option
		ajax.onLoaded = whenLoadedState; // Enable State dropdown and remove 'Loading...' option
		ajax.onError = handleError;
		ajax.onCompletion = createStates;	// Specify function that will be executed after file has been found
		ajax.runAJAX();		// Execute AJAX function
	}
}

function getCityList(country,state)
{
	var countryCode = country.options[country.selectedIndex].value;
	var stateCode = state.options[state.selectedIndex].value;
	
	var stateSelect = document.getElementById('r_state');
	var citySelect =  document.getElementById('r_city');
	//var optgroups = stateSelect.getElementsByTagName("optgroup"); //get optgroups (this is for UK only)
	
	citySelect.innerHTML = null; // Empty state select box
	
	//loop thru optgroups to remove (UK only)
	//for (i=optgroups.length-1; i>=0; i--) {
	//	stateSelect.removeChild(optgroups[i]);
	//}
	
	if(stateCode.length>0){
		ajax.requestFile = '/extensions/data/getCities.cfm?countryCode='+encodeURIComponent(countryCode)+'&stateCode='+encodeURIComponent(stateCode)+'&initialSelectionText='+initialSelectionText+'&initialSelectionValue='+initialSelectionValue+'&languageID='+languageID;	// Specifying which file to get
		ajax.onLoading = whenLoadingCity; // Disable State dropdown and add 'Loading...' option
		ajax.onLoaded = whenLoadedCity; // Enable State dropdown and remove 'Loading...' option
		ajax.onError = handleError;
		ajax.onCompletion = createCities;	// Specify function that will be executed after file has been found
		ajax.runAJAX();		// Execute AJAX function
	}
}

function createStates()
{
	var obj = document.getElementById('r_state');
	eval(ajax.response);	// Executing the response from Ajax as Javascript code	
}

function createCities()
{
	var obj = document.getElementById('r_city');
	eval(ajax.response);	// Executing the response from Ajax as Javascript code	
}

function whenLoadingState() 
{
	clearErrorHandling(); // deletes error message if there was previously an error
	
	o = document.getElementById('r_state');
	o.disabled = true;
	o.options[0] = new Option('Loading', '');
	
}

function whenLoadedState() 
{
	o = document.getElementById('r_state');
	o.options[0] = null;
	o.disabled = false;
	
}

function whenLoadingCity() 
{
	clearErrorHandling(); // deletes error message if there was previously an error
	
	o = document.getElementById('r_city');
	
	o.disabled = true;
	o.options[0] = new Option('Loading', '');
	
}

function whenLoadedCity() 
{
	o = document.getElementById('r_city');
	o.options[0] = null;
	o.disabled = false;
	
}

function handleError()
{
	var e = document.getElementById('ErrorHandling'); 
	e.innerHTML = "<p>There was a problem populating select box. Please try again.</p>";
}

function clearErrorHandling()
{
	var e = document.getElementById('ErrorHandling'); 
	e.innerHTML = "";
}
