function form_jumpbyselection(_jump2page, _selObj, _key, _newforward, _doit, _id) {
    if (_selObj.options[_selObj.selectedIndex].value.indexOf("no_action") >= 0) { return false; }
    eval("document.location='" + _jump2page + "?" + _key + "=" + _selObj.options[_selObj.selectedIndex].value + "&newforward=" + _newforward + "&id=" + _id + "&doit=" + _doit + "'");
}
function form_jumpbyselection_simple(_jump2page, _selObj, _key, _doit) {
    if (_selObj.options[_selObj.selectedIndex].value.indexOf("no_action") >= 0) { return false; }
    eval("document.location='" + _jump2page + "?" + _key + "=" + _selObj.options[_selObj.selectedIndex].value + "&doit=" + _doit + "'");
}
function trim(inputString) {
   // Removes leading and trailing spaces from the passed string. Also removes
   // consecutive spaces and replaces it with one space.
   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;
}
// toggle menu
function PartWrapperToggle(elementName, cancelBubble) {
    var HeaderElement = null;
    var BodyElement = null;
    if (document.getElementsByName) {
        HeaderElement = document.getElementsByName(elementName + "Header");
        BodyElement = document.getElementsByName(elementName + "Body");
        UpImage = document.getElementsByName(elementName + "Up");
        DownImage = document.getElementsByName(elementName + "Down");
        if (BodyElement) {
            if (BodyElement[0].style.display == "none") {
                BodyElement[0].style.display = "block";
                HeaderElement[0].className = "ListNuggetHeader";
                DownImage[0].style.display = "none";
                UpImage[0].style.display = "block";
                setCookieAdvanced("cookie_scart_" + elementName, 1, -5*365, '/', null, null);
            } else {
                BodyElement[0].style.display = "none";
                HeaderElement[0].className = "ListNuggetHeaderClosed";
                DownImage[0].style.display = "block";
                UpImage[0].style.display = "none";
                setCookieAdvanced("cookie_scart_" + elementName, 1, 5*365, '/', null, null);
            }
        }
    }
    if (cancelBubble && window.event) { window.event.cancelBubble = true; }
    return false;
}
// --- product's price --------------------------------------------------------
function recalculate_product_price_total_qty(form, qty_field) {
    form.temp_qty_field_value = qty_field.value;
    recalculate_product_price_total(form);
}
function recalculate_product_price_total(form) {
    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) { return objectProductTotals; }
    id_product = form.product_id.value;
    // some inits
    count_checkboxes = 0;
    count_checkboxes_selected = 0;
    count_radiobuttons = 0;
    count_radiobuttons_selected = 0;
    // 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);
    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 ? 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 multiple qty fields, check for NaN
    product_qty_field = form.product_add_qty;
    if (product_qty_field && product_qty_field.length && form.temp_qty_field_value) {
        for (var i = 0; i < product_qty_field.length; i++) { product_qty_field[i].value = form.temp_qty_field_value; }
    }
    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;
    // 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") {
            // check the name of the form's field
            var regexpr = new RegExp("^option_[0-9]+_value$");
            if ( ! regexpr.test(form.elements[i].name)) { continue; }
            // 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") {
                count_radiobuttons++;
                if ( ! form.elements[i].checked) { continue; }
                count_radiobuttons_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 += 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
    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 ? parseInt(form.product_taxinter_percent.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.setCountCheckboxes(count_checkboxes, count_checkboxes_selected);
    objectProductTotals.setCountRadiobuttons(count_radiobuttons, count_radiobuttons_selected);
    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"; } }
}
// --- 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;
}
// common radio button functions
function getSelectedRadio(buttonGroup) {
    if (buttonGroup[0]) { // if the button group is an array (one button is not an array)
        for (var i = 0; i < buttonGroup.length; i++) { 
            if (buttonGroup[i].checked) { return buttonGroup[i]; }
        }
    } else {
        if (buttonGroup.checked) { return buttonGroup; } // if the one button is checked, return zero
    }
    // if we get to this point, no radio button is selected
    return null;
}
// dynamic write by id
function dynamicWrite(_id, _text) {
    if (document.getElementById) {
        field = document.getElementById(_id);
        if (field) { field.innerHTML = _text; }
    } else if (document.all) {
        field = document.all[_id];
        if (field) { field.innerHTML = _text; }
    }
}
// generate random key
function generateRandomKey() {
    var keylen = 16; var asckey = ""; var hexkey = "";
    for (i = 0; i < keylen; i++) { asckey = asckey + String.fromCharCode((parseInt(Math.random() * 1000) % 94) + 49); }
    for (i = 0; i < asckey.length; i++) { hexkey += asckey.charCodeAt(i).toString(16); }
    return hexkey;
}

// set the style.display of an element
function setStyleDisplay(id_element, isShow) {
    dynamic_part = document.getElementById(id_element);
    dynamic_part.style.display = isShow ? 'block' : 'none';
}

// ----------------------------------------------------------------------------
// --- Object Oriented
// ----------------------------------------------------------------------------

// --- OBJECT : product totals ------------------------------------------------
function ObjectProductTotals() {
    this.total = 0;
    this.total_list = 0;
    this.count_checkboxes = 0;
    this.count_checkboxes_selected = 0;
    this.count_radiobuttons = 0;
    this.count_radiobuttons_selected = 0;
}
ObjectProductTotals.prototype.setTotals = function obj_pt_setTotals(total, total_list) {
    this.total = total;
    this.total_list = total_list;
}
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.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); }
    isReady = isReady || ((this.count_checkboxes == 0) && (this.count_radiobuttons == 0));
    return isReady;
}
/* 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_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";
    return resultStr;
}*/