// default expiration is a year from now
var default_expiry_days = 365;

function expiration(num_days)
{
	// set an expiration date a year in the future
	var expiry = new Date();
	expiry.setDate(expiry.getDate() + num_days);
	// this line was for minutes in the future.
	// expiry.setUTCMinutes(expiry.getUTCMinutes() + num_mins);
	return expiry.toGMTString();
}

function add_basket(product, qty, price, desc, reload)
{
	// NOTE: reload parameter is 1 if the page should reload with an alert,
	// otherwise, omit it.
	// if there is no price, it is zero
	if (!price)
	{
		price = 0;
	}
	// if there is no description, take it from prod num
	if (!desc)
	{
		desc = "Product "+product;
	}
	// add a cookie for this product and its quantity
	cookval = product+"="+desc+"|"+price+"|"+qty+";path=/;expires=" + expiration(default_expiry_days) ;
	document.cookie = cookval;
	// these two steps can't happen when we're doing an update.
	if (reload)
	{
		alert("This item has been added to your enquiry");
		location.reload();
	}
}

function del_basket(product, qty, reload)
{
	// NOTE: reload parameter is 1 if the page should reload,
	// otherwise, omit it.
	// change the product cookie to remove its expiry date and quantity
	// so it will expire when page is closed, and not show on basket
	var dt = new Date();
	dt.setFullYear(dt.getFullYear() - 1);
	//document.cookie = product+"=;expires=Fri, 02-Jan-1970 00:00:00 GMT";
	document.cookie = product+"=0;path=/;expires="+dt.toGMTString();
	if (reload)
	{
		location.reload();
	}
}

function cookieVal(cookieName)
{
	// currently not used.
	// get the value of this cookie
	thisCookie = document.cookie.split("; ");
	for (i=0; i<thisCookie.length; i++)
	{
		if (cookieName == thisCookie[i].split("=")[0])
		{
			return thisCookie[i].split("=")[1];
		}
	}
}

function round(number, decPlace)
{
	decPlace = (!decPlace ? 2 : decPlace);
	return Math.round(number * Math.pow(10, decPlace)) / Math.pow(10, decPlace);
}

function numberFormat(amount)
{
	var rawNumStr = round(amount) + '';
	rawNumStr = (rawNumStr.charAt(0) == '.' ? '0' + rawNumStr : rawNumStr);
	if (rawNumStr.charAt(rawNumStr.length - 3) == '.')
	{
		return rawNumStr
	}
	else if (rawNumStr.charAt(rawNumStr.length - 2) == '.')
	{
		return rawNumStr + '0';
	}
	else
	{
		return rawNumStr + '.00';
	}
}

function listCookie(make_email)
{
	// running this with a parameter of 1 will create the final page with the
	// link to the order email.  running it without a parameter will create
	// the preceding page with no link.
	var key;
	var value;
	var email = "";
	var total = 0;

	// list what's in the basket now

	document.write("<table width=100% cellspacing=2 cellpadding=2 border=0><form name=update>");
	// create an array of the cookies
	thisCookie = document.cookie.split("; ");
	// do nothing if there is no cookie
	if (document.cookie.length==0)
	{
		document.write("<tr><td colspan=4><font class=title>You have no items in your enquiry</font></td></tr></table>");
	}
	else
	{
		document.write("<tr><td class=bluecell><font class=title>Qty</font></td><td class=bluecell><font class=title>Description</font></td><td class=bluecell><font class=title>Part No.</font></td><td class=bluecell><font class=title>Price</font></td></tr>");
		// loop through cookie values
		for (i=0; i<thisCookie.length; i++)
		{
			// split out the key value pairs
			key = thisCookie[i].split("=")[0];
			value = thisCookie[i].split("=")[1];
			// value is pipe separated with desc|price|qty
			desc = value.split("|")[0];

			price = value.split("|")[1];
			qty = value.split("|")[2];

			// Add price to the total, leave off first dollar sign
			if (!isNaN(parseFloat(price.substr(1))))
			{
				if (!isNaN(qty))
				{
					total += parseFloat(price.substr(1)) * qty;
				}
			}

			// skip it if the quantity is undefined
			if (value)
			{
				document.write("<tr><td class=whitesmoke><input type=hidden name=key"+i+" value="+escape(key)+">");
				document.write("<input type=hidden name=desc"+i+" value="+escape(desc)+">");
				document.write("<input type=hidden name=price"+i+" value="+escape(price)+">");
				if (!make_email)
				{
					document.write("<input name=qty"+i + " size=4 value='" + qty + "'></td>");
				}
				else
				{
					document.write(qty + "</td>");
				}
				document.write("<td class=whitesmoke>" + desc + "</td>");
				document.write("<td class=whitesmoke>" + key + "</td>");
				document.write("<td class=whitesmoke align=right>" + price + "</td></tr>");

				email += key+" = "+desc+"\t"+price+"\t"+qty+"\n";
			}
		}
		// Put an estimated total row here
		document.write("<tr><td class=whitesmoke>&nbsp;</td><td class=whitesmoke>Estimated Total</td><td class=whitesmoke>&nbsp;</td><td align=right class=whitesmoke>€" + numberFormat(total) + "</td></tr>");
		// use this same information to create an email link
		// to send the order to us
		if (email != "")
		{

			// put an update order button here
			if (!make_email)
			{
				document.write("<tr><td bgcolor=white colspan=4><img src=/is_images/template/p.gif width=1 height=5 border=0></td></tr>");
				document.write("<tr><td bgcolor=white colspan=4><input type=button value='Update' onclick='update_basket(this.form);'>&nbsp;&nbsp;");
				// put a clear this basket button here
				document.write("<input type=button value='Clear' onclick='clear_basket(this.form);'></td></tr>");
				document.write("</form>");
				document.write("<tr><td bgcolor=white colspan=4><img src=/is_images/template/p.gif width=1 height=5 border=0></td></tr>");
			}
			document.write("</table>");

			// WRITE EMAIL
			// WE decided not to do this at all because it assumes a level of user we won't have
			if (0) //(make_email)
			{
				document.write("<table width=100% cellspacing=1 cellpadding=1 border=0>");
				document.write("<tr><td colspan=2 bgcolor=white><img src=/is_images/template/p.gif width=1 height=5 border=0></td></tr>");
				document.write("<tr><td colspan=2 valign=top bgcolor=whitesmoke><font class=redbold12px>Send this Enquiry</font></td></tr>");
				document.write("<tr><td colspan=2 valign=top><img src=/is_images/template/p2.gif width=100% height=1 border=0></td></tr>");
				document.write("<tr><td colspan=2 bgcolor=white><img src=/is_images/template/p.gif width=1 height=5 border=0></td></tr>");
				document.write("<tr><td colspan=2 bgcolor=white>Happy with your enquiry details? click the link below to launch your email program and send your enquiry to us.</td></tr>");
				document.write("<tr><td colspan=2 bgcolor=white><img src=/is_images/template/p.gif width=1 height=5 border=0></td></tr>");

				document.write("<tr><td width=16><img src=/is_images/template/fwd.gif width=16 border=0></td><td width=420>");

				document.write("<a href='mailto:nsysales@nonstopyacht.com?");
				document.write("subject="+escape('NonStopYacht Enquiry')+"&");
				document.write("body=");
				document.write(escape(email));
				document.write("'><font class=title>Email this Enquiry now!</font>");
				document.write("</a>");

				document.write("</td></tr>");
				document.write("</table>");
			}
		}
	}
}

function update_basket(f)
{
	// this is a real kludge-- we have to count the number of
	// input items in the form to find the number of quantity
	// boxes.  Can't think of another way.
	// IF YOU ADD ANY FORM ELEMENTS TO THIS FORM, RECALCULATE THIS!
	for (i=0; i<(f.length-2)/4; i++)
	{
		if (f['qty'+i].value == 0)
		{
			del_basket(unescape(f['key'+i].value), 0);
		}
		else
		{
			add_basket(unescape(f['key'+i].value), f['qty'+i].value, unescape(f['price'+i].value), unescape(f['desc'+i].value));
		}
	}
	location.reload();
}

function clear_basket(f)
{
	// code here to set all values to zero and call update basket
	// SAME WARNING: IF YOU ADD ELEMENTS TO THE FORM, RECALCULATE THE FOR LOOP!
	for (i=0; i<(f.length-2)/4; i++)
	{
			del_basket(unescape(f['key'+i].value), 0);
	}
	location.reload();
}

function count_basket()
{
	// show how many items are currently in the basket
	thisCookie = document.cookie.split("; ");
	// do nothing if there is no cookie
	if (document.cookie.length>0)
	{
		return(thisCookie.length);
	}
	else
	{
		return(0);
	}
}
