function v2(a) {
	thermoff = true;
	if (a instanceof Array) { 
		try {
		while (flag = a.shift()) {
			switch (flag) {
			case 'J': 
				eval(a.shift()); 
				break;
			case 'JX': 
				if (eval(a.shift())) 
					eval(a.shift());
				else
					dumb = a.shift(); 
				break;
			case 'C': 
				divname = a.shift();
				elem = document.getElementById(divname);
				if (!elem) {
					dumb = a.shift();
					alert('Location not found on screen: ' + divname + " = " + dumb);
				} else {
					if (elem.id == "bar") thermoff = false;
					elem.innerHTML = a.shift();
				}
				break;
			case 'CX': 
				divname = a.shift();
				elem = document.getElementById(divname);
				if (!elem) {
					dumb = a.shift();
				} else {
					if (elem.id == "bar") thermoff = false;
					elem.innerHTML = a.shift();
				}
				break;
			}
		}
		} catch (err) {
			txt = "The response from the server is invalid (" + err + ")\n";
			txt += a;
			alert(txt);
		}
	} else {
		alert("Problem in v2: " + a);
	}
	if (thermoff)
		document.getElementById('delay').innerHTML = "";
}

function replace(s) {
	ix = s.indexOf("~");
	if (ix == 0) {
		alert("Error: Ajax call failed = " + s);
		return;
	}
	elemid = s.substring(0,ix);
	content = s.substring(ix+1);
	document.getElementById(elemid).innerHTML = content;
}
function replace_all(s) {
	ix = s.indexOf("^")
	if (ix == 0) {
		alert("Error: Ajax call failed = " + s);
		return;
	}
	while (ix > 0) {
		ts = s.substring(0,ix);
		remains = s.subtring(ix+1);
		replace(ts);
		s = remains;
		ix = s.indexOf("^");
	}
	replace(s);
}	


var currenturl = unescape(window.location.href);
function delete_result(x) {
	if (x.length == 0) {
		window.location = currenturl;
	} else {
		if (currenturl.indexOf("?") > 0)
			window.location = currenturl + '&infomsg=' + escape(x);
		else
			window.location = currenturl + '?infomsg=' + escape(x);
	}
}
function delete_record(clazz, pid) {
	agent.call('ajax_functions.php','delete_record','delete_result', clazz, pid);
}
function delete_record_go(clazz, pid, nurl) {
	ix = currenturl.indexOf("?");
	if (ix > 0)
		currenturl = currenturl.substring(0,ix);
	if (nurl.length > 0)
		currenturl = currenturl + "?" + nurl;
	agent.call('ajax_functions.php','delete_record','delete_result', clazz, pid);
}

function stopspam() {
	var req = parseInt(document.forms[0].gm_spam1.value) + parseInt(document.forms[0].gm_spam2.value);
	var sol = parseInt(document.forms[0].gm_spam3.value);
	if (sol == req) 
		return true;
	alert("You must calculate the correct value to send this message");
	document.forms[0].gm_spam3.select();
	document.forms[0].gm_spam3.focus();
	return false;
}

// Workout the screen size
var viewportwidth = 0;
var viewportheight = 0;
function setviewport() {
	if (typeof window.innerWidth != 'undefined') {
	  viewportwidth = window.innerWidth;
	  viewportheight = window.innerHeight;
	} else {
	  viewportwidth = document.body.clientWidth;
	  viewportheight = document.body.clientHeight;
	}
}

function editpara(divid, ptid, comp) {
	agent.call('ajax_functions.php','editpara','v2', divid, ptid, comp);
}
function savepara(divid, ptid, comp) {
	elem = document.ep_form;
	if (elem) {
		savecancel = elem.save_cancel.value;
		if (savecancel == "cancel") {
			agent.call('ajax_functions.php','disppara','v2', divid, ptid, comp);
		} else {
			parms = form_to_array("ep_form");
			agent.call('ajax_functions.php','savepara','v2', divid, ptid, comp, parms);
		}
	}
}


function pagetitle(pageid) {
	agent.call('ajax_functions.php','pagetitle','v2', pageid);
}
function pagesave(pageid) {
	elem = document.ep_form;
	if (elem) {
		savecancel = elem.save_cancel.value;
		if (savecancel == "cancel") {
			agent.call('ajax_functions.php','pagedisp','v2', pageid);
		} else {
			parms = form_to_array("ep_form");
			agent.call('ajax_functions.php','pagesave','v2', pageid, parms);
		}
	}
}

// Utilities
function form_to_array(frmname) {
	var result = new Array();
	frm = eval("document." + frmname);
	if (!frm) {
		result.push("ERR: Form " + frmname + " not found");
		return result;
	}
	for (ix = 0; ix < frm.elements.length; ix++) {
		elem = frm.elements[ix];
		if (!elem.disabled) {
			result.push("~~" + elem.name);
			switch (elem.type) {
			case "select":
				result.push(elem.options[elem.selectedIndex].value);
				break;
			case "checkbox":
				if (elem.checked)
					result.push('true');
				else
					result.push('false');
				break;
			default:
				result.push(elem.value);
				break;
			}
		}
	}
	return result;
}

function go(url) {
	window.location = url;
}

// Retrieves the dialog content from the server
function mydialog() {
	afx = arguments[0];
	switch (arguments.length) {
	case 1:
		agent.call('ajax_functions.php','mydialog','pop_dialog', afx);
		break;
	case 2:
		agent.call('ajax_functions.php','mydialog','pop_dialog', afx, arguments[1]);
		break;
	case 3:
		agent.call('ajax_functions.php','mydialog','pop_dialog', afx, arguments[1], arguments[2]);
		break;
	case 4:
		agent.call('ajax_functions.php','mydialog','pop_dialog', afx, arguments[1], arguments[2], arguments[3]);
		break;
	}
}

// Dialog stuff
function pop_dialog(s) {
	document.getElementById('dialog').innerHTML = s;
	document.getElementById('dialog').style.display = 'block';
	dialogwidth = document.getElementById('large_picture').offsetWidth + 40;
	dialogheight = document.getElementById('large_picture').offsetHeight + 30;
	// alert("Dialog dim = " + dialogheight + " x " + dialogwidth);
	if (viewportheight == 0)
		setviewport();
	// alert("ViewPort dim = " + viewportheight + " x " + viewportwidth);
	shadecloth = document.getElementById('dialogshadecloth');
	shadecloth.style.top = 0;
	shadecloth.style.left = 0;
	shadecloth.style.width = '100%';
	shadecloth.style.height = '100%';
	shadecloth.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=60);";
	shadecloth.style.MozOpacity = 60/100;
	shadecloth.style.opacity = 60/100;
	
	my1dialog = document.getElementById('dialogcontent');
	my1dialog.style.width = dialogwidth;
	my1dialog.style.height = dialogheight;
	bordertop = Math.round((viewportheight - dialogheight) / 2);
	my1dialog.style.top = Math.round((viewportheight - dialogheight) / 2);
	borderleft = Math.round((viewportwidth - dialogwidth) / 2);
	my1dialog.style.left = Math.round((viewportwidth - dialogwidth) / 2);
	// alert("Borders = " + bordertop + " x " + borderleft);
	scrollwindow();
}

function hide_dialog() {
	document.getElementById('dialog').innerHTML = "";
	document.getElementById('dialog').style.display = 'none';
}

function preview(obj) {
	agent.call('ajax_functions.php','preview', 'replace', obj.options[obj.selectedIndex].value);
}

// gm_forms validate and save button function
function validateSave(button, validfunc) {
	button.disabled = true;
	buttonvalue = button.value;
	button.value = "Saving...";
	frm = button.form;
	vfix = validfunc.indexOf("(");
	if (validfunc && vfix) 
		rawvf = validfunc.substr(0,vfix);
	else
		rawvf = validfunc;
	res = true;
	if (eval(rawvf))
		res = eval(validfunc);
	if (res)
		return frm.submit();
	button.value = buttonvalue;
	button.disabled = false;
	return;
}

