// JavaScript Document
function VTexto(nombre,campo)
{
valor= campo.value
if ( valor == "" )
   {
    alert("Ingrese " + nombre)
    campo.select();
    campo.focus()
    return false
    }

if (Blancos(valor)) {
        alert(nombre + " debe ser distinto de Blancos");
        campo.select();
        campo.focus();
        return false;
        }
if (Invalidos(valor)) {
        alert("Debe ingresar " + nombre + " sin caracteres [<>'^|]%&,");
        campo.select();
        campo.focus();
        return false;
        }
return true		
}

function VListbox(nombre,campo)
{
valor= campo.value
if ( valor == "" )
   {
    alert("Seleccione " + nombre)
	campo.focus();
    return false;
    }

if (Blancos(valor)) {
        alert(nombre + " debe ser distinto de Blancos");
		campo.focus();
        return false;
        }
return true		
}

function VChecked(nombre,campo)
{
	 j=0
	 for (var i=0; i < campo.length; i++)
		{ 
		if (campo[i].checked==true)
		   {
					j++
 		   }
		}
	 if (j==0)
		 {
		 alert("Debe seleccionar " + nombre);
		 return false;
		 }	
return true;		 
}

function VNumber(nombre,campo)
{
valor=campo.value
if ( valor == "" )
   {
    alert("Ingrese " + nombre)
    campo.focus()
    return false
    }

if (Blancos(valor)) {
        alert(nombre + " debe ser distinto de blancos");
        campo.select();
        campo.focus();
        return false;
        }

if ( !isInteger(valor))
   {
    alert(nombre + " debe ser numérico")
    campo.select();
    campo.focus();    return false
    }

if ( valor.length < campo.maxlength)
   {
    alert(nombre + " debe ser numérico")
    campo.select();
    campo.focus();    return false
    }		
return true		
}

function VRut(nombre,campo,dv)
{

if (!checkdv(campo, dv))
   {
    campo.focus()
    return false;
    }
}

function borrar_gen() 
{
 page=window.location.pathname
 window.location.replace(page)
 return;
}

function VEmail(nombre,campo)
{
valor=campo.value
if (!checkMail(valor))
   {
        alert("E-MAIL requiere de un \"@\" y un \".\".\n\Por favor corrija.")
        campo.select();
        campo.focus();
        return false;
   }
}

function VLargo(nombre,campo,largo)
{
valor=campo.value
if (valor.length < largo) {
     alert (nombre + " debe tener " + largo + " dígitos");
     campo.select();
     campo.focus()
     return false;
    }
}
function VToday(nombre,campo)
{
  Today = new Date();
  Today = String(Today.getFullYear()) + right('0' + String(Today.getMonth()+1),2) + right('0' + String(Today.getDate()+1),2)
  fecha = campo.value.substr(6,4) + campo.value.substr(3,2) + campo.value.substr(0,2);
  if (fecha > Today)
   {
     alert (nombre + " debe ser menor igual a fecha actual");
     campo.select();
     campo.focus();
     return false;
   }
}

function VYear(nombre,campo)
{
  Today = new Date();
  year1 = Today.getFullYear()
  yearing = campo.value
  if ( yearing > year1)
   {
     alert (nombre + " debe ser menor igual a año actual");
     campo.select();
     campo.focus();
     return false;
   }
}
function right(str,nbr) { 
   return str.substr(str.length-nbr) 
} 

function IsDate(nombre,campo) {
    var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
	var dateStr = campo.value
    var matchArray = dateStr.match(datePat); // is the format ok?
	
    if (matchArray == null) {
        alert("Ingrese " + nombre + " en formato dd/mm/aaaa Ej: 15/01/1976");
		campo.select();
        campo.focus();
        return false;
    }
    var mToday = new Date();
    month =  matchArray[3]; // parse date into variables
	day = matchArray[1];
    year = matchArray[5];

	if (month.length < 2 )
	{
	   month = '0' + month
	 }  
	 if (day.length < 2 )
	{
	   day = '0' + day
	 }   
    
   campo.value = day +'/' + month + '/' + year
	   
    if (month < 1 || month > 12) { // check month range
        alert(nombre + " tiene mes invalido");
		campo.select();
        campo.focus();
        return false;
    }

    if (day < 1 || day > 31) {
        alert(nombre + " tiene día invalido");
  	    campo.select();
        campo.focus();
        return false;
    }

    if ((month==4 || month==6 || month==9 || month==11) && day==31) {
        alert(nombre + " Invalida")
	    campo.select();
        campo.focus();
        return false;
    }

    if (month == 2) { // check for february 29th
        var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
        if (day > 29 || (day==29 && !isleap)) {
            alert(nombre + " Invalida");
		    campo.select();
            campo.focus();
            return false;
        }
    }
    return true; // date is valid
}

function taLimit() {
	var taObj=event.srcElement;
	if (taObj.value.length==taObj.maxLength*1) return false;
}

function checkdv(obj_rut, obj_dig)
// -------------------------------
{
 var rut, dv

  rut = obj_rut.value
  dv =  obj_dig.value

  largo = rut.length;
  if ( largo < 1 )
   {
    alert("Debe ingresar el Número de  RUT completo");
    obj_rut.focus();
    obj_rut.select();
    return false;
   }

  if ( !checkCDV( dv,obj_dig ) ) {
     obj_dig.focus();
     obj_dig.select();
     return false;
     }
  if ( rut == null || dv == null )
     return false;

  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 Digito Verificador no corresponde al RUT ingresado; revise ambos campos");
    obj_rut.focus();
    obj_rut.select();

    return false;
   }

  return true;
}

//----------------------------------------------------------------------------------//
function checkCDV( dvr, obj_dig)
 {
  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')
   {
    alert ("Debe Ingresar Dígito Verificador Correcto");

    return false;
   }
  return true;
 }
//---------------------------------------------------------------------------------//
// chequea e-mail

  function checkMail(correo)
  {
   if (correo.indexOf ('@',0) == -1 || correo.indexOf ('.',0) == -1)
    {
     return false;
    }
   return true;
  }
//---------------------------------------------------------------------------------//
function Invalidos(orig)
{

 dato = orig + "";

 pos   = dato.indexOf("<",0);
 if (pos > -1)
  {
    return true;
  }

 pos = dato.indexOf(">");
 if (pos > -1)
  {
    return true;
  }

 pos = dato.indexOf("'");
 if (pos > -1)
  {
    return true;
  }

 pos = dato.indexOf('"');
 if (pos > -1)
  {
    return true;
  }

 pos = dato.indexOf("^");
 if (pos > -1)
  {
    return true;
  }
 pos = dato.indexOf("|");
 if (pos > -1)
  {
    return true;
  }
 pos = dato.indexOf("%");
 if (pos > -1)
  {
    return true;
  }
 pos = dato.indexOf("&");
 if (pos > -1)
  {
    return true;
  }
 pos = dato.indexOf("^");
 if (pos > -1)
  {
    return true;
  }
 pos = dato.indexOf(",");
 if (pos > -1)
  {
    return true;
  }
 return false;
}

function Blancos(orig)
//-------------------------
{
 var rep, obs2, obs

 obs = orig + "";

 rep = / /gi ;
 obs2 = obs.replace(rep, "");

 if (obs2 == "")   {
    return true;
    }

 return false;
}
//---------------------
function isInteger (s)
{ var i;

for (i = 0; i < s.length; i++)
{
var c = s.charAt(i);
if( i != 0 ) {
if (!isDigit(c)) return false;
} else {
if (!isDigit(c) && (c != "-") || (c == "+")) return false;
}
}
return true;
}

//---------------------
function isDigit (c)
{ return ((c >= "0") && (c <= "9"))
}

function ValFecha(nombre, day, month, year) {
	if (month.length < 2 )
	{
	   month = '0' + month
	 }  
	 if (day.length < 2 )
	{
	   day = '0' + day
	 }   
    

    if (month < 1 || month > 12) { // check month range
        alert(nombre + " tiene mes invalido");
        return false;
    }

    if (day < 1 || day > 31) {
        alert(nombre + " tiene día invalido");
        return false;
    }

    if ((month==4 || month==6 || month==9 || month==11) && day==31) {
        alert(nombre + " Invalida")
        return false;
    }


    if (month == 2) { // check for february 29th
        var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
        if (day > 29 || (day==29 && !isleap)) {
            alert(nombre + " Invalida");
            return false;
        }
    }
    return true; // date is valid
}


function ajusta_fecha(campo)
{
	var salida;
	if (campo.value!="")
	{
	salida = revisa_fecha(campo.value);
	if (salida == "" ) 
		{
			alert('Formato de fecha erroneo, debe usar dd/mm/yyyy');
			campo.value = "";
			campo.focus();
		}
	}
}

function revisa_fecha(fecha_in)
{
	var i;
	var texto;
	var salida;
	var valor;
	
	salida = fecha_in;
	texto = fecha_in.substr(0,2);
	if (isNaN(parseInt(texto)))
		salida="";
	else
	{
		valor = parseInt(texto,10);
		if (valor < 1 || valor > 31)
			salida = "";
	}
	texto = fecha_in.substr(2,1);
	if (texto != "/")
		salida = "";
	texto = fecha_in.substr(3,2);
	if (isNaN(parseInt(texto)))
		salida="";
	else
	{
		valor = parseInt(texto,10);
		if (valor < 1 || valor > 12)
			salida = "";
	}
	texto = fecha_in.substr(5,1);
	if (texto != "/")
		salida = "";
	texto = fecha_in.substr(6,4);
	if (isNaN(parseInt(texto)))
		salida="";
	else
	{
		valor = parseInt(texto,10);
		if (valor < 1000 || valor > 9999)
			salida = "";
	}
	return salida;
}

