/**
 * Trims whitespace
 *
 * @param string dString : the string to trim
 * @param integer dMode	 : in what mode to trim:
 *
 *		0 : Remove all whitespace in the string
 *
 *		1 : Remove whitespace at start and end of
 *			the string and replaces all multiple
 *			whitespace with single spaces
 *
 *		2 : Same as 1 except that it leaves all the
 *			newline chars alone
 *
 *		3 : Remove whitespace at start and end of
 *			the string
 *
 *		4 : Remove whitespace at start of string
 *
 *		5 : Remove whitespace at end of string
 *
 *		Default : Remove all whitespace in the string
 */
function Trim(dString, dMode)
{
	if(typeof dString != "string")
		return dString;

	switch(dMode)
	{
		case 0:
			return dString.replace(/\s+/g, "");
		case 1:
			return dString.replace(/(^\s+)|(\s+$)/g, "").replace(/\s+/g, " ");
		case 2:
			return dString.replace(/(^\s+)|(\s+$)/g, "").replace(/[ \t]+/g, " ");
		case 3:
			return dString.replace(/(^\s+)|(\s+$)/g, "");
		case 4:
			return dString.replace(/(^\s+)/g, "")
		case 5:
			return dString.replace(/(\s+$)/g, "")
		default:
			return dString.replace(/\s+/g, "");
	}
}

var dReturn = false;
function ValidateForm(dObject)
{
	if( ! document.getElementById("contactForm"))
		return;

	if(dReturn)
		return;
		
	var name = document.getElementById("name");
	var mail = document.getElementById("mail");
	var conf = document.getElementById("conf");
	var subj = document.getElementById("subj");
	var mess = document.getElementById("mess");

	var nameVal = name.value = Trim(name.value, 1);
	var mailVal = mail.value = Trim(mail.value, 0);
	var confVal = conf.value = Trim(conf.value, 0);
	var subjVal = subj.value = Trim(subj.value, 1);
	var messVal = mess.value = Trim(mess.value, 2);

	var dPattern    = new RegExp(/^[\w\.\-]+@([\w\-]+\.)+[a-zA-Z]{2,6}$/);

	var errors = 0;

	if(nameVal == "")
	{
		document.getElementById("lblName").innerHTML = "Name missing:*";
		document.getElementById("lblName").className = "invalid";
		errors++;
	}
	else
	{
		document.getElementById("lblName").innerHTML = "Name:*";
		document.getElementById("lblName").className = "";
	}

	if(mailVal == "")
	{
		document.getElementById("lblEmail").innerHTML = "Email missing:*";
		document.getElementById("lblEmail").className = "invalid";
		errors++;
	}
	else if( ! dPattern.test(mailVal))
	{
		document.getElementById("lblEmail").innerHTML = "Email not valid:*";
		document.getElementById("lblEmail").className = "invalid";
		errors++;
	}
	else
	{
		document.getElementById("lblEmail").innerHTML = "Email:*";
		document.getElementById("lblEmail").className = "";
	}

	if(confVal == "")
	{
		document.getElementById("lblConfirmation").innerHTML = "Email confirmation missing:*";
		document.getElementById("lblConfirmation").className = "invalid";
		errors++;
	}
	else if(confVal != mailVal)
	{
		document.getElementById("lblConfirmation").innerHTML = "Your inputs does not match*";
		document.getElementById("lblConfirmation").className = "invalid";
		errors++;
	}
	else
	{
		document.getElementById("lblConfirmation").innerHTML = "Email confirmation:*";
		document.getElementById("lblConfirmation").className = "";
	}

	if(subjVal == "")
	{
		document.getElementById("lblSubject").innerHTML = "Subject missing:*";
		document.getElementById("lblSubject").className = "invalid";
		errors++;
	}
	else if(subjVal.length < 3)
	{
		document.getElementById("lblSubject").innerHTML = "Subject too short:*";
		document.getElementById("lblSubject").className = "invalid";
		errors++;
	}
	else
	{
		document.getElementById("lblSubject").innerHTML = "Subject:*";
		document.getElementById("lblSubject").className = "";
	}

	if(messVal == "")
	{
		document.getElementById("lblMessage").innerHTML = "Message missing:*";
		document.getElementById("lblMessage").className = "invalid";
		errors++;
	}
	else if(messVal.length < 11)
	{
		document.getElementById("lblMessage").innerHTML = "Message too short:*";
		document.getElementById("lblMessage").className = "invalid";
		errors++;
	}
	else
	{
		document.getElementById("lblMessage").innerHTML = "Message:*";
		document.getElementById("lblMessage").className = "";
	}
	
	if(errors > 0)
	{
		StopDefault(dObject);
	}
}

function SetReturn()
{
	dReturn = true;
}

function InitFormValidation()
{
	if(document.getElementById("contactForm"))
	{
		SetListener(document.getElementById("contactForm"), 'submit', ValidateForm);
	}
}
SetListener(window, 'load', InitFormValidation);