var largeField = 4
var formulario = ""

var char_a = String.fromCharCode(225);
var char_e = String.fromCharCode(233);
var char_i = String.fromCharCode(237);
var char_o = String.fromCharCode(243);
var char_u = String.fromCharCode(250);
var char_n = String.fromCharCode(241);
var char_A = String.fromCharCode(193);
var char_E = String.fromCharCode(201);
var char_I = String.fromCharCode(205);
var char_O = String.fromCharCode(211);
var char_U = String.fromCharCode(218);
var char_N = String.fromCharCode(209);
var ipregunta = String.fromCharCode(191);

function validar()
	{
		rut=document.trabaje.Rut.value
		if (rut=="" )
		{
			alert("Debe Ingresar Rut.")
			document.trabaje.Rut.focus()
			return false;
		}	
		nombre=document.trabaje.Nombre.value
		if (nombre=="" )
		{
			alert("Debe Ingresar un Nombre")
			document.trabaje.Nombre.focus()
			return false;
		}	
		Paterno=document.trabaje.Paterno.value
		if (Paterno=="" )
		{
			alert("Debe Ingresar Apellido paterno")
			document.trabaje.Paterno.focus()
			return false;
		}	
		Materno=document.trabaje.Materno.value
		if (Materno=="" )
		{
			alert("Debe Ingresar Apellido Materno")
			document.trabaje.Materno.focus()
			return false;
		}	
		vitae=document.trabaje.vitae.value
		if (vitae=="" )
		{
			alert("Debe Ingresar Su Curriculum Vitae")
			document.trabaje.vitae.focus()
			return false;
		}
		area=document.trabaje.area.value
		if (area=="0" )
		{
			alert("Debe Ingresar "+char_a+"rea en donde desea trabajar")
			document.trabaje.area.focus()
			return false;
		}
		return true;	
	}

function setAction (){
	document.solicitud.action = action
}

function isEmail (s){
    var i = 1;
    var sLength = s.length;
    while ((i < sLength) && (s.charAt(i) != "@")) {
		i++
    }
    if ((i >= sLength) || (s.charAt(i) != "@")) return false;
    else i += 2;
    while ((i < sLength) && (s.charAt(i) != ".")) {
		i++
    }
    if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
    else return true;
}

function isNumber(obj){
	if((obj.keyCode<48)||(obj.keyCode>57)){
		obj.returnValue = false
		return false
	}
} 
function validateNumber( strValue, campo ) {
	var objRegExp = new RegExp("^[0-9]+$");
	if(trim(strValue) != ""){
	    if(!objRegExp.test(trim(strValue))){
			alert("Para campo " + campo  + " debe ingresar s"+char_o+"lo n"+char_u+"meros");
			return false
		}
		else{ 
			return true
		}
	}
	else
		return true
}

function validateCharMail( strValue) {
	var objRegExp = new RegExp("^[a-zA-Z0-9@._-]+$");
	if(trim(strValue) != ""){
	    if(!objRegExp.test(trim(strValue))){
			alert("Para campo mail debe ingresar caracteres v"+char_a+"lidos");
			return false
		}
		else{
			return true
		}
	}
	else
		return true
}

function trim( strValue ) {
	var objRegExp = /^(\s*)$/;
    //check for all spaces
    if(objRegExp.test(strValue)) {
		strValue = strValue.replace(objRegExp, '');
		if( strValue.length == 0)
			return strValue;
    }
	//check for leading & trailing spaces
	objRegExp = /^(\s*)([\W\w]*)(\b\s*$)/;
	if(objRegExp.test(strValue)) {
		//remove leading and trailing whitespace characters
		strValue = strValue.replace(objRegExp, '$2');
	}
	return strValue;
}
function characterNumber(field, name){
	if(field.value.length < largeField){
		alert("Campo " + name + " debe tener como m"+char_i+"nimo " + largeField + " caracteres");
		return false
	}else
		return true
}

function emptyField(field, name){
	if(trim(field.value) == ""){
		alert("Debe ingresar su " + name)
		return false
	}else
		return true
}

function validateField(field, name){
	if(!emptyField(field, name)){
		field.focus()
		return false
	}
	if(!characterNumber(field, name)){
		field.focus()
		return false
	}
	return true
}

function validarContacto(obj){
	if(!validateField(obj.Nombre, "Nombre")){
		return false
	}
	if(!validateField(obj.Telefono, "Tel"+char_e+"fono")){
			return false
	}
	if(!validateField(obj.Mail, "e-mail")){
		return false
	}
	if(!isEmail(obj.Mail.value)){
		alert("De ingresar una direcci"+char_o+"n de e-mail correcta")
		obj.Mail.select()
		return false
	}
	if(!validateCharMail(obj.Mail.value)){
		obj.Mail.focus()
		return false;
	}
	if(!validateField(obj.Mensaje, "Mensaje")){
			return false
	}

	return true
}

function validateComun(obj){
	
	if(!validateField(obj.Nombre, "Nombre")){
		return false
	}
	if(!validateNumber(obj.Rut.value, "Rut")){
		obj.Rut.focus();
		return false
	}
	if(!validateField(obj.Telefono, "Tel"+char_e+"fono")){
			return false
	}
	if(!validateNumber(obj.Telefono.value, "Tel"+char_e+"fono")){
		obj.Telefono.focus();
		return false
	}
	if(!validateField(obj.Fax, "Fax")){
		return false
	}
	if(!validateField(obj.Mail, "e-mail")){
		return false
	}
	if(!isEmail(obj.Mail.value)){
		alert("De ingresar una direcci"+char_o+"n de e-mail correcta")
		obj.Mail.select()
		return false
	}
	if(!validateCharMail(obj.Mail.value)){
		obj.Mail.focus()
		return false;
	}


	return true
}

function validate(obj){
	formulario = obj.formulario.value
	if(formulario == "Vehiculo" || formulario == "Soap"){
		if(!validateVehiculo(obj))
			return false
		else
			return true
	}
	if(formulario == "Hogar"){
		if(!validateHogar(obj))
			return false
		else
			return true
	}
	if(formulario == "Pyme"){
		if(!validatePyme(obj))
			return false
		else
			return true
	}
}
function validateVehiculo(obj){
	
	if(!validateComun(obj))
		return false
	if(trim(obj.Marca.value) == ""){
		alert("Debe ingresar Marca del veh"+char_i+"culo");
		obj.Marca.focus();
		return false
	}
	if(trim(obj.Modelo.value) == ""){
		alert("Debe ingresar Modelo del veh"+char_i+"culo");
		obj.Modelo.focus();
		return false
	}
	if(trim(obj.Anno.value) == ""){
		alert("Debe ingresar A"+n+"o del veh"+char_i+"culo");
		obj.Anno.focus();
		return false
	}
	if(!obj.vehiculonuevo[0].checked && !obj.vehiculonuevo[1].checked){
		alert("Debe ingresar opcion si veh"+char_i+"culo es nuevo")
		return false
	}
	if(trim(obj.Patente.value) == ""){
		alert("Debe ingresar Patente del veh"+char_i+"culo");
		obj.Patente.focus();
		return false
	}
	if(formulario !=  "Soap")
		if(!obj.deducible[0].checked && !obj.deducible[1].checked && 
			!obj.deducible[2].checked && !obj.deducible[3].checked){
				alert("Debe seleccionar una opci"+char_o+"n para deducible voluntario");
				return false
		}
	return true
}

function validateDireccion(obj){
	if(trim(obj.Direccion.value) == ""){
		alert("Debe ingresar Direcci"+char_o+"n");
		obj.Direccion.focus();
		return false
	}
	if(trim(obj.Comuna.value) == ""){
		alert("Debe ingresar Comuna");
		obj.Comuna.focus();
		return false
	}
	if(trim(obj.Ciudad.value) == ""){
		alert("Debe ingresar Ciudad");
		obj.Ciudad.focus();
		return false
	}
	return true
}

function validateMateria(obj){
	if(obj.edificio.checked && trim(obj.montoEdificio.value) == ""){
		alert("Debe ingresar un monto en UF para edificio")
		obj.montoEdificio.focus()
		return false
	}
	if(obj.contenido.checked && trim(obj.montoContenido.value) == ""){
		alert("Debe ingresar un monto en UF para contenido")
		obj.montoContenido.focus()
		return false
	}
	return true
}

function validateHogar(obj){
	if(!validateComun(obj))
		return false
	if(!validateDireccion(obj))
		return false
	if(!validateMateria(obj))
		return false
	if(obj.tipoVivienda.value == 0){
		alert("Debe seleccionar un tipo de vivienda")
		obj.tipoVivienda.focus()
		return false
	}		
	if(obj.tipoUso.value == 0){
		alert("Debe seleccionar un tipo de uso")
		obj.tipoUso.focus()
		return false
	}
	if(obj.tipoConstruccion.value == 0){
		alert("Debe seleccionar un tipo de construcci"+char_o+"n")
		obj.tipoConstruccion.focus()
		return false
	}
	return true
}

function validatePyme(obj){
	if(!validateComun(obj))
		return false
	if(!validateDireccion(obj))
		return false
	if(!validateMateria(obj))
		return false
	return true
}

function validarConvenioPacPat(obj){
	
	if(!validateField(obj.Nombre, "Nombre T"+char_i+"tular")){
		return false
	}
	
	if(!validateField(obj.Rut, "Rut")){
		return false
	}else if(!Rut(obj,obj.Rut,obj.Rut.value)){
		obj.Rut.focus();
		alert("El Rut ingresado es incorrecto");
		return false
	}
	
	if(!validateField(obj.Telefono, "Tel"+char_e+"fono")){
			return false
	}else if(!validateNumber(obj.Telefono.value, "Tel"+char_e+"fono")){
		obj.Telefono.focus();
		return false
	}
	
	if(!validateField(obj.Poliza, "P"+char_o+"liza")){
			return false
	}else if(!validateNumber(obj.Poliza.value, "P"+char_o+"liza")){
		obj.Poliza.focus();
		return false
	}
	
	pago="no";
	pagoindex="";
	for(p=0;p<=3;++p){
		if(obj.Pago[p].checked){
			pago="si";
			pagoindex=p;
		}
	}
	if(pago=="no"){
		alert("Debe seleccionar una opci"+char_o+"n de pago")
		return false
	}
	
	if(obj.NumCuotas.value == ""){
		obj.NumCuotas.focus();
		alert("Debe ingresar el N"+char_u+"mero de Cuotas");
		return false
	}else if(isNaN(obj.NumCuotas.value)){
		obj.NumCuotas.focus();
		alert("Error en el N"+char_u+"mero de Cuotas");
		return false
	}else{
		if((pagoindex==0)||(pagoindex==1)){
			if((obj.NumCuotas.value>11)||(obj.NumCuotas.value<0)){
				obj.NumCuotas.focus();
				alert("N"+char_u+"mero de Cuotas inv"+char_a+"lido");
				return false
			}
		}else if((pagoindex==2)||(pagoindex==3)){
			if((obj.NumCuotas.value>10)||(obj.NumCuotas.value<0)){
				obj.NumCuotas.focus();
				alert("N"+char_u+"mero de Cuotas inv"+char_a+"lido");
				return false
			}
		}
	}
	
	vencimiento="no";
	vencimientoindex="";
	for(v=0;v<=2;++v){
		if(obj.Vencimiento[v].checked){
			vencimiento="si";
			vencimientoindex=v;
		}
	}
	if(vencimiento=="no"){
		alert("Debe seleccionar un d"+char_i+"a de vencimiento de la cuota")
		return false
	}
	
	if(pagoindex==0){
		if(!validateField(obj.PACnombre, "Pago PAC Nombre T"+char_i+"tular")){
			return false
		}
		if(!validateField(obj.PACrut, "Pago PAC Rut")){
			return false
		}else if(!Rut(obj,obj.PACrut,obj.PACrut.value)){
			obj.Rut.focus();
			alert("El Rut ingresado en Pago PAC es incorrecto");
			return false
		}
		if(!validateField(obj.PACtelefono, "Pago PAC Tel"+char_e+"fono")){
				return false
		}else if(!validateNumber(obj.PACtelefono.value, "Pago PAC Tel"+char_e+"fono")){
			obj.PACtelefono.focus();
			return false
		}
		if(!validateField(obj.PACdireccion, "Pago PAC Direcci"+char_o+"n")){
			return false
		}
		if(!validateField(obj.PACcomuna, "Pago PAC Comuna")){
			return false
		}
		if(!validateField(obj.PACciudad, "Pago PAC Ciudad")){
			return false
		}
		if(!validateField(obj.PACcuenta, "Pago PAC N"+char_u+"mero Cuenta Corriente")){
				return false
		}
		if(obj.PACbanco.selectedIndex == 0){
			alert("Debe seleccionar un banco en Pago PAC");
			return false
		}
		if(!validateField(obj.PACsucursal, "Pago PAC Sucursal banco")){
			return false
		}
	}
	
	if(pagoindex==1){
		if(!validateField(obj.PATnombre, "Pago PAT Nombre T"+char_i+"tular")){
			return false
		}
		if(!validateField(obj.PATrut, "Pago PAT Rut")){
			return false
		}else if(!Rut(obj,obj.PATrut,obj.PATrut.value)){
			obj.Rut.focus();
			alert("El Rut ingresado en Pago PAT es incorrecto");
			return false
		}
		if(!validateField(obj.PATtelefono, "Pago PAT Tel"+char_e+"fono")){
				return false
		}else if(!validateNumber(obj.PATtelefono.value, "Pago PAT Tel"+char_e+"fono")){
			obj.PATtelefono.focus();
			return false
		}
		if(!validateField(obj.PATdireccion, "Pago PAT Direcci"+char_o+"n")){
			return false
		}
		if(!validateField(obj.PATcomuna, "Pago PAT Comuna")){
			return false
		}
		if(!validateField(obj.PATciudad, "Pago PAT Ciudad")){
			return false
		}
		numtarjeta="si";
		for(c=1;c<=4;++c){
			eval("objvalidar = obj.PATnumTarjeta"+c+";");
			if( ( objvalidar.value == "" ) || ( isNaN(objvalidar.value) ) ){
				numtarjeta="no";
			}else{
				if( objvalidar.value.length == 1){
					valortmp="000"+objvalidar.value;
					objvalidar.value = valortmp;
				}else if( objvalidar.value.length == 2){
					valortmp="00"+objvalidar.value;
					objvalidar.value = valortmp;
				}else if( objvalidar.value.length == 3){
					valortmp="0"+objvalidar.value;
					objvalidar.value = valortmp;
				}
			}
		}
		if( numtarjeta == "no" ){
			alert("Debe ingresar un n"+char_u+"mero de Tarjeta v"+char_a+"lida");	
			return false
		}
		if(!validateField(obj.PATbanco, "Pago PAT Banco emisor")){
			return false
		}
		if((obj.PATvencimientoMes.value=="")||(isNaN(obj.PATvencimientoMes.value))){
			alert("Debe ingresar un mes de vencimiento de la Tarjeta de Cr"+e+"dito v"+char_a+"lido");	
			return false
		}else if((obj.PATvencimientoMes.value<1)||(obj.PATvencimientoMes.value>12)){
			alert("Mes de vencimiento de la Tarjeta de Cr"+e+"dito inv"+char_a+"lido");	
			return false
		}
		if((obj.PATvencimientoAno.value=="")||(isNaN(obj.PATvencimientoAno.value))){
			alert("Debe ingresar un a"+char_n+"o de vencimiento de la Tarjeta de Cr"+e+"dito v"+char_a+"lido");	
			return false
		}else if((obj.PATvencimientoAno.value<2008)||(obj.PATvencimientoAno.value>3000)){
			alert("A"+char_n+"o de vencimiento de la Tarjeta de Cr"+e+"dito inv"+char_a+"lido");	
			return false
		}
		tarjeta="no";
		tarjetaindex="";
		for(t=0;t<=3;++t){
			if(obj.PATtarjeta[t].checked){
				tarjeta="si";
				tarjetaindex=t;
			}
		}
		if(tarjeta=="no"){
			alert("Debe seleccionar una Tarjeta de Cr"+e+"dito")
			return false
		}
	}

	return true
}

function formbtnfix(obj,accion){
	
	if(validate(obj)){
		obj.accion.value = accion;
		obj.submit();
	}
}

function revisarDigito( objfom, objtexto, dvr )
{	
	dv = dvr + ""	
	if ( dv != '0' && dv != '1' && dv != '2' && dv != '3' && dv != '4' && dv != '5' && dv != '6' && dv != '7' && dv != '8' && dv != '9' && dv != 'k'  && dv != 'K')	
	{		
				
		objtexto.focus();		
		objtexto.select();		
		return false;	
	}	
	return true;
}

function revisarDigito2( objfom, objtexto, crut )
{	
	largo = crut.length;	
	if ( largo < 2 )	
	{		
				
		objtexto.focus();		
		objtexto.select();		
		return false;
	}	
	if ( largo > 2 )		
		rut = crut.substring(0, largo - 1);	
	else		
		rut = crut.charAt(0);	
	dv = crut.charAt(largo-1);	
	revisarDigito( objfom, objtexto, dv );	

	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() )	
	{		
				
		objtexto.focus();		
		objtexto.select();		
		return false;	
	}

	return true
}

function Rut(objfom,objtexto,texto)
{	
	var tmpstr = "";	
	for ( i=0; i < texto.length ; i++ )		
		if ( texto.charAt(i) != ' ' && texto.charAt(i) != '.' && texto.charAt(i) != '-' )
			tmpstr = tmpstr + texto.charAt(i);	
	texto = tmpstr;	
	largo = texto.length;	

	if ( largo < 2 )	
	{		
				
		objtexto.focus();		
		objtexto.select();		
		return false;
	}	

	for (i=0; i < largo ; i++ )	
	{			
		if ( texto.charAt(i) !="0" && texto.charAt(i) != "1" && texto.charAt(i) !="2" && texto.charAt(i) != "3" && texto.charAt(i) != "4" && texto.charAt(i) !="5" && texto.charAt(i) != "6" && texto.charAt(i) != "7" && texto.charAt(i) !="8" && texto.charAt(i) != "9" && texto.charAt(i) !="k" && texto.charAt(i) != "K" )
 		{			
						
			objtexto.focus();			
			objtexto.select();			
			return false;		
		}	
	}	

	var invertido = "";	
	for ( i=(largo-1),j=0; i>=0; i--,j++ )		
		invertido = invertido + texto.charAt(i);	
	var dtexto = "";	
	dtexto = dtexto + invertido.charAt(0);	
	dtexto = dtexto + '-';	
	cnt = 0;	

	for ( i=1,j=2; i<largo; i++,j++ )	
	{		
		//alert("i=[" + i + "] j=[" + j +"]" );		
		if ( cnt == 3 )		
		{			
			dtexto = dtexto + '.';			
			j++;			
			dtexto = dtexto + invertido.charAt(i);			
			cnt = 1;		
		}		
		else		
		{				
			dtexto = dtexto + invertido.charAt(i);			
			cnt++;		
		}	
	}	

	invertido = "";	
	for ( i=(dtexto.length-1),j=0; i>=0; i--,j++ )		
		invertido = invertido + dtexto.charAt(i);	

	objtexto.value = invertido.toUpperCase()		

	if ( revisarDigito2(objfom,objtexto,texto) )		
		return true;	

	return false;
}
