﻿var scrollUpdateList = new Array();

$(document).ready(function() {

    //solution for IE6 checkmarks and radio buttons
    var badBrowser = (/MSIE ((5\.5)|6)/.test(navigator.userAgent) && navigator.platform == "Win32");
    if (badBrowser) {
        $("input[type=checkbox]").css({
            'height': '13px',
            'width': '13px',
            'padding': '0',
            'margin': '0 1px 0 0',
            'vertical-align': 'middle',
            'float': 'left'
        });
    }

    //solution for mac firefox checkmark boxes
    var macFirefox = (/Firefox\/(2|3\.5)/.test(navigator.userAgent) && navigator.platform == "MacIntel" || navigator.platform == "MacPPC");
    if (macFirefox) {
        $("input[type=checkbox]").css({
            'vertical-align': 'baseline'
        });

        $(".grey-sidebar-inside-list table td label").css({
            'margin-top': '2px'
        });
    }

    var macFirefoxThree = (/Firefox\/(3)/.test(navigator.userAgent) && navigator.platform == "MacIntel" || navigator.platform == "MacPPC");
    if (macFirefoxThree) {
        $("input[type=checkbox]").css({
            'vertical-align': 'baseline'
        });

        $(".grey-sidebar-inside-list table td label").css({
            'margin-top': '3px'
        });
    }
    
    // When using both JQuery and ASP.NET AJAX, need to re-register the hover effect so it will work after a partial page postback.
    $(function() {
        Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(BeginRequestHandler);
        function BeginRequestHandler(sender, args) {
            for (x in scrollUpdateList) {
                control = document.getElementById(x);
                if (control != null) {
                    scrollUpdateList[x] = control.scrollTop;
                }
            }
        }

        Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
        function EndRequestHandler(sender, args) {

            if ($('.random-deals span:nth-child(4n)').length > 0) $('.random-deals span:nth-child(4n)').addClass('random-last-deal');

            if ($('.text-ellipsis').length > 0) $('.text-ellipsis').truncate({ max_length: 128 });
            
            for (x in scrollUpdateList) {
                control = document.getElementById(x);
                if (control != null) {
                    control.scrollTop = scrollUpdateList[x];
                }
            }

            //solution for mac firefox checkmark boxes
            var macFirefox = (/Firefox\/(2|3\.5)/.test(navigator.userAgent) && navigator.platform == "MacIntel" || navigator.platform == "MacPPC");
            if (macFirefox) {
                $("input[type=checkbox]").css({
                    'vertical-align': 'baseline'
                });

                $(".grey-sidebar-inside-list table td label").css({
                    'margin-top': '2px'
                });
            }

            var macFirefoxThree = (/Firefox\/(3)/.test(navigator.userAgent) && navigator.platform == "MacIntel" || navigator.platform == "MacPPC");
            if (macFirefoxThree) {
                $("input[type=checkbox]").css({
                    'vertical-align': 'baseline'
                });

                $(".grey-sidebar-inside-list table td label").css({
                    'margin-top': '3px'
                });
            }
        }
    });


    if ($('.random-deals span:nth-child(4n)').length > 0) $('.random-deals span:nth-child(4n)').addClass('random-last-deal');

    //$('.valued-vendors-container span:nth-child(4n)').addClass('valued-vendors-last');
    //$('.valued-vendors-container span:first-child').addClass('valued-vendors-first');
    //$('.valued-vendors-container span:nth-child(5)').addClass('valued-vendors-first');
    //$('.valued-vendors-container span:nth-child(9)').addClass('valued-vendors-first');


    // Capture Default Backspace Key
    if (typeof window.event != 'undefined') {
        document.onkeydown = function() {
            if (event.srcElement.tagName.toUpperCase() != 'TEXTAREA' && (event.srcElement.tagName.toUpperCase() != 'INPUT' || event.srcElement.type.toUpperCase() != 'TEXT') && (event.srcElement.tagName.toUpperCase() != 'INPUT' || event.srcElement.type.toUpperCase() != 'PASSWORD'))
                return (event.keyCode != 8);
        }
    } else {
        document.onkeypress = function(e) {
            if (e.target.nodeName.toUpperCase() != 'TEXTAREA' && (e.target.nodeName.toUpperCase() != 'INPUT' || e.target.type.toUpperCase() != 'TEXT') && (e.target.nodeName.toUpperCase() != 'INPUT' || e.target.type.toUpperCase() != 'PASSWORD'))
                return (e.which != 8);
        }
    }

    // Capture Enter Key on all except login & input controls
    if (window.event != 'undefined') {
        document.onkeydown = function() {
            if (event.srcElement.tagName.toUpperCase() != 'TEXTAREA' && (event.srcElement.tagName.toUpperCase() != 'INPUT' || event.srcElement.type.toUpperCase() != 'TEXT') && (event.srcElement.tagName.toUpperCase() != 'INPUT' || event.srcElement.type.toUpperCase() != 'PASSWORD')) {
                if (event.which || event.keyCode) {
                    if (event.which == '13' || event.keyCode == '13') {
                        return false;
                    }
                }
            }
        }
    }

});

function vCenterDialog(dlg)
{
    var pos = {
        sTop: function()
        {
            return window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop;
        },
        wHeight: function()
        {
            if ($.browser.safari)
            {
                return window.innerHeight;
            }
            else
            {
                return document.documentElement.clientHeight || document.body.clientHeight;
            }
        }
    };
    dlg.parent().css("top", (pos.sTop() + (pos.wHeight() / 2) - (dlg.height() / 2)));
}

function DetectEnter(e, obj, control)
{

    var pK = window.event ? window.event.keyCode : e.which;

    if (pK == 13)
    {
        
        if (window.event)
        {
            window.event.cancelBubble = true;
            window.event.returnValue = false;
        }
        else
        {
            e.returnValue = false;
            e.cancel = true;
            e.stopPropagation();
            e.preventDefault();
        }
        
        if (obj.value.length > 0)
        {
            $('#' + control).trigger("click");
        }
        return false;
    }
}

function SubmitLogin(action, uControl, pControl)
{
    var dynoForm = document.createElement("form");
    var ufield = document.createElement("input");
    var pfield = document.createElement("input");

    if ($('#' + uControl).val() == "" || $('#' + pControl).val() == "")
    {
//        var joinPopin = $("#dlgJoinNow")
//        if (joinPopin && joinPopin.dialog('isOpen'))
//        {
//            joinPopin.dialog('close');
//        }
        DisplayInfo('Please enter a user name and a password to log in.');
        return;
    }
    
    dynoForm.setAttribute("id", "pchFL");
    dynoForm.setAttribute("method", "post");
    dynoForm.setAttribute("action", action);

    ufield.setAttribute("type", "text");
    ufield.setAttribute("id", "loginname");
    ufield.setAttribute("name", "loginname");
    ufield.setAttribute("value", $('#' + uControl).val());

    pfield.setAttribute("type", "password");
    pfield.setAttribute("id", "password");
    pfield.setAttribute("name", "password");
    pfield.setAttribute("value", $('#' + pControl).val());

    dynoForm.appendChild(ufield);
    dynoForm.appendChild(pfield);
    document.body.appendChild(dynoForm);

    dynoForm.submit();
    return false;
}