var http = createRequestObject(); 

/* The following function creates an XMLHttpRequest object... */
function createRequestObject(){

	http = null

	var request_o; //declare the variable to hold the object.
	var browser = navigator.appName; //find the browser name
	if(browser == "Microsoft Internet Explorer"){
		/* Create the object using MSIE's method */
		request_o = new ActiveXObject("Microsoft.XMLHTTP");
	}else{
		/* Create the object using other browser's method */
		request_o = new XMLHttpRequest();
	}
	return request_o; //return the object
}


/* GetAjaxData */
function getAjaxData(requestPage, requestAction, requestQuerystring){

	var requestingPage = requestPage + '?action=' + requestAction + '&' + requestQuerystring;

	/* Create the request. */
	http.open('get', requestingPage, true);

	/* Define a function to call once a response has been received. This will be our handleProductCategories function that we define below. */
	switch(requestAction){
		case 'primaryRegionOnChange':

			http.onreadystatechange = populateSecondaryRegionOnChange; 
			break;
			
	}	

	/* Send the data. We use something other than null when we are sending using the POST method. */
	http.send(requestQuerystring);

}

function populateSecondaryRegionOnChange(){

	/* Make sure that the transaction has finished. The XMLHttpRequest object has a property called readyState with several states:
		0: Uninitialized
		1: Loading
		2: Loaded
		3: Interactive
		4: Finished 
	*/

	if(http.readyState == 4){ 
		
		if(http.status == 200) {

			/* Populate the Sale Select Object */
			var myXMLResponse = http.responseXML.getElementsByTagName('Region');
			
			document.forms[0].UserRegionId.options.length=0;
			
			if (myXMLResponse.length > 0) {
			
				document.forms[0].UserRegionId.disabled = 0;
			
				for(var r=0; r < (myXMLResponse.length); r++) { 

					document.forms[0].UserRegionId.options[r]=new Option(myXMLResponse[r].getElementsByTagName('RegionName')[0].firstChild.data, myXMLResponse[r].getElementsByTagName('RegionId')[0].firstChild.data, false)

				}
			
			} else {
			 document.forms[0].UserRegionId.disabled = 1;
				document.forms[0].UserRegionId.options[0]=new Option("There are no states/provinces available for this region","",false)
		
			}
			
		} else {

			//** Error Code

		}
	}

}