var on  = 1;
var off = 0;

var coupon_state = "OFF";			// discount coupon state: OFF= no discount; DOLLAR=discount is in dollars; PERCENT=discount is in percent
var cdis = 0;						// amount of coupon discount (%)
var discount_percent = 20			// coupon discount (%)
var discount_dollar = 3				// coupon discount ($)
var group_discount_percent = 30		// group discount (%)
var group_discount_dollar = 5		// group discount ($)
var group_size = 10					// number of tickets qualifying as a group
var coupons = new Array ();			// array of valid coupon names
coupons[0] = "PPI-Dave";
coupons[1] = "PPI-Jack";
coupons[2] = "PPI-Geri";
coupons[3] = "PPI-Ken";
coupons[4] = "PPI-Aundrea";
coupons[5] = "PPI-Stacy";
coupons[6] = "LNR-Cast";
coupons[7] = "LNR-Crew";
coupons[8] = "LNR-Sabre";
coupons[9] = "LNR-Senior";
coupons[10] = "LNR-Seniors";
coupons[11] = "LNR-PSA";

/************
* Clear All *
************/
function ClearAll (oForm)
{
	an = 0;																	// number of discount brkpts
	hn = 0;																	// number of handling brkpts
	qd = 0;																	// number of qty breakpoints
	sn = 0;																	// number of shipping brkpts
	stxt = "";																// shipping type text
	taxp = 0;																// tax percent for this order
	cdis = 0;																// amount of coupon discount (%)
	root.xx_ship = off;														// default is no shipping

	amt = 0
	oForm.tixtot.value = ""
	oForm.disctot.value = ""
	oForm.feetot.value = ""
	coupon_state = "OFF";
	if (oForm.default_amount) oForm.amount.value = oForm.default_amount.value
	if (oForm.default_amount) oForm.tot.value = Dollar(oForm.default_amount.value)
	if (oForm.default_quantity) oForm.quantity.value = oForm.default_quantity.value
	if (oForm.default_quantity) oForm.tix_count.value = oForm.default_quantity.value
	if (oForm.default_item_name) oForm.item_name.value = oForm.default_item_name.value
	if (oForm.default_item_number) oForm.item_number.value = oForm.default_item_number
	if (oForm.default_coupon_code) oForm.cpon.value = oForm.default_coupon_code
	if (oForm.default_on0) on0 = oForm.default_on0.value
	if (oForm.default_os0) os0 = oForm.default_os0.value
	if (oForm.default_on1) on1 = oForm.default_on1.value
	if (oForm.default_os1) os1 = oForm.default_os1.value
}

/******************************
* check for a discount coupon *
******************************/
function ChkCoup (oForm, val, amt)
{
	var i;

	if (val == "" || val == "<Enter Code>") return;
	if (amt == "" || isNaN (amt))return;
	
	if (coupon_state.toUpperCase() == "OFF")
	{
		alert("NOTE: Discounts are not available for this performance.");
		cdis = 0;														// remember the discount amt
		oForm.cpon.value = "<Enter Code>";
		return;
	}

	if (val =="GroupDiscountFlag")
	{
		cdis = amt;														// remember the discount amt
   		oForm.cpon.value = "<GROUP>";
		return;
	}

	cdis = 0;																// assume the worst
	for (i = 0; i < coupons.length; i++)
	{
		if (val.toUpperCase() == coupons[i].toUpperCase())					// ignore case
		{
			oForm.cpon.value = coupons[i];									// show actual coupon code
			cdis = amt;														// remember the discount amt
			return;
		}
	}
	alert ("'" + val + "' not a valid coupon code!");
	oForm.cpon.value = "<Enter Code>";
}

/**********************************
* Set the coupon processing state *
**********************************/
function SetCouponState(oForm, val)
{
	coupon_state = val;
}

/*******************************
* force to valid dollar amount *
*******************************/
function Dollar (val)
{
	var str,pos,rnd=0;
	if (val == 0) return "0.00"
	if (val < 1) rnd = 1;
	str = escape (val * 1.0 + 0.005001 + rnd);								// float, round, escape
	pos = str.indexOf (".");
	if (pos > 0) str = str.substring (rnd, pos + 3);
	return str;
}

/*****************************
* process radio and checkbox *
*****************************/
function ReadForm (oForm)
{
	var i, j, obj, pos, val, tok, tag;
	var amt = 0, qty = 0, disc=0, tixtot=0, disctot=0, feetot=0;
	var iname = "", inmr = "";
	var on0 = "", os0 = "", on1 = "", os1 = "";
	var ary = new Array ();
	
	if (oForm.tix_count.value >= group_size)
	{
    	switch (coupon_state.toUpperCase())
    	{
    	case "DOLLAR":
			ChkCoup (oForm, "GroupDiscountFlag", group_discount_dollar);
    		break;
    	case "PERCENT":
			ChkCoup (oForm, "GroupDiscountFlag", group_discount_percent);
    		break;
    	}
	}
	else
	{
    	switch (coupon_state.toUpperCase())
    	{
    	case "DOLLAR":
			ChkCoup (oForm, oForm.cpon.value, discount_dollar);
    		break;
    	case "PERCENT":
			ChkCoup (oForm, oForm.cpon.value, discount_percent);
    		break;
    	}
	}
	
	// if there are default values, initialize to them
	if (oForm.default_amount) amt = oForm.default_amount.value
	if (oForm.default_quantity) qty = oForm.default_quantity.value
	if (oForm.default_item_name) iname = oForm.default_item_name.value
	if (oForm.default_item_number) inmr = oForm.default_item_number.value
	if (oForm.default_on0) on0 = oForm.default_on0.value
	if (oForm.default_os0) os0 = oForm.default_os0.value
	if (oForm.default_on1) on1 = oForm.default_on1.value
	if (oForm.default_os1) os1 = oForm.default_os1.value

	// run entire form
	for (i = 0; i < oForm.length; i++)
	{
		obj = oForm.elements[i];											// a form element
		if (obj.type == "checkbox" || obj.type == "radio")					// checkbox or radio
		{
			if (obj.checked)												// did user check it?
			{
				iname = obj.value											// save item name
				val = obj.value;											// the value of the selection
				ary = val.split (" ");										// break apart the value
				for (j = 0; j < ary.length; j++)							// look at all items for single character tokens...
				{
					if (ary[j].length < 2) continue;						// skip to too short
					tok = ary[j].substring (0,1);							// first character
					val = ary[j].substring (1);								// rest of characters
					if (tok == "@") amt = val * 1.0;						// "@" to indicate that a price value follows.
					if (tok == "+") amt = amt + val * 1.0;					// "+" to indicate that a price increment follows.
					if (tok == "%") amt = amt + (amt * (val / 100.0));		// "%" to indicate that a price percent change follows.
					if (tok == "#")											// "#" to indicate that an item number follows.
					{
						if (oForm.item_number) oForm.item_number.value = val;
						ary[j] = "";										// zap this array element
					}
																			// look at all items for 3-character tokens...
					if (ary[j].length < 4) continue;						// skip to too short
					tok = ary[j].substring (0,3);							// first 3 chars
					val = ary[j].substring (3);								// rest of characters
					if (tok == "s1=")										// value for shipping
					{
						if (oForm.shipping) oForm.shipping.value = val;
						ary[j] = "";										// zap this array element
					}
					if (tok == "s2=")										// value for shipping2
					{
						if (oForm.shipping2) oForm.shipping2.value = val;
						ary[j] = "";										// zap this array element
					}
				} //end for j
				val = ary.join (" ");										// rebuild val with what's left
																			// Option Fields must be named in format "OptionX" where X=1a,1b,2a,2b,3i
				tag = obj.name.substring (obj.name.length-2);				// get flag as last 2 characters
				if	(tag == "1a") on0 = on0 + " " + val;
				else if (tag == "1b") os0 = os0 + " " + val;
				else if (tag == "2a") on1 = on1 + " " + val;
				else if (tag == "2b") os1 = os1 + " " + val;
				else if (tag == "3i") inmr = inmr + " " + val;
				
				if (iname.length == 0) iname = val;
			}
		}
	} // end for i
	
	// cannot contiue if invalid number of tickets
	if (oForm.tix_count.value <= 0) return;
	
	// Now summarize stuff just processed above and load in to form object
	tixtot = oForm.tix_count.value * amt
	feetot = oForm.tix_count.value * 1.0										// Fee hardcoded to $1 per ticket
  	switch (coupon_state.toUpperCase())
  	{
  	case "DOLLAR":
		disctot = oForm.tix_count.value * cdis
  		break;
  	case "PERCENT":
		disctot = oForm.tix_count.value * (amt * (cdis / 100.0))
  		break;
  	}
	amt = (tixtot - disctot + feetot)
	oForm.tixtot.value = "  $" + Dollar(tixtot)
	oForm.disctot.value = "- $" + Dollar(disctot)
	oForm.feetot.value = "+ $" + Dollar(feetot)
	oForm.tot.value = "  $" + Dollar (amt);
	oForm.amount.value = amt / oForm.tix_count.value;
	oForm.quantity.value = oForm.tix_count.value;
	oForm.item_name.value = iname;

	if (on0.length > 0) oForm.on0.value = on0;
	if (os0.length > 0) oForm.os0.value = os0;
	if (on1.length > 0) oForm.on1.value = on1;
	if (os1.length > 0) oForm.os1.value = os1;

	// manually set option names & values
	oForm.item_number.value = ""
	if (disctot > 0)
	{
    	switch (coupon_state.toUpperCase())
    	{
    	case "DOLLAR":
    		oForm.on1.value = "...$" + Dollar(cdis) + "/ticket Discount Applied (coupon " + oForm.cpon.value + ") ";
    		break;
    	case "PERCENT":
    		oForm.on1.value = "..." + cdis + "% Discount Applied (coupon " + oForm.cpon.value + ") ";
    		break;
    	}
		oForm.os1.value = "You saved $" + Dollar(disctot) + ". ";
		oForm.item_number.value = oForm.item_number.value + oForm.on1.value + " " + oForm.os1.value
	}
	else
	{
		oForm.on1.value = "";
		oForm.os1.value = "";
	}
	if (oForm.tix_count.value > 0)
	{
		oForm.on0.value = "Processing Fee ";
		oForm.os0.value = "$" + Dollar(feetot) + ".";
		oForm.item_number.value = oForm.item_number.value + oForm.on0.value + " " + oForm.os0.value
	}
	else
	{
		oForm.on0.value = "";
		oForm.os0.value = "";
	}
}

function Validate_Form (oForm)
{
	var msg = ""
	
	if (oForm.quantity.value < 1)
	{
		msg = msg + "Invalid Number of Tickets: " + oForm.tix_count.value + "\n"
	}

	// disable shows that are no longer available
	switch (oForm.item_name.value) 
	{
	case oForm.default_item_name.value:
		msg = msg + "Performance Not Selected" + "\n"
		break;
	case "Laffing Room Only + Dinner, Saturday, March 17th, 6:00 PM, Ridge Firehouse @35.00":
	case "Laffing Room Only (Show Only), Saturday, March 17th, 8:00 PM, Ridge Firehouse @25.00":
	case "Laffing Room Only, Friday, March 23rd, 8:00 PM, SJVFLC @15.00":
	case "Laffing Room Only, Saturday, March 24th, 2:00 PM, SJVFLC @10.00":
	case "Laffing Room Only, Saturday, March 24th, 8:00 PM, SJVFLC @15.00":
		msg = msg + "This performance is no longer available." + "\n" + "\n" + "Please select another performance."
		break;
	default:
		break;
	}

	if (msg.length > 0)
	{
		alert(msg);
		oForm.tix_count.value = 0	// force FrontPage validation failure to stop jump to PayPal
		return(false);
	}
	else
	{
//		alert("No errors");
		return(true);
	}
}

function Dump (oForm)
{
	var msg = ""
	msg = msg + "amount=" + oForm.amount.value + "\n"
	msg = msg + "quantity=" + oForm.quantity.value + "\n"
	msg = msg + "item_name=" + oForm.item_name.value + "\n"
	msg = msg + "item_number=" + oForm.item_number.value + "\n"
	msg = msg + "on0=" + oForm.on0.value + "\n"
	msg = msg + "os0=" + oForm.os0.value + "\n"
	msg = msg + "on1=" + oForm.on1.value + "\n"
	msg = msg + "os1=" + oForm.os1.value + "\n"
	alert(msg)
}