/*
	This Javascript is the pluggable part of the eSalesForm. It adheres
	to a specific interface detailed below. It provides information regarding
	texts. It is called when for finalization of a product whenever the 
	"to cart" button is pressed.
	
	interface for products:
		- preloadTexts
		- preloadProducts
		- validateForm
		- clearForm
		- equals(a, b)
		
	It requires the rest of the system to be available as it uses other components
	such as the texts and cart objects.

*/

var prodServiceUrl = config.urlBase + "service/to_cart.php?";
var prodRetrieveUrl = config.urlBase + "service/products.php";
var tldRetrieveUrl = config.urlBase + "service/whoislist.php";

var
	PSTATUS_OK = 100,
	PSTATUS_ERROR = 200,
	PSTATUS_TAKEN = 101;

var product = 
{
	/** 
	 * 	Preload texts that are used frequently in the product form
	 */
	preloadTexts : function()
	{
		texts.multiLoad(
				["PARK", "LITE", "GO!", "CMS", "BASIS", "PLUS", 
				 "PRODUCT_ERROR", "PRODUCT_CANCEL", "PRODUCT_DUPLICATE"]);
	},
	
	/**
	 *	Preload products
	 */
	preloadProducts : function()
	{
		$.getJSON(prodRetrieveUrl, function(data)
			{
				if (data.status == 200)	// error
				{
					document.location.href = "error.php?why=products";
				}
				
				var content = "<ul id='hType'>";
				for (var i in data.info.products)
				{
					var label = data.info.products[i].name;
					var value = data.info.products[i].portalProductId;
					var textId = data.info.products[i].textId;
					var price = data.info.products[i].price;
					
					var selected = 
						data.info.products[i].portalProductId == data.info.set.favProductId ? "checked" : "";
					
					content += "<li onmouseover='texts.show(\"" + textId + "\")'>" + 
									"<input type='radio' value='" + value +"' class='hType' name='hostingType' " + selected + ">&nbsp;" + 
										label +
										(price > 0 ? "&nbsp;&nbsp;-&nbsp;&nbsp;<span style='color: #666666;'>" + cart.priceString(price * 100) + " p/j</span>" : "" ) + 
										"&nbsp;<img src='images/informatie.png' alt='Informatie'>" +
								"</li>";
				}
				content += "</ul>";
				
				$("#hTypeWrap").html(content);
			});
		
	
		$.getJSON(tldRetrieveUrl, 
				function(data)
				{
					if (data.status == 200)
					{
						document.location.href = "error.php?why=tld";
					}
					
					var content = "";
					for (var key in data.info)
					{
						content += "<option value='" + data.info[key].tld + "'>." + data.info[key].tld + "</option>";
					}
					
					$("#dTLD").html(content);
					$("#dTLD").val(".nl");
				}
			);
	},
	
	selectedHosting : function()
	{
		// find the selected hosting type
		var hostType = "";
		var types = $(".hType");
		for (var i in types)
		{
			if (types[i].checked)
			{
				return types[i].value;
			}
		}
		return "";
	},
	
	/**
	 *	Decide whether the domain name is correct using a regex.
	 */
	correctName : function(name)
	{
		var newName = name.replace(/[^A-Za-z0-9\-\ ]/, '');
		return newName == name && name != "";
	},
	
	/** 
	 *	Validate form elements
	 */
	validateForm : function()
	{
		var key;
		var tld = $("#dTLD").val();
		var name = $("#dName").val();
		
		if (!product.correctName(name))
		{
			texts.show("PRODUCT_INCORRECT");
			$("#info").css({"backgroundColor" : "#76DFFF"});
			$("#info").animate({"backgroundColor" : "#ffffff"}, 1000);

			return;
		}
		
		if (key = product.duplicate(name, tld))
		{
			cart.removeProduct(key);
			
			// texts.show("PRODUCT_DUPLICATE");
			// $("#info").css({"backgroundColor" : "#76DFFF"});
			// $("#info").animate({"backgroundColor" : "#ffffff"}, 1000);
			// return;
		}
				
		// do json request domain check
		$.getJSON(
				prodServiceUrl + "tld=" + escape(tld) + "&name=" + escape(name) + "&type=" + escape(product.selectedHosting()),
				function (data)
				{
					if (data.status == PSTATUS_OK)
					{
						cart.add(data);
					}
					
					if (data.status == PSTATUS_TAKEN)
					{
						if (confirm("Dit domein is reeds bezet, alleen de eigenaar mag dit domein verhuizen. Wilt u doorgaan?"))
							cart.add(data);
						else
						{
							texts.show("PRODUCT_CANCEL");
							product.clearForm();
						}
					}
					
					if (data.status == PSTATUS_ERROR)
						texts.show("PRODUCT_ERROR");
				}
			);
	},
	
	/** 
	 *	Clear the information in the form
	 */
	clearForm : function()
	{
		
	},
	
	/**
	 *	Determine whether an equal domain name is available in the cart
	 */
	duplicate : function(name, tld)
	{
		for (var i in cart.products)
		{
			if (cart.products[i].name == name &&
				cart.products[i].tld == tld)
			{
				return i;
			}
		}
		return false;
	}
	
};
