// Support for deleting records with Ajax

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 = "";
}

// Loading
function popdelay() {
	mb = document.getElementById('delay');
	if (mb) 
		mb.innerHTML = "<div style='position: absolute; bottom: 50%; left: 50%;'>" +
						"<img src='images/loading_loading.gif' border='0'/></div>";
	msg = document.getElementById('message');
	if (msg)
		msg.innerHTML = ""; 
}
// Loading
function poptab0() {
	popdelay();
}
function poptab0off() {
	mb = document.getElementById('delay');
	if (mb) 
		mb.innerHTML = "";
}


// Booking stuff
function makebooking(obj, e) {
	var targ
	if (!e) var e = window.event
	if (e.target) targ = e.target
	else if (e.srcElement) targ = e.srcElement
	if (targ.nodeType == 3) // defeat Safari bug
		targ = targ.parentNode
	if (targ.tagName != "A" && targ.tagName != "IMG") {
		divs = obj.getElementsByTagName('div');
		catid = 0;
		resid = 0;
		if (document.rc_form) {
			rcform = document.rc_form;
			catix = rcform.rc_categories.selectedIndex;
			catid = rcform.rc_categories.options[catix].value;
			resid = rcform.rc_resources.value;
		}
		agent.call('ajax_functions.php','makebooking3','v2',divs[0].id, catid, resid);
	}
}
function besave() {
	poptab0();
	$can = eval("document.be_form.save_cancel.value");
	if ($can == "cancel") return bookingtable();
	frm = document.be_form;
	if (frm.be_timestart.selectedIndex < 0 || frm.be_timeend.selectedIndex < 0) {
		alert("Booking not completed - no valid period selected");
		poptab0off();
		return;
	}
	if (!frm.be_resourceid || frm.be_resourceid.value == "") {
		alert("Booking not completed - no resource selected");
		poptab0off();
		return;
	}
	parms = form_to_array("be_form");
	agent.call('ajax_functions.php', 'besave', 'v2', parms);
}
function adddesc(divid,desc) {
	obj = document.getElementById(divid);
	if (obj) {
		txt = obj.innerHTML;
		if (txt.length > 0)
			txt += "<br/>";
		obj.innerHTML = txt + desc;
	}
}
function popupdesc(event,bookcnt) {
	if (bookcnt > 0) {
		x=event.clientX - 180;
		y=event.clientY + 5;
		scrollx = (document.all)?document.body.scrollLeft:window.pageXOffset;
		scrolly = (document.all)?document.body.scrollTop:window.pageYOffset;
		// alert("x=" + x + " y=" + y + " scrollx=" + scrollx + " scrolly=" + scrolly);
		mb = document.getElementById("bookcnt" + bookcnt);
		if (mb) {
			mb.style.left = scrollx + event.clientX - 180;
			mb.style.top = scrolly + event.clientY + 5;
			mb.style.position = "absolute";
			mb.style.visibility = "visible";
			mb.style.display = "block";
		} else {
			alert("Error: Not found bookcnt" + bookcnt);
		}
	}
}
function popuphide(bookcnt) {
	if (bookcnt > 0) {
		mb = document.getElementById("bookcnt" + bookcnt);
		if (mb) {
			mb.style.visibility = "hidden";
			mb.style.display = "none";
		}
	}
}
function updatedtend(obj) {
	poptab0();
	periodid = obj.options[obj.selectedIndex].value;
	frm = obj.form;
	cv = frm.be_timeend.value;
	agent.call('ajax_functions.php', 'updatedtend', 'v2', periodid, cv);
}
function bookedit(bookid) {
	poptab0();
	agent.call('ajax_functions.php', 'bookedit', 'v2', bookid);
}
function pendingalert(bookid) {
	poptab0();
	agent.call('ajax_functions.php', 'pendingalert', 'v2', bookid);
}
function pendingapprove(bookid) {
	poptab0();
	agent.call('ajax_functions.php', 'pendingapprove', 'v2', bookid);
}
function pendingcancel(bookid) {
	poptab0();
	agent.call('ajax_functions.php', 'pendingcancel', 'v2', bookid);
}
function bextender(obj) {
	frm = obj.form;
	if (isNaN(parseInt(frm.be_resourceid.value))) {
		obj.checked = false;
		alert('Select a resource first');
		return;
	}
	if (obj.checked) {
		dtout = frm.be_timeend.value;
		ix = frm.be_resourceid.selectedIndex;
		resid = frm.be_resourceid.options[ix].value;
		agent.call('ajax_functions.php', 'extender', 'v2', resid, dtout);
		document.getElementById('extendbooking').style.display = 'block';
		return;
	}
	document.getElementById('extendbooking').style.display = 'none';
	document.getElementById('extendbooking').innerHTML = '';
	return;
}
function selectresources(catobj) {
	frm = catobj.form;
	ix = catobj.selectedIndex;
	catid = catobj.options[ix].value;
	timein = timeout = 0;
	if (frm.be_timestart.selectedIndex >= 0)
		timein = frm.be_timestart.options[frm.be_timestart.selectedIndex].value;
	if (frm.be_timeend.selectedIndex >= 0)
		timeout = frm.be_timeend.options[frm.be_timeend.selectedIndex].value;
	agent.call('ajax_functions.php','selectresources','v2',catid, timein, timeout);
}
// 15/4/08 Can't find any reference to this function being called
function catselection() {
	poptab0();
	frm = document.rc_form;
	ix = frm.rc_categories.selectedIndex;
	catid = frm.rc_categories.options[ix].value;
	agent.call('ajax_functions.php','catselection','v2',catid, 0);
}
function resselection(resid) {
	poptab0();
	frm = document.rc_form;
	frm.rc_resources.value = resid;
	ix = frm.rc_categories.selectedIndex;
	catid = frm.rc_categories.options[ix].value;
	agent.call('ajax_functions.php','resselection','v2',catid,resid);
	agent.call('ajax_functions.php','resourcedetail','v2',resid);
}
// A resource has been selected - we need to update the period list
function showimage(resobj) {
	frm = resobj.form;
	ix = resobj.selectedIndex;
	resid = resobj.options[ix].value;
	agent.call('ajax_functions.php','showimage','v2',resid);
	if (frm.be_timestart.selectedIndex < 0 || frm.be_timeend.selectedIndex < 0) {
		alert("Booking not completed - invalid periods");
		return bookingtable();
	}
	timein = frm.be_timestart.options[frm.be_timestart.selectedIndex].value;
	timeout = frm.be_timeend.options[frm.be_timeend.selectedIndex].value;
	agent.call('ajax_functions.php','updateperiodselection','v2',resid,timein,timeout);
}

// Booking Options
// Booking Options
// Booking Options
function bosave() {
	poptab0();
	frm = document.bo_form;
	parms = form_to_array("bo_form");
	agent.call('ajax_functions.php', 'bosave', 'v2', parms);
}

function optionchecked(cb, cntnotes) {
	elem = document.getElementById("notes_" + cntnotes);
	if (elem) {
		if (cb.checked)
			elem.disabled = false;
		else {
			elem.value = "";
			elem.disabled = true;
		}
	}
}

function boedit(bkid) {
	poptab0();
	agent.call('ajax_functions.php', 'boedit', 'v2', bkid);
}

// Repeat booking
// Repeat booking
// Repeat booking
function brepeater(obj) {
	frm = obj.form;
	if (isNaN(parseInt(frm.be_resourceid.value))) {
		obj.checked = false;
		alert('Select a resource first');
		return;
	}
	if (obj.checked) {
		dtin = frm.be_timestart.value;
		repid = frm.be_repeatid.value;
		agent.call('ajax_functions.php', 'repeater', 'v2', repid, dtin);
		document.getElementById('repeatbooking').style.display = 'block';
		return;
	}
	document.getElementById('repeatbooking').style.display = 'none';
	document.getElementById('repeatbooking').innerHTML = '';
	return;
}
// Pushed list repeats button
function listrepeats(repid, bkid) {
	agent.call('ajax_functions.php', 'repeatform', 'v2', repid, bkid);
}
// The user selected a new frequency
function freshenddates(obj) {
	poptab0();
	freq = obj.options[obj.selectedIndex].value;
	repid = obj.form.rb_passingid.value;
	agent.call('ajax_functions.php', 'freshenddates', 'v2', freq, repid); 
}
function rbsave() {
	poptab0();
	$can = eval("document.rb_form.save_cancel.value");
	if ($can == "cancel") return bookingtable();
	frm = document.rb_form;
	parms = form_to_array("rb_form");
	agent.call('ajax_functions.php', 'rbsave', 'v2', parms);
}
function bookdelete(bkid) {
	agent.call('ajax_functions.php', 'bookdelete', 'v2', bkid);
}
// Push a calendar into this box
function endcalpop() {
	poptab0();
	agent.call('ajax_functions.php','endcalpop','v2', 0);
}
function endcalpopback() {
	poptab0();
	agent.call('ajax_functions.php','endcalpop','v2',-1);
}
function endcalpopforw() {
	poptab0();
	agent.call('ajax_functions.php','endcalpop','v2',1);
}
function pickdate(szdate) {
	poptab0();
	// de = document.rb_form.rb_dateend;
	// de.options.length = 0;
	// de.options[0] = new Option(szdate,szdate);
	agent.call('ajax_functions.php','endcalpopreset','v2', szdate);
}

// Adjust the date
function moveto(timein) {
	poptab0();
	agent.call('ajax_functions.php','bookingtable','v2',timein,0);
}


// Other generic stuff now...
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) {
		if (nurl.substring(0,1) == '/')
			currenturl = nurl;
		else
			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;
}

function popschools(frm,div,siz,nil) {
	popschools2(frm,div,siz,nil,"schoolid");
}
function popschools2(frm,div,siz,nil,fname) {
	if (frm.pc) 
		pc = frm.pc.value;
	else 
		pc = frm.gm_pc.value;
	ipc = parseInt(pc);
	if (ipc >= 1000 && ipc <= 9999) {
		// PC is valid, send to server
		agent.call('ajax_functions.php','popschools','v2', div, pc, siz, nil, fname);	
	} else {
		alert("Post Code is invalid");
		frm.pc.focus();
	}
}

// Side bar
function barcalendars() {
	setdiv('barcalendars');
	agent.call('ajax_functions.php','setbar_calendars','');
}
function barresources() {
	setdiv('barresources');
	agent.call('ajax_functions.php','setbar_resources','');
}
function setdiv(o) {
	obj = document.getElementById(o);
	if (obj) {
		if (obj.style.display == 'none')
			obj.style.display = 'block';
		else
			obj.style.display = 'none';
	}
}
function popcal() {
	agent.call('ajax_functions.php','popcal','v2','calendar');
}
function opdateforward() {
	agent.call('ajax_functions.php','opdateforward','v2','calendar');
}
function opdateback() {
	agent.call('ajax_functions.php','opdateback','v2','calendar');
}

function barresx(id,max) {
	for (ix = 1; ix <= max; ix++) {
		if (ix == id)
			setdiv2('barres' + ix, 'block');
		else
			setdiv2('barres' + ix, 'none');
	}
}
function setdiv2(o,v) {
	obj = document.getElementById(o);
	if (obj) 
		obj.style.display = v;
}

// Suggestion
function suggestion(phpfile) {
	agent.call('ajax_functions.php','suggestion','v2',phpfile);
}
function sugsave(prefix) {
	poptab0();
	$can = eval("document.sug_form.save_cancel.value");
	if ($can == "cancel") {
		agent.call('ajax_functions.php', 'sugdefault', 'v2', 0);
	} else {
		parms = form_to_array("sug_form");
		agent.call('ajax_functions.php', 'sugsave', 'v2', parms);
	}
}

// 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;
	}
}

// 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) {
	pop_dialog_size(s, 500, 700);
}
function pop_dialog_size(s, height, width) {
	dialog = document.getElementById('dialog');
	dialog.className = 'dialog';
	dialog.innerHTML = s;
	dialog.style.display = 'block';
	if (viewportheight == 0)
		setviewport();
	// alert("ViewPort dim = " + viewportheight + " x " + viewportwidth);
	shadecloth = document.getElementById('dialogshadecloth');
	shadecloth.style.backgroundColor = 'black';
	shadecloth.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=40);";
	shadecloth.style.MozOpacity = 40/100;
	shadecloth.style.opacity = 40/100;
	
	dialog.style.width = width;
	dialog.style.height = height;
	bordertop = Math.round((viewportheight - height) / 2);
	// alert("Bordertop = " + bordertop + " vpheight=" + viewportheight + " height=" + height);
	dialog.style.top = Math.round((viewportheight - height) / 2);
	borderleft = Math.round((viewportwidth - width) / 2);
	dialog.style.left = Math.round((viewportwidth - width) / 2);
	scrollwindow();
}

function hide_dialog() {
	shadecloth = document.getElementById('dialogshadecloth');
	shadecloth.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=0);";
	shadecloth.style.MozOpacity = 0;
	shadecloth.style.opacity = 0;
	dialog = document.getElementById('dialog');
	dialog.innerHTML = "";
	dialog.style.display = 'none';
}

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

// Utilities
// Utilities
// 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;
}
