/**
 * Javascript functions for Merchandise area of the site
 * Mainly concerned with ajax requests
 */
function hideOrder(id)
{
	$("#view-order-"+id).hide();
	$("#view-order-link-"+id).text("view order");
	$("#view-order-link-"+id).attr("onclick", "viewOrder('"+ id +"')");
}

function viewOrder(id)
{

	$.ajax({
		url: "merchandise_query.php?type=getOrder&id="+id,
		cache: false,
		success: function(str){
			$("#view-order-"+id).html(str).show();
			$("#view-order-link-"+id).text("hide");
			$("#view-order-link-"+id).attr("onclick", "hideOrder('"+ id +"')");
		}
	});
}


function addPostage()
{

	var postage = document.checkoutForm.postage.value;

	//var total = $("#postage-total").load("merchandise_ajax.php?action=add_postage&postage="+postage);

	$.ajax({
		url: "merchandise_ajax.php?action=add_postage&postage="+postage,
		cache: false,
		success: function(str){
			var num = parseInt(str)/100;
			$("#postage-total").text(num.toFixed(2));
			$("#purchase-amount").attr("value",parseInt(str).toFixed(0));
		}
	});

}

var togg = 0;
function setShipping()
{
	if(togg==0)
	{
		$("#ShippingFirstName").attr("value",$("#BillingFirstName").val());
		$("#ShippingLastName").attr("value",$("#BillingLastName").val());
		$("#ShippingAddress1").attr("value",$("#BillingAddress1").val());
		$("#ShippingAddress2").attr("value",$("#BillingAddress2").val());
		$("#ShippingCity").attr("value",$("#BillingCity").val());
		$("#ShippingCounty").attr("value",$("#BillingCounty").val());
		$("#ShippingPostal").attr("value",$("#BillingPostal").val());
		$("#ShippingCountry").attr("value",$("#BillingCountry").val());
		togg = 1;
		return true;
	}
	if(togg==1)
	{
		var empty = '';
		$("#ShippingFirstName").attr("value",empty);
		$("#ShippingLastName").attr("value",empty);
		$("#ShippingAddress1").attr("value",empty);
		$("#ShippingAddress2").attr("value",empty);
		$("#ShippingCity").attr("value",empty);
		$("#ShippingCounty").attr("value",empty);
		$("#ShippingPostal").attr("value",empty);
		$("#ShippingCountry").attr("value",empty);
		togg = 0;
		return true;
	}

}

function createPreOrder()
{
	var str = $("#hsbcform").serialize();
	str = str + "&postageAmount=" + $("#postage").val();
	$.post("merchandise_hsbcpre.php", str, 
				function (data, textStatus) { 
					$("#order_id").attr("value",data);
					_generateHash();
				}
	);
	return false;
}

function _generateHash()
{
		var str = $("#hsbcform").serialize();
		$.ajax({
			type: "POST",
			url: "merchandise/merchandise_post.php",
			data: str,
			dataType: "html",
			success: function(msg){
				$("#order-hash").attr("value",msg);
				document.hsbcform.submit();
			}
		});
		return false;
}

function _markStatusss(id,statusType)
{
	
	$.ajax({
		url: "merchandise_query.php?type=markStatus&statusType="+statusType+"&id="+id,
		success: function(msg){
			alert(msg);
		}
 	});
	return false;
}

function addToCart(id,price,title)
{
	$("#cart-content").load("merchandise_ajax.php?action=add&id="+id+"&price="+price+"&title="+title);
	return false;
}


function emptyCart()
{
	$("#total").load("merchandise_ajax.php?action=emptycart");
	$("#cart-content").html("<b>Cart is empty</b>");
	return false;
}

function openWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function showOrdersByMonth(month,year)
{
	$("#orders-thismonth").load("merchandise_query.php?type=ordersByMonth&month="+month+"&year="+year);
}
