// JavaScript Document

function Tipo(txt, nombre, tipo)
{
    if(busca_espacios(txt) == false || txt != 0)
    {
        if(SoloTipo(txt,tipo) == false)
        {
            alert("El formato de "+nombre+" es incorrecto");
            return false;
        }
    }
return true;
}

//verifica que el campo no sea vacio
function vacioAlert( txt , nombre)
{
    if(busca_espacios(txt) == true || txt == 0)
    {
        alert("Debes ingresar un valor para " + nombre);
        return false;
    }
return true;
}

function vacioConfirm( txt , nombre)
{
    if(busca_espacios(txt)==true || txt == 0)
    {
        if(confirm("No has ingresado un valor para " +nombre+".\nSeguro que quieres continuar?"))
            return true;
        else
            return false;
    }
return true;
}



function busca_espacios(str)
{
    var espacios=0;
    var aux = " "
    for(var i=0;i<str.length;i++)
    {
        if(str.substring(i,i+1) == aux)
        {
            espacios++;
        }
    }

    if(espacios==str.length)
    {
        return true;
    }
    else
    {
        return false;
    }
}

function SoloTipo(InString, tipo)
{
    if(tipo == 'RUT'){
                if(!checkRut(InString))
                return false;
    }


    if (tipo == 'EMAIL'){
                if(!validarEmail(InString))
                return false;
    }

    if (tipo == 'POR'){
                if(InString > 100 || InString < 0)
                return false;
    }

    if (tipo == 'EX'){
            val1=InString.substring(InString.length-4,InString.length);
			val = val1.toLowerCase();
            if (val != '.jpg'&& val!='.gif' && val!='.bmp' && val != '.png' && val != '.tif'){
                return false;
            }
    }



        for (Cont=0; Cont < InString.length; Cont++) {
            Char = InString.charAt(Cont);

            if (tipo == 'T') {
                if(!esTelefono(Char))
                    return false;
            }


        if (tipo == 'N') {
                if(!esNumero(Char))
                    return false;
            }


        if (tipo == 'D') {
                if(!esDecimal(Char))
                    return false;
            }


        }


    return true;
 }

function esTelefono(Char)
{
        if(Char.length!=1)
            return false;
        Char=Char.toLowerCase();
        RefString="123456789 0()-";
        if (RefString.indexOf(Char.toLowerCase(), 0) == -1)
            return false;
        return true;
}

function esNumero(Char)
{
        if(Char.length!=1)
            return false;
        Char=Char.toLowerCase();
        RefString="1234567890";
        if (RefString.indexOf(Char.toLowerCase(), 0) == -1)
            return false;
        return true;
}

function esDecimal(Char)
{
        if(Char.length!=1)
            return false;
        Char=Char.toLowerCase();
        RefString="1234567890.";
        if (RefString.indexOf(Char.toLowerCase(), 0) == -1)
            return false;
        return true;
}


function validarEmail(valor) {
  if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(valor)){
    return (true);
  }
  else {

    return (false);
  }
}

function limpiar(texto)
{
var tmpstr = "";

for ( i=0; i < texto.length ; i++ )
if ( texto.charAt(i) != ' ' && texto.charAt(i) != '.' && texto.charAt(i) != '-' )
tmpstr = tmpstr + texto.charAt(i);texto = tmpstr;

return texto.toUpperCase();

}

function checkRut(texto)
{
var tmpstr = "";
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 < 8 )
{
return false;
}
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" )
{
return false;
}

}
if(checkDV(texto))
{
    return true;
}
else
{
    return false;
}


}
function checkDV(crut)
{

   largo = crut.length;

  if ( largo >= 8 )
    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() )
	{
    return false;
  }

return true;
}

function direccion(id,accion)
{
	if (accion == 'eliminar')
	{
		var answer = confirm("Está seguro que desea borrar este archivo.\nRecuerda que esto no se puede deshacer.");
		if(answer)
		{
			document.forma.operacion.value = accion;
			document.forma.id_paso.value = id;
			document.forma.action = "./index.php";
			document.forma.submit();
			return true;
		}
	}

	else
	{
		document.forma.operacion.value = accion;
		document.forma.id_paso.value = id;
		document.forma.action = "./index.php";
		document.forma.submit();
	}
}

function pasar(id)
{
		document.forma.comienzo.value = id;
		document.forma.action = "./index.php";
		document.forma.submit();
}


function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);