﻿$j = $;

// String methods
String.prototype.trim = function (str) {
    var me = this.toString();
    if (!str) { str = " " };
    if (me.length) {
        while (me.length && (me.indexOf(str) == 0 || me.lastIndexOf(str) == me.length - str.length)) {
            if (me.indexOf(str) == 0) { me = me.substring(str.length) };
            if (me.lastIndexOf(str) == me.length - str.length) { me = me.substring(0, me.length - str.length) };
        };
    };
    return me;
};
String.prototype.lTrim = function (str) {
    var me = this.toString();
    if (!str) { str = " " };
    if (me.length) {
        while (me.length && me.indexOf(str) == 0) {
            if (me.indexOf(str) == 0) { me = me.substring(str.length) };
        };
    };
    return me;
};
String.prototype.rTrim = function (str) {
    var me = this.toString();
    if (!str) { str = " " };
    if (me.length) {
        while (me.length && me.lastIndexOf(str) == me.length - str.length) {
            if (me.lastIndexOf(str) == me.length - str.length) { me = me.substring(0, me.length - str.length) };
        };
    };
    return me;
};
String.prototype.toFloat = function () {
    return parseFloat(this);
};
String.prototype.toInt = function () {
    return parseInt(this);
};
String.prototype.contains = function (str) {
    if (this.indexOf(str) > -1) { return true };
    return false;
};
Number.prototype.toCurrency = function () {
    var i = parseFloat(this);
    if (isNaN(i)) { i = 0.00; }
    var minus = '';
    if (i < 0) { minus = '-'; }
    i = Math.abs(i);
    i = parseInt((i + .005) * 100);
    i = i / 100;
    s = new String(i);
    if (s.indexOf('.') < 0) { s += '.00'; }
    if (s.indexOf('.') == (s.length - 2)) { s += '0'; }
    s = minus + s;
    return parseFloat(s);
};
Array.prototype.clear = function () {
    var me = [];
    return me;
};
if (!Array.prototype.indexOf) {
    Array.prototype.indexOf = function (elt /*, from*/) {
        var len = this.length;

        var from = Number(arguments[1]) || 0;
        from = (from < 0)
         ? Math.ceil(from)
         : Math.floor(from);
        if (from < 0)
            from += len;

        for (; from < len; from++) {
            if (from in this &&
          this[from] === elt)
                return from;
        }
        return -1;
    };
}


// add jQuery timer function
if (typeof (jQuery) != "undefined") {
    jQuery.timer = function (interval, callback) {
        var interval = interval || 100;

        if (!callback)
            return false;

        _timer = function (interval, callback) {
            this.stop = function () {
                clearInterval(self.id);
            };
            this.internalCallback = function () {
                callback(self);
            };
            this.reset = function (val) {
                if (self.id)
                    clearInterval(self.id);

                var val = val || 100;
                this.id = setInterval(this.internalCallback, val);
            };
            this.interval = interval;
            this.id = setInterval(this.internalCallback, this.interval);
            var self = this;
        };
        return new _timer(interval, callback);
    };
};

// add tooltip functionality

$j(function () {
    $j(".helper").each(function () {
        $j(this).qtip({
            content: {
                text: "<img src='/images/design/16px_spinner.gif' alt='Loading...' class='icon' />Loading...",
                ajax: {
                    url: "/includes/tooltips.aspx",
                    type: "post",
                    data: { "snippet": $j(this).attr("snippet"), "addinfo": $j(this).attr("addinfo") }
                }
            },

            style: {
                tip: false,
                classes: "ui-tooltip-shadow ui-tooltip-rounded"
            },
            hide: { fixed: true, event: "mouseout" },
            show: { event: "click" },
            position: {
                my: "left top",
                at: "left center",
                adjust: { y: -20 }
            }
        });
    }).click(function () { return false; });
});


// check for live chat availability
function checkChat() {
    $j.ajax({
        type: "POST",
        url: "/includes/miscServices.asmx/CheckChatStatus",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data) {
            if (data.d == "True") { // online
                $j("#divTouchTabs div:eq(4)").show();
            } else { // offline
                $j("#divTouchTabs div:eq(4)").hide();
            }
        }
    });
};

$j(function () {
    var chatTab = $j("#divTouchTabs div:eq(4)").mouseover(function () {
        clearTimeout(chatTab.timer);
    });
    chatTab.timer = setTimeout(function () {
        chatTab.mouseover();
    }, 30000);

    checkChat();
    $j.timer(20000, function () {
        checkChat();
    });
});
//Mouseover phone and chat touch tabs.
$j(function() {
    $j("#divTouchTabs div").click(function(event) {
        if($j(this).find("a").length > 0) {
            window.open($j(this).find("a").attr("href"));
        };
    }).filter(":gt(2)").mouseover(function() {
        var diff = -140
        if (parseInt($j(this).css("margin-left").replace("px", "")) < 0) { diff = 0 };
        $j(this).animate({
            marginLeft: diff+"px"
        }, 800);
    });
});

function setCountry(elem, target) {
    $j("#" + target).val($j(elem).find(":selected").attr('cccode'));
};
function replicateAddress() {
    var shippingInfo = $j("#divShippingInfo").show();
    $j("#ctl00_phldrBody_chkSameShipping:checked").each(function () {
        shippingInfo.hide();
    });
};

// set up Login box
$j(function () {
    $j("#lnkLogin").qtip({
        id: "login",
        content: {
            text: $j("#divLogin"),
            title: "<img src='/images/design/16px_account.gif' class='icon' />My Account"
        },
        show: "click",
        hide: { fixed: true, event: "unfocus" },
        position: {
            my: "left top",
            at: "left top",
            container: $j("#aspnetForm div:eq(0)"),
            adjust: { y: -10, x: -11 }
        },
        style: {
            tip: false,
            classes: "ui-tooltip-shadow ui-tooltip-rounded ui-tooltip-light"
        }
    }).click(function () { return false; });

    //Set up field validation
    $j("span.validation-error").bind("DOMAttrModified propertychange", function (e) {
        // Exit early if IE because it throws this event lots more
        if (e.originalEvent.propertyName && e.originalEvent.propertyName != "isvalid") return;
        var controlToValidate = $j("#" + this.controltovalidate);
        var validators = controlToValidate.attr("Validators");
        if (validators == null) return;
        var isValid = true;
        $j(validators).each(function () {
            if (this.isvalid !== true) {
                isValid = false;
            }
        });
        if (isValid) {
            controlToValidate.parent().removeClass("invalid");
        } else {
            controlToValidate.parent().addClass("invalid");
        }
    });
});

function isNumberic(evt) {
    var charCode = (evt.which) ? evt.which : event.keyCode
    if (charCode > 31 && (charCode < 48 || charCode > 57))
        return false;
    return true;
};
function formatCurrency(amount) {
    var i = parseFloat(amount);
    if (isNaN(i)) { i = 0.00; }
    var minus = '';
    if (i < 0) { minus = '-'; }
    i = Math.abs(i);
    i = parseInt((i + .005) * 100);
    i = i / 100;
    s = new String(i);
    if (s.indexOf('.') < 0) { s += '.00'; }
    if (s.indexOf('.') == (s.length - 2)) { s += '0'; }
    s = minus + s;
    return s;
};




//        $j(function() {
//            var xmlDoc = document.createElement("xmlDoc");
//            var xmlInfo = document.createElement("userinfo");
//            xmlInfo.setAttribute("currentpage", window.location.href);
//            xmlInfo.setAttribute("pagetitle", document.title);
//            xmlDoc.appendChild(xmlInfo);
//            var xmlBrowser = document.createElement("browser")
//            xmlBrowser.setAttribute("name", navigator.appName);
//            xmlBrowser.setAttribute("version", navigator.appVersion);
//            xmlBrowser.setAttribute("platform", navigator.platform);
//            xmlBrowser.setAttribute("codename", navigator.appCodeName);
//            xmlInfo.appendChild(xmlBrowser);
//            var xmlScreen = document.createElement("screen");
//            xmlScreen.setAttribute("colordepth", window.screen.colorDepth);
//            xmlScreen.setAttribute("width", window.screen.width);
//            xmlScreen.setAttribute("height", window.screen.height);
//            xmlInfo.appendChild(xmlScreen);
//            $j.ajax({
//                type: "POST",
//                url: "/includes/miscServices.asmx/StartStats",
//                dataType: "xml",
//                data: {data: xmlDoc.innerHTML},
//                success: function() {
//                }
//            });
//        });
//    
//        $j(window).bind("beforeunload", function() {
//            $j.ajax({
//                type: "POST",
//                async: false,
//                url: "/includes/miscServices.asmx/EndStats",
//                dataType: "xml"
//            });
//        });


