// invoked when moving a product in the cart
function f_move2cart_form(pid) {
    window_move2cart = window.open('', 'win_move2cart', 'left=50,screenX=50,top=50,screenY=50,width=500,height=150,scrollbars=0');
    window_move2cart.focus();
    eval("this.document.add2cart_form_" + pid + ".target = 'win_move2cart';");
    eval("this.document.add2cart_form_" + pid + ".action.value = 'buy_only';");
    eval("this.document.add2cart_form_" + pid + ".submit();");
    return true;
}
// invoked when adding a product in the cart
function f_add2cart_form(pid) {
    eval("this.document.add2cart_form_" + pid + ".target = '';");
    eval("this.document.add2cart_form_" + pid + ".action.value = 'buy';");
    eval("this.document.add2cart_form_" + pid + ".submit();");
    return true;
}
// forward to location
function form_jumpbylocation(_location) {
    if (_location != '') { eval("parent.location='" + location_prefix + _location + "'"); }
}
// --- calendar ---------------------------------------------------------------
function useCalendar(url, dateAsTextField, dateAsTimeField) {
    targetDateAsTextField = dateAsTextField;
    targetDateAsTimeField = dateAsTimeField;
    if (dateAsTimeField != null) { url += '&date_as_time_selected=' + escape(dateAsTimeField.value); }
    if (dateAsTextField != null) { url += '&date_as_text_selected=' + escape(dateAsTextField.value); }
    calendarWindow = window.open(url, 'Calendar', 'left=50,screenX=50,top=50,screenY=50,width=250,height=250,scrollbars=0');
    calendarWindow.focus();
}
// --- cookies ----------------------------------------------------------------
function getCookie(name) {
    if (document.cookie.length > 0) {
        begin = document.cookie.indexOf(name + "=");
        if (begin != -1) {
            begin += name.length + 1;
            end = document.cookie.indexOf(";", begin);
            if (end == -1) end = document.cookie.length;
            return unescape(document.cookie.substring(begin, end));
        }
    }
    return null;
}
function setCookie(name, value, expiredays) {
    var expireDate = new Date();
    expireDate.setTime(expireDate.getTime() + (expiredays * 24 * 3600 * 1000));
    document.cookie = name + "=" + escape(value) + 
        ((expiredays == null) ? "" : "; expires=" + expireDate.toGMTString());
}
function setCookieAdvanced(name, value, expiredays, path, domain, secure) {
    var expireDate = new Date();
    expireDate.setTime(expireDate.getTime() + (expiredays * 24 * 3600 * 1000));
    var curCookie = name + "=" + escape(value) +
        ((expiredays != null) ? "; expires=" + expireDate.toGMTString() : "") +
        ((path != null) ? "; path=" + path : "") +
        ((domain != null) ? "; domain=" + domain : "") +
        ((secure != null) ? "; secure" : "");
    document.cookie = curCookie;
}
function delCookie(name) {
    if (getCookie(name)) { document.cookie = name + "=" + "; expires=Thu, 01-Jan-70 00:00:01 GMT"; }
}
// --- product's price --------------------------------------------------------
function recalculate_product_price_total(form) {
    if ( ! document.getElementById) { return; }
    // get the form based on id_product
    if ( ! form) { return; }
    // get the product's id
    if ( ! form.product_id) { return; }
    id_product = form.product_id.value;
    // get the product's currency digits
    currency_digits = form.product_currency_digits ? parseInt(form.product_currency_digits.value) : 2;
    // get the price_total based on id_product
    price_total_field = document.getElementById("product_price_total_" + id_product);
    if ( ! price_total_field) { return; }
    // include base price ?
    include_base_price = form.include_base_price ? form.include_base_price.value : "yes";
    price_total = parseFloat(include_base_price == "yes" ? form.product_price_base.value : 0);
    // get the quantity, check for NaN
    product_qty_field = form.product_add_qty;
    product_qty = product_qty_field ? parseInt(product_qty_field.value) : 1;
    product_qty = isNaN(product_qty) ? 1 : product_qty;
    // go through all form elements
    for (var i = 0; i < form.elements.length; i++) {
        var field_type = form.elements[i].type;
        if (field_type != "hidden") {
            if (field_type == "checkbox") {
                if ( ! form.elements[i].checked) { continue; }
                //alert(form.elements[i].value);
            }
            // unescape first
            value = unescape(form.elements[i].value);
            // accumulate the price of the option
            price_option = /<price>([^<]+)<\/price>/i.exec(value);
            if (price_option) { price_total += parseFloat(price_option[1]); }
            price_option = /<price value=.?([^<]+).?\/>/i.exec(value);
            if (price_option) { price_total += parseFloat(price_option[1]); }
        }
    }
    // change the price on the page
    price_total_field.innerHTML = (product_qty*price_total).toFixed(currency_digits);
}
// --- product's options ------------------------------------------------------
function option_toggle(e) {
	if (e.checked) { option_highlight(e); } else { option_unhighlight(e); }
}
function option_highlight(e) {
    var r = null;
    if (e.parentNode && e.parentNode.parentNode) {
        r = e.parentNode.parentNode;
    } else if (e.parentElement && e.parentElement.parentElement) {
        r = e.parentElement.parentElement;
    }
    if (r) { if (r.className == "product_options_body") { r.className = "product_options_selected"; } }
}
function option_unhighlight(e) {
    var r = null;
    if (e.parentNode && e.parentNode.parentNode) {
        r = e.parentNode.parentNode;
    } else if (e.parentElement && e.parentElement.parentElement) {
        r = e.parentElement.parentElement;
    }
    if (r) { if (r.className == "product_options_selected") { r.className = "product_options_body"; } }
}
