﻿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;
        }
    }
}



//---------------------------------------------------------------------------

function gotoEdit(i) {
    openWindow(i)
}
function gotoSearch() {
    document.forms["Form1"].submit();
}
function gotoPage(i) {
    document.getElementById("currentPage").value = i;
    document.forms["Form1"].submit();
}

//------------------- 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, "");
    }
}
