function CartProductSingle() {
}

CartProductSingle.opener = null;
CartProductSingle.backendScript = null;
CartProductSingle.callingBlock = null;

CartProductSingle.initiate = function() {
	$("div.messagesErrors").hide();
	$("a.single-add-button").click(CartProductSingle.runSingleAdd);
	$("form.product-cart-add").submit(CartProductSingle.AddToCart);
	/*if($("#externalProductLink").find("a").attr("href") != undefined
			&& $("#externalProductLink").find("a").attr("href").length > 0)
	{
		$(".add_to_cart_holder").hide();
		$("p.price_colour").hide();
	}
	else
	{
		$("#externalProductLink").hide();
	}*/
};

CartProductSingle.runSingleAdd = function() {
	//submit the current form
	$(this).parent('.product_actions').parent('.product_block').find('form.product-cart-add').submit();
	return false;
};

CartProductSingle.AddToCart = function () {
	CartProductSingle.backendScript = this.action;
	CartProductSingle.callingBlock = this;
	
	var url = this.action;
	
	//initialize communications
	var comm = new Communication(url);
	
	//send request
	comm.sendByPost($(this).serialize(), CartProductSingle.handleMessagesAndErrors);
	
	//$.post(this.action, $(this).serialize(), CartProductSingle.handleMessagesAndErrors);
	return false;
}

CartProductSingle.handleMessagesAndErrors = function(response) {

	var returnedData = eval("(" + response + ")");
	
	var divMessagesErrors = $(CartProductSingle.callingBlock).parent("div.product_block").find('div.messagesErrors');
	divMessagesErrors.show();
	
	//remove classes
	divMessagesErrors.removeClass("adminActionMessagesSearch");
	divMessagesErrors.removeClass("adminActionErrorsSearch");
	
	if(returnedData[0] > 0)
	{
		//add class for errors
		divMessagesErrors.addClass("adminActionErrorsSearch");
		divMessagesErrors.html(returnedData[1]);
	}
	else
	{
		//add class for messages
		divMessagesErrors.addClass("adminActionMessagesSearch");
		
		//auth code update
		$("input[name*='qseusc']").val(returnedData[2])
		//update the cart items at the top
		$("li.cart").children("strong").html(returnedData[3]);
		//display the checkout button
		var backendPath = CartProductSingle.backendScript.substr(0, CartProductSingle.backendScript.lastIndexOf("/")+1);
		var search='href=""';
		var replace='href="'+CartProductSingle.backendScript+'"';
		returnedData[1] = returnedData[1].replace(search, replace);
		search='src=""';
		replace='src="' + backendPath + 'images/btns/checkout_btn.gif"';
		returnedData[1] = returnedData[1].replace(search, replace);
		
		divMessagesErrors.html(returnedData[1]);
	}
};
