var agt = navigator.userAgent.toLowerCase ();
var is_major = parseInt (navigator.appVersion);
var is_minor = parseFloat (navigator.appVersion);
var cmp_window=false;
var is_nav  = ((agt.indexOf ('mozilla') != -1) && (agt.indexOf ('spoofer') == -1) && (agt.indexOf ('compatible') == -1) && (agt.indexOf ('opera') == -1) && (agt.indexOf ('webtv') == -1) && (agt.indexOf ('hotjava') == -1) && (agt.indexOf ('safari') == -1));
var is_nav6 = (is_nav && (is_major == 5) && (agt.indexOf ("netscape") != -1) && (agt.indexOf ("netscape/7") == -1));
var is_nav6up = ((is_nav && (is_major >= 5)) || (is_nav && (agt.indexOf ('netscape/7') != -1)));
var is_ie      = ((agt.indexOf ("msie") != -1) && (agt.indexOf ("opera") == -1) && (agt.indexOf ("safari") == -1));
var is_firefox = ((agt.indexOf ("firefox") != -1) && (agt.indexOf ("opera") == -1) && (agt.indexOf ("safari") == -1));
var is_opera = (agt.indexOf ("opera") != -1);
var is_nn = ((is_nav6 || is_nav6up) ? true : false);
var all_itogo = 0;

function open_win_basket()
{
        win_basket = window.open('basket.php','basket_win','resizable=yes,scrollbars=yes,center=yes,width=720,height=600,left=500,screenX=500');
        //win_basket.document.location.reload();
        //win_basket.focus();
}

function add_good_basket(good_id,len)
{
                //len = stringReplace(len,' ','');
        len = parseInt(len);
        if(!isNaN(len))
        {
                var basket_all_id = getCookie('basket_all_id');
                //alert(basket_all_id);
                if(basket_all_id != null || basket_all_id != '')
                {
                        //alert('b');
                        basket_all_id = basket_all_id + good_id +':::'+len+',';
                        document.cookie='basket_all_id='+basket_all_id+'; path=/; expires=Mon, 01-Jan-2010 00:00:00 GMT'
                        //clearInterval(basket_win.interval);
                }
                else
                {
                        //alert('a');
                        document.cookie='basket_all_id='+good_id+':::'+len+',; path=/; expires=Mon, 01-Jan-2010 00:00:00 GMT'
                }
                if(document.getElementById('basket_link'))
                {
                        current_len = getCookie('basket_all_id');
                        ar_len = explode(',',current_len);
                        end_len = 0;
                        //end_len = ar_len.length - 1;
                        for(i = 0; i < ar_len.length - 1; i++)
                        {
                                ar_temp = explode(':::',ar_len[i])
                                end_len += parseInt(ar_temp[1]);
                        }
                        document.getElementById('basket_link').innerHTML = 'Ваша корзина: <a href="#" style="font-size: 12px; color: rgb(74, 143, 185); font-weight: bold; font-family: Arial,Helvetica,sans-serif;" onclick="open_win_basket()">Перейти</a><br>Товаров в корзине: ' + end_len;
                }
                open_win_basket();
        }
}

function stringReplace(originalString, findText, replaceText)
{
        var pos = 0;
        var len = findText.length ;
        pos = originalString.indexOf(findText) ;
        while (pos != -1)
        {
                preString = originalString. substring (0 , pos);
                poststring = originalString. substring (pos + len, originalString. length) ;
                originalString = preString + replaceText + poststring;
                pos = originalString. indexOf (findText) ;
        }
        return originalString;
}

function delete_good_basket(good_id)
{

        all_goods_id.splice(good_id,1,'asd');
        document.getElementById('len_'+good_id).value = 'asd';
        var str = '';
        var itogo = 0;
        var flag_empty = true;
        for(i = 0; i < all_goods_id.length; i++)
        {
                if(all_goods_id[i] != null && all_goods_id[i] != 'asd')
                {
                        str = str + i+':::'+document.getElementById('len_'+i).value+',';
                        itogo = itogo + parseInt(document.getElementById('all_price_len_'+i).outerText);
                        flag_empty = false;
                }
        }

        if(flag_empty)
        {
                if(opener.document.getElementById('basket_link'))
                {
                        opener.document.getElementById('basket_link').innerHTML = 'Ваша корзина: Пуста<br />&nbsp;';
                }
                window.close();
        }

        //alert(str);
        document.cookie='basket_all_id='+str+'; path=/; expires=Mon, 01-Jan-2010 00:00:00 GMT'
        document.getElementById('itogo').innerText = itogo;
        document.getElementById('tr_'+good_id).style.display = 'none';

        current_len = getCookie('basket_all_id');
        ar_len = explode(',',current_len);
        end_len = 0;
        for(i = 0; i < ar_len.length - 1; i++)
        {
             ar_temp = explode(':::',ar_len[i])
             end_len += parseInt(ar_temp[1]);
        }
        opener.document.getElementById('basket_link').innerHTML = 'Ваша корзина: <a href="#" style="font-size: 12px; color: rgb(74, 143, 185); font-weight: bold; font-family: Arial,Helvetica,sans-serif;" onclick="open_win_basket()">Перейти</a><br>Товаров в корзине: ' + end_len;

        converting();
}

function converting()
{
        all_input = document.forms.save_buy.getElementsByTagName('input');
        document.getElementById('itogo').innerText = '0';

        ff_all_itogo = 0;
        for(i = 0; i < all_input.length; i++)
        {
                len = all_input[i].value;
                len = parseInt(len);
                if(!isNaN(len))
                {
                        if(is_firefox)
                        {
                                all_price = parseInt(document.getElementById('price_'+all_input[i].name).childNodes[0].nodeValue) * parseInt(all_input[i].value);

                                document.getElementById('all_price_'+all_input[i].name).innerHTML = all_price;

                                ff_all_itogo += all_price;

                                all_input[i].value = len;
                        }
                        else
                        {
                                all_price = parseInt(document.getElementById('price_'+all_input[i].name).outerText) * parseInt(all_input[i].value);

                                document.getElementById('all_price_'+all_input[i].name).innerText = all_price;
                                document.getElementById('itogo').innerText = parseInt(document.getElementById('itogo').innerText) + all_price;

                                all_itogo = parseInt(document.getElementById('itogo').innerText) + all_price;

                                all_input[i].value = len;
                        }
                }
                else
                {
                        document.getElementById('all_price_'+all_input[i].name).innerText = '0';

                }

        }

        item_sposob = document.forms.buy_1.delivery.options[document.forms.buy_1.delivery.selectedIndex].value;

        if(is_firefox)
        {
                document.getElementById('itogo').innerHTML = ff_all_itogo;
                all_itogo = ff_all_itogo;

                if(all_itogo < 10000 && item_sposob == '1')
                {
                        //document.getElementById('price_dostavka').innerHTML = 'Доставка по Москве<br /> в пределах МКАД<br /> состовляет 250 рублей';
                        //document.getElementById('price_dostavka').style.color = 'black';
                }
                if(all_itogo >= 10000 && item_sposob == '1')
                {
                        //document.getElementById('price_dostavka').innerHTML = 'Бесплатная доставка<br /> по Москве<br /> в пределах МКАД';
                        //document.getElementById('price_dostavka').style.color = 'green';
                }
        }
        else
        {
                if(parseInt(document.getElementById('itogo').innerText) < 10000 && item_sposob == '1')
                {
                        //document.getElementById('price_dostavka').innerHTML = 'Доставка по Москве<br /> в пределах МКАД<br /> состовляет 250 рублей';
                        //document.getElementById('price_dostavka').style.color = 'black';
                }
                if(parseInt(document.getElementById('itogo').innerText) >= 10000 && item_sposob == '1')
                {
                        //document.getElementById('price_dostavka').innerHTML = 'Бесплатная доставка<br /> по Москве<br /> в пределах МКАД';
                        //document.getElementById('price_dostavka').style.color = 'green';
                }
        }
}

function save_len()
{
        str = '';
        for(i = 0; i < all_goods_id.length; i++)
        {
                if(all_goods_id[i] != null && all_goods_id[i] != 'asd')
                {
                        str = str + i+':::'+document.getElementById('len_'+i).value+',';
                }
        }
        //alert(str);
        document.cookie='basket_all_id='+str+'; path=/; expires=Mon, 01-Jan-2010 00:00:00 GMT';

        current_len = getCookie('basket_all_id');
        ar_len = explode(',',current_len);
        end_len = 0;
        for(i = 0; i < ar_len.length - 1; i++)
        {
             ar_temp = explode(':::',ar_len[i])
             end_len += parseInt(ar_temp[1]);
        }
        opener.document.getElementById('basket_link').innerHTML = 'Ваша корзина: <a href="#" style="font-size: 12px; color: rgb(74, 143, 185); font-weight: bold; font-family: Arial,Helvetica,sans-serif;" onclick="open_win_basket()">Перейти</a><br>Товаров в корзине: ' + end_len;
}

function save_orders()
{
        all_input = document.forms.save_buy.getElementsByTagName('input');
        str = '';
        for(i = 0; i < all_input.length; i++)
        {
                len = all_input[i].value;
                len = parseInt(len);
                if(!isNaN(len))
                {
                        input_name = all_input[i].name;
                        input_name = stringReplace(input_name,'len_','');

                        //document.getElementById('price_'+all_input[i].name).childNodes[0].nodeValue
                        if(is_firefox)
                        {

                                all_price = parseInt(document.getElementById('price_'+all_input[i].name).childNodes[0].nodeValue) * parseInt(all_input[i].value);

                                str = str + document.getElementById('name_good_'+input_name).childNodes[0].nodeValue+':::'+parseInt(all_input[i].value)+':::'+all_price+':::'+document.getElementById('artikul_good_'+input_name).childNodes[0].nodeValue+':::'+document.getElementById('id_good_'+input_name).childNodes[0].nodeValue+':::'+document.getElementById('nalichie_good_'+input_name).childNodes[0].nodeValue+'|';
                                //str = str + document.getElementById('name_good_'+input_name).outerText+':::'+parseInt(all_input[i].value)+':::'+all_price+':::'+document.getElementById('artikul_good_'+input_name).outerText+':::'+document.getElementById('id_good_'+input_name).outerText+':::'+document.getElementById('nalichie_good_'+input_name).outerText+'|';
                                //alert(str);
                        }
                        else
                        {
                                all_price = parseInt(document.getElementById('price_'+all_input[i].name).outerText) * parseInt(all_input[i].value);

                                str = str + document.getElementById('name_good_'+input_name).outerText+':::'+parseInt(all_input[i].value)+':::'+all_price+':::'+document.getElementById('artikul_good_'+input_name).outerText+':::'+document.getElementById('id_good_'+input_name).outerText+':::'+document.getElementById('nalichie_good_'+input_name).outerText+'|';
                        }

                }
        }

        document.forms.buy_1.good.value = str;
        document.forms.buy_1.submit();
}

function switch_text_dostavka(id)
{
        switch(id)
        {
                case '1' :
                {
                        converting();
                }
                break;
                case '2' :
                {
                        //document.getElementById('price_dostavka').innerHTML = 'Стоимость доставки в ваш регион уточнит менеджер при оформлении заказа';
                        //document.getElementById('price_dostavka').style.color = 'black';
                }
                break;
                case '3' :
                {
                        //document.getElementById('price_dostavka').innerHTML = 'Стоимость доставки в ваш регион уточнит менеджер при оформлении заказа';
                        //document.getElementById('price_dostavka').style.color = 'black';
                }
                break;

        }
}

function explode( delimiter, string, limit ) {
    // http://kevin.vanzonneveld.net
    // +     original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +     improved by: kenneth
    // +     improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +     improved by: d3x
    // +     bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // *     example 1: explode(' ', 'Kevin van Zonneveld');
    // *     returns 1: {0: 'Kevin', 1: 'van', 2: 'Zonneveld'}
    // *     example 2: explode('=', 'a=bc=d', 2);
    // *     returns 2: ['a', 'bc=d']

    var emptyArray = { 0: '' };

    // third argument is not required
    if ( arguments.length < 2 ||
        typeof arguments[0] == 'undefined' ||
        typeof arguments[1] == 'undefined' )
    {
        return null;
    }

    if ( delimiter === '' ||
        delimiter === false ||
        delimiter === null )
    {
        return false;
    }

    if ( typeof delimiter == 'function' ||
        typeof delimiter == 'object' ||
        typeof string == 'function' ||
        typeof string == 'object' )
    {
        return emptyArray;
    }

    if ( delimiter === true ) {
        delimiter = '1';
    }

    if (!limit) {
        return string.toString().split(delimiter.toString());
    } else {
        // support for limit argument
        var splitted = string.toString().split(delimiter.toString());
        var partA = splitted.splice(0, limit - 1);
        var partB = splitted.join(delimiter.toString());
        partA.push(partB);
        return partA;
    }
}

function getCookie(name)
{
        var cookie = ' ' + document.cookie;
        var search = ' ' + name + '=';
        var setStr = null;
        var offset = 0;
        var end = 0;
        if(cookie.length > 0)
        {
                offset = cookie.indexOf(search);
                if(offset != -1)
                {
                        offset += search.length;
                        end = cookie.indexOf(';', offset)
                        if(end == -1)
                        {
                                end = cookie.length;
                        }
                        setStr = unescape(cookie.substring(offset, end));
                }
        }
        return(setStr);
}
