/**
 * Updates the displayed policy price when the
 * "Cancel for Business Reasons" upsell is checked or unchecked.
 * @author JVM
 */
function cancelBizReasonCalc(){

    var bizReasonCheckbox = document.getElementById("cancelBizReason");

    // This is the price of the upsell
    var regexPattern = "[0-9]+.[0-9]{2}";
    var rawPriceString = document.getElementById("cancelBizReasonPrice").innerHTML;
    rawPriceString = rawPriceString.replace(',','');
    var upsellPriceString = rawPriceString.match(regexPattern);
    var upsellPrice = parseFloat(upsellPriceString);

    // This is the baseline price
    var rawInsurancePrice = document.getElementById("baseStdPrice").innerHTML;
    rawInsurancePrice = rawInsurancePrice.replace(',','');
    var insurancePriceString = rawInsurancePrice.match(regexPattern);
    var insurancePrice = parseFloat(insurancePriceString);

    if(bizReasonCheckbox.checked) { // add the upsell
        document.getElementById("stdprice").innerHTML = formatCurrency(((insurancePrice+upsellPrice).toFixed(2)));
    }
    else  { // show the price without the upsell
        document.getElementById("stdprice").innerHTML = document.getElementById("baseStdPrice").innerHTML;
    }

}


/**
* Updates the displayed policy price when the
* "Cancel for Any Reason" upsell is checked or unchecked.
* @author JVM
*/
function cancelAnyReasonCalc() {

    var anyReasonCheckbox = document.getElementById("cancelAnyReason");

    // This is the price of the upsell
    var regexPattern = "[0-9]+.[0-9]{2}";
    var rawPriceString = document.getElementById("cancelAnyReasonPrice").innerHTML;
    rawPriceString = rawPriceString.replace(',','');
    var upsellPriceString = rawPriceString.match(regexPattern);
    var upsellPrice = parseFloat(upsellPriceString);

    // This is the baseline price
    var rawInsurancePrice = document.getElementById("baseExtPrice").innerHTML;
    rawInsurancePrice = rawInsurancePrice.replace(',','');
    var insurancePriceString = rawInsurancePrice.match(regexPattern);
    var insurancePrice = parseFloat(insurancePriceString);

    if(anyReasonCheckbox.checked) { // add the upsell
        document.getElementById("extprice").innerHTML = formatCurrency(((insurancePrice+upsellPrice).toFixed(2)));
    } else { // show the price without the upsell
        document.getElementById("extprice").innerHTML = document.getElementById("baseExtPrice").innerHTML;
    }

}

/**
* Updates the displayed policy price when the
* "Cancel for Any Reason" upsell is checked or unchecked.
* @author JVM
*/
function cancelAnyReasonCalc2() {

    var anyReasonCheckbox = document.getElementById("cancelAnyReason");

    // This is the price of the upsell
    var regexPattern = "[0-9]+.[0-9]{2}";
    var rawPriceString = document.getElementById("cancelAnyReasonPrice").innerHTML;
    rawPriceString = rawPriceString.replace(',','');
    var upsellPriceString = rawPriceString.match(regexPattern);
    var upsellPrice = parseFloat(upsellPriceString);

    // This is the baseline price
    var rawInsurancePrice = document.getElementById("stdprice").innerHTML;
    rawInsurancePrice = rawInsurancePrice.replace(',','');
    var insurancePriceString = rawInsurancePrice.match(regexPattern);
    var insurancePrice = parseFloat(insurancePriceString);

    if(anyReasonCheckbox.checked) { // add the upsell
        document.getElementById("stdprice").innerHTML = formatCurrency(((insurancePrice+upsellPrice).toFixed(2)));
    } else { // show the price without the upsell
        document.getElementById("stdprice").innerHTML = formatCurrency(((insurancePrice-upsellPrice).toFixed(2)));
    }

}






/**
* update the innnerHTML of the final price for cancel any reason upsell
* @author MXS
*/
com.csatp.form.cancelAnyReasonCalcStd = function(onLoadFlag)
{
    var checkboxId = document.getElementById("cancelAnyReason");

    var upsellPrice = com.csatp.form.getPrices("cancelAnyReasonPrice");
    var insurancePrice = com.csatp.form.getPrices("stdprice");

    if(checkboxId.checked) { // add the upsell
        document.getElementById("stdprice").innerHTML = formatCurrency(((insurancePrice+upsellPrice).toFixed(2)));
    } else if (!checkboxId.checked){ // show the price without the upsell
        if (!onLoadFlag){ // show the price without the upsell
            document.getElementById("stdprice").innerHTML = formatCurrency(((insurancePrice-upsellPrice).toFixed(2)));
        }
    }
};




/**
* update the innnerHTML of the final price for collision damage waiver upsell
* @author MXS
*/
com.csatp.form.collisionDamageWaiverCalcStd = function(onLoadFlag)
{
    var checkboxId = document.getElementById("collisionDamageWaiverFlag");

    var upsellPrice = com.csatp.form.getPrices("collisionDamageWaiverPrice");
    var insurancePrice = com.csatp.form.getPrices("stdprice");

    if(checkboxId.checked) { // add the upsell
        document.getElementById("stdprice").innerHTML = formatCurrency(((insurancePrice+upsellPrice).toFixed(2)));
    } else if (!checkboxId.checked){ // show the price without the upsell only if not on page load
        if (!onLoadFlag){
            document.getElementById("stdprice").innerHTML = formatCurrency(((insurancePrice-upsellPrice).toFixed(2)));
        }
    }
};





/**
* strip out commas from price
* @author MXS
*/ 
com.csatp.form.getPrices = function(id){
    // This is the price of the upsell
    var regexPattern = "[0-9]+.[0-9]{2}";

    var rawPriceString = document.getElementById(id).innerHTML;
    rawPriceString = rawPriceString.replace(',','');
    var upsellPriceString = rawPriceString.match(regexPattern);

    return parseFloat(upsellPriceString);
};





/**
* If a user were to click "Buy now" and click the back arrow, IE would cache clicked
* checkboxes' states even though they were never submitted/processed into the form
* beans on our website.   This in turn breaks dynamic price calculations we do
* through Javascript to show them how much an upsell costs.   This method insures
* whenever the quoteoptions_fs page is loaded that the upsell elements are always
* forced in sync with whatever is saved in the purchaseform.
*
* @author JVM
*/
function resetUpsells() {

    var anyReasonCheckBox = document.getElementById("cancelAnyReason");
    var bizReasonCheckBox = document.getElementById("cancelBizReason");

    // the user has clicked the back arrow and the browser has cached a checkedbox that was
    //   never submitted.   We need to reset its state so dynamic price calculations function
    if(actionFormCancelAnyReason == '0' && anyReasonCheckBox.checked) {
        anyReasonCheckBox.checked = false;
    }

    if(actionFormCancelBizReason == '0' && bizReasonCheckBox.checked) {
        bizReasonCheckBox.checked = false;
    }

    // this patches a situation where the user has purchased and the box is checked
    //    on the purchaseform, but not checked on the client (essentially the opposite of above)
    if(actionFormCancelAnyReason == '1' && !anyReasonCheckBox.checked) {
        anyReasonCheckBox.checked = true;
    }


    if(actionFormCancelBizReason == '1' && !bizReasonCheckBox.checked) {
        bizReasonCheckBox.checked = true;
    }


}


/**
*  Original:  Cyanide_7 (leo7278@hotmail.com)
*  Web Site:  http://www7.ewebcity.com/cyanide7 -->
*/
function formatCurrency(num) {
    num = num.toString().replace(/\$|\,/g,'');

    if(isNaN(num))
        num = "0";

    var sign = (num == (num = Math.abs(num)));
    num = Math.floor(num*100+0.50000000001);
    var 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);
}
   
   
   
   

    
  
