//This happens after validateOrder() succeeds
function validateCoupon( )
{
	//find coupon value
	var isValid = false;
	var coupon = '';
	// COUPON MAX LENGTH IS 8 CHARACTERS
	var validCoupons = new Array('LUCKYDAY','HONOR',); //PUT THE COUPONS IN UPPER CASE HERE SEPARATED BY COMMAS
	var discountPercent = new Array('5','5',);    //PUT THE MATCHING COUPON DISCOUNT PERCENTAGE HERE SEPARATED BY COMMAS
    if (document.formPay.coupon.value != null)
	{
		coupon = document.formPay.coupon.value;
		coupon = coupon.toUpperCase();
	}
	else return true;
	
	var index;
	for (index=0; index < validCoupons.length; index++)
	{
		if (coupon == validCoupons[index])
		{
			isValid = true;
			break;
		}
	}
	if (isValid)
	{
		var price = parseFloat(document.formPay.amount.value);
		price = price - (price * ((discountPercent[index] - 0) / 100));
		var pstr = new String(price);
		var lastChar = pstr.indexOf('.');
		if (lastChar == -1)
		{
			pstr += '.00';
		}
		else if (lastChar + 3 > pstr.length)
		{
			pstr += '0';
		}
		else if (pstr.length > lastChar + 3)
		{
			pstr = pstr.substring(0, lastChar + 3);
		}
		document.formPay.amount.value = pstr;
		document.formPay.on0.value = 'Coupon';
		document.formPay.os0.value = validCoupons[index];
	}
	return true;
}

function updatePrice( selectElem )
{
	var selIndex = selectElem.selectedIndex;
	if (selIndex > 0)
	{
		var itemNum = document.formPay.item_number.value.substr(0,4);
		var inches = selectElem.options[selIndex].text.substr(0,2);
		var itemName = document.formPay.const_item_name.value;
		if (itemName.indexOf(' (') > 0)
		{
			itemName = itemName.substring(0,itemName.indexOf(' ('));
		}
		document.formPay.const_amount.value = selectElem.value;
		document.formPay.item_number.value = itemNum + '-' + inches;
		document.formPay.const_item_name.value = itemName + ' (' + selectElem.options[selIndex].text + ')';
	}
}

function validateOrder()
{
	try
	{
		if (document.formPay.neckLength.value == '')
		{
			alert('Please select a necklace length.');
			return false;
		}
	}
	catch (err)
	{
		// nothing to do 
	}
	var quantity = document.formPay.quantity.value - 0;
	if (quantity == null || isNaN(quantity) || quantity < 1)
	{
		alert('Please choose the quantity that you would like to purchase.');
		return false;
	}
	//need to reset the initial fields every time
	document.formPay.amount.value = document.formPay.const_amount.value;
	document.formPay.item_name.value = document.formPay.const_item_name.value;
	document.formPay.on0.value = '';
	document.formPay.os0.value = '';
	return true;
}
