// simple formcheck
function checkform() {
	if (trim(document.getElementById("requiredname").value) == "") {
		alert("You haven't stated your (nick)name.\nThis is required for commenting on this entry!");
		document.getElementById("requiredname").focus();
		return false;
	}
	if (trim(document.getElementById("requiredcomment").value) == "") {
		alert("You haven't written a comment.\nThis is required!\nWhy else would you try and post this form ?");
		document.getElementById("requiredcomment").focus();
		return false;
	}
	else { return true; }
}
function checkcontactform() {
	if (trim(document.getElementById("contactName").value) == "") {
		alert("You haven't stated your name.\nIt would be nice to know who's trying to get in touch with me!");
		document.getElementById("contactName").focus();
		return false;
	}
	if (trim(document.getElementById("contactEmail").value) == "") {
		alert("You haven't filled in your e-mail address.\nWithout it I cannot get back in touch with you!");
		document.getElementById("contactEmail").focus();
		return false;
	}
	if (trim(document.getElementById("contactQuestion").value) == "") {
		alert("You haven't written anything.\nThat is sort of the point with this form!");
		document.getElementById("contactQuestion").focus();
		return false;
	}
	else { return true; }
}
// simple e-mailcheck
function isValidEmail(email) { 
	if (email.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1) {
		return true; 
	}
	else {
		alert ("This does not appear to be a valid e-mail address.\nPlease enter your e-mail address in the form: you@you.com");
		return false; 
	} 
} 
// advanced e-mailcheck
function emailCheck (emailStr,field) {
	var alertmsg = true;
	if (emailCheck.arguments.length == 2 ) alertmsg = emailCheck.arguments[1];
	if(emailStr.length > 0){
		var emailPat = /^(.+)@(.+)$/;
		var specialChars = "\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";
		var validChars = "\[^\\W" + specialChars + "\]";
		var quotedUser = "(\"[^\"]*\")";
		var ipDomainPat = /^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
		var atom = validChars + '+';
		var word = "(" + atom + "|" + quotedUser + ")";
		var userPat = new RegExp("^" + word + "(\\." + word + ")*$");
		var domainPat = new RegExp("^" + atom + "(\\." + atom +")*$");
		var matchArray = emailStr.match(emailPat);
		if (matchArray == null) {
			if (alertmsg) {
				alert("The current e-mail address seems to be incorrect. Please check the @ and the '.'");
				document.getElementById(field).focus();
				return false;
			}
		}
		var user = matchArray[1];
		var domain = matchArray[2];
		if (user.match(userPat) == null) {
		    if (alertmsg) {
				alert("The current e-mail address seems to be incorrect. Please check the username.");
				document.getElementById(field).focus();
			    return false;
			}
		}
		var IPArray = domain.match(ipDomainPat);
		if (IPArray != null) {
		    // this is an IP address
			  for (var i = 1; i <= 4; i++) {
			    if (IPArray[i] > 255) {
			        if (alertmsg) {
						alert("The current e-mail address seems to be incorrect. Please check the IP-adres.");
						document.getElementById(field).focus();
						return false;
					}
			    }
		    }
		    return true;
		}
		var domainArray = domain.match(domainPat)
		if (domainArray == null) {
			if (alertmsg) {
				alert("The current e-mail address seems to be incorrect. Please check the domain.");
				document.getElementById(field).focus();
			    return false;
			}
		}
		var atomPat = new RegExp(atom,"g");
		var domArr = domain.match(atomPat);
		var len = domArr.length;
		if (domArr[domArr.length-1].length < 2 || domArr[domArr.length-1].length > 3) {
			// the address must end in a two letter or three letter word.
			if (alertmsg) {
				alert("The current e-mail address seems to be incorrect. An e-mail address usually ends on with two or three letters.");
				document.getElementById(field).focus();
				return false;
			}
		}
		if (len < 2) {
			var errStr = "The current e-mail address seems to be incorrect. Please check the domain.";
			if (alertmsg) {
				alert(errStr);
				document.getElementById(field).focus();
				return false;
			}
		}
	}
	return true;
}
/*	removes leading and trailing spaces and replaces double spaces with a single space;
	if not a string is given, the input is returned unchanged */
function trim(inputString) {
	if (typeof inputString != "string") { return inputString; }
	var retValue = inputString;
	var ch = retValue.substring(0, 1);
	while (ch == " ") {
		// check leading spaces
		retValue = retValue.substring(1, retValue.length);
		ch = retValue.substring(0, 1);
	}
	ch = retValue.substring(retValue.length-1, retValue.length);
	while (ch == " ") {
		// check trailing spaces
		retValue = retValue.substring(0, retValue.length-1);
		ch = retValue.substring(retValue.length-1, retValue.length);
	}
	while (retValue.indexOf("  ") != -1) {
		// find multiple spaces within the string
		retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length);
	}
	// return the string
	return retValue;
}
// dynamically change the background-color of an input-field
function changeBackground(obj,on_off) {
	/*	global values for colours of inputfields/textareas
		these values override values from CSS */
	var bgColorOff = '#fff';
	var bgColorOn = '#ff9';
	var colorOff = '#000';
	var colorOn = '#000';
	/* get the name of the current stylesheet
	var currentStyleSheet = getActiveStyleSheet();
	// if 'reverse' stylesheet, use other background-colour and font-colour than default
	if(currentStyleSheet == "reverse") {
		bgColorOff = '#000';
		colorOff = '#fff';
	} */
	// if focus NOT on inputfield/textarea, set background-colour and font-colour to default
	if (on_off == 0) {
		document.getElementById(obj).style.backgroundColor = bgColorOff;
		document.getElementById(obj).style.color = colorOff;
	}
	// if focus, set background-colour and font-colour to focus colours
	else {
		document.getElementById(obj).style.backgroundColor = bgColorOn;
		document.getElementById(obj).style.color = colorOn;
	}
}
// BEGIN DYNAMIC COMMENT PREVIEW FUNCTIONS -- thanks to Mike Davidson (http://www.mikeindustries.com/ for the help) 
// live comment preview - author 
function ReloadNameDiv() {
	document.getElementById("commentspreviewheader").style.display="block";
	document.getElementById("commentspreview").style.display="block";
	document.getElementById('NameDisplay').innerHTML = document.getElementById("requiredname").value;
}
// live comment preview  - comment body 
function ReloadTextDiv() { 
	document.getElementById("commentspreviewheader").style.display="block";
	document.getElementById("commentspreview").style.display="block";
	document.getElementById('CommentDisplay').innerHTML = '<p>'+document.getElementById('requiredcomment').value.replace(/(\r\n|\n)/g,'<br />').replace(/(<br \/>){2,}/gi,'<'+'/p><p>')+'<'+'/p>';
}
// by default do not show the commentsheader and preview until something is typed in either name or text
document.getElementById("commentspreviewheader").style.display="none";
document.getElementById("commentspreview").style.display="none";
// END DYNAMIC COMMENT PREVIEW FUNCTIONS 

