﻿function ShowToolTip(ToolTipBoxId, InputControlId, sectionId) {
    var toolTipCtrl = document.getElementById(ToolTipBoxId);
    var inputCtrl = document.getElementById(InputControlId);

    if (toolTipCtrl != null && inputCtrl != null) {
        var NS4Plus = (document.layers) ? 1 : 0;
        var IE4Plus = (document.all) ? 1 : 0;
        var FIREFOX = navigator.userAgent.indexOf("Firefox");

        if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)) {
            var ieversion = new Number(RegExp.$1)
            if (ieversion >= 8) {

                toolTipCtrl.style.left = (inputCtrl.offsetLeft + inputCtrl.offsetWidth) + "px";

                if (inputCtrl.type != "textarea")
                    toolTipCtrl.style.top = (0 - inputCtrl.offsetHeight) + "px";
                else {
                    toolTipCtrl.style.top = inputCtrl.offsetTop + "px";
                }

                toolTipCtrl.style.visibility = "visible";

            }
            else {
                toolTipCtrl.style.left = inputCtrl.offsetLeft + inputCtrl.offsetWidth;
                toolTipCtrl.style.top = 0 - inputCtrl.offsetHeight;
                toolTipCtrl.style.visibility = "visible";
            }
        }
        else if (FIREFOX) {
            toolTipCtrl.style.left = (inputCtrl.offsetLeft + inputCtrl.offsetWidth) + "px";

            if (inputCtrl.type != "textarea")
                toolTipCtrl.style.top = (0 - inputCtrl.offsetHeight) + "px";
            else {
                toolTipCtrl.style.top = inputCtrl.offsetTop + "px";
            }

            toolTipCtrl.style.visibility = "visible";
        }
        else {
            toolTipCtrl.style.left = (inputCtrl.offsetLeft + inputCtrl.offsetWidth) + "px";

            if (inputCtrl.type != "textarea")
                toolTipCtrl.style.top = (0 - inputCtrl.offsetHeight) + "px";
            else {
                if (InputControlId != "keywords")
                    toolTipCtrl.style.top = inputCtrl.offsetTop + "px";
                else
                    toolTipCtrl.style.top = (0 - inputCtrl.offsetHeight) + "px";
            }

            toolTipCtrl.style.visibility = "visible";
        }
    }

    if (sectionId != null)
        ActiveSection(sectionId)
}

function HideToolTip(ToolTipBoxId, sectionId, currencyInputControlId, allowBlank) {
    var toolTipCtrl = document.getElementById(ToolTipBoxId);

    if (toolTipCtrl != null)
        toolTipCtrl.style.visibility = "hidden";

    if (sectionId != null)
        DeactiveSection(sectionId)

    //Currency format
    var currencyInputControl = document.getElementById(currencyInputControlId);
    if (currencyInputControl != null) {
        var isAllowBlank = (allowBlank && allowBlank == true);
        if (!isAllowBlank) {
            currencyInputControl.value = formatCurrency(currencyInputControl.value);
        }
        else {
            currencyInputControl.value = (currencyInputControl.value != "") ? formatCurrency(currencyInputControl.value, allowBlank) : "";
        }
    }
}

function ActiveSection(sectionId) {
    var section = document.getElementById(sectionId);

    if (section != null)
        section.className = section.className + " activeSection";
}

function DeactiveSection(sectionId) {
    var section = document.getElementById(sectionId);

    if (section != null)
        section.className = section.className.replace(" activeSection", "");
}

function formatCurrency(num, allowBlank) {
    num = num.toString().replace(/\$|\,/g, '');

    if (isNaN(num)) {
        var isAllowBlank = (allowBlank && allowBlank == true);
        if (isAllowBlank)
            return "";
        else
            num = "0";
    }

    var sign = (num == (num = Math.abs(num)));
    num = Math.floor(num * 100 + 0.50000000001);
    var 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 + '.' + cents);
}

function findPosX(obj) {
    var curleft = 0;
    if (obj.offsetParent)
        while (1) {
        curleft += obj.offsetLeft;
        if (!obj.offsetParent)
            break;
        obj = obj.offsetParent;
    }
    else if (obj.x)
        curleft += obj.x;
    return curleft;
}

function findPosY(obj) {
    var curtop = 0;
    if (obj.offsetParent)
        while (1) {
        curtop += obj.offsetTop;
        if (!obj.offsetParent)
            break;
        obj = obj.offsetParent;
    }
    else if (obj.y)
        curtop += obj.y;
    return curtop;
}
