/* 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(); 
/* Function called to get the product categories list */

function displayDetails(id,stage,mode){
		
	/* 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. 
	*/
	document.getElementById('IdDetails').value = id;
	elementname = "displayDetails" + document.getElementById('IdDetails').value;
	elementname1 = "showDetails" + document.getElementById('IdDetails').value;
	elementname2 = "Row" + document.getElementById('IdDetails').value;
	symbol = "displaySymbol" + document.getElementById('IdDetails').value;
	if(document.getElementById(elementname1).style.display =="")
	{
		document.getElementById(elementname).innerHTML = "";
		document.getElementById(elementname).style.display ="none";
		document.getElementById(elementname1).style.display ="none";
		document.getElementById(elementname2).style.display ="none";
		document.getElementById(symbol).innerText = "+";
		return false;
	}
	http.open('get', 'index.php?stage=' + stage + '&mode=' + mode + '&id=' 
			+ id);
	/* Define a function to call once a response has been received. This will be our
		handleProductCategories function that we define below. */
	http.onreadystatechange = handleDetails;
	/* Send the data. We use something other than null when we are sending using the POST
		method. */
	http.send(null);
}
function handleDetails(){
	/* 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;
		elementname = "displayDetails" + document.getElementById('IdDetails').value;
		elementname1 = "showDetails" + document.getElementById('IdDetails').value;
		elementname2 = "Row" + document.getElementById('IdDetails').value;
		document.getElementById(elementname).style.display="";
		document.getElementById(elementname1).style.display="";
		document.getElementById(elementname2).style.display="";
		/* 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(elementname).innerHTML = response;
	}
	else if(http.readyState == 1)
	{
		document.getElementById(elementname).innerHTML = "Loading...";
	}
	
	
}

function getProducts(category){	id = category.options[category.selectedIndex].value;		http.open('get', 'index.php?stage=products&mode=getCategoryProducts&id='+ id);	/* Define a function to call once a response has been received. This will be our		handleProductCategories function that we define below. */	http.onreadystatechange = handleProducts; 	/* Send the data. We use something other than null when we are sending using the POST		method. */	http.send(null);}function handleProducts(){	elementname = "products";	if(http.readyState == 4){ //Finished loading the response		var response = http.responseText;		document.getElementById(elementname).innerHTML=response;	}}
function getProductParents(category)
{
	cat_id = category.options[category.selectedIndex].value;
	
	http.open('get', 'index.php?stage=products&mode=getCategoryProductParents&category_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 = handleProductParents; 
	/* Send the data. We use something other than null when we are sending using the POST
		method. */
	http.send(null);
}
function handleProductParents()
{
	elementname = "products";
	if(http.readyState == 4){ //Finished loading the response
		var response = http.responseText;
		document.getElementById(elementname).innerHTML=response;
	}
}



function updateStatus(id,currentstatus){
	/* 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. 
	*/
	statuselementname = "newstatus"+id;
	document.getElementById('sid').value = id;
	if(document.getElementById(statuselementname).value != "")
		currentstatus = document.getElementById(statuselementname).value;
	http.open('get', 'index.php?stage=categories&mode=updateSubcategoryStatus&id=' 
			+ id + '&current_status=' + currentstatus);
	/* Define a function to call once a response has been received. This will be our
		handleProductCategories function that we define below. */
	http.onreadystatechange = handleStatus; 
	/* 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 handleStatus(){
	/* 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 */
		elementname = "updateNewStatus" + document.getElementById('sid').value;
	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. */
		//get updated status
		var response = http.responseText;
		
		//parse the xml doc
		/* 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. */
//			alert(response);

		statuselementname = "newstatus" + document.getElementById('sid').value;
		if(response == 1)
			document.getElementById(elementname).innerHTML = "Make inactive";
		else
			document.getElementById(elementname).innerHTML = "Make active";

		document.getElementById(statuselementname).value=response;
		alert("Subcategory status updated successfully.");
	}
	else if(http.readyState == 1)
	{
		document.getElementById(elementname).innerHTML = "Updating...";
	}
	
}


