//######################################################################
//# 
//# Copyright 1994-2002 Onyx Software Corporation. All rights reserved.
//#
//# Description: Client side script to handle refreshing region list
//#              based on country selection.
//#
//# Notes      : Most of this file will be used by IE 4 and above only.
//#              Only the refresh() function is used by other browsers.
//#
//######################################################################

var country;
var region;
var sCountryOptionList;
var sRegionOptionList;
var url = "OnyxCountryRegionRS.asp";

function initializePage() {

	// retrieve country list
	getCountryList(country);
	setCountryList();
	
	// retrieve region list, based on country (select current region if
	// appropriate)
	getRegionList(country, region);
	setRegionList();
}

// Gets the list of countries for the country dropdown
function getCountryList(sCountry) {
	var oResult = RSExecute(url,"GetCountryList", sCountry);
	if (oResult.return_value.indexOf("OPTION") != -1) {
		sCountryOptionList = oResult.return_value;
	}
	else {		
		sCountryOptionList = "<OPTION VALUE=''> </OPTION>";
	}
}

// Gets the list of regions for a country for the region dropdown
function getRegionList(sCountry, sRegion) {
	var oResult = RSExecute(url,"GetRegionList", sCountry, sRegion);
	if (oResult.return_value.indexOf("OPTION") != -1) {
		sRegionOptionList = oResult.return_value;
	}
	else {
		sRegionOptionList = "<OPTION VALUE=''> </OPTION>";
	}
}

// Refreshes the region list when the country changes
function refreshRegionList() {
	country = document.forms['member'].Country.value;
	getRegionList(country, region);
	setRegionList();
}

// Puts the list of countries available into the dropdown
function setCountryList() {
	var sHTML = "<SELECT STYLE='position:relative; width: 100%;' name='Country' id='Country' onchange='refreshRegionList()'>" + sCountryOptionList + "</SELECT>";
	document.forms['member'].Country.outerHTML = sHTML;
	country = document.forms['member'].Country.options.value;
}

// Puts the list of regions available into the dropdown
function setRegionList() {
	var sHTML = "<SELECT STYLE='position:relative; width: 100%;' name='Region' id='Region'>" + sRegionOptionList + "</SELECT>";
	document.forms['member'].Region.outerHTML = sHTML;
	
	// Commented out so that when an existing user changes country by
	// mistake, region will default back to the value in the DS if they
	// change the country back.  Though this means the variable doesn't 
	// store the current value on the form as the country variable does, 
	// this is OK, because we don't need to reference region except during
	// page load - after that it is all driven by the country dropdown, and
	// the save procedure will grab the value from the form, not the region
	// variable.
	
	//region = document.forms['member'].st.options.value;
}


// This function is called to refresh the region list when the user changes the
// country in the dropdown.  In order to preserve the user's other entries, we will
// post the form and allow the ProcessFields function to read the data from the
// form and render it again.

function refresh() {

	var fForm = document.forms["member"];
	var oProcForm = document.createElement("input");
	oProcForm.setAttribute("type", "hidden")
	oProcForm.setAttribute("value", "True")
	oProcForm.setAttribute("id", "NetscapeRefresh")
	oProcForm.setAttribute("name", "NetscapeRefresh")
	fForm.appendChild(oProcForm);
	fForm.submit();

}

