/* 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
}

/* The variable http will hold our new XMLHttpRequest object. */
var http = createRequestObject();

/* Function called to get the product categories list */
function getLocations(){
	/* 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', '/includes/ajax.php?action=getLocations&country_id='
			+ document.getElementById('country').value);
	/* Define a function to call once a response has been received. This will be our
		handleProductCategories function that we define below. */
	http.onreadystatechange = handleLocations;
	/* 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 handleLocations(){
	/* 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
		/* clear the locationsbox */
		while (document.getElementById('locations').length > [[0]])
        document.getElementById('locations').remove([[0]]);

		/* 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.responseXML.getElementsByTagName('returnvalues')[0];
		var items = response.getElementsByTagName("location");
		for(var i=0; i<items.length; i++){

			var item = items[i];

			var lid = item.getElementsByTagName("lid")[0].firstChild.nodeValue;
			var lname = item.getElementsByTagName("name")[0].firstChild.nodeValue;
			document.getElementById('locations').options[document.getElementById('locations').length] = new Option(lname, lid);
		}
		/* 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('ajaxLocations').innerHTML = response;
	}
}

/* Function called to get the product categories list */
function getUserDetails(date_id,user_id,my_id){
	/* 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', '/includes/ajax.php?action=getUserDetails&date_id='+date_id+'&user_id='+user_id+'&my_id='+my_id);
	/* Define a function to call once a response has been received. This will be our
		handleProductCategories function that we define below. */
	http.onreadystatechange = handleUserDetails;
	/* 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 handleUserDetails(){
	/* 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
		/* 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;
		document.getElementById("ajaxDetails").innerHTML = response;
		document.getElementById("ajaxDetails").style.display = 'block';
	}
}
function get_category_list(cat_id){
	/* 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', '../includes/ajax.php?action=get_category_list&cat_id='+cat_id);
	/* Define a function to call once a response has been received. This will be our
		handleProductCategories function that we define below. */
	http.onreadystatechange = handleCategoryList;
	/* 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 handleCategoryList(){
	/* 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
		/* 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;
		document.getElementById("ajaxDetails").innerHTML = response;
	}
}
function get_state_list(country_id){
	/* 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', '../includes/ajax.php?action=get_state_list&country='+country_id);
	/* Define a function to call once a response has been received. This will be our
		handleProductCategories function that we define below. */
	http.onreadystatechange = handleStateList;
	/* Send the data. We use something other than null when we are sending using the POST
		method. */
	http.send(null);
}
function get_state_list_main(country_id){
	/* 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', 'includes/ajax.php?action=get_state_list&country='+country_id);
	/* Define a function to call once a response has been received. This will be our
		handleProductCategories function that we define below. */
	http.onreadystatechange = handleStateList;
	/* 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 handleStateList(){
	/* 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
		/* 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;
		document.getElementById("ajaxDetails").innerHTML = response;
	}
}
function get_state_edit_list(country_id, user_id){
	/* 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', '../includes/ajax.php?action=get_state_edit_list&country='+country_id+'&user='+user_id);
	/* Define a function to call once a response has been received. This will be our
		handleProductCategories function that we define below. */
	http.onreadystatechange = handleStateEditList;
	/* 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 handleStateEditList(){
	/* 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
		/* 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;
		document.getElementById("ajaxDetails").innerHTML = response;
	}
}
