// JavaScript Document
/* validacion de numeros */
function isNatural(str) {
	var i;
	var c;
	var tmp = new String;
	if (str == null || str.length == null)
		return false;
	tmp = Trim(str);
	if (tmp.length == 0)
		return false;
	for (i=0;i<tmp.length;i++){
		c = tmp.charAt(i);
		if ( c < '0' || c > '9' )
			return false;
	}
	return true;		
}
function isReal(str) {
	var c;
	var tmp;
	var p;
	if ( str == null || str.length == null)
		return false;
	tmp = Trim(str);
	if (tmp.length == 0)
		return false;
	c = tmp.charAt(0);
	if ( c == '-' || c == '+' )
		tmp = tmp.substring(1,tmp.length);
	p = tmp.indexOf('.');
	if ( p == -1 )
		return isNatural(tmp);
	return isNatural(tmp.substring(0,p)) 
		&& isNatural(tmp.substring(p+1, tmp.length));
}

function LTrim(str) {
	var i;
	var inicio; //posicion desde donde no hay espacios
	if (str == null)
		return "";
	largo = str.length;
	inicio = 0;
	for (i=0; i<largo; i++) {
		c = str.charAt(i);
		if (c == ' ')
			inicio = i + 1;
		else
			break;
	}
	return str.substring(inicio,largo);
}

function RTrim(str) {
	var i;
	var fin; //posicion hasta donde no hay espacios
	if (str == null)
		return "";
	fin = str.length;
	for (i=fin-1; i>=0; i--) {
		c = str.charAt(i);
		if (c == ' ')
			fin = i;
		else
			break;
	}
	return str.substring(0,fin);
}

function Trim(str) {
	return(RTrim(LTrim(str)));
}



/* validacion de letras */
function valida(e) { // 1
    tecla = (document.all) ? e.keyCode : e.which; // 2
    if (tecla==8) return true; // 3
    patron =/[A-Za-z\s]/; // 4
    te = String.fromCharCode(tecla); // 5
    return patron.test(te); // 6
} 





/* Funcion que retorna la hora */
function RetornaFecha()
{
 var fecha;
 fecha=new Date();
 dias=new Array("Domingo","Lunes","Martes","Miercoles","Jueves","Viernes","Sabado");
 meses=new Array("Enero","Febrero","Marzo","Abril","Mayo","Junio","Julio","Agosto","Septiembre","Octubre","Noviembre","Diciembre");
 var cadena=fecha.getDate() + " de " + meses[fecha.getMonth()] +" de "+ fecha.getFullYear();
 return cadena;
 }
function mueveReloj()
{ 
 momentoActual = new Date() 
 hora = momentoActual.getHours() 
 minuto = momentoActual.getMinutes() 
 segundo = momentoActual.getSeconds() 

 str_segundo = new String (segundo) 
 if (str_segundo.length == 1) 
  segundo = "0" + segundo 

 str_minuto = new String (minuto) 
 if (str_minuto.length == 1) 
     minuto = "0" + minuto 

 str_hora = new String (hora) 
  if (str_hora.length == 1) 
     hora = "0" + hora 

 horaImprimible = hora + " : " + minuto + " : " + segundo 

 document.form_reloj.reloj.value = horaImprimible 

 setTimeout("mueveReloj()",1000) 
} 




/* Validacion Contacto*/
function Validar()
{
if(FrmMen.txtNombre.value=="")
  {
   alert("ingrese Nombre");
   FrmMen.txtNombre.focus();
   return;
  }
if(FrmMen.txtNombre.value.length <4)
   {
	alert("Escriba por lo menos 4 caracteres en el campo \"Nombre\".");
	FrmMen.txtNombre.focus();
	return;
  }  
if(FrmMen.txtApPaterno.value=="")
  {
   alert("ingrese Apellido Paterno");
   FrmMen.txtApPaterno.focus();
   return;
  }
 if(FrmMen.txtApPaterno.value.length <4)
   {
	alert("Escriba por lo menos 4 caracteres en el campo \"Nombre\".");
	FrmMen.txtApPaterno.focus();
	return;
  }   
if(FrmMen.txtApMaterno.value=="")
  {
   alert("ingrese Apellido Materno");
   FrmMen.txtApMaterno.focus();
   return;
  }
 if(FrmMen.txtApMaterno.value.length <4)
   {
	alert("Escriba por lo menos 4 caracteres en el campo \"Nombre\".");
	FrmMen.txtApMaterno.focus();
	return;
  }     
 if(FrmMen.txtMail.value=="")
  {
   alert("ingrese E-Mail");
   FrmMen.txtMail.focus();
   return;
  }  
txt=FrmMen.txtMail.value;
if (txt.indexOf("@")<3)
{
alert("Lo siento, la cuenta de correo parece errónea. Por favor, "
+" comprueba el prefijo y el signo '@'.");
FrmMen.txtMail.focus();
return;
}
if ((txt.indexOf(".com")<5)&&(txt.indexOf(".org")<5)
&&(txt.indexOf(".gov")<5)&&(txt.indexOf(".net")<5)
&&(txt.indexOf(".cl")<5)&&(txt.indexOf(".edu")<5))
{
alert("Lo siento. Pero esa cuenta de correo parece errónea. Por favor,"
+" comprueba el sufijo (que debe incluir alguna terminación como: "
+".com, .edu, .net, .org, .gov , .cl)");
FrmMen.txtMail.focus();
return;
}

   
if(FrmMen.txtMensaje.value=="")
  {
   alert("ingrese un Mensaje");
   FrmMen.txtMensaje.focus();
   return;
  } 

var boton=0; 
for(i=0; ele=FrmMen.elements[i]; i++)
{ 
  if (ele.type=='radio') 
   if (ele.checked) 
   {
    boton=1;
	break;
	}
 } 
if (boton==1)
{
FrmMen.submit();
}else{ 
alert('Debe seleccionar un Tipo de Consulta');
return;
} 

}

/*   Validacion Registro Nacional Personas Naturales  */    

function ValidarIngreso()
{
var valor=0;
//validad nombre usuario
if(document.frmIng.txtNomUser.value=="")
  {
   alert("ingrese Nombre de Usuario");
   document.frmIng.txtNomUser.focus();
    return;
  }
if(document.frmIng.txtNomUser.value.length <4)
   {
	alert("Escriba por lo menos 4 caracteres en el campo \"Nombre de Usuario\".");
	document.frmIng.txtNomUser.focus();
	return;
  }  
if(document.frmIng.txtNomUser.value.length >8)
   {
	alert("Escriba un maximo de 8 caracteres en el campo \"Nombre de Usuario\".");
	document.frmIng.txtNomUser.focus();
	return;
  }    
// valida password campos vacios 
if(document.frmIng.Pass.value=="")
  {
   alert("ingrese Password");
   document.frmIng.Pass.focus();
   return;
   }
if(document.frmIng.Pass.value.length <4)
   {
	alert("Escriba por lo menos 4 caracteres en el campo \"Password\".");
	document.frmIng.Pass.focus();
	return;
  }  
 if(document.frmIng.rePass.value=="")
  {
   alert("ingrese Password Nuevamente");
   document.frmIng.rePass.focus();
   return;
  }
if(document.frmIng.rePass.value.length <4)
   {
	alert("Escriba por lo menos 4 caracteres en el campo \"Password\".");
	document.frmIng.rePass.focus();
	return;
  }  
// Valida password iguales  
if(document.frmIng.rePass.value!=frmIng.Pass.value)
{
 alert("Repita la password correctamente \"Password\".");
	frmIng.rePass.focus();
	return;
} 

// Valida correo
 if(frmIng.txtCorreo.value=="")
  {
   alert("ingrese E-Mail");
   frmIng.txtCorreo.focus();
   return;
  }  
txt=frmIng.txtCorreo.value;
if (txt.indexOf("@")<3)
{
alert("Lo siento, la cuenta de correo parece errónea. Por favor, "
+" comprueba el prefijo y el signo '@'.");
frmIng.txtCorreo.focus();
return;
}
if ((txt.indexOf(".com")<5)&&(txt.indexOf(".org")<5)
&&(txt.indexOf(".gov")<5)&&(txt.indexOf(".net")<5)
&&(txt.indexOf(".cl")<5)&&(txt.indexOf(".edu")<5))
{
alert("Lo siento. Pero esa cuenta de correo parece errónea. Por favor,"
+" comprueba el sufijo (que debe incluir alguna terminación como: "
+".com, .edu, .net, .org, .gov , .cl)");
frmIng.txtCorreo.focus();
return;
}

//Valida Nombre    
if(frmIng.txtNom.value=="")
  {
   alert("ingrese Nombre");
   frmIng.txtNom.focus();
   return;
  }
if(frmIng.txtNom.value.length <3)
   {
	alert("Escriba por lo menos 3 caracteres en el campo \"Nombre\".");
	frmIng.txtNom.focus();
	return;
  }  
if(frmIng.txtApPater.value=="")
  {
   alert("ingrese Apellido Paterno");
   frmIng.txtApPater.focus();
    return;
  }
 if(frmIng.txtApPater.value.length <4)
   {
	alert("Escriba por lo menos 4 caracteres en el campo \"Apellido Paterno\".");
	frmIng.txtApPater.focus();
	return;
  }   
if(frmIng.txtApMater.value=="")
  {
   alert("ingrese Apellido Materno");
   frmIng.txtApMater.focus();
   return;
  }
 if(frmIng.txtApMater.value.length <4)
   {
	alert("Escriba por lo menos 4 caracteres en el campo \"Apellido Materno\".");
	frmIng.txtApMater.focus();
	return;
  }  
// validacion rut //    
if(frmIng.txtRut.value=="")
  {
   alert("ingrese su RUT");
   frmIng.txtRut.focus();
   return;
  }   
  if(frmIng.txtDig.value=="")
  {
   alert("ingrese su Digito ");
   frmIng.txtDig.focus();
   return;
  }   
var tmpstr = "";
var intlargo =frmIng.txtRut.value+'-'+frmIng.txtDig.value
if (intlargo.length> 0)
	{
		crut = frmIng.txtRut.value+'-'+frmIng.txtDig.value
		largo = crut.length;
		if ( largo <2 )
		{
			alert('rut inválido')
			frmIng.txtRut.focus();
			return false;
		}
		for ( i=0; i <crut.length ; i++ )
		if ( crut.charAt(i) != ' ' && crut.charAt(i) != '.' && crut.charAt(i) != '-' )
		{
			tmpstr = tmpstr + crut.charAt(i);
		}
		rut = tmpstr;
		crut=tmpstr;
		largo = crut.length;
	
		if ( largo> 2 )
			rut = crut.substring(0, largo - 1);
		else
			rut = crut.charAt(0);
	
		dv = crut.charAt(largo-1);
	
		if ( rut == null || dv == null )
		return 0;
	
		var dvr = '0';
		suma = 0;
		mul  = 2;
	
		for (i= rut.length-1 ; i>= 0; i--)
		{
			suma = suma + rut.charAt(i) * mul;
			if (mul == 7)
				mul = 2;
			else
				mul++;
		}
	
		res = suma % 11;
		if (res==1)
			dvr = 'k';
		else if (res==0)
			dvr = '0';
		else
		{
			dvi = 11-res;
			dvr = dvi + "";
		}
	
		if ( dvr != dv.toLowerCase() )
		{
			alert('El Rut Ingreso es Invalido')
			frmIng.txtRut.focus();
			return;
		}
		
	}
//Domicilio
if(frmIng.txtDomi.value=="")
  {
   alert("ingrese su Domicilio");
   frmIng.txtDomi.focus();
   return;
  }  
//Numero de domicilio
if(frmIng.txtDomiNum.value=="")
 {
  alert('Debe ingresar su Numero');
  frmIng.txtDomiNum.focus();
  return;
 }
  
//Ciudad
  if(frmIng.txtCiu.value=="")
  {
   alert("ingrese su Ciudad");
   frmIng.txtCiu.focus();
   return;
  }  
//Comuna
if(frmIng.txtComu.value=="")
  {
   alert("ingrese su Comuna");
   frmIng.txtComu.focus();
   return;
  } 
if(!isNatural(frmIng.txtTelefPre.value))
 {
  alert('Debe ingresar solo Numero');
  return;
 }  
if(!isNatural(frmIng.txtTelef.value))
 {
  alert('Debe ingresar solo Numero');
  return;
 }   
 frmIng.action="registro_usu.php";
 frmIng.submit();     
}
