// JavaScript Document




function Tipo(txt, nombre, tipo)
{
    if(busca_espacios(txt) == false)
    {
        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)
    {
        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 llenarDatos(tipo,id)
{
	var img_oo = "http://uai.ecorp.cl/img/roja_t.png";
	var img_oko = "http://uai.ecorp.cl/img/verde_t.png";
	

	if(document.getElementById(id).checked == true)
	{
		
		if(document.getElementById('trabaja').value == "SI")
		{
			//SE LLENAN LOS DATOS CON LOS DATOS ANTERIORES
			if(document.getElementById('empresa').value != ""){
				document.getElementById('empresa_facturacion').value = document.getElementById('empresa').value;	
				cambiaRequerido('requerido','empresa_facturacion','empresa_facturacion','out',img_oo,img_oko);
			}
			
			if(document.getElementById('giro').value != ""){
				document.getElementById('giro_facturacion').value = document.getElementById('giro').value;	
				cambiaRequerido('requerido','giro_facturacion','giro_facturacion','out',img_oo,img_oko);
			}
			if(document.getElementById('rut_empresa').value != ""){
				document.getElementById('rut_empresa_facturacion').value = document.getElementById('rut_empresa').value;	
				cambiaRequerido('requerido','rut_empresa_facturacion','rut_empresa_facturacion','out',img_oo,img_oko);
			}
			
			if(document.getElementById('direccion_comercial').value != ""){
				document.getElementById('direccion_comercial_facturacion').value = document.getElementById('direccion_comercial').value;	
				cambiaRequerido('requerido','direccion_comercial_facturacion','direccion_comercial_facturacion','out',img_oo,img_oko);
			}
			if(document.getElementById('comuna_comercial').value != ""){
				document.getElementById('comuna_comercial_facturacion').value = document.getElementById('comuna_comercial').value;	
				cambiaRequerido('requerido','comuna_comercial_facturacion','comuna_comercial_facturacion','out',img_oo,img_oko);
			}
			
			if(document.getElementById('ciudad_comercial').value != ""){
				document.getElementById('ciudad_comercial_facturacion').value = document.getElementById('ciudad_comercial').value;
				cambiaRequerido('requerido','ciudad_comercial_facturacion','ciudad_comercial_facturacion','out',img_oo,img_oko);
			}
		}
	}
	
	if(document.getElementById(id).checked == false)
	{
		if(document.getElementById('trabaja').value == "SI")
		{
		//SE VACIAN LOS CAMPOS
			document.getElementById('empresa_facturacion').value = "";	
			cambiaRequerido('requerido','empresa_facturacion','empresa_facturacion','out',img_oo,img_oko);
			
			document.getElementById('giro_facturacion').value = "";
			cambiaRequerido('requerido','giro_facturacion','giro_facturacion','out',img_oo,img_oko);
			document.getElementById('rut_empresa_facturacion').value = "";	
			cambiaRequerido('requerido','rut_empresa_facturacion','rut_empresa_facturacion','out',img_oo,img_oko);
			document.getElementById('direccion_comercial_facturacion').value = "";	
			cambiaRequerido('requerido','direccion_comercial_facturacion','direccion_comercial_facturacion','out',img_oo,img_oko);
			document.getElementById('comuna_comercial_facturacion').value = "";
			cambiaRequerido('requerido','comuna_comercial_facturacion','comuna_comercial_facturacion','out',img_oo,img_oko);
			document.getElementById('ciudad_comercial_facturacion').value = "";
			cambiaRequerido('requerido','ciudad_comercial_facturacion','ciudad_comercial_facturacion','out',img_oo,img_oko);
		}

	}
	
	
}