// functie die verplichte velden afgaat
function checkForm(field01,fieldtext01,field02,fieldtext02) {
	// kijk of er een gebruikersnaam is ingevoerd
	if (document.getElementById(field01).value=="") {
		alert(fieldtext01);
		document.getElementById(field01).focus();
		return false;
	}
	// kijk of er een wachtwoord is ingevoerd
	if (document.getElementById(field02).value=="") {
		alert(fieldtext02);
		document.getElementById(field02).focus();
		return false;
	}
	return true;
}
// functie die 2 checkboxen hetzelfde laat werken als twee radiobuttons
function checkThis(checkBox01,checkBox02) {
	if(document.getElementById(checkBox01).checked) { document.getElementById(checkBox02).checked = false; }
	else { document.getElementById(checkBox01).checked = true; }
}
function checkDelete(checkBox01,checkBox02) {
	if(!confirm("Are you sure that you want to tag this comment for deletion?")) {
		document.getElementById(checkBox01).checked = false;
		document.getElementById(checkBox02).checked = true;
	}
}
// if categoryimage is dis-/enabled don't/show extra fields
function hideShowExtraFields(checkValue,checkField,checkFieldId01,checkFieldId02) {
	if(checkValue==1) {
		document.getElementById(checkFieldId01).style.display="block";
		document.getElementById(checkFieldId02).style.display="block";
		if(checkField != '') { document.getElementById(checkField).focus(); }
	}
	else {
		document.getElementById(checkFieldId01).style.display="none";
		document.getElementById(checkFieldId02).style.display="none";
		if(checkField != '') { document.getElementById(checkField).focus(); }
	}
}
// advanced emailcheck
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;
}
// no native JS-trim function, so let's create one
function trim(inputString) {
	/*	removes leading and trailing spaces and replaces double spaces with a single space;
		if not a string is given, the input is returned unchanged */
	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;
}
// webstandard way of opening links in new windows
function externalLinks() {
	if (!document.getElementsByTagName) { return; }
	var anchors = document.getElementsByTagName("a");
	for (var i=0; i<anchors.length; i++) {
		var anchor = anchors[i];
		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") {
			anchor.target = "_blank"; 
		} 
	} 
} 
window.onload = externalLinks;