// GRAPHA INTUITIVE www.grapha.net
// Departamento de Desarollo
// Autor: Enrique Valentin Morganizo
// Funciones de validacion del formulario 
vacio='';
function crearray(n){
     this.lenght=n;
     for (var j=1;j<=n;j++){
	      this[j]=0;
   }
}
function esVacio(s) 
{ 
    for(var i = 0; i < s.length; i++) { 
        var c = s.charAt(i); 
        if ((c != ' ') && (c != '\n') && (c != '\t')) return false; 
    } 
    return true; 
} 
 
function tieneComilla(s) 
{ 
    for(var i = 0; i < s.length; i++) { 
        var c = s.charAt(i); 
        if (c == '\'') return true; 
    } 
    return false; 
} 
function sinEspacio (a) {
	var numero;
	numero = a.length;
	var variable =' ';
	var  i=0;
	var bandera =0;
	for (i=0;i<=numero;i++){
		if (a.charAt(i) == variable ){
			bandera=1;
			i=numero+1;
		}
	}
	if (bandera==1){
		return false;
	}else{
		return true;
	}
}
function sinNumeros (a) {
  var numero;
  numero = a.length;
  var i;
  var bandera =0;
  for (i=0;i<=numero;i++){
		if (isNaN(a.charAt(i))){
			bandera=1;
			i=numero+1;
		}
	}
	if (bandera==1){
		return false;
	}else{
		return true;
	}
}

function sinLetras (a) {
   var numero;
   var variable =' ';
  numero = a.length;
  var i;
  var bandera =0;
  if (numero!=0){
    for (i=0;i<numero;i++){
	  	if ((a.charAt(i)!=variable) && (!isNaN(a.charAt(i)))) {
		  	bandera=1;
			  break;
		  }
	  }
  }
	if (bandera==1){
		return true;
	}else{
		return false;
	}
}
// Funciones de validacion del envio del formulario.


function inicializaFormulario(f) 
{ 
    document.f.txt_nombre.opcional=false; 
    document.f.txt_nombre.label="Nombre";
    document.f.txt_apellido1.opcional=false; 
    document.f.txt_apellido1.label="Apellido 1";
	document.f.txt_apellido2.opcional=true; 
    document.f.txt_apellido2.label="Apellido 2";
    document.f.txt_cp.opcional=false; 
    document.f.txt_cp.label="C\u00f3digo postal";
	document.f.txt_ciudad.opcional=false; 
    document.f.txt_ciudad.label="Ciudad";
    document.f.txt_direccion.opcional=false; 
    document.f.txt_direccion.label="Direcci\u00f3n";
    document.f.txt_provincia.opcional=true;
    document.f.txt_provincia.label="Provincia";	
    document.f.txt_email.opcional=false; 
    document.f.txt_email.label="Correo electr\u00f3nico";
	document.f.txt_email2.opcional=true; 
    document.f.txt_email2.label="Correo electr\u00f3nico alternativo";
    document.f.txt_telefono.opcional=true; 
    document.f.txt_telefono.label="Tel\u00e9fono";
    document.f.txt_movil.opcional=true; 
    document.f.txt_movil.label="M\u00f3vil";	
}

function esValido(f) 
{ 
	var msg; 
    var empty_fields = ""; 
    var errors = ""; 
    var vacio= "";
    var cuestionario= "";
    // Inicializa los valores del formulario 
    inicializaFormulario (f); 
 	if (document.f.txt_tratamiento.selectedIndex==0){
		empty_fields += "\n          " + "Tratamiento"; 
	}
    // Validacion 
    for(var i = 0; i < document.f.length; i++) { 
        var e = document.f.elements[i];
        if ((e.type == "text") || (e.type == "textarea")|| (e.type == "password")) { 
            // primero chequea que no este vacio 
            if ((((e.value == null) ||(e.value == "") || esVacio(e.value)) && !e.opcional)) { 
                empty_fields += "\n          " + e.label; 
                continue; 
            } 
 
            // Chequea que los campos text no tenga comilla simple 
            if (tieneComilla(e.value)) { 
                errors += "\n          " + e.label;  
                continue; 
            }        
        }
    }
    
    // array con los campos de solo letras
    arrayletras = new crearray(5);
    arrayletras[0]="document.f.txt_nombre";
	arrayletras[1]="document.f.txt_apellido1";
	arrayletras[2]="document.f.txt_apellido2";
	arrayletras[3]="document.f.txt_provincia";
    var contador;
       for (contador=0;contador<=3;contador++){
       var a = eval(arrayletras[contador] + ".value");
          if (sinLetras(a)){
            var etiqueta = eval (arrayletras[contador] + ".label");
            errors += "\n          No puede haber n\u00fameros en el campo " + etiqueta;
          }
     }
     
	 
    // array con los campos de solo numeros
    arraynumeros = new crearray(3);
    arraynumeros[0]="document.f.txt_telefono";
	arraynumeros[1]="document.f.txt_movil";
	arraynumeros[2]="document.f.txt_cp";
    var contador;
       for (contador=0;contador<=2;contador++){
       var a = eval(arraynumeros[contador] + ".value");
          if (!sinNumeros(a)){
            var etiqueta = eval (arraynumeros[contador] + ".label");
            errors += "\n          No puede haber letras en el campo " + etiqueta;
          }
     }
     if (!empty_fields && !errors) {
		 if (document.f.txt_email.value!=''){
		  	valor= document.f.txt_email.value;
    			if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(valor))){
   					errors += "\n          Correo electr\u00f3nico";
  				}
		 }
		 if (document.f.txt_movil.value=='' && document.f.txt_telefono.value==''){
			 	errors += "\n          Introduce al menos un n\u00FAmero de Tel\u00e9fono";
		 }
  	}
   
    if (!empty_fields && !errors) {
    	crear_url();
    }else{
    	msg  = "______________________________________________________\n\n" 
    	msg += "Formulario no tramitado por tener uno o varios errores.\n"; 
    	msg += "Por favor, corrige los errores y reenv\u00edalo.\n"; 
    	msg += "______________________________________________________\n\n" 
    	if (empty_fields) { 
        	msg += "- Los siguientes campos obligatorios est\u00e1n vac\u00edos:"  
                	+ empty_fields + "\n"; 
    	} 
    	if (errors) { 
        	msg += "- Los siguientes campos tienen valores err\u00f3neos:"+ errors + "\n"; 
    	}
	    if (vacio) {
        	msg += vacio;
		}
    	alert(msg); 
    }
}
