/*
Combined:
osc-info-popup.js
osc-lightbox.js
osc-master.js
*/
/*
* osc-info-popup.js
*/
var tip = new Array();
var iTips = 0;

var altText = "Click here for information";
var infoIcon = "/_layouts/custom/jQuery/images/question-mark-icon.gif";

function closeTips(ref) {
    tip[ref].simpletip().close();
}

function InitialiseInfoPopupQW(obj) {

    if ($(obj).find("img").size() < 1) {
        $(obj).prepend('<img src="' + infoIcon + '" alt="' + altText + '" />')
    }

    var sContent = "<h3>" + $(obj).children("input[type=hidden]:eq(0)").val() + "</h3><br />";
    sContent += $(obj).children("input[type=hidden]:eq(1)").val() + "<br /><br />";
    sContent += "<a class='l3' style='text-decoration:underline' href='javascript:closeTips(" + iTips.toString() + ");' >Close</a>";

    tip[iTips] = $(obj).simpletip({
        modal: true,
        position: null,
        opener: $(obj).children("img"),
        fixed: true,
        hideEffect: 'none',
        onBeforeShow: function() {
            this.update(sContent);
            this.getTooltip().css("z-index", "10000");
            this.showModal();
        }
    });
    iTips++;
}

$(document).ready(function() {

    function SuccessGetInfoPopup(results, params) {
        if (params.base) {
            var currentTip = params.base;
            $("#header").css('z-index', '200');
            $("#content-body").css('z-index', '100');
            $(".password-info").css('z-index', '190');
            currentTip.update(results + currentTip.getCloseFn());
            currentTip.showModal();
        }
    }

    function FailureGetInfoPopup(message) {
        oLbox["lbox-generic"].setError('Unfortunately, an error occurred while trying to retrieve the information.');
        oLbox["lbox-generic"].update("append");
        oLbox["lbox-generic"].open();
    }

    $(".info-popup").each(function(i) {
        $(this).prepend('<img src="' + infoIcon + '" alt="' + altText + '" />')

        tip[iTips] = $(this).simpletip({
            modal: true,
            position: $(this).children("input[type=hidden]").val().split(';')[3],
            opener: $(this).children("img"),
            fixed: true,
            closeFn: "<br /><br /><a class='l3' style='text-decoration:underline' href='javascript:closeTips(" + iTips.toString() + ");' >Close</a>",
            param1: $(this).children("input[type=hidden]").val().split(';')[0],
            param2: $(this).children("input[type=hidden]").val().split(';')[1],
            param3: $(this).children("input[type=hidden]").val().split(';')[2],

            onBeforeShow: function() {
                Tower.OSC.MOSS.Forms.WebServices.OSCServices.GetInfoPopupHtml(this.getParam(1), this.getParam(2), this.getParam(3), SuccessGetInfoPopup, FailureGetInfoPopup, { base: this });
            },

            onBeforeHide: function() {
                $("#header").css('z-index', '0');
                $("#content-body").css('z-index', '0');
                $(".password-info").css('z-index', '0');
            }
        });

        iTips++;
    });

    $(".info-popup-qw").each(function() {
        if ($(this).parent().is(":visible") || $(this).parent().css("display") != "none") {
            InitialiseInfoPopupQW(this);
        }
    });

    var zIndexNumber = 100;

    $("#content-body div").each(function() {
        $(this).css("zIndex", zIndexNumber);
        zIndexNumber -= 1;
    });

});



/*
* osc-lightbox.js
*/
; (function() {

    /* === lightbox manager === */
    var defaults = {};

    $.OscLightboxMgr = function(config) {
        this.Init(config);
    };

    // shortcut alias + extending obj
    $olbm = $.OscLightboxMgr;
    $olbm.fn = $olbm.prototype = {};
    $olbm.fn.extend = $olbm.extend = $.extend;

    // obj methods
    $olbm.fn.extend({
        Init: function(config) {
            this.config = $.extend({}, defaults, config || {});

            jQuery.each(oLboxCfg, function(idx, config) {
                oLbox[idx] = new $.OscLightbox(config);
            });
        }
    });

    /* === lightbox === */

    var dlg_defaults = {
        id: '',
        dpo: 'autoOpen: false, modal: true, closeOnEscape: false, resizable: false',
        m: 60,
        w: 600,
        o: 'backgroundColor: "#000"',
        btns: '"Close": function() {$(this).dialog("close");}',
        ajax: false,
        prod: '',
        page: ''
    };

    $.OscLightbox = function(config) {
        this.Init(config);
    };

    // shortcut alias + extending obj
    $olb = $.OscLightbox;
    $olb.fn = $olb.prototype = {};
    $olb.fn.extend = $olb.extend = $.extend;

    $olb.fn.extend({
        Init: function(config) {
            this.config = $.extend({}, dlg_defaults, config || {});

            if (this.config.id != '') {
                eval('$("#' + this.config.id + '").dialog({' + this.config.dpo + ', margin: ' + this.config.m +
                    ', width: ' + this.config.w + ', overlay: { ' + this.config.o +
                    ' }, buttons: { ' + this.config.btns + ' }});');
            }
        },

        setError: function(error) {
            this.error = '<ul><li>' + error + '</li></ul>';

            if (this.content == null || this.content == '')
                this.content = $('#' + this.config.id).html();
        },

        update: function(type, head, desc, html) {
            var content = '';
            if (this.content == null)
                this.content = $('#' + this.config.id).html();

            if (type == null)
                type = "overwrite";

            if (head != null && head != '') {
                content += '<h6 class="h2">' + head + '</h6>\r\n';
            }

            if (type == "append")
                content += this.content;

            if (desc != null && desc != '') {
                content += '<p class="bc1">' + desc + '</p>\r\n';
            }

            if (html != null && html != '') {
                content += html;
            }

            if (this.error != null) {
                content += this.error;
            }

            if (type == "prepend")
                content += this.content;

            $('#' + this.config.id).html(content);
        },

        open: function() {
            UnlockScreen();
            if (this.config.ajax) {
                Tower.OSC.MOSS.Forms.WebServices.OSCServices.GetLightboxHtml(this.config.prod, this.config.page, this.config.id, this.WSCallSuccessful, this.WSCallFailed, { base: this });
            }
            else {
                $('#' + this.config.id).dialog('open');
            }
        },

        close: function() {
            $('#' + this.config.id).dialog('close');
        },

        callbackFn: null,

        WSCallSuccessful: function(results, params) {
            var divID = params.base.config.id;

            oLbox[divID].update("append", "", "", results);
            $('#' + divID).dialog('open');
        },

        WSCallFailed: function(message) {
            oLbox["lbox-generic"].setError('Unfortunately, an error occurred while retrieving lightbox content.');
            oLbox["lbox-generic"].update("append");
            oLbox["lbox-generic"].open();
        }
    });

})(jQuery);

/*
* osc-master.js
*/
$(document).ready(
	function() {
	    $('input').checkBox();
	    $('#password-input').hide();
	    $('#login-input').hide();
	}
)
function showRealPasswordInput() {
    $('#fake-password-input').hide();
    $('#password-input').show();
    $('.password-input:last').focus();
}
function showRealLoginInput() {
    $('#fake-login-input').hide();
    $('#login-input').show();
    $('.login-input:last').focus();
}
function checkLoginInput() {
    if ($("#txtUserName").val() == "") {
        $('#fake-login-input').show();
        $('#login-input').hide();
        $('.login-input:last').focus();
    }
}
function checkPasswordInput() {
    if ($("#txtPassword").val() == "") {
        $('#fake-password-input').show();
        $('#password-input').hide();
        $('.password-input:last').focus();
    }
}
function fnum(str, dcp) {
    var x = str.split('.');
    var x1 = x[0];
    var dc = '';
    var x2 = x[1] + '';
    for (var i = x2.length; i <= dcp; i++)
        dc += '0';

    var rgx = /(\d*)(\d{3})/;
    while (rgx.test(x1)) {
        x1 = x1.replace(rgx, '$1' + ',' + '$2');
    }
    return x1 + x2 + dc;
}

//
// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.org
//
function getPageScroll() {

    var yScroll;

    if (self.pageYOffset) {
        yScroll = self.pageYOffset;
    } else if (document.documentElement && document.documentElement.scrollTop) {	 // Explorer 6 Strict
        yScroll = document.documentElement.scrollTop;
    } else if (document.body) {// all other Explorers
        yScroll = document.body.scrollTop;
    }

    arrayPageScroll = new Array('', yScroll)
    return arrayPageScroll;
}

//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
function getPageSize() {

    var xScroll, yScroll;

    if (window.innerHeight && window.scrollMaxY) {
        xScroll = document.body.scrollWidth;
        yScroll = window.innerHeight + window.scrollMaxY;
    } else if (document.body.scrollHeight > document.body.offsetHeight) { // all but Explorer Mac
        xScroll = document.body.scrollWidth;
        yScroll = document.body.scrollHeight;
    } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
        xScroll = document.body.offsetWidth;
        yScroll = document.body.offsetHeight;
    }

    var windowWidth, windowHeight;
    if (self.innerHeight) {	// all except Explorer
        windowWidth = self.innerWidth;
        windowHeight = self.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
        windowWidth = document.documentElement.clientWidth;
        windowHeight = document.documentElement.clientHeight;
    } else if (document.body) { // other Explorers
        windowWidth = document.body.clientWidth;
        windowHeight = document.body.clientHeight;
    }

    // for small pages with total height less then height of the viewport
    if (yScroll < windowHeight) {
        pageHeight = windowHeight;
    } else {
        pageHeight = yScroll;
    }

    // for small pages with total width less then width of the viewport
    if (xScroll < windowWidth) {
        pageWidth = windowWidth;
    } else {
        pageWidth = xScroll;
    }

    arrayPageSize = new Array(pageWidth, pageHeight, windowWidth, windowHeight)
    return arrayPageSize;
}

function showPTC(message) {
    if (typeof message == 'undefined') message = 'Processing, please wait...';

    var objBody = document.getElementsByTagName("body").item(0);

    var objCurrentLoadingDiv = document.getElementById('loadingDiv');

    var arrayPageSize = getPageSize();
    var arrayPageScroll = getPageScroll();

    if (objCurrentLoadingDiv) {
        var LoadingDivTop = arrayPageScroll[1] + ((arrayPageSize[3] - 35 - parseInt(objCurrentLoadingDiv.style.height)) / 2);
        var LoadingDivLeft = ((arrayPageSize[0] - 20 - parseInt(objCurrentLoadingDiv.style.width)) / 2);

        objCurrentLoadingDiv.style.top = (LoadingDivTop < 0) ? "0px" : LoadingDivTop + "px";
        objCurrentLoadingDiv.style.left = (LoadingDivLeft < 0) ? "0px" : LoadingDivLeft + "px";

        objCurrentLoadingDiv.style.display = "block";
    }
    else {
        var objLoadingImage = document.createElement("img");
        objLoadingImage.src = '/_layouts/Custom/TowerOSC/images/page-loader.gif';
        objLoadingImage.setAttribute('id', 'loadingImage');

        var objTextDiv = document.createElement("div");
        objTextDiv.setAttribute('id', 'loadingDivText');
        objTextDiv.innerHTML = message;

        var objLoadingDiv = document.createElement("div");
        objLoadingDiv.setAttribute('id', 'loadingDiv');
        objLoadingDiv.style.width = "350px";
        objLoadingDiv.style.height = "150px";
        objLoadingDiv.style.backgroundColor = "#ffffff";

        // centre div
        var LoadingDivTop = arrayPageScroll[1] + ((arrayPageSize[3] - 35 - parseInt(objLoadingDiv.style.height)) / 2);
        var LoadingDivLeft = ((arrayPageSize[0] - 20 - parseInt(objLoadingDiv.style.width)) / 2);
        objLoadingDiv.style.top = (LoadingDivTop < 0) ? "0px" : LoadingDivTop + "px";
        objLoadingDiv.style.left = (LoadingDivLeft < 0) ? "0px" : LoadingDivLeft + "px";

        objLoadingDiv.insertBefore(objLoadingImage, objLoadingDiv.firstChild);
        objLoadingDiv.insertBefore(objTextDiv, objLoadingDiv.firstChild);
        objBody.insertBefore(objLoadingDiv, objBody.firstChild);

        objLoadingDiv.style.display = "block";
    }
}

function hidePTC() {
    var objLoadingDiv = document.getElementById('loadingDiv');

    if (objLoadingDiv) {
        objLoadingDiv.style.display = "none";
    }
}

function LockScreen(displayPTC, message) {

    var lockPane = $("#lockPane");

    if (typeof displayPTC == 'undefined') displayPTC = true;
    if (typeof message == 'undefined') message = 'Processing, please wait...';

    if (lockPane) {
        lockPane.css("width", ($(window)[0].document.documentElement.scrollWidth) + "px");
        lockPane.css("height", ($(window)[0].document.documentElement.scrollHeight) + "px");
        lockPane.css("display", "");
        lockPane.css("cursor", "wait");
        lockPane.css("z-index", "999997");
    }

    if (displayPTC) {
        showPTC(message);
    }
}

function UnlockScreen() {
    var lockPane = $("#lockPane");
    if (lockPane) {
        lockPane.css("display", "none");
        lockPane.css("cursor", "default");
        lockPane.css("z-index", "-1");
    }
    hidePTC();
}

function CurrencyIntegerOnly_KeyPress(e) {
    if (!((e.which >= 48 && e.which <= 57) || e.which == 44 || e.which == 32 || e.which == 8 || e.which == 0)) {
        if (!e) {
            var e = window.event;
            e.returnValue = false;
            e.cancelBubble = true;
        }
        if (e.stopPropagation) {
            e.stopPropagation();
            e.preventDefault();
        }
    }
    else {
        if (e.target.value == "") {
            e.target.value = "$ ";
        }
        else if (document.selection) {
            var selectedText = document.selection.createRange().text;
            document.selection.clear();
            if (selectedText.indexOf("$ ") > -1) {
                e.target.value = "$ ";
            }
            else if (selectedText.indexOf("$") > -1) {
                e.target.value = "$ ";
            }
        }
    }

}

function FormatNumberThousands(number) {
    var value = number;
    var reg = /(-?\d+)(\d{3})/;
    while (reg.test(value)) {
        value = value.replace(reg, "$1,$2");
    }
    return value;
}

function MobilePhoneNumber_KeyPress(e) {
    window.status = e.which;
    if (!((e.which >= 48 && e.which <= 57) || e.which == 32 || e.which == 8 || e.which == 0)) {
        if (e.stopPropagation) {
            e.stopPropagation();
            e.preventDefault();
        }
    }
    else {
        if (e.target.value != "") {
            var value = e.target.value;
            value = value.replace(/[ ]/g, "");
            if (value.length > 7 && !(e.which == 8 || e.which == 0)) {
                if (e.stopPropagation) {
                    e.stopPropagation();
                    e.preventDefault();
                }
            }
        }
    }
}

function LandLineNumber_KeyPress(e) {
    window.status = e.which;
    if (!((e.which >= 48 && e.which <= 57) || e.which == 32 || e.which == 8 || e.which == 0)) {
        if (e.stopPropagation) {
            e.stopPropagation();
            e.preventDefault();
        }
    }
    else {
        if (e.target.value != "") {
            var value = e.target.value;
            value = value.replace(/[ ]/g, "");
            if (value.length > 6 && !(e.which == 8 || e.which == 0)) {
                if (e.stopPropagation) {
                    e.stopPropagation();
                    e.preventDefault();
                }
            }
        }
    }
}

function FormatPhoneNumber(number) {
    var value = number;
    var reg = /(-?\d+)(\d{4})/;
    while (reg.test(value)) {
        value = value.replace(reg, "$1" + " " + "$2");
    }
    return value;
}