﻿function MultiDimensionalArray(iRows, iCols) {
    var i;
    var j;
    var a = new Array(iRows);
    for (i = 0; i < iRows; i++) {
        a[i] = new Array(iCols);
    }
    return (a);
}

function formatCurrency(num) {
    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 + '.' + cents);
    return (((sign) ? '' : '-') + '$' + num);
}


function stripCurrency(instr) {
    var temp = instr.replace(/,/, "");
    temp = temp.replace("$", "");
    return temp;
}

//------- Get/Set the value of a radio button -----
// return the value of the radio button that is checked
// return an empty string if none are checked, or
// there are no radio buttons
function getRadioCheckedValue(radioObj) {
    if (!radioObj)
        return "";
    var radioLength = radioObj.length;
    if (radioLength == undefined)
        if (radioObj.checked)
        return radioObj.value;
    else
        return "";
    for (var i = 0; i < radioLength; i++) {
        if (radioObj[i].checked) {
            return radioObj[i].value;
        }
    }
    return "";
}

// set the radio button with the given value as being checked
// do nothing if there are no radio buttons
// if the given value does not exist, all the radio buttons
// are reset to unchecked
function setRadioCheckedValue(radioObj, newValue) {
    if (!radioObj)
        return;
    var radioLength = radioObj.length;
    if (radioLength == undefined) {
        radioObj.checked = (radioObj.value == newValue.toString());
        return;
    }
    for (var i = 0; i < radioLength; i++) {
        radioObj[i].checked = false;
        if (radioObj[i].value == newValue.toString()) {
            radioObj[i].checked = true;
        }
    }
}


//------- Select a language ---------------------------------------------
function selectLanguage(rootDir, language, url) {
    location.href = rootDir + "setLanguage.asp?language=" + language + "&url=" + url;
  
}

//------- toggle a button enable/disable on click event of a checkbox -----
function toggleCheck(me, btn) {
    var checked = me.checked;
    if (checked) {
        document.getElementById(btn).disabled = false;
    } else {
        document.getElementById(btn).disabled = true;
    }
} 

//------- enable/disable selection from dropdown selection box (NOT USED for this project yet.-----
function selectOnclick(e) {
    // e gives access to the event in all browser
    e = e || window.event;
    if (e) {
        e.cancelBubble = true;
        if (e.stopPropagation) {
            e.stopPropagation();
        }
    }
    return false;
}

function enableThis(eleID) {
    document.getElementById(eleID).disabled = false;
}
    
//---------------------------------------------------------------------------

function gotoEdit(i) {
    openWindow(i)
}
function gotoSearch() {
    document.forms[1].submit();
}
function gotoPage(i) {
    document.getElementById("currentPage").value = i;
    document.forms[1].submit();
}

function showFile(file, isImage) {
    var feature = "height=600,width=1000,status=yes,toolbar=no,menubar=no,location=yes,scrollbars=yes,resizable=yes";
    if (isImage)
        var win = window.open("ShowImage.asp?file=" + file, "image", feature);
    else
        var win = window.open(file, "file", feature);

    win.focus();
    return false;
}
//------------------- start of multiple row delete ----------------------
function ConfirmDelete(FormName, FieldName) {
    if (0 == GetSelectedRowCount(FormName, FieldName)) {
        alert('Please select item(s) to delete.');
        return false;
    }
    if (confirm('Are you sure you want to delete the selected ones?')) {
        document.forms[FormName].command.value = "Delete"
        document.forms[FormName].submit();
    }
    return false;
}

function GetSelectedRowCount(FormName, FieldName) {
    if (!document.forms[FormName])
        return 0;
    var objCheckBoxes = document.forms[FormName].elements[FieldName];
    if (!objCheckBoxes)
        return 0;
    var countCheckBoxes = objCheckBoxes.length;
    if (!countCheckBoxes && objCheckBoxes.checked)
        return 1;
    else {
        var count = 0;
        for (var i = 0; i < countCheckBoxes; i++)
            if (objCheckBoxes[i].checked) count++;
        return count;
    }
}
//------------------- end of multiple row delete ----------------------

//<------ select all / deselect all with checkbox ------>
function SetAllCheckBoxes(FormName, FieldName, CheckValue) {
    if (!document.forms[FormName])
        return;
    var objCheckBoxes = document.forms[FormName].elements[FieldName];
    if (!objCheckBoxes)
        return;
    var countCheckBoxes = objCheckBoxes.length;
    if (!countCheckBoxes)
        objCheckBoxes.checked = CheckValue;
    else
    // set the check value for all check boxes
        for (var i = 0; i < countCheckBoxes; i++)
        objCheckBoxes[i].checked = CheckValue;
}


//<------ end of select all / deselect all with checkbox ------>

function trim(stringToTrim) {
    return stringToTrim.replace(/^\s+|\s+$/g, "");
}

function validateRequiredField(field, alerttxt) {
    with (field) {
        if (value == null || value == "")
        { alert(alerttxt); return false; }
        else { return true }
    }
}

function IsNumeric(strString)
//  check for valid numeric strings	
{
    var strValidChars = "0123456789.-";
    var strChar;
    var blnResult = true;

    if (strString.length == 0) return false;

    //  test strString consists of valid characters listed above
    for (i = 0; i < strString.length && blnResult == true; i++) {
        strChar = strString.charAt(i);
        if (strValidChars.indexOf(strChar) == -1) {
            blnResult = false;
        }
    }
    return blnResult;
}

function IsPositiveInteger(strString)
//  check for valid numeric strings
{
    var strValidChars = "0123456789";
    var strChar;
    var blnResult = true;

    if (strString.length == 0) return false;

    //  test strString consists of valid characters listed above
    for (i = 0; i < strString.length && blnResult == true; i++) {
        strChar = strString.charAt(i);
        if (strValidChars.indexOf(strChar) == -1) {
            blnResult = false;
        }
    }
    return blnResult;
}


function allowAlphaNumeric(obj) {
    if (/[^0-9A-Z]/.test(obj.value)) {
        obj.value = obj.value.toUpperCase().replace(/([^0-9A-Z])/g, "");
    }
}

function allowAlpha(obj) {
    if (/[^A-Z]/.test(obj.value)) {
        obj.value = obj.value.toUpperCase().replace(/([^A-Z])/g, "");
    }
}

function allowNumeric(obj) {
    if (/[^0-9]/.test(obj.value)) {
        obj.value = obj.value.toUpperCase().replace(/([^0-9])/g, "");
    }
}


//------------------- show big image on mouseover/hide it on mouseout ----------------------
    function showBigPic(e, picName, id) {
        var viewportwidth;
        var viewportheight;
        // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight

        if (typeof window.innerWidth != 'undefined') {
        viewportwidth = window.innerWidth,
        viewportheight = 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) {
        viewportwidth = document.documentElement.clientWidth,
        viewportheight = document.documentElement.clientHeight
        }

        // older versions of IE

        else {
        viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
        viewportheight = document.getElementsByTagName('body')[0].clientHeight
        }

        var picDiv = document.getElementById('bigPic');
        picDiv.style.visibility = "visible";

        var targ;
        if (!e) var e = window.event;
        if (e.target) targ = e.target;
        else if (e.srcElement) targ = e.srcElement;
        if (targ.nodeType == 3) // defeat Safari bug
        targ = targ.parentNode;
        /*            
        var posy = findPosY(targ) + 5;

        picDiv.style.top = posy + "px";
        picDiv.style.left = Math.floor((viewportwidth - 400) / 2) + "px";
        //  var imgLeft = document.getElementById('imgS1' + id).style.left;
        picDiv.innerHTML = '<img src="' + picName + '" border="1" />';
        */

        var posy = findPosY(targ) - 20;

        picDiv.style.top = posy + "px";
//        picDiv.style.left = Math.floor((viewportwidth - 800) / 2) + "px";
        picDiv.style.left = 300 + "px";
        picDiv.innerHTML = '<img src="' + picName + '" border="1" />';
}

    function hideBigPic() {
        var picDiv = document.getElementById('bigPic');
        picDiv.style.visibility = "hidden";
    }


    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;
    }
//------------------- end of showing big image on mouseover/hide it on mouseout ----------------------
