﻿var xmlHttp;
var formName;

function saveForm(q, otherTarget, conf, input1, input2) {

		xmlHttp=GetXmlHttpObject();
		if (xmlHttp==null) {
			alert ("Your browser does not support AJAX!");
			return;
		}
		var thisForm = document.getElementById(q);
		if (otherTarget && otherTarget != "") {
			formName = otherTarget;
		} else {
			formName = q + "Target";
		}
		var url="/display/ajaxTarget.php";
		for (var i=0;i<thisForm.length;i++)	{
			if (thisForm.elements[i].name != "") {
				if (i==0) {
					var qStr=thisForm.elements[i].name+"="+encodeURIComponent(thisForm.elements[i].value);
				} else {
					qStr=qStr+"&"+thisForm.elements[i].name+"="+encodeURIComponent(thisForm.elements[i].value);
				}
			}
		}
		if (conf) {
			if (input1) {
				var input1Value = thisForm.elements[input1].value;
				conf = conf + input1Value;
			}
			var confirmSave = confirm(conf);
		} else {
			var confirmSave = "0";
		}

	if (confirmSave) {
		if (otherTarget != "reloadPage") {
			xmlHttp.onreadystatechange=showSavedForm;
		}
		xmlHttp.open("POST",url,true);
		xmlHttp.setRequestHeader("Content-Type", 'application/x-www-form-urlencoded; charset=UTF-8');
		xmlHttp.send(qStr);
	} else {
		return false;
	}

}
function showSavedForm() {
	if (xmlHttp.readyState==4) {
		if (xmlHttp.responseText === "Session timed out") { // checks for timeout
			window.location.reload();
		} else {
		document.getElementById(formName).innerHTML=xmlHttp.responseText;
		}
	}
}
