// JE added [25/10/04]: cost (GBP) of shipping under £59				
var defaultDesc  = 'Foam Cushions';
var shippingCost = 9;		
// end

function SetOrderNo()
{
	d = new Date();
	var oDay = d.getDate();1.
	var oMonth = d.getMonth();
	var oYear = d.getFullYear();
	var oMinutes = d.getMinutes();
	var oHours = d.getHours();
	var oSeconds = d.getSeconds();
	var OrderNo = oDay + "-" + oMonth + "-" + oYear + "-" + oMinutes + "-" + oHours + "-" + oSeconds;
	document.check.trans_id.value = OrderNo;
}


function CheckFoam(name)
{
  if(name=="Standard Medium")
    return '0.009648';
  if(name=="V38")
    return '0.009648';
  if(name=="R400H")
    return '0.01584';
  if(name=="R400")
    return '0.01584';
  if(name=="Memory foam 60")
    //return '0.0432';
	return '0.0205958';
}

function Calc()
{
    var alertmessage = '';

	if(document.check.length.value == "")
	 alertmessage += '   - Please enter a length\n';
	if (document.check.length.value != "")
	{
		var fieldValue = document.check.length.value;
		var accept = "0123456789."
		for (var z = 0; z<fieldValue.length; z++) 
		{ 
 			var character = fieldValue.charAt(z)      
 			if (accept.indexOf(character) == -1) {
			alertmessage += '   - Invalid Characters in length\n';
			break; }
		} 
	}
	if(document.check.width.value == "")
	 alertmessage += '   - Please enter a width\n';
	if (document.check.width.value != "")
	{
		var fieldValue = document.check.width.value;
		var accept = "0123456789."
		for (var z = 0; z<fieldValue.length; z++) 
		{ 
 			var character = fieldValue.charAt(z)      
 			if (accept.indexOf(character) == -1) {
			alertmessage += '   - Invalid Characters in width\n';
			break; }
		} 
	}
	if(document.check.depth.value == "")
	 alertmessage += '   - Please enter a depth\n';
	if (document.check.depth.value != "")
	{
		var fieldValue = document.check.depth.value;
		var accept = "0123456789."
		for (var z = 0; z<fieldValue.length; z++) 
		{ 
 			var character = fieldValue.charAt(z)      
 			if (accept.indexOf(character) == -1) {
			alertmessage += '   - Invalid Characters in depth\n';
			break; }
		} 
	}
	 
	if(alertmessage!='')
	{
	  var topMessage = 'You have not completed all required information:\n';
	  var bottomMessage = 'Please try again when the information has been entered';
	  alert(topMessage+alertmessage+bottomMessage);
	  return "error";
	}
	else
	{
	if(document.check.dimension[1].checked == false)
	{
	  var dimension="cms";
	  document.check.M_dimension.value = "cm";
	}
	else
	{
	  var dimension="inches";
	  document.check.M_dimension.value = "Inches";
	}
	
	if(dimension=="inches")
	{
	 var length = document.check.length.value;
	 var width = document.check.width.value;
	 var depth = document.check.depth.value;
	 var foam = CheckFoam(document.check.foam.value);
	}
	else
	{
	 var length = document.check.length.value / 2.5;
	 var width = document.check.width.value / 2.5;
	 var depth = document.check.depth.value / 2.5;
	 var foam = CheckFoam(document.check.foam.value);
	}
	
	
	var area = length * width;
	var area2 = area * foam;
	var subTotal = area2 * depth;
	subTotal = subTotal.toFixed(2);
	
	document.check.subtotal.value = subTotal;
	return "done";
	}
}

function calcTotals()
{
  if(document.check.subtotal.value == "0.00")
    var alertmessage = Calc();
  else
    var alertmessage='done';
  
  if(alertmessage=='done')
  {
   var quantity = document.check.qty.value;
   
   if(quantity > 0){
    var total = document.check.subtotal.value;
    total = total*quantity;
    var sTotal = (1*quantity)+total;
    var dsTotal = (4*quantity)+total;
	
	total = total * 1.175;
	sTotal = sTotal * 1.175;
	dsTotal = dsTotal * 1.175;
	
	total = total.toFixed(2);
	sTotal = sTotal.toFixed(2);
    dsTotal = dsTotal.toFixed(2);
  
    document.check.foamTotal.value = total;
    document.check.sfoamTotal.value = sTotal;
    document.check.dsfoamTotal.value = dsTotal;
	document.check.M_length.value=document.check.length.value;
	document.check.M_width.value=document.check.width.value;
	document.check.M_depth.value=document.check.depth.value;
	document.check.M_foam.value=document.check.foam.value;
	document.check.M_qty.value=document.check.qty.value;
   }
   else
   {
     alert("Please enter a value for quantity that is greater than zero");
   }
  }
}

function Purchase(clicked)
{
	SetOrderNo();
	calcTotals();
	// Wrap description added to foam type KW 17 Oct 2007
	var wraptype = "";
	if(clicked=="foam")
	{
		document.check.amount.value=document.check.foamTotal.value;
	}
	if(clicked=="sfoam")
	{
		document.check.amount.value=document.check.sfoamTotal.value;
		wraptype = "_Stockinette";
	}
	if(clicked=="dsfoam")
	{
		document.check.amount.value=document.check.dsfoamTotal.value;
		wraptype = "_Dracon_Stockinette";
	}
	
	// added by JE:
	// 25th Oct 2004
	// add the £9 shipping cost if product is under £59.
	var amount_zero = false;
	if(document.check.amount.value == ""){
		amount_zero = true;
	}
	
	var prod_details = "prod=" + document.check.M_length.value + document.check.M_dimension.value + "x" + document.check.M_width.value + document.check.M_dimension.value + "x" + document.check.M_depth.value+ document.check.M_dimension.value + "x" + document.check.M_foam.value + wraptype + ",item_amount=" + (document.check.subtotal.value * 1.175).toFixed(2) + "x" + document.check.M_qty.value;
	var prods = "prod=" + document.check.M_length.value + document.check.M_dimension.value + "x" + document.check.M_width.value + document.check.M_dimension.value + "x" + document.check.M_depth.value + document.check.M_dimension.value + "x" + document.check.M_foam.value + wraptype;
	
	if(document.check.amount.value <= 59)
	{
		document.check.desc.value = defaultDesc + ' (PLUS £' + shippingCost + ' Shipping Costs)';
		document.check.amount.value = Number(document.check.amount.value) + shippingCost;
		var prod_details = prod_details + ";prod=SHIPPING,item_amount=" + shippingCost;
	}else{
		document.check.desc.value = defaultDesc;
	}

	//Foam Cushions
	
	document.check.order.value = prod_details;
	document.check.callback.value = "http://www.mattress-solutions.co.uk/cms/callback.php?a=y" + "&amp;" + prods + "&amp;qty=" + document.check.M_qty.value + ";http://www.mattress-solutions.co.uk/cms/callback.php?a=n" + "&amp;" + prods + "&amp;qty=" + document.check.M_qty.value;
	
	if(document.check.amount.value != "" && amount_zero == false){
		document.check.submit();
	}else{
		alert("You will need to input sizes before you can purchase your cushions");
	}
}

