﻿// Javascript File

// Función IsNull - Para preguntar en javascript si algo no está definido
// Evento document.onkeypress - Captura el Evento KeyPress y evita CTRL+P (Impresión)
// Evento document.onkeydown - Captura el Evento KeyDown y evita CTRL+P (Impresión)
// Evento document.oncontextmenu - Evita el botón derecho salvo en determinados controles
// Evento document.onselectstart - Evita poder serleccionar texto salvo en determinados controles
// Evento document.ondragstart - Inhabilita poder iniciar un Drag&Drop
// Evento window.onerror - Marcar siempre el objeto window sin error

/* Solo para IE */
//document.onkeydown = ControlImpresion;
//document.onkeypress = ControlImpresion;
//document.oncontextmenu = ControlContexto;
//document.onselectstart = SelectStart;
document.ondragstart = DragStart;
window.onerror = ErrorWindow;

/* Devolver verdadero o falso de un objeto */
function IsNull(o)
{
    return ("undefined" == typeof(o) || "unknown" == typeof(o) || null == o)
}

/* Control de teclado, anular CTRL + P -> Imprimir */
function ControlImpresion()
{
    var event = window.event;
    if (event.ctrlKey && event.keyCode == 80)
    {
        event.keyCode = 0;
        return false;
    }
}

/* Para evitar el botón de la derecha, si no es un text, textarea, etc. */
function ControlContexto()
{
    var s = event.srcElement.tagName;
    event.returnValue =(!event.srcElement.disabled && (document.selection.createRange().text.length > 0 || s == "TEXTAREA" || s == "INPUT" && event.srcElement.type == "text"));
}

/* No permitir seleccionar si no es un texto o un textarea */
function SelectStart()
{
    var s = event.srcElement.tagName;
    if (s != "INPUT" && s != "TEXTAREA") event.returnValue = false;
}

/* No permitir Drags */
function DragStart()
{
    event.returnValue = false;
}

/* Marcar el objeto window siempre sin error */
function ErrorWindow()
{
    return false;
}

function openStdDlg(sPath, oArgs, iWidth, iHeight, bResizable, bModeless, sCustomWinParams)
{
    if (IsNull(sCustomWinParams))
    {
        sCustomWinParams = "";
    }

//    if ((!IsNull(bResizable) && bResizable))
//    {
//        iWidth	+= parseInt(LOCID_DIALOG_OFFSET_WIDTH, 10);
//        iHeight += parseInt(LOCID_DIALOG_OFFSET_HEIGHT, 10);
//    }

    iWidth	= getAdjustedWidthForScreen(iWidth);
    iHeight	= getAdjustedHeightForScreen(iHeight);
    bResizable = (!IsNull(bResizable) && bResizable) ? "yes" : "no";

    var iLeft = getLeftMargin(iWidth);
    var iTop = getTopMargin(iHeight);

    sCustomWinParams = "dialogWidth:" + iWidth + "px;dialogHeight:" + iHeight + "px;dialogLeft=" + iLeft + "px;dialogTop=" + iTop + "px;help:0;status:0;center:1;resizable:" + bResizable + ";" + sCustomWinParams;

    if (IsNull(bModeless) || !bModeless)
    {
        return window.showModalDialog(sPath, oArgs, sCustomWinParams);
    }
    else
    {
        return window.showModelessDialog(sPath, oArgs, sCustomWinParams);
    }
}

function OpenWindow(sPath,sNombre,sFeatures)
{
    window.open(sPath,sNombre,sFeatures);
    return false;
}

function BuscarCliente()
{
     return openStdDlg("ClientesBuscar.aspx?dlg=1",null,450,600,false,false);
}

function getAdjustedWidthForScreen(iX)
{
    var iWidth = iX;
    if (IsNull(iX) || iX == 0)
    {
        iX = (screen.availWidth - 80);
    }
    iX = (iX >= screen.availWidth) ? screen.availWidth : iX;
    return iX;
}

function getAdjustedHeightForScreen(iY)
{
    if (IsNull(iY) || iY == 0)
    {
        return (screen.availHeight - 80);
    }
    else
    {
        return (iY >= screen.availHeight) ? screen.availHeight : iY;
    }
}


function getLeftMargin(iX)
{
    var iLeft = 0;
    if ((screen.availWidth - iX) > 0)
    {
        iLeft = (screen.availWidth - iX)/2
    }
    return iLeft;
}

function getTopMargin(iY)
{
    var iTop = 0;
    if ((screen.availHeight - iY) > 0)
    {
        iTop = (screen.availHeight - iY)/2
    }
    return iTop;
    }
