// 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 ValidaFormulario(form){	
	if(vacio(form.nombres.value)){
    	alert("Debe indicar un nombre.");
	}
	else if(vacio(form.apellidos.value)){
      	alert("Debe indicar un apellido.");
	}
	
	else if(!email(form.correo.value, "-_")){
      alert("Dirección de correo electronico incorrecta.");
	}
	else if(vacio(form.fecha.value)){
      	alert("Debe indicar su fecha de nacimiento.");
	}	
	else if(form.color.value == '0'){
    	alert("Seleccione su Color Favorito.");
	}
	else if(form.ocasion.value == '0'){
    	alert("Seleccione una Ocasió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); 
}
