// Support for deleting records with Ajax

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

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