// ImitTools.js


//***************************************
// Check Rut field leaving the box STEP 1
//***************************************

function checkRutField(texto)
{
  var tmpstr = "";
  var retorno = "";
  for ( i=0; i < texto.length ; i++ )
    if ( texto.charAt(i) != ' ' && texto.charAt(i) != '.' && texto.charAt(i) != '-' )
      tmpstr = tmpstr + texto.charAt(i);
      
  texto = tmpstr;
  largo = texto.length;


  if ( largo < 7 )
  {
    retorno="Debe ingresar un rut valido";
    //eval('window.document.' + form + '.' + textName + '.value="";');
    //eval('window.document.' + form + '.' + textName + '.focus();');
    return retorno;
  }

  var invertido = "";

  for ( i=(largo-1),j=0; i>=0; i--,j++ )
    invertido = invertido + texto.charAt(i);


  var dtexto = "";

  dtexto = dtexto + invertido.charAt(0);
  dtexto = dtexto + '-';
  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++;
    }
  }

  invertido = "";

  for ( i=(dtexto.length-1),j=0; i>=0; i--,j++ )
    invertido = invertido + dtexto.charAt(i);

  //eval('window.document.' + form + '.' + textName + '.value = invertido;');
  retorno = 'OK_' + invertido;
  
  if ( checkDV(texto) == "OK" )
    return retorno; //true;
    
  // El rut es incorrecto
  return "Debe ingresar un rut valido"; //false;
}

//***************************************
// Check Rut field leaving the box STEP 2
//***************************************

function checkDV( crut )

{
  var  retorno = "";
  largo = crut.length;
  
  if ( largo >= 6 )
    rut = crut.substring(0, largo - 1);
  else
    rut = crut.charAt(0);
  dv = crut.charAt(largo-1);
  

 if ( rut == null || dv == null )
      return 0;

  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 rut es invalido"); // El rut es invalido
    retorno = "EL rut es invalido";
    //eval('window.document.' + form + '.' + textName + '.value="";');
    //eval('window.document.' + form + '.' + textName + '.focus();');
    return "retorno"; //false;
  }

      return "OK"; //true;
}

//***************************************
// Retorna el Rut sin puntos ni guiones y sin DV
//***************************************
function RutFieldSF(texto)
{
  var tmpstr = "";
  var retorno = "";
  for ( i=0; i < texto.length - 1 ; i++ )
    if ( texto.charAt(i) != ' ' && texto.charAt(i) != '.' && texto.charAt(i) != '-' )
      tmpstr = tmpstr + texto.charAt(i);
      
  texto = tmpstr;
  return tmpstr;
}

function ltrim(s) {
   return s.replace(/^\s+/, "");
}

function rtrim(s) {
   return s.replace(/\s+$/, "");
}

function trim(s) {
   return rtrim(ltrim(s));
}

function MostrarFilas(Fila) {
   // Almacenamos en "elementos" el objeto "Fila" recibido como parámetro
   var elementos = document.getElementsByName(Fila);

   // Mostramos todos los objetos del mismo identificador
   // Con propiedad display = "" dejamos al navegador que ponga su tipo por defecto
   for (i = 0; i < elementos.length; i++)
   {
      elementos[i].style.display ="";
      elementos[i].style.visibility = "visible";
   }
}

function OcultarFilas(Fila) {
   // Almacenamos en "elementos" el objeto "Fila" recibido como parámetro
   var elementos = document.getElementsByName(Fila);

   // Ocultamos todos los objetos del mismo identificador
   for (k = 0; k < elementos.length; k++)
   {
      elementos[k].style.display = "none";
      elementos[k].style.visibility = "hidden";
   }
}

////Archivos necesarios en la pagina que llama a esta función
///jquery.floatbox.1.0.6.min.js
///jquery-1.3.2.min.js
function openLoading(Mensaje)
{
//    $.floatbox({
//		content: "<p id='mensaje_cargando' style='text-align:center;'>" + mensaje0 + "<br /><img src='../Imagen/new/ajax-loader_mini.gif' /></p>",
//		fade: false,
//		button: "",
//		bgConfig : {
//        }
//	});
    $.floatbox({
		content: "<br /><br /><p id='mensaje_cargando' style='text-align:center;'>" + mensaje0 + "<br /><br /></p>",
		fade: false,
		button: "",
		bgConfig : {
        }
	});
}
function closeLoading()
{
    $.closebox({
	});
}

// MORE INFO

function Show_MoreInfo(div_id, div_idPadre) //String

{
    div_obj = document.getElementById(div_id);
    div_idPadre = document.getElementById(div_idPadre);
  
    if (div_obj.style.display == 'none')
    {
        div_obj.style.display = 'block';
        div_idPadre.children(0).innerText = div_idPadre.children(0).innerText.replace('+','-'); 
    }
    else
    {
        div_obj.style.display = 'none';
        div_idPadre.children(0).innerText = div_idPadre.children(0).innerText.replace('-','+'); 
    }
}
// FIN 


function ValidaNumero(Objeto, MinVal, Glosa)
{
	var Texto = Objeto.value;
	
	if (Texto.length == 0)
    {	
		alert('Debe Ingresar el ' + Glosa);
        return false;
    }
	
	if (!isNaN(Texto))
	{
		if (Texto > MinVal)
		{
			return true;
		}
		else
		{
			alert(Glosa + ' debe ser mayor que ' + MinVal);
			return false;
		}	
	}
	else
	{
		alert('El ' + Glosa + ' debe ser un numero');
		return false;
	}	
					
}


function solomayusculas()
{
	var tecla = window.event.keyCode;
	//alert(tecla);
	if (tecla > 96 && tecla < 123)
	{
		window.event.keyCode=tecla - 32;
	}
}

function solominusculas()
{
	var tecla = window.event.keyCode;
	//alert(tecla);
	if (tecla > 64 && tecla < 91)
	{
		window.event.keyCode=tecla + 32;
	}
}