var addToCartButton     = "http://www.motasem.co.uk/js/cart.png";
var addToCartButtonText = "PayPal - The safer, easier way to pay online.";
var checkoutButton      = "http://www.motasem.co.uk/js/checkout.png";
var returnAddress       = "http://localhost" + window.location.pathname;
var contactAddress      = "sales@motasem.co.uk";
// var itemname = "Leucathea Dress";
// var price = "455";
// var sizes = array("6", "8", "10", "12");
// var colours = array("Blue", "Black");
function makeCart(){
	if (itemname == "" || itemname == undefined){
		return false; // exit if there is no item onsale in this page..
	}
	
	var sizeOptions = "";
	if (sizes != undefined){
		if (sizes.length > 0){
			sizeOptions = "<select id='sizes' name='sizes' class='cartselect'>";
			sizeOptions += "<option value='0'>Select size</option>";	
			for (var x = 0; x < sizes.length; x++){
				sizeOptions += "<option value='" + sizes[x] + "'>" + sizes[x] + "</option>";
			}
			sizeOptions += "</select>";
		}
	}
	
	var colourOptions = "";
	if (colours != undefined){
		if (colours.length > 0){
			colourOptions = "<select id='colours' name='colours' class='cartselect'>";
			colourOptions += "<option value='0'>Select colour</option>";
			for (var x = 0; x < colours.length; x++){
				colourOptions += "<option value='" + colours[x] + "'>" + colours[x] + "</option>";
			}
			colourOptions += "</select>";
		}
	}
	
	var cartHTML = "";
	cartHTML += "<style type='text/css'>\n";
	cartHTML += "	.cartselect{border: 1px solid #25314C; color:#25314C; background-color:#9E8F91;}\n";
	cartHTML += "	.cartselect option {color:#25314C; background-color: #9E8F91;}\n";
	cartHTML += "	.cartselect option alt {color:#FFFFFF; background-color: #9E8F91;}\n";	
	cartHTML += "</style>\n";
	cartHTML += "<table cellspacing='0' cellpadding='0' border='0'>\n";
	cartHTML += "	<tr>\n";
	cartHTML += "		<td colspan='2' class='style2 style6'>\n";
	cartHTML += "			Price: <b>&pound;" + price + "</b><br><br>\n";
	cartHTML += "				" + sizeOptions + "\n";
	cartHTML += "				" + colourOptions + "\n<br><br>";
	cartHTML += "		</td>\n";
	cartHTML += "	</tr>\n";
	cartHTML += "	<tr>\n";
	cartHTML += "		<td valign='bottom'>\n";
	cartHTML += "			<form target='paypal' id='shoppingCart' name='shoppingCart' action='https://www.paypal.com/cgi-bin/webscr' method='post'>\n";
	cartHTML += "				<a href='javascript:return false;'><img onclick='javascript:submitForm(\"shoppingCart\");return false;' src='" + addToCartButton + "' border='0' name='submit_button' alt='" + addToCartButtonText + "'></a>\n";
	cartHTML += "				<img alt='' border='0' src='https://www.paypal.com/en_GB/i/scr/pixel.gif' width='1' height='1'>\n";
	cartHTML += "				<input type='hidden' name='add' value='1'>\n";
	cartHTML += "				<input type='hidden' name='cmd' value='_cart'>\n";
	cartHTML += "				<input type='hidden' name='business' value=" + contactAddress + ">\n";
	cartHTML += "				<input type='hidden' name='item_name' value='" + itemname + "'>\n";
	cartHTML += "				<input type='hidden' name='item_number' value=''>\n";
	cartHTML += "				<input type='hidden' name='amount' value='" + price + "'>\n";
	cartHTML += "				<input type='hidden' name='no_shipping' value='0'>\n";
	cartHTML += "				<input type='hidden' name='no_note' value='1'>\n";
	cartHTML += "				<input type='hidden' name='currency_code' value='GBP'>\n";
	cartHTML += "				<input type='hidden' name='lc' value='GB'>\n";
	cartHTML += "				<input type='hidden' name='bn' value='PP-ShopCartBF'>\n";
	cartHTML += "				<input type='hidden' name='return' value='" + returnAddress + "'>\n";
	cartHTML += "				<input type='hidden' name='cancel_return' value='" + returnAddress + "'>\n";
	//cartHTML += "				<!-- <input type='hidden' name='cpp_header_image' value='http%3A%2F%2Fwww.motasem.co.uk%2Fv2%2Ftest%2Ftitle2.gif'> -->\n";
	cartHTML += "				<input type='hidden' name='cpp_headerback_color' value='9E8F91'>\n";
	cartHTML += "			</form>\n";
	cartHTML += "		</td>\n";
	cartHTML += "		<td valign='top' align='right'>\n";
	cartHTML += "			<form target='paypal' id='checkout' name='checkout' action='https://www.paypal.com/cgi-bin/webscr' method='post'>\n";
	cartHTML += "				<input type='hidden' name='cmd' value='_cart'>\n";
	cartHTML += "				<input type='hidden' name='business' value='" + contactAddress + "'>\n";
	cartHTML += "				<a href='javascript:return false;'><img onclick='javascript:submitForm(\"checkout\");return false;' src='" + checkoutButton + "' border='0' name='submit_button' alt='" + addToCartButtonText + "'></a>\n";
	cartHTML += "				<input type='hidden' name='display' value='1'>\n";
	cartHTML += "				<input type='hidden' name='notify_url' value='" + returnAddress + "'>\n";
	cartHTML += "				<input type='hidden' name='cpp_headerback_color' value='9E8F91'>\n";
	cartHTML += "			</form>\n";
	cartHTML += "		</td>\n";
	cartHTML += "	</tr>\n";
	cartHTML += "</table>\n";
	if (document.getElementById("cart") != undefined){
		document.getElementById("cart").innerHTML = cartHTML;
	}else{
		alert("Cannot create Paypal shopping cart - please contact the site administrator for assistance.\n[ERROR: NOWHERE_TO_PUT_CART]");
	}
	return;
}

function submitForm(formname){
	if (formname == "" || formname == undefined){
		alert("Cannot submit details to Paypal - please contact the site administrator for assistance.\n[ERROR: FORM_NOT_SPECIFIED]");
		return false;
	}else{
		var theform = document.getElementById(formname);
		if (formname == "checkout"){
			//alert("Proceeding to checkout");
			//return false;
			theform.submit();
		}else{
			if (theform == undefined){
				alert("Cannot submit details to Paypal - please contact the site administrator for assistance.\n[ERROR: FORM_NOT_FOUND]");
				return false;
			}else{
				var chosenSize = -1;
				if (sizes != undefined){
					if (sizes.length > 0){
						chosenSize = document.getElementById('sizes').options[document.getElementById('sizes').selectedIndex].value;
						if (chosenSize == 0){
							alert("Please choose the item size.");
							return false;
						}
					}
					
				}
				var chosenColour = -1;
				if (colours != undefined){
					if (colours.length > 0){
						chosenColour = document.getElementById('colours')[document.getElementById('colours').selectedIndex].value;
						if (chosenColour == 0){
							alert("Please choose the item colour.");
							return false;
						}
					}
				}
				
				var itemDetails = itemcode;
				if (chosenSize != -1){
					itemDetails += "Size " + chosenSize + ", ";
				}else{
					itemDetails += "One size, ";
				}
				if (chosenColour != -1){
					itemDetails += "Colour : " + chosenColour;
				}else{
					itemDetails += "Standard colour";
				}
				theform.item_number.value = itemDetails;
				theform.submit();
				return false;
			}
		}
	}
}
