/* 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 get_data(user_input, c_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. */
		//var Current = document.companyForm.companySelect.selectedIndex;
		//user_input = document.companyForm.companySelect.options[Current].value;
	http.open('get', 'internal_request2.php?action=get_data&stage=' 
			+ user_input + '&category=' + c_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 get_data2(user_input, c_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. */
		//var Current = document.companyForm.companySelect.selectedIndex;
		//user_input = document.companyForm.companySelect.options[Current].value;
		
	http.open('get', 'internal_request2.php?action=get_data2&stage=' 
			+ user_input + '&category=' + c_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 get_cat(c_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. */
		//var Current = document.companyForm.companySelect.selectedIndex;
		//user_input = document.companyForm.companySelect.options[Current].value;
		
	http.open('get', 'internal_request2.php?action=get_cat&category=' + c_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 get_sec(c_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. */
		//var Current = document.companyForm.companySelect.selectedIndex;
		//user_input = document.companyForm.companySelect.options[Current].value;
		
	http.open('get', 'internal_request2.php?action=get_sec&sector=' + c_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 called to handle the list that was returned from the internal_request.php file.. */
function handleProducts(){
	/* 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;
		/* 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('listing').innerHTML = response;
	}
}
/*This is the show / hide menu code */
var status = 1;
menus_status_array = new Array ();
menus_status_array["subMenu1"] = "show";
menus_status_array["subMenu2"] = "show";
menus_status_array["sec_sub1"] = "hide";
menus_status_array["sec_sub2"] = "hide";
menus_status_array["sec_sub3"] = "hide";
menus_status_array["cat_sub1"] = "hide";
menus_status_array["cat_sub2"] = "hide";
menus_status_array["cat_sub3"] = "hide";
menus_status_array["cat_sub4"] = "hide";
menus_status_array["cat_sub5"] = "hide";


		

function showHideMenu(the_id){
	var obj = document.getElementById(the_id);
	var arrow = the_id + "arrow";
	var pm = the_id + "PM";
	
	if(the_id.charAt(0) == "c"){
		var c_id = the_id.charAt(7);
		get_cat(c_id);
	}
	if(the_id.charAt(0) == "s"){
		var c_id = the_id.charAt(7);
		if(the_id == "subMenu1" || the_id == "subMenu2"){
		}else{
		get_sec(c_id);
		}
	}

	if( menus_status_array[the_id] != "hide" ){
		obj.style.display = "none";
		menus_status_array[the_id] = "hide";
		if(the_id.charAt(3) == "_"){
			document[pm].src = "img/plus.gif";
		} else {
			document[arrow].src = "img/titleArrowOff.gif";
		}
		
	}
	else {
		obj.style.display = "block";
		menus_status_array[the_id] = "show";
		if(the_id.charAt(3) == "_"){
			document[pm].src = "img/minus.gif";
		} else {
			document[arrow].src = "img/titleArrowOn.gif";
		}
		
		
	}
}
