// 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() {
    var referrer = document.referrer;
    if (referrer != "") {
        referrer_current = getCookie("cookie_scart_referrer");
        if (referrer_current == null) { setCookieAdvanced("cookie_scart_referrer", referrer, 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 moving a product to the cart
function f_move2cart_form(pid) {
    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'; }
    }
    // open window
    window_move2cart = window.open('', 'win_move2cart', 'left=50,screenX=50,top=50,screenY=50,width=500,height=150,scrollbars=0,resizable=1');
    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
    scart_referrer = getCookie("cookie_scart_referrer");
    // submit the form
    eval("this.document.add2cart_form_" + pid + ".target = 'win_move2cart';");
    eval("this.document.add2cart_form_" + pid + ".scart_referrer.value = '" + (scart_referrer != null ? scart_referrer : "") + "';");
    eval("this.document.add2cart_form_" + pid + ".form_action_value.value = '" + action_value + "';");
    eval("this.document.add2cart_form_" + pid + ".submit();");
    return true;
}
// invoked when adding a product to the cart
function f_add2cart_form(pid) {
    // get some meta info
    scart_referrer = getCookie("cookie_scart_referrer");
    // submit the form
    eval("this.document.add2cart_form_" + pid + ".target = '';");
    eval("this.document.add2cart_form_" + pid + ".scart_referrer.value = '" + (scart_referrer != null ? scart_referrer : "") + "';");
    eval("this.document.add2cart_form_" + pid + ".form_action_value.value = 'buy';");
    eval("this.document.add2cart_form_" + pid + ".submit();");
    return true;
}
// forward to location
function form_jumpbylocation(_location) {
    if (_location != '') { eval("document.location='" + location_prefix + _location + "'"); }
}
// 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;
}
function checkWin(win_opened) { if ( ! win_opened) { showPopupBlockerMsg(); return false; } return true; }
function showPopupBlockerMsg() { alert("Please turn off the ad/popup blocker to allow the requested function."); }
function showNoPopupBlockerMsg() { alert("It seems you do not have a problematic Ad/Popup blocker turned on for your browser.\nYou should be able to work with the webpages without any problems."); }
// --- 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,resizable=1');
    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"; }
}
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;
}
// toggle menu
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(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; }
    price_list_total_field = document.getElementById("product_price_list_total_" + id_product);
    // get the base prices
    price_base = form.product_price_base ? parseFloat(form.product_price_base.value): 0;
    price_list_base = form.product_price_list_base ? parseFloat(form.product_price_list_base.value): 0;
    // include base price ?
    include_base_price = form.include_base_price ? form.include_base_price.value : "yes";
    price_total = 0;
    if (form.product_price_base) { price_total = include_base_price == "yes" ? price_base : 0; }
    price_list_total = price_total;
    if (form.product_price_list_base) { price_list_total = include_base_price == "yes" ? price_list_base : 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; }
            } else if (field_type == "radio") {
                if ( ! form.elements[i].checked) { continue; }
            }
            // 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 += parseFloat(price_option[1]); }
            price_option = /<price value=.?([0-9\.]+).?\/>/i.exec(value);
            if (price_option) { price_total += 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 += parseFloat(price_list_option[1]); }
        }
    }
    // change the price on the page
    price_total_field.innerHTML = (product_qty*price_total).toFixed(currency_digits);
    // change the price_list on the page if necessary
    if (price_list_total_field) { price_list_total_field.innerHTML = (product_qty*price_list_total).toFixed(currency_digits); }
}
// --- product's options ------------------------------------------------------
function radio_toggle(group_radio) {
    for (var i = 0; i < group_radio.length; i++) { option_toggle(group_radio[i]); }
}
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"; } }
}
// --- verify functions -------------------------------------------------------
function isEmailCorrect(email) {
    return email.match(/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\.int)|(\.biz)|(\.info)|(\..{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;
}