// JavaScript Document
function cartSubmit()
{
	var win = window.open("","checkout", "location=0,status=1,scrollbars=1,resizable=1,width=650,height=550");
	win.focus();
}

function addToCart(cb, prodid, val)
{
	var name = cb.attr("name"); //retrieve title of link so we can compare with php file
	$.ajax({
		method: "get",url: "/struct/Cart/cart.switchboard.php",data: "mode=prodAdd&prod="+name,
		beforeSend: function(){}, //show loading just when link is clicked
		complete: function(){}, //stop showing loading when the process is complete
		success: function(html){ //so, if data is retrieved, store it in html
		cartContent.html(html); //show the html inside .content div
	 	}
	 }); //close $.ajax(
}

function removeFromCart(prodid, val)
{
	alert(prodid + ' is being removed from cart: -' + val);
}

function selcheck(cb)
{
	var name = cb.attr("name");
	var val = cb.attr("value");
	
	if(cb.attr("checked") == true)
	{
		return "addItem";
	}
	else
	{
		return "removeItem";
	}
}

function checkSelectedProducts()
{
	var cat = $("[name=prod_category]").attr("value");
	for(var i in cart)
	{
		if(cat == cart[i]["category_id"])
		{
			var prod = "[name=" + cart[i]["product_name"] + "]";
			if($(prod).attr("type") == "checkbox")
			{
				$(prod).attr("checked", true);
			}
			else
			{
				$(prod).attr("value", cart[i]["product_amount"]);
			}
		}
	}
}

// When the document loads do everything inside here ...
 $(document).ready(function(){
 	 $('.cartContent').load('/struct/Cart/cart.switchboard.php');	//by default initally load text from boo.php
	 checkSelectedProducts();
	
	 $("#remove_prod_item").live("click", function()
	 {
		var here = $(this);
		var mode = "removeItem";
		var name = here.attr("rel");
		var obj = "[name=" + name +"]";
		$.ajax({
			method: "get",url: "/struct/Cart/cart.switchboard.php",data: "mode=" + mode +"&prod="+name,
			beforeSend: function(){
				$("#cartloader").show();
				$(".cartContent").hide();
			}, //show loading just when link is clicked
			complete: function(){
				$("#cartloader").hide();
				$(".cartContent").show();
			}, //stop showing loading when the process is complete
			success: function(html){ //so, if data is retrieved, store it in html
				$(".cartContent").html(html); //show the html inside .content div
				if($(obj).attr("type") == "checkbox")
				{
					$(obj).attr("checked", false);
				}
				else
				{
					$(obj).attr("value","");
				}
			}
		 }); //close $.ajax(
		return false;
	 });
	
	 $('#eventform input.select').click(function() { //start function when any link is clicked
		var mode = selcheck($(this));
		var name = $(this).attr("name"); //retrieve title of link so we can compare with php file
		
		var qname = $(this).attr("name") + "_qty";
		var prodqty = document.eventform[qname].value;
		
		$.ajax({
			method: "get",url: "/struct/Cart/cart.switchboard.php",data: "mode=" + mode +"&prod="+name+"&prodqty="+prodqty,
			beforeSend: function(){
				$("#cartloader").show();
				$(".cartContent").hide();
			}, //show loading just when link is clicked
			complete: function(){
				$("#cartloader").hide();
				$(".cartContent").show();
			}, //stop showing loading when the process is complete
			success: function(html){ //so, if data is retrieved, store it in html
			$(".cartContent").html(html); //show the html inside .content div
			}
		 }); //close $.ajax(
	 }); //close click(
	 
	 $('#cartList input').click(function() { //start function when any link is clicked
		var mode = "removeItem";
		var name = $(this).attr("name"); //retrieve title of link so we can compare with php file
		$.ajax({
			method: "get",url: "/struct/Cart/cart.switchboard.php",data: "mode=" + mode +"&prod="+name,
			beforeSend: function(){}, //show loading just when link is clicked
			complete: function(){}, //stop showing loading when the process is complete
			success: function(html){ //so, if data is retrieved, store it in html
				$(".cartContent").html(html); //show the html inside .content div
			}
		 }); //close $.ajax(
	 }); //close click(	 
	 
	$("a.logOut").click(function(){
		var mode = "logOut";
		var name = $(this).attr("name"); //retrieve title of link so we can compare with php file
		$.ajax({
			method: "get",url: "/struct/Cart/cart.switchboard.php",data: "mode=" + mode +"&prod="+name,
			beforeSend: function(){}, //show loading just when link is clicked
			complete: function(){}, //stop showing loading when the process is complete
			success: function(html){ //so, if data is retrieved, store it in html
				alert(html);
				location.href = "index.php";
				//self.parent.tb_remove();
			}
		 }); //close $.ajax(
	}); //close logout 
	 
	$("a.close").click(function(){
		var mode = "close";
		var name = $(this).attr("name"); //retrieve title of link so we can compare with php file
		$.ajax({
			method: "get",url: "/struct/Cart/cart.switchboard.php",data: "mode=" + mode +"&prod="+name,
			beforeSend: function(){}, //show loading just when link is clicked
			complete: function(){}, //stop showing loading when the process is complete
			success: function(html){ //so, if data is retrieved, store it in html
				alert(html);
				self.parent.tb_remove();
			}
		 }); //close $.ajax(
	}); //close close
	 
	$("a.checkOut").click(function(){
		//alert("We are STARTING to send the info off for processing.");
		var mode = "checkOut";
		$.ajax({
			method: "get",url: "/struct/Cart/cart.switchboard.php",data: "mode=" + mode,
			beforeSend: function(){
				document.orderPage_transactionType.submit();
			}, //show loading just when link is clicked
			complete: function(){}, //stop showing loading when the process is complete
			success: function(html){ //so, if data is retrieved, store it in html
				//alert(html);
				self.parent.tb_remove();
			}
		 }); //close $.ajax(
	}); //close checkOut
		
	$(".qty").change(function(){
		//alert("we have changed the quantity");
		var name = $(this).attr("rel");
		var prodqty = $(this).attr("value");
		
		if(document.eventform[name].checked == true)
		{
			$.ajax({
				method: "get",url: "/struct/Cart/cart.switchboard.php",data: "mode=updateCart&prod="+name+"&prodqty="+prodqty,
				beforeSend: function(){
					$("#cartloader").show();
					$(".cartContent").hide();
				}, //show loading just when link is clicked
				complete: function(){
					$("#cartloader").hide();
					$(".cartContent").show();
				}, //stop showing loading when the process is complete
				success: function(html){ //so, if data is retrieved, store it in html
				$(".cartContent").html(html); //show the html inside .content div
				}
			 }); //close $.ajax(
		}
		
	});
			
	$(".donation").change(function(){
		var name = $(this).attr("name");
		var val = $(this).attr("value");
		var mode = "donation";
		
		$.ajax({
			method: "get",url: "/struct/Cart/cart.switchboard.php",data: "mode=" + mode + "&prod=" + name + "&value=" + val,
			beforeSend: function(){
				$("#cartloader").show();
				$(".cartContent").hide();
			}, //show loading just when link is clicked
			complete: function(){
				$("#cartloader").hide();
				$(".cartContent").show();
			}, //stop showing loading when the process is complete
			success: function(html){ //so, if data is retrieved, store it in html
				//alert(html);
				$(".cartContent").html(html);
			}
		 }); //close $.ajax(
	});
	
	$(".in_the_name_of").change(function(){
		var name = $(this).attr("name");
		var val = $(this).attr("value");
		var mode = "in_the_name_of";
		
		$.ajax({
			type: "post",
			url: "/struct/Cart/cart.switchboard.php",
			data: "mode=" + mode + "&in_the_name_of=" + val,
			beforeSend: function(){
				$("#cartloader").show();
				$(".cartContent").hide();
			}, //show loading just when link is clicked
			complete: function(){
				$("#cartloader").hide();
				$(".cartContent").show();
			}, //stop showing loading when the process is complete
			success: function(html){ //so, if data is retrieved, store it in html
				//alert(html);
				$(".cartContent").html(html);
			}
		 }); //close $.ajax(
	})
		
	
 }); //close $(
