/* The following function creates an XMLHttpRequest object... */

function createRequestObject(){
	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
}

/* You can get more specific with version information by using 
	parseInt(navigator.appVersion)
	Which will extract an integer value containing the version 
	of the browser being used.
*/

/* The variable http will hold our new XMLHttpRequest object. */
var http  = createRequestObject(); 
var http2 = createRequestObject();
var http3 = createRequestObject();
var http4 = createRequestObject();
var http5 = createRequestObject();
var http6 = createRequestObject();
var http7 = createRequestObject();



function initialize(country,state,sku) {
	getCountries(country);
	loadStates(country,state);
	getPrices(sku);
	getFees(sku);
}


/* Function called to get the product categories list */
function getStates(country){
	/* Create the request. The first argument to the open function is the method (POST/GET),
		and the second argument is the url... 
		document contains references to all items on the page
		We can reference document.form_category_select.select_category_select and we will
		be referencing the dropdown list. The selectedIndex property will give us the 
		index of the selected item. 
	*/
	http.open('get', '/ajaxFunctions.php?action=get_states&countryCode=' + country.options[country.selectedIndex].value);
	/* Define a function to call once a response has been received. This will be our
		handleProductCategories function that we define below. */
	http.onreadystatechange = handleStates; 
	/* Send the data. We use something other than null when we are sending using the POST
		method. */
	http.send(null);
}

/* Function called to handle the list that was returned from the internal_request.php file.. */
function handleStates(){
	/* 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){ //Finished loading the response
		// only if status is "OK"
        if (http.status == 200) {
			/* We have got the response from the server-side script,
				let's see just what it was. using the responseText property of 
				the XMLHttpRequest object. */
			var response = http.responseText;
			/* And now we want to change the product_categories <div> content.
				we do this using an ability to get/change the content of a page element 
				that we can find: innerHTML. */
			document.getElementById('stateCode').innerHTML = response;
		}
	}
}


function getCountries(country){
	http.open('get', '/ajaxFunctions.php?action=get_countries&countryCode=' + country);
	http.onreadystatechange = handleCountries; 
	http.send(null);
}

function handleCountries(){
	if(http.readyState == 4){ //Finished loading the response
		// only if "OK"
        if (http.status == 200) {
			var response = http.responseText;
			document.getElementById('countryCode').innerHTML = response;
		}
	}
}

function loadStates(country,state){
	http2.open('get', '/ajaxFunctions.php?action=get_states&countryCode=' + country + '&stateCode=' + state);
	http2.onreadystatechange = handleLoadStates; 
	http2.send(null);
}

function handleLoadStates(){
	if(http2.readyState == 4){ //Finished loading the response
		// only if "OK"
        if (http2.status == 200) {
			var response = http2.responseText;
			document.getElementById('stateCode').innerHTML = response;
		}
	}
}


/* Function called to get the product categories list */
function getPrices(sku){
	/* Create the request. The first argument to the open function is the method (POST/GET),
		and the second argument is the url... 
		document contains references to all items on the page
		We can reference document.form_category_select.select_category_select and we will
		be referencing the dropdown list. The selectedIndex property will give us the 
		index of the selected item. 
	*/
    if (document.getElementById('rgProduct_0').checked)
	  {
	  var prod_val = document.getElementById('rgProduct_0').value;
	  }
    if (document.getElementById('rgProduct_1').checked)
	  {
	  var prod_val = document.getElementById('rgProduct_1').value;
	  }
	if (document.getElementById('rgProduct_2').checked)
	  {
	  var prod_val = document.getElementById('rgProduct_2').value;
	  }

	if (sku==null) 
		{ http3.open('get', '/ajaxFunctions.php?action=get_prices&productGroup=' + prod_val);}
	else
		{ http3.open('get', '/ajaxFunctions.php?action=get_prices&productGroup=' + prod_val + '&sku=' + sku);}
		
	/* Define a function to call once a response has been received. This will be our
		handleProductCategories function that we define below. */
	http3.onreadystatechange = handlePrices; 
	/* Send the data. We use something other than null when we are sending using the POST
		method. */
	http3.send(null);
}

function handlePrices(){
	if(http3.readyState == 4){ //Finished loading the response
		// only if "OK"
        if (http3.status == 200) {
			var response = http3.responseText;
			document.getElementById('orderwrapper').innerHTML = response;
		}
	}
}

function getFees(sku){
	http4.open('get', '/ajaxFunctions.php?action=get_setup&sku=' + sku );
	http4.onreadystatechange = handleGetSetupFee; 
	http4.send(null);
	http5.open('get', '/ajaxFunctions.php?action=get_discount&sku=' + sku );
	http5.onreadystatechange = handleGetDiscount; 
	http5.send(null);
	http6.open('get', '/ajaxFunctions.php?action=get_tax&sku=' + sku );
	http6.onreadystatechange = handleGetTax; 
	http6.send(null);
	http7.open('get', '/ajaxFunctions.php?action=get_total&sku=' + sku );
	http7.onreadystatechange = handleGetTotal; 
	http7.send(null);
}

function handleGetSetupFee(){
	if(http4.readyState == 4){ //Finished loading the response
		// only if "OK"
        if (http4.status == 200) {
			var response = http4.responseText;
			document.getElementById('setup').innerHTML = response;
		}
	}
}
function handleGetDiscount(){
	if(http5.readyState == 4){ //Finished loading the response
		// only if "OK"
        if (http5.status == 200) {
			var response = http5.responseText;
			document.getElementById('discount').innerHTML = response;
		}
	}
}
function handleGetTax(){
	if(http6.readyState == 4){ //Finished loading the response
		// only if "OK"
        if (http6.status == 200) {
			var response = http6.responseText;
			document.getElementById('tax').innerHTML = response;
		}
	}
}
function handleGetTotal(){
	if(http7.readyState == 4){ //Finished loading the response
		// only if "OK"
        if (http7.status == 200) {
			var response = http7.responseText;
			document.getElementById('total').innerHTML = response;
		}
	}
}

