
function controlForm(){
	var test = true;
	initFields();
	if(trim(document.contactForm.firstName.value)==""){
		printError(document.contactForm.firstName);
		if (test) test = false;
	}
	if(trim(document.contactForm.lastName.value)==""){
		printError(document.contactForm.lastName);
		if (test) test = false;
	}
	if(trim(document.contactForm.mail.value)==""){
		printError(document.contactForm.mail);
		if (test) test = false;
	}
	if(trim(document.contactForm.msg.value)==""){
		printError(document.contactForm.msg);
		if (test) test = false;
	}
	if(!controlEmail(document.contactForm.mail)){
		printError(document.contactForm.mail);
		if (test) test = false;
	}
	if(trim(document.contactForm.tel.value)!=""){
		var exp = new RegExp("^[0-9-.]*$","g");
		if(!exp.test(document.contactForm.tel.value)){
			printError(document.contactForm.tel);
			if (test) test = false;
		}
	}
	return test;
}



function controlEmail(element) { 
	var email = element.value; 
	if (email.search(/^[_a-z0-9-]+(.[_a-z0-9-]+)*[^._-]@[a-z0-9-]+(.[a-z0-9]{2,4})*$/) == -1){ 
		return false;
	}
	return true;
}

function printError(element){
	element.style.border = "1px solid red" ;
	document.getElementById("error").style.display='block';
}

function trim(inword){
   	word = inword.toString();
   	var i=0;
   	var j=word.length-1;
   	while(word.charAt(i) == " ") i++;
   	while(word.charAt(j) == " ") j--;
   	if (i > j) {
		return word.substring(i,i);
	} else {
		return word.substring(i,j+1);
	}
}

function initFields(){
	document.contactForm.firstName.style.border = "1px solid #463F30" ;
	document.contactForm.lastName.style.border = "1px solid #463F30" ;
	document.contactForm.mail.style.border = "1px solid #463F30" ;
	document.contactForm.tel.style.border = "1px solid #463F30" ;
	document.contactForm.msg.style.border = "1px solid #463F30" ;
	document.getElementById("error").style.display='none';
}
