var ver=navigator.appVersion;
var DOM = (document.getElementById) ? 1 : 0;
var NS4 = (document.layers) ? 1 : 0;
var NS6=  (DOM && parseInt(ver) >= 5) ?1:0;
var IE4 = (document.all) ? 1 : 0;
var ver4 = (NS4 || IE4) ? 1 : 0;
      var  gotlayers = 1;
	  var x=1;
	  var y=1;
		if (document.layers){
			document.captureEvents(Event.MOUSEMOVE);
		}
		document.onmousemove = handlerMM;
var exp = new Date(); 
exp.setTime(exp.getTime() + (7*24*60*60*1000));


function confirmLink(theLink)
{
	confirmMsg = 'Вы уверены, что хотите удалить этот узел (рубрику) и все его статьи?';

    var is_confirmed = confirm(confirmMsg);
    if (is_confirmed) {
        theLink.href += '&is_js_confirmed=1';
    }

    return is_confirmed;
} // end of the 'confirmLink()' function 

function popUp_mouse(menuName,on)
{
		if (document.layers){
			document.captureEvents(Event.MOUSEMOVE);
		}
		document.onmousemove = handlerMM;
		
        if (gotlayers) {
                if (on) {

                        if (NS4) {
								document.layers[menuName].left = x;
                                document.layers[menuName].top = y;
                                document.layers[menuName].visibility = "show";
                        } else if (DOM) {
								doca = document.getElementById(menuName);	
								//childec = document.getElementsByName("child");	
//								node = document.getElementsByName("node");									
//							    node.item(0).setAttribute("value",id);
							    //childec.item(0).setAttribute("value",x);
                                doca.style.left = x+ "px";
                                doca.style.top = y+ "px";
                                document.getElementById(menuName).style.visibility = "visible";
								

								
                        } else {
							    document.all("node").value=id;
								document.all[menuName].style.Left=x;
								document.all[menuName].style.Top=y;								
                                document.all[menuName].style.visibility = "visible";

                        }
                } else {
                        if (NS4) {
                                document.layers[menuName].visibility = "hide";
                        } else if (DOM) {
                                document.getElementById(menuName).style.visibility = "hidden";
                        } else {
                                document.all[menuName].style.visibility = "hidden";
                        }
                }

        }
}

function popUp(menuName,on) {
        if (gotlayers) {
                if (on) {

                        if (NS4) {
                                document.layers[menuName].visibility = "show";
                        } else if (DOM) {
                                document.getElementById(menuName).style.visibility = "visible";
                        } else {
                                document.all[menuName].style.visibility = "visible";
                        }
                } else {
                        if (NS4) {
                                document.layers[menuName].visibility = "hide";
                        } else if (DOM) {
                                document.getElementById(menuName).style.visibility = "hidden";
                        } else {
                                document.all[menuName].style.visibility = "hidden";
                        }
                }

        }
}


function handlerMM(e){
	x = (NS4||NS6) ? e.pageX : document.body.scrollLeft+event.clientX-200
	y = (NS4||NS6) ? e.pageY : document.body.scrollTop+event.clientY
}

function popUp_cart(on)
{
		check_empty();
        menuName="cart";
		if (document.layers){
			document.captureEvents(Event.MOUSEMOVE);
		}
		document.onmousemove = handlerMM;
		
        if (gotlayers) {
                if (on) {

                        if (NS4) {
								document.layers[menuName].left = x;
                                document.layers[menuName].top = y;
                                document.layers[menuName].visibility = "show";
                        } else if (DOM) {
								doca = document.getElementById(menuName);	
								//childec = document.getElementsByName("child");	
//								node = document.getElementsByName("node");									
//							    node.item(0).setAttribute("value",id);
							    //childec.item(0).setAttribute("value",x);
                                doca.style.left = x+ "px";
                                doca.style.top = y+ "px";
                                document.getElementById(menuName).style.visibility = "visible";
								

								
                        } else {
							    document.all("node").value=id;
								document.all[menuName].style.Left=x;
								document.all[menuName].style.Top=y;								
                                document.all[menuName].style.visibility = "visible";

                        }
                } else {
                        if (NS4) {
                                document.layers[menuName].visibility = "hide";
                        } else if (DOM) {
                                document.getElementById(menuName).style.visibility = "hidden";
                        } else {
                                document.all[menuName].style.visibility = "hidden";
                        }
                }

        }
}

function clear_cart()
{
	if (document.all) 
	{
   		document.all("spisok").innerHTML = '';
	}
	else
	{
		document.getElementById("spisok").innerHTML = '';
	}
	DeleteCookie('goods');
	check_empty(); // чтобы сработало, блин
}

function check_empty()
{
	var goods=GetCookie('goods');
	if (goods==null) goods="";
				if (DOM) {
								document.getElementById("spisok").innerHTML=goods;
								if (document.getElementById("spisok").innerHTML == "")
								{
									document.getElementById("td_order").innerHTML = '<span class="link1234">Ваша корзина пока пуста...</span>';
								}
								else
								{
									document.getElementById("td_order").innerHTML = '<a href="order.php" class="link1234">Заказать!</a>';
								}
				} else	{
								document.all("spisok").innerHTML=goods;
								if (document.all("spisok").innerHTML == "")
								{
									document.all("td_order").innerHTML = '<span class="link1234">Ваша корзина пока пуста...</span>';
								}
								else
								{
									document.all("td_order").innerHTML = '<a href="order.php" class="link1234">Заказать!</a>';
								}
				}
}

	
function GetCookie (name) {  
var arg = name + "=";  
var alen = arg.length;  
var clen = document.cookie.length;  
var i = 0;  
while (i < clen) {    
var j = i + alen;    
if (document.cookie.substring(i, j) == arg)      
return getCookieVal (j);    
i = document.cookie.indexOf(" ", i) + 1;    
if (i == 0) break;   
}  
return null;
}

function getCookieVal(offset) {
var endstr = document.cookie.indexOf (";", offset);
if (endstr == -1)
endstr = document.cookie.length;
return unescape(document.cookie.substring(offset, endstr));
}

function SetCookie (name, value) {  
var argv = SetCookie.arguments;  
var argc = SetCookie.arguments.length;  
var expires = (argc > 2) ? argv[2] : null;  
var path = (argc > 3) ? argv[3] : null;  
var domain = (argc > 4) ? argv[4] : null;  
var secure = (argc > 5) ? argv[5] : false;  
document.cookie = name + "=" + escape (value) + 
((expires == null) ? "" : ("; expires=" + expires.toGMTString())) + 
((path == null) ? "" : ("; path=" + path)) +  
((domain == null) ? "" : ("; domain=" + domain)) +    
((secure == true) ? "; secure" : "");
}

function DeleteCookie (name) {  
var exp = new Date();  
exp.setTime (exp.getTime() - 1);  
var cval = GetCookie (name);  
document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}

function add_to_cart(id_add, text_add, action)
{
	begin_text = "<!--id_goods:" + id_add + "-->";
	end_text = ' <a href="javascript:add_to_cart(\'' + id_add + '\' , \'' + text_add + '\',\'del\')"><img src="template/bosng/images/del_old.gif" alt="удалить" border="0"></a><br>';
	reg_str='<!--id_goods:' + id_add + '-->(.*?)<br>';

		if (action != "add")
		{
			text_total="";
		}
		else
		{
			text_total = begin_text + text_add + end_text;
		}

	var goods=GetCookie('goods');
	if (goods == null) goods="";
	goods = goods.replace( new RegExp( reg_str, "gim"), "" );	
	goods = goods + text_total;

	if (document.all) 
	{
		document.all("spisok").innerHTML = goods;
	}
	else
	{
		document.getElementById("spisok").innerHTML = goods;
	}
		SetCookie('goods',goods,exp);
		check_empty();
		if (action == "add")
		{	
			popUp_cart(true);
		}
}

function del_item (id_add)
{
	reg_str='<!--id_goods:' + id_add + '-->(.*?)<br>';

	var goods=GetCookie('goods');
	if (goods == null) goods="";
	goods = goods.replace( new RegExp( reg_str, "gim"), "" );	
	
	name_td="id__" + id_add;
	 
	if (document.all) 
	{
		for (i=1;i<5;i++)
		{
			name_td="id" + i + "_goods_" + id_add;
			document.all(name_td).innerHTML = "";
		}
	}
	else
	{
		document.getElementById(name_td).innerHTML = "";
	}
		SetCookie('goods',goods,exp);
		re_summa();
}
function re_summa()
{
	sum = 0;
	for (i = 0 ; i < 100 ; i++)
	{
			kolvoname="kolvo_" + i;
			pricename="price_ok_" + i;
		if (document.all) 
		{
			if (document.all(kolvoname))
			{
				var kolvo = document.all(kolvoname).value;	
				var price_ok = document.all(pricename).value;
				sum = sum + price_ok*kolvo;
			}
			else
			{
				break;
			}
			
		}
		else
		{
//alert (sum + " " + price_ok + " " + kolvo + "pricename:" + pricename);
			var theDV = document.forms["order_it"];
			if (theDV.elements[kolvoname])
			{
				kolvo=theDV.elements[kolvoname].value;
				price_ok = theDV.elements[pricename].value;
				sum = sum + price_ok*kolvo;
			}
		}

		
		

	}
	sum="<b>Сумма заказа: "+sum + "$</b>";
	if (document.all) 
	{
		document.all("summa").innerHTML = sum;
	}
	else
	{
		document.getElementById("summa").innerHTML = sum;
	}
}