    var request;

	function makeRequest()
	{
		try // ühenduse loomine mitte-Microsoft lehitsejate puhul
		{
			request = new XMLHttpRequest();
		}
		catch(microsoft) // ühenduse loomine Microsoft lehitsejate jaoks
		{
			try
			{
				request = new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch(teinemicrosoft)
			{
				request = new ActiveXObject("Microsoft.XMLHTTP");
			}
		}
		if(request == null) // kui ühenduse loomine ebaõnnestus, anna sellest märku
		{
			alert("XmlHttpRequest failure");
		}
	}
	
/* ---------------------------- shopping basket ------------------------------- */
	function basket_insertItem(itemID)
	{
	    makeRequest();

		var nodeBasket = document.getElementById('basketinfo-container');
		nodeBasket.style.backgroundColor = '#7C7C7C';

		var url = "http://www.kopkop.ee/shoppingbasket.php?action=insert&itemID=" + escape(itemID);
		request.open("GET", url, true);
		request.onreadystatechange = basket_updateInfo;
		request.send(null);
	}

	function basket_updateInfo()
    {
	    if(request.readyState == 4 && request.status == 200)
		{
		    var responsetext = request.responseText;
			var arrResponse = responsetext.split('/-/');
			var nodeBasket = document.getElementById('basketinfo-container');
			basketSize = arrResponse[1];
			var nodeTextnode = document.createTextNode(arrResponse[0]);

			while(nodeBasket.firstChild)nodeBasket.removeChild(nodeBasket.firstChild);

            var nodeA = document.createElement("a");
			nodeA.setAttribute("href", "/" + defaultlang + "/?viewbasket=1");
			nodeA.appendChild(nodeTextnode);

			nodeBasket.appendChild(nodeA);
			nodeBasket.style.backgroundColor = 'FFFFFF';
		}
	}

	function basket_emptybasket()
	{
	 	makeRequest();
		var url = "http://www.kopkop.ee/shoppingbasket.php?action=empty";
		request.open("GET", url, true);
		request.onreadystatechange = basket_updateInfo;
		request.send(null);   
	}

	function basket_removeItem(itemID)
	{
		makeRequest();
		var url = "http://www.kopkop.ee/shoppingbasket.php?action=remove&itemID=" + escape(itemID);
		request.open("GET", url, true);
		request.onreadystatechange = basket_updateInfo;
		request.send(null);
	}

	function checkIfNumericAndUpdate(obj, productID, oneprice)
	{
		var priceitemID = "price_" + productID;
		var totalpriceitemID = "totalprice";

	    if(!isNumeric(obj.value))
		{
			var previousprice = eval(document.getElementById(priceitemID).firstChild.data);
			obj.value = ""

			var nullNode = document.createTextNode("0");
			emptyNode(document.getElementById(priceitemID));
			document.getElementById(priceitemID).appendChild(nullNode);

			var bigtotalprice = eval(document.getElementById(totalpriceitemID).firstChild.firstChild.data) - previousprice;
			var totalPriceNode = document.createTextNode(bigtotalprice);
			emptyNode(document.getElementById(totalpriceitemID).firstChild);
			document.getElementById(totalpriceitemID).firstChild.appendChild(totalPriceNode);
		}
		else
		{
			var previousprice = eval(document.getElementById(priceitemID).firstChild.data);
		    var totalprice = oneprice * obj.value;

			var priceNode = document.createTextNode(totalprice);
			emptyNode(document.getElementById(priceitemID));
			document.getElementById(priceitemID).appendChild(priceNode);

			var bigtotalprice = eval(document.getElementById(totalpriceitemID).firstChild.firstChild.data) - previousprice + totalprice;
			var totalPriceNode = document.createTextNode(bigtotalprice);
			emptyNode(document.getElementById(totalpriceitemID).firstChild);
			document.getElementById(totalpriceitemID).firstChild.appendChild(totalPriceNode);
		}
	}

	function emptyNode(obj)
	{
	    while(obj.firstChild)obj.removeChild(obj.firstChild);
	}

	function isNumeric(sText)
	{
	   var ValidChars = "0123456789";
	   var IsNumber=true;
	   var Char;

	 
	   for (i = 0; i < sText.length && IsNumber == true; i++) 
		  { 
		  Char = sText.charAt(i); 
		  if (ValidChars.indexOf(Char) == -1) 
			 {
			 IsNumber = false;
			 }
		  }
	   return IsNumber;
   }

   function removeProduct(productID, productPrice)
   {
       var amountID = "order_" + productID;
	   var nodeAmount = document.getElementById(amountID);
	   nodeAmount.value = 0;
	   checkIfNumericAndUpdate(nodeAmount, productID, productPrice);

	   basket_removeItem(productID);
	   var rowID = "basketrow_" + productID;
	   var nodeRow = document.getElementById(rowID);
	   if(nodeRow)nodeRow.parentNode.removeChild(nodeRow);
   }

   function getNode(nodeID)
   {
	   if(document.all)
	   {
		   var node = document.all[nodeID];
	   }
	   else if(document.layers)
	   {
		   var node = document.layers[nodeID];
	   }
	   else
	   {
		   var node = document.getElementById(nodeID);
	   }
	   return node;
   }

/* ---------------------------- /shopping basket ------------------------------- */	


	function showInsidePicture(id, description)
	{
		  var imgInsidepicture = getNode('insidepicture');
		  if(!imgInsidepicture)
		  {
		      var imgInsidepicture = document.createElement("img");
			    imgInsidepicture.id = "insidepicture";
			    var nodeContainer = getNode("productinfo-container");

			  emptyNode(nodeContainer);

			  nodeContainer.appendChild(imgInsidepicture);
		  }
		  imgInsidepicture.src = '/resizedpicture_w.php?ID='+id+'&maxwidth=465';
		  
		  var nodeDescription = getNode('insidepicture-description');
      if(nodeDescription)
      {
        emptyNode(nodeDescription);
		    if('' != description)
		    {
          var nodeContent = document.createTextNode(description);
          nodeDescription.appendChild(nodeContent);
        }
      }
	}

	function showPicture(content, width, height)
	{
		 var newwin = window.open(content, 'Pilt', 'location=no,menubar=no,status=no,titlebar=no,toolbar=no,width='+width+',height='+height+'');
	   newwin.focus();
  }
	var OS,browser,version,total,thestring;		

	function doOnLoad()
	{
		resizeWindow();
	}


    function getBodySize()
    {
        var myHeight = 0;
        if( typeof( window.innerWidth ) == 'number' )
        {
            //Non-IE
            myHeight = window.innerHeight;
        }
        else if( document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight))
        {
            //IE 6+ in 'standards compliant mode'
            myHeight = document.documentElement.clientHeight;
        }
        else if(document.body && (document.body.clientWidth || document.body.clientHeight))
        {
            //IE 4 compatible
            myHeight = document.body.clientHeight;
        }
        return myHeight;
    }

    function resizeWindow()
    {
        if(document.getElementById)
        {
            var container = document.getElementById('container');
            var middlecontainer = document.getElementById('middle-container');
            var bottomcontainer = document.getElementById('bottom-container');
        }
        else if(document.layers)
        {
            var container = document.layers['container'];
            var middlecontainer = document.layers['middle-container'];
            var bottomcontainer = document.layers['bottom-container'];
        }
        else if(document.all)
        {
            var container = document.all['container'];
            var middlecontainer = document.all['middle-container'];
            var bottomcontainer = document.all['bottom-container'];
        }
	
		if(document.body.scrollHeight)
		{
			var container_height = container.scrollHeight;
			var middlecontainer_height = middlecontainer.scrollHeight;
		}
		else if(document.body.offsetHeight)
		{
			var container_height = container.offsetHeight;
			var middlecontainer_height = middlecontainer.offsetHeight;
		}
		var body_height = getBodySize();
		var bottomcontainer_height = 25;

		if(body_height > container_height)
		{
			var new_middlecontainer_height = middlecontainer_height + body_height - container_height;
			middlecontainer.style.height = new_middlecontainer_height + 'px';
		}
		bottomcontainer.style.visibility = 'visible';
}

function resizeFrameHeight(frameID)
{
    var height = document.getElementById('pictureTable').clientHeight;
	var nodeIframe = parent.document.getElementById(frameID);
	nodeIframe.height = height + 20 + 'px';
	nodeIframe.parentNode.style.visibility = 'visible';
}

function resizeFrameHeightV(frameID)
{
    var height = document.getElementById('pictureTable').clientHeight;
 var nodeIframe = parent.document.getElementById(frameID);
 
 if ( height < 400 ) {
 nodeIframe.height = height + 20 + 'px';
 } else {
 nodeIframe.height = 420 + 'px';
 }
 nodeIframe.parentNode.style.visibility = 'visible';
}

function showParentInsidePicture(id, title)
{
	
	  var imgInsidepicture = parent.document.getElementById('insidepicture');
	  if(!imgInsidepicture)
	  {
		  var imgInsidepicture = parent.document.createElement("img");
		  imgInsidepicture.id = "insidepicture";
		  var nodeContainer = parent.document.getElementById("productinfo-container");

		  emptyNode(nodeContainer);

		  nodeContainer.appendChild(imgInsidepicture);
	  }
	  imgInsidepicture.src = '/resizedpicture_w.php?ID='+id+'&maxwidth=465';

	  var nodePictureHeader = parent.document.getElementById('specialgallery_picturetitle');
	  
	  if(nodePictureHeader)
	  {
		  emptyNode(nodePictureHeader);
		  if(title == "")title = "&nbsp;";
		  nodePictureHeader.innerHTML = title;
	  }
}


/* ====================================================================== */
/* colorselect functions */
/* ====================================================================== */

// add image to the list
function colorselect_selectImage(itemID, imageID, pictureID, numImages, imageX, imageY, realSizeX, realSizeY)
{
	if(null == numImages)var numImages = 5;

	var nodeImgRow = getNode("colorselect_imagerow_" + itemID);

	if(nodeImgRow)
	{
		//alert(nodeImgRow.childNodes.length + "---" + numImages + "==" + "colorselect_imagerow_" + itemID);
		var tElements = nodeImgRow.getElementsByTagName("td");
		var tdLength = tElements.length;
		
		if(tdLength >= numImages)
		{
			nodeImgRow.removeChild(tElements[0]);
		}

		var nodeImgCell = document.createElement("td");
		
		var nodeImgLink = document.createElement("a");
		nodeImgLink.setAttribute("href", "javascript:showPicture('/colorselectpicture.php?ID=" + imageID + "', " + realSizeX + ", " + realSizeY + ")");
		//nodeImgLink.onclick = function(){showPicture('/colorselectpicture.php?ID='+ imageID, realSizeX, realSizeY);}
		
		var nodeImg = document.createElement("img");

		var imgHref = "/phpThumb/phpThumb.php?src=/upload/gallery/" + pictureID + "&w=" + imageX;
		if(0 < imageY)imgHref += "&h=" + imageY;

		nodeImg.setAttribute("src", imgHref);
		nodeImg.setAttribute("alt", "");
		nodeImg.setAttribute("border", "0");
		if(0 < imageX)nodeImg.setAttribute("width", imageX);
		if(0 < imageY)nodeImg.setAttribute("height", imageY);

		nodeImgLink.appendChild(nodeImg);
		nodeImgCell.appendChild(nodeImgLink);
		nodeImgRow.appendChild(nodeImgCell);
	}

}

/* ====================================================================== */
/* /colorselect functions */
/* ====================================================================== */

