﻿var arrDictionary = {};

function switchPartner(sender) {
    if (sender.src.indexOf("_sw") > -1) {
        sender.src = sender.src.replace("_sw", "");
    } else {
        sender.src = sender.src.replace(".png", "_sw.png");
    }
}

function showMenueItem(elemId) {
    var parentElem = document.getElementById(elemId.id.substring(0, elemId.id.indexOf("_")));
    var subElem = document.getElementById(parentElem.id + "_sub");
    var offsetParentLeft = 0;

    if (navigator.appName == "Microsoft Internet Explorer" && navigator.appVersion.substring(0, 1) < 5) {
        offsetParentLeft = document.getElementById("cMenu").offsetLeft + document.getElementById("cMenu_Content").offsetLeft;
    }

    if (subElem != null) {
        subElem.style.display = "block";
        subElem.style.top = parentElem.offsetTop + parentElem.offsetHeight + "px";
        subElem.style.left = offsetParentLeft + parentElem.offsetLeft + "px";
        subElem.style.width = parentElem.offsetWidth - 24 + "px";
    }
}

function hideMenueItem(elemId) {
    var parentElem = document.getElementById(elemId.id.substring(0, elemId.id.indexOf("_")));
    var subElem = document.getElementById(parentElem.id + "_sub");

    if (subElem != null) {
        subElem.style.display = "none";
    }
}

function showHelp(elem, key) {
    var text = arrDictionary[key];
    if (text != null) {
        showHelpText(elem, text);
    }
}

function showHelpText(elem, text) {
    var layer = document.getElementById("help");
    var pHelp = document.getElementById("pHelp");
    layer.style.display = "block";
    layer.style.left = getX(elem) + "px";
    layer.style.top = getY(elem) + 20 + "px";
    pHelp.innerHTML = text;
}

function hideHelp() {
    var layer = document.getElementById("help");
    layer.style.display = "none";
}

function getY(oElement) {
    var iReturnValue = 0;
    while (oElement != null) {
        iReturnValue += oElement.offsetTop;
        oElement = oElement.offsetParent;
    }
    return iReturnValue;
}

function getX(oElement) {
    var iReturnValue = 0;
    while (oElement != null) {
        iReturnValue += oElement.offsetLeft;
        oElement = oElement.offsetParent;
    }
    return iReturnValue;
}

function showMessage(sText, mType) {
    var imgPopupIcon = document.getElementById('imgPopupIcon');
    var lblPopupText = document.getElementById('lblPopupText');
    var imgUrl = "";

    switch (mType) {
        case 0: imgUrl = imgInfo.replace("~/", ""); break;
        case 1: imgUrl = imgSuccess.replace("~/", ""); break;
        case 2: imgUrl = imgError.replace("~/", ""); break;
    }

    lblPopupText.innerHTML = sText;
    imgPopupIcon.src = imgUrl;
    popupInfo.Show();
    alignPopup();
}

function alignPopup() {
    var wrapperPopup = document.getElementById("wrapperPopup");
    var imgPopupIcon = document.getElementById("imgPopupIcon");
    var lblPopupText = document.getElementById("lblPopupText");

    lblPopupText.style.width = wrapperPopup.offsetWidth - imgPopupIcon.offsetWidth - 10 + "px";

    if (lblPopupText.offsetHeight < imgPopupIcon.offsetHeight) {
        lblPopupText.style.marginTop = (imgPopupIcon.offsetHeight - lblPopupText.offsetHeight) / 2 + "px";
        wrapperPopup.style.height = imgPopupIcon.offsetHeight + "px";
    } else {
        lblPopupText.style.marginTop = "0px";
        wrapperPopup.style.height = lblPopupText.offsetHeight + "px";
    }

    popupInfo.SetSize(wrapperPopup.offsetWidth, wrapperPopup.offsetHeight);
    return;
}

function checkForNumbers(e) {
    var evt = (e) ? e : window.event;
    var charCode = (evt.which) ? evt.which : evt.keyCode;

    if ((charCode < 48 || charCode > 57) && charCode != 8 && charCode != 46 && charCode != 9) {
        return false;
    }
    return true;
}

function formatNumber(elem) {
    elem.value = addCommas((elem.value == "" ? "0" : elem.value));

    return true;
}

function addCommas(nStr) {
    nStr += '';
    x = nStr.split(',');
    x1 = x[0];
    x2 = x.length > 1 ? ',' + x[1] : ',00';
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1)) {
        x1 = x1.replace(rgx, '$1' + '.' + '$2');
    }
    return x1 + x2;
}

function switchPreviewImage(imageUrl) {
    var img = document.getElementById("imgDetailMain");

    if (img.src != imageUrl) {
        img.src = imageUrl;
        lpMain.Show();

        if (img.onload == null) {
            img.onload = hideLoadingPanel;
        }
    }
}

function hideLoadingPanel(s, e) {
    lpMain.Hide();

    if (s != undefined && s != null) {
        s.onload = null;
    } else {
        this.onload = null;
    }
}

function showLargeImage(elem) {
    var posimg = elem.src.indexOf("&");
    var url = (posimg > -1 ? elem.src.substring(0, posimg) : elem.src);
    var imgLarge = document.getElementById("imgLarge");

    if (imgLarge.src != url) {
        popupLargeImage.SetSize(10, 10);
        popupLargeImage.Hide();
        imgLarge.src = url;
        imgLarge.onload = rePositionLargeImage;
        lpMain.Show();
    } else {
        popupLargeImage.Show();
    }
}

function rePositionLargeImage(s, e) {
    if (s != undefined && s != null) {
        s.onload = null;
    } else {
        this.onload = null;
    }
    lpMain.Hide();
    popupLargeImage.Show();
}
