// invoke the function that has to be executed on every page
executeOnEveryPage_header();
// invoked on every generated webpage (in the <header> section)
function executeOnEveryPage_header() {
    // get an associative array of URL key/value pairs
    urlArgs = getUrlArguments(window.location);
    // get referrer url arguments
    var newReferrer = urlArgs["referrer"];
    // if no referrer then take the referrer of this page and save it as cookie
    var referrer = newReferrer ? newReferrer : document.referrer;
    if (referrer != "") {
        referrer_current = getCookie("cookie_scart_referrer");
        if (referrer_current == null) { setCookieAdvanced("cookie_scart_referrer", referrer, 1, '/', null, null); }
    }
    // save scart_affiliate_id url arguments as cookies
    var cookieScartAffiliateId = urlArgs["scart_affiliate_id"];
    if (cookieScartAffiliateId) { setCookieAdvanced("cookie_scart_affiliate_id", cookieScartAffiliateId, 1, '/', null, null); }
}
// invoked on every generated webpage (before the </body> tag)
function executeOnEveryPage_bottom() {
    if (document.getElementsByName) {
        // find all select fields for "search by category" and select the appropriate option
        searchby_category_fields = document.getElementsByName("searchby_category");
        if (searchby_category_fields) {
            for (var i = 0; i < searchby_category_fields.length; i++) {
                for (var j = 1; j < searchby_category_fields[i].length; j++) {
                    if (searchby_category_fields[i].options[j].value == current_category_ids_path) {
                        searchby_category_fields[i].options[j].selected = true;
                    }
                }
            }
        }
    }
}

// invoked when adding a product to the wishlist
function f_add2wishlist_form(pid) { return f_add2wishlist_form_alias1(pid, '0', false, ''); }
function f_add2wishlist_form_alias1(pid, rpid, isCheckOptions, msgSelectOption) {
    if (isCheckOptions) {
        // check if no option is selected and show appropriate message
        eval("form = this.document.add2cart_form_" + pid + ";");
        objectProductTotals = rpid > 0 ? recalculate_product_price_total_2(form, "_arr[" + rpid + "]") : recalculate_product_price_total(form);
        if (objectProductTotals && ! objectProductTotals.isProductReadyToOrder()) { alert(msgSelectOption); return false; }
        if (objectProductTotals && ! objectProductTotals.isProductReadyToOrderRequired()) { alert(msgSelectOption); return false; }
    }
    // set the default action
    action_value = 'add2wishlist';
    // get some meta info
    add2cartForm = document.forms["add2cart_form_" + pid];
    scart_referrer = getCookie("cookie_scart_referrer");
    scart_affiliate_id = getCookie("cookie_scart_affiliate_id");
    // submit the form
    eval("this.document.add2cart_form_" + pid + ".target = '';");
    if (add2cartForm.scart_referrer) { eval("this.document.add2cart_form_" + pid + ".scart_referrer.value = '" + (scart_referrer != null ? scart_referrer : "") + "';"); }
    if (add2cartForm.scart_affiliate_id) { eval("this.document.add2cart_form_" + pid + ".scart_affiliate_id.value = '" + (scart_affiliate_id != null ? scart_affiliate_id : "") + "';"); }
    eval("this.document.add2cart_form_" + pid + ".form_action_value.value = '" + action_value + "';");
    if (rpid > 0) { eval("this.document.add2cart_form_" + pid + ".rpid.value = '" + rpid + "';"); }
    eval("this.document.add2cart_form_" + pid + ".submit();");
    return true;
}

// invoked when moving a product to the cart
function f_move2cart_form(pid) { return f_move2cart_form_alias1(pid, '0', false, ''); }
function f_move2cart_form_alias1(pid, rpid, isCheckOptions, msgSelectOption) {
    if (isCheckOptions) {
        // check if no option is selected and show appropriate message
        eval("form = this.document.add2cart_form_" + pid + ";");
        objectProductTotals = rpid > 0 ? recalculate_product_price_total_2(form, "_arr[" + rpid + "]") : recalculate_product_price_total(form);
        if (objectProductTotals && ! objectProductTotals.isProductReadyToOrder()) { alert(msgSelectOption); return false; }
        if (objectProductTotals && ! objectProductTotals.isProductReadyToOrderRequired()) { alert(msgSelectOption); return false; }
    }
    // set the default action
    action_value = 'buy_only';
    // if there is a cart info on the generated page then add the product and refresh the info
    if (document.getElementById) {
        _scart_order_qty = document.getElementById("scart_order_qty");
        _scart_order_subtotal = document.getElementById("scart_order_subtotal");
        _scart_order_grandtotal = document.getElementById("scart_order_grandtotal");
        if (_scart_order_qty || _scart_order_subtotal || _scart_order_grandtotal) { action_value = 'buy_only_special'; }
    }
    // get the form and execute the onsubmit
    add2cartForm = document.forms["add2cart_form_" + pid];
    onsubmitResult = add2cartForm.onsubmit();
    if ((typeof(onsubmitResult) == 'undefined') || onsubmitResult) {
        // open window
        window_move2cart = window.open('', 'win_move2cart', 'left=50,screenX=50,top=50,screenY=50,width=500,height=150,scrollbars=0,resizable=1');
        if (window.focus) { window_move2cart.focus(); }
        // show progress animated gif
        //window_move2cart.document.write('<html><body><br><p align="center"><img src="images_website/anim_progress.gif" border="0"></p></body></html>');
        // get some meta info
        add2cartForm = document.forms["add2cart_form_" + pid];
        scart_referrer = getCookie("cookie_scart_referrer");
        scart_affiliate_id = getCookie("cookie_scart_affiliate_id");
        // submit the form
        eval("this.document.add2cart_form_" + pid + ".target = 'win_move2cart';");
        if (add2cartForm.scart_referrer) { eval("this.document.add2cart_form_" + pid + ".scart_referrer.value = '" + (scart_referrer != null ? scart_referrer : "") + "';"); }
        if (add2cartForm.scart_affiliate_id) { eval("this.document.add2cart_form_" + pid + ".scart_affiliate_id.value = '" + (scart_affiliate_id != null ? scart_affiliate_id : "") + "';"); }
        eval("this.document.add2cart_form_" + pid + ".form_action_value.value = '" + action_value + "';");
        if (rpid > 0) { eval("this.document.add2cart_form_" + pid + ".rpid.value = '" + rpid + "';"); }
        add2cartForm.submit();
    }
    return true;
}

// invoked when adding a product to the cart
function f_add2cart_form(pid) { return f_add2cart_form_alias1(pid, '0', false, ''); }
function f_add2cart_form_alias1(pid, rpid, isCheckOptions, msgSelectOption) {
    if (isCheckOptions) {
        // check if no option is selected and show appropriate message
        eval("form = this.document.add2cart_form_" + pid + ";");
        objectProductTotals = rpid > 0 ? recalculate_product_price_total_2(form, "_arr[" + rpid + "]") : recalculate_product_price_total(form);
        if (objectProductTotals && ! objectProductTotals.isProductReadyToOrder()) { alert(msgSelectOption); return false; }
        if (objectProductTotals && ! objectProductTotals.isProductReadyToOrderRequired()) { alert(msgSelectOption); return false; }
    }
    // get some meta info
    add2cartForm = document.forms["add2cart_form_" + pid];
    scart_referrer = getCookie("cookie_scart_referrer");
    scart_affiliate_id = getCookie("cookie_scart_affiliate_id");
    // submit the form
    eval("this.document.add2cart_form_" + pid + ".target = '';");
    if (add2cartForm.scart_referrer) { eval("this.document.add2cart_form_" + pid + ".scart_referrer.value = '" + (scart_referrer != null ? scart_referrer : "") + "';"); }
    if (add2cartForm.scart_affiliate_id) { eval("this.document.add2cart_form_" + pid + ".scart_affiliate_id.value = '" + (scart_affiliate_id != null ? scart_affiliate_id : "") + "';"); }
    eval("this.document.add2cart_form_" + pid + ".form_action_value.value = 'buy';");
    if (rpid > 0) { eval("this.document.add2cart_form_" + pid + ".rpid.value = '" + rpid + "';"); }
    onsubmitResult = add2cartForm.onsubmit();
    if ((typeof(onsubmitResult) == 'undefined') || onsubmitResult) { add2cartForm.submit(); }
    return true;
}

// onsubmit of the add2cart form
function onsubmit_add2cart_form(aForm) { 
    if (typeof(onsubmit_add2cart) == 'function') {
        onsubmitResult = onsubmit_add2cart(aForm);
        if (typeof(onsubmitResult) == 'undefined') { return true; } else { return onsubmitResult; }
    }
    return true;
}

// forward to location
function form_jumpbylocation(_location) {
    if (_location != '') {
        var regexpr = new RegExp("^http[s]?://");
        var _locationFull = regexpr.test(_location) ? _location : location_prefix + _location;
        eval("document.location='" + _locationFull + "'"); 
    }
}

// Removes leading and trailing spaces from the passed string. Also removes consecutive spaces and replaces it with one space.
function trim(inputString) {
    if (typeof inputString != "string") { return inputString; }
    var retValue = inputString;
    var ch = retValue.substring(0, 1);
    while (ch == " ") { // Check for spaces at the beginning of the string
        retValue = retValue.substring(1, retValue.length);
        ch = retValue.substring(0, 1);
    }
    ch = retValue.substring(retValue.length-1, retValue.length);
    while (ch == " ") { // Check for spaces at the end of the string
        retValue = retValue.substring(0, retValue.length-1);
        ch = retValue.substring(retValue.length-1, retValue.length);
    }
    while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
        retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
    }
    return retValue; // Return the trimmed string back to the user
}

// window functions
function getWinProps(_width, _height, _scroll, _resizable) {
    var winl = (screen.width - _width) / 2; var wint = (screen.height - _height) / 2;
    winprops = 'left='+winl+',screenX='+winl+',top='+wint+',screenY='+wint+',width='+_width+',height='+_height+',scrollbars='+_scroll+',resizable='+_resizable;
    return winprops;
}

// --- 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"; }
}
function checkCookiesEnabled(msgDisabled) {
    setCookie("scart_cookies_enabled", "yes", 1);
    if (getCookie("scart_cookies_enabled") == "yes") {
        delCookie("scart_cookies_enabled");
        return true;
    } else {
        if (msgDisabled != "") { alert(msgDisabled); }
    }
    return false;
}

// --- toggles ----------------------------------------------------------------

function PartWrapperToggle(elementName, cancelBubble) {
    var BodyElement = null;
    if (document.getElementsByName) {
        BodyElement = document.getElementsByName(elementName + "Body");
        UpImage = document.getElementsByName(elementName + "Up");
        DownImage = document.getElementsByName(elementName + "Down");
        if (BodyElement) {
            for (var i = 0; i < BodyElement.length; i++) {
                if (BodyElement[i].style.display == "none") {
                    BodyElement[i].style.display = "";
                    DownImage[i].style.display = "none";
                    UpImage[i].style.display = "";
                    setCookieAdvanced("cookie_scart_" + elementName, 1, 5*365, '/', null, null);
                } else {
                    BodyElement[i].style.display = "none";
                    DownImage[i].style.display = "";
                    UpImage[i].style.display = "none";
                    setCookieAdvanced("cookie_scart_" + elementName, 1, -5*365, '/', null, null);
                }
            }
        }
    }
    if (cancelBubble && window.event) { window.event.cancelBubble = true; }
    return false;
}
function ToggleByCookie(elementName) {
    if (getCookie("cookie_scart_" + elementName) != 1) { PartWrapperToggle(elementName, false); }
}

// --- product's price --------------------------------------------------------

function recalculate_product_price_total_qty(form, qty_field) {
    return recalculate_product_price_total_qty_2(form, qty_field, '');
}
function recalculate_product_price_total_qty_2(form, qty_field, field_suffix) {
    if ( ! form) { return null; }
    form['temp_qty_field_value' + field_suffix] = qty_field.value;
    return recalculate_product_price_total_2(form, field_suffix);
}
function recalculate_product_price_total(form) {
    return recalculate_product_price_total_2(form, '');
}
function recalculate_product_price_total_2(form, field_suffix) {
    objectProductTotals = null;
    if ( ! document.getElementById) { return objectProductTotals; }
    // get the form based on id_product
    if ( ! form) { return objectProductTotals; }
    // get the product's id
    if ( ! form['product_id' + field_suffix]) { return objectProductTotals; }
    id_product = form['product_id' + field_suffix].value;
    // some inits
    count_group_options_selected = 0;
    count_required = 0;
    count_required_selected = 0;
    count_checkboxes = 0;
    count_checkboxes_selected = 0;
    count_radiobuttons = 0;
    count_radiobuttons_selected = 0;
    count_dropdowns = 0;
    count_dropdowns_selected = 0;
    // get the product's currency digits
    currency_digits = form['product_currency_digits' + field_suffix] ? parseInt(form['product_currency_digits' + field_suffix].value) : 2;
    // get the price_total based on id_product
    price_total_field = document.getElementById("product_price_total_" + id_product);
    price_total_field_arr = document.getElementsByName("product_price_total_" + id_product);
    //if ( ! price_total_field) { return objectProductTotals; }
    // get the price_tax_total based on id_product
    price_tax_total_field = document.getElementById("product_price_tax_total_" + id_product);
    price_tax_total_field_arr = document.getElementsByName("product_price_tax_total_" + id_product);
    // get the price_list based on id_product
    price_list_total_field = document.getElementById("product_price_list_total_" + id_product);
    price_list_total_field_arr = document.getElementsByName("product_price_list_total_" + id_product);
    // get the base prices
    price_base = form['product_price_base' + field_suffix] ? parseFloat(form['product_price_base' + field_suffix].value): 0;
    price_list_base = form['product_price_list_base' + field_suffix] ? parseFloat(form['product_price_list_base' + field_suffix].value): 0;
    // include base price ?
    include_base_price = form['include_base_price' + field_suffix] ? form['include_base_price' + field_suffix].value : "yes";
    price_total = 0;
    if (form['product_price_base' + field_suffix]) { price_total = include_base_price == "yes" ? price_base : 0; }
    price_list_total = price_total;
    if (form['product_price_list_base' + field_suffix]) { price_list_total = include_base_price == "yes" ? price_list_base : 0; }
    // get the quantity, check for multiple qty fields, check for NaN
    product_qty_field = form['product_add_qty' + field_suffix];
    if (product_qty_field && product_qty_field.type == "select-one") {
        product_qty = product_qty_field.options[product_qty_field.selectedIndex].value;
    } else {
        if (product_qty_field && product_qty_field.length && form['temp_qty_field_value' + field_suffix]) {
            for (var i = 0; i < product_qty_field.length; i++) { product_qty_field[i].value = form['temp_qty_field_value' + field_suffix]; }
        }
        product_qty = product_qty_field ? parseInt(product_qty_field.length ? product_qty_field[0].value : product_qty_field.value) : 1;
    }
    product_qty = isNaN(product_qty) ? 1 : product_qty;
    // create regular expression if necessary
    if (field_suffix != "") {
        // prepare the field_suffix to be used as a regular expression create the regular expression
        fieldSuffixRegExprStr = field_suffix.replace("[", "\\["); fieldSuffixRegExprStr = fieldSuffixRegExprStr.replace("]", "\\]");
        var fieldSuffixRegExpr = new RegExp(fieldSuffixRegExprStr + "$");
    }
    // go through all form elements
    var radio_index = 0; var radio_name_old = "";
    for (var i = 0; i < form.elements.length; i++) {
        if (fieldSuffixRegExpr && ! fieldSuffixRegExpr.test(form.elements[i].name)) { continue; }
        var field_type = form.elements[i].type;
        if (field_type != "hidden") {
            // check the name of the form's field and grab the option index
            var regexpr = new RegExp("^option_([0-9]+)_value" + (field_suffix != "" ? "" : "$"));
            option_index_arr = regexpr.exec(form.elements[i].name);
            if ( ! option_index_arr) { continue; }
            // get the option's quantity
            option_qty = form['option_' + option_index_arr[1] + '_qty' + field_suffix] ? parseInt(form['option_' + option_index_arr[1] + '_qty' + field_suffix].value) : 1;
            option_qty = isNaN(option_qty) ? 1 : option_qty;
            // go by the field type
            if (field_type == "checkbox") {
                count_checkboxes++;
                if ( ! form.elements[i].checked) { continue; }
                count_checkboxes_selected++;
            } else if (field_type == "radio") {
                if (radio_name_old != form.elements[i].name) { radio_index = 0; radio_name_old = form.elements[i].name; }
                count_radiobuttons++; radio_index++;
                if ( ! form.elements[i].checked) { continue; }
                count_radiobuttons_selected++;
                // get the radio button's quantity
                option_qty_fields = form['option_' + option_index_arr[1] + '_qty' + field_suffix];
                if (option_qty_fields && option_qty_fields.length) {
                    option_qty = parseInt(option_qty_fields[radio_index - 1].value);
                    option_qty = isNaN(option_qty) ? 1 : option_qty;
                }
            } else if (field_type == "select-one") {
                count_dropdowns++;
                var hasPrice = false;
                var dropdown_value = unescape(form.elements[i].options[form.elements[i].selectedIndex].value);
                if (dropdown_value == "") { continue; }
                if (dropdown_value == "<required/>") { continue; }
                var regexpr1 = new RegExp("<price>[\.0-9]+</price>");
                hasPrice = regexpr1.test(dropdown_value);
                var regexpr2 = new RegExp("<price value=[\"'][\.0-9]+[\"']/>");
                hasPrice = hasPrice || regexpr2.test(dropdown_value);
                if (hasPrice) { count_dropdowns_selected++; }
            }
            // count the selected options in a group
            count_group_options_selected++;
            // unescape first
            value = unescape(form.elements[i].value);
            // accumulate the price of the option
            price_option = /<price>([0-9\.]+)<\/price>/i.exec(value);
            if (price_option) { price_total += option_qty * parseFloat(price_option[1]); }
            price_option = /<price value=.?([0-9\.]+).?\/>/i.exec(value);
            if (price_option) { price_total += option_qty * parseFloat(price_option[1]); }
            // accumulate the price_list of the option
            price_list_option = /<price_list value=.?([0-9\.]+).?\/>/i.exec(value);
            if (price_list_option) { price_list_total += option_qty * parseFloat(price_list_option[1]); }
        }
        // hidden field
        else {
            // check the name of the form's field
            var regexpr = new RegExp("^group_[0-9]+_required" + (field_suffix != "" ? "" : "$"));
            if ( ! regexpr.test(form.elements[i].name)) { continue; }
            if (form.elements[i].value != "yes") { count_group_options_selected = 0; continue; }
            count_required++;
            if (count_group_options_selected > 0) { count_group_options_selected = 0; count_required_selected++; }
        }
    }
    
    // change the price on the page
    var price_total_new = (product_qty*price_total).toFixed(currency_digits);
    if (price_total_field_arr && price_total_field_arr.length > 0) {
        for (var i = 0; i < price_total_field_arr.length; i++) { price_total_field_arr[i].innerHTML = price_total_new; }
    } else if (price_total_field) {
        price_total_field.innerHTML = price_total_new;
    }
    // change the price_list on the page if necessary
    var price_list_total_new = (product_qty*price_list_total).toFixed(currency_digits);
    if (price_list_total_field_arr && price_list_total_field_arr.length > 0) {
        for (var i = 0; i < price_list_total_field_arr.length; i++) { price_list_total_field_arr[i].innerHTML = price_list_total_new; }
    } else if (price_list_total_field) {
        price_list_total_field.innerHTML = price_list_total_new;
    }
    // change the price_tax on the page
    var taxinter_percent = form['product_taxinter_percent' + field_suffix] ? parseInt(form['product_taxinter_percent' + field_suffix].value) : 0;
    var price_tax_total_new = (product_qty*(price_total + price_total*(taxinter_percent/100))).toFixed(currency_digits);
    if (price_tax_total_field_arr && price_tax_total_field_arr.length > 0) {
        for (var i = 0; i < price_tax_total_field_arr.length; i++) { price_tax_total_field_arr[i].innerHTML = price_tax_total_new; }
    } else if (price_tax_total_field) {
        price_tax_total_field.innerHTML = price_tax_total_new;
    }
    // create the return object
    objectProductTotals = new ObjectProductTotals();
    objectProductTotals.setTotals(price_total, price_list_total);
    objectProductTotals.setCountRequired(count_required, count_required_selected);
    objectProductTotals.setCountCheckboxes(count_checkboxes, count_checkboxes_selected);
    objectProductTotals.setCountRadiobuttons(count_radiobuttons, count_radiobuttons_selected);
    objectProductTotals.setCountDropdowns(count_dropdowns, count_dropdowns_selected);
    //alert(objectProductTotals.toString());
    return objectProductTotals;
}

// --- product's options ------------------------------------------------------

function radio_toggle(group_radio) {
    if (group_radio.length) {
        for (var i = 0; i < group_radio.length; i++) { option_toggle(group_radio[i]); }
    } else {
        option_toggle(group_radio);
    }
}
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"; } }
}
function setAllFormFieldsWithSameNames(field_src) {
    if (field_src && field_src.form) {
        fields = field_src.form[field_src.name];
        if (fields && fields.length) {
            for (var i = 0; i < fields.length; i++) { fields[i].value = field_src.value; }
        }
    }
}

// --- verify functions -------------------------------------------------------

function isEmailCorrect(email) {
    return email.match(/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\.int)|(\.biz)|(\.info)|(\.name)|(\..{2,2}))$)\b/gi);
}
function isIPCorrect(ip) {
    return ip.match(/[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/gi);
}

// --- populate field based on the type of the field --------------------------

function populate_field_bytype(field_current, field_value) {
    if (field_current.type == "text" || field_current.type == "textarea") {
        field_current.value = field_value;
    } else if (field_current.type == "checkbox") {
        field_current.checked = field_value != "" ? 1 : 0;
    } else if (field_current.type == "select-one") {
        for (var i = 0; i < field_current.length; i++) {
            if (field_current[i].value == field_value) { field_current[i].selected = true; break; }
        }
    } else if (field_current.type == "radio") {
        field_current.checked = field_value != "" ? 1 : 0;
    } else {
        if (field_current.length) {
            for (var i = 0; i < field_current.length; i++) {
                if (field_current[i].value == field_value) { field_current[i].checked = true; break; }
            }
        }
    }
}

// return the value of the given form field
function get_field_value_bytype(field_current) {
    field_value = "";
    if (field_current.type == "text" || field_current.type == "textarea") {
        field_value = field_current.value;
    } else if (field_current.type == "checkbox") {
        field_value = field_current.checked ? field_current.value : "";
        //field_value = field_current.value;
    } else if (field_current.type == "select-one") {
        field_value = field_current.value;
    } else if (field_current.type == "radio") {
        field_value = field_current.checked ? field_current.value : "";
    } else {
        if (field_current.length) {
            for (var i = 0; i < field_current.length; i++) {
                if (field_current[i].checked) { field_value = field_current[i].value; break; }
            }
        }
    }
    return field_value;
}
// set focus to the given form field
function set_focus_bytype(field_current) {
    if (field_current.type == "text" || field_current.type == "textarea") {
        field_current.focus();
    } else if (field_current.type == "checkbox") {
        field_current.focus();
    } else if (field_current.type == "select-one") {
        field_current.focus();
    } else if (field_current.type == "radio") {
        field_current.focus();
    } else {
        if (field_current.length) {
            for (var i = 0; i < field_current.length; i++) {
                if (field_current[i].checked) { field_current[i].focus();; break; }
            }
        }
    }
    return field_value;
}

// onmouseover and onmouseout of the menu items
function onmouseover_categorytype(id_categorytype, name_categorytype, image_name) {}
function onmouseout_categorytype(id_categorytype, name_categorytype, image_name) {}
function onmouseover_category(id_category, name_category, image_name) {}
function onmouseout_category(id_category, name_category, image_name) {}
function onmouseover_subcategory(id_subcategory, name_subcategory, image_name) {}
function onmouseout_subcategory(id_subcategory, name_subcategory, image_name) {}

// onimageload
function onimageload_categorytype(image_name) {}
function onimageload_category(image_name) {}
function onimageload_subcategory(image_name) {}

// --- select tag functions ---------------------------------------------------

function selectOptionByCurrentPage(workingForm, selectFieldName) {
    currentPath = window.location.pathname;
    currentPath = currentPath.substring(currentPath.lastIndexOf("/") + 1);

    if (currentPath != '') {
        if (workingForm[0][selectFieldName]) {
            for (j = 0; j < workingForm.length; j++) {
                var selectField = workingForm[j][selectFieldName];
                for (i = 1; i < selectField.length; i++) {
                    if (selectField.options[i].value.indexOf(currentPath) == 0) {
                        selectField.options[i].selected = true; break;
                    }
                }
            }
        } else if (workingForm[selectFieldName]) {
            for (i = 1; i < workingForm[selectFieldName].length; i++) {
                if (workingForm[selectFieldName].options[i].value.indexOf(currentPath) == 0) {
                    workingForm[selectFieldName].options[i].selected = true; break;
                }
            }
        }
    }
}

// --- innerHTML helper functions ---------------------------------------------

function setInnerHTMLById(elementId, innerHTMLValue) {
    if (document.getElementById) {
        targetField = this.document.getElementById(elementId);
        targetField_arr = document.getElementsByName(elementId);
        if (targetField_arr && targetField_arr.length > 0) {
            for (var i = 0; i < targetField_arr.length; i++) { targetField_arr[i].innerHTML = innerHTMLValue; }
        } else if (targetField) {
            targetField.innerHTML = innerHTMLValue;
        }
    }
}

// --- navigation bar help functions ------------------------------------------

var navTabNameSelected = new Array();
function setSelectedNavTabName(id_navbar, navTabName) {
    navTabNameSelected[id_navbar] = navTabName;
}
function selectNavTabByName(id_navbar, navTabName) {
    return selectNavTabByNameWithSelectionClassNames(id_navbar, navTabName, "navtab_selected", "navtab_notselected");
}
function selectNavTabByNameWithSelectionClassNames(id_navbar, navTabName, classSelected, classNotSelected) {
    if (document.getElementById) {
        if (navTabNameSelected[id_navbar] && navTabNameSelected[id_navbar] != "") {
            field_arr = document.getElementsByName(navTabNameSelected[id_navbar]);
            if (field_arr && field_arr.length > 0) {
                for (var i = 0; i < field_arr.length; i++) { field_arr[i].style.display = "none"; }
            } else {
                field = document.getElementById(navTabNameSelected[id_navbar]);
                if (field) { field.style.display = "none"; }
            }
            
            field_arr = document.getElementsByName("a_" + navTabNameSelected[id_navbar]);
            if (field_arr && field_arr.length > 0) {
                for (var i = 0; i < field_arr.length; i++) { field_arr[i].className = classNotSelected; }
            } else {
                field = document.getElementById("a_" + navTabNameSelected[id_navbar]);
                if (field) { field.className = classNotSelected; }
            }
        }
        
        field_arr = document.getElementsByName(navTabName);
        if (field_arr && field_arr.length > 0) {
            for (var i = 0; i < field_arr.length; i++) { field_arr[i].style.display = "block"; }
        } else {
            field = document.getElementById(navTabName);
            if (field) { field.style.display = "block"; }
        }
        
        field_arr = document.getElementsByName("a_" + navTabName);
        if (field_arr && field_arr.length > 0) {
            for (var i = 0; i < field_arr.length; i++) { field_arr[i].className = classSelected; }
        } else {
            field = document.getElementById("a_" + navTabName);
            if (field) { field.className = classSelected; }
        }
        
        setCookieAdvanced("cookie_scart_" + id_navbar, navTabName, 1*365, '/', null, null);
        navTabNameSelected[id_navbar] = navTabName;
    }
    return false;
}

// ----------------------------------------------------------------------------
// --- Object Oriented
// ----------------------------------------------------------------------------

// --- OBJECT : product totals ------------------------------------------------

function ObjectProductTotals() {
    this.total = 0;
    this.total_list = 0;
    this.count_required = 0;
    this.count_required_selected = 0;
    this.count_checkboxes = 0;
    this.count_checkboxes_selected = 0;
    this.count_radiobuttons = 0;
    this.count_radiobuttons_selected = 0;
    this.count_dropdowns = 0;
    this.count_dropdowns_selected = 0;
}
ObjectProductTotals.prototype.setTotals = function obj_pt_setTotals(total, total_list) {
    this.total = total;
    this.total_list = total_list;
}
ObjectProductTotals.prototype.setCountRequired = function obj_pt_setRequired(count_required, count_required_selected) {
    this.count_required = count_required;
    this.count_required_selected = count_required_selected;
}
ObjectProductTotals.prototype.setCountCheckboxes = function obj_pt_setCountCheckboxes(count_checkboxes, count_checkboxes_selected) {
    this.count_checkboxes = count_checkboxes;
    this.count_checkboxes_selected = count_checkboxes_selected;
}
ObjectProductTotals.prototype.setCountRadiobuttons = function obj_pt_setCountRadiobuttons(count_radiobuttons, count_radiobuttons_selected) {
    this.count_radiobuttons = count_radiobuttons;
    this.count_radiobuttons_selected = count_radiobuttons_selected;
}
ObjectProductTotals.prototype.setCountDropdowns = function obj_pt_setCountDropdowns(count_dropdowns, count_dropdowns_selected) {
    this.count_dropdowns = count_dropdowns;
    this.count_dropdowns_selected = count_dropdowns_selected;
}
ObjectProductTotals.prototype.isProductReadyToOrder = function obj_pt_isProductReadyToOrder() {
    var isReady = (this.total != 0);
    if (this.count_checkboxes > 0) { isReady = isReady || (this.count_checkboxes_selected > 0); }
    if (this.count_radiobuttons > 0) { isReady = isReady || (this.count_radiobuttons_selected > 0); }
    if (this.count_dropdowns > 0) { isReady = isReady || (this.count_dropdowns_selected > 0); }
    isReady = isReady || ((this.count_checkboxes == 0) && (this.count_radiobuttons == 0) && (this.count_dropdowns == 0));
    return isReady;
}
ObjectProductTotals.prototype.isProductReadyToOrderRequired = function obj_pt_isProductReadyToOrderRequired() {
    return this.count_required <= this.count_required_selected;
}
/* for debug purposes only
ObjectProductTotals.prototype.toString = function obj_pt_toString() {
    var resultStr  = "total = " + this.total + "\n";
    resultStr += "total_list = " + this.total_list + "\n";
    resultStr += "count_required = " + this.count_required + "\n";
    resultStr += "count_checkboxes = " + this.count_checkboxes + "\n";
    resultStr += "count_checkboxes_selected = " + this.count_checkboxes_selected + "\n";
    resultStr += "count_radiobuttons = " + this.count_radiobuttons + "\n";
    resultStr += "count_radiobuttons_selected = " + this.count_radiobuttons_selected + "\n";
    resultStr += "count_dropdowns = " + this.count_dropdowns + "\n";
    resultStr += "count_dropdowns_selected = " + this.count_dropdowns_selected + "\n";
    return resultStr;
}*/

// --- string helper functions ------------------------------------------------

function startsWith(str, substr) {
	var reg = new RegExp("^" + substr);
	return reg.test(str);
}

function endsWith(str, substr) {
	var reg = new RegExp(substr + "$");
	return reg.test(str);
}

// --- css helper functions ---------------------------------------------------

function setClassNameByElementId(fieldId, className) {
    if (document.getElementsByName) {
        field = document.getElementById(fieldId);
        if (field) {
            field.className = className;
        }
    }
}

// --- windows helper functions -----------------------------------------------

function openNewWindow(url, winName) {
    winAttributes = "left=100,top=100,width=500,height=100,scrollbars=no,resizable=yes,toolbar=no";
    newWindow = window.open(url, winName, winAttributes);
    if (window.focus) { newWindow.focus(); }
}

function getPageSizeAttrs() {
    if (window.innerHeight) {
        pageWidth = window.innerWidth;
        pageHeight = window.innerHeight;
    } else {
        pageWidth = document.body.offsetWidth; 
        pageHeight = document.body.offsetHeight;
    }
    arrayPageSize = new Array();
    arrayPageSize["left"] = document.body.scrollLeft;
    arrayPageSize["top"] = document.body.scrollTop;
    arrayPageSize["width"] = pageWidth;
    arrayPageSize["height"] = pageHeight;
    return arrayPageSize; 
}

// returns an associative array of URL key/value pairs
function getUrlArguments(urlValue) {
    var urlArgs = new Array();
    urlValue += "";
    if (urlValue) {
        urlParts = urlValue.split('?');
        if (urlParts[1]) {
            arguments = urlParts[1].split('&');
            if (arguments[0]) {
                for (i in arguments) {
                    pair = arguments[i].split('=');
                    if (pair[1]) {
                        urlArgs[pair[0]] = pair[1];
                    }
                }
            }
        }
    }
    return urlArgs;
}

function getUrlBaseName(urlValue) {
    baseName = ""; urlValue += "";
    if (urlValue) {
        urlParts = urlValue.split('?');
        baseName = urlParts[0];
    }
    return baseName;
}

function linkAddReferrerAndForwardToOriginalLink(aLink) {
    cookieReferrer = getCookie("cookie_scart_referrer");
    if (cookieReferrer != null && cookieReferrer != "" && aLink.href) {
        urlArgs = getUrlArguments(aLink.href);
        urlArgs["referrer"] = cookieReferrer;
        urlArgsStr = "";
        for (argKey in urlArgs) { urlArgsStr += (urlArgsStr != "" ? "&" : "") + argKey + "=" + escape(urlArgs[argKey]); }
        if (urlArgsStr != "") { urlArgsStr = getUrlBaseName(window.location) + "?" + urlArgsStr; window.location = urlArgsStr; }
        return false;
    }
    return true;
}

