﻿function RBLib_ObjectExists(objId) {
    if (document.getElementById(objId)) {
        return true;
    }
    return false;
}
function RBLib_IsBlank(str) {
    if (!str) {return true;}
    for (i = 0; i < str.length; i++) {
        if (' ' !== str[i]) { return false; }
    }
    return true;
}
function RBLib_Trim(str) {
    return RBLib_RTrim(RBLib_LTrim(str));
}
function RBLib_LTrim(str) {
    var i = 0;
    while (i < str.length && str[i] === ' ') { i++; }
    return str.substring(i, str.length - i);
}
function RBLib_RTrim(str) {
    var i = str.length - 1;
    while (i > 0 && str[i] === ' ') { i -= 1; }
    return str.substring(0, i + 1);
}
function RBLib_ConfirmDelete(ctlName, desc) {
    if (!RBLib_ObjectExists(ctlName)) {
        alert('Control Id ' + ctlName + ' not found.');
        return false;
    }
    var ctl = document.getElementById(ctlName);
    if (ctl.disabled) {
        return false;
    }
    return confirm('Are you sure you want to delete this ' + desc + '?');
}
function RBLib_ConfirmRun(ctlName, desc) {
    if (!RBLib_ObjectExists(ctlName)) {
        alert('Control Id ' + ctlName + ' not found.');
        return false;
    }
    var ctl = document.getElementById(ctlName);
    if (ctl.disabled) {
        return false;
    }
    return confirm('Are you sure you want to run this ' + desc + '?');
}
function RBLib_ValidateField(id, msg, flag) {
    var ctrl, val, regex;
    if (!RBLib_ObjectExists(id)) {
        return true;
    }
    ctrl = document.getElementById(id);
    val = ctrl.value;
    if (RBLib_IsBlank(val)) {
        alert(msg + ' is required.');
        ctrl.focus();
        return false;
    }
    if (flag.length == 0) {
        return true;
    }
    if (!RBLib_RegExMatch(flag, val)) {
        alert(msg + ' is not valid.');
        ctrl.focus();
        return false;
    }
    return true;
}
function RBLib_RegExMatch(flag, str) {
    switch (flag) {
        case 'E':
            regex = /^([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5})$/;
            break;
        case 'Z':
            //regex = /(^\d{5}(-\d{4})?$)|(^[ABCEGHJKLMNPRSTVXY]{1}\d{1}[A-Z]{1} *\d{1}[A-Z]{1}\d{1}$)/;
            regex = /(^([0-9]{5})$)|(^[ABCEGHJKLMNPRSTVXYabceghjklmnprstvxy]{1}\d{1}[A-Za-z]{1} *\d{1}[A-Za-z]{1}\d{1}$)/;
            break;
        default:
            regex = /^([a-zA-Z,#/ \.\(\)\-\+\*]*[2-9])([a-zA-Z,#/ \.\(\)\-\+\*]*[0-9]){2}([a-zA-Z,#/ \.\(\)\-\+\*]*[2-9])([a-zA-Z,#/ \.\(\)\-\+\*]*[0-9]){6}[0-9a-zA-Z,#/ \.\(\)\-\+\*]*$/;
    }
    return str.match(regex);
}
function RBLib_ValidateMatch(id1, id2, msg) {
    var ctrl1 = document.getElementById(id1);
    var ctrl2 = document.getElementById(id2);
    if (ctrl1.value !== ctrl2.value) {
        alert(msg + ' do not match.');
        ctrl1.focus();
        return false;
    }
    return true;
}    
function RBLib_GetRadioValue(id) {
    if (!RBLib_ObjectExists(id)) {
        return '';
    }
    var radioOpt = document.getElementById(id);
    if (radioOpt.checked) {
       return radioOpt.value;
    }
    return '';            
}
function RBLib_ClearList(list) {
    for (i = list.options.length - 1; i >= 0; i--) {
        list.removeChild(list.options[i]);
    }
}
function RBLib_IsUniqueItem(list, item) {
    for (var i = 0; i < list.length; i++) {
        if (list.options[i].value === item) {
            return false;
        }
    }
    return true;
}
