var defaultEmptyOK = false
var checkNiceness = true;
var digits = "0123456789";
var lowercaseLetters = "abcdefghijklmnopqrstuvwxyzáéíóúñü"
var uppercaseLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZÁÉÍÓÚÑ"
var whitespace = " \t\n\r";
var phoneChars = "()-+ ";
if (idioma == "en"){
	var mMessage = "Error : This field cannot be left void"
	var mSeleccion = "Error: This option cannot be left with no choie made"
	var pPrompt = "Error: ";
	var pAlphanumeric = "Enter a text containing only letters and/or numbers.";
	var pAlphabetic   = "Enter a text containing only letter characters.";
	var pInteger = "Enter a whole number.";
	var pNumber = "Enter a number.";
	var pPhoneNumber = "Enter a valid telephone number.";
	var pEmail = "Enter a valid electronic mail address.";
	var pName = "Enter a text containing only letter characters, numbers or spaces.";
	var pNice = "Commas cannot be used here.";
	var pRut = "Chilean Tax ID. number not valid. Please check.";
	var pFecha = "Error: The date entered is incorrect. (Format: dd/mm/yyyy)";
	var pEnteroPositivo = "Enter a number greater than zero."
	//var pEnteroPositivoCero = "Ingrese un entero mayor o igual a cero."
	//isAlphabetic(1),isAlphanumeric (2),isInteger(3),isNumber(4),isEmail(5),isPhoneNumber(6),isName(7)
	//var pHora = "La hora ingresada no es válida.\n(Formato: hh:mm)";
}
else{
	var mMessage = "Error: No puede dejar este campo vacio."
	var mSeleccion = "Error: No puede dejar esta opción sin seleccionar."
	var pPrompt = "Error: ";
	var pAlphanumeric = "Ingrese un texto que contenga solo letras y/o números.";
	var pAlphabetic   = "Ingrese un texto que contenga solo letras.";
	var pInteger = "Ingrese un número entero.";
	var pNumber = "Ingrese un número.";
	var pPhoneNumber = "Ingrese un número de teléfono válido.";
	var pEmail = "Ingrese una dirección de correo electrónico válida.";
	var pName = "Ingrese un texto que contenga sólo letras, números o espacios.";
	var pNice = "No puede utilizar comillas aquí.";
	var pRut = "RUT no válido.Por favor verifique.";
	var pFecha = "Error: La fecha ingresada no es correcta.\n(Formato: dd/mm/aaaa)";
	var pEnteroPositivo = "Ingrese un entero mayor que cero."
	var pEnteroPositivoCero = "Ingrese un entero mayor o igual a cero."
	//isAlphabetic(1),isAlphanumeric (2),isInteger(3),isNumber(4),isEmail(5),isPhoneNumber(6),isName(7)
	var pHora = "La hora ingresada no es válida.\n(Formato: hh:mm)";
}



var v_nota_minima = 1;

var v_nota_maxima = 7;

var pNota = "La nota ingresada no es válida:\nDebe estar entre " + v_nota_minima + " y " + v_nota_maxima + ", y utilizar un (.) como separador decimal.";



function anyoBisiesto(anyo){

	if (anyo < 100){var fin = anyo + 1900;}	else {var fin = anyo ;}

	if (fin % 4 != 0) { return false;}

	else {

		if (fin % 100 == 0)	{

			if (fin % 400 == 0)	{return true;} else	{return false;}

		}

		else{ return true; }

	}

}



function isRutDigit (c)

{

	return ((c >= "0") && (c <= "9") || (c=="k") || (c=="K") )

}





function isRut (s)

{

    if (isEmpty(s)) 

       if (isRut.arguments.length == 1) return defaultEmptyOK;

       else return (isRut.arguments[1] == true);

    if (isWhitespace(s)) return false;

    var i = 1;

    var sLength = s.length;



    while ((i < sLength) && (s.charAt(i) != "-") && (isDigit(s.charAt(i))) )

    { i++

    }

    

    

    if ( (s.charAt(i) == "-") && ( (i+1)==(sLength-1) ) && (isRutDigit(s.charAt(i+1)) ) && valida_rut(s) )return true;

    else return false;

}

/*

                nombre =formulario.elements[i].name;

				var elem = new RegExp("rut","gi");

				if (elem.test(nombre)){

				    if ((isEmpty(formulario.elements[i].value)) && (formulario.elements[i].type!="HIDDEN") ) {

					   window.open("../mantenedores/buscador_personas.asp","v2","scrollbars=yes toolbar=no menubar=no width=600 height=600") 

					   return false;}

				 }

*/

function valida_rut(rut)

{

   /*var IgStringVerificador, IgN, IgSuma, IgDigito, IgDigitoVerificador, rut;

   for( i=10-rut.length; i>0; i-- ) rut = '0' + rut; 

   IgStringVerificador = '32765432';

   IgSuma = 0;

   for( IgN = 0; IgN < 8 && IgN < rut.length; IgN++)

      IgSuma = eval(IgSuma + '+' + rut.substr(IgN, 1) + '*' + IgStringVerificador.substr(IgN, 1) + ';');

   IgDigito = 11 - IgSuma % 11;

   IgDigitoVerificador = IgDigito==10?'K':IgDigito==11?'0':IgDigito;*/

   var suma=0;

		var digito=0;

		var j=1;

		var i;

		suma = parseInt(suma);

		digito = parseInt(digito);

		var campo = rut

		var carnet = parseInt(parseFloat(campo));

		//alert (carnet)

			for(i=campo.length;i>=0;i--)

			{

				j++;

				if(j>7) j=2;

				suma = suma + (carnet%10)*j;

				carnet = parseInt(carnet / 10);

			}

			digito = parseInt(11 - (suma%11));

			digito = digito==10?'K':digito==11?'0':digito;

   if (rut.substr(rut.length-1) == digito) {return true;}

   else {return false;}

}



function makeArray(n) {

   for (var i = 1; i <= n; i++) {

      this[i] = 0

   } 

   return this

}



function isEmpty(s)

{   return ((s == null) || (s.length == 0))

}



function isWhitespace (s)

{   var i;

    if (isEmpty(s)) return true;

    for (i = 0; i < s.length; i++)

    {   

        var c = s.charAt(i);

        if (whitespace.indexOf(c) == -1) return false;

    }

    return true;

}

function isFecha(fecha) {

 var str, mes, dia, anyo, febrero;  

 expr = /^[0-9]{2}\/[0-9]{2}\/[0-9]{4}$/;

 str = fecha;

 if((m = str.match(expr))==null) {

	esFecha1 = false;

 }

 else{	

     dia  = fecha.split("/")[0];

     mes  = fecha.split("/")[1];

     anyo = fecha.split("/")[2];   				

	 

     if(anyoBisiesto(anyo)) {febrero=29;} else { febrero=28;}

	 if ((mes<1) || (mes>12)){

		   esFecha1 = false;

	 }

	 else{

	    if ((mes==2) && ((dia<1) || (dia>febrero))){

			   esFecha1 = false;

		}

		else {

		   if (((mes==1) || (mes==3) || (mes==5) || (mes==7) || (mes==8) || (mes==10) || (mes==12)) && ((dia<1) || (dia>31))){

		 	  esFecha1 = false;

		   }

		   else{

			   if (((mes==4) || (mes==6) || (mes==9) || (mes==11)) && ((dia<1) || (dia>30))){

				   esFecha1 = false;

			   }

			   else{

				  esFecha1 = true;

			   }	  

		   }

		}

	  }

   }

   return ( esFecha1 );

}







function isHora(p_hora)

{

	//var p_formato_hora = /^[0-9]{2}:[0-9]{2}(:[0-9]{2})*$/;   //Con segundos

	var p_formato_hora = /^[0-9]{2}:[0-9]{2}$/;  //Sin segundos

	

	if (p_hora.search(p_formato_hora) < 0)

		return false;		

	

	var arr_hora = p_hora.split(/:/);

	var v_horas = parseInt(arr_hora[0]);

	var v_minutos = parseInt(arr_hora[1]);

					

	if (v_horas < 0)

		return false;

		

	if (v_horas > 23)

		return false;

		

	if (v_minutos < 0)

		return false;

		

	if (v_minutos > 59)

		return false;		

	

	/*if (arr_hora.length = 3) {

		var v_segundos = parseInt(arr_hora[2]);

		

		if (v_segundos < 0)

			return false;

			

		if (v_segundos > 59)

			return false;		

	}*/

			

	return true;

}





function isNota(p_nota)

{

	if (!isNumber(p_nota))

		return false;

		

	if (parseFloat(p_nota) < parseFloat(v_nota_minima))

		return false;

		

	if (parseFloat(p_nota) > parseFloat(v_nota_maxima))

		return false;

	

	return true;	

}







function stripCharsInBag (s, bag)

{   var i;

    var returnString = "";

 

    for (i = 0; i < s.length; i++)

    {   var c = s.charAt(i);

        if (bag.indexOf(c) == -1) returnString += c;

    }



    return returnString;

}





function stripCharsNotInBag (s, bag)

{   var i;

    var returnString = "";

    for (i = 0; i < s.length; i++)

    {   

        var c = s.charAt(i);

        if (bag.indexOf(c) != -1) returnString += c;

    }



    return returnString;

}





function stripWhitespace (s)

{   return stripCharsInBag (s, whitespace)

}



function charInString (c, s)

{   for (i = 0; i < s.length; i++)

    {   if (s.charAt(i) == c) return true;

    }

    return false

}



function stripInitialWhitespace (s)

{   var i = 0;

    while ((i < s.length) && charInString (s.charAt(i), whitespace))

       i++;

    return s.substring (i, s.length);

}



function isLetter (c)

{

    return( ( uppercaseLetters.indexOf( c ) != -1 ) ||

            ( lowercaseLetters.indexOf( c ) != -1 ) )

}



function isDigit (c)

{   return ((c >= "0") && (c <= "9"))

}



function isLetterOrDigit (c)

{   return (isLetter(c) || isDigit(c))

}



function isInteger (s)

{   var i;

    if (isEmpty(s)) 

       if (isInteger.arguments.length == 1) return defaultEmptyOK;

       else return (isInteger.arguments[1] == true);

    

    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 isEnteroPositivo(s)

{

	if (!isInteger(s)) {

		return false;

	}

	

	if (s <= 0) {

		return false;

	}

	

	return true;

}



function isEnteroPositivoCero(s)

{

	if (!isInteger(s)) {

		return false;

	}

	

	if (s < 0) {

		return false;

	}

	

	return true;

}





function isNumber (s)

{   var i;

    var dotAppeared;

    dotAppeared = false;

    if (isEmpty(s)) 

       if (isNumber.arguments.length == 1) return defaultEmptyOK;

       else return (isNumber.arguments[1] == true);

    

    for (i = 0; i < s.length; i++)

    {   

        var c = s.charAt(i);

        if( i != 0 ) {

            if ( c == "." ) {

                if( !dotAppeared )

                    dotAppeared = true;

                else

                    return false;

            } else     

                if (!isDigit(c)) return false;

        } else { 

            if ( c == "." ) {

                if( !dotAppeared )

                    dotAppeared = true;

                else

                    return false;

            } else     

                if (!isDigit(c) && (c != "-") || (c == "+")) return false;

        }

    }

    return true;

}



function isAlphabetic (s)

{   var i;



    if (isEmpty(s)) 

       if (isAlphabetic.arguments.length == 1) return defaultEmptyOK;

       else return (isAlphabetic.arguments[1] == true);

    for (i = 0; i < s.length; i++)

    {   

        var c = s.charAt(i);



        if (!isLetter(c))

        return false;

    }

    return true;

}



function isAlphanumeric (s)

{   var i;



    if (isEmpty(s)) 

       if (isAlphanumeric.arguments.length == 1) return defaultEmptyOK;

       else return (isAlphanumeric.arguments[1] == true);



    for (i = 0; i < s.length; i++)

    {   

        var c = s.charAt(i);

        if (! (isLetter(c) || isDigit(c) ) )

        return false;

    }



    return true;

}





function isName (s)

{

    if (isEmpty(s)) 

       if (isName.arguments.length == 1) return defaultEmptyOK;

       else return (isAlphanumeric.arguments[1] == true);

    

    return( isAlphanumeric( stripCharsInBag( s, whitespace ) ) );

}



function isPhoneNumber (s)

{   var modString;

    if (isEmpty(s)) 

       if (isPhoneNumber.arguments.length == 1) return defaultEmptyOK;

       else return (isPhoneNumber.arguments[1] == true);

    modString = stripCharsInBag( s, phoneChars );

    return (isInteger(modString))

}



function isEmail (s)

{

    if (isEmpty(s)) 

       if (isEmail.arguments.length == 1) return defaultEmptyOK;

       else return (isEmail.arguments[1] == true);

    if (isWhitespace(s)) return false;

    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 isNice(s)

{

        var i = 0;

        var sLength = s.length;

        var b = 1;

        while(i<sLength) {

                if( (s.charAt(i) == "\"") || (s.charAt(i) == "'" ) ) b = 0;

                i++;

        }

        return b;

}



function statBar (s)

{   window.status = s

}



function warnEmpty (theField)

{   theField.focus()

    msg=mMessage

    if(theField.type=="select-one") {msg=mSeleccion;}

    alert(msg);

    statBar(msg)

    return false

}



function warnInvalid (theField, s)

{	

	theField.focus()

    theField.select()

    alert(s)

    statBar(pPrompt + s)

    return false

}



function isAll (s)

{   return true

}



function checkField (theField, theFunction, emptyOK, s)

{   

    var msg;

	theFunction=theFunction.toUpperCase();

	emptyOK=emptyOK.toUpperCase();

    if (checkField.arguments.length < 3) emptyOK = defaultEmptyOK;

    if (checkField.arguments.length == 4) {

        msg = s;

    } else {

        if( theFunction == "LE" ) msg = pAlphabetic;

        if( theFunction == "LN" ) msg = pAlphanumeric;

        if( theFunction == "IN" ) msg = pInteger;

        if( theFunction == "NU" ) msg = pNumber;

        if( theFunction == "EM" ) msg = pEmail;

        if( theFunction == "TE" ) msg = pPhoneNumber;

        if( theFunction == "NO" ) msg = pName;

		if( theFunction == "RU" ) msg = pRut;

		if( theFunction == "FE" ) msg = pFecha;

		if( theFunction == "EP" ) msg = pEnteroPositivo;

		if( theFunction == "E0" ) msg = pEnteroPositivoCero;

		if( theFunction == "HO" ) msg = pHora;

		if( theFunction == "NT" ) msg = pNota;

    }

	

	if( theFunction == "TO" ) theFunction = isAll;

	if( theFunction == "LE" ) theFunction = isAlphabetic;

	if( theFunction == "LN" ) theFunction = isAlphanumeric;

	if( theFunction == "IN" ) theFunction = isInteger;

	if( theFunction == "NU" ) theFunction = isNumber;

	if( theFunction == "EM" ) theFunction = isEmail;

	if( theFunction == "TE" ) theFunction = isPhoneNumber;

	if( theFunction == "NO" ) theFunction = isName;

	if( theFunction == "RU" ) theFunction = isRut;

	if( theFunction == "FE" ) theFunction = isFecha;	

	if( theFunction == "EP" ) theFunction = isEnteroPositivo;	

	if( theFunction == "E0" ) theFunction = isEnteroPositivoCero;	

	if( theFunction == "HO" ) theFunction = isHora;	

	if( theFunction == "NT" ) theFunction = isNota;	

	

	if( emptyOK == "S" ) {emptyOK = true;}else{emptyOK = false;}

    

    if ((emptyOK == true) && (isEmpty(theField.value))) return true;



    if ((emptyOK == false) && (isEmpty(theField.value))) 

        return warnEmpty(theField);



    /*if ( checkNiceness && !isNice(theField.value))

        return warnInvalid(theField, pNice);*/



    if (theFunction(theField.value) == true) 

        return true;

    else

        return warnInvalid(theField,msg);

}







function checkRut(p_campo_rut, p_campo_dv, theFunction, emptyOK, s)

{

	var msg;

	var valor;

	

	valor = p_campo_rut.value + "-" + p_campo_dv.value;

	if (valor=="-") valor = "";

	

	theFunction=theFunction.toUpperCase();

	emptyOK=emptyOK.toUpperCase();

    if (checkRut.arguments.length < 4) emptyOK = defaultEmptyOK;

    if (checkRut.arguments.length == 5) {

        msg = s;

    } else {

		msg = pRut;		

    }



	theFunction = isRut;

		

	if( emptyOK == "S" ) {emptyOK = true;}else{emptyOK = false;}

		   

    if ((emptyOK == true) && (isEmpty(valor))) return true;

	

	if ((emptyOK == false) && (isEmpty(valor))) 

        return warnEmpty(p_campo_rut);



    if (theFunction(valor) == true) 

        return true;

    else{

		return warnInvalid(p_campo_rut,msg);

	}

        

}











function preValidaFormulario(formulario)

	{

	var num_elementos = formulario.length;

	

	for ( var i = 0;i < num_elementos; i++) {

		s = formulario.elements[i].id;

		

		if (formulario.elements[i].type!="button" && formulario.elements[i].type!="hidden" && formulario.elements[i].disabled==false) {			

			if (s != '') { // Para que no se caiga al ir un ID de un elemento del formulario vacío.

				if (!checkField( formulario.elements[i], s.charAt(0)+s.charAt(1), s.charAt(3) )) {

					return false;

				}

			}

	    }

		

		var campos = formulario.elements[i].value.split(/ /);

		if ( (formulario.elements[i].type == 'hidden') && ( (s.charAt(0) + s.charAt(1)) == 'RU') && (campos.length == 2) && (!formulario.elements[i].disabled)) {

			if (!checkRut(formulario.elements[campos[0]], formulario.elements[campos[1]], s.charAt(0) + s.charAt(1), s.charAt(3))) {

				return false;

			}

		}

	}

	

	return true;

}





function CA(d){

 for (var i=0;i<d.elements.length;i++){

    var e = d.elements[i];

    if ((e.name != 'check_all') && (e.type=='checkbox')){

       e.checked = d.check_all.checked;

       if (d.check_all.checked){

          hL(e);

       }

       else {

          dL(e);

       }

    }

  }

}



function CCA(CB){

   if (CB.checked)

      hL(CB);

   else

      dL(CB);

}

function hL(E){

 while (E.tagName!="TR"){

      E=E.parentElement;

   }

 E.className = "H";

}

function dL(E){

 while (E.tagName!="TR"){

    E=E.parentElement;

 }

E.className = "";

}

function comilla(palabra){	

  largo = palabra.length;		

  for (i=0; i < largo ; i++ )

  { 

	 var caracter = palabra.charAt(i)	

	 var comilla_simple = caracter.search( /[']/i );

	 if( comilla_simple != -1 ){ 

   		 return true;

	 }

  }

  return false;

}



function roundFun(number,noOfPlaces){

    val = (Math.round(number*Math.pow(10,noOfPlaces)))/Math.pow(10,noOfPlaces)

    val = val.toString()

    if(val.indexOf(".")==-1)

         val = val.toString()// +      ".00"

    return(val)

}





function Dv(p_rut)

{

	if (!isNumber(p_rut)) {

		return "";

	}

	

   var IgStringVerificador, IgN, IgSuma, IgDigito, IgDigitoVerificador, rut;   

   rut = p_rut.toString() + "  ";     

   for( i=10-rut.length; i>0; i-- ) rut = '0' + rut; 

   IgStringVerificador = '32765432';

   IgSuma = 0;

   for( IgN = 0; IgN < 8 && IgN < rut.length; IgN++)

      IgSuma = eval(IgSuma + '+' + rut.substr(IgN, 1) + '*' + IgStringVerificador.substr(IgN, 1) + ';');

   IgDigito = 11 - IgSuma % 11;

   IgDigitoVerificador = IgDigito==10?'K':IgDigito==11?'0':IgDigito;

	

   return IgDigitoVerificador;

}





function buscar_persona(p_campo_rut, p_campo_dv)

{

var v2;

v2=window.open("../mantenedores/buscador_personas.asp?campo_rut=" + p_campo_rut + "&campo_dv=" + p_campo_dv,"v2","scrollbars=yes toolbar=no menubar=no width=600 height=600");

}