var GoToShoppingCartPage = false;
function LTrim(str) {
   var whitespace = new String(" ");
   var s = new String(str);

   if (whitespace.indexOf(s.charAt(0)) != -1) {

      var j=0, i = s.length;

      while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
         j++;

      s = s.substring(j, i);
   }
   return s;
}

function RTrim(str)	{
   var whitespace = new String(" ");

   var s = new String(str);

   if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {

      var i = s.length - 1;       

      while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
         i--;
      s = s.substring(0, i+1);
   }
   return s;
}
function RemoveLightBoxAddToCart ()	{
	document.getElementById('fadeCart').style.display='none';
	document.getElementById('lightCart').style.display='none';	
	return false;
}

function Trim(str)
{
   return RTrim(LTrim(str));
}

function GetXmlHttpObject() {
	var xmlHttpGnl=null;
	try {
		//Firefox, Opera 8.0+, Safari
		xmlHttpGnl=new XMLHttpRequest();
	} catch (e) {
		try {
			//Internet Explorer
			xmlHttpGnl=new ActiveXObject("Msxm12.XMLHTTP");
		} catch(e) {
			xmlHttpGnl=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttpGnl;
}
function addToCart(pid)
{
	var qty = 1;
	var a = DisplayLightBoxAddToCart(pid, qty);
	return false;
}
function GetProductDetail (pid, qty)	{
		if(pid == '' || pid <= 0 ) return false;		
		xmlHttpAddtoCart=GetXmlHttpObject();			
		if(xmlHttpAddtoCart == null) 
		{
			alert("Browser doesnt support ajax");
			return;
		}
		var url=Site_URL+"Ajax_Prod_Detail.php?pid="+pid;		
		xmlHttpAddtoCart.onreadystatechange=GetProductDetail_response;
		xmlHttpAddtoCart.open("GET",url,true);
		xmlHttpAddtoCart.send(null);
		return false;		
		function GetProductDetail_response () {
		if(xmlHttpAddtoCart.readyState==4 || xmlHttpAddtoCart.readyState=="complete") 	{
			response=xmlHttpAddtoCart.responseText;
				
				if(Trim(response) != ''){
				var response_ary = response.split('##');			
				var product_price = response_ary[2];					
				document.getElementById('LightSKU').innerHTML  = response_ary[0];
				document.getElementById('LightProdName').innerHTML = response_ary[1];				
				document.getElementById('LightProdPrice').innerHTML = response_ary[2];			
				document.getElementById('LightProdImageUrl').src = response_ary[3];	
				document.getElementById('LightProdImageUrl').alt = response_ary[1];	
				document.getElementById('LightProdImageUrl').title = response_ary[1];	
				}
				return false;
			}		
		}
}
function showdeadcenterdiv(Xwidth,Yheight,divid) {
	var scrolledX, scrolledY;
	if( self.pageYOffset ) {
	scrolledX = self.pageXOffset;
	scrolledY = self.pageYOffset;
	} else if( document.documentElement && document.documentElement.scrollTop ) {
	scrolledX = document.documentElement.scrollLeft;
	scrolledY = document.documentElement.scrollTop;
	} else if( document.body ) {
	scrolledX = document.body.scrollLeft;
	scrolledY = document.body.scrollTop;
	}
	
	// Next, determine the coordinates of the center of browser's window
	
	var centerX, centerY;
	if( self.innerHeight ) {
	centerX = self.innerWidth;
	centerY = self.innerHeight;
	} else if( document.documentElement && document.documentElement.clientHeight ) {
	centerX = document.documentElement.clientWidth;
	centerY = document.documentElement.clientHeight;
	} else if( document.body ) {
	centerX = document.body.clientWidth;
	centerY = document.body.clientHeight;
	}
	
	// Xwidth is the width of the div, Yheight is the height of the
	// div passed as arguments to the function:
	var leftOffset = scrolledX + (centerX - Xwidth) / 2;
	var topOffset = scrolledY + (centerY - Yheight) / 2;
	// The initial width and height of the div can be set in the
	// style sheet with display:none; divid is passed as an argument to // the function
	var o=document.getElementById(divid);
	var r=o.style;
	r.position='absolute';
	r.top = topOffset + 'px';
	r.left = leftOffset + 'px';
	r.display = "block";
} 
	
function getPageSizeWithScroll()	{
	if (window.innerHeight && window.scrollMaxY) {// Firefox
		yWithScroll = window.innerHeight + window.scrollMaxY;
		xWithScroll = window.innerWidth + window.scrollMaxX;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		yWithScroll = document.body.scrollHeight;
		xWithScroll = document.body.scrollWidth;
	} else { // works in Explorer 6 Strict, Mozilla (not FF) and Safari
		yWithScroll = document.body.offsetHeight;
		xWithScroll = document.body.offsetWidth;
  	}
	arrayPageSizeWithScroll = new Array(xWithScroll,yWithScroll);
	//alert( 'The height is ' + yWithScroll + ' and the width is ' + xWithScroll );
	return arrayPageSizeWithScroll;
}
function AjaxAddToCart(pid, qty)	{		
		if(pid == '' || pid <= 0 ) return false;
		xmlHttpAddtoCart=GetXmlHttpObject();			
		if(xmlHttpAddtoCart == null) 
		{
			alert("Browser doesnt support ajax");
			return;
		}		
		var url=Site_URL+"AjaxAddToCart.php";		
		var params = "pid=" + pid;
		params = params + "&qty=" + qty;
		
		xmlHttpAddtoCart.onreadystatechange=AjaxAddToCart_response;
		xmlHttpAddtoCart.open("POST",url,true);
		xmlHttpAddtoCart.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlHttpAddtoCart.setRequestHeader("Content-length", params.length);
		xmlHttpAddtoCart.setRequestHeader("Connection", "close");
		xmlHttpAddtoCart.send(params);
		return false;
		
		function AjaxAddToCart_response() 
		{
			if(xmlHttpAddtoCart.readyState==4 || xmlHttpAddtoCart.readyState=="complete") 
			{
				response=xmlHttpAddtoCart.responseText;		
				var response_ary = response.split('|');
				document.getElementById('cart_tot').innerHTML  = response_ary[0];				
				document.getElementById('cart_item').innerHTML = "Item( "+response_ary[1]+" )";			
				RemoveLightBoxAddToCart();
				if(GoToShoppingCartPage == true) {
					window.location = Site_URL+'index.php?file=shoppingcart';
					return false;
				}		
				return false;
			}
		}
}
function DisplayLightBoxAddToCart (pid, pqty)	{
	GetProductDetail (pid, pqty) ;
	document.getElementById('fadeCart').style.display='block';
	document.getElementById('lightCart').style.display='block';
	arrayPageSizeWithScroll = getPageSizeWithScroll();
	document.getElementById('fadeCart').style.height = arrayPageSizeWithScroll[1] + "px";

	showdeadcenterdiv(700,400,'lightCart');
	document.getElementById("AddToCartContinue").onclick = function(){
			AjaxAddToCart(pid , pqty);
		} 
	document.getElementById("AddToCartCheckout").onclick = function(){
			AjaxAddToCart(pid , pqty);
			GoToShoppingCartPage = true;
		} 	
	return false;
}
