function procesar() {
    if (typeof(Page_ClientValidate) == 'function') {
      if (Page_ClientValidate()){
        document.getElementById("procesando").style.display = 'inline';
        document.getElementById("ctl00_contenido_bttProcesar").style.display = 'none';
        document.body.style.cursor="wait";
      }
    }
    else {
        document.getElementById("procesando").style.display = 'inline';
        document.getElementById("ctl00_contenido_bttProcesar").style.display = 'none';
        document.body.style.cursor="wait";
    }
}

function procesar2() {
    if (typeof(Page_ClientValidate) == 'function') {
      if (Page_ClientValidate()){
        document.getElementById("procesando").style.display = 'inline';
        document.getElementById("bttProcesar").style.display = 'none';
        document.body.style.cursor="wait";
      }
    }
    else {
        document.getElementById("procesando").style.display = 'inline';
        document.getElementById("bttProcesar").style.display = 'none';
        document.body.style.cursor="wait";
    }
}

var enterActivado = false; 

function activaEnter() { 
	enterActivado = true; 
} 

function desactivaEnter() { 
	enterActivado = false; 
} 

function PressTeclaEnter(){ 
	if(event.keyCode == 13 && !enterActivado) { 
	    event.keyCode = 9;
    }
}

function PressTeclaEnterBoton(obj){ 
	if(event.keyCode == 13 && !enterActivado) { 
	    event.cancel = true;
	    obj.click();
    }
}

function uwOnKeyDown(oChooser, oEvent, obj){
	if(oEvent.event.keyCode == 13) { 
       oEvent.event.keyCode = 9;
    }
}

function keyDownInEdit(oEdit, keyCode, oEvent) {
	if(oEvent.event.keyCode == 13) { 
       oEvent.event.keyCode = 9;
    }
}


function help(idObjeto) {
  popupWin(800,600,'/ERP/Help/'+idObjeto+'.aspx','Ayuda',0,0,'No','No','No','No','Yes','Yes');
}

function popupWin(iWidth,iHeight,pUrl,sNombre,iTop,iLeft, sStatus, sToolbar, sMenubar, sLocation, sScrollbars, sResizable){
    return	window.open(pUrl,sNombre,"height="+iHeight+",width="+iWidth+",status="+sStatus+",toolbar="+sToolbar+",menubar="+sMenubar+",location="+sLocation+", scrollbars ="+sScrollbars+", resizable ="+sResizable);
}

function popupModalWin(iWidth,iHeight,pUrl,sNombre,sScroll, sStatus){
    return	window.showModalDialog(pUrl, sNombre,"dialogHeight: "+iWidth+"px; dialogWidth: "+iHeight+"px; edge: Raised; center: yes; help: No; resizable: No; status:"+sStatus+"; scroll:"+sScroll+";");
}

//function valida_nombre_benef(source, arguments) {
//    if (document.getElementById("ctl00_contenido_chkUnPago").checked) {
//      //Está Marcado chkUnPago
//    if (document.getElementById("ctl00_contenido_txtBenef").value.length == 0) {
//      //Largo Nombre Beneficiaro = 0 => ERROR
//       arguments.IsValid=false;
//    }
//    else {
//       arguments.IsValid=true;
//    }	    
//    }
//    else {
//      //No está Marcado chkUnPago
//    arguments.IsValid=true;
//    }
//}

function check_rut(source, arguments) {
	var POND = "32765432"
	var DV_AUX = ""
	var NUM_AUX = ""
	var LPOND = 0
	var SUM = 0
	var RESTO = 0
	var DI = ""

    var rut = format_rut(arguments.Value);

	if (rut.length == 0) { 
		arguments.IsValid=false;
	}
	else {	
    DV_AUX = rut.charAt(rut.length-1);
    if (DV_AUX.toUpperCase() == "E") {
		  arguments.IsValid=true;
    }
    else {
      // Elimina espacios, puntos y guiones
	    NUM_AUX = rut.substring(0,rut.length-2);
	    var tmpstr = "";
	    for ( i=0; i < NUM_AUX.length ; i++ )
		    if ( NUM_AUX.charAt(i) != ' ' && NUM_AUX.charAt(i) != '.' && NUM_AUX.charAt(i) != '-' )
			    tmpstr = tmpstr + NUM_AUX.charAt(i);
	    NUM_AUX = tmpstr;
    	
      LPOND = 8;
      SUM = 0;
	    while (LPOND > 0)
		    {
	        SUM = SUM + (parseInt(NUM_AUX.substring(LPOND,LPOND-1)) * parseInt(POND.substring(LPOND, LPOND-1)));
	        LPOND = LPOND - 1;
		    }
    	
	    RESTO = SUM - (Math.floor(SUM / 11) * 11);
      
      if (RESTO == 0)
        DI = "0";
      else
        if (RESTO == 1)
          DI = "K";
        else
          DI = 11 - RESTO;

      if (DI == DV_AUX) {
		     arguments.IsValid=true;
      } else {
         arguments.IsValid=false;
      }
    }
  }
}

function format_number(num) {
	return num.toLocaleString();
}

function its_a_digit(character) {

    var digit_characters = "0123456789";

    // Si no está en la cadena digit_characters,
    // entonces no es un dígito y devuelve false

    if (digit_characters.indexOf(character) == -1) {
        return false;
    }
    
    // En caso contrario, es un número y devuelve true
    return true;
}

function solo_numeros(campo) {
	var campoECT;
	campoECT = campo;

	var berror = false;
	for (countertipodato=0 ; countertipodato < campoECT.length; countertipodato++) {
		if (countertipodato == 0) {
			// Primer elemento
			if (!its_a_digit(campoECT.charAt(0))) {
				berror=true;
			}
		}
		else {
			if (!(its_a_digit(campoECT.charAt(countertipodato)))) {
				berror=true;
			}
		}
	}
	if (berror) {
		return false;
	}
	return true;
}

function format_rut(objRut) {
	rut = objRut
  // Elimina espacios, puntos y guiones
	var tmpstr = "";
	for ( i=0; i < rut.length ; i++ )
		if ( rut.charAt(i) != ' ' && rut.charAt(i) != '.' && rut.charAt(i) != '-' )
			tmpstr = tmpstr + rut.charAt(i);
	rut = tmpstr;
	largo = rut.length;

	if (largo == 0) {
		return;
	}
	
	// Completa los espacios con 0
	for ( i=0; largo < 9; i++)
		{
			rut='0'+rut;
			largo = rut.length;
		}
	
	// Coloca los puntos	
	var invertido = "";
	for ( i=(largo-1),j=0; i>=0; i--,j++ )
		invertido = invertido + rut.charAt(i);
	var drut = "";
	drut = drut + invertido.charAt(0);
	drut = drut + '-';
	cnt = 0;
	for ( i=1,j=2; i<largo; i++,j++ )
	{
		if ( cnt == 3 )
		{
			drut = drut + '.';
			j++;
			drut = drut + invertido.charAt(i);
			cnt = 1;
		}
		else
		{
			drut = drut + invertido.charAt(i);
			cnt++;
		}
	}
	invertido = "";
	for ( i=(drut.length-1),j=0; i>=0; i--,j++ )
		invertido = invertido + drut.charAt(i);

	return invertido.toUpperCase();
		
}

function trim_spaces(from_where) {
	var temp_string = this
	if (arguments.length == 0) {
		from_where = "BOTH"
	}
	
	if (from_where.toUpperCase() == "LEFT" || from_where == "BOTH") {
		while (temp_string.left(1) == " ") {
			temp_string = temp_string.substrin(1)
		}
	}
	
	if (from_where.toUpperCase() == "RIGHT" || from_where == "BOTH") {
		while (temp_string.right(1) == " ") {
			temp_string = temp_string.substring(0,temp_string.length - 2)
		}
	}
	return temp_string
}

 function reemplazar(texto,textoBuscado,nuevaCadena) {
  
  while (texto.indexOf(textoBuscado)>-1) {
   pos= texto.indexOf(textoBuscado);
   texto = "" + (texto.substring(0, pos) + nuevaCadena 
     + texto.substring((pos + textoBuscado.length), texto.length));
  }

  return texto;
 }

//calcular la edad de una persona 
//recibe la fecha como un string en formato español 
//devuelve un entero con la edad. Devuelve false en caso de que la fecha sea incorrecta o mayor que el dia actual 
function calcular_edad(fecha){ 

    //calculo la fecha de hoy 
    hoy=new Date() 
    //alert(hoy) 

    //calculo la fecha que recibo 
    //La descompongo en un array 
    var array_fecha = fecha.split("-") 
    //si el array no tiene tres partes, la fecha es incorrecta 
    if (array_fecha.length!=3) 
       return false 

    //compruebo que los ano, mes, dia son correctos 
    var ano 
    ano = parseInt(array_fecha[2]); 
    if (isNaN(ano)) 
       return false 

    var mes 
    mes = parseInt(array_fecha[1]); 
    if (isNaN(mes)) 
       return false 

    var dia 
    dia = parseInt(array_fecha[0]); 
    if (isNaN(dia)) 
       return false 


    //si el año de la fecha que recibo solo tiene 2 cifras hay que cambiarlo a 4 
    if (ano<=99) 
       ano +=1900 

    //resto los años de las dos fechas 
    edad=hoy.getYear()- ano - 1; //-1 porque no se si ha cumplido años ya este año 

    //si resto los meses y me da menor que 0 entonces no ha cumplido años. Si da mayor si ha cumplido 
    if (hoy.getMonth() + 1 - mes < 0) //+ 1 porque los meses empiezan en 0 
       return edad 
    if (hoy.getMonth() + 1 - mes > 0) 
       return edad+1 

    //entonces es que eran iguales. miro los dias 
    //si resto los dias y me da menor que 0 entonces no ha cumplido años. Si da mayor o igual si ha cumplido 
    if (hoy.getUTCDate() - dia >= 0) 
       return edad + 1 

    return edad 
} 
