function leftTrim(sString){
	while (sString.substring(0,1) == ' '){
		sString = sString.substring(1, sString.length);
	}
	return sString;
}

function replaceInControl(oControl,rPattern,sReplacement){
	// EXAMPLES:
	
	// Just remove the newlines
	// replaceInControl(oYourControl,/\n/g,'');
	
	// Replace newlines with "{{NL}}"
	// replaceInControl(oYourControl,/\n/g,'{{NL}}');
	
	// Replace any number of whitespaces or newlines with a single space.
	//replaceInControl(oYourControl,/\s+/g,' ');
	
	return oControl = oControl.replace(rPattern,sReplacement);
	
}


/* BEGIN - AJAX SCRIPT FOR POSTING TO  */
function AjaxCall(callPage, qstring, setDivID, aCallBack){
	var xmlHttp;
	xmlHttp=GetXmlHttpObject();

	if (xmlHttp==null){
		alert ("Your browser does not support AJAX!");
		return;
	} 
  	// prep calling page with sid to prevent caching
	callPage=callPage+"?sid="+Math.random();
	// open call to posting page
	xmlHttp.open("POST",callPage,true);
	
	// attach callback function to xmlhttprequest object 
	xmlHttp.onreadystatechange=function(){ 
		if (xmlHttp.readyState==4){
			// set container div with contents
			if(setDivID){
				setThis = document.getElementById(setDivID);
				setThis.innerHTML = xmlHttp.responseText;
			}
			// user defined call back on success
			if(aCallBack){
				eval(aCallBack);
			}
		}
	}
	
	if(qstring.indexOf("=")==-1&&qstring!=''){
		
		// qstring is a form - no equal sign for variables, parse it into a URL string of variables
		var frm = document[qstring];
		frmL = frm.elements.length;
		frmE = frm.elements;
		postFile = "";
		for(i=0;i<frmL;i++){
			
			if(frmE[i].options){
				allVals = "";
				for(z=0;z<frmE[i].options.length;z++){
					if(frmE[i].options[z].selected&&frmE[i].options[z].value!=''){
						allVals = allVals + frmE[i].options[z].value + ',';
					}
				}
				if(allVals.charAt(allVals.length-1)==','){
					allVals = allVals.substring(0, allVals.length - 1);
				}
				postFile = postFile + "&" + frmE[i].name + "=" + escape(allVals);
			}
			else{
				if(frmE[i].type=="radio"||frmE[i].type=="checkbox"){
				//alert("radio!");
					if(frmE[i].checked==true){
						postFile = postFile + "&" + frmE[i].name + "=" + escape(frmE[i].value);
					}
				}
				else{
					postFile = postFile + "&" + frmE[i].name + "=" + escape(frmE[i].value);
				}
			}
		
		}
		qstring = postFile;
	}
	// replace difficult characters here:
	//****************************
	// 1. Need to replace %A0 with fully encoded &nbsp; - JavaScript URL is NOT being decoded properly on backend.
	qstring = qstring.replace(/%A0/g, "%26nbsp%3B");  // double (and more) spaces
	
	qstring = "&" + qstring;
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xmlHttp.send(qstring);
	
}
// AJAX CALL FOR HANDLING XML RETURNED DATA
function AjaxCallXML(callPage, qstring, customCallBack){
	var xmlHttp;
	xmlHttp=GetXmlHttpObject();

	if (xmlHttp==null){
		alert ("Your browser does not support AJAX!");
		return;
	} 
  	// prep calling page with sid to prevent caching
	callPage=callPage+"?sid="+Math.random();
	// open call to posting page
	xmlHttp.open("POST",callPage,true);
	
	// attach callback function to xmlhttprequest object 
	xmlHttp.onreadystatechange=function(){ 
		if (xmlHttp.readyState==4){
			// USE CUSTOM CALLBACK TO TRIGGER PROCESSING OF RETURNED XML
			uncleanXML = xmlHttp.responseText;
			cleanedXML = replaceInControl(uncleanXML,/\n|\\n|\r|\t/g,'');
			cleanedXML = leftTrim(cleanedXML);
			myCallBackEval = customCallBack + "(\'" + cleanedXML + "\')";
			eval(myCallBackEval);
			/*
			if(cleanedXML == 1){
				eval(myCallBackEval);
			}
			else{
				//document.getElementById("message").innerHTML = myCallBackEval;
				document.syncData.tempholder.value = myCallBackEval;
			}
			*/
		}
	}
	
	if(qstring.indexOf("=")==-1&&qstring!=''){
		
		// qstring is a form - no equal sign for variables, parse it into a URL string of variables
		var frm = document[qstring];
		frmL = frm.elements.length;
		frmE = frm.elements;
		postFile = "";
		for(i=0;i<frmL;i++){
			
			if(frmE[i].options){
				allVals = "";
				for(z=0;z<frmE[i].options.length;z++){
					if(frmE[i].options[z].selected&&frmE[i].options[z].value!=''){
						allVals = allVals + frmE[i].options[z].value + ',';
					}
				}
				if(allVals.charAt(allVals.length-1)==','){
					allVals = allVals.substring(0, allVals.length - 1);
				}
				postFile = postFile + "&" + frmE[i].name + "=" + escape(allVals);
			}
			else{
				if(frmE[i].type=="radio"||frmE[i].type=="checkbox"){
				//alert("radio!");
					if(frmE[i].checked==true){
						postFile = postFile + "&" + frmE[i].name + "=" + escape(frmE[i].value);
					}
				}
				else{
					postFile = postFile + "&" + frmE[i].name + "=" + escape(frmE[i].value);
				}
			}
		
		}
		qstring = postFile;
	}
	// replace difficult characters here:
	//****************************
	// 1. Need to replace %A0 with fully encoded &nbsp; - JavaScript URL is NOT being decoded properly on backend.
	qstring = qstring.replace(/%A0/g, "%26nbsp%3B");  // double (and more) spaces
	
	qstring = "&" + qstring;
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xmlHttp.send(qstring);
	
}
/* END - AJAX SCRIPT FOR POSTING TO  */

function GetXmlHttpObject(){
	var xmlHttp=null;
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e){
 		// Internet Explorer
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e){
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}

/* XML PARSER - CREATES AN XML DOCUMENT IN JS */

function XMLDoc(xmlString){
	
	if (document.implementation.createDocument) {
        var parser = new DOMParser()
        doc = parser.parseFromString(xmlString, "text/xml")
    // MSIE
    } else if (window.ActiveXObject) {
        doc = new ActiveXObject("Microsoft.XMLDOM")
        doc.async="false"
        doc.loadXML(xmlString)
    }
    return doc;
	/*
	//Internet Explorer
	try {
		xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
	}
	catch(e){
		//Firefox, Mozilla, Opera, etc.
		try {
			xmlDoc=document.implementation.createDocument("","",null);
		}
		catch(e){
			alert(e.message);
			return;
		}
	}
	return xmlDoc;
	*/
}

