//
// The following function creates an XMLHttpRequest object
// we call this function when the page loads
//
function createRequestObject(){
	//declare the variable to hold the object.
	var request_o;
	//find the browser name
	var browser = navigator.appName; 
	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 the object
	return request_o; 
}

/* 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.
*/



// Function called to get the new page
function loadPage(url, containerId, method, formId){
	// The variable http will hold our new XMLHttpRequest object.
	http = createRequestObject(); 
	/* 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. 
	*/
	// lock the form
	
	if(method == ''){
		// open up the page
		http.open('get', url);
		// Define a function to call once a response has been received. This will be our
		// printPage function that we define below.
		http.onreadystatechange=function() {printPage(containerId)};
		// Send the data. We use something other than null when we are sending using the POST method.
		http.send(null);
	}else if (method == 'post'){
		formProcessLock('processing', 'SubmitButton');
		// get all the variables from the form in a usable format
		var parameters = appendVariables(formId);
		// Define a function to call once a response has been received. This will be our
		// printPage function that we define below.
		http.onreadystatechange=function() {printPage(containerId)};
      	http.open('POST', url, true);
      	http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      	http.setRequestHeader("Content-length", parameters.length);
      	http.setRequestHeader("Connection", "close");
		http.send(parameters);
	}else if (method == 'get'){
		// get all the variables from the form in a usable format
		parameters = appendVariables(formId);
		// open up the page
		http.open('get', url + "?" + parameters);
		// printPage function that we define below.
		http.onreadystatechange=function() {printPage(containerId)};
		// 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 printPage(containerId){	
	/* 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
		// Status chacks to see whether the url exists or not
		if (http.status == 200) {
			// set the variable to contain all the returned results
			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(containerId).innerHTML = response;
		}
	}
}
// this will delete the page from the selected div
function unloadPage(containerId){
	document.getElementById(containerId).innerHTML = '';
}
// this function will append variables in a format usable by javascript for this
function appendVariables(formId){
	// reset the parameters
	var parameters = '';
	// find the form
	theForm = document.getElementById(formId);
	// loop through each name-value pair and append it to the parameters
	for(var i=0; i<theForm.elements.length; i++){
		// make sure we don't have an undefined object the first element seems to come up like this
		if (theForm.elements[i].name != undefined){
			// if it's a checkbox make sure it's checked
			if (theForm.elements[i].checked == true & theForm.elements[i].type == "checkbox"){
				// append it
				parameters += theForm.elements[i].name + "=" + theForm.elements[i].value;
				// add & if it not at the end
				if (i<theForm.elements.length-1){
					parameters += "&";
				}
			}else if(theForm.elements[i].type != "checkbox"){
				// append it
				parameters += theForm.elements[i].name + "=" + theForm.elements[i].value;
				// add & if it not at the end
				if (i<theForm.elements.length-1){
					parameters += "&";
				}
			}			
		}
    }
	// return the string
	return (parameters);
}
// this function loads in javascript or css to use on the loaded pages
function loadobjs(){
	if (!document.getElementById)
	return
		for (i=0; i<arguments.length; i++){
			var file=arguments[i]
			var fileref=""
			if (loadedobjects.indexOf(file)==-1){ //Check to see if this object has not already been added to page before proceeding
			if (file.indexOf(".js")!=-1){ //If object is a js file
				fileref=document.createElement('script')
				fileref.setAttribute("type","text/javascript");
				fileref.setAttribute("src", file);
			}else if (file.indexOf(".css")!=-1){ //If object is a css file
				fileref=document.createElement("link")
				fileref.setAttribute("rel", "stylesheet");
				fileref.setAttribute("type", "text/css");
				fileref.setAttribute("href", file);
			}
		}
		if (fileref!=""){
			document.getElementsByTagName("head").item(0).appendChild(fileref)
			loadedobjects+=file+" " //Remember this object as being already added to page
		}
	}
}
// this function wil turn on a loading gram and disable the submit button
function formProcessLock(displayId, submitButton){
	if (document.layers) document.layers[displayId].display="block";
    else document.getElementById(displayId).style.display="block";
	
	tempButton = document.getElementById(submitButton);
	tempButton.disabled=true;
}