/** 
	This file contains the methods used to connect with the client webpage 
	and the server
*/

//-------------General Functions ------------//

/**
	This function creates the XMLHTTPRequest object as needed.
*/
var xmlHttp;
var poststr;
function GetXmlHttpObject(){
	var objXMLHttp=null;
	if (window.XMLHttpRequest){
		objXMLHttp=new XMLHttpRequest();
	}
	else if (window.ActiveXObject){
		objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	return objXMLHttp;
}

//This encodes the item and returns the string
function encodeItem(attribute,value) {
	if(value == ""){
		value="null";
	}
	return attribute + "=" + encodeURI(encodeURIComponent(value));
}

// ----------------- End General Functions -----------------------//

/**
	This function sends the request to the page specified:
	type = GET or POST
	address = URL to send data to (just path/filename)
	data = The GET data (Starting with ?) or the POST string encoded URI
	async = Whether or not to send an async request (true/false) (default = true)
*/
function AJAX(type,address,data,async,onload){
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null){
		alert ("Browser does not support HTTP Request");
		return;
	} 
	xmlHttp.onreadystatechange=function(){
	  if(xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
    	// Get the data from the server's response
    	xmldoc = xmlHttp.responseXML;
    	onload.call(this, xmldoc);
	  }	
	}
	if(type == "POST"){
		xmlHttp.open(type,address + "&sid=" + Math.random(),async);
		xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlHttp.setRequestHeader("Content-length", data.length);
		xmlHttp.setRequestHeader("Connection", "close");
		xmlHttp.send(data);
	}
	else if(type == "GET"){
		xmlHttp.open(type,address + data + "&sid=" + Math.random(),async);
		xmlHttp.send(null);
	}
}

/**
	This function checks if a node has children, and if it does, returns them.
	node is an XML node and action is a default value, or null if 
	you want nothing returned.
*/
function returnNodeTextIfExists(node,action){
	alert(typeof node);
	if(node.hasChildNodes()){
		alert("inside function" + node.childNodes[0].nodeValue);
		return node.childNodes[0].nodeValue;
	}
	else{
		if(action == "null"){
			return;
		}
		else{
			return action;
		}
	}
}

/**
	This function sends a file name back to a text box on another page.
*/
function selectFile(filename,elementid){
	if (window.opener && !window.opener.closed){
    	window.opener.document.getElementById(elementid).value = filename;
    }
  	window.close();
}