// JavaScript Document
function clearText(field) {
	if (field.defaultValue == field.value) field.value = '';
	else if (field.value == '') field.value = field.defaultValue;	
}
//*************************************************************************************************
//Fecha: 01/04/2010
//Autor: Adolfo Gurrieri
function redimensionar() {
	//Obtengo la altura del contenido del iframe
	var altura = document.getElementById('contenedor').clientHeight  - 250;

	//Cambio la altura del iframe
    if(document.getElementById('entrada') != null) {
		document.getElementById('entrada').height = altura;
		document.getElementById('entrada').style.height = altura + 'px';
	}
}
//*************************************************************************************************
//Fecha: 01/04/2010
//Autor: Adolfo Gurrieri
//Descripcion: Quita espacios en blanco a la izquierda del texto
function LTrim(s){
  var s2    = "";
  var largo = s.length;

  for (var i = 0; (i<largo) && (s.substr(i,1)==" "); i++ )  ;
  for (         ;  i<largo                         ; i++ )  s2 = s2 + s.substr(i,1);
  return(s2);
}
//*************************************************************************************************
//Fecha: 01/04/2010
//Autor: Adolfo Gurrieri
//Descripcion: Quita espacios en blanco a la derecha del texto
function RTrim(s){
  var s2    = "";
  var largo = s.length;

  for (var i=largo-1; (i>=0) && (s.substr(i,1)==" "); i-- )  ;
  for (             ;  i>=0                         ; i-- )  s2 = s.substr(i,1) + s2;
  return(s2);
}
//*************************************************************************************************
//Fecha: 01/04/2010
//Autor: Adolfo Gurrieri
//Descripcion: Valida si un objeto es vacio
function IsValid_Empty( a_objeto ) {

  //"Combo"
  if (a_objeto.type == "select-one") {
    if (a_objeto.selectedIndex < 0)  return( true );
    return( ( RTrim( LTrim( a_objeto.options[a_objeto.selectedIndex].value ) ) == "" ) ? true : false );
  }

  //"Listbox"
  if (a_objeto.type == "select-multiple") {
    if (a_objeto.selectedIndex < 0)  return( true );
    return( ( RTrim( LTrim( a_objeto.options[a_objeto.selectedIndex].text ) ) == "" ) ? true : false );
  }

  //"Editbox"
  return( ( RTrim( LTrim( a_objeto.value ) ) == "" ) ? true : false )
}
//*************************************************************************************************
//Fecha: 01/04/2010
//Autor: Adolfo Gurrieri
// Desc: Valida E-mail
function IsValid_EMail( a_objeto ) {
  //Vacio
  if( IsValid_Empty( a_objeto ) )  return( false );

  //Strings de expresiones regulares: username y hostname
  var ExpresionRegular1_Str = "(@.*@)|(\\.\\.)|(@\\.)|(\\.@)|(^\\.)";
  var ExpresionRegular2_Str = "^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$";

  //Objetos expresiones regulares
  var ExpresionRegular1 = new RegExp( ExpresionRegular1_Str );
  var ExpresionRegular2 = new RegExp( ExpresionRegular2_Str );

  //Retorna
  if (a_objeto.type == "select-one") {
    return( !ExpresionRegular1.test( a_objeto.options[a_objeto.selectedIndex].text )  &&
             ExpresionRegular2.test( a_objeto.options[a_objeto.selectedIndex].text ) );
  }
  else if (a_objeto.type == "select-multiple") {
    return( !ExpresionRegular1.test( a_objeto.options[a_objeto.selectedIndex].text )  &&
             ExpresionRegular2.test( a_objeto.options[a_objeto.selectedIndex].text ) );
  }
  else
    return( !ExpresionRegular1.test( a_objeto.value )  &&
             ExpresionRegular2.test( a_objeto.value ) );
}
//*************************************************************************************************
//Fecha: 01/04/2010
//Autor: Adolfo Gurrieri
//Descripcion: Valida si un numero es entero
function IsValid_NumberInt( a_objeto ) {
  //Vacio
  if( IsValid_Empty( a_objeto ) )  return( false );

  //Strings de expresiones regulares
  var ExpresionRegular_Str = "^(([0-9]+)|([0-9]{1,3}(\\" + VAL_SEPARADOR_MILES + "[0-9][0-9][0-9])*))$"

  //Objetos expresiones regulares
  var ExpresionRegular = new RegExp( ExpresionRegular_Str );

  //Retorna
  if (a_objeto.type == "select-one") {
    return( ExpresionRegular.test( a_objeto.options[a_objeto.selectedIndex].value ) );
  }
  else if (a_objeto.type == "select-multiple") {
    return( ExpresionRegular.test( a_objeto.options[a_objeto.selectedIndex].value ) );
  }
  else
    return( ExpresionRegular.test( a_objeto.value ) );
}
//*************************************************************************************************
//Fecha: 01/04/2010
//Autor: Adolfo Gurrieri
//Descripcion: Valida si es una fecha valida
function IsValid_Date( a_objeto ) {
  //Vacio
  if( IsValid_Empty( a_objeto ) )  return( false );

  //Strings de expresiones regulares
  var meses                = "1|2|3|4|5|6|7|8|9|01|02|03|04|05|06|07|08|09|10|11|12";
  var dias                 = "1|2|3|4|5|6|7|8|9|01|02|03|04|05|06|07|08|09|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31";
  var ExpresionRegular_Str = "^("+dias+")\(\/|-)("+meses+")\(\/|-)[0-9][0-9][0-9][0-9]$"

  //Objetos expresiones regulares
  var ExpresionRegular = new RegExp( ExpresionRegular_Str );

  //Evaluar expresiones
  var resultado;
  var valorObjeto;
  var largoObjeto;

  if (a_objeto.type == "select-one") {
    resultado = ExpresionRegular.test( a_objeto.options[a_objeto.selectedIndex].text );
    valorObjeto = a_objeto.options[a_objeto.selectedIndex].text;
    largoObjeto = valorObjeto.length;
  }
  else if (a_objeto.type == "select-multiple") {
    resultado = ExpresionRegular.test( a_objeto.options[a_objeto.selectedIndex].text );
    valorObjeto =a_objeto.options[a_objeto.selectedIndex].text;
    largoObjeto = valorObjeto.length;
  }
  else {
    resultado = ExpresionRegular.test( a_objeto.value );
    valorObjeto = a_objeto.value;
    largoObjeto = valorObjeto.length;
  }

  if ( resultado ){
    var largo= largoObjeto;
    var dia  = valorObjeto.substr(0,2);
    var mes  = valorObjeto.substr(3,2);
    var anno = valorObjeto.substr(largo-4,4);
    if (mes == "02") {
      //Si es divisible por 4 y no por 100 a no ser que
      //          sea div por 400 -> 29 2000 si 1900 no
      var bisiesto = (((anno%4 == 0) & (anno%100 != 0)) | (anno%400 == 0))?true:false;
      resultado = (bisiesto)?(eval(dia+"<30")):(eval(dia+"<29"))
    }
    //Revisa los dias 31 del mes
    else if (dia=="31" && ( (eval(mes+"<8")) && (mes%2==0) || (eval(mes+">7")) && (mes%2==1)))
      resultado = false;
  }

  if (Number(anno)<1900)
    resultado = false

  //Retorna
  return( resultado );
}
//*************************************************************************************************
//Fecha: 01/04/2010
//Autor: Adolfo Gurrieri
// Desc:
function chequeaRut(obj_rut){
  var texto = obj_rut.value;
  var rut_limpio = "";
  var strMsgErr = "";

// Le quita los ptos, espacios  y raya (12.345.678-9 => 123456789)
  for ( i=0; i < texto.length ; i++ )
    if ( texto.charAt(i) != ' ' && texto.charAt(i) != '.' && texto.charAt(i) != '-' )
      rut_limpio = rut_limpio + texto.charAt(i);
  texto = rut_limpio;
  largo = texto.length;

// Descarta los ceros al inicio del rut (0010 => 10)
  for ( i=0; texto.charAt(i) == '0' ; i++ );
  texto = texto.substring(i, texto.length);
  largo = texto.length;

// Verifica el largo minimo (minimo 2 caracteres)
  if ( largo < 2 ){
    strMsgErr = "El rut debe tener por lo menos 2 caracteres.";
    obj_rut.focus();
    obj_rut.select();
    return strMsgErr;
  }

// Verifica caracteres validos [1234567890kK)
  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" ){
      strMsgErr = "El valor ingresado no corresponde a un R.U.T valido.";
      obj_rut.select();
      obj_rut.focus();
      return strMsgErr;
    }
  }

// Invierte el rut
  var invertido = "";
  for ( i=(largo-1),j=0; i>=0; i--,j++ )
    invertido = invertido + texto.charAt(i);

// Formatea el rut con separador de miles y el guion
  var dtexto = "";
  dtexto = invertido.charAt(0) + '-';
  cnt = 0;
  for ( i=1,j=2; i<largo; i++,j++ ){
    if ( cnt == 3 ){
      dtexto = dtexto + '.';
      j++;
      dtexto = dtexto + invertido.charAt(i);
      cnt = 1;
    }
    else{
      dtexto = dtexto + invertido.charAt(i);
      cnt++;
    }
  }

// Invierte el rut invertido ( o sea lo deja ok)
  invertido = "";
  for ( i=(dtexto.length-1),j=0; i>=0; i--,j++ )
    invertido = invertido + dtexto.charAt(i);

  obj_rut.value = invertido;

// Verifica el digito verificador
  if ( !chequeaDV(texto) ){
    strMsgErr = "El rut es incorrecto.";
    obj_rut.focus();
    obj_rut.select();
    return strMsgErr;
  }
  //si el rut es valido retorna un strMsgErr vacio
  return strMsgErr;
}
//*************************************************************************************************
//si rut esta ok retorna true, sino false
function chequeaDV( crut ){

  largo = crut.length;

  if ( largo > 2 )
    rut_sin_dv = crut.substring(0, largo - 1);
  else
    rut_sin_dv = crut.charAt(0);
  dv = crut.charAt(largo-1);

  if ( rut_sin_dv == null || dv == null )
    return false;

  var dvr = '0';

  suma = 0;
  mul  = 2;

  for (i= rut_sin_dv.length -1 ; i >= 0; i--){
    suma = suma + rut_sin_dv.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() ){
    //rut mal
    return false;
  }

  //rut ok
  return true;
}




