function SelectAll()
{
	if(document.cart.selectall.checked==true)	
	{
		for(var i=0; i<document.cart.pid.length; i++)
		{
			document.cart.pid[i].checked = true;
		}
	}
	else
	{
		for(var i=0; i<document.cart.pid.length; i++)
		{
			document.cart.pid[i].checked = false;
		}
	}
}
function ViewCart()
{
	if (window.XMLHttpRequest)
	{ // Mozilla, Safari, ...
		xmlhttp = new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{ // IE
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.open('GET', 'cart/update_details.php?type=viewcart', true);
	xmlhttp.onreadystatechange = gotoViewCartPage; 	
	xmlhttp.send(null);
}
function gotoViewCartPage()
{
	if (xmlhttp.readyState == 4)
	{
		str = xmlhttp.responseText;
		
		if(str!='')
		{
			//document.getElementById("cartlist").innerHTML=str;
			window.location.href='cart.html';
		}
	}	
}
function AddtoCartProduct(ProductId,ProductName,ProductPrice)
{
	var ProductName = encodeURIComponent(ProductName);
	if (window.XMLHttpRequest)
	{ // Mozilla, Safari, ...
		xmlhttp = new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{ // IE
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.open('GET', 'cart/update_details.php?ProductId='+ProductId+'&ProductName='+ProductName+'&ProductPrice='+ProductPrice+'&type=addtocart', true);
	xmlhttp.onreadystatechange = gotoCartPage; 	
	xmlhttp.send(null);
}
function gotoCartPage()
{
	if (xmlhttp.readyState == 4)
	{
		str = xmlhttp.responseText;
		if(str!='')
		{
			//document.getElementById("cartlist").innerHTML=str;
			window.location.href='cart.html';
		}
	}	
}
function AddtoCartProductDeal(ProductIds)
{
	if (window.XMLHttpRequest)
	{ // Mozilla, Safari, ...
		xmlhttp = new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{ // IE
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.open('GET', 'cart/update_details.php?ProductIds='+ProductIds+'&type=addtocart_deal', true);
	xmlhttp.onreadystatechange = gotoCartPage; 	
	xmlhttp.send(null);
}
function UpdateColor(pid)
{
	var Color = document.getElementById("Color"+pid).value;		
	if (window.XMLHttpRequest)
	{ // Mozilla, Safari, ...
		xmlhttp = new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{ // IE
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.open('GET', 'cart/update_details.php?Color='+Color+'&pid='+pid+'&type=updatecolor', true);
	xmlhttp.onreadystatechange = gotoUpdateColor; 	
	xmlhttp.send(null);
}

function gotoUpdateColor()
{
	if (xmlhttp.readyState == 4)
	{
		str = xmlhttp.responseText;
		if(str!='')
		{
			document.getElementById("cartlist").innerHTML=str;
		}
	}	
}
/*function removeItems(pid)
{
	var c_value = "";
	for (var i=0; i < document.cart.pid.length; i++)
	{	
		 if (document.cart.pid[i].checked && document.cart.pid[i].value!="temp")
         {
		    c_value = c_value + document.cart.pid[i].value + ",";
      	 }
   	}
	if(c_value=="")
	{
		alert('Please select items to remove from your cart');
	}
	else
	{
		if (window.XMLHttpRequest)
		{ // Mozilla, Safari, ...
			xmlhttp = new XMLHttpRequest();
		}
		else if (window.ActiveXObject)
		{ // IE
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		
		xmlhttp.open('GET', 'cart/update_details.php?pids='+c_value+'&type=remove', true);
		xmlhttp.onreadystatechange = gotoNextPage; 	
		xmlhttp.send(null);
	}
}
function updateCart()
{
	var p_code = document.cart.pcode.value;
	var c_value = "";
	for (var i=0; i < document.cart.pid.length; i++)
	{	
		    if(document.cart.pid[i].value!="temp")
			{
			c_value = c_value + document.cart.pid[i].value + ",";
			}
   	}
	
	var q_value = "";
	for (var j=0; j < document.cart.qty.length; j++)
	{	
		    if(document.cart.qty[j].value!="temp")
			{
			q_value = q_value + document.cart.qty[j].value + ",";
			}
   	}
	
	if (window.XMLHttpRequest)
	{ // Mozilla, Safari, ...
		xmlhttp = new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{ // IE
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.open('GET', 'cart/update_details.php?pids='+c_value+'&qty='+q_value+'&type=update', true);
	xmlhttp.onreadystatechange = gotoNextPage; 	
	xmlhttp.send(null);
}
*/
function updateCart(pid)
{
	var c_value = pid;	
	var q_value = document.getElementById("qty"+pid).value;
	var p_code = document.getElementById("pcode"+pid).value;
	if(q_value==0){
		alert("You have entered invalid product quantity, please re-enter it.");
		document.getElementById("qty"+pid).value = document.getElementById("prev_qty"+pid).value;
		document.getElementById("qty"+pid).focus();
		return false
	}else{
		if (window.XMLHttpRequest)
		{ // Mozilla, Safari, ...
			xmlhttp = new XMLHttpRequest();
		}
		else if (window.ActiveXObject)
		{ // IE
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		xmlhttp.open('GET', 'cart/update_details.php?pids='+c_value+'&qty='+q_value+'&pcode='+p_code+'&type=update', true);
		xmlhttp.onreadystatechange = gotoNextPage; 	
		xmlhttp.send(null);
	}
}
function removeItems(pid)
{
	var c_value = pid;
	if(c_value!="")
	{
		if (window.XMLHttpRequest)
		{ // Mozilla, Safari, ...
			xmlhttp = new XMLHttpRequest();
		}
		else if (window.ActiveXObject)
		{ // IE
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		
		xmlhttp.open('GET', 'cart/update_details.php?pids='+c_value+'&type=remove', true);
		xmlhttp.onreadystatechange = gotoNextPage; 	
		xmlhttp.send(null);
	}
}

function gotoNextPage()
{
	if (xmlhttp.readyState == 4)
	{
		
		str = xmlhttp.responseText;
		if(str==0){
			alert("Your Cart is empty, please add some products to your cart!!");
			location.href = 'cart.html';
		}
		else
		document.getElementById("cartlist").innerHTML = str;
	}	
}

function checkOut()
{
		
	for (var i=0; i < document.cart.pid.length; i++)
	{	
		if(document.cart.pid[i].value!="temp")
		{		
			var colorid = 'Color'+document.cart.pid[i].value;
			if (document.getElementById(colorid).selectedIndex==0) {
				alert("Please Select Color");
				document.getElementById(colorid).focus();			
				return false; 
			}
		}
	}
	var c_value = "";
	for (var i=0; i < document.cart.pid.length; i++)
	{	
			if(document.cart.pid[i].value!="temp")
			{
			c_value = c_value + document.cart.pid[i].value + ",";
			}
	}
	
	var q_value = "";
	for (var j=0; j < document.cart.qty.length; j++)
	{	
			if(document.cart.qty[j].value!="temp")
			{
			q_value = q_value + document.cart.qty[j].value + ",";
			}
	}
	
	if (window.XMLHttpRequest)
	{ // Mozilla, Safari, ...
		xmlhttp = new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{ // IE
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	xmlhttp.open('GET', 'cart/update_details.php?pids='+c_value+'&qty='+q_value+'&type=update', true);
	xmlhttp.onreadystatechange = gotoCheckOutPage; 	
	xmlhttp.send(null);
}
function gotoCheckOutPage()
{
	if (xmlhttp.readyState == 4)
	{
		pagename = document.getElementById('redirectName').value;
		location.href=pagename;
	}
}

function CopyShippingAdress()
{
	if(document.user.SameAddress.checked==true)
	{
		var Country = document.user.Country.value;
		document.user.ShippingFirstName.value = document.user.FirstName.value;
		document.user.ShippingLastName.value = document.user.LastName.value;
		document.user.ShippingAddress1.value = document.user.Address1.value;
		document.user.ShippingCity.value = document.user.City.value;
		document.user.ShippingZip.value = document.user.Zip.value;
		//document.user.ShippingCountry.value = document.user.Country.value;
		//document.user.ShippingState.value = document.user.State.value;						
		document.user.OtherShippingState.value = document.user.OtherState.value;								
		initShippingCountry(Country,'ShippingCountry','ShippingState',document.user.Country.value,document.user.State.value);		
		if(document.user.State.value=='Other')
			document.getElementById("DivShippingState").style.display="";
		else	
			document.getElementById("DivShippingState").style.display="none";
	}
	else
	{
		document.user.ShippingFirstName.value = "";
		document.user.ShippingLastName.value = "";
		document.user.ShippingAddress1.value = "";
		document.user.ShippingCity.value = "";
		document.user.ShippingState.value = "";
		document.user.ShippingZip.value = "";
		document.user.ShippingCountry.value = "";
		if(document.user.ShippingState.value=='Other')
			document.getElementById("DivShippingState").style.display=""
		else	
			document.getElementById("DivShippingState").style.display="none"
	}
}

function checkoutForm()
{
	validateUserForm();
	if(document.user.currid.value==1)
	{
		UpdateUserData();
	}
}
function validatePaypalInfo(mpcodestaus,price)
{
	if(document.user.CardNumber.value=="")
	{
			alert('CardNumber is required');
			document.user.CardNumber.focus();
			return false;
	}
	if(document.user.exMonth.value=="")
	{
			alert('Expiration Date is required');
			document.user.exMonth.focus();
			return false;
	}
	if(document.user.exYear.value=="")
	{
			alert('Expiration Date is required');
			document.user.exYear.focus();
			return false;
	}
	if(document.user.CardCode.value=="")
	{
			alert('CCV is required');
			document.user.CardCode.focus();
			return false;
	}
	if(mpcodestaus=='Y')
	{
		Sexy.confirm('<h1>Checkout Alert! </h1><p>Are you sure you want to checkout? Please add more products to your order in order to maximize your voucher! There is $'+price+' remaining.<br><br>The voucher is for one-time use only and if you proceed, you will not be able to use it again and will forfeit the remaining value.</p>' ,{ onComplete: 
		  function(returnvalue) {
			if(returnvalue)
			{
				document.user.submit();
				return true;
			}
			else
			{
				window.location.reload();
			}
		  }
		});
		return false;
	}
	
}
function validateUserForm()
{
	if(document.user.FirstName.value=="")
	{
			alert('First Name is required');
			document.user.FirstName.focus();
			return false;
	}
	if(document.user.LastName.value=="")
	{
			alert('Last Name is required');
			document.user.LastName.focus();
			return false;
	}
	
	if(document.user.Address1.value=="")
	{
			alert('Address is required');
			document.user.Address1.focus();
			return false;
	}
	if(document.user.City.value=="")
	{
			alert('City is required');
			document.user.City.focus();
			return false;
	}
	if(document.user.State.value=="")
	{
			alert('State is required');
			document.user.State.focus();
			return false;
	}
	if(document.user.State.value=="Other")
	{
		if(document.user.OtherState.value=="")
		{
			alert('State is required');
			document.user.OtherState.focus();
			return false;
		}
	}
	if(document.user.Zip.value=="")
	{
			alert('Zip is required');
			document.user.Zip.focus();
			return false;
	}
	if(document.user.Country.value=="")
	{
			alert('Country is required');
			document.user.Country.focus();
			return false;
	}

	if(document.user.Email.value=="")
	{
			alert('Email is required');
			document.user.Email.focus();
			return false;
	}
	if(document.user.Email.value!="")
	{
		if(!emailCheck(document.user.Email.value))
		{
			document.user.Email.value="";
			document.user.Email.focus();
			return false;
		}
	}
	if(document.user.Phone.value=="")
	{
			alert('Phone is required');
			document.user.Phone.focus();
			return false;
	}

	if(document.user.ShippingFirstName.value=="")
	{
			alert('Shipping First Name is required');
			document.user.ShippingFirstName.focus();
			return false;
	}
	if(document.user.ShippingLastName.value=="")
	{
			alert('Shipping Last Name is required');
			document.user.ShippingLastName.focus();
			return false;
	}
	if(document.user.ShippingAddress1.value=="")
	{
			alert('Shipping Address is required');
			document.user.ShippingAddress1.focus();
			return false;
	}
	if(document.user.ShippingCity.value=="")
	{
			alert('Shipping City is required');
			document.user.ShippingCity.focus();
			return false;
	}
	if(document.user.ShippingState.value=="")
	{
			alert('Shipping State is required');
			document.user.ShippingState.focus();
			return false;
	}
	if(document.user.ShippingState.value=="Other")
	{
		if(document.user.OtherShippingState.value=="")
		{
			alert('Shipping State is required');
			document.user.OtherShippingState.focus();
			return false;
		}
	}
	if(document.user.ShippingZip.value=="")
	{
			alert('Shipping Zip is required');
			document.user.ShippingZip.focus();
			return false;
	}

	if(document.user.ShippingCountry.value=="")
	{
			alert('Shipping Country is required');
			document.user.ShippingCountry.focus();
			return false;
	}
	

	document.user.currid.value=1;
}
function UpdateUserData()
{
			//var CardNumber = encodeURIComponent(document.user.CardNumber.value);
			//var ExpDate = (document.user.exMonth.value)+'/'+(document.user.exYear.value);
			//var CardCode = encodeURIComponent(document.user.CardCode.value);
			var CartId = encodeURIComponent(document.user.CartId.value);
			var userId = encodeURIComponent(document.user.userId.value);
			var FirstName = encodeURIComponent(document.user.FirstName.value);
			var LastName = encodeURIComponent(document.user.LastName.value);
			var Email = encodeURIComponent(document.user.Email.value);
			var Phone = encodeURIComponent(document.user.Phone.value);
	
			var Address1 = encodeURIComponent(document.user.Address1.value);
			var City = encodeURIComponent(document.user.City.value);
			var State = encodeURIComponent(document.user.State.value);
			var OtherState = encodeURIComponent(document.user.OtherState.value);			
			var Zip = encodeURIComponent(document.user.Zip.value);
			var Country = encodeURIComponent(document.user.Country.value);
	
			var ShippingFirstName = encodeURIComponent(document.user.ShippingFirstName.value);
			var ShippingLastName = encodeURIComponent(document.user.ShippingLastName.value);
			var ShippingAddress1 = encodeURIComponent(document.user.ShippingAddress1.value);
			var ShippingCity = encodeURIComponent(document.user.ShippingCity.value);
			var ShippingState = encodeURIComponent(document.user.ShippingState.value);
			var OtherShippingState = encodeURIComponent(document.user.OtherShippingState.value);			
			var ShippingZip = encodeURIComponent(document.user.ShippingZip.value);
			var ShippingCountry = encodeURIComponent(document.user.ShippingCountry.value);
	
			if (window.XMLHttpRequest)
			{ // Mozilla, Safari, ...
				xmlhttp = new XMLHttpRequest();
			}
			else if (window.ActiveXObject)
			{ // IE
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
			//document.getElementById('loaderbar').className = 'showloaderbar';
			xmlhttp.open('GET', 'cart/update_details.php?CartId='+CartId+'&FirstName='+FirstName+'&LastName='+LastName+'&userId='+userId+'&Email='+Email+'&Phone='+Phone+'&Address1='+Address1+'&City='+City+'&State='+State+'&OtherState='+OtherState+'&Zip='+Zip+'&Country='+Country+'&ShippingFirstName='+ShippingFirstName+'&ShippingLastName='+ShippingLastName+'&ShippingAddress1='+ShippingAddress1+'&ShippingCity='+ShippingCity+'&ShippingState='+ShippingState+'&OtherShippingState='+OtherShippingState+'&ShippingZip='+ShippingZip+'&ShippingCountry='+ShippingCountry+'&type=userdata', true);
//			xmlhttp.onreadystatechange = updateCheckoutForm; 	
			xmlhttp.onreadystatechange = gotoFinalPage; 	
			xmlhttp.send(null);
}

function updateCheckoutForm()
{
	if (xmlhttp.readyState == 4)
	{
		var c_value = "";
		for (var i=0; i < document.cart.pid.length; i++)
		{	
				if(document.cart.pid[i].value!="temp")
				{
				c_value = c_value + document.cart.pid[i].value + ",";
				}
		}
		
		var q_value = "";
		for (var j=0; j < document.cart.qty.length; j++)
		{	
				if(document.cart.qty[j].value!="temp")
				{
				q_value = q_value + document.cart.qty[j].value + ",";
				}
		}
		c_value = c_value + "0,";		
		q_value = q_value + "0,";		
		
		if (window.XMLHttpRequest)
		{ // Mozilla, Safari, ...
			xmlhttp = new XMLHttpRequest();
		}
		else if (window.ActiveXObject)
		{ // IE
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		
		xmlhttp.open('GET', 'cart/update_details.php?pids='+c_value+'&qty='+q_value+'&type=update', true);
		xmlhttp.onreadystatechange = gotoFinalPage; 	
		xmlhttp.send(null);
	}
}

function gotoFinalPage()
{
	if (xmlhttp.readyState == 4)
	{
		//document.getElementById('loaderbar').className = 'hideloaderbar';
		str = xmlhttp.responseText;
		location.href='checkorder.html';
	}	
}
function update_mpdiscount(uid,subtotal,page)
{
	var uid = uid;	
	var subtotal = subtotal;		
	var mpcode = document.getElementById("mpcode").value;
	if(mpcode==''){
		alert("Please Enter Voucher Code.");
		return false;
	}else{
		if (window.XMLHttpRequest)
		{ // Mozilla, Safari, ...
			xmlhttp = new XMLHttpRequest();
		}
		else if (window.ActiveXObject)
		{ // IE
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		xmlhttp.open('GET', 'cart/update_details.php?uid='+uid+'&subtotal='+subtotal+'&mpcode='+mpcode+'&page='+page+'&type=update_mpdiscount', true);
		xmlhttp.onreadystatechange = gotoMPDiscountPage; 	
		xmlhttp.send(null);
	}
}
function gotoMPDiscountPage()
{
	if (xmlhttp.readyState == 4)
	{
		//document.getElementById('loaderbar').className = 'hideloaderbar';
		str = xmlhttp.responseText;
//		location.href='checkorder.html';
		document.getElementById("cartlist").innerHTML=str;
	}	
}
function emailCheck(s1) {
	emailStr=s1;
	var emailPat=/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/
	var matchArray=emailStr.match(emailPat)
	if (matchArray==null) 
	{
		//alert("Email address seems incorrect (check @ and .'s)")
		alert("Email format should be xyz@yzx.com")
		return false
	}
	return true;
}
function testNumber(val)
{
	if (isNaN(val))
	{
	return false;
	}
}
function noNumbers(e)
{
	var keynum;
	var keychar;
	var numcheck;
	
	if(window.event) // IE
	  {
	  keynum = e.keyCode;
	  }
	else if(e.which) // Netscape/Firefox/Opera
	  {
	  keynum = e.which;
	  }
	keychar = String.fromCharCode(keynum);
	numcheck = /[0-9\b]/;
	return numcheck.test(keychar);
}

