﻿// INICIO VALIDACIONES
var CodArea = "0";
var CodComuna = "0";





function TrimCasilla(obj)
    {
        //alert(obj.id);
        document.getElementById(obj.id).value = Trim(document.getElementById(obj.id).value);
//        if(parseInt(document.getElementById(obj.id).value.length) < 3)
//        {
//            document.getElementById(obj.id).focus();
//        }
    }

function KeyPress()
    {
        if(event.keyCode == 13)
        {   
            if(ValidaTelefonos_Area("txt_fono2") == true)
            {
                //document.all.btn_aceptar.onclick();
                document.all.txt_fono3.focus();
                //document.all["CustomValidator1"].isvalid;
            }
        }
    }

function TelefonoValidador()
    {
        if(parseInt(document.getElementById('txt_fono3').value.length) >= parseInt(document.getElementById('txt_fono2').value.length))
        {
            document.all["CustomValidator1"].controltovalidate = "txt_fono3";
        }  
    }

function ValidaTelefonos(source, arguments)
    {   
        document.all["CustomValidator1"].errormessage = '* Ingrese al menos dos teléfonos válidos.';    
        var NumInvalid = "";
        for(i=1;i<=2;i++)
        {   
            if(ValidaTelefonos_Area("txt_fono" + i) == false)
            {
                NumInvalid += i.toString();
            }
        }
        i = null;
        if(parseInt(document.getElementById('txt_fono3').value.length) < 8)
        {
            NumInvalid += "3";     
        }        
        if(parseInt(NumInvalid.length) <= 1)
        {
            document.all["CustomValidator1"].errormessage = '';
            arguments.IsValid=true;
            return;
        }
        else
        {     
            switch(NumInvalid)
            {
                case "12":
                case "13":
                case "123":
                    document.all["CustomValidator1"].controltovalidate = "txt_fono1";
                    break;
                default:
                    if(parseInt(document.getElementById('txt_fono2').value.length) >= parseInt(document.getElementById('txt_fono3').value.length))
                    {
                        document.all["CustomValidator1"].controltovalidate = "txt_fono2";
                    }
                    break;
            }
            arguments.IsValid=false;
            return;
        }             
    }
 
function ValidaTelefono1(source, arguments)
    {
        if(ValidaTelefonos_Area("txt_fono1") == true)
        {
            arguments.IsValid=true;
            return;
        }
        else
        {
            arguments.IsValid=false;
            return;
        }
    }    
    
function ValidaTelefono2(source, arguments)
    {
        if(ValidaTelefonos_Area("txt_fono2") == true)
        {
            arguments.IsValid=true;
            return;
        }
        else
        {
            arguments.IsValid=false;
            return;
        }
    }
    
function ValidaTelefonos_Area(_NombreFono)
    {        
        switch(CodArea)
        {
            case "2":
            case "32":            
            case "41":            
                if(parseInt(document.getElementById(_NombreFono).value.length) == 7 && IsNum(document.getElementById(_NombreFono).value)== true)
                {
                    return true;
                }
                else
                {
                    return false;
                }
                break;
            case "0":
                return false;
                break;  
            default:
                if(parseInt(document.getElementById(_NombreFono).value.length) == 6 && IsNum(document.getElementById(_NombreFono).value)== true)
                {
                    return true;
                }
                else
                {
                    return false;
                }
                break;
        } 
    }           
    
function ValidaEmailC3(source, arguments)
    {
        if(parseInt(document.all.txt_emaildom2.value.length) >= 2)
        {
            arguments.IsValid=true;
            return;
        }
        else
        {
            arguments.IsValid=false;
            return;
        }             
    }

//Funciones string
function LTrim(s)
    {
	    // Devuelve una cadena sin los espacios del principio
	    var i=0;
	    var j=0;
    	
	    // Busca el primer caracter <> de un espacio
	    for(i=0; i<=s.length-1; i++)
		    if(s.substring(i,i+1) != ' '){
			    j=i;
			    break;
		    }
	    return s.substring(j, s.length);
    }
    
function RTrim(s)
    {
	    // Quita los espacios en blanco del final de la cadena
	    var j=0;
    	
	    // Busca el último caracter <> de un espacio
	    for(var i=s.length-1; i>-1; i--)
		    if(s.substring(i,i+1) != ' '){
			    j=i;
			    break;
		    }
	    return s.substring(0, j+1);
    }
    
function Trim(s)
    {
	    // Quita los espacios del principio y del final
	    return LTrim(RTrim(s));
    }
    
function IsNum(numstr) 
    {
        
        if (numstr+"" == "undefined" || numstr+"" == "null" || numstr+"" == "")
        { 
            return false;
        }
        var isValid = true;
        numstr += ""; 
        for (j = 0; j < numstr.length; j++) 
        {
            if(!(numstr.charAt(j) >= "0" && numstr.charAt(j) <= "9")) 
            {
                isValid = false;
                break;
            } 
        }
        j = null;
        return isValid;
    } 
//Fin funciones string 
 
//Validación de Rut.
function ValidaRut(source, arguments)
    {
        if(ValidaDigRut()== true && ValidaSecRut() == true)
        {
            arguments.IsValid=true;
            return;
        }
        else
        {
            document.all.txt_rutdig.value = "";
            document.all.txt_rut.focus();
            arguments.IsValid=false;
            return;
        }
    }
    
function ValidaSecRut()
    {
        var posicion = 1;
        var str = document.all.txt_rut.value;
        if(str.length >= 6) //según Aldo L. 6,7,8
        { 
            while (posicion < parseInt(str.length))
            {
                if (str.substring(0, 1) == str.substring(posicion, posicion + 1))
                {
                    posicion++;
                }
                else
                {
                    return true;
                }
            }
        }
        return false;
    }
        
function ValidaDigRut()
    {
        var rut=document.all.txt_rut.value;
        var count=0;
        var count2=0;
        var factor=2;
        var suma=0;
        var sum=0;
        var digito=0;
        count2=rut.length - 1;
        while(count < rut.length)
        {
            sum = factor * (parseInt(rut.substr(count2,1))); 
            suma = suma + sum;
            sum=0;

            count = count + 1;
            count2 = count2 - 1;
            factor = factor + 1;

            if(factor > 7)
            {
            factor=2; 
            } 
        }

        digito= 11 - (suma % 11)

        if(digito==11)
        {
            digito=0;
        }

        if(digito==10)
        {
            digito="K";
        }
        if(digito == document.all.txt_rutdig.value.toUpperCase())
        {
            return true;
        }
        else
        {
            return false;
        }
    }    
//************FUNCION SEGÚN RUT    
function ValEmpPer()
    {
        if (parseInt(document.all.txt_rut.value) >= 50000000)
        {
            document.getElementById('empresa').style.display='block';
        }
        else
        {
            document.getElementById('txt_rsocial').value = "";
            document.getElementById('empresa').style.display='none';
        }
    }
    
function ValidaRSocial(source, arguments)
    {
        if (parseInt(document.all.txt_rut.value) >= 50000000)
        {
            var rsocial = document.getElementById('txt_rsocial').value;
            rsocial = Trim(rsocial);
            document.getElementById('txt_rsocial').value = rsocial;
            if(parseInt(rsocial.length) < 5)
            {
                arguments.IsValid = false;
                return;        
            }
        }
        arguments.IsValid = true; 
        return;
    }
    
function ValidaDireccion(source, arguments)
    {
        var direccion = document.getElementById('txt_direccion').value;
        direccion = Trim(direccion);
        document.getElementById('txt_direccion').value = direccion; 
        if(parseInt(direccion.length) >= 3)
        {
            arguments.IsValid = true; 
            return;
        }
        else
        {
            arguments.IsValid = false;
            return; 
        }
        
    }
//FIN VALIDACIONES
//***************************************************************************
function ColorActivo(obj)
    {
        obj.style.backgroundColor='#A3FF40';
    }
function ColorDesactivo(obj)
    {
        obj.style.backgroundColor='#FFFFFF';
    }

function LimpiarForm()
    {  
        try
        {
            document.all.dlist_dirigidoa.selectedIndex = 0;
        }
        catch(error0)
        {}
        try
        {
            //solo formularios para empresa
            document.getElementById('txt_rsocial').value = "";
        }
        catch(error1)
        {}
        document.getElementById('txt_rut').value = "";
        document.getElementById('txt_rutdig').value = "";
        document.getElementById('txt_nombres').value = "";
        document.getElementById('txt_apaterno').value = "";
        document.getElementById('txt_amaterno').value = "";
        document.all.rdb_sexo[1].checked = false;
        document.all.rdb_sexo[2].checked = false; 
        document.getElementById('txt_direccion').value = "";
        document.all.dlist_region.selectedIndex = 0;
        document.all.dlist_comuna.selectedIndex = 0;
        document.getElementById('txt_cod1').value = "";
        document.getElementById('txt_cod2').value = "";
        document.getElementById('txt_fono1').value = "";
        document.getElementById('txt_fono2').value = "";
        document.getElementById('txt_fono3').value = "";
        document.getElementById('txt_fonoanexo').value = "";
        document.getElementById('txt_email').value = "";
        document.getElementById('txt_emaildom1').value = "";
        document.getElementById('txt_emaildom2').value = "";
        document.getElementById('txt_observacion').value = "";
        //valido para cotizador
//        try
//        {
//            document.all.dlist_decisioncompra.selectedIndex = 0;
//            document.all.rdb_seguro[1].checked = false;
//            document.all.rdb_seguro[2].checked = false; 
//            document.all.rdb_financiamiento[1].checked = false;
//            document.all.rdb_financiamiento[2].checked = false; 
//        }
//        catch(error2)
//        {}
        try
        {
            document.all.dlist_zona.selectedIndex = 0;
            document.all.dlist_servicioTec.selectedIndex = 0;
        }
        catch(error3)
        {}
        //cotizacion repuesto
        try
        {
            document.getElementById('txt_modelo').value = "";
            document.getElementById('txt_anovehiculo').value = "";
            document.all.rdb_uso[1].checked = false;
            document.all.rdb_uso[2].checked = false; 
            /*Nuevos campos agregados 11-01-07*/
            document.getElementById('txt_chasis').value = "";
            document.all.rdb_version[1].checked = false;
            document.all.rdb_version[2].checked = false;
        }
        catch(error4)
        {}
        //Registro web
        try
        {
            document.getElementById('txt_patente').value = "";
            document.getElementById('txt_anocompra').value = "";
        }
        catch(error5)
        {}
        //regitro web
         try
        {
            document.all.rdb_dueno[1].checked = false;
            document.all.rdb_dueno[2].checked = false; 
            document.all.rdb_usuario[1].checked = false;
            document.all.rdb_usuario[2].checked = false; 
            document.all.rdb_nuevo[1].checked = false;
            document.all.rdb_nuevo[2].checked = false; 
        }
        catch(error6)
        {}
        //test drive
        try
        {
            document.all.dlist_vehiculo.selectedIndex = 0;
        }
        catch(error7)
        {}
        //Reserva Hora
        try
        {
            document.getElementById('txt_revtecnica').value = "";
        }
        catch(error8)
        {}
    }    

function LimpiarFormAux()
    {  
        try
        {
            document.getElementById('txt_rsocial').value = "";
        }
        catch(ex){}
        document.getElementById('txt_nombres').value = "";
        document.getElementById('txt_apaterno').value = "";
        document.getElementById('txt_amaterno').value = "";
        document.all.rdb_sexo[1].checked = false;
        document.all.rdb_sexo[2].checked = false; 
        document.getElementById('txt_direccion').value = "";
        document.all.dlist_region.selectedIndex = 0;
        document.all.dlist_comuna.selectedIndex = 0;
        document.getElementById('txt_cod1').value = "";
        document.getElementById('txt_cod2').value = "";
        document.getElementById('txt_fono1').value = "";
        document.getElementById('txt_fono2').value = "";
        document.getElementById('txt_fono3').value = "";
        document.getElementById('txt_fonoanexo').value = "";
        document.getElementById('txt_email').value = "";
        document.getElementById('txt_emaildom1').value = "";
        document.getElementById('txt_emaildom2').value = "";
}      
//INICIO CALLBACKS
function CargaComunas_CallBack(response)
    {
        var droplist = document.getElementById('dlist_comuna');
        //droplist.disabled = true;
        for(i = droplist.options.length - 1;i > 0;i--)//limpiamos combox si tienes campos 
        {
            droplist.options[i] = null;
        }
        
        if(document.all.dlist_region.selectedIndex > 0)
        {
            droplist.options[0] = new Option("(Seleccione)","0",true,true);        
            for(var i = 0 ; i < response.value.Tables[0].Rows.length ;i++)
            {
                droplist.options[i + 1] = new Option(response.value.Tables[0].Rows[i].NomComuna,response.value.Tables[0].Rows[i].CodComuna,false,false);
            }
            droplist.disabled = false;
        }
        document.getElementById('txt_cod1').value = "";
        document.getElementById('txt_cod2').value = "";
        //alert("Entro a function comunas");
    }   
    
function CargaAutomotoras_CallBack(response)
    {
        var droplist = document.getElementById('dlist_servicioTec');
        //droplist.disabled = true;
        for(i = droplist.options.length - 1;i > 0;i--)//limpiamos combox si tienes campos 
        {
            droplist.options[i] = null;
        }
        
        if(document.all.dlist_zona.selectedIndex > 0)
        {
            droplist.options[0] = new Option("(Seleccione)","0",true,true);        
            for(var i = 0 ; i < response.value.Tables[0].Rows.length ;i++)
            {
                droplist.options[i + 1] = new Option(response.value.Tables[0].Rows[i].nombre,response.value.Tables[0].Rows[i].codigo,false,false);
            }
            droplist.disabled = false;
        }
        else
        {
            droplist.disabled = true;
        }
    }
    
function CargaCodigoArea_CallBack(response)
    {
        //document.alert(response);
        document.getElementById('txt_cod1').value = response.value;
        document.getElementById('txt_cod2').value = response.value;
        CodArea = response.value;
    } 

/*funcion para el retorno de datos cliente*/
function CargaDatosCliente_CallBack(response)
    {
        
        LimpiarFormAux();
          
        try    
        {
            try
            {
                document.getElementById('txt_rsocial').value = response.value.Tables[0].Rows[0].RSocial;
            }
            catch(ex2){}    
            document.getElementById('txt_nombres').value = response.value.Tables[0].Rows[0].Nombres;
            document.getElementById('txt_apaterno').value = response.value.Tables[0].Rows[0].Apellido1;
            document.getElementById('txt_amaterno').value = response.value.Tables[0].Rows[0].Apellido2;
            if(response.value.Tables[0].Rows[0].Sexo == "1")
            {
                document.getElementById('rdb_sexo_1').checked = true;
            }
            else
            {
                document.getElementById('rdb_sexo_0').checked = true;
            }
            document.getElementById('txt_direccion').value = response.value.Tables[0].Rows[0].Direccion;
            document.getElementById('dlist_region').value= response.value.Tables[0].Rows[0].Region;
            document.getElementById('dlist_comuna').disabled = false;
            CodComuna = response.value.Tables[0].Rows[0].Comuna;                       
            
            ClassAjax.CargaComunas(document.getElementById('dlist_region').value,CargaComunas_CallBack2);
            document.getElementById('txt_fono1').value = response.value.Tables[0].Rows[0].FonoCasa;
            document.getElementById('txt_fono2').value = response.value.Tables[0].Rows[0].FonoOficina;
            document.getElementById('txt_fono3').value = response.value.Tables[0].Rows[0].FonoCelular;
            document.getElementById('txt_fonoanexo').value = response.value.Tables[0].Rows[0].Anexo;
            
            try{
                var email = response.value.Tables[0].Rows[0].Email; 
                var Arrayemail = new Array();
                var Arraydominio = new Array();
                
                Arrayemail = email.split('@');
                Arraydominio = Arrayemail[1].split('.');
                
                document.getElementById('txt_email').value = Arrayemail[0];
                document.getElementById('txt_emaildom1').value = Arraydominio[0];
                document.getElementById('txt_emaildom2').value = Arraydominio[1];
            }catch(ex3){}
        }  
        catch(ex1){}      
    }
    
    function CargaComunas_CallBack2(response)
    {
        var droplist = document.getElementById('dlist_comuna');
        //droplist.disabled = true;
        for(i = droplist.options.length - 1;i > 0;i--)//limpiamos combox si tienes campos 
        {
            droplist.options[i] = null;
        }
        
        if(document.all.dlist_region.selectedIndex > 0)
        {
            droplist.options[0] = new Option("(Seleccione)","0",true,true);        
            for(var i = 0 ; i < response.value.Tables[0].Rows.length ;i++)
            {
                droplist.options[i + 1] = new Option(response.value.Tables[0].Rows[i].NomComuna,response.value.Tables[0].Rows[i].CodComuna,false,false);
            }
            droplist.disabled = false;
        }
        document.getElementById('txt_cod1').value = "";
        document.getElementById('txt_cod2').value = "";
        //alert("Entro a function comunas");
        
        document.getElementById('dlist_comuna').value= CodComuna;
        var CodRegion = document.getElementById('dlist_region').value;
        ClassAjax.CargaCodigoArea(CodComuna,CodRegion,CargaCodigoArea_CallBack);
    }
