function toggle(e) {
	if (document.getElementById(e)) {
		var t = document.getElementById(e);
		t.style.display == "block" ? t.style.display = "none" : t.style.display = "block";
	}
}

function toggleOn(e) {
	if (document.getElementById(e)) {
		var t = document.getElementById(e);
		t.style.display = "block";
	}
}

function toggleOff(e) {
	if (document.getElementById(e)) {
		var t = document.getElementById(e);
		t.style.display = "none";
	}
}

function showLogin() {
	toggle("loginBox");
}

function getAd(a) {
	return false;
}

function editItem(i) {
	self.location.href = "?mode=edit&id=" + i;
}

function checkBoxLength(frm,fld) {
	try { 
		len = document[frm][fld].length;
	}
	catch(e) {
		len = 1;
	}
	return len;
}


function checkBoxCounter(frm,fld) {
	total = 0;
	if (document[frm][fld]) {
		len = checkBoxLength(frm,fld);
		if (len == 1) {
			if (document[frm][fld].checked == true) {
				total = 1;
			}
		}
		else {
			for (c=0; c<len; c++) {
				if (document[frm][fld][c].checked == true) {
					total = total + 1;
				}
			}
		}
	}
	return total;
}

function checkBoxAll(frm,fld) {
	try {
		t = checkBoxLength(frm,fld);
		st = checkBoxCounter(frm,fld);
		action = (st < t) ? true : false;
		if (t==1) {
			document[frm][fld].checked = action;
		}
		else {
			for (c=0; c<t; c++) {
				document[frm][fld][c].checked = action;
			}
		}
	}
	catch(e) {
		alert("Sorry, this function is not available!");
	}
}

function CheckEmail(addy) {
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(addy) == false) {
		return false;
	}
	else {
		return true;
	}
}

function CheckURL(url) {
	var theLink = new String(url);
	var prefix = "http://";
	if (theLink.length == 0) {
		return false;
	}
	else if (theLink.toLowerCase().indexOf(prefix) != 0) {
		return false;
	}
	else {
		return true;	
	}
}

function setDivContent(d,c) {
	if (document.getElementById(d)) {
		document.getElementById(d).innerHTML = c;
	}
}

function pF() {
	if (document.styleSheets) {
		if (document.styleSheets.length >= 3) {
			if (document.styleSheets[0].title == "default" && document.styleSheets[1].title == "print") {
				document.styleSheets[0].disabled = true;
				document.styleSheets[1].disabled = false;
				document.styleSheets[2].disabled = false;
				window.print();
			}
		} else if (document.getElementById("theCss")) {
			document.getElementById("theCss").href = "/hr_print.css";
		} else {
			alert("A higher version of javascript is required!");
		}
	} else {
		alert("A higher version of javascript is required!");
	}
}

function getMailer() {
	document.m.submit();
}

function getComments() {
	toggle("comments");
}

function addComment() {
	toggleOff("commentResponse");
	toggleOn("commentForm");
	document.cF.comment.value = "";
}

function cancelComment() {
	toggleOff("commentForm");
}

function verifyAddComment() {	
	if (document.cF.authorname.value.length < 2) {
		alert("Please enter your name");
		document.cF.authorname.focus();
	} else if (!(CheckEmail(document.cF.authoremail.value))) {
		alert("Please enter a valid e-mail address");
		document.cF.authoremail.focus();
	} else if (document.cF.comment.value.length < 4) {
		alert("Please enter your comment");
		document.cF.comment.focus();
	} else {
		postComment();
	}
}

function postComment() {
	toggleOff("commentForm");
	toggleOn("commentResponse");
	//document.getElementById("commentResponse").innerHTML = "Processing ...";
	setDivContent("commentResponse","Processing ...");
	var fP = "";
	fP = fP + "articleid=" + encodeURI(document.cF.articleid.value); 
	fP = fP + "&authorname=" + encodeURI(document.cF.authorname.value); 
	fP = fP + "&authoremail=" + encodeURI(document.cF.authoremail.value); 
	fP = fP + "&comment=" + encodeURI(document.cF.comment.value);
	doAjPost("commentResponse","/comment/",fP);
}

function doAjPost(e,u,fP) {
	ajs = true;
	var xmlHttp;
	var el = document.getElementById(e);
	try {
		xmlHttp = new XMLHttpRequest();
	}
	catch (e) {
		try {
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			try {
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e) {
				alert("Error: your browser does not support this method!");
				ajs = false;
			}
		}
	}
	
	try {
		if (ajs) {		
			xmlHttp.onreadystatechange=function() {
				if (xmlHttp.readyState == 4) {
					el.innerHTML = xmlHttp.responseText;
				}
			}
			xmlHttp.open("POST",u,true);
			xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8"); 
			xmlHttp.send(fP);
		}
	} catch (e) {
		alert("Error: request not available!");
	}
}

function verifyJoinMailingList() {	
	if (!(CheckEmail(document.m.address.value))) {
		alert("Please enter a valid e-mail address");
		document.m.address.focus();
	} else {
		joinMailingList();
	}
}

function joinMailingList() {
	setDivContent("emailForm","Processing ...");
	var fP = "emailaddress=" + encodeURI(document.m.address.value); 
	doAjPost("emailForm","/mailinglist/",fP);
}

function keepAlive() {
	doAjPost("keepAliveContainer","/admin/keepalive/","");
}

function right(str, n){
	if (n <= 0) {
		return "";
	} else if (n > String(str).length) {
		return str;
	} else {
		var iLen = String(str).length;
		return String(str).substring(iLen, iLen - n);
	}
}
