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;

ebr = (navigator.appName.indexOf('Microsoft') != 0) ? false : true;

global_s_word = '';
global_ar_result = new Array();
global_item_result = -1;
global_item_result_i = 0;
global_t = setTimeout('search_predicative()','1000');
global_count_result = 0;
t_end();

function t_end()
{
	clearTimeout(global_t);
}

function t_start(event)
{
	if (window.event) event = window.event;
	key = (event.keyCode ? event.keyCode : event.which ? event.which : null)
	
	if(key == 0x0D) //Enter
	{	
		document.location.href = $('div_predicative_result').childNodes[global_item_result].title;
	}
	
	if(key == 0x28) //down
	{
		for(i = 0; i < $('div_predicative_result').childNodes.length; i++)
		{
			$('div_predicative_result').childNodes[i].style.color = '#287BC1';
			$('div_predicative_result').childNodes[i].style.backgroundColor = '#ffffff';
		}
		
		global_item_result++;
		if($('div_predicative_result').childNodes[global_item_result])
		{		
			$('div_predicative_result').childNodes[global_item_result].style.color = '#ffffff';
			$('div_predicative_result').childNodes[global_item_result].style.backgroundColor = '#287BC1';
		}
		else
		{
			global_item_result--
			$('div_predicative_result').childNodes[global_item_result].style.color = '#ffffff';
			$('div_predicative_result').childNodes[global_item_result].style.backgroundColor = '#287BC1';
		}
		
	}
	
	if(key == 0x26) //Up
	{		
		for(i = 0; i < $('div_predicative_result').childNodes.length; i++)
		{
			$('div_predicative_result').childNodes[i].style.color = '#287BC1';
			$('div_predicative_result').childNodes[i].style.backgroundColor = '#ffffff';
		}
		
		global_item_result--;
		if($('div_predicative_result').childNodes[global_item_result])
		{		
			$('div_predicative_result').childNodes[global_item_result].style.color = '#ffffff';
			$('div_predicative_result').childNodes[global_item_result].style.backgroundColor = '#287BC1';
		}
		else
		{
			global_item_result++;
			$('div_predicative_result').childNodes[global_item_result].style.color = '#ffffff';
			$('div_predicative_result').childNodes[global_item_result].style.backgroundColor = '#287BC1';
		}
		
	}
	
	global_t = setTimeout('search_predicative()','400');
	global_count_result = 0;
}

function hide_all_li()
{
	for(i = 0; i < $('div_predicative_result').childNodes.length; i++)
	{
		$('div_predicative_result').childNodes[i].style.color = '#287BC1';
		$('div_predicative_result').childNodes[i].style.backgroundColor = '#ffffff';
	}
}

function search_predicative()
{
	if($('predicative_word').value.length >= 3)
	{
		if(global_s_word != $('predicative_word').value)
		{			
			$('div_predicative_result').innerHTML = '';
			
			global_count_result = 0;
			global_item_result_i = 0;
			global_item_result = -1;
			global_s_word = $('predicative_word').value;
			
			ar_temp_word = global_s_word.split(' ');
			ar_temp_word_items = new Array();
			global_ar_result = new Array();
			
			for(i = 0; i < ar_temp_word.length; i++)
			{
				ar_temp_word_items.push(false);
			}
			
			ar_goods_length = ar_goods.length;
			for(i = 0; i < ar_goods_length; i++)
			{
				current_y = true;
				for(b = 0; b < ar_temp_word.length; b++)
				{
					if(!ar_goods[i].include(ar_temp_word[b]))
					{
						current_y = false;
					}
				}
				if(current_y)
				{
					global_count_result++;
					global_ar_result.push(i);
				}
				
				if(global_count_result == 20)
				{
					break;
				}
				
			}
			
			if(global_ar_result.length > 0)
			{			
				bounds = getBounds($('predicative_word'));
				$('div_predicative_result').style.top = bounds.top + 24 + 'px';
				$('div_predicative_result').style.left = bounds.left + 'px';
				$('div_predicative_result').style.display = 'block';
				
				$('input_s_manufacter').style.visibility = 'hidden';
				
				for(i = 0; i < global_ar_result.length; i++)
				{
					$('div_predicative_result').innerHTML = $('div_predicative_result').innerHTML + '<li id="li_'+global_item_result_i+'" onclick="document.location.href = this.title" onmouseover="hide_all_li(); global_item_result = '+global_item_result_i+'; this.style.backgroundColor = \'#287BC1\'; this.style.color = \'#ffffff\'" onmouseout="this.style.backgroundColor = \'#ffffff\'; this.style.color = \'#287BC1\'" style="color:#287BC1; background-color:#ffffff; cursor:pointer" title="good.php?good_id='+ar_goods_id[global_ar_result[i]]+'">' + ar_goods[global_ar_result[i]] + '</li>';
					global_item_result_i++;
				}
			}
			
			t_end();
		}
		
	}
	else
	{
		$('input_s_manufacter').style.visibility = 'visible';
		$('div_predicative_result').style.display = 'none';
	}
}

function getBounds(element)
{
        var left = element.offsetLeft;
        var top = element.offsetTop;
        for (var parent = element.offsetParent; parent; parent = parent.offsetParent)
        {
                left += parent.offsetLeft;
                top += parent.offsetTop;
        }
        return {left: left, top: top, width: element.offsetWidth, height: element.offsetHeight};
}

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-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('img_basket'))
				{
						document.getElementById('img_basket').src = 'img/site/basket_on.jpg';
				}
                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';

        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;

        }
}