﻿
// Move item from 1 listbox to another
// uses addOtion
// bankTxt is a text box used to store added options so can be passed to server
// pon (plus(1) or negative(0)) is used to indicate whether the item be removed is to added or removed from the final collection when handled on the server
function moveListItem(srcBox, desBox, bankTxt, pon) {
    var srcLbx = document.getElementById(srcBox);
    var desLbx = document.getElementById(desBox);
    var bank = document.getElementById(bankTxt);

    //alert(srcBox + ", " + desBox);
    var i;
    for (i = srcLbx.options.length - 1; i >= 0; i--) {
        if (srcLbx.options[i].selected) {
            addOption(desLbx, srcLbx.options[i].text, srcLbx.options[i].value);
            if (pon == 1) {
                bank.value = bank.value + "," + srcLbx.options[i].value + ":" + srcLbx.options[i].text;
            }
            else if (pon == 0) {
                //bank.value = bank.value + ", (" + srcLbx.options[i].value + ")";
                bank.value = bank.value.replace("," + srcLbx.options[i].value + ":" + srcLbx.options[i].text, "");
            }
            srcLbx.remove(i);
        }
    }
}
//end of move item

// Add option
function addOption(selectbox, text, value) {
    var optn = document.createElement("OPTION");
    optn.text = text;
    optn.value = value;
    selectbox.options.add(optn);
}
//end of add option

// Remove option
function remOption(selectbox, bankTxt) {
    var lbx = document.getElementById(selectbox);
    var bank = document.getElementById(bankTxt);
    var i;

    for (i = lbx.options.length - 1; i >= 0; i--) {
        if (lbx.options[i].selected) {
            bank.value = bank.value.repalce("," + lbx.options[i].value + ":" + srcLbx.options[i].text, "");
            bank.value = bank.value.repalce(", " + lbx.options[i].value + ":" + srcLbx.options[i].text, "");
            lbx.remove(i);
        }
    }
}
// end remove otpion

// Populate ListBox from csv Text Box
function popLbx(selectbox, srcTxt, srcSelectBox) {
    var lbx = document.getElementById(selectbox);
    var srcStr = document.getElementById(srcTxt).value;
    var srcLbx = document.getElementById(srcSelectBox);

    lbx.length = 0;

    if (srcStr.length > 0) {
        var arrItems = new Array();
        arrItems = srcStr.split(",");
        var i;

        for (i = 0; i < arrItems.length; i++) {
            var arrItem = new Array();
            arrItem = arrItems[i].split(":");
            addOption(lbx, arrItem[1], arrItem[0]);
            var j;
            for (j = srcLbx.options.length - 1; j >= 0; j--) {
                if (srcLbx.options[j].value == arrItem[0]) {
                    srcLbx.remove(j);
                }
            }
        }
    }
}
// end populate Listbox

// Check if file is image
function chkImg(fn) {
    alert(fn);
    var arrFileName = new Array();
    arrFileName = fn.split(".");
    
    switch (arrFileName[1].toLowerCase()) {
        case "jpg": return true;
        case "jpeg": return true;
        case "gif": return true;
        case "png": return true;
        default: return false;
    }
}
// end chk file

// Show Div
function showDv(dvID) {
    var dv = document.getElementById(dvID);

    if (dv.style.visibility != 'visible') {
        dv.style.visibility = 'visible';
        dv.style.height = 'auto';
    } else {
        dv.style.visibility = 'hidden';
        dv.style.height = '0px';
    }
}

// Restrict Characters
function restrictChars(ipt, exp) {
    var str = ipt.value;
    var rChrs = "/ \\ = # , : ; | ? &";

    for (var i = 0; i < rChrs.length; i++) {
        if (str.indexOf(rChrs.charAt(i)) >= 0 && rChrs.charAt(i) != " ") {
            var isExp = false;
            if (exp != null) {
                for (var j = 0; j < exp.length; j++) {
                    if (rChrs.charAt(i) == exp.charAt(j)) {
                        isExp = true;
                    }
                }
            }
            if (isExp == false) {
                ipt.value = str.replace(rChrs.charAt(i), "");
                alert("Illegal Character. The following characters are not permitted: " + rChrs);
            }
        }
    }
}

// Light Box
function lightBox(onOff, w, h) {
    var lBox = document.getElementById('lightbox');
    var lBoxImg = document.getElementById('lBoxImg');
    var light = document.getElementById('light');
    //var fadeDv = document.getElementsByName('fade');
    //var fade = fadeDv[0];
    var fade = document.getElementById('fade');
    if (onOff == "on") {
        lBox.style.display = "block";
        //light.style.display = "block";
        //fade.style.display = "block";

        var bWidth;
        var bHeight;

        if (typeof window.innerWidth != 'undefined') {
            bWidth = window.innerWidth;
            bHeight = window.innerHeight;
        }
        else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0) {
            bWidth = document.documentElement.clientWidth;
            bHeight = document.documentElement.clientHeight;
        }
        else {
            bWidth = document.getElementsByTagName('body')[0].clientWidth;
            bHeight = document.getElementsByTagName('body')[0].clientHeight;
        }

        if (w == 0) {
            w = lBoxImg.width + 32;
        } else {
            w = w + 32;
        }
        if (h == 0) {
            h = lBoxImg.height + 32;
        } else {
            h = h + 32;
        }

        var x;
        var y;
        if (w > bWidth) {
            h = parseInt((bWidth / w) * h);
            w = bWidth;
        }
        if (h > bHeight) {
            w = parseInt((bHeight / h) * w);
            h = bHeight;
        }
        lBoxImg.style.width = (w - 32) + "px";
        lBoxImg.style.height = (h - 32) + "px";
        x = parseInt((bWidth - w) / 2);
        y = parseInt((bHeight - h) / 2);

        if (h > bHeight - 79) {
            h = bHeight - 79;
            y = (bHeight - h) / 2;
        }

        light.style.left = x + "px";
        light.style.top = y + "px";
        light.style.width = w + "px";
        light.style.height = h + "px";
    } else {
        lBox.style.display = "none";
        lBoxImg.src = loadingGif.src;
        //light.style.display = "none";
        //fade.style.display = "none";
    }
}

function viewImg(srcImg, thmb, w, h) {
    var lBoxImg = document.getElementById('lBoxImg');
    var srcImg = document.getElementById(srcImg);
    lBoxImg.src = loadingGif.src;
    if (thmb == 1) {
        var thmbPath = ""
        if (srcImg.tagName == 'IMG') {
            thmbPath = srcImg.src.replace("thmbs/", "");
        } else {
            var rptImg = srcImg.getElementsByTagName('IMG')[0];
            thmbPath = rptImg.src.replace("thmbs/", "");
        }
        lBoxImg.src = thmbPath;
    } else {
        lBoxImg.src = srcImg.src;
    }
    lBoxImg.alt = srcImg.alt;

    lightBox('on', w, h);
}

// Get Querystring value
function queryStr(param) {
    var qs = window.location.search.substring(1);
    var arrParams = qs.split("&");
    for (var i = 0; i < arrParams.length; i++) {
        var arrParam = arrParams[i].split("=");
        if (arrParam[0] == param) {
            return arrParam[1].replace("+", " ");
        }
    }
}

// Get Browser Dims
function browserDims(woh) {
    var bWidth;
    var bHeight;
    // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight

    if (typeof window.innerWidth != 'undefined') {
        bWidth = window.innerWidth;
        bHeight = window.innerHeight;
    }

    // IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)

    else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0) {
        bWidth = document.documentElement.clientWidth;
        bHeight = document.documentElement.clientHeight;
    }

    // older versions of IE

    else {
        bWidth = document.getElementsByTagName('body')[0].clientWidth;
        bHeight = document.getElementsByTagName('body')[0].clientHeight;
    }

    if (woh == "w") {
        return bWidth;
    }
    else if (woh == "h") {
        return bHeight;
    }
}

//Get div position
function getDivPos(obj) {
    var curleft = curtop = 0;
    if (obj.offsetParent) {
        do {
            curleft += obj.offsetLeft;
            curtop += obj.offsetTop;
        } while (obj = obj.offsetParent);
        return [curleft, curtop];
    }
}
