
function NumberWithFormat(vNumero,vFormat,vObject,vMonto) {
    
	//alert(vNumero+", "+vFormat+", "+vObject+", "+vMonto); //**
   var vWithFormat='';
   var vDec   = '';
   var vEnt   = '';
   var j = 0;
   var vPos=0;
   var vNumDec=0;
   var vFinalFormat='';

   strSelection=false;
        
   if ( vMonto ) {
   
        vPos = vFormat.search(",");
   
        if (vPos > -1 ) {
                vDec = vFormat.substr(vPos+1);
                vNumDec= vDec.length;
        }
        if( vNumDec > 0 ) 
        {
              vPos = vNumero.search(",");
              if (vPos < 0 ) vPos = vNumero.search("[.]");
              
              if (vPos > -1 ){
                vEnt = vNumero.substr(0,vPos);
                vDec= vNumero.substr(vPos+1,vNumDec);
                vNumero = vEnt+vDec;
                vNumDec = vNumDec - vDec.length;
              }
              for (var i = 1 ; i <= vNumDec; i++)
                  vNumero = vNumero + '0';
              
        }
   }
   
   j =(vNumero.length-1)
   for ( var i = (vFormat.length-1) ; i >= 0 ; i--) {
  
        if (vFormat.charAt(i) == '0' || vFormat.charAt(i) == '#'  || vFormat.charAt(i) == '$') {
            if( j > -1) {
                vWithFormat = vNumero.charAt(j) + vWithFormat;
                j--;
            }else
              vWithFormat = '0' + vWithFormat;
        }else {
            vWithFormat = vFormat.charAt(i)+vWithFormat;
        }
   }
   j =(vNumero.length-1)
   for ( var i = 0; i <= (vFormat.length-1) ; i++) {
        if ( vFormat.charAt(i) == '0' ) {
            vFinalFormat = vFinalFormat + vWithFormat.charAt(i);
        }else if ( vFormat.charAt(i) =='#' || vFormat.charAt(i) =='$') {
            if ( vWithFormat.charAt(i) == '0' && vFinalFormat == '' ) 
                vFinalFormat = vFinalFormat + '';
            else
                vFinalFormat = vFinalFormat + vWithFormat.charAt(i);
        }else {
            if (vFinalFormat != '' && vFinalFormat != '-') 
                vFinalFormat = vFinalFormat + vWithFormat.charAt(i);
        }
   }
   if (vObject != null) {

        vPos = vFinalFormat.search("-");
        if (vPos > -1)
	        vFinalFormat = '-'+vFinalFormat.substr(0,vPos)+vFinalFormat.substr(vPos+1);

		vObject.value = vFinalFormat;
   }
   return vFinalFormat;
}
function ValidateNumber(vObject,e,vMonto,vDecimal,vSign,vTotalDecimal) {
	//alert(vObject+", "+e+", "+vMonto+", "+vDecimal+", "+vSign+", "+vTotalDecimal); //**
    var vPos;
    var vNumber;
    var vNumberStr='';
    var vContador = 0;
	var vEnt = 0;
	var vDec = 0;

    var eKeyCode = 0;


	if (typeof vTotalDecimal == 'undefined')
		vTotalDecimal = 2;

    eKeyCode = (window.event?window.event.keyCode:(e?e.which:0));
    
    if (8==eKeyCode) return true;

    if (vSign){
		if(vObject.value.length > vObject.maxLength - 1)
		{
            if (eKeyCode != 45 ) 
            {
				if (strSelection) 
				{
					strSelection=false;
					return true;
				}
				return false;
			}
        }
    } else {
        if (eKeyCode == 45 ) return false;
    }


    if (vMonto) {
        if ((eKeyCode <=47 || eKeyCode >=58 ) && eKeyCode !=46 && eKeyCode !=45 && eKeyCode !=8) {
            return false;
        }
        if (vDecimal ){
            if (eKeyCode == 46 ) {
                vPos = vObject.value.search(",");
				if (vPos < 0 ) vPos = vObject.value.search("[.]");
                if (vPos > -1)
                    return false;
                else
                    return true;
	        }else{
				if (strSelection) 
				{
					vObject.value = "";
					strSelection=false;
				}
                vPos = vObject.value.search(",");
				if (vPos < 0 ) vPos = vObject.value.search("[.]");
                if (vPos > -1) {
					vDec = vTotalDecimal - 1;
					if (vObject.value.substr(vPos+1).length > vDec)
						return false;
			    } else {
					vEnt = vObject.maxLength - vTotalDecimal - 2;
			        if (vObject.value.length > vEnt) 
						return false;
			    }
            }
        }else{
            if (eKeyCode == 46 ) {
                return false;
            }
        }
	        
        if (vSign){
            if (eKeyCode == 45 ) {
				if (strSelection) 
				{
					vObject.value = "-";
					strSelection=false;
				}
                vPos = vObject.value.search("-");
                if (vPos > -1)
                    return false;
                else
					if (vObject.value.length == 0)	
						return true;
	                else							
						return false;
	        }else{
				if (strSelection) 
				{
					vObject.value = "";
					strSelection=false;
				}
                vPos = vObject.value.search("-");
                if (vPos > -1)
                {
	                vNumberStr = "-"+vObject.value.substr(0,vPos)+vObject.value.substr(vPos+1);
					vObject.value = vNumberStr;
	            }
                return true;
			}   
        }
    }else{
        if ((eKeyCode <=47 || eKeyCode >=58 )) {
            return false;
        }
    }
    return true;
}
function NumberWithoutFormat(vNumero,vObject,vMonto){
    var vWithoutFormat='';
    var vDec    = '';
    var vNumDec = 0;
    var vDecAux ='';

    vPos = vNumero.search(",");

    if(vPos > -1 ) {
        vDec = vNumero.substr(vPos+1);
        vNumero = vNumero.substr(0,vPos);
    
    }
    
    for( var i = 0; i <= (vNumero.length-1);i++)
    {
        if (!vMonto){
            if( vNumero.charCodeAt(i) >= 48 && vNumero.charCodeAt(i) <=57){
                vWithoutFormat=vWithoutFormat+vNumero.charAt(i);
            }else if ( vNumero.charCodeAt(i)==44){
                vWithoutFormat=vWithoutFormat+vNumero.charAt(i);
            }
        }else{
            if( vNumero.charCodeAt(i) >= 48 && vNumero.charCodeAt(i) <=57){
                vWithoutFormat=vWithoutFormat+vNumero.charAt(i);
            }else if ( vNumero.charCodeAt(i)==44){
                vWithoutFormat=vWithoutFormat+vNumero.charAt(i);
            }else if ( vNumero.charCodeAt(i)==45){
                vWithoutFormat=vWithoutFormat+vNumero.charAt(i);
            }
        
        }
    }
    
    if( vDec > 0 ){
        vDecAux= vDec;
        for ( var i = (vDec.length-1); i >= 0;i--){
            if (vDec.charCodeAt(i)==48)
                vDecAux = vDec.substr(0,i);
            else
                i = -1;
        }
        vDec=vDecAux;
        vWithoutFormat = vWithoutFormat + ','+vDec;
    }       
            
            
    if (vObject != null){
		strSelection=true;
        vObject.value = vWithoutFormat;
        vObject.select();
    }
    return vWithoutFormat;
    
}


function RutWithFormat(vNumero,vFormat,vObject) {
   var vWithFormat='';
   var j = 0;
   var vPos=0;
   var vFinalFormat='';
   var vDv='';
   var vDvIng='';
	

	
   //alert("vObject : "+vObject.value.length)
   if(vObject.value.length ==0){
		//alert("Debe Ingresar (*)Rut");
		return false;
   }
   
	vDvIng = vNumero.substr(vNumero.length-1,1);
	vDv = checkRut(vNumero.substr(0,vNumero.length-1));
	vDvIng =( 'k'==vDvIng?'K':vDvIng) ;
	if (vDvIng != vDv) {
		
	   alert('Rut invalido');
	   document.getElementById('data_rut').value = "";
		document.getElementById('data_rut').focus();
	   if (vObject != null)
	       vObject.focus();
	   return false;
	}
        
   j =(vNumero.length-1)
   for ( var i = (vFormat.length-1) ; i >= 0 ; i--) {
  
        if (vFormat.charAt(i) == '0' || vFormat.charAt(i) == '#') {
            if( j > -1){
                vWithFormat = vNumero.charAt(j) + vWithFormat;
                j--;
            }else
              vWithFormat = '0' + vWithFormat;
        }else {
            vWithFormat = vFormat.charAt(i)+vWithFormat;
        }
   }
   j =(vNumero.length-1)
   for ( var i = 0; i <= (vFormat.length-1) ; i++) {
        if ( vFormat.charAt(i) == '0' ) {
            vFinalFormat = vFinalFormat + vWithFormat.charAt(i);
        }else if ( vFormat.charAt(i) =='#') {
            if ( vWithFormat.charAt(i) == '0' && vFinalFormat == '' ) 
                vFinalFormat = vFinalFormat + '';
            else
                vFinalFormat = vFinalFormat + vWithFormat.charAt(i);
        }else {
            if (vFinalFormat != '' && vFinalFormat != '-') 
                vFinalFormat = vFinalFormat + vWithFormat.charAt(i);
        }
   }
   if (vObject != null) {
    vObject.value = vFinalFormat;
   }
   //
       var rut = document.getElementById('data_rut').value
	var largo 
   rut = rut.replace(".","");
		rut = rut.replace(".","");
		rut = rut.replace("-","");
		largo = rut.length;
		largo = largo - 1;
		rut = rut.substring(0,largo);
			if (rut  >= 50000000 ){
				alert("Estimado cliente, esta tarjeta es sólo para personas naturales.");
				document.getElementById('data_rut').value = "";
				document.getElementById('data_rut').focus();
				return;
			}
			
			if (rut  > 21000000 & rut < 50000000){
				alert("Estimado cliente, no es posible procesar su solicitud, por favor diríjase a cualquier sucursal del Banco Santander.");
				document.getElementById('data_rut').value = "";
				document.getElementById('data_rut').focus();
				return;
			}
	//
   return vFinalFormat;
}
function ValidateRut(vObject,e) {
    var vPos;
    var eKeyCode = 0;

    eKeyCode = (window.event?window.event.keyCode:(e?e.which:0));

    vPos = vObject.value.search('k')
    if (vPos == -1) vPos = vObject.value.search('K');

    if ((vPos > -1) && (eKeyCode !=8))
        return false;

    if ((eKeyCode <=47 || eKeyCode >=58 ) && (eKeyCode !=107) && (eKeyCode !=75) &&(eKeyCode !=8))
        return false;
    if (eKeyCode == 75 || eKeyCode == 107)
        return true;

    return true;
}
function RutWithoutFormat(vNumero,vObject) {
    var vWithoutFormat='';

    for( var i = 0; i <= (vNumero.length-1);i++) {
        if ( ( vNumero.charCodeAt(i) >= 48 && vNumero.charCodeAt(i) <=57) || (vNumero.charCodeAt(i) == 107) || (vNumero.charCodeAt(i) == 75)) {
            if (vNumero.charCodeAt(i)==75 || vNumero.charCodeAt(i)==107)
                vWithoutFormat=vWithoutFormat+'K';
            else
                vWithoutFormat=vWithoutFormat+vNumero.charAt(i);
        }else if ( vNumero.charCodeAt(i)==44){
            vWithoutFormat=vWithoutFormat+vNumero.charAt(i);
        }
    }
    if (vObject != null) {
        vObject.value = vWithoutFormat;
	    vObject.select();
    }
    return vWithoutFormat;
}
function checkRut(vRut) {
    var vKey = '0K987654321';
    var vVal ;
    var vRutFmt = NumberWithFormat(vRut,'0000000000',null,false);
    vVal = parseInt(vRutFmt.substr(0,1)) * 5 +
           parseInt(vRutFmt.substr(1,1)) * 4 +
           parseInt(vRutFmt.substr(2,1)) * 3 +
           parseInt(vRutFmt.substr(3,1)) * 2 +
           parseInt(vRutFmt.substr(4,1)) * 7 +
           parseInt(vRutFmt.substr(5,1)) * 6 +
           parseInt(vRutFmt.substr(6,1)) * 5 +
           parseInt(vRutFmt.substr(7,1)) * 4 +
           parseInt(vRutFmt.substr(8,1)) * 3 +
           parseInt(vRutFmt.substr(9,1)) * 2;
    vVal = vVal % 11;
    return vKey.substr(vVal, 1);
}


function PresionaEnter(obj,objBlur) {
	if(obj.value.length>0) {
		if (event.keyCode==13) {
			/*event.keyCode = 9;*/
			objBlur.focus();
			return false;
		}
		return true;
	}
}
function Left(str, n){
	if (n <= 0)
	    return "";
	else if (n > String(str).length)
	    return str;
	else
	    return String(str).substring(0,n);
}
function Right(str, n){
    if (n <= 0)
       return "";
    else if (n > String(str).length)
       return str;
    else {
       var iLen = String(str).length;
       return String(str).substring(iLen - n, iLen);
    }
}


function GetChar(Event)
{
 if (navigator.appName == "Netscape") 
  return (Event.which);
 else 
  return (Event.keyCode);
}

function DelChar(Event)
{
 if (navigator.appName == "Netscape") 
  Event.which   = 0;
 else 
  Event.keyCode = 0;
}
function isInteger(Event)
{ 
 var charCode = GetChar(Event); 
 if ((charCode >= 48 && charCode <= 57) || charCode == 46)
 { 
  return charCode; 
 } 
 else 
 {
  DelChar(Event);
  return -1; 
 }
} 

function txtNumeroDocumento_onkeypress(Event)
{
 isInteger(Event);
}

function txtNumeroOperacion_onkeypress(Event)
{
 isInteger(Event);
}

function js_isdigit(Str)
{
// Valida si es digito (estrictamente)
  var ch, uno=0
  
  if (Str.length == 0)
   return false;
  for (i = 0 ; i < Str.length ; i++) {
   ch = Str.substring(i,i+1);
   if(ch == '.')
   uno = uno + 1;
   
   if (uno>=2)
   return false;
   
   if (ch != '0' && ch != '1' && ch != '2' && ch != '3' && ch != '4' && ch != '5' && ch != '6' && ch != '7' && ch != '8' && ch != '9' )
   return false;
  }
  return true;    
}

//Funciones para validar formulario 1
function envia() {

	if (document.getElementById('data_rut').value  == ''){
	alert('Ingrese Rut');
	document.getElementById('data_rut').focus();
	return;
	}
		
	if (document.getElementById('data_nombre').value  == ''){
	alert('Ingrese Nombre');
	document.getElementById('data_nombre').focus();
	return;
	}
	
	nombre = document.getElementById('data_nombre').value;

	if (!/[A-Za-zñÑ\s]/.test(nombre)){
		alert("Nombres debe tener solo letras");
		document.getElementById('data_nombre').value = "";
		document.getElementById('data_nombre').focus();
		return;
    }
	
	apellidoPat = document.getElementById('data_apePat').value;
	
	if (document.getElementById('data_apePat').value  == ''){
	alert('Ingrese Apellido Paterno');
	document.getElementById('data_apePat').focus();
	return;
	}
	
	if (!/[A-Za-zñÑ\s]/.test(apellidoPat)){
		alert("Apellido Paterno debe tener solo letras");
		document.getElementById('data_apePat').value = "";
		document.getElementById('data_apePat').focus();
		return;
    }
    
    apellidoMat = document.getElementById('data_apeMat').value;
    
	if (document.getElementById('data_apeMat').value  == ''){
	alert('Ingrese Apellido Materno');
	document.getElementById('data_apeMat').focus();
	return;
	}
	
	if (!/[A-Za-zñÑ\s]/.test(apellidoMat)){
		alert("Apellido Materno debe tener solo letras");
		document.getElementById('data_apeMat').value = "";
		document.getElementById('data_apeMat').focus();
		return;
    }
	
	if (document.getElementById('data_dia').value  == '-1'){
	alert('Seleccione dia de nacimiento');
	return;
	}
	
	if (document.getElementById('data_Mes').value  == '-1'){
	alert('Seleccione mes de nacimiento');
	return;
	}
	
	if (document.getElementById('data_anio').value  == '-1'){
	alert('Seleccione año de nacimiento');
	return;
	}
	
	if (document.getElementById('data_genero').value  == '-1'){
	alert('Seleccione sexo');
	return;
	}
	
	if (document.getElementById('data_estadoCiv').value == '-1'){
	alert('Seleccione estado civil');
	return;
	}
	
	if (document.getElementById('data_nivEst').value == '-1'){
	alert('Seleccione nivel de estudios');
	return;
	}
	
	if (document.getElementById('data_profesion').value == '-1'){
	alert('Seleccione profesión');
	return;
	}
	
	if (document.getElementById('data_tipo_trabajo').value  == '-1'){
	alert('Seleccione tipo de trabajo');
	return;
	}
	
	var hoy = new Date();
	var year = hoy.getYear();
	var resta;
	
	if(navigator.appName == "Netscape"){
	year += 1900
	}
	valor = document.getElementById('data_ingreso').value
	resta = year - valor

	if (document.getElementById('data_tipo_trabajo').value  == 'PDE'){
		if(document.getElementById('data_ingreso').value  == ''){
			alert("Ingrese año");
			document.formulario.data_ingreso.focus();
			return;
			}
		if(resta <= 2){
			if(document.getElementById('data_mes_ingreso').value == '-1'){
				alert("Ingrese mes");
				return;
			}
		}
	}

	
	if (document.getElementById('data_tipo_trabajo').value  == 'PDE'){
		if (document.getElementById('data_contrato').value  == '-1'){
			alert('Seleccione tipo de contrato');
			return;
		}
	}

	if (document.getElementById('data_tipo_trabajo').value  == 'PIN'){
		if(document.getElementById('data_ingreso').value  == ''){
			alert("Ingrese año");
			return;
		}
		if(resta <= 2){
			if(document.getElementById('data_mes_ingreso').value == '-1'){
				alert("Ingrese mes");
				return;
			}
		}
	}
	
	if (document.getElementById('data_renta').value  == '-1'){
	alert('Seleccione Renta');
	return;
	}
	
	else{
	
	document.formulario.method = 'post';
	document.formulario.action = 'index2.asp';
	document.formulario.submit();
	}
}



//Funciones para validar formulario 2
function envia2() {
	
	calle = document.getElementById('data_calle').value;
	if (document.getElementById('data_calle').value  == ''){
	alert('Ingrese nombre de calle');
	document.getElementById('data_calle').focus();

	return;
	}
	
	if (!/[A-Za-zñÑ\s]/.test(calle)){
		alert("Calle debe tener solo letras");
		document.getElementById('data_calle').value = "";
		document.getElementById('data_calle').focus();
		return;
    }
    
	ncasa = document.getElementById('data_ncasa').value; 
	
	if (document.getElementById('data_ncasa').value  == ''){
	alert('Ingrese número de calle');
	document.getElementById('data_ncasa').focus();
	return;
	}
	
	if (!/\d/.test(ncasa)){
		alert("Número de casa debe tener solo numeros");
		document.getElementById('data_ncasa').value = "";
		document.getElementById('data_ncasa').focus();
		return;
    }

//	if (document.getElementById('data_resto').value  == ''){
//	alert('Ingrese Dpto/Block/Villa/Condominio');
//	document.getElementById('data_resto').focus();
//	return;
//	}
	
	if (document.getElementById('data_region').value  == '0'){
	alert('Seleccione Region');
	return;
	}

	if (document.getElementById('data_region').value  == '99'){
	alert ('Estimado cliente, por este medio sólo se pueden solicitar tarjetas a entregar en las regiones V, VIII y RM. Por favor diríjase a cualquier sucursal del Banco Santander.')
	return ;
	}
	
	if (document.getElementById('data_comuna').value  == '-1'){
	alert('Seleccione Comuna');
	return;
	}
	
	if (document.getElementById('data_telefono1').value  == '' && document.formulario.data_celular1.value  == ''){
	alert('Debe ingresar al menos un teléfono de contacto');
	return;
	}
	
	telefono = document.getElementById('data_telefono1').value;
	if (telefono != "") {
	if (!/\d/.test(telefono)){
		alert("Telefono debe tener solo numeros");
		document.getElementById('data_telefono1').value = "";
		document.getElementById('data_telefono1').focus();
		return;
    }
    }
    
    celular = document.getElementById('data_celular1').value;
    if (celular != ""){
	if (!/\d/.test(celular)){
		alert("Celular debe tener solo numeros");
		document.getElementById('data_celular1').value = "";
		document.getElementById('data_celular1').focus();
		return;
    }
    }
    
	if (document.getElementById('data_email').value  == ''){
	alert('Ingrese Email');
	document.getElementById('data_email').focus();
	return;
	}
	
	if(!validarEmail(document.getElementById('data_email').value))
	{
	alert('Email ingresado es incorrecto');
	document.getElementById('data_email').value = "";
	document.getElementById('data_email').focus();
	return;
	}
	
	else{
	document.formulario.method = 'post';
	document.formulario.action = 'generaLog.asp';
	document.formulario.submit();
	}
}


function validarEmail(valor) {
  if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(valor)){
   return true;
  } else {
   return false;
  }
}



//ingreso de solo numeros

function soloNum(Event)
{
 tecla = (document.all) ? Event.keyCode : Event.which;
 if (tecla==8) return true;
 patron =/\d/;
 te = String.fromCharCode(tecla);
 return patron.test(te); 
}

//fin


//ingreso de solo letras

function soloChar(Event)
{
  tecla = (document.all) ? Event.keyCode : Event.which;
 if (tecla==8) return true;
 patron =/[A-Za-zñÑ\s]/;
 te = String.fromCharCode(tecla);
 return patron.test(te); 
}
//fin


//valida fecha de ingreso
function fechaIngreso(valor)
{

var hoy = new Date();
var year = hoy.getYear();
var resta;

	if(valor.length < 4){
	alert("Año incorrecto")
	document.getElementById('data_ingreso').value = "";
	document.getElementById('data_ingreso').focus();
	return;
	}
	if(navigator.appName == "Netscape"){
	year += 1900
	}
	
	resta = year - valor
	if(valor > year){
		alert("Año ingresado no puede ser mayor al año actual.");
		document.getElementById('data_ingreso').value = "";
		document.getElementById('data_ingreso').focus();
		document.getElementById('data_mes_ingreso').selectedIndex=0;
		document.getElementById("TRAB2").style.display = "none";
		return;
	}
	if(valor < 1950){
		alert("Año ingresado no puede ser menor a 1950.");
		document.getElementById('data_ingreso').value = "";
		document.getElementById('data_ingreso').focus();
		document.getElementById('data_mes_ingreso').selectedIndex=0;
		document.getElementById("TRAB2").style.display = "none";
		return;
	}
	if(resta <= 2){
		document.getElementById("TRAB2").style.display = "";
	}
	else
		document.getElementById("TRAB2").style.display = "none";
		document.getElementById('data_mes_ingreso').selectedIndex=0;
}
//fin

//valida fecha de ingreso
function validaMes(valor)
{
var hoy = new Date();
var year = hoy.getYear();
var anio = document.getElementById('data_ingreso').value;
var mes = hoy.getMonth();

if(navigator.appName == "Netscape"){
	year += 1900
	}
	
	if(year == anio){
	if(valor > mes + 1){
		alert("Mes ingresado no puede ser mayor o igual al mes actual.");
		document.getElementById('data_ingreso').value = "";
		document.getElementById('data_ingreso').focus();
			document.getElementById('data_mes_ingreso').selectedIndex=0;
		document.getElementById("TRAB2").style.display = "none";
	}
	}
}
//fin
