var gRandomStr;

function formSubmit(theForm, action, successCB, param, failCB, failParam) {
	if(!theForm) {
		alert("Form not found");
		return;
	}
	var params="";
	var num = theForm.elements.length;
	var formId=null;
	var elemArr = new Array();
	var elemCount = 0;
	for (var i=0; i<num; ++i) {
		var elem = theForm.elements[i];
		if (elem.getAttribute("post") || elem.checked) {
			if (params != "") { params = params + '&'; }
			params = params + elem.name + '=' + escape(encodeURI(elem.value));
			if (elem.name == 'formId') { formId = elem.value; }
			elemArr[elemCount]=elem.name;
			elemCount++;
		}
	}
	for (var i=0; i<elemCount; ++i) { // clear the error messages
		var idname = formId+"_error_"+elemArr[i];
		var elem = document.getElementById(idname);
		if (elem) { elem.innerHTML=""; }
/* this will happen only for checkboxes that are not posted - ignore
		else { alert(idname+" not found"); return false; }
*/
	}

//alert("action="+action+":"+params);
	document.getElementById(formId+'_status').innerHTML='<img src="images/loading.gif" />';
	makePOSTRequest(action, params, function(data, responseCode) {
		if (http_request.readyState == 4) {
			if (http_request.status == 200) {
				processFormResponse(http_request.responseText, successCB, param, failCB, failParam);
			} else {
				alert('There was a problem with the request:'+http_request.status+':');
			}
		}
	});
}

function processFormResponse(data, successCB, param, failCB, failParam) {
//alert("data="+data);
	var lines = data.split("\n");
	var formId, formStatus="", errorCode;
	var errors = new Object();
	for (i=0; i<lines.length; ++i) {
		var idx=lines[i].indexOf("=");
		var name = lines[i].substr(0,idx);
		var value = lines[i].substr(idx+1);
		if (name == "formId") { formId = value; }
		if (name == "status") { formStatus = value; }
		if (name == "errorCode") { errorCode = value; }
		if (name.indexOf("error.") == 0) {
			var fieldName = name.substr(name.indexOf(".")+1);
			errors[fieldName] = value;
		}
	}
	if (!formId) { postLog(2, "Error processing form Response:"+data); alert("An error has occurred and has been reported to our technical team. Please try again later."); }
	else
	if (!errorCode) { alert("errorCode is missing"); }
	else {
		document.getElementById(formId+'_status').innerHTML=formStatus;
		for (fieldName in errors) {
			var elem = document.getElementById(formId+"_error_"+fieldName);
			if (!elem) { alert("Missing "+formId+"_error_"+fieldName+". Please refresh your browser and try again."); }
			else { document.getElementById(formId+"_error_"+fieldName).innerHTML=errors[fieldName]; }
		}
	}

	if (errorCode == 0) {
		if (successCB != null) {
			if (param == null) { successCB(); }
			else { successCB(param); }
		}
	} else {
		if (failCB != null) {
			if (failParam == null) { failCB(); }
			else { failCB(failParam); }
		}
	}
}

function postLog(type, msg) {
params = "log="+type;
params = params + '&msg=' + escape(encodeURI(msg));
makePOSTRequest("/postLog.php", params, function(data, responseCode) { });
}

function showDiv(divId) {
	document.getElementById(divId).style.display='block';
}

function hideDiv(divId) {
	document.getElementById(divId).style.display='none';
}

function hideShowDiv(hideDiv, showDiv) {
	document.getElementById(hideDiv).style.display='none';
	document.getElementById(showDiv).style.display='block';
}

function showMsg(msg) {
	document.getElementById('msgBar').innerHTML=msg;
}

function openWin(url) {
    window.open(url, "_blank", "status=no,top=50,left=200,resizable=yes,scrollbars=1",false);
}

function setRandomStr(str) { gRandomStr=str; }

function getRandomStr() { return gRandomStr; }

function isEmpty(val) { return (val == undefined || val == ""); }

String.prototype.startsWith = function(str)
{return (this.match("^"+str)==str)}

String.prototype.endsWith = function(str)
{return (this.match(str+"$")==str)}

function getTimeInterval(tsec) {
m = tsec/60;
h = m/60;
d = h/24;
if (d>1) {
dh = h%24;
return daystr(parseInt(d))+" and "+hourstr(parseInt(dh));
} else if (h>1) {
hm=m%60;
return hourstr(parseInt(h))+" and "+minstr(parseInt(hm));
} else if (m>1) {
return minstr(parseInt(m));
} else {
return tsec+" seconds";
}
}

function daystr(d) { if (d > 1) return d+" days"; else return d+" day"; }
function hourstr(h) { if (h > 1) return h+" hours"; else return h+" hour"; }
function minstr(m) { if (m > 1) return m+" minutes"; else return m+" minute"; }
