function GetURLParam(name) {
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp(regexS);
  var results = regex.exec(window.location.href);
  return results == null ? "" : results[1];
}

function SetReadMode() {
   document.getElementById("menu").style.display = "none";
}

function popup(mylink, windowname, wndheight, wndwidth, top, left) {
    if (!window.focus) return;
    if (arguments.length < 3) wndheight = '370';
    if (arguments.length < 4) wndwidth = '600';
    if (arguments.length < 5) top = (screen.height / 2) - (wndheight / 2);
    if (arguments.length < 6) left = (screen.width / 2) - (wndwidth / 2);
    var href = (typeof(mylink) == 'string') ? mylink : mylink.href; 
    return window.open(href, windowname, 'top='+top+',left='+left+',width='+wndwidth+',height='+wndheight+',scrollbars=no,toolbar=no,menubar=no,resizable=no');
}

function swapImage(btnObj,sURL) {
    btnObj.src = sURL;
}

function formatCurrency(num, showCents) {
    num = num.toString().replace(/\$|\,/g,'');
    if (isNaN(num)) num = "0";
    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num * 100 + 0.50000000001);
    cents = num % 100;
    num = Math.floor(num / 100).toString();
    if (cents < 10) cents = "0" + cents;
    for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)
        num = num.substring(0, num.length - (4 * i + 3)) + ','+ num.substring(num.length - (4 * i + 3));
    return ((sign ? '' : '-') + '$' + num + (showCents ? ('.' + cents) : ''));
}

function setMoneyCounter() {
    var startDate = new Date(2009, 11, 22);
    var nowDateTimeCST = new Date();
    nowDateTimeCST.setMinutes(nowDateTimeCST.getMinutes() + nowDateTimeCST.getTimezoneOffset());
    var amount = (nowDateTimeCST.valueOf() - startDate.valueOf()) / 1000 / 3 * 2 + 13200000;
    function updateMoneyCounter() {
        amount = amount + 2;
        try {document.getElementById('spanCashAmount').textContent = "$" + Math.round(amount); } catch (err) { }
        try { document.getElementById('spanCashAmount').innerHTML = "$" + Math.round(amount); } catch (err) { }
        setTimeout(updateMoneyCounter, 3 * 1000);
    }
    updateMoneyCounter();
}

$(function () {
    // register default submit
    $('form input, form select').live('keypress', function (e) {
        if ($(this).parents('form').find('button[type=submit].default, input[type=submit].default').length <= 0)
            return true;
        if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
            $(this).parents('form').find('button[type=submit].default, input[type=submit].default').click();
            return false;
        } else {
            return true;
        }
    });
}); 

//**********************  CLIENT ERRORS LOGGING **********************************//

var base = $('head base').attr('href');
function logClientScriptPageError(err, url, line) {
    if (base && (window.errors || 0) < 10) {
        window.errors = (window.errors || 0) + 1;
        var message = 'line: ' + line + '; url=' + url + '; error: ' + err + '; platform: ' + navigator.platform + '; userAgent: ' + navigator.userAgent;
        $.post(base + 'logclienterror.aspx', { errorContent: encodeURIComponent(message), errorUrl: encodeURIComponent(document.location.href) });
    }
    if (window.defaultOnError) { window.defaultOnError(err, url, line); }
}
window.defaultOnError = window.onerror;
window.onerror = logClientScriptPageError;

//**********************  GOOGLE ANALYTICS **********************************//

function onHtmlScript() {
    if (document.location.host == "www.ibankup.com") {
        var _gaq = _gaq || [];
        _gaq.push(['_setAccount', 'UA-2708714-3']);
        _gaq.push(['_trackPageview']);

        (function() {
            var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
            ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
            var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
        })();
    }
}


$(function () {
    $.fn.extend({
        createToolTip: function () {
            displayedTip = null;
            focusedTip = null;
            tipCloseTimout = null;
            var spacing = 4;
            //if (!options.halign) align = option.align;
            var showToolTip = function (tip, owner) {
                var halign = tip.alignmentHorizontal;
                var valign = tip.alignmentVertical;
                var dy = 0;
                switch (valign.toLowerCase()) {
                    case "top": dy = -tip.outerHeight() - spacing; break;
                    case "bottom": dy = owner.outerHeight() + spacing; break;
                    default: dy = 0; break;
                }
                var dx = 0;
                switch (halign.toLowerCase()) {
                    case "left": dx = -spacing - tip.outerWidth(); break;
                    case "right": dx = owner.outerWidth() + spacing; break;
                    default: dx = 0; break;
                }
                var p = owner.position();
                tip.css({ "top": p.top + dy });
                tip.css({ "left": p.left + dx });
                tip.css({ "visibility": "visible" });
                tip.css({ "display": "none" });
                tip.fadeIn();
            }; //show
            var hideToolTip = function (tip) {
                tip.css({ "visibility": "hidden" });
            };

            return jQuery.map($(this), function (item) {
                var tip = $(item);
                tip.alignmentHorizontal = "right";
                tip.alignmentVertical = "middle";
                if (tip.attr("alignment")) {
                    var alignment = tip.attr("alignment").split(',');
                    if (alignment.length = 2) {
                        tip.alignmentHorizontal = alignment[0];
                        tip.alignmentVertical = alignment[1];
                    }
                }
                
                var owner = $('#' + tip.attr("owner"));
                var listenersNames = tip.attr("listeners").split(',');
                for (var i = 0; i < listenersNames.length; i++) { listenersNames[i] = jQuery.trim(listenersNames[i]); }
                $(listenersNames).each(function () {
                    var listener = $('#' + this);
                    listener.focus(function () {
                        if (displayedTip && displayedTip.attr("owner") == tip.attr("owner")) {
                            clearTimeout(tipCloseTimout);
                        }
                        if (focusedTip == null || focusedTip.attr("owner") != tip.attr("owner")) {
                            focusedTip = tip;
                            displayedTip = tip;
                            showToolTip(tip, owner);
                        }
                    }); //focus
                    listener.blur(function () {
                        tipCloseTimout = setTimeout(function () {
                            focusedTip = null;
                            displayedTip = null;
                            hideToolTip(tip);
                        }, 200);
                    });
                    listener.mouseover(function () {
                        if (displayedTip && displayedTip.attr("owner") == tip.attr("owner")) {
                            clearTimeout(tipCloseTimout);
                        }
                        if (displayedTip == null || displayedTip.attr("owner") != tip.attr("owner")) {
                            if (focusedTip != null)
                                hideToolTip(focusedTip);
                            displayedTip = tip;
                            showToolTip(tip, owner);
                        }
                        if (navigator.userAgent.match(/iPad/i) != null) (this).focus();
                    });
                    listener.mouseout(function () {
                        tipCloseTimout = setTimeout(function () {
                            if (focusedTip == null || focusedTip.attr("owner") != tip.attr("owner")) {
                                displayedTip = null;
                                hideToolTip(tip);
                                if (focusedTip != null) {
                                    displayedTip = focusedTip;
                                    showToolTip(focusedTip, $('#' + focusedTip.attr("owner")));
                                }
                            }
                        }, 200);
                    });
                }); //each
            });
        }
    });

    $.extend({
        externalDialog: function (p) {
            var dialog = $('<div id="' + p.dialogName + '" style="padding:0px;"><iframe style="width:100%;height:100%;" frameborder="0"></iframe></div>').appendTo('body');
            var pOpen = p.open;
            var pClose = p.close;
            p.open = function (event, ui) {
                if (pOpen) pOpen(event, ui);
                $("#" + p.dialogName + " iframe:first").attr("src", p.dialogSrc);
            };
            p.close = function (event, ui) {
                if (pClose) pClose(event, ui)
                $("#" + p.dialogName + " iframe:first").remove(); $("#" + p.dialogName).remove();
            };
            $("#" + p.dialogName).dialog(p);
        }
    });

    $('select,input[type="text"][readonly!="readonly"],input[type="password"][readonly!="readonly"],input[type="radio"][readonly!="readonly"],input[type="checkbox"][readonly!="readonly"],input[type="button"][readonly!="readonly"],input[type="submit"][readonly!="readonly"]')
        .focus(function () { $(this).addClass("focus"); })
        .blur(function () { $(this).removeClass("focus"); });


    $(document).ready(function () {
        $('div.tooltip').createToolTip();
    });
});
