/*-----------------------------------------------------------------------------
 * Javascript Functions
 *-----------------------------------------------------------------------------
 * dynamic_product_prices.js
 *-----------------------------------------------------------------------------
 * Author:   Estelle Winterflood
 * Email:    cubecart@expandingbrain.com
 * Store:    http://cubecart.expandingbrain.com
 *
 * Date:     January 10, 2008
 * Compatible with CubeCart Version:  4.x.x
 *-----------------------------------------------------------------------------
 * SOFTWARE LICENSE AGREEMENT:
 * You must own a valid license for this modification to use it on your
 * CubeCart™ store. Licenses for this modification can be purchased from
 * Estelle Winterflood using the URL above. One license permits you to install
 * this modification on a single CubeCart installation only. This non-exclusive
 * license grants you certain rights to use the modification and is not an
 * agreement for sale of the modification or any portion of it. The
 * modification and accompanied documentation may not be sublicensed, sold,
 * leased, rented, lent, or given away to another person or entity. This
 * modification and accompanied documentation is the intellectual property of
 * Estelle Winterflood.
 *-----------------------------------------------------------------------------
 * DISCLAIMER:
 * The modification is provided on an "AS IS" basis, without warranty of
 * any kind, including without limitation the warranties of merchantability,
 * fitness for a particular purpose and non-infringement. The entire risk
 * as to the quality and performance of the Software is borne by you.
 * Should the modification prove defective, you and not the author assume 
 * the entire cost of any service and repair. 
 *-----------------------------------------------------------------------------
 */

function ale(f){var old = window.onload;if (typeof window.onload != 'function') {window.onload = f;} else {window.onload = function() {old();f();}}}function st(e,t){if(e) {e.innerText = t;e.textContent = t;}}function gv(o){var a = [];for(var i=0; i<o.length; ++i) {for(var j=0; o[i] && j<o[i].length; ++j) {if (o[i][j].options)a.push(o[i][j].options[o[i][j].selectedIndex].value);else if (o[i][j].checked)a.push(o[i][j].value);else if (o[i][j].length) {for(var k=0; o[i][j] && k<o[i][j].length; ++k) {if (o[i][j][k].options)a.push(o[i][j][k].options[o[i][j][k].selectedIndex].value);else if (o[i][j][k].checked)a.push(o[i][j][k].value);}}}}return a;}
function sh(e,h){if(e) { e.innerHTML = h;e.htmlContent = h; }}

// Safe guard - prevent javascript errors from incorrect installation
function dpCheckInstallation(showAlert)
{
    if (typeof dp_options == 'undefined'
            // global store settings
            || typeof dp_usePP == 'undefined'
            || typeof dp_useQE == 'undefined'
            || typeof dpFormatPrice == 'undefined'
            // product settings
            || typeof dp_price == 'undefined'
            || typeof dp_element == 'undefined'
            || typeof dp_optionPrices == 'undefined'
            || typeof dp_optionSymbols == 'undefined'
            || typeof dp_options == 'undefined') {
        // if these javascript variables are not set -- indicates installation error!
        if (showAlert) alert("Message for the store owner...\n\nDynamic Product Prices has not been installed correctly, make sure all new files are uploaded successfully then carefully check:\n- styleTemplates/content/viewProd.tpl");

        return false;
    }
    return true;
}

function dpUpdatePrice()
{
    if (!dpCheckInstallation(true)) {
        return;
    }

    var price = dp_price;
    var priceChange = 0;
    var rateChange = 0;

    var a = gv(dp_options);
    for (var i=0; i<a.length; ++i) {
        if (a[i] in dp_optionPrices) {
            var optPrice = dp_optionPrices[a[i]];
            var optSymbol = dp_optionSymbols[a[i]];
            /* Integration with Percentage Price Product Options */
            if (dp_usePP && optSymbol=='%') {
                rateChange += optPrice / 100;
            } else if (optPrice > 0) {
                if (optSymbol=='+') priceChange += optPrice;
                else if (optSymbol=='-') priceChange -= optPrice;
                /* Integration with Quick Edit Product Options */
                else if (dp_useQE && optSymbol=='=') price = optPrice;
            }
        }
    }
    if (rateChange != 0.0) price = price + (price * rateChange);  /* Percentage Price Product Options */
    price = price + priceChange;

    var sp = document.getElementById(dp_element);
    st(sp,dpFormatPrice(price));
}


