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('http://www.neograd.ru/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-2020 00:00:00 GMT'
                        //clearInterval(basket_win.interval);
                }
                else
                {
                        //alert('a');
                        document.cookie='basket_all_id='+good_id+':::'+len+',; path=/; expires=Mon, 01-Jan-2020 00:00:00 GMT'
                }
                if(document.getElementById('info_from_basket'))
                {
                        current_cookie = getCookie('basket_all_id');
                        current_cookie_len = current_cookie.split(',');
                        current_cookie_len.pop();

                        document.getElementById('info_from_basket').innerHTML = 'Товаров в корзине: '+current_cookie_len.length+'<br />Перейти в <a href="javascript: void(open_win_basket())">корзину</a>';
                }
                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)
        {
                //opener.document.getElementById('img_basket').src = 'img/site/basket.jpg';
                window.close();
        }

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

        if(document.getElementById('info_from_basket'))
        {
                current_cookie = getCookie('basket_all_id');
                current_cookie_len = current_cookie.split(',');
                current_cookie_len.pop();

                document.getElementById('info_from_basket').innerHTML = 'Товаров в корзине: '+current_cookie_len.length+'<br />Перейти в <a href="javascript: void(open_win_basket())">корзину</a>';
        }

        converting();
}

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

        ff_all_itogo = 0;

        //document.getElementById('test_test').innerHTML = all_input[0].name;
        //document.getElementById('test_test').innerHTML = document.getElementById('test_test').innerHTML + all_input[1].name;

        for(i = 0; i < all_input.length; i++)
        {
                len = all_input[i].value;
                len = parseInt(len);
                if(!isNaN(len))
                {
                        if(is_firefox)
                        {
                                //alert(all_input.length);
                                //temp_x = document. getElementById('price_'+all_input[i].name);
                                //ffobj = new XMLSerializer();
                                //temp_str = ffobj.serializeToString(document.getElementById('price_'+all_input[i].name));

                                //a1 = temp_str;

                                //a1 = stringReplace(a1,'<DIV id="qwe">','');
                                //a1 = stringReplace(a1,'</DIV>','');

                                //document.getElementById('test_test').innerHTML += ' '+ a1;


                                //document.getElementById('test_test').innerHTML += document.getElementById('price_'+all_input[i].name).childNodes[0].nodeValue;

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

                                //document.getElementById('test_test').innerHTML = all_price;

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

                                ff_all_itogo += all_price;

                                //document.getElementById('test_test').innerHTML = document.getElementById('itogo').childNodes[0].nodeValue + all_price;

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

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 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);
}