
// define toFixed for mac browsers
Number.prototype.toFixed = function(places)
	{
	var temp = ( Math.round( this * Math.pow(10,places) ) ).toString();
	return temp.substring(0,temp.length-places) + "." + temp.substring(temp.length-places, temp.length);
	}

function intandenter(e)
	{
	var key;
	if(window.event) key = window.event.keyCode;     //IE
	else key = e.which;     //firefox & netscape
	if (key == 13) return false;	// return
	if (key > 31 && (key < 48 || key > 57)) return false;
	}

function settotal(quantity, price, totalvar)
	{
	var float_q;
	var float_p = parseFloat(price);
	var total;

	if (isNaN(quantity)) 
		{ float_q = parseFloat(quantity.value); }
	else
		{ float_q = 0.00; }
	if (isNaN(float_q))
		{ total = ''; }
	else
		{ total = (float_q*float_p); }
	if (total == '') { totalvar.value = ''; }
	else { totalvar.value = total.toFixed(2); }
	}

function calculate(formObj)
	{
	// step through form data and add anything that starts with 'total' to gt_temp
	var gt_temp = 0.00;

	for (var i=0; i<formObj.elements.length; i++)
		{
		if ((formObj.elements[i].name.indexOf('total') > -1))
			{ 
			if (formObj.elements[i].value > 0)
				{
				gt_temp += parseFloat(formObj.elements[i].value);
				}
			}
		}
	formObj.GrandTotal.value = gt_temp.toFixed(2);
	return true;
	}

function checkForm(formobj)
	{
	// if GrandTotal is > 0

	var fieldRequired = Array("Name","email","Phone","DeliveryTime","DeliveryDate");
	var fieldDescription = Array("Name","email","Phone","Delivery or Pick Up Time","Delivery or Pick Up Date");
	var alertMsg = "Please check the following fields:\n"; // 35 CHARS
	
	for (var i = 0; i < fieldRequired.length; i++)
		{
		var obj = formobj.elements[fieldRequired[i]];
		if (obj)
			{
			if (Trim(obj.value) == "" || obj.value == null)
				{ alertMsg += " - " + fieldDescription[i] + "\n"; }
			}
		}

	if (alertMsg.length > 35)
		{
		alert(alertMsg);
		return false;
		}

	if (formobj.GrandTotal.value < 0.01)
		{
		alert('You need to have made at least one purchase');
		return false;
		}

	return true;
	}

function Trim(TRIM_VALUE)
	{
	if(TRIM_VALUE.length < 1)	{ return""; }
	TRIM_VALUE = RTrim(TRIM_VALUE);
	TRIM_VALUE = LTrim(TRIM_VALUE);
	if(TRIM_VALUE=="")
		{ return ""; }
	else
		{ return TRIM_VALUE; }
	} 

function RTrim(VALUE)
	{
	var w_space = String.fromCharCode(32);
	var v_length = VALUE.length;
	var strTemp = "";
	if(v_length < 0) { return ""; }
	var iTemp = v_length -1;

	while(iTemp > -1)
		{
		if(VALUE.charAt(iTemp) != w_space)
			{
			strTemp = VALUE.substring(0,iTemp +1);
			break;
			}
		iTemp = iTemp-1;
		} 
	return strTemp;
	}

function LTrim(VALUE)
	{
	var w_space = String.fromCharCode(32);
	if(v_length < 1) { return ""; }
	var v_length = VALUE.length;
	var strTemp = "";
	var iTemp = 0;

	while(iTemp < v_length)
		{
		if(VALUE.charAt(iTemp) != w_space)
			{
			strTemp = VALUE.substring(iTemp,v_length);
			break;
			}
		iTemp = iTemp + 1;
		} 
	return strTemp;
	}
