/**
 *
 *	AJAX Object and Functions
 * 	-------------------------
 * 	Creating of AJAX object, sending AJAX requests and handling the response - requires "php/ajax.php"
 *
 **/
 

	// For looping requests, time between loop
	var timeInt = 2500;

	// Sending methods - POST/GET
	var sendMethod = 'POST';




	// Creation of the XMLHttpRequest object
	function initHTTP(){
	
		// Blank variable
		var http = false;
		
		// Mozilla, Safari, etc.
		if (window.XMLHttpRequest) {
		
			http = new XMLHttpRequest();
		
		// Internet Explorer
		} else if (window.ActiveXObject) {
			try {
				// Internet Explorer 7+
				http = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
				try {
					// Internet Explorer 5+
					http = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e) {
					
				}
			}
		}
		
		// Can't create object! Old browser...
		if (!http) {
			alert('Your web browser is not compatible');
			return false;
		}
		
		// Return AJAX object
		return http; 
		
	}
	
	
	
	
	// Handling of AJAX response 	
	function handleResponse(reqObj, divtag, fetchloop, params, fetchscriptpath) {
		
		// AJAX ready state:
		//
		//		0 = Uninitialized
		// 	 	1 = Loading
		//		2 = Finished loading
		//		3 = Almost ready for use
		//		4 = Loaded
		
		if(reqObj.readyState == 4){
		
			// Page request status
			//
			//		200 = OK
			//		403 = Forbidden
			//		404 = Page Not Found
			//		500	= Internal Server Error

			if(reqObj.status == 200) {

				// Fill the select div tag with returned content
				document.getElementById(divtag).innerHTML = reqObj.responseText;
				
			}
			
			// If request is a loop, repeat the same request in the time interval
			if(fetchloop == true) {
				setTimeout("sendRequest('"+params+"', '"+divtag+"', true, '"+fetchscriptpath+"')", timeInt);
			} 
			
		}
		
	}
	
	
	// Send AJAX Request function
	function sendRequest(parameters, divtag, loop, scriptpath) {
	
		// Initialise the AJAX object
		var ajax = initHTTP(); 
		
		// If making a loop request
		if(loop == true) {
	
			// When ready call the handleResponse function (and loop request)
			ajax.onreadystatechange = function() { 
				handleResponse(ajax, divtag, true, parameters, scriptpath); 
			}
			
		} else {
			
			// When ready call the handleResponse function
			ajax.onreadystatechange = function() { 
				handleResponse(ajax, divtag, false, parameters, scriptpath); 
			}
		
		}
		
		
		// If sending via POST
		if(sendMethod == 'POST') {
			
			// Open the connection to the server side script
			ajax.open(sendMethod, scriptpath, true);
			
			// HTTP Headers
			ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			ajax.setRequestHeader("Content-length", parameters.length);
			ajax.setRequestHeader("Connection", "Keep-Alive"); 
			
			// Send request parameters to the server side script
			ajax.send(parameters+"&ts="+(new Date().getTime()));
			
		} 
		
		// If sending via GET
		if(sendMethod == 'GET') {
			
			// Open the connection to the server side script
			ajax.open(sendMethod, scriptpath+"?"+parameters+"&ts="+(new Date().getTime()), true);
			
			// HTTP Headers
			ajax.setRequestHeader("Content-length", parameters.length);
			ajax.setRequestHeader("Connection", "Keep-Alive"); 
			
			// Send nothing as information is passed through URL above
			ajax.send(null);
			
		} 
		
	
	}
