function LMSQueryString(variable) {
    var query = window.location.search.substring(1);
    var vars = query.split("&");
    for (var i = 0; i < vars.length; i++) {
        var pair = vars[i].split("=");
        var uPair = pair[0].toUpperCase();
        var uVariable = variable.toUpperCase();
        //if (pair[0].toUpperCase() == variable.toUpperCase()) {
        if (uPair == uVariable) {
            return unescape(pair[1]);
        }
    }
    return '';
}

function LMSReplaceSingleQuotes(stringCheck) {
    var s = new String(stringCheck);
    return s.replace(/'/g, "''");
}

function LMSOpenLogistics(ILT) {
    window.open('/catalog/display.logistics.aspx?Resourceilt=' + ILT, '', 'top=20,left=20,width=400,height=600,toolbar=0,location=0,resizable=1');
}

function LMSOpenHelp(HelpFile) {
    // Asked to open "Help" window in "real" IE popup -- so that user can view help while using the page,
    // rather thatn thru modal RadWindow.

    var win = window.open(HelpFile, "", "width=600,height=500,toolbar=no,location=no,directories=no,status=no,menubar=no, scrollbars=yes,resizable=yes,copyhistory=no");
    win.focus();

}

function LMSOpenRadWindow(URL, height, width, x, y, modal, behavior, title) {
    var oWindow;
    var oManager;
    var useMSAjax = false;
    
    try {
        if (wdwAjax == true)
            useMSAjax = true;
    } catch (e) { }
    
    if (useMSAjax) {
        try {
            oManager = GetRadWindowManager();
        } catch (e) {
            oManager = LMSGetRadWindow().BrowserWindow.GetRadWindowManager();
        }

        if (behavior == 'Close') {
            oWindow = oManager.getWindowByName('WdwClose');
        }
        else if (behavior == 'ClosePostBack') {
            oWindow = oManager.getWindowByName('WdwClosePostBack');
        }
        else if (behavior == 'Resize') {
            oWindow = oManager.getWindowByName('WdwResize');
        }
        else
            oWindow = radopen(null, null);
        
        if (modal)
            oWindow.set_modal(modal);

        oWindow.setUrl(URL);
        oWindow.setSize(width, height);

        if (!modal)
            oWindow.moveTo(x, y);

        if (title != '' && title != undefined)
            oWindow.set_title(title);

        oWindow.show();
    } else {
        try {
            oManager = GetRadWindowManager();
        } catch (e) {
            oManager = LMSGetRadWindow().BrowserWindow.GetRadWindowManager();
        }
        if (behavior == 'Close') {
            oWindow = oManager.GetWindows()[0];
            oWindow.Show();
            try {
                oWindow.SetUrl(URL);
            } catch (e) { }
        }
        else if (behavior == 'ClosePostBack') {
            oWindow = oManager.GetWindows()[1];
            oWindow.Show();
            try {
                oWindow.SetUrl(URL);
            } catch (e) { }
        }
        else if (behavior == 'Resize') {
            oWindow = oManager.GetWindows()[2];
            oWindow.Show();
            try {
                oWindow.SetUrl(URL);
            } catch (e) { }
        }
        else
            oWindow = window.radopen(URL, null);

        try {
            oWindow.SetSize(width, height);
        } catch (e) { }

        if (modal)// && behavior != 'Resize') 
            oWindow.SetModal(modal);
        else
            oWindow.MoveTo(x, y);

        if (title != '')
            oWindow.SetTitle(title);
    }
}

function LMSGetRadWindow() {
    var oWindow = null;
    if (window.radWindow) oWindow = window.radWindow;
    else if (window.frameElement.radWindow) oWindow = window.frameElement.radWindow;
    return oWindow;
}

function LMSCloseRadWindow() {
    var oWindow = LMSGetRadWindow();
    oWindow.Close();
}

function LMSCloseRadWindowPostBack(radWindow) {
    radWindow.BrowserWindow.__doPostBack('', 'LMSCloseRadWindowPostBack');
    radWindow.Close();
}

var borderStyle;
function LMSHighlight(control) {
    borderStyle = document.getElementById(control).style.border;
    document.getElementById(control).style.border = '3px solid yellow';
}

function LMSHighlightOff(control) {
    document.getElementById(control).style.border = borderStyle;
}

function LMSEditControl(control, url) {
    event.returnValue = false;
    LMSOpenRadWindow('/admin/admin.pages.aspx?control=' + control + '&page=' + url, 250, 350, 0, 0, true, 'Close', '');
}

function LMSEditCustomText(id, label, url) {
    if (label != '') {
        LMSOpenRadWindow('/admin/admin.pages.aspx?label=' + label + '&page=' + url, 600, 800, 0, 0, true, 'Close', '');
    }
    else {
        LMSOpenRadWindow('/admin/admin.pages.aspx?id=' + id + '&page=' + url, 600, 800, 0, 0, true, 'Close', 'Edit Custom Text');
    }

}

function LMSReadCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
}

function LMSCreateCookie(name, value, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    }
    else var expires = "";
    document.cookie = name + "=" + value + expires + "; path=/";
}

function LMSGrayOut(vis, HTML, options) {
    // Pass true to gray out screen, false to ungray
    // options are optional.  This is a JSON object with the following (optional) properties
    // opacity:0-100         // Lower number = less grayout higher = more of a blackout 
    // zindex: #             // HTML elements with a higher zindex appear on top of the gray out
    // bgcolor: (#xxxxxx)    // Standard RGB Hex color code
    // grayOut(true, {'zindex':'50', 'bgcolor':'#0000FF', 'opacity':'70'});
    // Because options is JSON opacity/zindex/bgcolor are all optional and can appear
    // in any order.  Pass only the properties you need to set.
    var options = options || {};
    var zindex = options.zindex || 50;
    var opacity = options.opacity || 20;
    var opaque = (opacity / 100);
    var bgcolor = options.bgcolor || '#000000';
    var dark = document.getElementById('darkenScreenObject');
    var tbody = document.getElementsByTagName("body")[0];
    var load = document.getElementById('statusObject');
    if (!dark) {
        // The dark layer doesn't exist, it's never been created.  So we'll
        // create it here and apply some basic styles.
        // If you are getting errors in IE see: http://support.microsoft.com/default.aspx/kb/927917
        var tnode = document.createElement('div');           // Create the layer.
        tnode.style.position = 'absolute';                 // Position absolutely
        tnode.style.top = '0px';                           // In the top
        tnode.style.left = '0px';                          // Left corner of the page
        tnode.style.overflow = 'hidden';                   // Try to avoid making scroll bars            
        tnode.style.display = 'none';                      // Start out Hidden
        tnode.style.textAlign = 'center';
        tnode.id = 'darkenScreenObject';                   // Name it so we can find it later
        tbody.appendChild(tnode);                            // Add it to the web page
        dark = document.getElementById('darkenScreenObject');  // Get the object.
    }

    if (!load) {
        var load = document.createElement('div');
        load.style.position = 'absolute';
        load.style.left = '50%';
        load.style.top = '50%';
        load.style.width = '150px';
        load.style.padding = '10px';
        load.style.marginLeft = '-50px';
        load.style.marginTop = '-50px';
        load.style.backgroundColor = 'white';
        load.style.border = '1px solid black';
        load.style.textAlign = 'center';
        load.style.zIndex = zindex * 2;
        load.id = 'statusObject';
        tbody.appendChild(load);
    }

    if (vis) {
        // Calculate the page width and height 
        if (typeof (window.innerWidth) == 'number') {
            //Non-IE
            var pageWidth = window.innerWidth + 'px';
            var pageHeight = window.innerHeight + 'px';
        } else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
            //IE 6+ in 'standards compliant mode'
            var pageWidth = document.documentElement.clientWidth + 'px';
            var pageHeight = document.documentElement.clientHeight + 'px';
        } else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
            //IE 4 compatible
            var pageWidth = document.body.clientWidth + 'px';
            var pageHeight = document.body.clientHeight + 'px';
        } else {
            var pageWidth = '100%';
            var pageHeight = '100%';
        }

        //set the shader to cover the entire page and make it visible.
        dark.style.opacity = opaque;
        dark.style.MozOpacity = opaque;
        dark.style.filter = 'alpha(opacity=' + opacity + ')';
        dark.style.zIndex = zindex;
        dark.style.backgroundColor = bgcolor;
        dark.style.width = pageWidth;
        dark.style.height = pageHeight;
        dark.style.display = 'block';
        load.innerHTML = HTML;
        load.style.display = 'block';
    } else {
        dark.style.display = 'none';
        load.style.display = 'none';
    }
}

function LMSTextCounter(field, maxlimit) {
    if (field.value.length > maxlimit) // if too long...trim it!
    {
        //alert ("You have reached " + maxlimit + "-character limit.");
        field.value = field.value.substring(0, maxlimit);
    }
}


/**************************************************
* dom-drag.js
* 09.25.2001
* www.youngpup.net
* Script featured on Dynamic Drive (http://www.dynamicdrive.com) 12.08.2005
**************************************************
* 10.28.2001 - fixed minor bug where events
* sometimes fired off the handle, not the root.
**************************************************/

var Drag = {

    obj: null,

    init: function(o, oRoot, minX, maxX, minY, maxY, bSwapHorzRef, bSwapVertRef, fXMapper, fYMapper) {
        o.onmousedown = Drag.start;

        o.hmode = bSwapHorzRef ? false : true;
        o.vmode = bSwapVertRef ? false : true;

        o.root = oRoot && oRoot != null ? oRoot : o;

        if (o.hmode && isNaN(parseInt(o.root.style.left))) o.root.style.left = "0px";
        if (o.vmode && isNaN(parseInt(o.root.style.top))) o.root.style.top = "0px";
        if (!o.hmode && isNaN(parseInt(o.root.style.right))) o.root.style.right = "0px";
        if (!o.vmode && isNaN(parseInt(o.root.style.bottom))) o.root.style.bottom = "0px";

        o.minX = typeof minX != 'undefined' ? minX : null;
        o.minY = typeof minY != 'undefined' ? minY : null;
        o.maxX = typeof maxX != 'undefined' ? maxX : null;
        o.maxY = typeof maxY != 'undefined' ? maxY : null;

        o.xMapper = fXMapper ? fXMapper : null;
        o.yMapper = fYMapper ? fYMapper : null;

        o.root.onDragStart = new Function();
        o.root.onDragEnd = new Function();
        o.root.onDrag = new Function();
    },

    start: function(e) {
        var o = Drag.obj = this;
        e = Drag.fixE(e);
        var y = parseInt(o.vmode ? o.root.style.top : o.root.style.bottom);
        var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right);
        o.root.onDragStart(x, y);

        o.lastMouseX = e.clientX;
        o.lastMouseY = e.clientY;

        if (o.hmode) {
            if (o.minX != null) o.minMouseX = e.clientX - x + o.minX;
            if (o.maxX != null) o.maxMouseX = o.minMouseX + o.maxX - o.minX;
        } else {
            if (o.minX != null) o.maxMouseX = -o.minX + e.clientX + x;
            if (o.maxX != null) o.minMouseX = -o.maxX + e.clientX + x;
        }

        if (o.vmode) {
            if (o.minY != null) o.minMouseY = e.clientY - y + o.minY;
            if (o.maxY != null) o.maxMouseY = o.minMouseY + o.maxY - o.minY;
        } else {
            if (o.minY != null) o.maxMouseY = -o.minY + e.clientY + y;
            if (o.maxY != null) o.minMouseY = -o.maxY + e.clientY + y;
        }

        document.onmousemove = Drag.drag;
        document.onmouseup = Drag.end;

        return false;
    },

    drag: function(e) {
        e = Drag.fixE(e);
        var o = Drag.obj;

        var ey = e.clientY;
        var ex = e.clientX;
        var y = parseInt(o.vmode ? o.root.style.top : o.root.style.bottom);
        var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right);
        var nx, ny;

        if (o.minX != null) ex = o.hmode ? Math.max(ex, o.minMouseX) : Math.min(ex, o.maxMouseX);
        if (o.maxX != null) ex = o.hmode ? Math.min(ex, o.maxMouseX) : Math.max(ex, o.minMouseX);
        if (o.minY != null) ey = o.vmode ? Math.max(ey, o.minMouseY) : Math.min(ey, o.maxMouseY);
        if (o.maxY != null) ey = o.vmode ? Math.min(ey, o.maxMouseY) : Math.max(ey, o.minMouseY);

        nx = x + ((ex - o.lastMouseX) * (o.hmode ? 1 : -1));
        ny = y + ((ey - o.lastMouseY) * (o.vmode ? 1 : -1));

        if (o.xMapper) nx = o.xMapper(y)
        else if (o.yMapper) ny = o.yMapper(x)

        Drag.obj.root.style[o.hmode ? "left" : "right"] = nx + "px";
        Drag.obj.root.style[o.vmode ? "top" : "bottom"] = ny + "px";
        Drag.obj.lastMouseX = ex;
        Drag.obj.lastMouseY = ey;

        Drag.obj.root.onDrag(nx, ny);
        return false;
    },

    end: function() {
        document.onmousemove = null;
        document.onmouseup = null;
        Drag.obj.root.onDragEnd(parseInt(Drag.obj.root.style[Drag.obj.hmode ? "left" : "right"]),
									parseInt(Drag.obj.root.style[Drag.obj.vmode ? "top" : "bottom"]));
        Drag.obj = null;
    },

    fixE: function(e) {
        if (typeof e == 'undefined') e = window.event;
        if (typeof e.layerX == 'undefined') e.layerX = e.offsetX;
        if (typeof e.layerY == 'undefined') e.layerY = e.offsetY;
        return e;
    }
};
