// JavaScript Document

//*********************************Validar Campos Vacios***************************************
function vacio(cadena)
  {                                    // DECLARACION DE CONSTANTES
    var blanco = " \n\t" + String.fromCharCode(13); // blancos
                                       // DECLARACION DE VARIABLES
    var i;                             // indice en cadena
    var es_vacio;                      // cadena es vacio o no
    for(i = 0, es_vacio = true; (i < cadena.length) && es_vacio; i++) // INICIO
      es_vacio = blanco.indexOf(cadena.charAt(i)) != - 1;
    return(es_vacio);
  }  
 //*********************************************************************************************
 
//*************************************Validar si es mail***************************************
function email(cadena, otros)
  {                                    // DECLARACION-INICIALIZACION VARIABLES
    var i, j;                          // indice en cadena
    var es_email = 0 < cadena.length;  // cadena es email o no
    i = salta_alfanumerico(cadena, 0, otros); // INICIO
    if(es_email = 0 < i)               // lee "alfanum*"
      if(es_email = (i < cadena.length))
        if(es_email = cadena.charAt(i) == '@') // lee "alfanum@*"
          {
            i++;
            j = salta_alfanumerico(cadena, i, otros);
            if(es_email = i < j)       // lee "alfanum@alfanum*"
              if(es_email = j < cadena.length)
                if(es_email = cadena.charAt(j) == '\.')
                  {                    // lee "alfanum@alfanum.*"
                    j++;
                    i = salta_alfanumerico(cadena, j, otros);
                    if(es_email = j < i) // lee "alfanum@alfanum.alfanum*"
                      while(es_email && (i < cadena.length))
                        if(es_email = cadena.charAt(i) == '\.')
                          {
                            i++;
                            j = salta_alfanumerico(cadena, i, otros);
                            if(es_email = i < j) // lee "alfanum@alfanum.alfanum[.alfanum]*"
                              i = j;
                          }
                  }
          }
    return(es_email);
  } 
 
function salta_alfanumerico(cadena, i, otros)
  {                                    // DECLARACION DE VARIABLES
    var j;                             // indice en cadena
    var car;                           // caracter de cadena
    var alfanum;                       // cadena[j] es alfanumerico u otros
    for(j = i, alfanum = true; (j < cadena.length) && alfanum; j++) // INICIO
      {
        car = cadena.charAt(j);
        alfanum = alfanumerico(car) || (otros.indexOf(car) != -1);
      }
    if(!alfanum)                       // lee "alfanumX"
      j--;
    return(j);
  }
  
function alfanumerico(car){	
    return(alfabetico(car) || numerico(car));
}
  
function alfabetico(car){              
	// DECLARACION DE CONSTANTES
    // caracteres alfabeticos
    var alfa = "ABCDEFGHIJKLMNOPQRSTUWXYZabcdefghijklmnopqrstuvxyz";
    return(alfa.indexOf(car) != - 1);  // INICIO
}
  
function numerico(car){                                    
	// DECLARACION DE CONSTANTES
    var num = "0123456789";            // caracteres numericos
    return(num.indexOf(car) != - 1);   // INICIO
}
  
//*********************************************************************************************

//Validar Formulario Persona Jurídica

function ValidaJuridico(form){
	
	if(form.tipoemp.value == '0'){
    	alert("Debe indicar el tipo de persona Jurídica.");
	}	
	else if(vacio(form.rif.value)){
    	alert("Debe indicar el Rif.");
	}
	else if(vacio(form.razon.value)){
      	alert("Debe indicar la Razón Social de la Empresa.");
	}
	else if(vacio(form.actividad.value)){
      	alert("Debe indicar la actividad comercial de la Empresa.");
	}	
	else if(vacio(form.direccion.value)){
      	alert("Debe indicar la dirección fiscal de la Empresa.");
	}	
	else if(vacio(form.codtelefe.value)){
      	alert("Debe indicar el código de área del telefono de la Empresa.");
	}
	else if(vacio(form.telefonoe.value)){
      	alert("Debe indicar el teléfono de la Empresa.");
	}
	else if(vacio(form.codfaxe.value)){
      	alert("Debe indicar el código de área del fax de la Empresa.");
	}
	else if(vacio(form.faxe.value)){
      	alert("Debe indicar el fax de la Empresa.");
	}
	else if(!email(form.correoe.value, "-_")){
      alert("Dirección de correo electronico incorrecta de la empresa.");
	}
	else if(form.autorizado.value == '0'){
    	alert("Si usted no está debidamente autorizado por la empresa le sugiero que haga un registro como Persona Natural.");
	}
	else if(vacio(form.cargo.value)){
      	alert("Debe indicar el cargo en la Empresa.");
	}
	else if(vacio(form.departamento.value)){
      	alert("Debe indicar el departamento en la Empresa.");
	}	
	
	//Datos Registro de la Empresa
	else if(vacio(form.cedularl.value)){
      	alert("Debe indicar la cédula del representante legal.");
	}	
	else if(vacio(form.cregistro.value)){
      	alert("Debe indicar el circuito.");
	}	
	else if(vacio(form.nregistro.value)){
      	alert("Debe indicar el numero del registro.");
	}	
	else if(vacio(form.tomo.value)){
      	alert("Debe indicar el tomo del registro.");
	}	
	else if(vacio(form.ano.value)){
      	alert("Debe indicar el año del registro.");
	}	
	else if(vacio(form.representantelegal.value)){
      	alert("Debe indicar el representante legal.");
	}	
	
	//Datos de la persona responsable de la Cuenta
	
	else if(form.nacionalidad.value == '0'){
    	alert("Debe seleccionar la nacionalidad.");
	}
	else if(vacio(form.cedula.value)){
    	alert("Debe indicar la Cédula.");
	}
	else if(vacio(form.nombres.value)){
    	alert("Debe indicar sus Nombres.");
	}
	else if(vacio(form.apellidos.value)){
    	alert("Debe indicar sus Apellidos.");
	}
	else if(vacio(form.fecha.value)){
    	alert("Debe indicar la Fecha de Nacimiento.");
	}
	else if(form.profesion.value == '0'){
    	alert("Debe seleccionar su Profesión.");
	}
	else if(form.pais.value == '0'){
    	alert("Debe seleccionar su País de origen.");
	}
	else if(form.estado.value == '0'){
    	alert("Debe seleccionar su Estado de origen.");
	}
	else if(form.ciudad.value == '0'){
    	alert("Debe seleccionar su Ciudad de origen.");
	}
	else if(vacio(form.urbanizacion.value)){
    	alert("Debe indicar la urbanizacion donde vive.");
	}
	else if(vacio(form.calle.value)){
    	alert("Debe indicar la calle donde vive.");
	}
	else if(form.tipoinm.value == '0'){
    	alert("Debe seleccionar el tipo de Inmueble.");
	}
	else if(vacio(form.inmueble.value)){
    	alert("Debe indicar el nombre del Inmueble.");
	}
	else if(vacio(form.codtelf.value)){
      	alert("Debe indicar el código de área de su telefono.");
	}
	else if(vacio(form.telefono.value)){
      	alert("Debe indicar su teléfono.");
	}
	else if(form.codcel.value == '0'){
    	alert("Debe seleccionar el código de su Celular.");
	}
	else if(!email(form.correo.value, "-_")){
      alert("Dirección de correo electrónico, persona responsable incorrecta.");
	}
	else if(vacio(form.usuario.value)){
      	alert("Debe indicar un usuario.");
	}
	else if(vacio(form.clave.value)){
      	alert("Debe indicar una Clave.");
	}
	else if(!(form.clave.value == form.clave2.value)){
      	alert("La clave no es la misma que el campo de verificación.");
	}
	else{
		return(true);
	}

return (false);
}

// Validar Formulario Persona Natural

function ValidaNatural(form){	
	
	//Datos de la persona responsable de la Cuenta
	
	if(form.nacionalidad.value == '0'){
    	alert("Debe seleccionar la nacionalidad.");
	}
	else if(vacio(form.cedula.value)){
    	alert("Debe indicar la Cédula.");
	}
	else if(vacio(form.nombres.value)){
    	alert("Debe indicar sus Nombres.");
	}
	else if(vacio(form.apellidos.value)){
    	alert("Debe indicar sus Apellidos.");
	}
	else if(vacio(form.fecha.value)){
    	alert("Debe indicar la Fecha de Nacimiento.");
	}
	else if(form.profesion.value == '0'){
    	alert("Debe seleccionar su Profesión.");
	}
	else if(form.pais.value == '0'){
    	alert("Debe seleccionar su País de origen.");
	}
	else if(form.estado.value == '0'){
    	alert("Debe seleccionar su Estado de origen.");
	}
	else if(form.ciudad.value == '0'){
    	alert("Debe seleccionar su Ciudad de origen.");
	}
	else if(vacio(form.urbanizacion.value)){
    	alert("Debe indicar la urbanizacion donde vive.");
	}
	else if(vacio(form.calle.value)){
    	alert("Debe indicar la calle donde vive.");
	}
	else if(form.tipoinm.value == '0'){
    	alert("Debe seleccionar el tipo de Inmueble.");
	}
	else if(vacio(form.inmueble.value)){
    	alert("Debe indicar el nombre del Inmueble.");
	}
	else if(vacio(form.codtelf.value)){
      	alert("Debe indicar el código de área de su telefono.");
	}
	else if(vacio(form.telefono.value)){
      	alert("Debe indicar su teléfono.");
	}
	else if(form.codcel.value == '0'){
    	alert("Debe seleccionar el código de su Celular.");
	}
	else if(vacio(form.celular.value)){
      	alert("Debe indicar su Celular.");
	}
	else if(!email(form.correo.value, "-_")){
      alert("Dirección de correo electrónico, persona responsable incorrecta.");
	}
	else if(vacio(form.usuario.value)){
      	alert("Debe indicar un usuario.");
	}
	else if(vacio(form.clave.value)){
      	alert("Debe indicar una Clave.");
	}
	else if(!(form.clave.value == form.clave2.value)){
      	alert("La clave no es la misma que el campo de verificación.");
	}
	else{
		return(true);
	}

return (false);
}

//---------------- Permitir solo Numeros--------------------------------

function SoloNumeros(obj){
      var tecla = window.event.keyCode;
      if(48<=tecla && tecla<=57){
      }
       else{
            window.event.keyCode=0;
          }
}


function ValidarRif(obj){
	var tecla = window.event.keyCode;
	
	var posicion = obj.value.length;
	
	//alert (posicion);	
	var tecla = window.event.keyCode;
     
	  if(48<=tecla && tecla<=57 && obj.value.length <=7){
      }
       else{
            if (tecla==45 && obj.value.length == 8){
			}
			else{
				if(48<=tecla && tecla<=57 && obj.value.length == 9){
				}
				else{
					window.event.keyCode=0;
				}
			}
           }	
}

function IsNumeric(valor) 
{ 
var log=valor.length; var sw="S"; 
for (x=0; x<log; x++) 
{ v1=valor.substr(x,1); 
v2 = parseInt(v1); 
//Compruebo si es un valor numérico 
if (isNaN(v2)) { sw= "N";} 
} 
if (sw=="S") {return true;} else {return false; } 
} 
var primerslap=false; 
var segundoslap=false; 
function formateafecha(fecha) 
{ 
var long = fecha.length; 
var dia; 
var mes; 
var ano; 
if ((long>=2) && (primerslap==false)) { dia=fecha.substr(0,2); 
if ((IsNumeric(dia)==true) && (dia<=31) && (dia!="00")) { fecha=fecha.substr(0,2)+"/"+fecha.substr(3,7); primerslap=true; } 
else { fecha=""; primerslap=false;} 
} 
else 
{ dia=fecha.substr(0,1); 
if (IsNumeric(dia)==false) 
{fecha="";} 
if ((long<=2) && (primerslap=true)) {fecha=fecha.substr(0,1); primerslap=false; } 
} 
if ((long>=5) && (segundoslap==false)) 
{ mes=fecha.substr(3,2); 
if ((IsNumeric(mes)==true) &&(mes<=12) && (mes!="00")) { fecha=fecha.substr(0,5)+"/"+fecha.substr(6,4); segundoslap=true; } 
else { fecha=fecha.substr(0,3);; segundoslap=false;} 
} 
else { if ((long<=5) && (segundoslap=true)) { fecha=fecha.substr(0,4); segundoslap=false; } } 
if (long>=7) 
{ ano=fecha.substr(6,4); 
if (IsNumeric(ano)==false) { fecha=fecha.substr(0,6); } 
else { if (long==10){ if ((ano==0) || (ano<1900) || (ano>2100)) { fecha=fecha.substr(0,6); } } } 
} 
if (long>=10) 
{ 
fecha=fecha.substr(0,10); 
dia=fecha.substr(0,2); 
mes=fecha.substr(3,2); 
ano=fecha.substr(6,4); 
// Año no viciesto y es febrero y el dia es mayor a 28 
if ( (ano%4 != 0) && (mes ==02) && (dia > 28) ) { fecha=fecha.substr(0,2)+"/"; } 
} 
return (fecha); 
}
