﻿// JavaScript Document
function validateFormOnSubmit(theForm) {
var reason = "";



	  reason += validateEmpty(theForm.nombre);
	  reason += validateEmail(theForm.Email);	  
	  reason += validatePhone(theForm.telefono);
	  reason += validateEmpty(theForm.msg_area);

      
  if (reason != "") {
    alert("POR FAVOR COMPRUEBE LOS CAMPOS OBLIGATORIOS: \n \n" + reason);
    return false;
  }

  return true;
}



// EVENTOS :: ANULADO POR EL TEMA DE LAS PERSIANAS :: VA DENTRO DE LOS FORMULARIOS
/*
function eventos() {		
		
    if (document.all) {
	form1.nombre.onblur = function(){ validateEmpty(form1.nombre); }
	form1.Email.onblur = function(){ validateEmail(form1.Email); }
	form1.telefono.onblur = function(){ validatePhone(form1.telefono); }	
	form1.msg_area.onblur = function(){ validateEmpty(form1.msg_area); }

	} else if (document.getElementById) {
	document.getElementById("nombre").onblur = function(){ validateEmpty(document.form1.nombre);	}
	document.getElementById("Email").onblur = function(){ validateEmail(form1.Email); }
	document.getElementById("telefono").onblur = function(){ validatePhone(form1.telefono); }	
	document.getElementById("msg_area").onblur = function(){ validateEmpty(document.form1.msg_area);	}

	
	}

		

};
*/




function validateEmpty(fld) {
    var error = "";
	// funcion para recorrer un campo y asegurarse que no queda vacio
	function vacio(q) {
        for ( i = 0; i < q.length; i++ ) {
                if ( q.charAt(i) != " " ) {
					return true
			}
        }
        return false
	}
// dependiendo de cada campo el mensaje es diferente
// campos afectados: from, vacioDos
	if(fld.name == "nombre") {
	var elcampo = "nombre";
	} else {
		var elcampo = "mensaje";
	}
///////////////////////

	if( vacio(fld.value) == false || fld.value.length == 0 || !fld.value) {
		/*fld.style.background = '#F00';*/
		fld.style.border = '1px solid #F00';		
        error = "\n El campo " + elcampo + " no ha sido cubierto."
    } else {
		/*fld.style.background = 'White';*/
		fld.style.border = '1px solid #999';
    }

    return error;
}

//--------------------------





function validateEmptyDos(fld) {
    var error = "";
    var er_primero = /^ /;
    if (fld.value.length == 0 || !fld.value || fld.value == "") {
        /*fld.style.background = '#F00';*/		
		fld.style.border = '1px solid #F00'; 
        error = "\n El campo debe estar cubierto."
	} else if (er_primero.test(fld.value)) {
		/*fld.style.background = '#F00';*/
		fld.style.border = '1px solid #F00'; 
        error = "\n Hay un espacio al principio del campo."
	} else {
        /*fld.style.background = 'White';*/
		fld.style.border = '1px solid #999';
    }
    return error;   
}






function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
} 

function validateEmail(fld) {
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
    
    if (fld.value == "") {
        /*fld.style.background = '#F00';*/
		fld.style.border = '1px solid #F00';
        error = "\n No has introducido una dirección de correo electrónico.";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        /*fld.style.background = '#F00';*/
		fld.style.border = '1px solid #F00';
        error = "\n Por favor introcuzca una dirección de correo válida.";
    } else if (fld.value.match(illegalChars)) {
        /*fld.style.background = '#F00';*/
		fld.style.border = '1px solid #F00';
        error = "\n La dirección de correo electrónico contiene caracteres no permitidos.";
    } else {
        /*fld.style.background = 'White';*/
		fld.style.border = '1px solid #999';
    }
    return error;
}




function validatePhone(fld) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, ''); 
	var blancos = /\s/;   

   if (fld.value == "") {
        error = "\n No has introducido un número de teléfono.";
        /*fld.style.background = '#F00';*/
		fld.style.border = '1px solid #F00';
	 } else if (blancos.test(fld.value)) { 
	   error = "\n El número de teléfono no puede contener espacios vacíos.";
        /*fld.style.background = '#F00';*/
		fld.style.border = '1px solid #F00';
    } else if (isNaN(stripped)) {
        error = "\n El número de teléfono debe contener caracteres válidos.";
        /*fld.style.background = '#F00';*/
		fld.style.border = '1px solid #F00';
    } else if (!(stripped.length == 9)) {
        error = "\n El número de teléfono debe contener nueve números sin espacios.";
        /*fld.style.background = '#F00';*/
		fld.style.border = '1px solid #F00';
    } else {
        /*fld.style.background = 'White';*/
		fld.style.border = '1px solid #999';
    }
    return error;
}
