﻿function AJAX_createRequestObject(){
	var req;
	if (window.XMLHttpRequest) {
		// browser has native support for XMLHttpRequest object
		req = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		// try XMLHTTP ActiveX (Internet Explorer) version
		req = new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		alert('no request object');
	}
	return req;
}
var http;
/* FUNCTION TO FILL AN HTML CONTAINER */
var container_id;
var this_script;
function AJAX_fillContainerGet(script_name,parameters,div_id) {
	// assign global variables
	container_id = div_id;
	this_script = script_name;
	// create a request object
	http = AJAX_createRequestObject();
	http.open('GET', script_name+"?"+parameters,true);
	http.onreadystatechange = AJAX_fillContainer;
	http.send(null);
}
function AJAX_fillContainerPost(script_name,parameters,div_id) {
	// assign global variables
	container_id = div_id;
	this_script = script_name;
	// create a request object
	http = AJAX_createRequestObject();
	//http.abort;
	http.open('POST', script_name,true);
	//http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
	http.onreadystatechange = AJAX_fillContainer;
	http.send(parameters);
}
function AJAX_fillContainer(){
	if( http.readyState == 4 ) {
		var pos = http.responseText.indexOf('<js>');
		if ( pos == -1) {
			document.getElementById(container_id).innerHTML = http.responseText;
		} else {
			// there is some javascript to evaluate
			//alert(http.responseText.substr(pos+10));
			document.getElementById(container_id).innerHTML = http.responseText.substr(0,pos);
			eval(http.responseText.substr(pos+4));
		}
		// set the form focus
		// all first page forms must be called "form1"
		if ( document.forms['form1'] ) {
			for ( var i = 0; i < document.forms['form1'].elements.length; i++ ) {
				var t = document.forms['form1'].elements[i].type;
				if ( t == "text" || t == "textarea" || t == "file" || t == "password" ) {
					document.forms['form1'].elements[i].focus();
					break;
				}
			}
		}
	}
	// make sure the "page" div is visible
	//bhOnLoad();
}
/* FUNCTIONS TO VALIDATE SOMETHING */
var fail_message;
function AJAX_getValidateGet(script_name,parameters,message) {
	fail_message = message;
	http = AJAX_createRequestObject();
	http.open('GET', script_name+"?"+parameters,true);
	http.onreadystatechange = AJAX_showValidation;
	http.send(null);
}
function AJAX_getValidatePost(script_name,parameters,message) {
	fail_message = message;
	http = AJAX_createRequestObject();
	//http.abort;
	http.open('POST', script_name,true);
	//http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
	http.onreadystatechange = AJAX_showValidation;
	http.send(parameters);
}
function AJAX_showValidation() {
	if( http.readyState == 4 ) {
		if ( http.responseText == "0" ) {
			alert(fail_message);
		}
	}
}
/* FUNCTIONS TO PERFORM A TASK */
function AJAX_runScript() {
	/*
	*	PARAMETERS
	*	0	-	Address (URL) or script name
	*	1	-	Parameters without the ? in the form of name1=val1&name2=val2&...
	*	2	-	Form field value to evaluate. must take the form of
	* 					"document.formname.fieldname.value"
	*			in this case parameters must end with the field name as in name=
	*				eg. name1=val1&name2=
	*			the 3rd parameter value will be appended to the parameter list
	*	3	-	if "E" the response text is assumed to be javascript and will be evaluated.
	*			the default value is "S" to Show an alert box with the result
	*/
	var args=AJAX_runScript.arguments;
	var parameters;
	if ( args[1] != null ) {
		parameters = args[1];
	} else {
		parameters = null;
	}
	if ( args[2] != null ) {
		parameters += escape(eval(args[2]));
	}
	if ( args[3] == null ) {
		args[3] = "S";
	}
	parameters = encodeURI(parameters);
	http = AJAX_createRequestObject();
	//http.abort;
	http.open('POST', args[0],true);
	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	if ( args[3] == "E" ) {
		http.onreadystatechange = AJAX_evalScriptResponse;
	} else {
		http.onreadystatechange = AJAX_showScriptResponse;
	}
	http.send(parameters);
}
function AJAX_showScriptResponse() {
	if( http.readyState == 4 ) {
		if ( http.responseText != "" ) {
			alert(http.responseText);
		}
		AjaxEnabled = true;
	}
	return false;
}
function AJAX_evalScriptResponse() {
	if( http.readyState == 4 ) {
		if ( http.responseText != "" ) {
			eval(http.responseText);
			//alert(http.responseText);
		}
		AjaxEnabled = true;
	}
	return false;
}
/* FORM FIELD DUPLICATION CHECK */
var duplicate_form_name;
var duplicate_field_name;
var duplicate_message;
function AJAX_isDuplicate(check_form,check_field,table,tablecolumn,tableidcolumn,idvalue,message) {
	/*
		check_form		-	the name of the html form
		check_field		-	the name of the form field whose value we are checking
		table			-	name of the SQL table we are checking in
		tablecolumn		-	name of the table column we are checking in
		tableidcolumn	-	name of the unique SQL table's row identifier column
		idvalue			-	the current unique identifier value (zero of it is an INSERT form)
		message			-	message to report when a duplicate has been encountered
	*/
	var fieldvalue = document.forms[check_form].elements[check_field].value;
	var parameters = 'table='+table+'&tablecolumn='+tablecolumn+'&tableidcolumn='+tableidcolumn+'&idvalue='+idvalue+'&fieldvalue='+escape(fieldvalue);
	duplicate_form_name = check_form;
	duplicate_field_name = check_field;
	duplicate_message = message;

	http = AJAX_createRequestObject();
	//http.abort;
	http.open('post','check_for_duplicate.php');
	//http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
	http.onreadystatechange = AJAX_handleDuplicateResponse;
	http.send(parameters);

}
function AJAX_handleDuplicateResponse() {
	if( http.readyState == 4 ) {
		if ( http.responseText == "Y" ) {
			alert(duplicate_message);
			document.forms[duplicate_form_name].elements[duplicate_field_name].value="";
			document.forms[duplicate_form_name].elements[duplicate_field_name].focus();
		}
	}
}
/* END OF FORM FIELD DUPLICATION CHECK */

/* FORM FIELD VALIDATION CHECK */
var validate_form_name;
var validate_field_name;
var validate_clear_field;
function AJAX_validateField(check_form,check_field,glo_var,session_id,clear_field) {
	/*
		check_form		-	the name of the html form
		check_field		-	the name of the form field whose value we are checking
		glo_var			-	name of the global variable to check value against
							(this must be stored in the session global variables field)
		session_id		-	current session Id
		clear_field		-	Y or N - delete the form field value if not validated
	*/
	var fieldvalue = document.forms[check_form].elements[check_field].value;
	var parameters = 'a=1&s='+session_id+'&var='+glo_var+'&fieldvalue='+escape(fieldvalue);
	validate_clear_field = clear_field;
	validate_form_name = check_form;
	validate_field_name = check_field;

	http = AJAX_createRequestObject();
	//http.abort;
	http.open('post','ajax_checks.php');
	http.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
	http.onreadystatechange = AJAX_handleValidateField;
	http.send(parameters);

}
function AJAX_handleValidateField() {
	if( http.readyState == 4 ) {
		if ( http.responseText == "N" ) {
			alert("Security Code was not entered correctly");
			if ( validate_clear_field == "Y" ) {
				document.forms[validate_form_name].elements[validate_field_name].value="";
			}
			document.forms[validate_form_name].elements[validate_field_name].focus();
		}
	}
}
/* END OF FORM FIELD VALIDATION CHECK */
var ajax = {
	request : '',
	params : '',
	showWait : false,
	sendMethod : 'POST',
	http : Object,
	init : function(request,showWait,sendMethod) {
		if ( sendMethod == null ) {
			this.sendMethod = "GET";
		} else {
			this.sendMethod = sendMethod;
		}
		if ( this.sendMethod == "GET" ) {
			this.request = request;
		} else {
			var pos = request.indexOf('?');
			if (pos == -1) {
				this.request = request;
				this.params = '';
			} else {
				this.request = request.substring(0,pos);
				this.params = request.substr(pos+1);
			}
		}
		if ( showWait == null ) {
			this.showWait = false;
		} else {
			this.showWait = showWait;
		}
		this.createRequestObject();
		this.sendRequest();
	},
	createRequestObject : function() {
		if ( navigator.appName == "Microsoft Internet Explorer" ) {
			this.http = new ActiveXObject("Microsoft.XMLHTTP");
		} else {
			this.http = new XMLHttpRequest();
		}
	},
	sendRequest : function() {
		if ( this.showWait ) {
			// display the Wait icon
			document.getElementById('wait').style.visibility="visible";
		}
		if ( this.sendMethod == 'POST' ) {
			this.http.open('POST', this.request,true);
		} else {
			this.http.open('GET', this.request,true);
		}
		this.http.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
		this.http.onreadystatechange = ajax.getResponse;
		if ( this.sendMethod == 'POST' ) {
			this.http.send(this.params);
		} else {
			this.http.send(null);
		}
	},
	getResponse : function() {
		if ( ajax.http.readyState == 4 ) {
			if ( ajax.showWait ) document.getElementById('wait').style.visibility="hidden";
			eval(ajax.http.responseText);
			ajax.http = null;
		}
	}
}
function AJAX(request,showWait,sendMethod) {
	/*	PARAMETERS
		request		-	php script + parameters to run on server
		showWait	-	true/false display the wait icon (default: false)
		sendMethod	-	"GET" or "POST" (default: "POST")
		All parameters must already be urlencoded
	*/
	ajax.init(request,showWait,sendMethod);
}
