// JavaScript Document
function IsMail(YourMail)
{
	var Template = /^[a-z][a-z-_0-9.]+@[a-z-_=>0-9.]+.[a-z]{2,3}$/i //Formato de direccion de correo electronico
	
	if(YourMail.value!="") 
	{
		if (Template.test(YourMail.value)) 
		{							}
		else
		{   alert("El Email que ha Ingresado tiene Caracteres Invalidos");
			YourMail.focus();
			YourMail.select();
		}		
	}
	return false;
}


function IsTexto(texto)
{
	var Template = /^[a-zA-Z ]+$/i //Formato de alfanumerico
	if(texto.value!="") 
	{
		if (Template.test(texto.value)) 
		{							}
		else
		{   alert("El Nombre que ha Ingresado tiene Caracteres Invalidos");
			texto.focus();
			texto.select();
		}		
	}		
	return false;
}

function IsDay(day)
{
	if(day.value!="") 
	{
		if ( (isNaN(day.value)) || (day.value<1) || (day.value >31) )
		{
			alert("No es un Dia Valido");
			day.focus();
			day.select();			
		}
	}
	
	return false;	
}

function IsMonth(month)
{
	if(month.value!="") 
	{
		if ( (isNaN(month.value)) || (month.value<1) || (month.value >12) )
		{
			alert("No es un Mes Valido");
			month.focus();
			month.select();
		}
	}	
	return false;	
}

function IsYear(year)
{
	if(year.value!="") 
	{
		if ( (isNaN(year.value)) || (year.value<1900) || (year.value >2000) )
		{
			alert("No es un año Valido. Ingrese entre 1900-2000");
			year.focus();
			year.select();
		}		
	}	
	return false;	
}

function SpecialKeypress(e)
{
	// http://www.lookuptables.com/
	var arrSKey = new Array(51);
    arrSKey[0]  = 33 // !
    arrSKey[1]  = 34 // "
	arrSKey[2]  = 35 // #
	arrSKey[3]  = 36 // $
    arrSKey[4]  = 37 // %
	arrSKey[5]  = 38 // &
    arrSKey[6]  = 39 // '
    arrSKey[7]  = 40 // (
    arrSKey[8]  = 41 // )
    arrSKey[9]  = 42 // *
    arrSKey[10]  = 43 // +
    arrSKey[11]  = 44 // ,
    arrSKey[12]  = 47 // /
	arrSKey[13]  = 58 // :
	arrSKey[14]  = 59 // ;
    arrSKey[15] = 60 // <
    arrSKey[16] = 61 // =
    arrSKey[17] = 62 // >
	arrSKey[18] = 63 // ?
	arrSKey[19] = 64 // @
    arrSKey[20] = 91 // [
    arrSKey[21] = 92 // \
    arrSKey[22] = 93 // ]
    arrSKey[23] = 94 // ^
	arrSKey[24] = 96 // `
    arrSKey[25] = 123 // {
    arrSKey[26] = 124 // |
    arrSKey[27] = 125 // }
	arrSKey[28] = 126 // ~
	arrSKey[29] = 161 // ¡
	arrSKey[30] = 162 
	arrSKey[31] = 163 
	arrSKey[32] = 164 
	arrSKey[33] = 165 
	arrSKey[34] = 166 
	arrSKey[35] = 167 
	arrSKey[36] = 168 
	arrSKey[37] = 169 
	arrSKey[38] = 170 
	arrSKey[39] = 171 
	arrSKey[40] = 172 
	arrSKey[41] = 173 
	arrSKey[42] = 174 
	arrSKey[43] = 175 
	arrSKey[44] = 176 
	arrSKey[45] = 177 
	arrSKey[46] = 178 
	arrSKey[47] = 179 
	arrSKey[48] = 180 
	arrSKey[49] = 181 
	arrSKey[50] = 182 	
   
    var skey;
  
    skey = (document.all) ? e.keyCode : e.which;
   	for(i = 0 ; i <arrSKey.length ; i++)
	{
   		if (skey == arrSKey[i])
		{
   			return false;
   		}
   	}
}

function validarInputFecha(obj, value)
{
	if (value!="") 
	{
		if (!checkdate(value))
	  	{
	   		alert("Formato de fecha incorrecto");
	   		if (obj!=null)
			{
				obj.value='';
	    		obj.focus();
	    		return false;
	   		}
	  	}
	 }
	 return true;
}

function checkdate(fecha)
{
	var err=0
	if ( fecha.length != 10) err=1
	dia = fecha.substring(0,2)
	slash1 = fecha.substring(2,3)
	mes = fecha.substring(3,5)
	slash2 = fecha.substring(5,6)
	anho = fecha.substring(6,10)
	if ( dia<1 || dia>31) err = 1
	if ( slash1 != '/' ) err = 1
	if ( mes<1 || mes>12) err = 1
	if ( slash1 == '/' && slash2 != '/' ) err = 1
	if ( anho < 0 || anho > 2200 ) err = 1
	if ( mes == 4 || mes == 6 || mes == 9 || mes == 11 )
	{
		if (dia==31) err=1
	}
	if (mes == 2)
	{
		var g = parseInt(anho/4)
	    if (isNaN(g))
		{
	      err = 1
	    }
	    if (dia >29) err =1
	    if (dia ==29 && ((anho/4)!=parseInt(anho/4))) err=1
	}
	return (!(err==1));
}

function emailCheck (emailStr) {
/* The following pattern is used to check if the entered e-mail address
   fits the user@domain format.  It also is used to separate the username
   from the domain. */
var emailPat=/^(.+)@(.+)$/
/* The following string represents the pattern for matching all special
   characters.  We don't want to allow special characters in the address. 
   These characters include ( ) < > @ , ; : \ " . [ ]    */
var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
/* The following string represents the range of characters allowed in a 
   username or domainname.  It really states which chars aren't allowed. */
var validChars="\[^\\s" + specialChars + "\]"
/* The following pattern applies if the "user" is a quoted string (in
   which case, there are no rules about which characters are allowed
   and which aren't; anything goes).*/
var quotedUser="(\"[^\"]*\")"
/* The following pattern applies for domains that are IP addresses,
   rather than symbolic names. */
var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
/* The following string represents an atom (basically a series of
   non-special characters.) */
var atom=validChars + '+'
/* The following string represents one word in the typical username.*/
var word="(" + atom + "|" + quotedUser + ")"
// The following pattern describes the structure of the user
var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
/* The following pattern describes the structure of a normal symbolic
   domain, as opposed to ipDomainPat, shown above. */
var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")

var matchArray=emailStr.match(emailPat)
if (matchArray==null) {
	//alert("Email address seems incorrect (check @ and .'s)")
	return false
}
var user=matchArray[1]
var domain=matchArray[2]

// See if "user" is valid 
if (user.match(userPat)==null) {
    // user is not valid
    //alert("The username doesn't seem to be valid.")
    return false
}

var IPArray=domain.match(ipDomainPat)
if (IPArray!=null) {
    // this is an IP address
	  for (var i=1;i<=4;i++) {
	    if (IPArray[i]>255) {
	        alert("Destination IP address is invalid!")
		return false
	    }
    }
    return true
}

// Domain is symbolic name
var domainArray=domain.match(domainPat)
if (domainArray==null) {
	//alert("The domain name doesn't seem to be valid.")
    return false
}

/* Now we need to break up the domain to get a count of how many atoms
   it consists of. */
var atomPat=new RegExp(atom,"g")
var domArr=domain.match(atomPat)
var len=domArr.length
if (domArr[domArr.length-1].length<2 || 
    domArr[domArr.length-1].length>3) {
   //alert("The address must end in a three-letter domain, or two letter country.")
   return false
}

// Make sure there's a host name preceding the domain.
if (len<2) {
   var errStr="This address is missing a hostname!"
   //alert(errStr)
   return false
}

// If we've gotten this far, everything's valid!
return true;
}

/*function checkNuma(numero)
{
	if (numero.length > 0)
	{
		cadena="0123456789";
		if(!es_alfanumerico(numero,cadena)) 
		{
			alert("El valor ingresado debe ser numérico");
			return false;
		}
		return true;
	}
}*/

function es_alfanumerico(str,Validos)
{
	strInputString=str.toString();
	strValidos=Validos.toString();
	fin_i = strInputString.length+10;
	fin_z = strValidos.length + 10;
	encontro=false;
	for(i=0;i<strInputString.length;i++)
	{
		for(z=0;z<strValidos.length;z++)
		{
			if(strInputString.substr(i,1) == strValidos.substr(z,1))
			{
				z=fin_z;
			}
		}
		if(z==fin_z+1)
			encontro = true;
		else
		{
			i=fin_i;
			encontro = false;
		}
	}
	return encontro;
}

function validar() 
{
  var i =0;
	
  if (document.form1.txtNombre.value == "" && i==0)
  { 
	alert("Por favor ingresa tu nombre.");
    window.document.form1.txtNombre.focus();
    window.document.form1.txtNombre.select();    
	i=i+1;
  }

  if (document.form1.txtNombre.value != "" && i==0)
  {
	if (document.form1.txtNombre.length > 0 && i==0)
	{
		cadena="áéíóúabcdefghijklmnñopqrstuvwxyzABCDEFGHIJKLMNÑOPQRSTUVWXYZ ";
		if(!es_alfanumerico(document.form1.txtNombre.value,cadena) && i==0) 
		{
			alert("El nombre ingresado no es correcto");
			window.document.form1.txtNombre.value="";
			window.document.form1.txtNombre.focus();
			window.document.form1.txtNombre.select();	
			i=i+1;
		}
	}
  }
  
  if (document.form1.txtApPaterno.value == "" && i==0)
  { 
	alert("Por favor ingresa tu apellido paterno.");
    window.document.form1.txtApPaterno.focus();
    window.document.form1.txtApPaterno.select();    
	i=i+1;
  }

  if (document.form1.txtApPaterno.value != "")
  {
	if (document.form1.txtApPaterno.length > 0)
	{
		cadena=" áéíóúabcdefghijklmnñopqrstuvwxyzABCDEFGHIJKLMNÑOPQRSTUVWXYZ- ";
		if(!es_alfanumerico(document.form1.txtApPaterno.value,cadena) && i==0) 
		{
			alert("El apellido ingresado no es correcto");
			window.document.form1.txtApPaterno.value="";
			window.document.form1.txtApPaterno.focus();
			window.document.form1.txtApPaterno.select();    
			i=i+1;
		}
	}
  }

  if (document.form1.txtApMaterno.value == "" && i==0)
  { 
	alert("Por favor ingresa tu apellido materno.");
    window.document.form1.txtApMaterno.focus();
    window.document.form1.txtApMaterno.select();    
	i=i+1;
  }

  if (document.form1.txtApMaterno.value != "")
  {
	if (document.form1.txtApMaterno.length > 0)
	{
		cadena="áéíóúabcdefghijklmnñopqrstuvwxyzABCDEFGHIJKLMNÑOPQRSTUVWXYZ- ";
		if(!es_alfanumerico(document.form1.txtApMaterno.value,cadena) && i==0) 
		{
			alert("El apellido ingresado no es correcto");
			window.document.form1.txtApMaterno.value="";
			window.document.form1.txtApMaterno.focus();
			window.document.form1.txtApMaterno.select();    
			i=i+1;
		}
	}
  }
  
  if (document.form1.txtNumDoc.value == "" & i==0)
  { 
	alert("Por favor ingresa tu número de documento de identidad.");
    window.document.form1.txtNumDoc.focus();
    window.document.form1.txtNumDoc.select();    
	i=i+1;
  }

  if (!checkDOC(document.form1.txtNumDoc.value,document.form1.CboIdDoc.value) && i==0)
  {
	i=i+1;  
//	return false;
  }  

  if (document.form1.txtDiaNac.value != "" || document.form1.txtMesNac.value != "" || document.form1.txtAnoNac.value != "")
  {
     cadena="01234567890"
     if (((!es_alfanumerico(document.form1.txtDiaNac.value,cadena)) || (document.form1.txtDiaNac.value<1) || (document.form1.txtDiaNac.value >31)) && (i==0))
     {
		alert("El día ingresado no es válido");
		window.document.form1.txtDiaNac.value="";
		window.document.form1.txtDiaNac.focus();
		window.document.form1.txtDiaNac.select();
		i=i+1;
     }
	 
     if (((!es_alfanumerico(document.form1.txtMesNac.value,cadena)) || (document.form1.txtMesNac.value<1) || (document.form1.txtMesNac.value >12)) && (i==0))
     {
		alert("El mes ingresado no es válido");
		window.document.form1.txtMesNac.value="";
		window.document.form1.txtMesNac.focus();
		window.document.form1.txtMesNac.select();
		i=i+1;
     }
	 
     if (((!es_alfanumerico(document.form1.txtAnoNac.value,cadena)) || (document.form1.txtAnoNac.value<1900) || (document.form1.txtAnoNac.value >2000)) && (i==0))
     {
		alert("El año ingresado no es válido");
		window.document.form1.txtAnoNac.value="";
		window.document.form1.txtAnoNac.focus();
		window.document.form1.txtAnoNac.select();
		i=i+1;
     }     
  }

  if (document.form1.cboUbicacion.value != "00")
  {
	  if (document.form1.cboUbicacion.value == "01")
	  {
		if (document.form1.txtTelOficina.value == "" && i==0)
		{
			alert("Debes ingresar el teléfono de tu oficina");
			window.document.form1.txtTelOficina.value="";
			window.document.form1.txtTelOficina.focus();
			window.document.form1.txtTelOficina.select();
			i=i+1;
		}
	  }
	  if (document.form1.cboUbicacion.value == "02")
	  {
		if (document.form1.txtTelcasa.value == "" && i==0)
		{
			alert("Debes ingresar el teléfono de tu casa");
			window.document.form1.txtTelcasa.value="";
			window.document.form1.txtTelcasa.focus();
			window.document.form1.txtTelcasa.select();    
			i=i+1;
		}
	  }
	  if (document.form1.cboUbicacion.value == "03")
	  {
		 if (document.form1.txtCelular.value == "" && i==0)
		 {
			alert("Debes ingresar el teléfono de tu celular");
			window.document.form1.txtCelular.value="";
			window.document.form1.txtCelular.focus();
			window.document.form1.txtCelular.select();    
			i=i+1;
		 }
	  }
  }

  if (document.form1.txtTelcasa.value != "")
  {
	if (!checkNuma(document.form1.txtTelcasa.value,"txtTelcasa") && i==0)
	{
		alert("El teléfono de tu casa debe ser numérico");
		window.document.form1.txtTelcasa.value="";
		window.document.form1.txtTelcasa.focus();
		window.document.form1.txtTelcasa.select();    
		i=i+1;
	}
  }

  if (document.form1.txtTelOficina.value != "")
  {
	if (!checkNuma(document.form1.txtTelOficina.value,"txtTelOficina") && i==0)
	{
		alert("El teléfono de tu oficina debe ser numérico");
		window.document.form1.txtTelOficina.value="";
		window.document.form1.txtTelOficina.focus();
		window.document.form1.txtTelOficina.select();
		i=i+1;
	}
  }

  if (document.form1.txtCelular.value != "")
  {
	if (!checkNuma(document.form1.txtCelular.value,"txtCelular") && i==0)
	{
		alert("El teléfono de tu celular debe ser numérico");
		window.document.form1.txtCelular.value="";
		window.document.form1.txtCelular.focus();
		window.document.form1.txtCelular.select();
		i=i+1;
	}
  }
  
  if (document.form1.txtEmailPrincipal.value != "")
  { 
	if(!emailCheck(document.form1.txtEmailPrincipal.value) && i==0) 
	{
		alert("El e-mail ingresado no es correcto");
		window.document.form1.txtEmailPrincipal.value="";
		window.document.form1.txtEmailPrincipal.focus();
		window.document.form1.txtEmailPrincipal.select();    
		i=i+1;
	}
  }
  

  if (document.form1.txtMensaje.value == "" && i==0)
  { 
	alert("Por favor ingresa tu mensaje.");
    window.document.form1.txtMensaje.focus();
    window.document.form1.txtMensaje.select();    
	i=i+1;
  }
  
  var palabrasNoPermitidas = ["html","table","tr","td","script","javascript","div","span","document","alert","frame"];
  var mensaje = document.form1.txtMensaje.value;
  var listaMensaje = mensaje.split(" ");
  var count = 0
  for(var j=0;j<listaMensaje.length;j++)
  {
	for(var k=0;k<palabrasNoPermitidas.length;k++)
	{
	  if(listaMensaje[j].indexOf(palabrasNoPermitidas[k])!=-1)
	  {
		  count++;
		  break;
	  }
	}
  }
  alert(count);
  if(count != 0)
  {
	alert("El mensaje contiene palabras no válidas");  
	window.document.form1.txtMensaje.focus();
    window.document.form1.txtMensaje.select();    
	i=i+1;
  }
 
  if(i==0) 
  {
	  document.form1.target="_self";
	  document.form1.submit();  
  }
  
}
 
function checkDOC(id,seleccion)
{
	if (seleccion == "DNI" || seleccion == "LE")
	{
		if (caracter(id))
 		{
 			alert("El documento debe ser numérico");
			window.document.form1.txtNumDoc.value="";
            window.document.form1.txtNumDoc.focus();
			window.document.form1.txtNumDoc.select();
			return false;
		}
	}
	else
	{
		validos="áéíóúabcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNÑOPQRSTUVWXYZ";
		if(!es_alfanumerico(id,validos)) 
		{
			alert("El valor ingresado debe ser alfanumérico");
			window.document.form1.txtNumDoc.value="";
            window.document.form1.txtNumDoc.focus();
            window.document.form1.txtNumDoc.select();
			return false;
		}
	}
	return true;
}

function checkNuma(numero,nombre)
{
	if (numero.length > 0)
	{
		cadena="0123456789";
		if(!es_alfanumerico(numero,cadena)) 
		{
			if (nombre == "txtTelcasa")
			{
				window.document.form1.txtTelcasa.value="";
				window.document.form1.txtTelcasa.focus();
				window.document.form1.txtTelcasa.select();    	
			}
			if (nombre == "txtTelOficina")
			{
				window.document.form1.txtTelOficina.value="";
				window.document.form1.txtTelOficina.focus();
				window.document.form1.txtTelOficina.select();    	
			}
			
			if (nombre == "txtNumDoc")
			{
				window.document.form1.txtNumDoc.value="";
				window.document.form1.txtNumDoc.focus();
				window.document.form1.txtNumDoc.select();    	
			}
			return false;
		}
	}
	return true;
}

function caracter(num)
{
   largo = num.length;
   for (i=0; i < largo ; i++ )
  { 
    if ( num.charAt(i) !="0" && num.charAt(i) != "1" && num.charAt(i) !="2" && num.charAt(i) != "3" && num.charAt(i) != "4" && num.charAt(i) !="5" && num.charAt(i) != "6" && num.charAt(i) != "7" && num.charAt(i) !="8" && num.charAt(i) != "9" ) 
    {
	  return true;
    }
   }
	 return false;
} 

function enblanco()
{
	window.document.form1.txtNumDoc.value = "";
}
