﻿
function addClickFunction(id) {
    {
        var b = document.getElementById(id);
        if (b && typeof (b.click) == 'undefined')
            b.click = function() {
                {
                    var result = true;
                    if (b.onclick) result = b.onclick();

                    if (typeof (result) == 'undefined' || result) {
                        {
                            eval(b.href);
                        }
                    }
                }
            }
    }
}

function addEvent(elm, evType, fn, useCapture) {
    if (elm.addEventListener) {
        elm.addEventListener(evType, fn, useCapture);
        return true;
    }
    else if (elm.attachEvent) {
        var r = elm.attachEvent('on' + evType, fn);
        return r;
    }
    else {
        elm['on' + evType] = fn;
    }
}

function isEnterKey(ev) {
    var enterkeyCode = 13;

    if (ev.keyCode) {
        if (ev.keyCode == enterkeyCode) {
            return true;
        }
    }
    else if (ev.which == enterkeyCode) {
        return true;
    }
}

function jqCheckAll(checkboxId, containerId) {
    $("#" + containerId + " :checkbox").attr('checked', $('#' + checkboxId).is(':checked'));
}

// handle keypress submit - browser agnostic
// catch the keypress on a textbox, if the keystroke is the (enter) then call __doPostback to do
// a manual postback using that control

function submitOnEnterPress(ev, buttonId) {
    var enterkeyCode = 13;    // 13 is for (enter)
    if (window.event)  // ie
    {
        if (ev.keyCode == enterkeyCode) {
            __doPostBack(buttonId, '');
            return false;
        }
    }
    else if (ev.which)  // all other
    {
        if (ev.keyCode == enterkeyCode) {
            __doPostBack(buttonId, '');
            return false;
        }
    }

    return true;
}


function unvalidate(myValidationGroup) {
    // Remove the validator control(s) from display.
    var myValidators = Page_Validators;
    if ((typeof (myValidators) != "undefined") && (myValidators != null)) {
        for (i = 0; i < myValidators.length; i++) {
            var myValidator = myValidators[i];
            if (myValidationGroup == null || IsValidationGroupMatch(myValidator, myValidationGroup)) {
                if (myValidator.style.visibility.length > 0 && myValidator.style.display.length == 0) {
                    myValidator.style.visibility = 'hidden';
                }
                else if ((myValidator.style.display.length > 0 || myValidator.style.display == "") && myValidator.style.visibility.length == 0) {
                    myValidator.style.display = 'none';
                }
            }
        }
    }
}

function isValueGreaterThanZero(ctlClientID) {
    var quantityString = $get(ctlClientID).value.toString().trim();

    var quantity = 0;

    if (quantityString != "") {
        quantity = parseInt(quantityString, 10);
    }

    return quantity > 0;
}

function roundDollarDown(unroundedAmount) {
    var returnValue = unroundedAmount;

    returnValue = returnValue * 100;
    returnValue = Math.floor(returnValue);
    returnValue = returnValue / 100;

    return returnValue;

}

function roundDollarUp(unroundedAmount) {
    var returnValue = unroundedAmount;

    returnValue = returnValue * 100;
    returnValue = Math.ceiling(returnValue);
    returnValue = returnValue / 100;

    return returnValue;

}

//function roundDollarNormal(unroundedAmount) {
//    var returnValue = unroundedAmount;

//    returnValue = returnValue * 100;
//    returnValue = Math.round(returnValue);
//    returnValue = returnValue / 100;

//    return returnValue;
//}
function roundDollarNormal(unroundedAmount) {
    var truncValue = unroundedAmount.toFixed(3);
    var returnValue = unroundedAmount;

    returnValue = parseFloat(Math.round(parseFloat(returnValue * 100)) / 100);
    var difference = (truncValue - returnValue).toFixed(3);
    if (difference == .005)
        returnValue += .01;

    return returnValue;
}


function FormatContribution(discount, forDisplay) {
    var returnValue = "0";

    if (!isNaN(discount)) {
        discount = (discount * 100).toFixed(2);
        var discountDisplay;

        if (((discount * 100) % 1) == 0) {
            returnValue = (discount * 100).toFixed(0);
        }
        else {
            returnValue = (discount * 100).toFixed(2);
        }
    }

    if (forDisplay)
        return returnValue + "%";
    return returnValue;

}

function formatDollars(amount, isForDisplay) {
    var returnValue = "0.00";

    if (amount.toString().trim() != "") {
        if (!isNaN(amount)) {
            returnValue = parseFloat(amount).toFixed(2);

            if (isForDisplay) {
                returnValue = "$" + commaFormatted(returnValue);
            }
        }
    }

    return returnValue;
}

function formattedDiscountDisplay(percent) {
    percent = Math.round(parseFloat(percent * 10000)) / 100;
    var helper = percent.toString();

    if (helper.indexOf(".") != -1) {
        helper = helper.substring(0, helper.indexOf(".") + 3);
        percent = parseFloat(helper);
    }
    else {
        percent = parseInt(helper, 10);
    }

    return percent + "%";
}

function roundNumber(num, dec) {
    var result = Math.round(num * Math.pow(10, dec)) / Math.pow(10, dec);
    return result;
}

function commaFormatted(amount) {
    var delimiter = ","; // replace comma if desired
    var a = amount.split('.', 2)
    var d = a[1];
    var i = parseInt(a[0]);
    if (isNaN(i)) { return ''; }
    var minus = '';
    if (i < 0) { minus = '-'; }
    i = Math.abs(i);
    var n = new String(i);
    a = [];
    while (n.length > 3) {
        var nn = n.substr(n.length - 3);
        a.unshift(nn);
        n = n.substr(0, n.length - 3);
    }
    if (n.length > 0) { a.unshift(n); }
    n = a.join(delimiter);
    if (d.length < 1) { amount = n; }
    else { amount = n + '.' + d; }
    amount = minus + amount;
    return amount;
}
// end of function CommaFormatted()


function getChar(e) {
    var key;
    var keychar;

    if (window.event)
        key = window.event.keyCode;
    else if (e)
        key = e.which;
    else
        return null;
        
    keychar = String.fromCharCode(key);

    return keychar;
}

function isAnAlphaOrDelete(e) {
    var key;
    var keychar;

    if (window.event)
        key = window.event.keyCode;
    else if (e)
        key = e.which;
    else
        return true;

    if (key == 46) return true;
    
    keychar = String.fromCharCode(key);

    
    if ((("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ").indexOf(keychar) > -1))
        return true;
    else
        return false;
}

function isNavigationKey(e) {
    var key;
    var keychar;

    if (window.event)
        key = window.event.keyCode;
    else if (e)
        key = e.which;
    else
        return true;
    keychar = String.fromCharCode(key);

    // control keys
    if ((key == null) || (key == 0) || (key == 8) ||
    (key == 9) || (key == 13) || (key == 27) || (key == 37) || (key == 39))
        return true;
        
    return false;
}

function isANumber(e, dec) {
    var key;
    var keychar;

    if (window.event)
        key = window.event.keyCode;
    else if (e)
        key = e.which;
    else
        return true;
    keychar = String.fromCharCode(key);

    // control keys
    if ((key == null) || (key == 0) || (key == 8) ||
    (key == 9) || (key == 13) || (key == 27) || (key == 37) || (key == 39))
        return true;

    // numbers
    else if ((("0123456789").indexOf(keychar) > -1))
        return true;

    // decimal point jump
    else if (dec && keychar == ".") {
        return true;
    }
    // using key pad
    else if ((key > 95 && key < 106) || (dec && key == 110))
        return true;
    else
        return false;
}

function GenericWebServiceErrorHandler(oErr) {

    
    document.location.reload();
}


/* JQuery Utility Functions */
function BindBlurDollarFormat($elem) {
    $elem.blur(function() {
        $(this).val(formatDollars($(this).val(), false));
    });
}
function GetAppPath(){
    return $(":hidden[id$=hidAppPath]").val();
}

function TotalContainer($container, selector) {
    var total = 0;
    $(selector, $container).each(function() {
        var value = "";
        try {
            value = Number($(this).val());
        }
        catch (ex) {
            value = Number($(this).html());
        }

        if (!isNaN(value)) {
            total += value;
        }
    });
    return total;
}
/* End JQuery Utility FUnctions */

/* Start JScript Prototype functions */
String.prototype.toNumber = function() {
    var value = Number(this);
    if (isNaN(value)) {
        value = -1;
    }

    return value;
}

String.prototype.trim = function() {
    a = this.replace(/^\s+/, '');
    return a.replace(/\s+$/, '');
};

/* End JScript Prototype functions */

