/*----------------------------------------------------------------------------------------------
 *          FILE = $RCSfile$
 *----------------------------------------------------------------------------------------------
 *      Booking Number:         ?
 *
 *      Project:                Landing Gear Test Rigs
 *
 *      Created By:             Steve O'Brien, on 26/07/2006
 *
 *      Version:                $Revision$
 *
 *      Last Modified By:       $Author$ on $Date$
 *
 *      External Document:      None
 *----------------------------------------------------------------------------------------------
 * Description:
 *
 * Javascript ajax functions. (asyncronous javascript and XML) - 
 * the main purpose is to provide a communicative layer
 * between javascript and php enabling php function to be executed without reloading a page.
 *
 * main syntax javascript: sndReq('Function called in response.php', variable header string e.g. 
 * var1=1&var2=2&var3=3 etc).
 *
 * This also enables PHP on the server to call javascript functions:
 *
 * response.php: response.php should echo a string similar to the following:
 *
 * id_of_div_to_populate | html content of the div 
 * <divs>id_of_another_div_to_populate | htnl content of div   (extra divs can be populated using the <divs> tag)
 * <jsCallFun>name_of_javascript_function_to_execute 
 *
 * (the javascript function is executed after 
 * the div has been populated, enabling the function to control newly populated info
 *
 *----------------------------------------------------------------------------------------------
 */

	/*window.onerror = handleError; // safety net to trap all errors
	function handleError(message, URI, line) {
		alert('ERROR: ' + message + ' ' + URI + ' Line:' + line);// alert the user that this page may not respond properly
		return true; // this will stop the default message
	}*/
	
	function preload(divId){
		document.getElementById(divId).innerHTML = 'Loading...';
	}
	
	function createRequestObject() {
		var ro;
		var browser = navigator.appName;
		if(browser == "Microsoft Internet Explorer"){
			ro = new ActiveXObject("Microsoft.XMLHTTP");
		}else{
			ro = new XMLHttpRequest();
		}
		return ro;
	}
	
	var http = createRequestObject();
	
	
	function getQueryString(obj) {
		var getstr = "";
		for (i=0; i<obj.elements.length; i++) {
			//alert(obj.elements[i].tagName);
			//getQueryString(obj.elements[i]);
			
			if (obj.elements[i].tagName == "INPUT") {
				//alert("inside input")
				//alert(obj.elements[i].type)
				if (obj.elements[i].type == "text") {
					//alert("in ere")
					getstr += obj.elements[i].name + "=" + obj.elements[i].value + "&";
				}
				if (obj.elements[i].type == "checkbox") {
					if (obj.elements[i].checked) {
						getstr += obj.elements[i].name + "=" + obj.elements[i].value + "&";
				   	} else {
						getstr += obj.elements[i].name + "=&";
				   	}
				}
				if (obj.elements[i].type == "radio") {
					if (obj.elements[i].checked) {
						getstr += obj.elements[i].name + "=" + obj.elements[i].value + "&";
				   	}
				}
				if (obj.elements[i].type == "hidden") {
					getstr += obj.elements[i].name + "=" + obj.elements[i].value + "&";
				}
			}   
			if (obj.elements[i].tagName == "SELECT") {
				var sel = obj.elements[i];
				getstr += sel.name + "=" + sel.options[sel.selectedIndex].value + "&";
			}
			if (obj.elements[i].tagName == "TEXTAREA") {
				getstr += obj.elements[i].name + "=" + obj.elements[i].value + "&";
			}
		}
		//alert(getstr);
		return getstr;
	}
	
	
	
/*----------------------------------------------------------------------------
 *          Function = sndReq
 *----------------------------------------------------------------------------
 *      Created By:             Steve O'Brien, on 26/07/2006
 *----------------------------------------------------------------------------
 * Description:
 *
 * Function to call from html page
 *----------------------------------------------------------------------------
 */
	function sndReq(action, val, loadDivId, phpPage, formId) {
		//sets a null string default value
		//phpPage defines a different response.php page to process the results.
		
		var val = (val == null) ? "" : val;
		var loadDivId = (loadDivId == null) ? "" : loadDivId;
		var formId = (formId == null) ? "" : formId;
		var phpPage = (phpPage == null) ? "" : phpPage;

		if(document.getElementById('notloading') != null) document.getElementById('notloading').style.display = 'none';
		if(document.getElementById('loading') != null) document.getElementById('loading').style.display = 'block';

		var formObjQueryString = ""
		//get form data
		if (formId != ""){
			formObjQueryString = getQueryString(document.getElementById(formId));
			alert(formObjQueryString);
	}
		
		if (loadDivId != ""){
			//check if element exists
			var loadDiv = document.getElementById(loadDivId)
			if (loadDiv != null){
				document.getElementById(loadDivId).innerHTML = '<img src="/document/themes/blue/images/ajax-loader-darkblue.gif" /> loading...';
			}
		}

		//alert('lgtest website is currently being upgraded,  DONT PANIC!!! some strange messages will follow, however your data WILL be SAFE!');
		//alert(val);
		
		//the true value enables the request to work behind the scenes allowing the main window to remain resposive (THE WHOLE POINT OF AJAX!)
		if (phpPage != ""){
			http.open('post', phpPage + '?action='+action+'&val='+val,true);
		}else{
			http.open('post', '/includes/ajax/response.php?action='+action+'&val='+val, true);
		}
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http.onreadystatechange = handleResponse;
		http.send(null);
	}
/*----------------------------------------------------------------------------
 *          Function = printElt
 *----------------------------------------------------------------------------
 *      Created By:             Steve O'Brien, on 26/07/2006
 *----------------------------------------------------------------------------
 * Description:
 *
 * Finds the div id in the current html page, ensures its visible to the user
 * then populates it with the html content.
 * searches for javascript functions and executes them after content has been printed
 *----------------------------------------------------------------------------
 */
	function printElt(element) {
		
		var jsfns;
		var doJsFun;
		var display = new Array();
		
		var pos = element.indexOf('|');
		var el = element.substring(0,pos);
					
		html = element.substring(pos+1);
					
		if (html.indexOf('<jsCallFun>') != -1){
			jsfns = html.split('<jsCallFun>');
			html = jsfns[0];
			doJsFun = true;
		}else{
			html = element.substring(pos+1);
			doJsFun = false;
		}
		if (el != ''){
			//if the div is hidden then assume we want it to be visible to highlight the new html inserted otherwise leave it alone!
			if(document.getElementById(el).style.display == 'none') {
				document.getElementById(el).style.display = 'block'; //show the div if hidden
			}
			document.getElementById(el).innerHTML = html;
		}
		
		if(document.getElementById('notloading') != null)document.getElementById('notloading').style.display = 'block';
		if(document.getElementById('loading') != null)document.getElementById('loading').style.display = 'none';
		
		if(doJsFun){
			eval(jsfns[1]);
		}
	}
/*----------------------------------------------------------------------------
 *          Function = handleResponse
 *----------------------------------------------------------------------------
 *      Created By:             Steve O'Brien, on 26/07/2006
 *----------------------------------------------------------------------------
 * Description:
 *
 * Handles the response from response.php determins the div id's from the div html content.
 *----------------------------------------------------------------------------
 */
	function handleResponse() {
		if(http.readyState == 4){
			var response = http.responseText;
			var divs = new Array();
			
			if(response.indexOf('|' != -1)) {
				//alert(response);
				divs = response.split('<divs>');
				//alert(divs);
				for (var n = 0; n < divs.length; n++){
					printElt(divs[n]);
				}
			}
		}
	}