// JavaScript Document
var xmlHttp1, xmlHttp2, xmlHttp3, xmlHttp4, xmlHttp5, xmlHttp6, xmlHttp7;
var ajax_loader = "<center><div><img src='images/ajax.gif' width='24' height='24' /></div><div style='font-size: 11px; color: #cecece'>טוען..</div></center>";

	function GetShippingPriceById(shipping_type_id)
	{ 
		xmlHttp7=GetXmlHttpObject()
		
		if (xmlHttp7==null)
		{
			alert ("Browser does not support HTTP Request")
			return
		}
		
		var url="include/Ajax.php?act=07"
		url=url+"&shipping_type_id="+Number(shipping_type_id)
		url=url+"&sid="+Math.random()
		
		xmlHttp7.onreadystatechange=GetShippingPriceById_ajax
		xmlHttp7.open("GET",url,true)
		xmlHttp7.send(null)
	}
	
	function GetShippingPriceById_ajax() 
	{
		if (xmlHttp7.readyState==4 || xmlHttp7.readyState=="complete")
		{
			document.getElementById("shipping_price").value = xmlHttp7.responseText;
			
			try
			{
				CalculateBasketPrice();
			}
			catch (ex)
			{
			}
			
			try
			{
				CalculateOrderOpenForm();
			}
			catch (ex)
			{
			}
		} 
		/*else
		{
			document.getElementById("SalesAjax").innerHTML = ajax_loader;
		}*/
	}
	
	function ProductsSale()
	{ 
		xmlHttp1=GetXmlHttpObject()
		
		if (xmlHttp1==null)
		{
			alert ("Browser does not support HTTP Request")
			return
		}
		
		var url="include/Ajax.php?act=01"
		url=url+"&page="+Number(document.getElementById('products_sales_page').value)
		url=url+"&products_sales_list="+document.getElementById('products_sales_list').value
		url=url+"&per_page="+Number(document.getElementById('products_sales_per_page').value)
		url=url+"&sid="+Math.random()
		
		xmlHttp1.onreadystatechange=ProductsSale_ajax
		xmlHttp1.open("GET",url,true)
		xmlHttp1.send(null)
	}
	
	function ProductsSale_ajax() 
	{
		if (xmlHttp1.readyState==4 || xmlHttp1.readyState=="complete")
		{
			document.getElementById("SalesAjax").innerHTML = xmlHttp1.responseText;
			
			$("a[rel^='prettyPhoto']").prettyPhoto();
		} 
		else
		{
			document.getElementById("SalesAjax").innerHTML = ajax_loader;
		}
	}
	
	function GetCatalogsByCategoryId(category_id, selected_id)
	{
		xmlHttp2=GetXmlHttpObject()
		
		if (xmlHttp2==null)
		{
			alert ("Browser does not support HTTP Request")
			return
		}
		
		var url="include/Ajax.php?act=02"
		url=url+"&category_id="+Number(category_id)
		url=url+"&selected="+Number(selected_id)
		url=url+"&sid="+Math.random()
		
		xmlHttp2.onreadystatechange=GetCatalogsByCategoryId_ajax
		xmlHttp2.open("GET",url,true)
		xmlHttp2.send(null)
	}
	
	function GetCatalogsByCategoryId_ajax() 
	{
		if (xmlHttp2.readyState==4 || xmlHttp2.readyState=="complete")
		{
			document.getElementById("Catalogs_Ajax").innerHTML = xmlHttp2.responseText;
		} 
	}
	
	function IncStockProductBasket(product_id)
	{
		var stock = Number(document.getElementById("product_basket_" + product_id + "_stock").value) + 1;
		
		if (stock > 99)
		{
			stock = 99;
		}
		
		if (stock != document.getElementById("product_basket_" + product_id + "_stock").value)
		{
			document.getElementById("product_basket_" + product_id + "_stock").value = stock;
			UpdateProductStock(product_id, stock);
		}
	}
	
	function DecStockProductBasket(product_id)
	{
		var stock = Number(document.getElementById("product_basket_" + product_id + "_stock").value) - 1;
		
		if (stock < 1)
		{
			stock = 1;
		}
		
		if (stock != document.getElementById("product_basket_" + product_id + "_stock").value)
		{
			document.getElementById("product_basket_" + product_id + "_stock").value = stock;
			UpdateProductStock(product_id, stock);
		}
	}
	
	function UpdateProductStock(product_id, stock)
	{
		xmlHttp3=GetXmlHttpObject()
		
		if (xmlHttp3==null)
		{
			alert ("Browser does not support HTTP Request")
			return
		}
		
		var url="include/Ajax.php?act=03"
		url=url+"&product_id="+Number(product_id)
		url=url+"&stock="+Number(stock)
		url=url+"&sid="+Math.random()
		
		xmlHttp3.onreadystatechange=UpdateProductStock_ajax
		xmlHttp3.open("GET",url,true)
		xmlHttp3.send(null)
	}
	
	function UpdateProductStock_ajax() 
	{
		if (xmlHttp3.readyState==4 || xmlHttp3.readyState=="complete")
		{
			var result	= xmlHttp3.responseText;
			result 	   	= result.split("|");
			
			document.getElementById("product_basket_" + result[0] + "_price").innerHTML = result[1];
			
			BasketInfo();
		} 
	}
	
	function ProductToBasket(product_id)
	{
		xmlHttp4=GetXmlHttpObject()
		
		if (xmlHttp4==null)
		{
			alert ("Browser does not support HTTP Request")
			return
		}
		
		var url="include/Ajax.php?act=04"
		url=url+"&product_id="+Number(product_id)
		url=url+"&sid="+Math.random()
		
		xmlHttp4.onreadystatechange=ProductToBasket_ajax
		xmlHttp4.open("GET",url,true)
		xmlHttp4.send(null)
	}
	
	function ProductToBasket_ajax() 
	{
		if (xmlHttp4.readyState==4 || xmlHttp4.readyState=="complete")
		{
			var result	= xmlHttp4.responseText;
			result 	   	= result.split("|");
			
			document.getElementById("ProductsBasketBlock").innerHTML = result[0];
			
			try
			{
				if (result[1])
				{
					document.getElementById('ProductToBasketImg').innerHTML = '<img src="images/add_to_basket.gif" onmouseover="this.src = \'images/add_to_basket_over.gif\'" onmouseout="this.src = \'images/add_to_basket.gif\'" height="29" title="הוסף לסל" />';
				}
				else
				{
					document.getElementById('ProductToBasketImg').innerHTML = '<img src="images/remove_from_basket.gif" onmouseover="this.src = \'images/remove_from_basket_over.gif\'" onmouseout="this.src = \'images/remove_from_basket.gif\'" height="29" title="הסר מהסל" />';
				}
			}
			catch (ex)
			{
			}
			
			try
			{
				if (result[1])
				{
					document.getElementById('product_' + result[2] + '_to_basket').innerHTML = '<img src="images/product_add_to_basket.png" onmouseover="this.src = \'images/product_add_to_basket_over.png\'" onmouseout="this.src = \'images/product_add_to_basket.png\'" height="22" title="הוסף לסל" />';
				}
				else
				{
					document.getElementById('product_' + result[2] + '_to_basket').innerHTML = '<img src="images/product_remove_from_basket.png" onmouseover="this.src = \'images/product_remove_from_basket_over.png\'" onmouseout="this.src = \'images/product_remove_from_basket.png\'" height="22" title="הסר מהסל" />';
				}
			}
			catch (ex)
			{
			}
			
			ShowProductsBasket();
			BasketInfo();
			
			try
			{
				$("a[rel^='prettyPhoto']").prettyPhoto();
			}
			catch (ex)
			{
			}
		} 
	}
	
	function BasketInfo()
	{
		xmlHttp5=GetXmlHttpObject()
		
		if (xmlHttp5==null)
		{
			alert ("Browser does not support HTTP Request")
			return
		}
		
		var url="include/Ajax.php?act=05"
		url=url+"&sid="+Math.random()
		
		xmlHttp5.onreadystatechange=BasketInfo_ajax
		xmlHttp5.open("GET",url,true)
		xmlHttp5.send(null)
	}
	
	function BasketInfo_ajax() 
	{
		if (xmlHttp5.readyState==4 || xmlHttp5.readyState=="complete")
		{	
			var result	= xmlHttp5.responseText;
			result 	   	= result.split("|");
			
			document.getElementById("basket_info").innerHTML = result[0];
			document.getElementById("basket_header_info").innerHTML = result[2];
			AlertChangedBasketHeaderInfo();
			
			try
			{
				document.getElementById("order_basket_price").innerHTML = result[1];
				//document.getElementById("order_basket_price_send_button").innerHTML = result[1];
				
				var i = Number(result[1].indexOf(' '));
				document.getElementById("basket_price").value = result[1].substring(0,i).replace(",", "") ;
			}
			catch (ex)
			{
			}
			
			CalculateBasketPrice();
			
			try
			{
				if (document.getElementById('BasketButtons').style.display == "none" && Number(document.getElementById('num_products_in_basket').value) > 0)
				{
					$('#BasketButtons').fadeToggle("slow");
				}
				else if (Number(document.getElementById('num_products_in_basket').value) == 0)
				{
					document.getElementById('BasketButtons').style.display = "none";
					//$('#BasketButtons').fadeToggle("fast");
				}
			}
			catch (ex)
			{
			}
		} 
	}
	
	function MiniSlideshow()
	{
		xmlHttp6=GetXmlHttpObject()
		
		if (xmlHttp6==null)
		{
			alert ("Browser does not support HTTP Request")
			return
		}
		
		var url="include/Ajax.php?act=06"
		url=url+"&slideshow_list="+document.getElementById('slideshow_list').value
		url=url+"&start_index="+Number(document.getElementById('slideshow_start_index').value)
		url=url+"&sid="+Math.random()
		
		xmlHttp6.onreadystatechange=MiniSlideshow_ajax
		xmlHttp6.open("GET",url,true)
		xmlHttp6.send(null)
	}
	
	function MiniSlideshow_ajax() 
	{
		if (xmlHttp6.readyState==4 || xmlHttp6.readyState=="complete")
		{	
			document.getElementById("mini_slideshow_list").innerHTML = xmlHttp6.responseText;
		} 
	}
	
	function GetXmlHttpObject()
	{
		var xmlHttp=null;
		try
		{
			// Firefox, Opera 8.0+, Safari
			xmlHttp=new XMLHttpRequest();
		}
		catch (e)
		{
			//Internet Explorer
			try
			{
				xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch (e)
			{
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
		}
		return xmlHttp;
	}
