//
// RegionControl.js:
//
// This class supports a particular bit of functionality on a "region control", this is a drop-down box in which a state or province is selected.
// When a country is changed in a country drop-down box we can call the updateRegionControl method and re-populate the region drop-down box
// for the regions that are available in that country. 
//
// If you want a region label on your page to change from "States" to "Provinces" or whatever use <div id="regionLabel"></div>.
//
// This file create a global RegionControl object, ready to use called "gRegionControl".
//
// See webdev/asep/catalog.cfm for an example of how this might be used.
//

     
// RegionControl:
//
// This is the init method.
//
function RegionControl()
{
    this.regionName	    = "";		        // This holds the region name label, such as "state".  
    this.objRegionInfo  = new Object();     // An object created by the AJAX page, containing region information.
    this.AJAXutil       = new AJAXutil();   // This object handles the AJAX call.

    this.baseAJAXpath	= 'jsCode/ajax/';   // Don't have a live URL yet

} // RegionControl


// RegionControl::handleUpdateRegionControl:
//
// We use the region information that we retrieved using AJAX, to update the region control.
//
RegionControl.prototype.handleUpdateRegionControl = function() 
{
	var numOfRegionNodes    = this.objRegionInfo.regions.length;
	var regionForm		    = this.objRegionInfo.formName;							// Put the global formName in the local formName just to make the variable name shorter.

	this.regionName	        = this.objRegionInfo.regionName;						// Set the region name.

	document.getElementById("regionLabel").innerHTML    = this.regionName + ':';	// Change the region label to "states" or "provinces" or whatever.
	
	document.forms[regionForm].region.options.length	= (numOfRegionNodes+1);	   	// Update the select control with the list of regions.
	document.forms[regionForm].region.options[0].text	= "Please select one";		// This is always the first option.	
	
	for(var i=1; i <= numOfRegionNodes; i++)
		document.forms[regionForm].region.options[i].text = this.objRegionInfo.regions[i-1].region;

} // handleUpdateRegionControl


// RegionControl::UpdateRegionControl:
//
// This does an AJAX call to get the regions associated with this country.
//
RegionControl.prototype.UpdateRegionControl = function(formName, countryName, callback) 
{
	var url = this.baseAJAXpath + 'getRegionNames.cfm?formName=' + formName + "&countryName=" + countryName; 
	
	this.AJAXutil.httpRequest("GET", url, callback, true);      // The result will be returned to the handleResponse function.
			
} // RegionControl::UpdateRegionControl



