// fuse interactive 2007
// fullyloadedtea

if (window.XMLHttpRequest) var xmlHttp = new XMLHttpRequest();
else if (window.ActiveXObject) var xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");


function addToCart(productid,productname,productprice){

		var quantity = 1;

		//add it
		quantity = $('#quantity').val();

		$('#shoppingCart_button').addClass("selected").fadeIn("fast");
		//$.get("../../assets/ajax/addToCart.php", { productid: productid, productname: productname, price: productprice, quantity: quantity } );
		var url = "/assets/ajax/addToCart.php?productid=" + productid + "&productname=" + productname + "&price=" + productprice + "&quantity=" + quantity;
		xmlHttp.open("GET", url, false);
		xmlHttp.send(null);

		window.location = '../cart.php';
	}

function addToCart_Overview(productid,productname,productprice){

		var quantity = 1;

		//add it
		quantity = $('#quantity_' + productid).val();
		//put it into the shopping bag
		$('#shoppingCart_button').addClass("selected").fadeIn("fast");
		//$.get("../assets/ajax/addToCart.php", { productid: productid, productname: productname, price: productprice, quantity: quantity } );
		var url = "/assets/ajax/addToCart.php?productid=" + productid + "&productname=" + productname + "&price=" + productprice + "&quantity=" + quantity;
		xmlHttp.open("GET", url, false);
		xmlHttp.send(null);

		window.location = 'cart.php';
	}

function changeProductQuantity(cartitemid,pname,number,productprice,oldquantity){
		var newquantity = $('#quantity_' + number).val();
		var oldtotal = $('#totalPrice').attr("title");
		if(newquantity == 0) { removeProductFromCart(cartitemid); return;}


		if(newquantity > oldquantity || newquantity < oldquantity){
			var newtotal = Math.round(((oldtotal - (oldquantity*productprice)) + (newquantity*productprice))*100)/100;
			var quantitydifference = newquantity - oldquantity;
			//$.get("../assets/ajax/addToCart.php", { cartitemid: cartitemid, quantity: quantitydifference } );
			var url = "/assets/ajax/addToCart.php?cartitemid=" + cartitemid + "&quantity=" + quantitydifference;
			xmlHttp.open("GET", url, false);
			xmlHttp.send(null);

			$('#totalPrice').attr({title: newtotal});
			$('#quantity_' + number).attr({onchange: "changeProductQuantity('" + cartitemid + "','" +  pname + "','" + number + "','" + productprice + "','" + newquantity + "')"});
			$('#totalPrice').empty().append('Total: $' + newtotal + '&nbsp;');
			if($('#currency_code').val() == 'CAD') $('#totalPrice').append('CAD');
			//$('#quantity_' + number).val(newquantity);
		}
}

function removeProductFromCart(itemid){

		//var item_id = itemid+1;
		//$('#item_'+ item_id).slideUp("slow");
		//$.get("../assets/ajax/removeFromCart.php", { itemid: itemid } );

		var url = "/assets/ajax/removeFromCart.php?itemid=" + itemid;
		xmlHttp.open("GET", url, false);
		xmlHttp.send(null);

		document.location.reload( true );

	}

function checkOut(){

		if($('#item_1').children().is("td")){
			}
		else{
				$('#missingInfo').empty().attr({display: "block"}).append("There is no item in your shopping bag.").fadeIn("slow");
				return false;


			}

	}

function changeCurrency(currency){


		if(currency == 'ca'){
			currency = "CAD";
			$('#us_flag').attr({src: "../assets/images/us_flag_g.gif"});
			$('#ca_flag').attr({src: "../assets/images/ca_flag_s.gif"});

		}
		else {
			currency = "USD";
			$('#us_flag').attr({src: "../assets/images/us_flag_s.gif"});
			$('#ca_flag').attr({src: "../assets/images/ca_flag_g.gif"});
		}
		//$.get("http://www.fullyloadedtea.com/assets/ajax/changeCurrency.php", { currency: currency } );

		var url = "/assets/ajax/changeCurrency.php?currency=" + currency;
		xmlHttp.open("GET", url, false);
		xmlHttp.send(null);

		document.location.reload( true );

	}
function popitup(url) {
	newwindow=window.open(url,'name','height=150,width=250');
	if (window.focus) {newwindow.focus()}
	return false;
}

function validateDiscountCode(oldtotal,currentcurrency){
		var discountcode = $('#discountcode').val();

		var url = "http://www.fullyloadedtea.com/shop/validatediscountcode.php?discountcode=" + discountcode + "&oldtotal=" + oldtotal + "&action=validate";

		xmlHttp.open("GET", url, true);
		xmlHttp.onreadystatechange = stateChangedDiscountCode;
		xmlHttp.send(null);

}
function stateChangedDiscountCode()
		{
		$('#discountproblem').remove();
			if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
			{
				if(xmlHttp.responseText == "error"){
						$('#shopping_bag').find("h2").after("<div id=\"discountproblem\" class=\"missinginfo\">The discount code couldn't be applied.</div>").fadeIn("slow");
						return false;
					}
				else if(xmlHttp.responseText == "badcode"){
						$('#discountproblem').remove();
						$('#shopping_bag').find("h2").after("<div id=\"discountproblem\" class=\"missinginfo\">This discount code isn't valid</div>").fadeIn("slow");
						return false;
					}
				else if(xmlHttp.responseText > 0){

						var oldtotal = $('#totalPrice').attr("title");
						var newtotal = Math.round(oldtotal*(100 - xmlHttp.responseText))/100;
						$('#totalPrice').empty().append('Total: $' + newtotal + '&nbsp;');
						if($('#currency_code').val() == 'CAD') $('#totalPrice').append('CAD');
						$('#totalPrice').addClass("highlight");
						$('#shopping_bag').find("h2").after("<div id=\"discountproblem\" class=\"discountinfo\">Discount was applied to your total order.</div>").fadeIn("slow");
						$('#discountCodeButton').attr({onclick: ""});
						$('#shopping_bag').find("input.itemAmount").attr("value", function() { return Math.round(this.value *(100 - xmlHttp.responseText))/100; });
					}
			}
		}
function hoverBg(x){
//	alert(x.parentNode.className);
//	x.parentNode.style.backgroundColor = '#e8e8e8';
	x.style.backgroundColor = '#e8e8e8';
}
function hoverBgOut(x){
//	alert(x.parentNode.className);
//	x.parentNode.style.backgroundColor = '#f2f2f2';
	x.style.backgroundColor = '#f2f2f2';
}