// Wait function jquery Start
var tmp;
var xmlhttp;
var xmlhttp1;
var result;
function GetXmlHttpObject()
{
	if (window.XMLHttpRequest)
	{
		return new XMLHttpRequest();
	}
	if (window.ActiveXObject)
	{
	  // code for IE6, IE5
	  return new ActiveXObject("Microsoft.XMLHTTP");
	}
	return null;
}
function addToCart_test(val, url)
{
	document.getElementById('staticval').value = 1;
	ihtml = '<table width="247" border="0" cellspacing="0" cellpadding="0" style="background:#FFFFFF; border:1px solid #ccc;" id="mcart">   <tr style="background:#fbfbfb;">     <td align="left" valign="top"><div style="margin:5px; background: #f2f2f2; width:247px; height:142px;">         <div style=" font-family: Verdana, Arial, Helvetica, sans-serif; color:#000; font-weight:bold; font-size:12px; padding-top:50px; text-align:center"><img src="images/loading.gif" /><BR />           Loading...</div>       </div></td>   </tr> </table> ';
	window.onresize = myResize;
	addItems(val, url);
	function myResize() {
	  if (document.all)
	    {
			curscrwidth = document.body.clientWidth;
			if(curscrwidth > layoutwidth)
			{
				left = (curscrwidth - layoutwidth) / 2;
				document.getElementById("floating-cart").style.left = (left + ((layoutwidth - minicartwidth) - 6)) + "px";
			}
			else
			{
				document.getElementById("floating-cart").style.left = ((layoutwidth - minicartwidth) - 6) + "px";
			}
		}
	  else
	    {
			curscrwidth = window.innerWidth;
			if(curscrwidth > layoutwidth)
			{
				left = (curscrwidth - layoutwidth) / 2;
				document.getElementById("floating-cart").style.left = (left + ((layoutwidth - minicartwidth) - 15)) + "px";
			}
			else
			{
				document.getElementById("floating-cart").style.left = ((layoutwidth - minicartwidth) - 9) + "px";
			}
		}
	}
	document.getElementById("floating-cart").innerHTML = ihtml;

	myResize();
	document.getElementById('floating-cart').style.top = topspacing +"px";
	$("#floating-cart").fadeIn("slow");
	runEffect(val);
	$('html, body').animate({scrollTop:0}, 'slow'); 
}

function addItems(val, url)
{
	if(val != 999 && url.indexOf("ProductID") < 0)
	{
		productid = document.getElementById("PID" + val).value;
		parameters = "&ProductID=" + productid;
	}
	else if(url.indexOf("ProductID") >= 0)
	{
		parameters = "";	
	}
	else
	{
		var pid = new Array();
		var sess_id = new Array();
		var qty = new Array();
		var form = document.forms["checkoutfrm"];
		for(i=0; i < document.forms["checkoutfrm"].elements.length; i++)
		{
			if(document.forms["checkoutfrm"].elements[i].name == "MINPID[]")
				pid[pid.length] = form.elements[i].value;
			if(document.forms["checkoutfrm"].elements[i].name == "qty[]")
				qty[qty.length] = form.elements[i].value;
			if(document.forms["checkoutfrm"].elements[i].name == "sess_id[]")
				sess_id[sess_id.length] = form.elements[i].value;
		}
		parameters = "&ProductID=" + pid + "&sess_id="+sess_id+"&qty="+qty;
	}
	url = url + parameters;
	xmlhttp1=GetXmlHttpObject();
	
	if (xmlhttp1==null)
	{
		alert ("Browser does not support HTTP Request");
		return;
	}
//alert(url);
	xmlhttp1.onreadystatechange=StateChangeBuyNow;		
	xmlhttp1.open("GET",url,true);
	xmlhttp1.send(null);


}

function addToCart(val)
{
	document.getElementById('staticval').value = 1;
	ihtml = '<table width="247" border="0" cellspacing="0" cellpadding="0" style="background:#FFFFFF; border:1px solid #ccc;" id="mcart">   <tr style="background:#fbfbfb;">     <td align="left" valign="top"><div style="margin:5px; background: #f2f2f2; width:247px; height:142px;">         <div style=" font-family: Verdana, Arial, Helvetica, sans-serif; color:#000; font-weight:bold; font-size:12px; padding-top:50px; text-align:center"><img src="images/loading.gif" /><BR />           Loading...</div>       </div></td>   </tr> </table> ';

	window.onresize = myResize;
	function myResize() {
	  if (document.all)
	    {
			curscrwidth = document.body.clientWidth;
			if(curscrwidth > layoutwidth)
			{
				left = (curscrwidth - layoutwidth) / 2;
				document.getElementById("floating-cart").style.left = (left + ((layoutwidth - minicartwidth) - 6)) + "px";
			}
			else
			{
				document.getElementById("floating-cart").style.left = ((layoutwidth - minicartwidth) - 6) + "px";
			}
		}
	  else
	    {
			curscrwidth = window.innerWidth;
			if(curscrwidth > layoutwidth)
			{
				left = (curscrwidth - layoutwidth) / 2;
				document.getElementById("floating-cart").style.left = (left + ((layoutwidth - minicartwidth) - 15)) + "px";
			}
			else
			{
				document.getElementById("floating-cart").style.left = ((layoutwidth - minicartwidth) - 9) + "px";
			}
		}
	}
	document.getElementById("floating-cart").innerHTML = ihtml;

	myResize();
	document.getElementById('floating-cart').style.top = topspacing +"px";
	$("#floating-cart").fadeIn("slow");
	/*document.getElementById("cartoff").style.display = "none";
	document.getElementById("carton").style.display = "";*/
	runEffect(val);
	$('html, body').animate({scrollTop:0}, 'slow'); 
	//checkMiniCart();
}

function closeMiniCart()
{
	$("#floating-cart").slideUp();
}

function checkMiniCart()
{
	document.onclick=check;
    function check(e){
    var target = (e && e.target) || (event && event.srcElement);
    var obj = document.getElementById('floating-cart');
    if(checkParent(target))
	{
		closeMiniCart();
	}
}

function checkParent(t){
        while(t.parentNode){
            
            if(t==document.getElementById('floating-cart') || t == document.getElementById('txtStartDate') || t == document.getElementById('txtStopDate') )
            {
                return false;
            }
            t=t.parentNode
        }
        return true
    }
}
// Aniomation for add to cart
function runEffect(val){
options = { to: "#floating-cart", className: 'ui-effects-transfer'}; 
	$("#addtocart"+val).hide('transfer',options,2000,callback);			
}
function callback(){
	setTimeout(function(){
		$("#floating-cart:hidden").removeAttr('style').hide().fadeIn();
	}, 1000);
}

function loadCart()
{
	chtml = tmp[0];
	CartNumber = tmp[1];	

	$("#floating-cart").fadeOut("fast");
	document.getElementById("floating-cart").style.display = "none";
		
	document.getElementById("floating-cart").innerHTML = chtml;
	
	if(CartNumber <= 1)
		document.getElementById("floatcart").innerHTML = "Shopping Cart (" + CartNumber + " Item)";
	else
		document.getElementById("floatcart").innerHTML = "Shopping Cart (" + CartNumber + " Items)";		
		
	document.getElementById("floatcart").href = "index.php?p=shopping_cart";
	
	$("#floating-cart").slideDown();
	document.getElementById("staticval").value = 0;
}

function StateChangeBuyNow()
{
	if (xmlhttp1.readyState==4)
	{
		tmp = xmlhttp1.responseText;
		tmp = tmp.split("CUTHERE");
		setTimeout("loadCart()", 1000);
	}
}

function checkqtyarr()
{
	var qty = document.getElementsByName("qty[]");	
	var len = qty.length; 
	for(i=0; i<len; i++)
	{
		if(qty[i].value <= 0)
		{ 
			alert("Please Eneter valid digit only.");	
			qty[i].focus();
			return false;			
		}
	}
	//loadingScreen();	
	var pid = new Array();
	var sess_id = new Array();
	var qty = new Array();
//	var formlength = Number(document.getElementById("TotalCartItems").value);
	var MinPID = document.getElementsByName("MINPIDC[]");
	for(i=0; i < MinPID.length; i++)
	{
		pid[pid.length] = MinPID[i].value;
	}

	var SessArr = document.getElementsByName("sess_idC[]");
	for(i=0; i < SessArr.length; i++)
	{
		sess_id[sess_id.length] = SessArr[i].value;
	}
	
	var QtyArr = document.getElementsByName("qtyC[]");
	for(i=0; i < QtyArr.length; i++)
	{
		if(QtyArr[i].value == "" || QtyArr[i].value <= 0)
		{
			alert("Please Enter valid digit only!.");
			QtyArr[i].focus();
			return false;
		}
		else
			qty[qty.length] = QtyArr[i].value;
	}
	loadingScreen();	
	parameters = "&ProductID=" + pid + "&sess_id="+sess_id+"&qty="+qty;
	addToCart_test(999, ajaxpath + 'manage.php?p=shopping_cart_minicart&a=update'+parameters);	
}
