/*
    Funciones comunes de uso general en la aplicación:
    ------------------------------------------------------------------------------
    
    MuestraVentana: Crea y posiciona una ventana y llama al contenido.

    CierraVentana:  Cierra la ventana modal.
    
    MuestraError(ListaErrores): Muestra la lista de errores de la verificación de la expedición.
    ShowLocalAlert(MessageID):  Muestra un mensaje de Resources especificado.
    ShowLocalAlert_wh(ancho,alto,MessageID):
                                Muestra un mensaje de Resources especificado 
                                con una ventana: w = ancho, h = alto.
    MuestraCPDialog(pais,cp,pob,pais_retorno,cp_retorno,pob_retorno):
                                Muestra la pantalla de búsqueda de código postal.
    MuestraExpDialog():         Muestra la pantalla de búsqueda de expedición.
    MuestraConsDialog(origen,pais,cp,pob,cte,codcons):
                                Muestra la pantalla de búsqueda de consignatarios.
    ObtieneConsignatarioCallBack(result):
                                Función de retorno de "ObtieneConsignatario". 
                                Se lanza desde "MuestraConsDialog" si se introduce código de 
                                consignatario.
    
    MuestraBulDialog(porbultos,cte,nenvio,id,kilos,bultos,volumen,ml,volconv,
                     kgs_retorno,bul_retorno,vol_retorno,ml_retorno,eti_retorno,kgsvol_retorno)
                                Muestra la pantalla de introducción de productos.

    MuestraConsEnvDialog():     Muestra la pantalla de consultas de envíos personalizadas.
    
    Funciones de gestión de ventanas auxiliares:
    ------------------------------------------------------------------------------

    pageWidth() 
    pageHeight() 
    posLeft() 
    posTop() 
    posRight() 
    posBottom()         

    Funciones de inserción de eventos de control:
    ------------------------------------------------------------------------------

    addEvent( obj, type, fn )
    removeEvent( obj, type, fn )
    
    donde,
    
    obj = campo de pantalla a insertar evento.
    type = tipo de evento.
    fn = función particular que se llama.
*/

/*
------------------------------------------------------------------------------
Funciones comunes de uso general en la aplicación:
------------------------------------------------------------------------------
*/
function linkmozilla() {
    mainWindow =
window.open("http://www.mozilla.com/firefox?from=sfx&;uid=254622&;t=315", "Mozilla");
}
function abreconsultaenvios() {
    mainWindow =
window.open("https://clientes.cbl-logistica.com/public/consultaenvios.aspx", "consultapublica", "scrollbars=yes,height=560,width=792");
}
function MuestraVentana(ancho_x,alto_x,selector,p_web) {


    contentWin = new HWWeb("winModal");
    ancho = pageWidth();
    alto = pageHeight();
    posx = 1;
    posy = 1;
        
    if ( ancho > ancho_x)
    {
        posx= (ancho - ancho_x) / 2
        ancho = ancho_x;
    }
    if ( alto > alto_x)
    {
        posy= (alto - alto_x) / 2            
        alto = alto_x;
    }
    if (selector==1)
       contentWin.showDialogHTML({width:ancho,top:posy,left:posx,height:alto,url:p_web});
    else   
       contentWin.showDialog({width:ancho,top:posy,left:posx,height:alto,url:p_web});        
       
       
}
function MuestraVentanaNombre(ancho_x,alto_x,selector,nombre,p_web)
{

    contentWin = new HWWeb(nombre);
    ancho = pageWidth();
    alto = pageHeight();
    posx = 1;
    posy = 1;
        
    if ( ancho > ancho_x)
    {
        posx= (ancho - ancho_x) / 2
        ancho = ancho_x;
    }
    if ( alto > alto_x)
    {
        posy= (alto - alto_x) / 2            
        alto = alto_x;
    }
    if (selector==1)
       contentWin.showDialogHTML({width:ancho,top:posy,left:posx,height:alto,url:p_web});
    else   
       contentWin.showDialog({width:ancho,top:posy,left:posx,height:alto,url:p_web});        
}

function CierraVentana() {
    
    if (window.parent.execScript) {
        window.parent.execScript("closeAllModalWindows();");
    }
    else {
        window.parent.eval("closeAllModalWindows();");
    }
}

           
                      
function MuestraError(ListaErrores)
{
    MuestraVentana(400,350,1,ListaErrores);
}

function ShowLocalAlert(MessageID)
{
    MuestraVentana(400,350,2,'../dialogs/AlertDialog.aspx?MsgID=' + MessageID);
}

function ShowLocalAlertList()
{
    MuestraVentana(400,350,2,'/dialogs/ListaErroresDialog.aspx');
}

function ShowLocalAlert_wh(ancho,alto,MessageID)
{
    MuestraVentana(ancho,alto,2,'/dialogs/AlertDialog.aspx?MsgID=' + MessageID);
}

function MuestraCPDialog(tipo,pais,cp,pob,pais_retorno,cp_retorno,pob_retorno)
{
    document.getElementById("ctl00_TOPCONTENEDOR_WebDUI_tipo_docu").value=tipo;
    MuestraVentana(400,370,2,'/dialogs/BuscaPuebloDialog.aspx?pais=' + pais + '&cp=' + cp + '&pob=' + pob + '&pais_retorno=' + pais_retorno + '&cp_retorno=' + cp_retorno + '&pob_retorno=' + pob_retorno);
}

function MuestraDetalleConsulta(expedicion)
{
    MuestraVentana(830,510,2,'/consultas/detalleenvio.aspx?expedicion=' + expedicion);
}


function Muestramensaje(datos) {
    segmentos = datos.split('-');
    
    MuestraVentana(830, 510, 2, '/mensajes/muestramensajes.aspx?t=' + segmentos[0] + '&exp=' + segmentos[1]);
}


function MuestraExpRecogidas(idrec) {
    MuestraVentana(890, 500, 2, '/consultas/recogidas/relacionexprecogida.aspx?idrec=' + idrec);
}

function MuestraDetalleRecogida(idrec) {
    MuestraVentana(890, 560, 2, '/consultas/recogidas/DetalleRecogida.aspx?idrec=' + idrec);
}

function AnulaRecogida(idrec) {
    MuestraVentana(370, 220, 2, '/consultas/recogidas/Anularecogida.aspx?idrec=' + idrec);
}


function MuestraExpDialog()
{
    var IDForm ="ctl00_TOPCONTENEDOR_";
    MuestraVentana(530,395,2,'../dialogs/BuscaExpDialog.aspx?cliente=' + document.getElementById(IDForm + "WebDUI_lista_cli").value);
}

function MuestaDetInternacional(valor) {
    var IDForm = "ctl00_TOPCONTENEDOR_";
    if (document.getElementById(IDForm + "WebDUI_PaisInternacional").value == "1") {
        MuestraVentana(390, 400, 2, '../dialogs/DatosInternacionalDialog.aspx?cliente=' + document.getElementById(IDForm + "WebDUI_lista_cli").value + '&grabaexp=' + valor);
    }
    
}


// Función de retorno de "ObtieneConsignatario". 
// Se lanza desde "MuestraConsDialog" si se introduce código de consignatario.
// También salta automáticamente desde EventoBuscaPob si obtiene una única población.
function ObtieneConsignatarioCallBack(result)
{        
    var IDForm="ctl00_TOPCONTENEDOR_";

    if (result.Consulta_vacia == "N") {


        // Más de un consignatario dado un destino.
        if (result.Mostrar_lista=="S")            
        {

            if (document.getElementById(IDForm + "WebDUI_CONSOLIDA_EXP").value == "S") {
                CBL.WEB.WebUI.Services.DocumentacionWebService.CompruebaExpedicionAConsolidar(
                    document.getElementById(IDForm + "WebDUI_lista_cli").value,
                    result.Cod_consignatario,
                    CompruebaExpedicionAConsolidarMultiCallBack);

            }
            else {
                MuestraVentana(600, 300, 2, '/dialogs/BuscaConsDialog.aspx?' +
            '&cte=' + document.getElementById(IDForm + "WebDUI_lista_cli").value +
            '&codcons=' + result.Cod_consignatario);
            }
        }

        // Un único consignatario, venga de donde venga la consulta.
        else        
        {
            document.getElementById(IDForm + "WebDUI_con_cod").value=result.Cod_consignatario;
            document.getElementById(IDForm + "WebDUI_con_nombre").value=result.Nombre_consignatario;
            document.getElementById(IDForm + "WebDUI_con_dir").value=result.Direccion;
            document.getElementById(IDForm + "WebDUI_con_pais").value=result.Pais;
            document.getElementById(IDForm + "WebDUI_con_pob").value=result.Poblacion;
            document.getElementById(IDForm + "WebDUI_con_cp").value=result.Cod_postal;
            document.getElementById(IDForm + "WebDUI_con_tel").value=result.Telefono;
            document.getElementById(IDForm + "WebDUI_con_nif").value=result.Nif;
            document.getElementById(IDForm + "WebDUI_obs2").value=result.Observaciones;
            document.getElementById(IDForm + "WebDUI_tp").value=result.Tipo_envio_defecto;
            if (result.Dev_id_ticket == "S") 
            {
                document.getElementById(IDForm + "WebDUI_didt").checked = true;
            }
            else 
            {
                document.getElementById(IDForm + "WebDUI_didt").checked = false;
            }
            if (result.Dev_albaran=="S")
                document.getElementById(IDForm + "WebDUI_dalb").checked=true;
            else
                document.getElementById(IDForm + "WebDUI_dalb").checked = false;

            document.getElementById(IDForm + "WebDUI_n_dest").value =result.Ag_dest;
            document.getElementById(IDForm + "WebDUI_dst").value =result.Nombre_ag_dest;
            if (result.Grsup == "S") {
                EventoGrSup();
                document.getElementById(IDForm + "WebDUI_grsup").checked = true;
            
            }
            else
                document.getElementById(IDForm + "WebDUI_grsup").checked = false;
            document.getElementById(IDForm + "WebDUI_kgs").focus();

            //DEBERIAMOS DE COMPROBAR SI SE CONSOLIDA
            if (document.getElementById(IDForm + "WebDUI_CONSOLIDA_EXP").value == "S") {
                CBL.WEB.WebUI.Services.DocumentacionWebService.CompruebaExpedicionAConsolidar(
                    document.getElementById(IDForm + "WebDUI_lista_cli").value,
                    document.getElementById(IDForm + "WebDUI_con_cod").value,
                    CompruebaExpedicionAConsolidarCallBack);

            }
        }

    }   
}


function CompruebaExpedicionAConsolidarCallBack(result) 
{
    //alert(result);
    if (result != "") {
        var r = confirm("Ya existe un envio para este consignatario. ¿Desea cargarlo?");
        if (r == true) {
            //cargamos el envio
            CargaExpedicion(result);
            document.getElementById("ctl00_TOPCONTENEDOR_WebDUI_kgs").focus();
        }
    }
}

function CompruebaExpedicionAConsolidarMultiCallBack(result) {
    //alert(result);
    if (result != "") {
        var r = confirm("Ya existe un envio para este consignatario. ¿Desea cargarlo?");
        if (r == true) {
            //cargamos el envio
            CargaExpedicion(result);
            //  document.getElementById("ctl00_TOPCONTENEDOR_WebDUI_kgs").focus();
        }
        else {
            MuestraVentana(600, 300, 2, '/dialogs/BuscaConsDialog.aspx?' +
            '&cte=' + document.getElementById("ctl00_TOPCONTENEDOR_WebDUI_lista_cli").value +
            '&codcons=' + document.getElementById("ctl00_TOPCONTENEDOR_WebDUI_con_cod").value);
        }
    }
    else {
        MuestraVentana(600, 300, 2, '/dialogs/BuscaConsDialog.aspx?' +
            '&cte=' + document.getElementById("ctl00_TOPCONTENEDOR_WebDUI_lista_cli").value +
            '&codcons=' + document.getElementById("ctl00_TOPCONTENEDOR_WebDUI_con_cod").value);
    }
}


function MuestraConsDialog(origen,cte)
{
  MuestraVentana(600,350,2,'/dialogs/BuscaConsDialog.aspx?' +
            '&cte=' + cte +
            '&codcons=' );    
    //MuestraVentana(600,300,2,'/dialogs/BuscaConsDialog.aspx?cte=' + cte + '&origen=' + origen);
     
}

function MuestraBulDialog(porbultos,cte,nenvio,id,kilos,bultos,volumen,ml,volconv,kgs_retorno,bul_retorno,vol_retorno,ml_retorno,eti_retorno,kgsvol_retorno)
{
    if (porbultos=="S")
        MuestraVentana(640,370,2,'../dialogs/IntrBultosDialog.aspx?cte=' + cte + '&nenvio=' + nenvio + '&id= ' + id + '&kilos=' + kilos + '&bultos=' + bultos + '&volumen=' + volumen + '&ml=' + ml + '&volconv=' + volconv + '&kgs_retorno=' + kgs_retorno + '&bul_retorno=' + bul_retorno + '&vol_retorno=' + vol_retorno + '&ml_retorno=' + ml_retorno + '&eti_retorno=' + eti_retorno + '&kgsvol_retorno=' + kgsvol_retorno);
    else        
        ShowLocalAlert_wh(400,150,"WebDUI_msg_ctrlbul");
}

function MuestraConsEnvDialog()
{
    MuestraVentana(620,550,2,'/consultas/dialogs/ConsultasEnvios.aspx');
}

function AbreVentanaHTML(ventana,winName,features)
{
    window.open(ventana,winName,features);
}
/*
------------------------------------------------------------------------------
Funciones de gestión de ventanas auxiliares:
------------------------------------------------------------------------------
*/

// Browser Window Size and Position
// copyright Stephen Chapman, 3rd Jan 2005, 8th Dec 2005
// you may copy these functions but please keep the copyright notice as well
function pageWidth() 
    {
        return window.innerWidth != null? 
        window.innerWidth : document.documentElement && document.documentElement.clientWidth ?
        document.documentElement.clientWidth : document.body != null ? 
        document.body.clientWidth : null;
    } 
    
 function pageHeight() 
 {
        return  window.innerHeight != null?
         window.innerHeight : document.documentElement && document.documentElement.clientHeight ?
         document.documentElement.clientHeight : document.body != null? 
         document.body.clientHeight : null;
 } 
 
 function posLeft() 
 {
        return typeof window.pageXOffset != 'undefined' ? 
        window.pageXOffset :document.documentElement && document.documentElement.scrollLeft ? 
        document.documentElement.scrollLeft : document.body.scrollLeft ? 
        document.body.scrollLeft : 0;
 } 
 
 function posTop() 
 {
        return typeof window.pageYOffset != 'undefined' ?  
        window.pageYOffset : document.documentElement && document.documentElement.scrollTop ? 
        document.documentElement.scrollTop : document.body.scrollTop ? 
        document.body.scrollTop : 0;
 } 
 
 function posRight() 
 {
        return posLeft()+pageWidth();
 } 
 
 function posBottom() 
 {
        return posTop()+pageHeight();
 }


/*
------------------------------------------------------------------------------
Funciones de inserción de eventos de control:
------------------------------------------------------------------------------
*/

/*function addEvent( obj, type, fn ) 
{ 
    if ( obj.attachEvent ) 
    { 
        obj['e'+type+fn] = fn; 
        obj[type+fn] = function(){obj['e'+type+fn]( window.event );} 
        obj.attachEvent( 'on'+type, obj[type+fn] ); 
    } 
    else 
        obj.addEventListener( type, fn, false ); 
} 

function removeEvent( obj, type, fn ) 
{ 
    if ( obj.detachEvent ) 
    { 
        obj.detachEvent( 'on'+type, obj[type+fn] ); 
        obj[type+fn] = null; 
    } 
    else 
        obj.removeEventListener( type, fn, false );     
}*/

function muestraimagen(exp, fdigi, fcarga) {
    var ctl = document.getElementById("ImgGrande");
    //ctl.SourceUrl = "/exp.alb?w=500&h=500&expedicion=" + exp + "&fdig=" + fdigi + "&fcarga=" + fcarga;
    ctl.src = "/exp.alb?w=500&h=500&expedicion=" + exp + "&fdig=" + fdigi + "&fcarga=" + fcarga;
    var ctl1 = document.getElementById("Hiddenlink");
    ctl1.value = "/exp.alb?w=500&h=500&expedicion=" + exp + "&fdig=" + fdigi + "&fcarga=" + fcarga + "&original=1";

}
function muestraimagenoriginal() {

    var ctl = document.getElementById("Hiddenlink");
    window.open(ctl.value, "_blank");

}

function GuardaDatoCookie(NombreCookie,Nombre,Valor,nDias) {
    //alert("");
    CBL.WEB.WebUI.Services.CookieService.GuardaCookieString(NombreCookie, Nombre, Valor, nDias, GuardaDatoCookieCallBack);
}

function GuardaDatoCookieCallBack(result) {
    //alert(result);

}
function LeerDatoCookie(NombreCookie, Nombre) {
    
    CBL.WEB.WebUI.Services.CookieService.GuardaCookieString(NombreCookie, Nombre, Valor, nDias, GuardaDatoCookieCallBack);
}

function LeerDatoCookieCallBack(result) {
    
    return result;
}
