var AjHtml="";
// ALERT & POPUP

ns4 = document.layers;   
ie4 = document.all;   
nn6 = document.getElementById && !document.all; 

function showObject(id,totime) {  
	totime = totime || 0;
	
 
  if (ns4) {   
     document.eval(id).visibility = "show";   
  }   
  else if (ie4) {   
     document.all[id].style.visibility = "visible";   
  }   
  else if (nn6) {   
     document.getElementById(id).style.visibility = "visible";   
  }   
	
	if (totime>0) {
			setTimeout('hideObject("'+id+'")',totime);
	}	

}


function hideObject(id) {   
  if (ns4) {   
     document.eval(id).visibility = "hide";   
  }   
  else if (ie4) {   
     document.all[id].style.visibility = "hidden";   
  }   
  else if (nn6) {   
     document.getElementById(id).style.visibility = "hidden";   
  }   
}
function alert3s (text){
	var id="alertn1"
	text="<div align=right><a href=\"javascript:hideObject('alertn1');\">X</a></div><table height=60 width=300 border=0><tr><td valign=middle align=center>"+text+"</table>";
	var top = (document.body.clientHeight - 200)/2, left = (document.body.clientWidth-300)/2;

	if (ns4) {   
     document.eval(id).innerHTML = text;  
     document.eval(id).style.left=left; 
  }   
  else if (ie4) {   
     document.all[id].innerHTML = text;   
     document.all[id].style.left=left;
  }   
  else if (nn6) {   
     document.getElementById(id).innerHTML = text;   
     document.getElementById(id).style.left=left;   
  }  
  
	

	
		showObject('alertn1',3000);
	
	



}	





// END ALERT & POPUP
function ajaxRequest(){
 var activexmodes=["Msxml2.XMLHTTP", "Microsoft.XMLHTTP"] //activeX versions to check for in IE
 if (window.ActiveXObject){ //Test for support for ActiveXObject in IE first (as XMLHttpRequest in IE7 is broken)
  for (var i=0; i<activexmodes.length; i++){
   try{
    return new ActiveXObject(activexmodes[i])
   }
   catch(e){
    //suppress error
   }
  }
 }
 else if (window.XMLHttpRequest) // if Mozilla, Safari etc
  return new XMLHttpRequest()
 else
  return false
}


function showOrderList () {
	var nc=( Math.random());
	
var mygetrequest=new ajaxRequest()
mygetrequest.onreadystatechange=function(){
 if (mygetrequest.readyState==4){
  if (mygetrequest.status==200){
   

   showOrderPopup('alertn2',mygetrequest.responseText);
   
   
  }
  else{
   alert("Нет связи с сервером. Код: "+mygetrequest.status)
  }
 }
}
	var myhostname="http://"+window.location.hostname
	mygetrequest.open("GET", myhostname+"/order.html?action=showtextorder4js&nc="+nc, true)
	mygetrequest.send(null)
   
}
function showOrderPopup(id,text) {
	var top = (document.body.clientHeight - 500)/2, left = (document.body.clientWidth - 500)/2;
	text="<div align=right><a href=\"javascript:hideObject('alertn2');\">X</a>&nbsp;</div><div style='overflow:auto;width: 500px;height:110px;'><table height=60 width=100% border=0><tr><td valign=middle align=left>"+text+"</table></div><div align=right><a href=/buy.html class=order>оформить заказ</a>&nbsp;</div> ";

	if (ns4) {   
     document.eval(id).innerHTML = text;  
     document.eval(id).style.left=left; 
  }   
  else if (ie4) {   
     document.all[id].innerHTML = text;   
     document.all[id].style.left=left;
  }   
  else if (nn6) {   
     document.getElementById(id).innerHTML = text;   
     document.getElementById(id).style.left=left;   
  }  
  
	

	
		showObject('alertn2',30000);
	;
	
	
}		
	






///


function addItem (idt,thisRef) {
	var ids = getCookie("idit");	
	var IDar=new Array();
	
	thisRef = thisRef || 0;
	//alert(thisRef.value);
	if (thisRef) {
		thisRef.disabled=true;
	}
	
	
	if (ids) {
		IDar=ids.split(",");
		//уже в корзине		
		for (i=0; i< IDar.length; i++) {
			if (IDar[i] == idt) {
				showOrder();
				alert3s("Товар уже в корзине!");
				return false;
			}	
		}	
						alert3s("Товар добавлен в корзину!");
		setCookie("idit",ids+","+idt, "+1M", "/");
		showOrder();
		return true;
	}
			

	alert3s("Товар добавлен в корзину!");
	setCookie("idit",idt, "+1M", "/");
	showOrder();
	

	
	return true;
}	



function removeItem (id) {
	var ids = getCookie("idit");	
	var IDar=new Array();
	var ids2="";
	if (ids) {
		IDar=ids.split(",");
		//есть в корзине		
		for (i=0; i< IDar.length; i++) {
			if (IDar[i] != id) {
				ids2=ids2+IDar[i]+",";
			} else {
								alert3s("Товар удален из корзины!");
			}	
			
		}	
		ids2=ids2.substr(0, (ids2.length) -1);
		setCookie("idit",ids2, "+1M", "/");
		
		
	} 
	hideObject('alertn2');
	showOrderList()
	showOrder();
	return true;
}	


function showOrder() {
	showOrderHtml ();
	//setTimeout(showOrderHtml,3000);
	return true;
}	

function showOrderHtml () {
	var ids = getCookie("idit");
	var IDar=new Array();
	//alert(ids);
	if (!ids ) {
		document.getElementById('orderArea').innerHTML	= "нет товара";

	}	else {
			//alert(ids+" =-== ");
		IDar=ids.split(",");
		var col=IDar.length;
		var ost=col%10;
		var tovaro='товаров';
		if (ost==1) 
		 tovaro='товар'
		else if (ost>1 && ost<5)
			tovaro='товара';
		
		if (col>10 && col<21)
			tovaro='товаров';

		//var nc=( Math.random());
		//alert(nc);
		//req="action=showtextorder4js&nc="+nc;
		//ajaxpack.getAjaxRequest("/order.html",req, processGetPost, "txt");
		//alert(AjHtml);


		document.getElementById('orderArea').innerHTML	= "<a class=orderbig href=javascript:; onclick='showOrderList()' title='Смотреть выбранные товары'>"+IDar.length+" "+tovaro+"</a> <br><a href=/buy.html class=order><b>Оформить заказ</b></a>  <a href=javascript:; onclick='clearAll();return false;'  class=order>Очистить корзину</a>";

	}	


	
}	


function processGetPost(){
var myajax=ajaxpack.ajaxobj
var myfiletype=ajaxpack.filetype
if (myajax.readyState == 4){ //if request of file completed
if (myajax.status==200 || window.location.href.indexOf("http")==-1){ //if request was successful or running script locally
if (myfiletype=="txt")
AjHtml=(myajax.responseText)
else
alert(myajax.responseXML)
}
}
}

function clearAll() {
	
		expireAt = new Date("01 January, 1970 00:00:01");
		 document.cookie = "idit=; path=/; expires="+expireAt.toGMTString();
		
		showOrderHtml ();
}
