/*
Modificaciones
isdate(), 2 mayo 2001, 13:05 hrs.
*/

// ______________________________________________________________________
// Revisa si un campo tiene un valor, de lo contrario
function esVacio(valor)
{
// alert("esVacio");
 var string1=valor;
 var temp = "";

 string = '' + string1;
 splitstring = string.split(" ");
 for(i = 0; i < splitstring.length; i++)
  temp += splitstring[i];

 if (temp.length==0)
  return true;
 else
  return false;
}


// ____________________________________________________________________________
// Valida que los datos del campo sean números
// PARAMETROS:
// forma: nombre de la forma (this.form.name)
// campo: nombre del campo (this.name)
//
// SALIDAS:
// True: Si el campo tiene datos validos (únicamente números)
// False: Si el campo tiene datos invalidos 

function isInt(str)
{
 // if(str.length==0) 
 // return false;
//  var str = eval("document."+forma+"."+campo+".value")
  var RefString="0123456789";
//  var invalidos="                   & ' \ * ´ % # + ° ~ [ ] { } ¿ ? ¡ ! ";

  for(Count=0;Count < str.length;Count++)
  {
    TempChar = str.substring(Count,Count+1);
    if(RefString.indexOf(TempChar,0)==-1)
    {
//      alert("El campo solo puede contener números enteros");
//      eval("document."+forma+"."+campo+".focus()");
//      eval("document."+forma+"."+campo+".select()");
      return false;
    }
  }
  return true;
}

// ____________________________________________________________________________

function isDec(str)
{
  var RefString=".0123456789";
  var point	= 0;
  for(Count=0;Count < str.length;Count++)
  {
    TempChar = str.substring(Count,Count+1);
    if(RefString.indexOf(TempChar,0)==-1)
    { return false; }
	if(TempChar==".")
		point= point+1;
	if(parseInt(point)>1)	
    { return false; }
  }
  return true;
}


// ____________________________________________________________________________

function isChar(str)
{
  var RefString=" AÁBCDEÉFGHIÍJKLMNÑOÓPQRSTUÚVWXYZaábcdeéfghiíjklmnñoópqrstúuvwxyz&";

  for(Count=0;Count < str.length;Count++)
  {
    TempChar = str.substring(Count,Count+1);
    if(RefString.indexOf(TempChar,0)==-1)
    { return false; }
  }
  return true;
}

function isCharInt(str)
{
  var RefString="ABCDEFGHIJKLMNÑOPQRSTUVWXYZ01234567890";

  for(Count=0;Count < str.length;Count++)
  {
    TempChar = str.substring(Count,Count+1);
    if(RefString.indexOf(TempChar,0)==-1)
    { return false; }
  }
  return true;
}

// ____________________________________________________________________________
// Descripcion: Verifica que la fecha sea válida con el formato dd/mm/aaaa


function isdate(strValue)
{
//Para validar la fecha en el formato mm/dd/aaaa
//escribir en la variable strFormatoFecha = "US"

//Para validar la fecha en el formato dd/mm/aaaa
//escribir en la variable strFormatoFecha = ""
  var strFormatoFecha = "";
  var strFecha;
  var arrFecha;
  var strDia;
  var strMes;
  var strAnio;
  var intDia;
  var intMes;
  var intAnio;
  var booFound = false;
 // var cmpFecha = objFecha;
  var arrSeparador = new Array("-","/",".");
  var elementos;
  var error = 0;
  strFecha = strValue;
/////////////////////////////////////////////////
 // if (strFecha.length < 1)
 // { return true; }
//JAVIER	
//  alert(strValue);
  if (strFecha.length < 11 && strFecha.length > 5)
  { 
/////////////////////////////////////////////////
  for (elementos = 0; elementos < arrSeparador.length; elementos++)
  {
    if (strFecha.indexOf(arrSeparador[elementos]) != -1)
    {
      arrFecha = strFecha.split(arrSeparador[elementos]);
// Si los elementos del arreglo son diferente de 3 elementos ejemplo: 01/01/200/34/23/4
      if (arrFecha.length != 3)
      {
        error = 1;
        return false;
      }

      else
      {
        strDia = arrFecha[0];
        strMes = arrFecha[1];
        strAnio = arrFecha[2];
      }
      booFound = true;
    }

  }

//alert(strDia+" "+strMes+" "+strAnio)

/////////////////////////////////////////////////

  if (booFound == false)
  {
    if (strFecha.length>5)
    {
      strDia = strFecha.substr(0, 2);
      strMes = strFecha.substr(2, 2);
      strAnio = strFecha.substr(4);
    }
  }
/////////////////////////////////////////////////
// Si el año es de 2 digitos agrega 20 ejemplo: si es 01 entonces 2001
  if (strAnio.length == 2)
  { strAnio = '20' + strAnio; }

// Si el año es 0000
  if (parseInt(strAnio) <= 1900 )
  { return false }

/////////////////////////////////////////////////

  // Formato (mm/dd/aaaa)
  if (strFormatoFecha == "US")
  {
    strTemp = strDia;
    strDia = strMes;
    strMes = strTemp;
  }



/////////////////////////////////////////////////

  intDia = parseInt(strDia, 10);

  if (isNaN(intDia))
  {
    error = 2;
    return false;
  }

/////////////////////////////////////////////////

  intMes = parseInt(strMes, 10);

  if (isNaN(intMes))
  {
    error = 3;
    return false;
  }


/////////////////////////////////////////////////

  intAnio = parseInt(strAnio, 10);

  if (isNaN(intAnio))
  {
    error = 4;
    return false;
  }

/////////////////////////////////////////////////

  if (intMes>12 || intMes<1)
  {
    error = 5;
    return false;
  }

/////////////////////////////////////////////////

  if ((intMes == 1 || intMes == 3 || intMes == 5 || intMes == 7 || intMes == 8 || intMes == 10 || intMes == 12) && (intDia > 31 || intDia < 1))
  {
    error = 6;
    return false;
  }

/////////////////////////////////////////////////

  if ((intMes == 4 || intMes == 6 || intMes == 9 || intMes == 11) && (intDia > 30 || intDia < 1))
  {
    error = 7;
    return false;
  }

/////////////////////////////////////////////////

  if (intMes == 2)
  {
    if (intDia < 1)
    {
      error = 8;
      return false;
    }

    if (esAnioBisiesto(intAnio) == true)
    {
      if (intDia > 29)
      {
        error = 9;
        return false;
      }
    }

    else
    {
      if (intDia > 28)
      {
        error = 10;
        return false;
      }
    }
  }
/////////////////////////////////////////////////
return true;
}

else
{ return false }

}
//_____________________________________________________________________________
// Año bisiesto
function esAnioBisiesto(intAnio)
{
  if (intAnio % 100 == 0)
  {
    if (intAnio % 400 == 0)
    { return true; }
  }

  else
  {
    if ((intAnio % 4) == 0)
    { return true; }
  }

  return false;
}

/*
respaldo de isdate()

 function isdate()
{
  var pos,dia,mes,ano,fecha,longi,flag;

  fecha=strValue;
  //alert(fecha);
  pos=fecha.indexOf("/",0);
  if(pos==-1)
    return(false);

  dia=fecha.substring(0,pos);
  fecha=fecha.substring(pos+1,fecha.length);
  pos=fecha.indexOf("/",0);
  if(pos==-1 || (dia.length>2))
    return(false);

  mes=fecha.substring(0,pos);
  ano=fecha.substring(pos+1,fecha.length);
  if((mes.length > 2) || (ano.length > 4))
    return(false);
  if(ano.length != 4)
    return(false);
 
  flag=isdigit(dia);
  if(!flag)
    return(false);

  flag=isdigit(mes);
  if(!flag)
    return(false);

  flag=isdigit(ano);
  if(flag)
  {
    if((parseInt(mes,10)>=1 && parseInt(mes,10)<=12) && (parseInt(dia,10)>=1 && parseInt(dia,10)<=31))
    {
      if((",1,3,5,7,8,10,12,".indexOf(","+ parseInt(dia) +",",0)> -1) && (parseInt(mes)>31))
        return(false);
      else
     {
       if((",4,6,9,11,".indexOf(","+ parseInt(dia) +",",0)>-1) && (parseInt(mes)>30))
         return(false);

       if(parseInt(mes)==2)
       {
         if((parseInt(ano) % 4)==0 && (parseInt(dia)>29))
           return(false);
         else
         {
           if((parseInt(ano) % 4)!=0 && (parseInt(dia)>28))
             return(false);
         } // no bisiesto
       } // mes de febrero
     } //meses de 30 dias
   } //meses de 31 dias
   else
    return(false);
 }
  return true;    //no error
}
*/
// ____________________________________________________________________________

 function checaVacios(frm) {
  var i;
  var bandera=true;
  alert(frm.elements.length);
  for(i=0; i<frm.elements.length; i++) {
   if(frm.elements[i].value=="") {
    bandera=false;
	//alert(frm.elements[i].name);
    alert('El Campo no puede estar vacio');
    frm.elements[i].focus();
    break;
   }
  }
  if(bandera==true)
   return true;
  else
   return false;
 }


// ______________________________________________________________________
// Revisa si un campo tiene un valor, de lo contrario
// Se redirecciona al campo y se regresa un false

function revisaCampo(campo)
{
  var cadena=campo.value;
 if (esVacio(cadena))
 {
//alert("\n " + mensaje);
  campo.focus();
  return false;
 }
 else
  return true;
}


// ______________________________________________________________________
// Revisa si un campo tiene un valor de fecha válido, de lo contrario
// Se redirecciona al campo y se regresa un false

function revisaFecha(campoFecha)
{
 if (!isdate(campoFecha.value))
 {
  alert("\nLa fecha es incorrecta.\nVerifique que el formato sea dd/mm/aaaa");
  campoFecha.focus();
  return false;
 }
 return true;
}


  ////////////////////////////////////////////////////////////
  //	Funcion	: capturaFecha(strFecha)
  //	Desc	 	: valida que la sintaxis de la fecha sea correcta
  //				  en el formato dd/mm/yyyy
  // objeto	: es el objeto con el campo a validar
  ////////////////////////////////////////////////////////////
function	capturaFecha(objeto)
{
	var iDia;
	var iMes;
	var iAnio;
	
	var strDia 	= "";
	var strMes 	= "";
	var strAnio = "";
	var caso= 0;
	var strFecha = objeto.value;
	var error = false;
	
	formatoFecha = /\d{1,2}\/\d{1,2}\/\d{4}/;

	if (formatoFecha.test(strFecha) == false)
	{
	alert("\nLa fecha es incorrecta.\nVerifique que el formato sea dd/mm/aaaa y que sea una fecha valida");
	objeto.focus();
	return false;
	}

	for( i= 0; i < strFecha.length; i++)
	{
		if (strFecha.charAt(i) != "/" )
		{
			switch(caso)
			{
				case 0: strDia = strDia + strFecha.charAt(i);
					break;
				case 1: strMes = strMes + strFecha.charAt(i);
					break;
				case 2: strAnio= strAnio + strFecha.charAt(i);
					break;				
			}				
		}
		else
			caso++;
	}

	iDia 	= parseInt(strDia);
	iMes	= parseInt(strMes);
	iAnio	= parseInt(strAnio);

	if (iMes <= 12 && iDia <= 31) {
		switch (iMes) {
			case 1: case 3: case 5: case 7: case 8: case 10: case 12: 
					if (iDia > 31)
						error = true;
					break;
			case 4: case 6: case 9: case 11: 
			 		if (iDia > 30)
						error = true;
					 break;
			case 2 : 
					if (iAnio % 4 == 0) {
						if (iDia > 29)
							error = true;
					}
				  	else {
					  if (iDia > 28)
							error = true;
				  }
				  break;
		}
	}
	else
  		error = true;
  	
  	if (error)
  	{
	alert("\nLa fecha es incorrecta.\nVerifique que el formato sea dd/mm/aaaa y que sea una fecha valida");
	objeto.focus();
	return false;			
	}
  		
	return true;
}

//____________________________________________________________________________
// Valida que la direccion de correo sea válida
// Realizada con área de Internet

function validaCorreo(correoTmp,Requerido,Control)
{
//	alert("ValidaCorreo")
//	var correoTmp;  //correo a validar
//	var Requerido = 1;  //bandera de opcional el email
	var cIncorrecto = 0;
	var nCaract;
	var Caracteres = ".@-_1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
	var CaracteresNoInicio = ".-_";
	var arrDComunes = new Array("hotmail.com");

	splitstring = correoTmp.split(";");
	for(i = 0; i < splitstring.length; i++)
	{
	  if(splitstring[i].length==0 && i>0 && splitstring.length==i+1) continue;
	 	
	 	//Valida que el correo contenga caracteres válidos
		for (j = 0; j < splitstring[i].length; j++)
		{
//		alert( splitstring[i].charAt(j) );
			if (Caracteres.indexOf(splitstring[i].charAt(j)) == -1)
				{
				cIncorrecto = 1
				}
		}
		
		if(cIncorrecto == 0)
			{
			//Cuenta el numero de @'s
			splitstringArro = splitstring[i].split("@");
			if(splitstringArro.length==2)
				{ 
				//Que contiene solo una arroba
				//Separa el correo en dos secciones
				for(j = 0; j < splitstringArro.length;j++)
				{
				  if (splitstringArro[j].length==0)
				   {
				  	  cIncorrecto = 1
					  continue;
				   }
				
				 // Valida el correo,despues de la @
				//Separa la informacionpor cada punto
					splitstringPto = splitstringArro[j].split(".");
					//Valida que haya texto despues de un punto
					for(k=0;k<splitstringPto.length;k++)
						{
						if(splitstringPto[k].length==0  || CaracteresNoInicio.indexOf(splitstringPto[k].charAt(0)) >=0 )
							cIncorrecto = 1
						else
						if(splitstringPto.length < 2 && j==1)
						cIncorrecto = 1
						}
					//Si es un correo de losdominios "comunes", valida q sea correcto
					if(cIncorrecto == 0 && j==1)
					{
						for(k=0;k<arrDComunes.length;k++)
						{
							splitstringPtoTmp = arrDComunes[k].split(".");
							if(splitstringPto[0]==splitstringPtoTmp[0])
								{
								if(splitstringArro[j] != arrDComunes[k])
								cIncorrecto = 1
								}
						}
					}
					
				}
			}else
				{
				cIncorrecto = 1
				}
		}
	}
	
	
	if( cIncorrecto == 0  ||  (correoTmp.length==0 && Requerido==0) )
	{
	return true;
	}
	else 
	{
	alert("Correo Incorrecto...")
	Control.value = ""
	if(Control!="") eval(Control + ".focus();" );
	return false;
	}

}