// Funciones CEDATOS - Copyright 2010

function validarEmail(email)
{
	var d;
  	d  = email.indexOf("@"), 
	d2 = email.lastIndexOf(".");
  	if((d<=0)||(email.indexOf(" ")!=-1)||(d>=email.length-1)||(d2<=d)||(d2>=email.length-1))
  	{
    	return false;
  	}
  	return true;
}

function valida_contacto(){
	var theForm = document.form_contactos;
	var emailRE = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
	var telfRE = /^[0-9]{2,3}-? ?[0-9]{6,7}$/;
	var telfRE1 = /^[0-9]+$/;
	var errMsg = "";
	var setfocus = "";
	
	if (theForm['tmptxt'].value == ""){
		errMsg = "Seguridad es un campo obligatotio";
		theForm['tmptxt'].className='text_rojo';
		setfocus = "['tmptxt']";
	}
	
	if (theForm['mensaje'].value == ""){
		errMsg = "Mensaje es un campo obligatotio";
		document.getElementById('mensaje').className='text_rojo';
		setfocus = "['mensaje']";
	}
	
	if (theForm['departamento'].value == ""){
		errMsg = "Departamento es un campo obligatotio";
		document.getElementById('departamento').className='text_rojo';
		setfocus = "['departamento']";
	}
	
	if (!validarEmail(theForm['email'].value)){
		errMsg = "Ingrese un E-Mail Válido, campo obligatorio";
		document.getElementById('email').className='text_rojo';
		setfocus = "['email']";
	}

	if (!telfRE1.test(theForm['telefono'].value)){
		errMsg = "Teléfono es un campo obligatotio y solo permite ingreso de números";
		document.getElementById('telefono').className='text_rojo';
		setfocus = "['telefono']";
	}
	
	if (theForm['direccion'].value == ""){
		errMsg = "Dirección es un campo obligatotio";
		document.getElementById('direccion').className='text_rojo';
		setfocus = "['direccion']";
	}
	
	if (theForm['ciudad'].value == ""){
		errMsg = "Ciudad es un campo obligatotio";
		document.getElementById('ciudad').className='text_rojo';
		setfocus = "['ciudad']";
	}
	
	if (theForm['apellidos'].value == ""){
		errMsg = "Apellidos es un campo obligatotio";
		document.getElementById('apellidos').className='text_rojo';
		setfocus = "['apellidos']";
	}
	
	if (theForm['nombres'].value == ""){
		errMsg = "Nombres es un campo obligatotio";
		document.getElementById('nombres').className='text_rojo';
		setfocus = "['nombres']";
	}
	
	if (errMsg != ""){
		alert(errMsg);
		eval("theForm" + setfocus + ".focus()");
	}
	
	else {
		theForm['enviar'].disabled = true;
		theForm['restablecer'].disabled = true;
		theForm.submit();
	}
}

function valida_encuesta(){
	var theForm = document.form_encuesta;
	var errMsg = "";
	var setfocus = "";
	
	var marcado = "no";
	with(theForm){
		for ( var i = 0; i < opcion.length; i++ ) {
			if ( opcion[i].checked ) {
				marcado = "si";
			}
		}
		if ( marcado == "no"){
			errMsg = "Debe escoger una Opción";
		}
	}
	
	if (errMsg != ""){
		alert(errMsg);
		eval("theForm" + setfocus + ".focus()");
	}
	
	else {
		theForm.submit();
	}
}

function validaRUC(campo) {          
   
      numero = campo;
	  /* alert(numero); */

      var suma = 0;      
      var residuo = 0;      
      var pri = false;      
      var pub = false;            
      var nat = false;      
      var numeroProvincias = 22;                  
      var modulo = 11;
                  
      /* Verifico que el campo no contenga letras */                  
      var ok=1;
      for (i=0; i<numero.length && ok==1 ; i++){
         var n = parseInt(numero.charAt(i));
         if (isNaN(n)) ok=0;
      }
      if (ok==0){
         return false;
      }
                  
      if (numero.length < 10 ){              
         return false;
      }
     
      /* Los primeros dos digitos corresponden al codigo de la provincia */
      provincia = numero.substr(0,2);      
      if (provincia < 1 || provincia > numeroProvincias){           
		 return false;       
      }

      /* Aqui almacenamos los digitos de la cedula en variables. */
      d1  = numero.substr(0,1);         
      d2  = numero.substr(1,1);         
      d3  = numero.substr(2,1);         
      d4  = numero.substr(3,1);         
      d5  = numero.substr(4,1);         
      d6  = numero.substr(5,1);         
      d7  = numero.substr(6,1);         
      d8  = numero.substr(7,1);         
      d9  = numero.substr(8,1);         
      d10 = numero.substr(9,1);                
         
      /* El tercer digito es: */                           
      /* 9 para sociedades privadas y extranjeros   */         
      /* 6 para sociedades publicas */         
      /* menor que 6 (0,1,2,3,4,5) para personas naturales */ 

      if (d3==7 || d3==8){           
         return false;
      }         
         
      /* Solo para personas naturales (modulo 10) */         
      if (d3 < 6){           
         nat = true;            
         p1 = d1 * 2;  if (p1 >= 10) p1 -= 9;
         p2 = d2 * 1;  if (p2 >= 10) p2 -= 9;
         p3 = d3 * 2;  if (p3 >= 10) p3 -= 9;
         p4 = d4 * 1;  if (p4 >= 10) p4 -= 9;
         p5 = d5 * 2;  if (p5 >= 10) p5 -= 9;
         p6 = d6 * 1;  if (p6 >= 10) p6 -= 9; 
         p7 = d7 * 2;  if (p7 >= 10) p7 -= 9;
         p8 = d8 * 1;  if (p8 >= 10) p8 -= 9;
         p9 = d9 * 2;  if (p9 >= 10) p9 -= 9;             
         modulo = 10;
      }         

      /* Solo para sociedades publicas (modulo 11) */                  
      /* Aqui el digito verficador esta en la posicion 9, en las otras 2 en la pos. 10 */
      else if(d3 == 6){           
         pub = true;             
         p1 = d1 * 3;
         p2 = d2 * 2;
         p3 = d3 * 7;
         p4 = d4 * 6;
         p5 = d5 * 5;
         p6 = d6 * 4;
         p7 = d7 * 3;
         p8 = d8 * 2;            
         p9 = 0;            
      }         
         
      /* Solo para entidades privadas (modulo 11) */         
      else if(d3 == 9) {           
         pri = true;                                   
         p1 = d1 * 4;
         p2 = d2 * 3;
         p3 = d3 * 2;
         p4 = d4 * 7;
         p5 = d5 * 6;
         p6 = d6 * 5;
         p7 = d7 * 4;
         p8 = d8 * 3;
         p9 = d9 * 2;            
      }
                
      suma = p1 + p2 + p3 + p4 + p5 + p6 + p7 + p8 + p9;                
      residuo = suma % modulo;                                         

      /* Si residuo=0, dig.ver.=0, caso contrario 10 - residuo*/
      digitoVerificador = residuo==0 ? 0: modulo - residuo;                

      /* ahora comparamos el elemento de la posicion 10 con el dig. ver.*/                         
      if (pub==true){           
         if (digitoVerificador != d9){                          
            return false;
         }                  
         /* El ruc de las empresas del sector publico terminan con 0001*/         
         if ( numero.substr(9,4) != '0001' ){                    
            return false;
         }
      }         
      else if(pri == true){         
         if (digitoVerificador != d10){                          
            return false;
         }         
         if ( numero.substr(10,3) != '001' ){                    
            return false;
         }
      }      

      else if(nat == true){         
         if (digitoVerificador != d10){                          
            return false;
         }         
         if (numero.length >10 && numero.substr(10,3) != '001' ){                    
            return false;
         }
      }      
      return true;   
   }

function valida_ingreso(){
	var theForm = document.form_crea_cuenta;
	var emailRE = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
	var telfRE = /^[0-9]{2,3}-? ?[0-9]{6,7}$/;
	var telfRE1 = /^[0-9]+$/;
	var telfRE2 = /^[0-9]*$/;
	var conRE = /^(?!^[0-9]*$)(?!^[a-zA-Z]*$)^([a-zA-Z0-9]{8,10})$/;
	var errMsg = "";
	var setfocus = "";
	
	if (theForm['tmptxt'].value == ""){
		errMsg = "Seguridad es un campo obligatotio";
		theForm['tmptxt'].className='text_rojo';
		setfocus = "['tmptxt']";
	}

	if (!conRE.test(theForm['cpassword'].value)){
		errMsg = "Repita su Contraseña es un campo obligatotio";
		theForm['cpassword'].className='text_rojo';
		setfocus = "['cpassword']";
	}
	
	if (!conRE.test(theForm['password'].value)){
		errMsg = "Contraseña es un campo obligatotio";
		theForm['password'].className='text_rojo';
		setfocus = "['password']";
	}
	
	if (!telfRE2.test(theForm['fax'].value)){
		errMsg = "Fax sólo permite ingreso de números";
		setfocus = "['fax']";
	}
	
	if (!telfRE2.test(theForm['movil'].value)){
		errMsg = "Celular sólo permite ingreso de números";
		setfocus = "['movil']";
	}
	
	if (!telfRE1.test(theForm['convencional'].value)){
		errMsg = "Teléfono es un campo obligatorio y sólo permite ingreso de números";
		theForm['convencional'].className='text_rojo';
		setfocus = "['convencional']";
	}
	
	if (theForm['direccion'].value == ""){
		errMsg = "Dirección es un campo obligatotio";
		theForm['direccion'].className='text_rojo';
		setfocus = "['direccion']";
	}
	
	if (theForm['sector'].value == ""){
		errMsg = "Sector es un campo obligatotio";
		theForm['sector'].className='text_rojo';
		setfocus = "['sector']";
	}
	
	if (theForm['ciudad'].value == ""){
		errMsg = "Ciudad es un campo obligatotio";
		theForm['ciudad'].className='text_rojo';
		setfocus = "['ciudad']";
	}
	
	if (theForm['provincia'].value == ""){
		errMsg = "Provincia es un campo obligatotio";
		theForm['provincia'].className='text_rojo';
		setfocus = "['provincia']";
	}
		
	if (!validarEmail(theForm['email'].value)){
		errMsg = "Debe ingresar un E-mail válido, es un campo obligatotio";
		theForm['email'].className='text_rojo';
		setfocus = "['email']";
	}
	
	if (theForm['apellidos'].value == ""){
		errMsg = "Apellido es un campo obligatotio";
		theForm['apellidos'].className='text_rojo';
		setfocus = "['apellidos']";
	}
	
	if (theForm['nombres'].value == ""){
		errMsg = "Nombre es un campo obligatotio";
		theForm['nombres'].className='text_rojo';
		setfocus = "['nombres']";
	}
	
	if (!validaRUC(theForm['cedula'].value)){
		errMsg = "Debe ingresar un RUC o No. de Cédula válido (sólo números)";
		theForm['cedula'].className='text_rojo';
		setfocus = "['cedula']";
	}
	
	if (theForm['password'].value != theForm['cpassword'].value){
		errMsg = "Contraseña y Repita su Contraseña No Coinciden";
		setfocus = "['password']";
	}
	
	if (errMsg != ""){
		alert(errMsg);
		eval("theForm" + setfocus + ".focus()");
	}
	
	else {
		theForm['registrarse'].disabled = true;
		theForm['registrarse'].value = "PROCESANDO...";
		theForm.submit();
	}
}

function recuerda_contrasenia(){
	var theForm = document.form_recuerda;
	var emailRE = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
	var errMsg = "";
	var setfocus = "";
	
	if (!validarEmail(theForm['E-Mail'].value)){
		errMsg = "Debe ingresar un E-mail válido, es un campo obligatotio";
		theForm['E-Mail'].className='text_rojo';
		setfocus = "['E-Mail']";
	}

	if (errMsg != ""){
		alert(errMsg);
		eval("theForm" + setfocus + ".focus()");
	}
	
	else {
		theForm['consultar'].disabled = true;
		theForm['consultar'].value = "VERIFICANDO...";
		theForm.submit();
	}
}

function valida_ingresar(){
	var theForm = document.form_login;
	var errMsg = "";
	var setfocus = "";
	
	if (theForm['contrasenia'].value == ""){
		errMsg = "Contraseña es un campo obligatotio";
		theForm['contrasenia'].className='text_rojo';
		setfocus = "['contrasenia']";
	}

	if (!validarEmail(theForm['email'].value)){
		errMsg = "Debe ingresar un E-mail válido, es un campo obligatotio";
		theForm['email'].className='text_rojo';
		setfocus = "['email']";
	}
	
	if (errMsg != ""){
		alert(errMsg);
		eval("theForm" + setfocus + ".focus()");
	}
	
	else {
		theForm['ingresar'].disabled = true;
		theForm['ingresar'].value = "VERIFICANDO...";
		theForm.submit();
	}
}

function valida_editamisdatos(){
	var theForm = document.form_misdatos;
	var emailRE = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
	var telfRE = /^[0-9]{2,3}-? ?[0-9]{6,7}$/;
	var telfRE1 = /^[0-9]+$/;
	var telfRE2 = /^[0-9]*$/;
	var conRE = /^(?!^[0-9]*$)(?!^[a-zA-Z]*$)^([a-zA-Z0-9]{8,10})$/;
	var errMsg = "";
	var setfocus = "";

	if (!telfRE2.test(theForm['fax'].value)){
		errMsg = "Fax sólo permite ingreso de números";
		theForm['fax'].className='text_rojo';
		setfocus = "['fax']";
	}
	
	if (!telfRE2.test(theForm['movil'].value)){
		errMsg = "Móvil sólo permite ingreso de números";
		theForm['movil'].className='text_rojo';
		setfocus = "['movil']";
	}

	if (!telfRE1.test(theForm['convencional'].value)){
		errMsg = "Debe escribir un Telefono Convencional válido";
		theForm['convencional'].className='text_rojo';
		setfocus = "['convencional']";
	}
	
	if (theForm['direccion'].value == ""){
		errMsg = "Dirección es un campo obligatotio";
		theForm['direccion'].className='text_rojo';
		setfocus = "['direccion']";
	}
	
	if (theForm['sector'].value == ""){
		errMsg = "Sector es un campo obligatotio";
		theForm['sector'].className='text_rojo';
		setfocus = "['sector']";
	}
	
	if (theForm['ciudad'].value == ""){
		errMsg = "Ciudad es un campo obligatotio";
		theForm['ciudad'].className='text_rojo';
		setfocus = "['ciudad']";
	}
	
	if (theForm['provincia'].value == ""){
		errMsg = "Provincia es un campo obligatotio";
		theForm['provincia'].className='text_rojo';
		setfocus = "['provincia']";
	}
		
	if (!validarEmail(theForm['email'].value)){
		errMsg = "Debe ingresar un E-mail válido, es un campo obligatotio";
		theForm['email'].className='text_rojo';
		setfocus = "['email']";
	}
	
	if (theForm['apellidos'].value == ""){
		errMsg = "Apellido es un campo obligatotio";
		theForm['apellidos'].className='text_rojo';
		setfocus = "['apellidos']";
	}
	
	if (theForm['nombres'].value == ""){
		errMsg = "Nombre es un campo obligatotio";
		theForm['nombres'].className='text_rojo';
		setfocus = "['nombres']";
	}
	
	if (errMsg != ""){
		alert(errMsg);
		eval("theForm" + setfocus + ".focus()");
	}
	
	else {
		theForm['guardar'].disabled = true;
		theForm['guardar'].value = "PROCESANDO...";
		theForm['cancelar'].disabled = true;
		theForm.submit();
	}
}

function verifica_clave(){
	var theForm = document.form_cambia_clave;
	var conRE = /^(?!^[0-9]*$)(?!^[a-zA-Z]*$)^([a-zA-Z0-9]{8,10})$/;
	var errMsg = "";
	var setfocus = "";
	
	if (!conRE.test(theForm['Confirma'].value)){
		errMsg = "Repita Contraseña es un campo obligatotio\n y debe cumplir con la forma del ejemplo";
		theForm['Confirma'].className='text_rojo';
		setfocus = "['Confirma']";
	}
	
	if (!conRE.test(theForm['Nueva'].value)){
		errMsg = "Nueva Contraseña es un campo obligatotio\n y debe cumplir con la forma del ejemplo";
		theForm['Nueva'].className='text_rojo';
		setfocus = "['Nueva']";
	}
	
	if (theForm['Actual'].value == ""){
		errMsg = "Contraseña Actual es un campo obligatorio";
		theForm['Actual'].className='text_rojo';
		setfocus = "['Actual']";
	}
	
	if (theForm['Confirma'].value != theForm['Nueva'].value){
		errMsg = "No coincide Nueva Contraseña y Repita Contraseña";
		setfocus = "['Nueva']";
	}
	
	if (errMsg != ""){
		alert(errMsg);
		eval("theForm" + setfocus + ".focus()");
	}
	
	else {
		theForm['Enviar'].disabled = true;
		theForm['Enviar'].value = "PROCESANDO...";
		theForm['cancelar'].disabled = true;
		theForm.submit();
	}
}
function valida_pasantes(){
	var theForm = document.form_pasantes;
	var emailRE = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
	var telfRE = /^[0-9]{2,3}-? ?[0-9]{6,7}$/;
	var telfRE1 = /^[0-9]+$/;
	var errMsg = "";
	var setfocus = "";
	
	if (theForm['tmptxt'].value == ""){
		errMsg = "Seguridad es un campo obligatotio";
		theForm['tmptxt'].className='text_rojo';
		setfocus = "['tmptxt']";
	}
	
	if (theForm['hojavida'].value == ""){
		errMsg = "Currículum es un campo obligatotio";
		theForm['hojavida'].className='text_rojo';
		setfocus = "['hojavida']";
	}
	
	if (!validarEmail(theForm['email'].value)){
		errMsg = "Ingrese un E-Mail Válido, campo obligatorio";
		theForm['email'].className='text_rojo';
		setfocus = "['email']";
	}

	if (!telfRE1.test(theForm['telefono'].value)){
		errMsg = "Teléfono es un campo obligatotio y solo permite ingreso de números";
		theForm['telefono'].className='text_rojo';
		setfocus = "['telefono']";
	}
	
	if (theForm['direccion'].value == ""){
		errMsg = "Dirección es un campo obligatotio";
		theForm['direccion'].className='text_rojo';
		setfocus = "['direccion']";
	}
	
	if (theForm['ciudad'].value == ""){
		errMsg = "Ciudad es un campo obligatotio";
		theForm['ciudad'].className='text_rojo';
		setfocus = "['ciudad']";
	}
	
	if (theForm['apellidos'].value == ""){
		errMsg = "Apellidos es un campo obligatotio";
		theForm['apellidos'].className='text_rojo';
		setfocus = "['apellidos']";
	}
	
	if (theForm['nombres'].value == ""){
		errMsg = "Nombres es un campo obligatotio";
		theForm['nombres'].className='text_rojo';
		setfocus = "['nombres']";
	}
	
	if (errMsg != ""){
		alert(errMsg);
		eval("theForm" + setfocus + ".focus()");
	}
	
	else {
		theForm['enviar'].disabled = true;
		theForm['enviar'].value = "PROCESANDO...";
		theForm['restablecer'].disabled = true;
		theForm.submit();
	}
}
