﻿var _dollarSign_regex = new RegExp("\\$","g");
var _percentSign_regex = new RegExp("\\%","g");
function remove_matchedCharacters(s, matchRegexExp){
    if (!s || typeof s != "string") 
        return s;
    else 
        return s.replace(matchRegexExp, "");
}
function remove_dollarSign(s){
    return remove_matchedCharacters(s, _dollarSign_regex);
};
function remove_percentSign(s){
    return remove_matchedCharacters(s, _percentSign_regex);
};
function get_valueFromPrettyText(prettyTextSelector) {
    var text = (typeof $(prettyTextSelector).attr("value") == "undefined") 
        ? $(prettyTextSelector).text() //non-input element.
        : $(prettyTextSelector).val(); //input element.
    text = jQuery.trim(remove_dollarSign(text));
    text = jQuery.trim(remove_percentSign(text));
    if (text === "")
        return 0;
    var val = eeparseFloatTh(text);
    if (isNaN(val) || !isFinite(val)) {
        return -1; //invalid user input.
    }
    return val;
};
//returns the portion of the given amount that falls within the given range.
function inRange(amount, rangeMin, rangeMax) {
    if (myIsNaN(amount) || myIsNaN(rangeMin) || myIsNaN(rangeMax) || rangeMin >= rangeMax) {
        throw "inRange: invalid argument";
    }
    
    if (amount <= rangeMin)
        return 0;
    else if (amount > rangeMax)
        return (rangeMax - rangeMin);
    else 
        return (amount - rangeMin);
};
//class used by rate functions.
function Rate(min, max, rate, calc_fee_fxn /*optional*/) {
    this.min = min; //the start of the range for which the fee is applicable.
    this.max = max; //the end of the range for which the fee is applicable.
    this.rate = rate;
    //default fee calculation, which may be overridden.
    this.calc_fee = function(baseAmt){ return inRange(baseAmt, this.min, this.max) * this.rate; };
    if (calc_fee_fxn)
        this.calc_fee = calc_fee_fxn; //override.
};
function get_PromulgatedRate(useSalesPrice){
    var result = 0.0;
    var principal = useSalesPrice ? get_salesPrice() : get_loanAmount();
    if (principal <= 0)
        return 0;

    //Promulgated Rate Table.
    var rates = [
        new Rate(0, 100000, .00575, function(baseAmt){ return (baseAmt <= 17300) ? 100 : (inRange(baseAmt, this.min, this.max) * this.rate); }),
        new Rate(100000, 1000000, .00500),
        new Rate(1000000, 5000000, .00250),
        new Rate(5000000, 10000000, .00225),
        new Rate(10000000, Math.pow(2, 31), .00200)
    ];

    //calculate the fee for each range and sum the terms.
    for(var i in rates) {
        result += rates[i].calc_fee(principal);
    }
    
    return result;
};
function get_ReissueCredit(){
    var result = 0.0;
    var loanAmount = get_loanAmount();
    var priorPolicyAmt = get_priorPolicyAmount();
    if (loanAmount <= 0 || priorPolicyAmt < 0)
        return 0;
        
    var baseAmount = (priorPolicyAmt < loanAmount) ? priorPolicyAmt : loanAmount;
    
    //Reissue Credit Table.
    var rates = [
        new Rate(0, 100000, .00245),
        new Rate(100000, 1000000, .00200),
        new Rate(1000000, 5000000, .00050),
        new Rate(5000000, 10000000, .00025),
        new Rate(10000000, Math.pow(2, 31), .00050)
    ];

    //calculate the fee for each range and sum the terms.
    for(var i in rates) {
        result += rates[i].calc_fee(baseAmount);
    }    
    
    return -result;
};
function set_prettyAmount(el, n){
    var rounded_n = roundup(n, 2);
    var s = eedisplayFloatNDTh(rounded_n, 2); //convert to a string that uses thousandths separators (,).
    if (typeof $(el).attr("value") == "undefined") 
        $(el).text("$" + s);
    else //'input' element.
        //alert("set_prettyAmount: Element '" + $(el).attr("id") + "' is an input element; expected non-input element."); 
        $(el).val("$" + s);
};
function calculate_sum(selector) {
    var sum = 0.0;
    //sum all elements found by the selector expression.
    $(selector).each(function(){
        var term_text = (typeof $(this).attr("value") == "undefined") 
            ? $(this).text() //non-input element.
            : $(this).val(); //input element.
        var term_val = eeparseFloatTh(remove_dollarSign(term_text));
        if (!isNaN(term_val) && isFinite(term_val))
            sum += term_val; //sum all terms.
    });
    return sum;
}

