/*
################################################
############# Global functions #################
################################################

Last updated: 16 Jan 06
By: Tom-Marius Olsen
*/

/*
Basic function for showing and hiding the subNavs in the main menu.
*/
sfHover = function() {
	var sfEls = document.getElementById("nav").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);

/*
Open resized window
*/
function openBrWindow(theURL,winName,features) { //v2.0
	window.open(theURL,winName,features);
};

/*
################################################
########## Form validation functions ###########
################################################

USE:
We can prevet the server from doing some ekstra work if we can check that most formvalues are
formated correctly before they are submitted.

The easies way is to add this bit to the form: onsubmit="return validate();"
Then create the validate function containing the fields that needs to be checked like this:

function validate(){
	if(!required(document.forms[0].comment_tittle.value,'Skriv inn en tittel.')){ return false; };
};

*/

//Validate the string length og a field.
function required(FIELD,MESSAGE){
	//trim whitespace before we validate
    FIELD = FIELD.replace(/^\s+/,'').replace(/\s+$/,'');
	if (FIELD.length == 0) {
		alert(MESSAGE);
		return false;
	} else {
		return true;
	};
};

function validate_number(FIELD,MESSAGE){
	//Trim whitespace before we validate
	FIELD = FIELD.replace(/^\s+/,'').replace(/\s+$/,'');
	//remove numeric noise
	FIELD = FIELD.replace(/^[0-9]+$/, '');
	if (FIELD.length == 0) {
		return true;
	} else {
		alert(MESSAGE);
		return false;
	};
};

//Validate a emailadress
function validate_email(FIELD,MESSAGE){
	//Trim whitespace before we validate
	FIELD = FIELD.replace(/^\s+/,'').replace(/\s+$/,'');
	returnVal = checkregex(FIELD, /^[a-zA-Z_0-9-]+(\.[a-zA-Z_0-9-]+)*@([a-zA-Z_0-9-]+\.)+[a-zA-Z]{2,7}$/);
	if(returnVal){
		return true;
	} else {
		alert(MESSAGE);
		return false;
	};
};

//This functions is used to check regular expressions. 
//NB it also retuns false if the FIELD variable is empty, 
//althoug, it does not take into consideration whitespaces.
function checkregex(FIELD, regexPattern){
	if(FIELD.length == 0){
		return false;
	};
	return regexPattern.test(FIELD);
};