/**
 * DHTML email validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */

function echeck(str) {
    var at   = "@";
    var dot  = ".";
    var lat  = str.indexOf(at);
    var lstr = str.length;
    var ldot = str.indexOf(dot);
    
    if (str.indexOf(at) == -1) {
       //alert("Invalid E-mail ID");
       return false;
    }

    if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
       //alert("Invalid E-mail ID");
       return false;
    }

    if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
        //alert("Invalid E-mail ID");
        return false;
    }

     if (str.indexOf(at, (lat + 1)) != -1){
        //alert("Invalid E-mail ID");
        return false;
     }

     if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
        //alert("Invalid E-mail ID")
        return false
     }

     if (str.indexOf(dot,(lat+2))==-1){
        //alert("Invalid E-mail ID")
        return false
     }
    
     if (str.indexOf(" ")!=-1){
        //alert("Invalid E-mail ID")
        return false
     }

     return true					
}

function isEmailAddress(theElement, nombre_del_elemento ){
    var s = theElement.value;
    var filter=/^[A-Za-z][A-Za-z0-9_]*@[A-Za-z0-9_]+\.[A-Za-z0-9_.]+[A-za-z]$/;
    if (s.length == 0 ) 
        return true;
    if (filter.test(s))
        return true;
    else
        alert("Ingrese una dirección de correo válida");
    theElement.focus();
    return false;
}

function valida_envia(lang){
    //valido el nombre
	//alert (lang);
	var s = document.valida.email.value;
	var t = document.valida.telefon.value;
	//var filter = /^[A-Za-z][A-Za-z0-9_.]*[A-za-z]@[A-Za-z0-9_]+\.[A-Za-z0-9_.]+[A-za-z]$/;	
    var filter_t=/^([0-9\+\s\+\-])+$/;
    
	//if (filter.test(s))
    if (echeck(s)) 
		1;
	else {
        if (lang == 'eng') {	        
			alert("Incorrect Email Adress")
        }
        else {
            alert("Email incorrecte")
        }
		document.valida.email.focus();
		return 0;
    }
		
	if (filter_t.test(t))
		1;
    else {
        if (lang == 'eng') {
            alert("Incorrect Telephone number")
        }
        else {
            alert("Telefon incorrecte")
        }
		document.valida.telefon.focus();
		return 0;
    }
		
    if (document.valida.nom.value.length == 0){
       if (lang == 'eng'){
           alert("Required Field")
	   }
	   else{
           alert("Camp Obligatori")
	   }
       document.valida.nom.focus()
       return 0;
    }
	if (document.valida.cognoms.value.length == 0){
        if (lang == 'eng') {
            alert("Required Field")
        }
        else {
           alert("Camp Obligatori")
        }
        document.valida.cognoms.focus()
        return 0;
    }
	if (document.valida.empresa.value.length == 0) {
       if (lang == 'eng') {
           alert("Required Field")
	   }
	   else	{
           alert("Camp Obligatori")
	   }
       document.valida.empresa.focus()
       return 0;
    }
	if (document.valida.carrec.value.length == 0) {
        if (lang == 'eng') {
            alert("Required Field")
        }
        else {
            alert("Camp Obligatori")
        }
        document.valida.carrec.focus()
        return 0;
    }
	if (document.valida.telefon.value.length == 0){
        if (lang == 'eng'){
            alert("Required Field")
	   }
	   else{
           alert("Camp Obligatori")
	   }
       document.valida.telefon.focus()
       return 0;
    }		
		
	if (document.valida.email.value.length == 0){
       if (lang == 'eng') {
           alert("Required Field")
	   }
	   else {
           alert("Camp Obligatori")
	   }
       document.valida.email.focus()
       return 0;
    }
	if (document.valida.direccion.value.length == 0){
        if (lang == 'eng') {
            alert("Required Field")
	   }
	   else {
           alert("Camp Obligatori")
	   }
       document.valida.direccion.focus()
       return 0;
    }
	if (document.valida.cp.value.length == 0){
        if (lang == 'eng') {
            alert("Required Field")
	   }
	   else {
           alert("Camp Obligatori")
	   }
       document.valida.cp.focus()
       return 0;
    }
	if (document.valida.ciutat.value.length == 0){
       if (lang == 'eng') {
           alert("Required Field")
	   }
	   else {
           alert("Camp Obligatori")
	   }
       document.valida.ciutat.focus()
       return 0;
    }
	if (document.valida.pais.value.length == 0){
        if (lang == 'eng') {
            alert("Required Field")
	   }
	   else {
           alert("Camp Obligatori")
	   }
       document.valida.pais.focus()
       return 0;
    }
	//el formulario se envia
    //alert("Muchas gracias por enviar el formulario");
    document.valida.submit();
} 

