﻿var products;
var timer = 0;
var timer2 = 0;
var timer4 = 0;
var timer5 = 0;
var idx = 0;
var popupStatus = 0;
var recent;
var chImgTid = 0;
var curProd;
var maxProd;
var prevProd;
function changeImage() {
   
    if (arguments.length > 0) {
        curProd = arguments[0];
        window.clearTimeout(chImgTid);
    }

    if (prevProd != 0) {
        $("#newProd_div" + prevProd).css({ "display": "none" });
        document.getElementById('newProdImg' + prevProd).src = "images/offdot.gif";
    }
    document.getElementById('newProdImg' + curProd).src = "images/reddot.gif";
    $("#newProd_div" + curProd).fadeIn("slow");

    prevProd = curProd;
    if (curProd == maxProd)
        curProd = 1;
    else
        curProd++;

    chImgTid = window.setTimeout(changeImage, 5000);
}
function startBlink(el) {
    window.blinker = setInterval(function() {
        if (window.blink) {
            $('#' + el).css('visibility', 'visible');
            window.blink=false; 
        }
        else {
            $('#' + el).css('visibility', 'hidden');
            window.blink=true; 

        }
    }, 750);
}
function openPopup(s) {
    if (popupStatus == 0) {

        $("#backgroundPopup").css({ "opacity": "0.7" });
        $("#backgroundPopup").fadeIn("slow");
        $(s).fadeIn("slow");
        $(s)[0].focus();
        popupStatus = 1;
    }
    return false;
}
function disablePopup(s) {
    //disables popup only if it is enabled   
    if (popupStatus == 1) {
        $("#backgroundPopup").fadeOut("slow");
        $(s).fadeOut("slow");
        popupStatus = 0;
    }
    return false;
}
function centerPopup(s) {
//    //request data for centering   
    var windowWidth = document.documentElement.clientWidth;
    var windowHeight = document.documentElement.clientHeight;
//    var popupHeight = $(s).height();
//    var popupWidth = $(s).width();
//    //alert($(s).scrollTop());
//    var newTop = windowHeight / 2 - popupHeight / 2);
//    //var newTop = ($(s).scrollTop() / 2 - popupHeight / 2);
//    var newLeft = windowWidth / 2 - popupWidth / 2;

//    //alert(newTop);
//    //alert(newLeft);
//    //centering
//    $(s).css({
//        "position": "absolute",
//        "top": newTop,
//        "left": newLeft
    //    }).bgIframe();
    $(s).bgIframe().centerInClient();

    //only need force for IE6   
    $("#backgroundPopup").css({
        "height": windowHeight,
        "width": windowWidth
    });
}
function clearField(obj) {
    return;
    var text = trimAll(obj.value).toLowerCase();
    if (text == "keyword" || text == "$") {
        obj.value = "";
        $("#" + obj.id).css({ color: "#111", 'font-style': "normal" });
    }
}
function setField(obj, type) {
    return;
    var text = trimAll(obj.value).toLowerCase();
    if (text == "") {
        if (type == "s")
            obj.value = "Keyword";
        else if (type == "d")
            obj.value = "$";
            
        $("#" + obj.id).css({color: "#AABBCC", 'font-style': "italic"});
    }
    else
        $("#" + obj.id).css({color: "#111", 'font-style': "normal"});
}
function leftTrim(sString) {
    while (sString.substring(0, 1) == ' ') {
        sString = sString.substring(1, sString.length);
    }
    return sString;
}
function rightTrim(sString) {
    while (sString.substring(sString.length - 1, sString.length) == ' ') {
        sString = sString.substring(0, sString.length - 1);
    }
    return sString;
}
function trimAll(sString) {
    while (sString.substring(0, 1) == ' ') {
        sString = sString.substring(1, sString.length);
    }
    while (sString.substring(sString.length - 1, sString.length) == ' ') {
        sString = sString.substring(0, sString.length - 1);
    }
    return sString;
}
function isNum(argvalue) {
    argvalue = argvalue.toString();

    if (argvalue.length == 0)
        return false;

    for (var n = 0; n < argvalue.length; n++)
        if (argvalue.substring(n, n + 1) < "0" || argvalue.substring(n, n + 1) > "9")
        return false;

    return true;
}
function loadRecent(arg1, arg2) {
    var rl = $("#recentlist");
    var al = $("a_recentlink");
    var id = Math.floor(Math.random() * 100000);

    if (arg1) {
        
        
        var sUrl = window.location.href;
        if (sUrl.toLowerCase().indexOf("/secure/", 0) > 0) 
            sUrl = "../getajaxresponse.aspx?q=recentlist&secure=1&";
        else
            sUrl = "getajaxresponse.aspx?q=recentlist&";

        $.get(sUrl + "id=" + id, function(data) {
           $("#recentitemstable").remove();
            rl.append(data);
        });
    }
    if (arg2) {
        rl.attr().css({ top: (arg2.pageY + 10), left: (arg2.pageX) });
    }

    if (arg1) {
        rl.fadeIn("slow");
        //$("#linkToContinue").css("visibility","hidden");
    }
    else {
        rl.fadeOut("slow");
        //$("#linkToContinue").css("visibility","visible");
    }
//    if (arg2) {
//        rl.attr().css({ top: (arg2.pageY + 10), left: (arg2.pageX) });
//    }
//    
//    if (arg1) {
//        rl.fadeIn("slow");
//        //$("#linkToContinue").css("visibility","hidden");
//    }
//    else {
//        rl.fadeOut("slow");
//        //$("#linkToContinue").css("visibility","visible");
//    }
}
$.fn.centerInClient = function(options) {
    /// <summary>Centers the selected items in the browser window. Takes into account scroll position.
    /// Ideally the selected set should only match a single element.
    /// </summary>    
    /// <param name="fn" type="Function">Optional function called when centering is complete. Passed DOM element as parameter</param>    
    /// <param name="forceAbsolute" type="Boolean">if true forces the element to be removed from the document flow 
    ///  and attached to the body element to ensure proper absolute positioning. 
    /// Be aware that this may cause ID hierachy for CSS styles to be affected.
    /// </param>
    /// <returns type="jQuery" />
    var opt = { forceAbsolute: false,
                container: window,    // selector of element to center in
                completeHandler: null
              };
    $.extend(opt, options);
   
    return this.each(function(i) {
        var el = $(this);
        var jWin = $(opt.container);
        var isWin = opt.container == window;

        // force to the top of document to ENSURE that 
        // document absolute positioning is available
        if (opt.forceAbsolute) {
            if (isWin)
                el.remove().appendTo("body");
            else
                el.remove().appendTo(jWin.get(0));
        }

        // have to make absolute
        el.css("position", "absolute");

        // height is off a bit so fudge it
        var heightFudge = isWin ? 2.0 : 1.8;

        var x = (isWin ? jWin.width() : jWin.outerWidth()) / 2 - el.outerWidth() / 2;
        var y = (isWin ? jWin.height() : jWin.outerHeight()) / heightFudge - el.outerHeight() / 2;

        el.css("left", x + jWin.scrollLeft());
        el.css("top", y + jWin.scrollTop());

        // if specified make callback and pass element
        if (opt.completeHandler)
            opt.completeHandler(this);
    });
}
function loadFly() {
    $('#tmpDiv').remove();
    products.find('tr.active').removeClass('active');
    var activeEl = products.find('tr#' + idx);
    var windowHeight = document.documentElement.clientHeight;
    products.find('tr#' + idx).addClass('active');
    var divEl = $('#div_' + idx);
    var tallest = divEl.find('.col:eq(0)').height();

    divEl.find('.col').each(function(i) {
    if ($(this).height() > tallest) {
           
            tallest = $(this).height();
        }
    });
    
    //_this.find('.col').height(tallest);
    
    //alert(divEl.offset().height);
    var div = $('<div/>').attr({ id: 'tmpDiv' }).css({
        position: 'absolute',
        display: 'none',
        float: 'left',
        top: (activeEl.offset().top-24),
        left: (products.offset().left + products.width()),
        zIndex: 1000
    }).appendTo('body').hover(function() {
        clearTimeout(timer2);
        clearTimeout(timer4);
        clearTimeout(timer5);
    }, function() {
        timer2 = setTimeout(function() {
            $('#tmpDiv').fadeOut(300, function() { $(this).remove() });
            if ($('body').is(':not(#jeveghome)')) {
                products.fadeOut(300).find('tr.active').removeClass('active');
            } else {
                products.find('tr.active').removeClass('active');
            }
        }, 800);
    });
    //div.append($('#div_' + idx).bgIframe().clone()).show();
    div.append($('#div_' + idx).bgIframe().clone()).show(10, function() {
        var _this = $('#tmpDiv');
        var tallest = _this.find('.col:eq(0)').height();
        _this.find('.col').each(function(i) {
            if ($(this).height() > tallest) {
                tallest = $(this).height();
            }
        });
        var top = _this.offset().top;
        _this.find('.col').height(tallest);
        if (top + tallest + 40 > windowHeight) //adjust for popup header & footer
        {
            //see if popup can be adjusted without moving to the top
            var scrollPos = $(window).scrollTop();
            var newTop = windowHeight - (tallest + 40);
            var pageTop = scrollPos == 0 ? products.offset().top : top;
            if (newTop >= pageTop)
                _this.css({ top: newTop });
            else
                _this.css({ top: pageTop });
        }

    });		

}
$(document).ready(function() {
    var recent = $('#a_recentlink')
    products = $('#menu')
    //alert(products.children().children().length);
    products.children().children().hover(function() {
        clearTimeout(timer4)
        idx = $(this).attr('id');
        $(this).addClass('hover');
        //alert((this).id);
        var tmpDiv = $('#tmpDiv');
        timer = setTimeout(loadFly, 400);
    }, function() {
        $(this).removeClass('hover');
        clearTimeout(timer);
    });
    products.hover(function() {
        clearTimeout(timer2);
    }, function() {
        $('#tmpDiv').mouseout();
        timer5 = setTimeout(function() {
            $('#tmpDiv').fadeOut(300, function() { $(this).remove() });
            if ($('body').is(':not(#jeveghome)')) {
                products.fadeOut(300).find('tr.active').removeClass('active');
            } else {
                products.find('tr.active').removeClass('active');
            }
        }, 500);
    });
    recent.click(function(e) {
        loadRecent(true, e);
        return false;
    });
});

//credit card functions
//*-------------------------------------------------------------------*//
var Cards = new makeArray(5);
Cards[0] = new CardType("M", "51,52,53,54,55", "16");
var MasterCard = Cards[0];
Cards[1] = new CardType("V", "4", "13,16");
var VisaCard = Cards[1];
Cards[3] = new CardType("I", "30,36,38", "14");
var DinersClubCard = Cards[2];
Cards[4] = new CardType("D", "60,61,62", "16");
var DiscoverCard = Cards[3];
Cards[2] = new CardType("A", "34,37", "15");
var AmExCard = Cards[4];
//*-------------------------------------------------------------------*//
function CardType() {
            var n;
            var argv = CardType.arguments;
            var argc = CardType.arguments.length;

            this.objname = "object CardType";

            var tmpcardtype = (argc > 0) ? argv[0] : "CardObject";
            var tmprules = (argc > 1) ? argv[1] : "0,1,2,3,4,5,6,7,8,9";
            var tmplen = (argc > 2) ? argv[2] : "13,14,15,16,19";

            this.setCardNumber = setCardNumber;  // set CardNumber method.
            this.setCardType = setCardType;  // setCardType method.
            this.setLen = setLen;  // setLen method.
            this.setRules = setRules;  // setRules method.
            this.setExpiryDate = setExpiryDate;  // setExpiryDate method.

            this.setCardType(tmpcardtype);
            this.setLen(tmplen);
            this.setRules(tmprules);
            if (argc > 4)
                this.setExpiryDate(argv[3], argv[4]);

            this.checkCardNumber = checkCardNumber;  // checkCardNumber method.
            this.getExpiryDate = getExpiryDate;  // getExpiryDate method.
            this.getCardType = getCardType;  // getCardType method.
            this.isCardNumber = isCardNumber;  // isCardNumber method.
            this.isExpiryDate = isExpiryDate;  // isExpiryDate method.
            //this.luhnCheck = luhnCheck; // luhnCheck method.
            return this;
        }

        //*-------------------------------------------------------------------*//
        function checkCardNumber() {
            var argv = checkCardNumber.arguments;
            var argc = checkCardNumber.arguments.length;
            var cardnumber = (argc > 0) ? argv[0] : this.txtCardNumber;
            var year = (argc > 1) ? argv[1] : document.getElementById('ddlYears').value;
            var month = (argc > 2) ? argv[2] : document.getElementById('ddlMonths').value;
            this.setCardNumber(cardnumber);
            this.setExpiryDate(year, month);

            if (!this.isCardNumber())
                return false;
            if (!this.isExpiryDate())
                return false;

            return true;
        }
        //*-------------------------------------------------------------------*//
        function getCardType() {
            return this.cardtype;
        }
        function getExpiryDate() {
            return this.month + "/" + this.year;
        }
        function isCardNumber() {
            var argv = isCardNumber.arguments;
            var argc = isCardNumber.arguments.length;
            var cardnumber = (argc > 0) ? argv[0] : this.cardnumber;

            for (var n = 0; n < this.len.size; n++)
                if (cardnumber.toString().length == this.len[n]) {
                for (var m = 0; m < this.rules.size; m++) {
                    var headdigit = cardnumber.substring(0, this.rules[m].toString().length);
                    if (headdigit == this.rules[m])
                        return true;
                }
                return false;
            }
            return false;
        }
        function isExpiryDate() {
            var argv = isExpiryDate.arguments;
            var argc = isExpiryDate.arguments.length;

            year = argc > 0 ? argv[0] : this.year;
            month = argc > 1 ? argv[1] : this.month;

            if (!isNum(year + ""))
                return false;
            if (!isNum(month + ""))
                return false;
            today = new Date();
            expiry = new Date(year, month);
            if (today.getTime() > expiry.getTime())
                return false;
            else
                return true;
        }
        function makeArray(size) {
            this.size = size;
            return this;
        }
        function setCardNumber(cardnumber) {
            this.cardnumber = cardnumber;
            return this;
        }
        function setCardType(cardtype) {
            this.cardtype = cardtype;
            return this;
        }
        function setExpiryDate(year, month) {
            this.year = year;
            this.month = month;
            return this;
        }
        function setLen(len) {
            // Create the len array.
            if (len.length == 0 || len == null)
                len = "13,14,15,16,19";

            var tmplen = len;
            n = 1;
            while (tmplen.indexOf(",") != -1) {
                tmplen = tmplen.substring(tmplen.indexOf(",") + 1, tmplen.length);
                n++;
            }
            this.len = new makeArray(n);
            n = 0;
            while (len.indexOf(",") != -1) {
                var tmpstr = len.substring(0, len.indexOf(","));
                this.len[n] = tmpstr;
                len = len.substring(len.indexOf(",") + 1, len.length);
                n++;
            }
            this.len[n] = len;
            return this;

        }

        function setRules(rules) {
            // Create the rules array.
            if (rules.length == 0 || rules == null)
                rules = "0,1,2,3,4,5,6,7,8,9";

            var tmprules = rules;
            n = 1;
            while (tmprules.indexOf(",") != -1) {
                tmprules = tmprules.substring(tmprules.indexOf(",") + 1, tmprules.length);
                n++;
            }
            this.rules = new makeArray(n);
            n = 0;
            while (rules.indexOf(",") != -1) {
                var tmpstr = rules.substring(0, rules.indexOf(","));
                this.rules[n] = tmpstr;
                rules = rules.substring(rules.indexOf(",") + 1, rules.length);
                n++;
            }
            this.rules[n] = rules;
            return this;
        }
//end of credit card functions
