﻿var existingErrorsLogged = new Array();

if (jQuery) {
    // redefine getScript to allow for caching
    $.getScript = function(url, callback, cache) {
        $.ajax({
            type: "GET",
            url: url,
            success: callback,
            dataType: "script",
            cache: cache
        });
    };

    $(function() {            
        window.onerror = function(message, fileLocation, lineNumber) {
            try {
                // postback error
                var error = escape(message);
                var scriptFilename = escape(IsUndefined(fileLocation) ? "" : fileLocation);
                var scriptLocation = escape(IsUndefined(lineNumber) ? "" : lineNumber);
                var pageName = escape(location.href);
                var browser = "Unknown";

                if ($.browser.safari)
                    browser = "Safari";

                if ($.browser.opera)
                    browser = "Opera";

                if ($.browser.msie)
                    browser = "IE";

                if ($.browser.mozilla)
                    browser = "Firefox";

                browser += $.browser.version;

                browser = escape(browser);                

                var jsonData = "{'error':'" + error + "','scriptFilename':'" + scriptFilename + "','scriptLocation':'" + scriptLocation + "','pageName':'" + pageName + "','browser':'" + browser + "'}";

                if (existingErrorsLogged.indexOf(jsonData) != -1) {
                    return;
                } else {
                    existingErrorsLogged.push(jsonData);
                }

                $.ajax({
                    type: "POST",
                    url: "/WebServices/Error.asmx/LogClientScriptException",
                    data: jsonData,
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function(msg) { }
                });
            }
            catch (e) {
                if ($.browser.mozilla) console.log("Unable to log exception: " + e);
            }

            return true;
        };
    });    
}

function NavigateToLink( returnVal, args )
{
    window.location.href = decodeURIComponent(args);
}

function checkEnter(event){
    return ((event && event.which && event.which == 13) || (event.keyCode && event.keyCode == 13));
}

function openExpedite() { 
    var w = window.open('/helperwin.aspx?Header=How does Expediting work?&Message=In short, expediting your order moves your order to the top of the processing queue.  Production time still may vary, but call our 800 number at the top of the website if you need a rough delivery estimate.  Remember also to upgrade your shipping to Next Day or 2nd Day Air, as this significantly shortens the time it takes to get your signs, as well.  The charge to expedite your order is based on the size of your order.&HeaderClass=h3class&MessageClass=Normal','myWin','width=250,height=200,toolbar=no,directories=no');
    w.focus();
}

function CloseAndRedirect( redirectLocation)
{
    window.location = redirectLocation[0];
}

var win = null;
function openPopupTopRight (pagename,winname,w,h)
{
    sw = screen.availWidth - w - 20;
    win = window.open(pagename,winname,'width='+w+',height='+h+',top=20,left='+sw+',scrollbars=1,resizable=1');
    if(win)
        win.focus();
}

function showShopAllMenu()
{
    (document.getElementById("shop-all-categories")).className = "shop-all-categories_hover";
    (document.getElementById("shop-all-categories-menu")).style.display = "block";
}

function hideShopAllMenu()
{
    (document.getElementById("shop-all-categories")).className = "shop-all-categories";
    (document.getElementById("shop-all-categories-menu")).style.display = "none";
}

function swapImage(img, file)
        {
            if (document.images)
                img.src = file;
        }

        function IsUndefined(obj)
        {
            return 'undefined' == typeof obj;
        }
        
        function noNumbers(e)
        {
            var keynum;
            var keychar;
            var numcheck;
            if(window.event) // IE
            {
              keynum = e.keyCode;
            }
            else if(e.which) // Netscape/Firefox/Opera
            {
              keynum = e.which;
            }
            keychar = String.fromCharCode(keynum);
            if(keychar == '\r')
                SearchBtnHandle.click();
            return true;
        }

        
        function ShowPopup(e, cliploc, top, left)
        {
            ghd = document.getElementById('MagnaImage');//.src = cliploc;
            //ghd = document.getElementById('GlassHolderDiv');
            ghd.src = cliploc;
            // Set popup to visible
            topPosition = document.body.scrollTop + document.documentElement.scrollTop + e.clientY;
            leftPosition = document.body.scrollLeft + document.documentElement.scrollLeft + e.clientX;
            if(top == 0 && left == 1)
            {
                ghd.style.top = (topPosition + 20) + "px";         
                ghd.style.left = (leftPosition + 20) + "px";
            }
            else if(top == 1 && left == 1)
            {
                ghd.style.top = ((topPosition - ghd.height) - 35) + "px";         
                ghd.style.left = (leftPosition + 20) + "px";
            }
            else if(top == 0 && left == 0) 
            {
                ghd.style.top = (topPosition + 20) + "px";        
                ghd.style.left = ((leftPosition - ghd.width) - 35) + "px";
            }
            else
            {
                ghd.style.top = ((topPosition - ghd.height) - 35) + "px";         
                ghd.style.left = ((leftPosition - ghd.width) - 35) + "px";
            }
            if(top == 1 && topPosition < 280)
            {
                topPosition = 280;
                ghd.style.top = ((topPosition - ghd.height) - 35) + "px";
            }
            if(top == 0 && topPosition > 170)
            {
                topPosition = 170;
                ghd.style.top = (topPosition + 20) + "px";
            }
            ghd.style.display = "block";
        }

        function KeepPopup(e, top, left)
        {
            ghd = document.getElementById('MagnaImage');
            clipMain = document.getElementById('TableClipMain');
            // Set popup to visible
            topPosition = document.body.scrollTop + document.documentElement.scrollTop + e.clientY - 246;
            leftPosition = document.body.scrollLeft + document.documentElement.scrollLeft + e.clientX - 575;
            if(top == 0 && left == 1)
            {
                ghd.style.top = (topPosition + 20) + "px";         
                ghd.style.left = (leftPosition + 20) + "px";
            }
            else if(top == 1 && left == 1)
            {
                ghd.style.top = ((topPosition - ghd.height) - 35) + "px";         
                ghd.style.left = (leftPosition + 20) + "px";
            }
            else if(top == 0 && left == 0)
            {
                ghd.style.top = (topPosition + 20) + "px";        
                ghd.style.left = ((leftPosition - ghd.width) - 35) + "px";
            }
            else
            {
                ghd.style.top = ((topPosition - ghd.height) - 35) + "px";         
                ghd.style.left = ((leftPosition - ghd.width) - 35) + "px";
            }
            if(top == 1 && topPosition < 135)
                ghd.style.top = (topPosition + 20) + "px";
            if(top == 1 && topPosition < 280)
            {
                topPosition = 280;
                ghd.style.top = ((topPosition - ghd.height) - 35) + "px";
            }
            if(top == 0 && topPosition > 130)
            {
                topPosition = 110;
                ghd.style.top = (topPosition + 20) + "px";
            }
            if(leftPosition > 240)
            {
                leftPosition = 220;
                ghd.style.left = (leftPosition + 20) + "px";
            }
            ghd.style.display = "block";         
        }
        
        function HidePopup()
        {
            hp = document.getElementById('MagnaImage');
            hp.style.display = "none";
        }
function increaseFileInputWidth(radUpload, args)
                            {
                              //If the Checkboxes in RadUpload are not visible, change the index to 0
                              var cell = args.Row.cells[1];
                              var inputs = cell.getElementsByTagName('INPUT');
                              for (var i=0; i<inputs.length; i++)
                              {
                                if (inputs[i].type == "file")
                                {
                                  //Set the size property according to your preference
                                  inputs[i].size = 10;
                                }
                              }
                            }

function showUnderline(obj) 
{
    obj.style.textDecoration = 'underline';
}

function hideUnderline(obj) 
{
    obj.style.textDecoration = 'none';
}

String.prototype.trim = function () {
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
}

function clearFieldIfEqual(obj, strMatch) {
    if (obj.value.trim() == strMatch)
        obj.value = "";
}

function setFieldIfBlank(obj, strMatch) {
    if (obj.value.trim() == "")
        obj.value = strMatch;
}

$(function() {
        if ($("div#freeOfferCrossSell").size() > 0) {
            $("#freeOfferCrossSell").load("/p/free-offer-cross-sell #freeoffercontent", function() {
                $("#freeOfferCrossSell").css("background-image", "none");
            });
        }
});
