function validarut(rut)
{
  if (rut.length<9)
     return(false)
  i1=rut.indexOf("-");
  dv=rut.substr(i1+1);
  dv=dv.toUpperCase();
  nu=rut.substr(0,i1);

  cnt=0;
  suma=0;
  for (i=nu.length-1; i>=0; i--)
  {
    dig=nu.substr(i,1);
    fc=cnt+2;
    suma += parseInt(dig)*fc;
    cnt=(cnt+1) % 6;
   }
  dvok=11-(suma%11);
  if (dvok==11) dvokstr="0";
  if (dvok==10) dvokstr="K";
  if ((dvok!=11) && (dvok!=10)) dvokstr=""+dvok;

  if (dvokstr==dv)
     return(true);
  else
     return(false);
}

function isValidEmail(texto) 
{
	var addressIsValid = false;
	var invalidPatterns = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/;
	var validPatterns = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/;
	if (window.RegExp) {
		if (!invalidPatterns.test(texto) && validPatterns.test(texto)) {
			addressIsValid = true;
		}
		else {
			addressIsValid = false;
		}
	}
	else {
		if(texto.indexOf("@") >= 0)
		addressIsValid = true;
	}
	return addressIsValid;
}

function isNumber(texto)
{
	largo = texto.length;
	zero = texto.value;
	if (largo == 0)
		return false;
	if(zero==0)
	 return false;
	for (i=0; i < largo ; i++ )
	if (!isDigit(texto.charAt(i)))
		return false;
	return true
}

function isDigit(caracter)
{
	if ((caracter.charAt(0) >= "0") && (caracter.charAt(0) <= "9"))
		return  true
	return false
}

function validar() {
	errores=0;
	msg_error="Por favor, ingrese correctamente:\n";
	
	var rut      = document.fm.rut.value +'-'+ document.fm.dv.value;
	var email    = document.fm.email.value;
	var cod_area = document.fm.cod_area.value;
	var telefono = document.fm.telefono.value;
//	alert(document.fm.c_hipotecario.checked);
	var tipo_cred_0 = document.fm.c_hipotecario[0].checked;
	var tipo_cred_1 = document.fm.c_hipotecario[1].checked;
	var tipo_cred_2 = document.fm.c_hipotecario[2].checked;
	var tipo_cred_3 = document.fm.c_hipotecario[3].checked;
	//var tipo_cred_4 = document.fm.c_hipotecario[4].checked;
//	var tipo_cred_5 = document.fm.c_hipotecario[5].checked;
//	var tipo_cred_6 = document.fm.c_hipotecario[6].checked;	
	
	//alert(tipo_cred_6+"<-");
	
	
	sw = validarut(rut);

	if (rut=="") {
		errores++;
		msg_error=msg_error + " - Rut \n";
	}

	if(email=="")
	{
	  errores++;
	  msg_error=msg_error + " - E-mail \n";
	}
	
	if (!isValidEmail(email) && email!="") {
		errores++;
	  msg_error=msg_error + " - '"+ email +"' no es un e-mail válido! \n";
  }
	
	if(cod_area=="")
	{
	  errores++;
	  msg_error=msg_error + " - Código de Area \n";
	}
	
	if (!isNumber(cod_area) && cod_area!="") {
		errores++;
		msg_error=msg_error + " - El código de área ingresado no es válido, ingrese solo números. \n";
  }
	
	if(telefono=="")
	{
	  errores++;
	  msg_error=msg_error + " - Teléfono \n";
	}
	
	if (!isNumber(telefono) && telefono!="") {
		errores++;
		msg_error=msg_error + " - El número ingresado no es válido, ingrese solo números. \n";
  }

	if ((rut!="")&&(validarut(rut)==false)) {
		errores++;
		msg_error=msg_error + " - Rut no es válido\n";
	}
	if(!(tipo_cred_0) && !(tipo_cred_1) && !(tipo_cred_2) && !(tipo_cred_3))
	//if (!tipo_cred_0)
	{
		errores++;
		msg_error=msg_error + " - Tipo de crédito\n";
	}

	if (errores==0) {
		/*if ((tipo_cred_1==true) || (tipo_cred_2==true) || (tipo_cred_3==true)){*/
        //if (tipo_cred_2==true){

		//	window.open("popup/index.html" , "Seguro" , "width=500,height=290,scrollbars=YES") 
		//}
			
		document.fm.submit();
	} else {
		alert(msg_error);
		return;
	}
}

function validaSolicitud()
{
  errores=0;
	msg_error="Por favor, ingrese correctamente:\n";
	
	var rut        = document.fm.rut.value +'-'+ document.fm.dv.value;
	var nombre     = document.fm.nombre.value;
	var a_paterno  = document.fm.a_paterno.value;
	var a_materno  = document.fm.a_materno.value;
	var email      = document.fm.email.value;
	var cod_area   = document.fm.cod_area.value;
	var telefono   = document.fm.telefono.value;
	var t_credito  = document.fm.t_credito.value;
	var monto      = document.fm.monto.value;
	var plazo      = document.getElementById('plazo').value;
	var fecha_nac	   = document.getElementById('fecha_nac').value;
	var comuna      = document.getElementById('comuna').value;
	var region      = document.getElementById('region').value;
	var nivel_estudio      = document.getElementById('nivel_estudio').value;
	var profesion      = document.getElementById('profesion').value;
	var actividad      = document.getElementById('actividad').value;
	var anti_laboral      = document.getElementById('anti_laboral').value;
	var empleador      = document.getElementById('empleador').value;
	var cargo_trabajador      = document.getElementById('cargo_trabajador').value;
	var renta_liquida      = document.getElementById('renta_liquida').value;
	var estado_civil      = document.getElementById('estado_civil').value;
	var rut_conyugue      = document.getElementById('rut_conyugue').value +'-'+ document.getElementById('dv_conyugue').value;
	var renta_conyugue      = document.getElementById('renta_conyugue').value;

	
	sw = validarut(rut);
	if (rut=="") { errores++; msg_error=msg_error + " - Rut \n"; }

	if (!validarut(rut)){	errores++;  msg_error=msg_error + " - '"+ rut +"' no es un RUT válido! \n"; }
	
	if(nombre==""){ errores++;  msg_error=msg_error + " - Nombre \n";}
	
	if(a_paterno==""){ errores++; msg_error=msg_error + " - Apellido Paterno \n";}
	
	if(a_materno==""){ errores++; msg_error=msg_error + " - Apellido Materno \n";}

	if(email==""){ errores++; msg_error=msg_error + " - E-mail \n";	}

	if (!isValidEmail(email) && email!="") { errores++;  msg_error=msg_error + " - '"+ email +"' no es un e-mail válido! \n"; }

	if (fecha_nac=="") { errores++; msg_error=msg_error + " - Fecha Nacimiento \n";	}
	
	if(cod_area==""){  errores++;  msg_error=msg_error + " - Código de Area \n";}
	
	if (!isNumber(cod_area) && cod_area!="") {	errores++;	msg_error=msg_error + " - El código de área ingresado no es válido, ingrese solo números. \n";  }
	
	if(telefono==""){  errores++;  msg_error=msg_error + " - Teléfono \n";	}

	if(region==""){  errores++;  msg_error=msg_error + " - Región \n";	}

	if(comuna==""){  errores++;  msg_error=msg_error + " - Comuna \n";	}
//----------
	if(nivel_estudio==""){  errores++;  msg_error=msg_error + " - Nivel Estudio \n";	}

	if(profesion==""){  errores++;  msg_error=msg_error + " - Profesión \n";	}

	if(actividad==""){  errores++;  msg_error=msg_error + " - Actividad \n";	}

	if(anti_laboral==""){  errores++;  msg_error=msg_error + " - Antiguedad Laboral \n";	}

	if(empleador==""){  errores++;  msg_error=msg_error + " - Empleador \n";	}

	if(cargo_trabajador==""){  errores++;  msg_error=msg_error + " - Cargo Actual \n";	}

	if(renta_liquida==""){  errores++;  msg_error=msg_error + " - Renta Líquida \n";	}

	if(estado_civil==""){  errores++;  msg_error=msg_error + " - Estado Civil \n";	}

	if (estado_civil=="casado") {
		if (!validarut(rut_conyugue)){	errores++;  msg_error=msg_error + " - '"+ rut_conyugue +"' no es un RUT Conyuge válido! \n"; }

		if(renta_conyugue==""){  errores++;  msg_error=msg_error + " - Renta Líquida Conyugue \n";	}
	}
//-------
	if (!isNumber(telefono) && telefono!="") {	errores++;	msg_error=msg_error + " - El número ingresado no es válido, ingrese solo números. \n";  }
	
	if(monto==""){  errores++; msg_error=msg_error + " - Monto \n";	}
	
	if(plazo==""){  errores++;  msg_error=msg_error + " - Plazo \n";}
	
	if (errores==0) {
		document.fm.submit();
	} else {
		alert(msg_error);
		return;
	}
}

function validaContacto()
{
  errores=0;
	msg_error="Por favor, ingrese correctamente:\n";
	
	var rut        = document.fm.rut.value +'-'+ document.fm.dv.value;
	var nombre     = document.fm.nombre.value;
	var a_paterno  = document.fm.a_paterno.value;
	var a_materno  = document.fm.a_materno.value;
	var email      = document.fm.email.value;
	var c_email    = document.fm.c_email.value;
	var cod_area   = document.fm.cod_area.value;
	var telefono   = document.fm.telefono.value;
	var mensaje    = document.fm.mensaje.value;
	
	
	sw = validarut(rut);

	if (rut=="") {
		errores++;
		msg_error=msg_error + " - Rut \n";
	}
	
	if(nombre=="")
	{
	  errores++;
	  msg_error=msg_error + " - Nombre \n";
	}
	
	if(a_paterno=="")
	{
	  errores++;
	  msg_error=msg_error + " - Apellido Paterno \n";
	}
	
	if(a_materno=="")
	{
	  errores++;
	  msg_error=msg_error + " - Apellido Materno \n";
	}

	if(email=="")
	{
	  errores++;
	  msg_error=msg_error + " - E-mail \n";
	}
	
	if (!isValidEmail(email) && email!="") {
		errores++;
	  msg_error=msg_error + " - '"+ email +"' no es un e-mail válido! \n";
  }
	
	if(cod_area=="")
	{
	  errores++;
	  msg_error=msg_error + " - Código de Area \n";
	}
	
	if (!isNumber(cod_area) && cod_area!="") {
		errores++;
		msg_error=msg_error + " - El código de área ingresado no es válido, ingrese solo números. \n";
  }
	
	if(telefono=="")
	{
	  errores++;
	  msg_error=msg_error + " - Teléfono \n";
	}
	
	if (!isNumber(telefono) && telefono!="") {
		errores++;
		msg_error=msg_error + " - El número ingresado no es válido, ingrese solo números. \n";
  }
	
	if(email!=c_email)
	{
	  errores++;
	  msg_error=msg_error + " - Comprobación de e-mail \n";
	}
	
	if(mensaje=="")
	{
	  errores++;
	  msg_error=msg_error + " - Debe ingresar un mensaje \n";
	}
	
	if (errores==0) {
		document.fm.submit();
	} else {
		alert(msg_error);
		return;
	}
}
