function open_printable_version(link) // opens new window
{
    var win = "menubar=no,location=no,resizable=yes,scrollbars=yes";
    newWin = window.open(link, 'printableWin', win);
    if(newWin){newWin.focus();}
}

function confirmUnsubscribe() // unsubscription confirmation
{

    temp = window.confirm(translate.cnfrm_unsubscribe);
    if (temp) // delete
    {
        window.location = "index.php?killuser=yes";
    }

}

function validate() // newsletter subscription form validation
{
    if (document.subscription_form.email.value.length < 1) {
        alert(translate.err_input_email);
        return false;
    }
    if (document.subscription_form.email.value == 'Email') {
        alert(translate.err_input_email);
        return false;
    }
    return true;
}
function validate_disc() // review form verification
{
    if (document.formD.nick.value.length < 1) {
        alert(translate.err_input_nickname);
        return false;
    }

    if (document.formD.topic.value.length < 1) {
        alert(translate.err_input_message_subject);
        return false;
    }

    return true;
}
function validate_search() {

    if (document.Sform.price1.value != ""
            && ((document.Sform.price1.value < 0) || isNaN(document.Sform.price1.value))) {
        alert(translate.err_input_price);
        return false;
    }
    if (document.Sform.price2.value != ""
            && ((document.Sform.price2.value < 0) || isNaN(document.Sform.price2.value))) {
        alert(translate.err_input_price);
        return false;
    }

    return true;
}

function validate_input_digit(e) {
    var keynum;
    var keychar;
    var numcheck;
    try {
        if (window.event) // IE
        {
            keynum = window.event.keyCode;
        } else if (e.which) // Netscape/Firefox/Opera
        {
            keynum = e.which;
        }
    } catch (exception) {
        alert(exception.message);
    }

    keynum = parseInt(keynum);
    if (keynum == 13)
        return false;// ignore Enter
    if (keynum >= 33 && keynum <= 40)
        return true;// skip navigation buttons
    if (keynum == 8)
        return true;// skip backspace
    if (keynum == 17)
        return true;// skip ctrl
    if (keynum == 45)
        return true;// skip insert
    if (keynum == 46)
        return true;// skip delete
    if (keynum >= 96 && keynum <= 105) {
        keynum -= 48;
    }
    keychar = String.fromCharCode(keynum);
    // alert('char '+keychar+'\n code '+keynum);
    numcheck = /\d/;
    return numcheck.test(keychar);
    var res = numcheck.test(keychar);
    // if(!res)alert('char '+keychar+'\n code '+keynum);
    return res;
}

Behaviour.register({

    'input.input_message' : function(e) {

        e.onfocus = function() {

            this.className = this.className.replace(/input_message/, '')
                    + ' input_message_focus';

            if (this.value != this.getAttribute('rel'))
                return;
            this.value = '';
        }

        e.onblur = function() {
            if (this.value != '')
                return;
            this.className = this.className.replace(/input_message_focus/, '')
                    + ' input_message';
            this.value = this.getAttribute('rel');
        }
    },
    

    '.add2cart_handler' : function(element) {

// SIBIRIX

        element.onclick = function() {

            var objForm = getFormByElem(this);
            if (!objForm)
                return true;

            var r_productParam = getElementsByClass('product_option', objForm);
            var r_addPrice = getElementsByClass('addPrice', objForm);
            r_productParam = r_productParam.concat(r_addPrice);

            var query = '';
            var paramVal;
            for (var i = r_productParam.length - 1; i >= 0; i--) {

                if (r_productParam[i].name) {

                    if (r_productParam[i].type == 'radio') {

                        paramID = $("input[id^=" + r_productParam[i].name + "]:checked").attr('id');
                        paramVal = paramID.substr(r_productParam[i].name.length + 1);

                    } else { 
                    
                    if (r_productParam[i].type == 'checkbox') {

                        if (!r_productParam[i].checked) { continue; }
                        paramVal = r_productParam[i].id.substr(r_productParam[i].name.length + 1);

//alert('='+r_productParam[i].id+'='+r_productParam[i].name + '='+(r_productParam[i].name.length + 1));
                        cntVal = $('#' + r_productParam[i].id + '_count').val();

                        if (!parseInt(cntVal)) { continue; }
                        query += '&_' + r_productParam[i].id + '=' + parseInt(cntVal);

                        if (!parseInt(paramVal)) continue;
                        query += '&' + r_productParam[i].id + '=' + parseInt(paramVal);
                        continue;

                    } else {

                    if (r_productParam[i].id.substr(r_productParam[i].id.length - 5, 5) == 'count') { // count for checkboxes

                        continue;

                    } else {

                        paramVal = r_productParam[i].value;
                    
                    }}}

                    if (!parseInt(paramVal)) continue;
                    query += '&' + r_productParam[i].name + '=' + parseInt(paramVal);
                }
            }


            // for textile params
            r_productParam = getElementsByClass('textileAddPrice', objForm);

            if (r_productParam.length > 0) {

                size1 = $("select[name='option_1']").val();
                category1 = $("input[name='_option_1']").val();
                if (size1 && category1 && parseInt(size1) && parseInt(category1)) {
                
                    query += '&option_1=' + parseInt(size1) + '&_option_1=' + parseInt(category1);
                }

                size2 = $("select[name='option_2']").val();
                category2 = $("input[name='_option_2']").val();
                if (size2 && category2 && parseInt(size2) && parseInt(category2)) {
                
                    query += '&option_2=' + parseInt(size2) + '&_option_2=' + parseInt(category2);
                }

                primaryColorID = $("#primaryColorID").val();
                if (primaryColorID && parseInt(primaryColorID)) { query += '&option_3=' + parseInt(primaryColorID) + '&_option_3=1'; }

                secondaryColorID = $("#secondaryColorID").val();
                if (secondaryColorID && parseInt(secondaryColorID)) { query += '&option_4=' + parseInt(secondaryColorID) + '&_option_4=2'; }
            }
            // /for textile params

            
            // for mattrasses params
            if (typeof mattressFlag != 'undefined') {

                var parts, ortoBase;
                
                lengthId = $('input[name="mattressLength"]:checked').attr('id');
                lengthVal = lengthId.substr(15, lengthId.length - 15);
                query += '&option_add_' + lengthVal + '=' + lengthVal;


                mattressPricesBaseId = $('input[name="mattressPricesBase"][type="radio"]:checked').attr('id');

                parts = mattressPricesBaseId.substr(3).split('_');
                if (typeof parts[0] != 'undefined') { // widthID

                    query += '&option_add_' + parts[0] + '=' + parts[0];
                }
                if (typeof parts[1] != 'undefined') { // matherial base ID

                    query += '&option_add_' + parts[1] + '=' + parts[1];
                }

                var orthopedicBaseID = $('.mattressTbl tr')
                    .eq(3 + matherialRowsCnt)
                    .find('input[name="mattressPrices"]:checked')
                    .attr('id');

                if (typeof orthopedicBaseID != 'undefined' && orthopedicBaseID.length > 0) {

                    parts = orthopedicBaseID.split('_');
                    if (typeof parts[1] != 'undefined') { 
                    
                        query += '&option_add_' + parts[1] + '=' + parts[1];
                    }
                    if (typeof parts[2] != 'undefined') { query += '&option_add_' + parts[2] + '=' + parts[2]; }
                }


                var $curRow = $('.mattressTbl tr').eq(4 + matherialRowsCnt);

                while ($curRow.length) {
                
                    mattressId = $($curRow).find('input[name="mattressPrices"]:checked');
                    if (mattressId.length > 0) {

                        parts = $(mattressId).attr('id').split('_');
                        if (typeof parts[1] != 'undefined') { 
                    
                            query += '&option_add_' + parts[1] + '=' + parts[1];
                        }
                        if (typeof parts[2] != 'undefined') { query += '&option_add_' + parts[2] + '=' + parts[2]; }
                        break;
                    }
            
                    $curRow = $curRow.next();
                }

                
                //if ($('#mattressDouble').val() == '1') { query += '&option_add_double=1'; }
//alert('+'+query); return false;
            }
            // /for mattrasses params
            
            
            var r_productQty = getElementByClass('product_qty', objForm);
            if (r_productQty) {
                r_productQty = parseInt(r_productQty.value);
                if (r_productQty > 1) {
                    query += '&product_qty=' + r_productQty;
                }
            }

            var url = ORIG_LANG_URL + set_query('?ukey=cart&view=noframe&action=add_product&' + query + '&productID='
                + objForm.getAttribute('rel'), '');
//alert('+'+url); return false;
            openFadeIFrame(url);
            return false;
        }

// /SIBIRIX
    },
    
    
    '.product_option' : function(e) {
        e.onchange = function() {
            var objForm = getFormByElem(this);
            if (!objForm)
                return true;

            var r_productParam = getElementsByClass('product_option', objForm);
            var price = parseFloat(getElementByClass('product_price', objForm).value);
            var list_price = 0;
            var obj = getElementByClass('product_list_price', objForm);
            if (obj)
                list_price = parseFloat(obj.value);

            for (var i = r_productParam.length - 1; i >= 0; i--) {

                var option = select_getCurrOption(r_productParam[i]);
                if (!option)
                    continue;

                price += parseFloat(option.getAttribute('rel'));
                list_price += parseFloat(option.getAttribute('rel'));
            }

            getElementByClass('totalPrice', objForm).innerHTML = formatPrice(price);
            var obj = getElementByClass('regularPrice', objForm);
            if (obj)
                obj.innerHTML = formatPrice(list_price);
            var obj = getElementByClass('youSavePrice', objForm);
            if (obj)
                obj.innerHTML = formatPrice(list_price - price)
                        + ' ('
                        + Math
                                .round((list_price - price) / list_price * 100,
                                        2) + '%)';
        }
    },


    '.hndl_proceed_checkout' : function(e) {
        e.onclick = function() {
            openFadeIFrame(ORIG_LANG_URL + set_query('?ukey=cart&view=noframe'));
            return false;
        }
    },
    'input.goto' : function(e) {
        e.onclick = function() {
            if (this.className.search(/confirm/) !== -1
                    && !window.confirm(this.getAttribute('title')))
                return
            document.location.href = this.getAttribute('rel');
        }
    },
    '.gofromfade' : function(e) {
        e.onclick = function() {

            parent.document.location.href = this.href;
            parent.closeFadeIFrame();
            return false;
        }
    },
    'input.digit' : function(e) {
        e.onkeydown = function(event) {
            return validate_input_digit(event);
        }
    }
});

Behaviour.addLoadEvent(function() {

    var totalPrices = getElementsByClass('totalPrice');
    for (var k = totalPrices.length - 1; k >= 0; k--) {

        var objForm = getFormByElem(totalPrices[k]);
        if (!objForm)
            continue;;

        var r_productParam = getElementsByClass('product_option', objForm);
        var price = parseFloat(getElementByClass('product_price', objForm).value);
        var list_price = 0;
        var obj = getElementByClass('product_list_price', objForm);
        if (obj)
            list_price = parseFloat(obj.value);

        for (var i = r_productParam.length - 1; i >= 0; i--) {

            var option = select_getCurrOption(r_productParam[i]);
            if (!option)
                continue;

            price += parseFloat(option.getAttribute('rel'));
            list_price += parseFloat(option.getAttribute('rel'));
        }
        totalPrices[k].innerHTML = formatPrice(price);
        var obj = getElementByClass('regularPrice', objForm);
        if (obj)
            obj.innerHTML = formatPrice(list_price);
        var obj = getElementByClass('youSavePrice', objForm);
        if (obj)
            obj.innerHTML = formatPrice(list_price - price) + ' ('
                    + Math.round((list_price - price) / list_price * 100, 2)
                    + '%)';
    }
});
