﻿//global function to show/hide an element dynamically.
function displayRow(rowID, show) {
    var row = document.getElementById(rowID);

    if (show == true) {
        if (row.style.display == 'none') row.style.display = '';
    } else {
        if (row.style.display == '') row.style.display = 'none';
    }
}

//global function to enable/disable a validator dynamically.
function enableValidator(validatorID, enable) {
    var myVal = document.getElementById(validatorID);

    if (enable == true) {
        //this will not cause the validator to fire immediately when enabled.
        myVal.enabled = true;
        ValidatorUpdateDisplay(myVal);
    } else {
        //disable the validator
        ValidatorEnable(myVal, false);
    }
}

//global function to close a ValidatorCalloutExtender dialog
function revalidateControl(controlID, validatorID) {    
    var myControl = document.getElementById(controlID);    

    if (myControl.value == '') {
        // Force the validator to close by putting a dummy value in the textbox and revalidating
        myControl.value = '~';
        var myValidator = document.getElementById(validatorID);
        ValidatorEnable(myValidator, true)
        myControl.value = '';
        ValidatorEnable(myValidator, false)
    }
}

//global function to enable a checkbox to enable/disable a control and a validator on click
function checkboxControlEnable(checkboxID, controlID, validatorID) {
    var myCheckBox = document.getElementById(checkboxID);
    var myControl = document.getElementById(controlID);
    
    if (myCheckBox.checked == true) {
        myControl.disabled = true;
        enableValidator(validatorID, false);
    } else {
        myControl.disabled = false;
        enableValidator(validatorID, true);
    }
}

function checkboxControlEnable2(checkboxID, controlID, validatorID) {
    var myCheckBox = document.getElementById(checkboxID);
    var myControl = document.getElementById(controlID);

    if (myCheckBox.checked == true) {
        myControl.disabled = true;
        //enableValidator(validatorID, false);
    } else {
        myControl.disabled = false;
        //enableValidator(validatorID, true);
    }
}

//function to show a table row if "Other" is selected in a dropdownlist.  Hides row if
//"Other" is not selected.  It will also manage the specified validator accordingly.
function dropDownListWatcher(dropDownListID, validatorControlID, validatedControlID) {
    var myDropDownListControl = document.getElementById(dropDownListID);

    if (myDropDownListControl[myDropDownListControl.selectedIndex].text == 'Other') {
        displayRow('LineOfBusinessOtherRow', true);
        enableValidator(validatorControlID, true);
    } else {
        displayRow('LineOfBusinessOtherRow', false);
        enableValidator(validatorControlID, false);
        revalidateControl(validatedControlID, validatorControlID);
    }
}

//configure status message not to show
var statusmsg = ""
function hidestatus() {
    window.status = statusmsg
    return true
}

//Only allow numeric input
function isNumberKey(evt) {
    var charCode = (evt.which) ? evt.which : evt.keyCode;

    if (charCode > 31 && (charCode < 48 || charCode > 57))
        return false;

    return true;
}

//Only allow currency input
function isCurrencyKey(evt) {
    var charCode = (evt.which) ? evt.which : evt.keyCode

    if (charCode == 44 || charCode == 46) // , or . key
        return true;

    if (charCode > 31 && (charCode < 48 || charCode > 57))
        return false;

    return true;
}


//format currency fields to US Dollar
function formatCurrency(num, dol) {
    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));

    if (dol) {
        return (((sign) ? '' : '-') + '$' + num + '.' + cents);
    } else {
        return (((sign) ? '' : '-') + num + '.' + cents);
    }
}


function wopen(url, name, w, h) {
    // Fudge factors for window decoration space.
    // In my tests these work well on all platforms & browsers.
    w += 32;
    h += 54;
    wleft = (screen.width - w) / 2;
    wtop = (screen.height - h) / 2;
    var win = window.open(url,
    name,
    'width=' + w + ', height=' + h + ', ' +
    'left=' + wleft + ', top=' + wtop + ', ' +
    'location=no, menubar=no, ' +
    'status=no, toolbar=no, scrollbars=yes, resizable=no');
    // Just in case width and height are ignored
    win.resizeTo(w, h);
    // Just in case left and top are ignored
    win.moveTo(wleft, wtop);
    win.focus();
}

