try 
{
	http = new XMLHttpRequest(); /* e.g. Firefox */
} 
catch(e) 
{
	try 
	{
    	http = new ActiveXObject("Msxml2.XMLHTTP"); 
  	}
	catch (e) 
	{
    	try 
		{
    		http = new ActiveXObject("Microsoft.XMLHTTP");  /* some versions IE */
    	} 
		catch (E) 
		{
			http = false;
		} 
	} 
}

function checkNum()
{
	var carCode = event.keyCode;
	if ((carCode < 48) || (carCode > 57))
	{
		alert('Negative Values And Decimals Are Prohibited');
		event.cancelBubble = true;
		event.returnValue = false;
	}
} 



 // The server-side scripts
/*function updateOrder(pvid) 
{	
	var url = "ajax_shopping_cart.php?response=Y&pvid="+pvid;
	var myRandom=parseInt(Math.random()*99999999);  // cache buster
	
	http.open("POST", url + "&rand=" + myRandom, true);
	http.onreadystatechange = handleHttpResponse;
	http.send(null);
}*/

function ClearCart() 
{
	var url = "ajax_shopping_cart.php?response4=Y";
	var myRandom=parseInt(Math.random()*99999999);  // cache buster
	
	http.open("POST", url + "&rand=" + myRandom, true);
	http.onreadystatechange = handleHttpResponse4;
	http.send(null);
	
	x = setTimeout("updateDisplay()",1500);
}

function updateOrderWithQty(pvid,prod_var_details,objQTY) 
{	

	if(objQTY.value=="0")
	{
		alert("Quantity Cannot Be Zero");
		return false;
	}
	if(objQTY.value=="")
	{
		alert("Quantity Cannot Be Left Empty");
		return false;
	}
	qty=objQTY.value;
	
	if(!CheckNum(qty))
	{
		alert("Please Enter Numeric Values Only");
		return false;
	}
	
	if(prod_var_details!="NA")
	{
		var ProductVarietyDetails=prod_var_details.split("~");
		
		//////////////////        CHECKING FOR MIN MAX LEVEL  - FOR PRODUCT VARIETY         /////////////////////
		weight=ProductVarietyDetails[0];
		minOrderwt=ProductVarietyDetails[1];
		maxOrderwt=ProductVarietyDetails[2];
		multiple=ProductVarietyDetails[3];
		
		if(!isNaN(weight))
		{
			
			weight=parseFloat(weight);
			reqWeight=parseFloat(qty*weight);
			if(!isNaN(minOrderwt) && minOrderwt!="0")
			{
				minOrderwt=parseFloat(minOrderwt);
				if(reqWeight<minOrderwt)				//REQUIRED WEIGHT IS LESS THAN MIN ORDER LEVEL
				{
					qtyAll=minOrderwt/weight;
					minQty=parseFloat(qtyAll);
					alert("Minimun  Allowed Qty is "+minQty+ " !!");
					return false;
				}
				
			}
					
			if(!isNaN(maxOrderwt) && maxOrderwt!="0")
			{
				maxOrderwt=parseFloat(maxOrderwt);
				if(reqWeight>maxOrderwt)				//REQUIRED WEIGHT IS MORE THAN MAX ORDER LEVEL
				{
					qtyAll=maxOrderwt/weight;
					maxQty=parseFloat(qtyAll);
					alert("Maximum  Allowed Qty is " + maxQty + " !!");
					return false;
				}
				
			}
			
			if(!isNaN(multiple) && multiple!="0")
			{
				multiple=parseFloat(multiple);
				factor=parseFloat(qty%multiple);
				if(factor>0)					//REQUIRED WEIGHT IS LESS THAN MIN ORDER LEVEL
				{
					multiple1=parseFloat(multiple);
					multiple2=parseFloat(multiple*2);
					alert("Order Qty Can Be  in Multiples of "+multiple1+ ", "+multiple2+"  etc!!");
					return false;
				}
				
			}
		}
	}
	/////////////////////////////////////////////////////////////////////////////////////
	
	
	var url = "ajax_shopping_cart.php?response=Y&orderQTY="+qty+"&pvid="+pvid;
	var myRandom=parseInt(Math.random()*99999999);  // cache buster
	
	http.open("POST", url + "&rand=" + myRandom, true);
	http.onreadystatechange = handleHttpResponse;
	http.send(null);
}

function updateDisplay() 
{	
	var url = "ajax_shopping_cart.php?response=Y";
	var myRandom=parseInt(Math.random()*99999999);  // cache buster
	
	http.open("POST", url + "&rand=" + myRandom, true);
	http.onreadystatechange = handleHttpResponse;
	http.send(null);
}

 // The server-side scripts
function updateQuantity(pvid,prod_var_details,item_number,originalQTY) 
{
	quantvalue=document.getElementById("quant"+pvid).value;	
	quant=document.getElementById("quant"+pvid);
	if(quantvalue=="0")
	{
		alert("Quantity Cannot Be Zero");
		quant.value=originalQTY;
	}
	else if(quantvalue=="")
	{
		alert("Quantity Cannot Be Left Empty");
		quant.value=originalQTY;
	}
	
	quantvalue=document.getElementById("quant"+pvid).value;
	
	if(!CheckNum(quantvalue))
	{
		alert(3);
		document.getElementById("quant"+pvid).value=originalQTY;
		return false;
	}
	if(quantvalue < 1)
	{
		alert("Minimum Quantity Allowed Is 1");
		document.getElementById("quant"+item_number).value=originalQTY;
		return false;
	}
	
	if(prod_var_details!="NA")
	{
		var ProductVarietyDetails=prod_var_details.split("~");
		
		//////////////////        CHECKING FOR MIN MAX LEVEL  - FOR PRODUCT VARIETY         /////////////////////
		qty=quantvalue;
		weight=ProductVarietyDetails[0];
		minOrderwt=ProductVarietyDetails[1];
		maxOrderwt=ProductVarietyDetails[2];
		multiple=ProductVarietyDetails[3];
		
		if(!isNaN(weight))
		{
			weight=parseFloat(weight);
			reqWeight=parseFloat(qty*weight);
			if(!isNaN(minOrderwt) && minOrderwt!="0")
			{
				minOrderwt=parseFloat(minOrderwt);
				if(reqWeight<minOrderwt)				//REQUIRED WEIGHT IS LESS THAN MIN ORDER LEVEL
				{
					qtyAll=minOrderwt/weight;
					minQty=parseFloat(qtyAll);
					alert("Minimun  Allowed Qty is "+minQty+ " !!");
					quant.value=originalQTY;
					return false;
				}
				
			}
					
			if(!isNaN(maxOrderwt) && maxOrderwt!="0")
			{
				maxOrderwt=parseFloat(maxOrderwt);
				if(reqWeight>maxOrderwt)				//REQUIRED WEIGHT IS MORE THAN MAX ORDER LEVEL
				{
					qtyAll=maxOrderwt/weight;
					maxQty=parseFloat(qtyAll);
					alert("Maximum  Allowed Qty is " + maxQty + " !!");
					quant.value=originalQTY;
					return false;
				}
				
			}
			
			if(!isNaN(multiple) && multiple!="0")
			{
				multiple=parseFloat(multiple);
				factor=parseFloat(qty%multiple);
				if(factor>0)					//REQUIRED WEIGHT IS LESS THAN MIN ORDER LEVEL
				{
					multiple1=parseFloat(multiple);
					multiple2=parseFloat(multiple*2);
					alert("Order Qty Can Be  in Multiples of "+multiple1+ ", "+multiple2+"  etc!!");
					quant.value=originalQTY;
					return false;
				}
				
			}
		}
	}
	/////////////////////////////////////////////////////////////////////////////////////
	var url = "ajax_shopping_cart.php?response2=Y&pvid="+pvid;
	var myRandom=parseInt(Math.random()*99999999);  // cache buster
	http.open("POST", url + "&item_number="+item_number+"&quantvalue="+quantvalue+"&rand=" + myRandom, true);
	http.onreadystatechange = handleHttpResponse2;
	http.send(null);
}

function CheckCart(count)
{
	info = new Array();
	product_id=new Array();	//ALL THE PRODUCT IDS
	product_wt=new Array();	//WEIGHT OF ALL THE PRODUCTS
	
	product_info=document.getElementById("product_info").value;
	if(product_info!="NA")
	{
		for(i=0;i<count;i++)
		{
			val=document.getElementById("prod_var"+i).value;
			temp=val.split("-");		//"PID - WEIGHT - PVID"
			quant=document.getElementById("quant"+temp[2]).value;

			if(InArray(product_id,temp[0] ) )
			{
				//THEN ADD THE WEIGHT 
				ind=Array_Exists( product_id,temp[0] );
				if(ind!=-1)//NOT FOUND
				{
					wt=product_wt[ind];
					if( !isNaN(wt) && ( !isNaN(temp[1]) && temp[1]!="" ) )
					{
						wt=parseFloat(wt)+parseFloat(temp[1])*parseFloat(quant);
						product_wt[ind]=wt;
					}
				}
			}
			else
			{
				product_id.push(temp[0]);
				if(!isNaN(temp[1]) && temp[1]!="")
				{
					product_wt.push(parseFloat(temp[1])*parseFloat(quant));
				}
				else
					product_wt.push('');
			}
			
		}
		
		info=product_info.split("^");
		msg="";
		err=0;
		for(i=0;i<info.length;i++)
		{
			prod_detail=new Array();
			prod_detail=info[i].split("-");
	
			prodname=prod_detail[0];
			minwt=prod_detail[1];
			maxwt=prod_detail[2];
			pid=prod_detail[3];
			
			//alert("PRODUCT NAME "+prodname+"\nMin Wt  =>"+minwt+"\nMax Wt  =>"+maxwt);
			
			index=Array_Exists(product_id,pid);
			if(index!=-1)
			{
				if(product_wt[index])
				purchasewt=product_wt[index];
				else
				purchasewt="";
			}
			if(purchasewt!="" && !isNaN(purchasewt))
			{
				purchasewt=parseFloat(purchasewt);
				
				if(!isNaN(minwt) && minwt!="0" && minwt!="")
				{
					minwt=parseFloat(minwt);
					if(purchasewt < minwt)
					{
						msg= " Min " + minwt +" gms of "+prodname+" can be purchased \n "+msg;
						err=1;
					}
				}
				if(!isNaN(maxwt) &&  maxwt!="0" && maxwt!="")
				{
					maxwt=parseFloat(maxwt);
					if(purchasewt > maxwt)
					{
						msg= " Max " + maxwt +" gms of "+prodname+" can be purchased \n "+msg;
						err=1;
					}
				}
			}
			

		}
		if(err)
		{
			alert(msg);
			return false;
		}
		else
			return true;
		//window.location.href="bnn_shopping_cart.php";
	}	
		
}

function removeShopItem(pvid,item_number) 
{
	var url = "ajax_shopping_cart.php?response3=Y&pvid="+pvid;
	var myRandom=parseInt(Math.random()*99999999);  // cache buster
	
	http.open("POST", url + "&item_number="+item_number+"&rand=" + myRandom, true);
	http.onreadystatechange = handleHttpResponse3;
	http.send(null);
	
	x = setTimeout("updateDisplay()",1500);
}

function handleHttpResponse() 
{
	if (http.readyState == 4) 
	{
		results = http.responseText.split("~~~");
		if(results[1] == 0)
			alert("Already Added To The Shopping Cart");
		
		if(document.getElementById('shoppingDIV'))
			document.getElementById('shoppingDIV').innerHTML = results[0];
		
		if(document.getElementById("TOPANCHOR"))
		location.href=document.getElementById("TOPANCHOR").href;
		
  	}
}

function handleHttpResponse2() 
{
	if (http.readyState == 4) {	}
}

function handleHttpResponse3() 
{
	if (http.readyState == 4) { }
}

function handleHttpResponse4() 
{
	if (http.readyState == 4) { }
}

function InArray(SearchArray,Key)			//RETURS THETRUE IF FOUND ELSE FALSE
{
	for(k=0;k<SearchArray.length;k++)
	{
		if(SearchArray[k]==Key)
			return true;
	}
	return false;
}

function Array_Exists(SearchArray,Key)		//RETURS THE INDEX WHERE THE KEY IS FOUND  oR RETURN -1 is Not Found
{
	for(j=0;j<SearchArray.length;j++)
	{
		if(SearchArray[j]==Key)
			return j;
	}
	return -1;
}
