
/**
 * Function makeGetRequest
 *
 * @desc Sends an AJAX Get Request to the given URL. The Response is handled by the given returnFunction
 * @author Wes Wright
 * @param String returnFunction - The Name of the Function that will handle the AJAX Request Return value
 * @param String url - The URL the AJAX Request is sent to
 * @return void
 */
function makeGetRequest(url, waitingFunction, responseFunction) {
	
	/* local variables */
    var httpRequest = false;

    if (window.XMLHttpRequest) {
        httpRequest = new XMLHttpRequest();
        if (httpRequest.overrideMimeType) {
            httpRequest.overrideMimeType("text/xml");
        }
    } 
    else if (window.ActiveXObject) { // IE
        try {
            httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
        } 
        catch (e) {
            try {
                httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
            } 
            catch (e) {}
        }
    }
    if (!httpRequest) {
        alert("Cannot create an XMLHTTP instance");
        return false;
    }    
    
	httpRequest.onreadystatechange = function() {
		processRequestResults(httpRequest, waitingFunction, responseFunction);
	};   
	
    httpRequest.open("GET", url, true);
    httpRequest.send(null);
    
}/* End Function makeGetRequest */


/**
 * Function processRequestResults
 *
 * @desc Handles the Return from the AJAX Request
 * @author Wes Wright
 * @param Object httpRequest - The AJAX Request Object
 * @param String waitingFunction - The Function to call to execute whilst the AJAX Request is waiting for a Response
 * @param String responseFunction - The Function to which the Response Value is passwed once the AJAX Request is complete
 * @return void
 */
function processRequestResults(httpRequest, waitingFunction, responseFunction) {	
	
	if (httpRequest.readyState == 4) {
	 			
		/* Check for the Complete Status */ 	
		if (httpRequest.status == 200) {
			
			/* Send the Returned Value to the Reponse Function */	
			responseFunction(httpRequest.responseText);
		}				
	}
	else {
		
		/* Call the Waiting Function */
		waitingFunction();			
	}
	
}/* End Function processRequestResults */





/* this is a JavaScript file that contains many functions required for ajax*/
function ajax_request() {
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		http_request = new XMLHttpRequest();
	}
	else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	
	if (!http_request) {
		return false;
	}
	return http_request;
}

/**
 * XMLHttpRequest in a nutshell
 * 
 *
 * author Yee Hong CHAN
 * 
 * This is a function call httpRequest which takes a object that has the following functions and variables
 *
 * obj.prototype.postVar = function(); 
 *    param - void
 *    return - String postvar - the post variable string
 *    it return the post var query string, "" on error (will trigger the postError function)
 *
 * 
 * obj.prototype.callback = function();
 *    param - void
 *    return - void
 *    The callback function when the request is finish
 * 
 *
 * obj.prototype.postError = function();
 *    param - void
 *    return - void
 *    The error handler function when postVar returns ""
 *
 *
 * obj.http_request = null; //variable of type XMLHttpRequest
 * function constructor () {
 *		if (this.http_request == null ) {
 *			this.http_request = ajax_request();
 *		}
 *		else {
 *			this.http_request.abort();
 *		}
 * }
 */
function httpRequest(obj_handle, url) {
	if (obj_handle == null && !obj_handle.http_request) { 
		//the object does not exist, cannot obtain an HttpRequest object, use normal post and submit the form
		return true;
	}
	else{
		// input checking is done in the post var function if an error is trigger, postVar func should return ""
		postvar = obj_handle.postVar();
		if (postvar.length>0) {
			//add default var
			postvar = "&ajax=1"+postvar;
			
			// set up the call back function
			obj_handle.http_request.onreadystatechange = function(){obj_handle.callback();};
			obj_handle.http_request.open("POST", url, true);
			obj_handle.http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			obj_handle.http_request.send(postvar);
		}
		else {
			obj_handle.postError();
		}
		return false;
	}
}


function formData2QueryString(docForm) {
	var submitContent = '';
	var formElem;
	var lastElemName = '';

	for (i = 0; i < docForm.elements.length; i++) {
		formElem = docForm.elements[i];
		switch (formElem.type) {
			// Text fields, hidden form elements
			case 'select-one':
				// single select
				submitContent += formElem.name+'='+escape(formElem.value)+'&';
			break;
			
			case 'select-multiple': // multiple assume the name of the multiple select be  element.name = somename[]
				count = formElem.options.length;
				for(j=0; j<count; j++){
					if (formElem.options[j].selected) {
						submitContent += formElem.name+'='+escape(formElem.options[j].value)+"&";
					}
				}
			break;
			
			// Radio buttons
			case 'radio':
				if (formElem.checked) {
					submitContent += formElem.name + '=' + escape(formElem.value) + '&'
				}
				break;
    
			// Checkboxes - assuming the checkbox name is element.name = somename[]
			case 'checkbox':
				if (formElem.checked) {
					submitContent += formElem.name+'='+escape(formElem.value)+"&";
				}
				break;
			
			//'text','hidden','password','textarea'
			default :
				submitContent += formElem.name+'='+escape(formElem.value)+'&';
				break;
		}
	}
	// Remove trailing separator
	submitContent = submitContent.substr(0, submitContent.length - 1);
	return submitContent;
}