﻿// JScript File
function aalert(x) {
    alert(x);
}
function update(elemValue)
    {
        for (var x = 1; x < form1.ddlRegion.options.length; x++) {
            if (form1.ddlRegion.options[x].text == elemValue[0]) {
                form1.ddlRegion.options[x].selected = "1";
                form1.ddlRegion.disabled = "1";
                break;
            }
        }
        for (var x = 1; x < form1.ddlComuna.options.length; x++) {
            if (form1.ddlComuna.options[x].text == elemValue[1]) {
                form1.ddlComuna.options[x].selected = "1";
                form1.ddlComuna.disabled = "1";
                break;
            }
        }
//        document.getElementById('lblComuna').innerHTML = elemValue[1];
        document.getElementById('txtDireccion').value = elemValue[2];
        document.getElementById('hdDireccion').value=elemValue[2];
        document.getElementById('lblSucursal').innerHTML=elemValue[3];
    }
    
    function updateUnidades(elemValue)
    {
        document.getElementById('lblCantEnvioManual').innerHTML=elemValue[0];
        document.getElementById('hdUnidadesManual').value=elemValue[0];
    }
    
    function updateArchivo(elemValue)
    {
        document.getElementById('lblCantEnvioAuto').innerHTML=elemValue[0];
        document.getElementById('hdUnidadesAuto').value=elemValue[0];
        
        document.getElementById('lblArchivot').innerHTML=elemValue[1];
        document.getElementById('hdArchivo').value=elemValue[1];
    }

    function updateAutoCartas(elemValue) {
        document.getElementById('hdcantprod').value = elemValue[0];
        var myField = document.getElementById('lblArchivo');
        if (myField!=null)
            myField.innerText = elemValue[1] + ' Envs:' + elemValue[0];
    }


    function updateAutoPeso(elemValue) {
        var myField = document.getElementById('txtPeso');
        if (myField != null)
            myField.innerText = elemValue[0];
    }

    function cambiardirec()
    {
        var dir = document.getElementById('lblDireccion').innerHTML;
        window.open('APLINSUC.aspx?dir='+dir,'bustpf','toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=0,resizable=0,copyhistory=0, top=100, left=200 , width=400,height=120');
        return false;
    }

    function ingresoAuto() {
        if (document.form1.ddlProdClientes.value=="9999"
        ||  document.form1.ddlProdClientes.value=="0"){
            document.form1.ddlProdClientes.focus();
            aalert("Seleccione un Producto");   
            return false;
        }
        window.open('APLIINGREGAUTO.aspx?p=' + document.form1.ddlProdClientes.value + '&d='+ document.form1.ddlDestino.value , 'ingrega', 'toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=1,resizable=0,copyhistory=0, top=100, left=200 , width=700,height=620');
        return false;
    }

    function ingresoAutoPaquet() {
            window.open('APLIINGREGAUTOpaq.aspx', 'ingrega', 'toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=1,resizable=0,copyhistory=0, top=10, left=20 , width=1200,height=620');
            return false;
    }

    function ingresoManual() {
        window.open('APLIINGREGMANUAL.aspx', 'ingregm', 'toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=1,resizable=0,copyhistory=0, top=100, left=200 , width=800,height=620');
        return false;
    }

    function ingresoManualpaq() {
        window.open('APLIINGREGMANUALpaq.aspx', 'ingregm9', 'toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=1,copyhistory=0, top=10, left=20 , width=1200,height=620');
        return false;
    }

    function ingresoDirectorio() {
        var peso = parseInt(document.form1.txtPeso.value);
        var largo = parseInt(document.form1.txtLargo.value);
        var ancho = parseInt(document.form1.txtAncho.value);
        var alto = parseInt(document.form1.txtAlto.value);
        var valor = parseInt(document.form1.txtValr.value);
        var ref = document.form1.txtRef.value;
        var cont = document.form1.txtCont.value; 
        if ((valor == "" || valor == "0" || valor.toString() == "NaN") 
        ||  (peso == "" || peso == "0" || peso.toString() == "NaN") 
        ||  (largo == "" || largo == "0" || largo.toString() == "NaN") 
        ||  (ancho == "" || ancho == "0" || ancho.toString() == "NaN")   
        ||  (alto == "" || alto == "0" || alto.toString() == "NaN")) {
            aalert("Faltan datos para generar envíos usando la Libreta de Direcciones\nSe requieren:\n - Peso, Largo, Ancho, Alto\n - Valor");
            return false;
        }
        window.open('Directorio.aspx?p=' + valor + ';' + peso + ';' + largo + ';' + ancho + ';' + alto + ';' + ref + ';' + cont, 'direct', 'toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=1,copyhistory=0, top=10, left=20 , width=1200,height=620');
        return true;
    }

    function buscaragen()   
    {
        window.open('APLINAGENCIA.aspx','busagen','toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=1,resizable=0,copyhistory=0, top=100, left=200 , width=400,height=110');
        return false;
    }
    
    function validarAdjuntar()
    {
        if ( document.form1.hdidarchivo.value == "S")
        {
            if (document.form1.hdadmlinea.value=="")
            {
                aalert("Debe Grabar el Producto antes de Adjuntar");      
                return false;
            }     
        } 
        else
        {
            aalert("Opción no Disponible para este Producto");      
            return false;
        }
        
        return true;
    } 
    
    function validarEliminar()
    {
        if (document.form1.hdadmlinea.value=="")
        {
            aalert("Seleccione un Producto");      
            return false;
        }  
           
        return confirm('Confirma Eliminación');
    } 
    
    function validar()
    {
        var mesa =" y Presione Grabar";
        if (document.form1.ddlMaquinaFranq.value=="9999") 
        {   
            if (document.form1.ddlMaquinaFranq.length>1)
            {
                document.form1.ddlMaquinaFranq.focus();
                aalert("Seleccione una Maquina Franqueadora"+mesa);   
                return false;
            }
        }
    
        if (document.form1.ddlModoImposicion.value=="9999")
        { 
            document.form1.ddlModoImposicion.focus(); 
            aalert("Seleccione Modo de Imposición"+mesa);  
            return false;
        }
	
        if (document.form1.ddlModoImposicion.value=="MOIM002")
        {
            if (document.getElementById('lblSucursal').innerHTML == "") 
            {
                if (document.form1.btnBuscar != null) 
                {
                    document.form1.btnBuscar.focus();
                    aalert("Seleccione una Sucursal" + mesa);
                    return false;
                }
            }  
        }
	
        if (document.form1.ddlModoImposicion.value == "MOIM001") 
        {
            if (document.form1.ddlRegion(document.form1.ddlRegion.selectedIndex).value=="")
            {
                aalert("Seleccione lugar de Imposición(Sucursal Activa ó Dirección del Cliente)"+ mesa);   
                return false;
            }

            if (document.form1.ddlRegion(document.form1.ddlRegion.selectedIndex).text == "----------") {
                aalert("Seleccione Region de Imposición(Dirección del Cliente)");
                return false;
            }
            if (document.form1.ddlComuna(document.form1.ddlComuna.selectedIndex).text == "----------") {
                aalert("Seleccione Comuna de Imposición(Dirección del Cliente)");
                return false;
            }


            if (!ValidarFecha(document.form1.txtFechaRefImposicion.value))
            {
                document.form1.txtFechaRefImposicion.focus();
                aalert("Fecha Referencial Inválida - Formato(dd/mm/yyyy)");
                return false;
            }
    	
            if (document.form1.txtFechaVencimiento.value!="")    
            {
                if (!ValidarFecha(document.form1.txtFechaVencimiento.value))
                {
                    document.form1.txtFechaVencimiento.focus();
                    aalert("Fecha Vencimiento Inválida - Formato(dd/mm/yyyy)");
                    return false;
                } 
            }
        }
        return true;
    }
    
    function validarGrabar()
    {
        if (!validar())
            return false;
            
        var destinovar =document.form1.ddlDestino.value.substr(document.form1.ddlDestino.value.indexOf('/')+1,document.form1.ddlDestino.value.length); 
        if ((document.form1.hdadmidlprod.value=="LIN001") || (document.form1.hdadmidlprod.value=="LIN003")) 
        {
            if (destinovar!="CLP")
            {
                document.form1.ddlDestino.focus();
                aalert("Destino Seleccionado, No Corresponde a Producto");
                event.returnValue = false;    
                return false;
            }
        }
        if ((document.form1.hdadmidlprod.value=="LIN002") || (document.form1.hdadmidlprod.value=="LIN005")) 
        {
            if (destinovar=="CLP")
            {
                document.form1.ddlDestino.focus();
                aalert("Destino Seleccionado, No Corresponde a Producto");
                event.returnValue = false; 
                return false;
            }
        }     
        if (document.form1.ddlDestino.value=="9999")
        {
            document.form1.ddlDestino.focus();
            aalert("Seleccione un Destino");
            event.returnValue = false; 
            return false;
        }     
        var cant=quitarespacios( document.form1.txtCantidadTotal.value );
        if (cant=="")
        {
            document.form1.txtCantidadTotal.focus();
            aalert( "Debe ingresar Cantidad Total" );
            event.returnValue = false; 
            return false;
        }
        var canttot = parseInt(cant);
        if (isNaN(canttot))
        {
            document.form1.txtCantidadTotal.focus();
            aalert("Cantidad Total Erronea");
            event.returnValue = false;
            return false;
        }
        if ( canttot <= 0)
        {
            document.form1.txtCantidadTotal.focus();
            aalert("Cantidad Total no Puede ser Cero");
            event.returnValue = false;
            return false;
        }
         
        var peso=quitarespacios( document.form1.txtPeso.value );
        if( peso=="" )
        {
            aalert( "Debe ingresar Peso Unitario" );
            document.form1.txtPeso.focus();
            event.returnValue = false;
            return false;
        } 
        var pesotot = parseInt(peso);
        if (isNaN(pesotot))
        {
            document.form1.txtPeso.focus();
            aalert("Peso Unitario Erroneo");
            event.returnValue = false;
            return false;
        }
        if( pesotot<=0 )
        {
            document.form1.txtPeso.focus();
            aalert("Peso Unitario no Puede ser Cero");
            event.returnValue = false;
            return false;
        }  
        return true;
    }

    function confirmFinalizar() {
        var myField = document.getElementById('hdEnviado');
        if (myField != null)

        if (document.form1.hdEnviado.value == "SI") {
            alert("Información en Proceso. \n" + "Espere por favor...");
            return false;
        }
        else {
            document.form1.hdEnviado.value = "SI";
            var textodesp = "Esta Seguro de Admitir";
            if ( document.form1.btnFinalizar.value=="Finalizar")
            {
                var textodesp = "Esta Seguro de Finalizar";
            }
            if (confirm(textodesp))
                return true;
            document.form1.hdEnviado.value == "NO";
            return false;
        }
    }
    
    function validarFinalizar()
    {
        if( !validar() )
            return false;
            
        if ( document.form1.hdcantprod.value == "0")
        {
            aalert("Debe Ingresar un Producto Como Mínimo, Antes de Finalizar");   
            return false;
        }
        var fecha = quitarespacios(document.form1.txtFechaRefImposicion.value);
        var Dia = parseFloat(fecha)
        var Mes = parseInt(document.form1.txtFechaRefImposicion.value.substr(3, 2));
        var Anyo = parseInt(document.form1.txtFechaRefImposicion.value.substr(6,4));
        var now = new Date();
        var fechaRef = new Date(now.getTime());
        fechaRef.setFullYear(Anyo, Mes-1, Dia);


        var v_ar_fecha =document.form1.txtFechaRefImposicion.value.split("/");	
        var v_fecha_pro=v_ar_fecha[2]+v_ar_fecha[1]+v_ar_fecha[0];
        var v_fecha    =new Date();
        var v_fecha_act=v_fecha.getFullYear() + ((v_fecha.getMonth()+1)>9?(v_fecha.getMonth()+1):'0'+(v_fecha.getMonth()+1)) + (v_fecha.getDate()>9?v_fecha.getDate():'0'+v_fecha.getDate());


        //aalert("prueba " + v_fecha_pro + ' ' + v_fecha_act);

        //if ( now > fechaRef )
        /*if ( v_fecha_pro<v_fecha_act)
        {
            document.form1.txtFechaRefImposicion.focus();
            aalert("Fecha Ref de Imposición Anterior a Hoy");       
            return false;
        }*/
        if (document.form1.txtHoraIni != null) {

            var cadena1 = quitarespacios(document.form1.txtHoraIni.value);
            if (cadena1 == "") {
                document.form1.txtHoraIni.focus();
                aalert("Ingrese Hora Referencial");
                return false;
            }
            if (!formatohora(document.form1.txtHoraIni)) {
                return false;
            }
        }

        if (document.form1.ddlModoImposicion.value == "MOIM001") {
            var cadena2 = quitarespacios(document.form1.txtHoraFin.value);
            if (cadena2 == "") {
                document.form1.txtHoraFin.focus();
                aalert("Ingrese Hora Hasta");
                return false;
            }
            if (cadena2 != "") {
                if (!formatohora(document.form1.txtHoraFin)) {
                    return false;
                }
            }
        }
        if (document.form1.ddlModoImposicion.value == "MOIM001") {
            var cadena3 = quitarespacios(document.form1.txtNombreContacto.value);
            if (cadena3 == "") {
                document.form1.txtNombreContacto.focus();
                aalert("Ingrese Contacto");
                return false;
            }
            var cadena4 = quitarespacios(document.form1.txtTelefRetiro.value);
            if (cadena4 == "") {
                document.form1.txtTelefRetiro.focus();
                aalert("Ingrese Telefono Contacto");
                return false;
            }
            if (cadena4.length > 10) {
                document.form1.txtTelefRetiro.focus();
                aalert("Telefono Contacto muy largo (10 max.)");
                return false;
            }
        } 
        
        return confirmFinalizar();
    } 

    function validarSiguiente() {

        if (document.form1.ddlModoImposicion.value=="9999")
        { 
            document.form1.ddlModoImposicion.focus(); 
            aalert("Seleccione Modo de Imposición");  
            return false;
        }

        if (document.form1.ddlModoImposicion.value=="MOIM002")
        {
            if (document.getElementById('lblSucursal').innerHTML=="")
            {
                if (document.form1.btnBuscar != null) {
                    document.form1.btnBuscar.focus();
                    aalert("Seleccione una Sucursal");
                    return false;
                }
            }  
        }

        if (document.form1.ddlModoImposicion.value == "MOIM001") {
            var ddl = document.getElementById('ddlRegion');

            if (ddl.options(ddl.selectedIndex).value == "") {
                aalert("Seleccione Region de Imposición(Dirección del Cliente)");
                return false;
            }
            if (ddl.options(ddl.selectedIndex).text == "----------") {
                aalert("Seleccione Region de Imposición(Dirección del Cliente)");
                return false;
            }

            var com = document.getElementById('ddlComuna');
            if (com.options(com.selectedIndex).value == "") {
                aalert("Seleccione Comuna de Imposición...");
                return false;
            }
            if (com.options(com.selectedIndex).text == "----------") {
                aalert("Seleccione Comuna de Imposición...");
                return false;
            }

            if (!ValidarFecha(document.form1.txtFechaRefImposicion.value)) {
                document.form1.txtFechaRefImposicion.focus();
                aalert("Fecha Referencial Erronea - Formato(dd/mm/yyyy)");
                return false;
            }
            if (document.form1.txtHoraIni != null) {
                var cadena1 = quitarespacios(document.form1.txtHoraIni.value);
                if (cadena1 == "") {
                    document.form1.txtHoraIni.focus();
                    aalert("Ingrese Hora Referencial");
                    return false;
                }

                if (!formatohora(document.form1.txtHoraIni)) {
                    return false;
                }
            }
            if (document.form1.txtHoraFin != null) {
                var cadena2 = quitarespacios(document.form1.txtHoraFin.value);
                if (document.form1.ddlModoImposicion.value == "MOIM001") {
                    if (cadena2 == "") {
                        document.form1.txtHoraFin.focus();
                        aalert("Ingrese Hora Hasta");
                        return false;
                    }
                }
                if (cadena2 != "") {
                    if (!formatohora(document.form1.txtHoraFin)) {
                        return false;
                    }
                }
            }
        }
        
        
        if (document.form1.ddlModoImposicion.value=="MOIM001") 
        {
            var cadena3=quitarespacios(document.form1.txtNombreContacto.value);
            if (cadena3=="")
            {
                 document.form1.txtNombreContacto.focus();
                 aalert("Ingrese Contacto");   
                 return false;
            }
            var cadena4=quitarespacios(document.form1.txtTelefRetiro.value);  
            if (cadena4=="")
            {
                  document.form1.txtTelefRetiro.focus();
                aalert("Ingrese Telefono Contacto");   
                 return false;
            }
            if (cadena4.length>10)
            {
                 document.form1.txtTelefRetiro.focus();
                 aalert("Telefono Contacto muy largo (10 max.)");   
                 return false;
            }
        } 
        if ((document.getElementById('lblCantEnvioAuto').innerHTML==0)&&(document.getElementById('lblCantEnvioManual').innerHTML==0))
        {
            if (document.form1.btnIngregManual.Enabled = "1") document.form1.btnIngregManual.focus();
            if (document.form1.btnIngregAuto.Enabled = "1") document.form1.btnIngregAuto.focus();
            aalert("Ingrese Registros en RECUPERAR ARCHIVO O INGRESO MANUAL");   
            return false;
        } 
        return true;
    }
        
    function validarNumRef()
    {
        if (document.form1.cajanref.value=="")
        {
            document.form1.cajanref.focus();
            aalert("Ingrese el Número de Referencia"); 
            return false;
        }
        if (parseInt(document.form1.cajanref.value)<=0)
        {
            f.cajanref.focus();
            aalert("Número de Referencia Mayor a cero"); 
            return false;
        }
        return true;
    }

    function validarNumEnvio()
    {
        var caja=quitarespacios(document.form1.cajanenv.value);
        if (caja.length<13)
        {
            document.form1.cajanenv.focus();
            aalert("Ingrese 13 Números como Mínimo");
            return false;
        }
        if (funciondig(caja)!=parseInt(caja.substr(12,1)))
        {
            document.form1.cajanenv.focus();
            aalert("El Dígito no es Correspondiente \n" + "para ese Código de Envío.");
            return false;
        }   
        return true;
    }    
    