
// For showing Added to cart message in a floating window instead of an alert
var add2cartMsg = 1;

/* ------------------------------------------------------------------------------------------------ */
/* all4Carp_onSelectSize																			*/
/* ------------------------------------------------------------------------------------------------ */
function all4Carp_onSelectSize (index)
{
	switch (index)
	{
		case 0	: // height
			all4Carp_loadSizesBy (1);
			all4Carp_loadSizesBy (2);
			break;

		case 1	: // width
			all4Carp_loadSizesBy (0);
			all4Carp_loadSizesBy (2)
			break;

		case 2	: // dept
			all4Carp_loadSizesBy (0);
			all4Carp_loadSizesBy (1);
			break;

		case 3	: // only height
			break;
	}

	if (index != 3)
	{
		// clean custom size fields
		document.getElementById("customSize").value 			= "";
		document.getElementById("customSizeHeight").value 		= "";
		document.getElementById("customSizeWidth").value 		= "";
		document.getElementById("customSizeDept").value 		= "";
		document.getElementById("customSizePriceValue").value 	= "";
	}
	else
	{
		document.getElementById("productPrice").value 			= "";
	}


	all4Carp_setProductSize (true);
}


/* ------------------------------------------------------------------------------------------------ */
/* all4Carp_loadSizesBy																				*/
/* ------------------------------------------------------------------------------------------------ */
function all4Carp_loadSizesBy (index)
{
	var fieldName1, fieldName2, index1, index2;

	switch (index)
	{
		case 0	: // height
			fieldName1		= "sizeWidth";
			index1			= 1;
			fieldName2		= "sizeDept";
			index2			= 2;
			selectName		= "sizeHeight";
			break;

		case 1	: // width
			fieldName1		= "sizeHeight";
			index1			= 0;
			fieldName2		= "sizeDept";
			index2			= 2;
			selectName		= "sizeWidth";
			break;

		case 2	: // dept
			fieldName1		= "sizeHeight";
			index1			= 0;
			fieldName2		= "sizeWidth";
			index2			= 1;
			selectName		= "sizeDept";
			break;
	}

	if (document.getElementById(selectName + "_spn") == undefined) return;

	var fieldValue1 = document.getElementById(fieldName1).value;
	var fieldValue2 = document.getElementById(fieldName2).value;
	var selectValue	= document.getElementById(selectName).value;

	var options = new Array;

	for (var i = 0; i < sizesArray.length; i++)
	{
		if ((fieldValue1 == 0 || sizesArray[i][index1] == fieldValue1) &&
			(fieldValue2 == 0 || sizesArray[i][index2] == fieldValue2))
		{
			if (options.count == 0 || ("#"+options.join("#")+"#").indexOf("#"+sizesArray[i][index]+"#") == -1)
				options.push (sizesArray[i][index]);
		}
	}

	options = options.sort ();

	var selectHtml = "<select id='" + selectName + "' onchange='all4Carp_onSelectSize(" + index + ")'>" + 
						"<option value='0'>" + selectName.substring(4) + "</option>";

	for (var i = 0; i < options.length; i++)
	{
		selectHtml += "<option value='" + options[i] + "'";
		
		if (selectValue == options[i])
			selectHtml += " selected='true'";
		
		if ((i % 2) == 1)
			optionClass = "unevenRow";
		else
			optionClass = "evenRow";

		selectHtml += " class='" + optionClass + "'>" + options[i] + "</option>";
	}

	selectHtml += "</select>";

	document.getElementById(selectName + "_spn").innerHTML = selectHtml;
}

/* ------------------------------------------------------------------------------------------------ */
/* all4Carp_loadAllSizeSelection																	*/
/* ------------------------------------------------------------------------------------------------ */
function all4Carp_loadAllSizeSelection ()
{
	for (var index = 0; index < 3; index++)
	{
		switch (index)
		{
			case 0	: // height
				selectName		= "sizeHeight";
				break;

			case 1	: // width
				selectName		= "sizeWidth";
				break;

			case 2	: // dept
				selectName		= "sizeDept";
				break;
		}

		if (document.getElementById(selectName + "_spn") == undefined) continue;

		var options = new Array;

		for (var i = 0; i < sizesArray.length; i++)
		{
			options.push (sizesArray[i][index]);
		}

		options = options.sort ();

		var selectHtml = "<select id='" + selectName + "' onchange='all4Carp_onSelectSize(" + index + ")'>" + 
							"<option value='0' selected='true'>" + selectName.substring(4) + "</option>";

		for (var i = 0; i < options.length; i++)
		{
			selectHtml += "<option value='" + options[i] + "'";
		
			if ((i % 2) == 1)
				optionClass = "unevenRow";
			else
				optionClass = "evenRow";

			selectHtml += " class='" + optionClass + "'>" + options[i] + "</option>";
		}

		selectHtml += "</select>";

		document.getElementById(selectName + "_spn").innerHTML = selectHtml;
	}
}

/* ------------------------------------------------------------------------------------------------ */
/* all4Carp_setProductSize        										                            */
/* ------------------------------------------------------------------------------------------------ */
function all4Carp_setProductSize ()
{
	var productId = document.getElementById("theProductId").value;

	var width 	= 0;
	var dept	= 0;

	if (document.getElementById("sizeWidth") != undefined)
		width	= document.getElementById("sizeWidth").value;

	var height	= document.getElementById("sizeHeight").value;

	if (document.getElementById("sizeDept") != undefined)
		dept	= document.getElementById("sizeDept").value;
		
	var withDept = false;
	if (document.getElementById("sizeDept_spn") != undefined && document.getElementById("sizeDept_spn").style.display == "")
		withDept = true;

	var onlyHeight = false;
	if (document.getElementById("sizeOnlyHeight_spn") != undefined)
		onlyHeight = true;

	if ((width != 0 && height != 0 && (!withDept || (withDept && dept != 0))) || (onlyHeight && height != 0))
	{
		if (!onlyHeight)
		{
			var customSizeValue = height + " / " + width;

			if (withDept)
				customSizeValue += " / " + dept;

			document.getElementById("customSize").value = customSizeValue;
		}

		xml = "<data>" +
				"<command>private.getPriceBySize</command>"			+
				"<productId>"	+ productId  	+ "</productId>" 	+
				"<height>"		+ height		+ "</height>"		+
				"<width>"		+ width			+ "</width>"		+
				"<dept>"		+ dept			+ "</dept>"			+
		  	  "</data>";

		xmlRequest.init (xml);
		xmlRequest.sendAsyncRequest ("server.php", xmlRequest.obj, "all4Carp_setProductSize_response");
		return false;		
	}
}

/* ------------------------------------------------------------------------------------------------ */
/* all4Carp_setProductSize_response     							                               	*/
/* ------------------------------------------------------------------------------------------------ */
function all4Carp_setProductSize_response ()
{
	xmlRequest.init(commonDecode(asyncHttpObj.responseText));
						
	try
	{
		var theSizePrice   = xmlRequest.getValue("sizePrice");
		var theSizePriceId = xmlRequest.getValue("sizePriceId");
	}
	catch (e)
	{
		alert ("AJAX Error");
		return false;
	}

	document.getElementById("sizePriceValue").value	= theSizePrice;
	document.getElementById("sizePriceId").value 	= theSizePriceId;

	all4Carp_updatePrice ();
}

/* ------------------------------------------------------------------------------------------------ */
/* all4Carp_setProductColor      									                             	*/
/* ------------------------------------------------------------------------------------------------ */
function all4Carp_setProductColor (colorId, colorPrice, colorFunction, productId)
{
	colorPrice = colorPrice * 1;	

	// change color small pic
	var colorImg 	= "<img style='visibility: visible;' src='productVarFiles/" + colorId + "_size0.jpg' />";
	document.getElementById("customColor").innerHTML 		= colorImg;

	// change color big pic
	var bigPicColor = document.getElementById("productColorBigPic_" + colorId).innerHTML;
	var colorImgBig = "<a href='productVarFiles/" + bigPicColor + "' style='cursor:pointer;position:relative;top:22px;left:3px;' " + 
					  "   onclick='return hs.expand(this)'><img src='loadedFiles/viewPic.png' style='cursor: pointer;'/></a>";
	document.getElementById("customColorViewer").innerHTML 	= colorImgBig;

	// save color id & price
	document.getElementById("colorPriceId").value 	 = colorId;
	document.getElementById("colorPriceValue").value = colorPrice;
	document.getElementById("colorPriceFunction").value = colorFunction;

	// handle product color pic
	if (productId != undefined)
	{
		// set product color variation
		document.getElementById("currProductPic").src    = "productVarFiles/" + colorId + "_" + productId + "_size0.jpg";	// size0
	}
	else
	{
		// return original product pic
		var oProductPic = document.getElementById("productPic").src;
		if (oProductPic != "")
			document.getElementById("currProductPic").src = oProductPic;
	}

	all4Carp_updatePrice ();
}

/* ------------------------------------------------------------------------------------------------ */
/* all4Carp_updatePrice																				*/
/* ------------------------------------------------------------------------------------------------ */
function all4Carp_updatePrice ()
{
	var sizePrice 	= document.getElementById("sizePriceValue").value;

	if (sizePrice == "")
		sizePrice 	= document.getElementById("customSizePriceValue").value;

	var colorPrice			= document.getElementById("colorPriceValue").value;
	var colorFunction		= document.getElementById("colorPriceFunction").value;

	var theProductPrice = document.getElementById("productPriceValue").value;

	if (sizePrice == "" && theProductPrice != 0)
		sizePrice = theProductPrice;	// this is the base price

	if (sizePrice == "" && colorPrice != 0)
		sizePrice = "0";				// this is a product with colors only

	if (sizePrice != "")
	{
		// set total price as size price
		var totalPrice = sizePrice * 1;

		// add color price
		if (colorPrice != "")
		{
			colorPrice = colorPrice * 1;

			if (colorFunction == "plusPercent")
			{
				totalPrice = Math.round((totalPrice * 100) + (totalPrice * colorPrice));
				totalPrice = totalPrice / 100;
			}
			else if (colorFunction == "minusPercent")
			{
				totalPrice = Math.round((totalPrice * 100) - (totalPrice * colorPrice));
				totalPrice = totalPrice / 100;
			}
			else if (colorFunction == "plusUnit")
			{
				totalPrice = Math.round((totalPrice * 100) + (colorPrice * 100));
				totalPrice = totalPrice / 100;
			}
			else if (colorFunction == "minusUnit")
			{
				totalPrice = Math.round((totalPrice * 100) - (colorPrice * 100));
				totalPrice = totalPrice / 100;
			}
		}

		// calc price by quantity
		var pQuantity = document.getElementById("customQuantity").value * 1;
		if (pQuantity > 0)
		{
			totalPrice = Math.round(totalPrice * 100 * pQuantity);
			totalPrice = totalPrice / 100
		}
			
		document.getElementById("productPrice").innerHTML = "&#163;" + totalPrice;
		document.getElementById("totalPriceValue").value  = totalPrice;
	}
	else
	{
		// this a product with no variations

		if (theProductPrice != "")
		{
			var pQuantity = document.getElementById("customQuantity").value * 1;
			if (pQuantity > 0)
			{
				totalPrice = Math.round(theProductPrice * 100 * pQuantity);
				totalPrice = totalPrice / 100
			}
			
			document.getElementById("productPrice").innerHTML = "&#163;" + totalPrice;
			document.getElementById("totalPriceValue").value  = totalPrice;
		}
	}


	
}

/* ------------------------------------------------------------------------------------------------ */
/* all4Carp_openPicWindow														                 	*/
/* ------------------------------------------------------------------------------------------------ */
function all4Carp_openPicWindow()
{
	if (document.getElementById("productPic2Size0").src.indexOf("shopFiles") != -1)
		window.open(document.getElementById("productPic2Size0").src);
	else
		window.open(document.getElementById("productPic1Size0").src);

/*	var picToView = document.getElementById("currProductPic");
	var picToOpen = "";

	if (picToView.src == document.getElementById("productPic").src)	
	{
		if (document.getElementById("productPic2Size0").src.indexOf("shopFiles") != -1)
			window.open(document.getElementById("productPic2Size0").src);
		else
			window.open(document.getElementById("productPic1Size0").src);
	}
	else
	{
		var colorId = document.getElementById("colorPriceId").value;
		window.open( "productVarFiles/" + document.getElementById("productVarPicName_" + colorId).innerHTML);
	}
*/
}

/* ------------------------------------------------------------------------------------------------ */
/* all4Carp_setProductQuantity                                    									*/
/* ------------------------------------------------------------------------------------------------ */
function all4Carp_setProductQuantity (oSelect)
{
	var pQuantity = oSelect.value;

	if (pQuantity > 0)
	{
		all4Carp_updatePrice ();
	}
}

/* ------------------------------------------------------------------------------------------------ */
/* all4Carp_onTypeCustomSize																		*/
/* ------------------------------------------------------------------------------------------------ */
function all4Carp_onTypeCustomSize ()
{
	var productId = document.getElementById("theProductId").value;

	var width	= document.getElementById("customSizeWidth").value;
	var height	= document.getElementById("customSizeHeight").value;
	var dept	= document.getElementById("customSizeDept").value;

	var withDept = false;
	if (document.getElementById("sizeDept_spn").style.display == "")
		withDept = true;

	var charpos = width.search("[^0-9]"); 
    if (width.length > 0 &&  charpos >= 0) 
    { 
    	alert ("Width: Only digits allowed.");
		document.getElementById("customSizeWidth").value = "";
        return false; 
    }

	var charpos = height.search("[^0-9]"); 
    if (height.length > 0 &&  charpos >= 0) 
    { 
    	alert ("Height: Only digits allowed.");
		document.getElementById("customSizeHeight").value = "";
        return false; 
    }

	if (withDept)
	{
		var charpos = dept.search("[^0-9]"); 
	    if (dept.length > 0 &&  charpos >= 0) 
    	{ 
	    	alert ("Dept: Only digits allowed.");
			document.getElementById("customSizeDept").value = "";
        	return false; 
	    }
	}

	if (width != "" && height != "" && (!withDept || (withDept && dept != 0)))
	{
		xml = "<data>" +
				"<command>private.getPriceByCustomSize</command>"	+
				"<productId>"	+ productId  	+ "</productId>" 	+
				"<height>"		+ height		+ "</height>"		+
				"<width>"		+ width			+ "</width>"		+
				"<dept>"		+ dept			+ "</dept>"			+
		  	  "</data>";

		xmlRequest.init (xml);
		xmlRequest.sendAsyncRequest ("server.php", xmlRequest.obj, "all4Carp_setCustomSizePrice");
		return false;
	}
}

/* ------------------------------------------------------------------------------------------------ */
/* all4Carp_setCustomSizePrice (all4Carp_onTypeCustomSize response							        */
/* ------------------------------------------------------------------------------------------------ */
function all4Carp_setCustomSizePrice()
{
	var width	= document.getElementById("customSizeWidth").value;
	var height	= document.getElementById("customSizeHeight").value;
	var dept	= document.getElementById("customSizeDept").value;

	var withDept = false;
	if (document.getElementById("sizeDept_spn").style.display == "")
		withDept = true;

	xmlRequest.init(commonDecode(asyncHttpObj.responseText));
						
	try
	{
		var theSizePrice = xmlRequest.getValue("sizePrice");
	}
	catch (e)
	{
		alert ("AJAX Error");
		return false;
	}

	if (theSizePrice == "-1")	// this is standard combination
	{
		if (document.getElementById("sizeHeight") != undefined)
		{
			all4Carp_loadAllSizeSelection ();
	
			document.getElementById("sizeHeight").value	= height;
			document.getElementById("sizeWidth").value	= width;
			document.getElementById("sizeDept").value	= dept;

			all4Carp_loadSizesBy  (0);
			all4Carp_onSelectSize (0)

			alert("This is a standard size combination");
		}

		return false;
	}
	else
	{
		var customSizeValue = height + " / " + width

		if (withDept)
			customSizeValue += " / " + dept;

		document.getElementById("customSize").value  		  = customSizeValue;
		document.getElementById("customSizePriceValue").value =	theSizePrice;

		document.getElementById("sizePriceId").value 		  = "";
		document.getElementById("sizePriceValue").value		  = "";

		if (document.getElementById("sizeHeight") != undefined)
		{
			document.getElementById("sizeHeight").value			  = 0;
			document.getElementById("sizeWidth").value			  = 0;
			document.getElementById("sizeDept").value			  = 0;
		}

		all4Carp_updatePrice ();

		return false;
	}
}

/* ------------------------------------------------------------------------------------------------ */
/* all4Carp_addProductToCart																		*/
/* ------------------------------------------------------------------------------------------------ */
function all4Carp_addProductToCart ()
{
	var productId = document.getElementById("theProductId").value;

	var customQuantity = document.getElementById("customQuantity").value;
	if (customQuantity == 0)
	{
		alert("Please choose a quantity");
		return;
	}

	var customSizeId 	= document.getElementById("sizePriceId").value;
	var customSizePrice = document.getElementById("customSizePriceValue").value;
	var customSize 		= document.getElementById("customSize").value;
	var theProductPrice	= document.getElementById("productPriceValue").value;
	
	if (document.getElementById("customSizeHeight") != undefined)	// check if we can choose size
	{
		if (customSize == "" && customSizeId == "" && customSizePrice == "" && theProductPrice == "")
		{
			alert("Please choose a size.");
			return;
		}
	}

	if (document.getElementById("sizeOnlyHeight_spn") != undefined)
	{
		if (document.getElementById("productPrice").innerHTML == "")
		{
			alert("Please choose a " + document.getElementById("productChooseText").innerHTML + ".");
			return;
		}
	}

	if (document.getElementById("productColorsTbl") != undefined)
	{
		var customColorId = document.getElementById("colorPriceId").value;
		if (customColorId == "")
		{
			alert("Please choose a color.");
			return;
		}
	}

	if (customSize != "" && customSizePrice != "" && customSizeId == "")
	{
		all4Carp_addProductCustomSizes ();
	}
	else
	{		
		shop_addProduct (productId, customQuantity, customColorId, customSizeId);
	}
}

/* ------------------------------------------------------------------------------------------------ */
/* all4Carp_addProductCustomSizes																	*/
/* ------------------------------------------------------------------------------------------------ */
function all4Carp_addProductCustomSizes ()
{
	var productId 		= document.getElementById("theProductId").value;

	var customSizePrice = document.getElementById("customSizePriceValue").value;
	var width			= document.getElementById("customSizeWidth").value;
	var height			= document.getElementById("customSizeHeight").value;
	var dept			= document.getElementById("customSizeDept").value;

	xml = "<data>" +
				"<command>private.addPrivateSizeVariations</command>"	+
				"<productId>"	+ productId  		+ "</productId>" 	+
				"<height>"		+ height			+ "</height>"		+
				"<width>"		+ width				+ "</width>"		+
				"<dept>"		+ dept				+ "</dept>"			+
				"<price>"	 	+ customSizePrice  	+ "</price>" +
	  	  "</data>";

	xmlRequest.init (xml);
	xmlRequest.sendAsyncRequest ("server.php", xmlRequest.obj, "all4Carp_addProductCustomSizes_response");
	return false;
}

/* ------------------------------------------------------------------------------------------------ */
/* all4Carp_addProductCustomSizes_response															*/
/* ------------------------------------------------------------------------------------------------ */
function all4Carp_addProductCustomSizes_response()
{
	xmlRequest.init(commonDecode(asyncHttpObj.responseText));
						
	try
	{
		var sizeVariationId = xmlRequest.getValue("variationId");
	}
	catch (e)
	{
		alert ("AJAX Error");
		return false;
	}	

	var productId 	   = document.getElementById("theProductId").value;
	var customQuantity = document.getElementById("customQuantity").value;
	var customColor    = document.getElementById("colorPriceId").value;

	shop_addProduct (productId, customQuantity, customColor, sizeVariationId);
}

/* ------------------------------------------------------------------------------------------------ */
/* all4Carp_submitLoginForm																			*/
/* ------------------------------------------------------------------------------------------------ */
function all4Carp_submitLoginForm ()
{
	var formValidator = new Validator('clubLoginForm');

	formValidator.clearAllValidations ();

	formValidator.addValidation('username',		'required',		'Please enter your email');
	formValidator.addValidation('username',		'required',		'Please enter a valid email');
	formValidator.addValidation('password',		'required',		'Please enter password');

	return formValidator.validate ();
}

/* ------------------------------------------------------------------------------------------------ */
/* all4Carp_submitForgotPasswordForm																*/
/* ------------------------------------------------------------------------------------------------ */
function all4Carp_submitForgotPasswordForm ()
{
	var formValidator = new Validator('clubLoginForm');

	formValidator.clearAllValidations ();

	formValidator.addValidation('username',		'required',		'Please enter your email');
	formValidator.addValidation('username',		'required',		'Please enter a valid email');

	if (formValidator.validate ())
	{
		document.getElementById("forgotPasswordForm").email.value = document.getElementById("clubLoginForm").username.value;
		document.getElementById("forgotPasswordForm").submit ();
		return true;
	}
	return false;
}

function all4Carp_changeShipmentOption ()
{
	var shipmentSelect = document.getElementById("shipment");
	var shipmentSelectId = shipmentSelect.options[shipmentSelect.selectedIndex].id;
	document.getElementById("MC_ShipmentId").value = shipmentSelectId;
	document.getElementById("MC_ShipmentPrice").value = document.getElementById('shipmentPrice_spn').innerHTML;
}
