WAIVE_SHIPPING_CHARGE = false;

TYPE_SUB          = 1;
TYPE_FP           = 2;
TYPE_SERIES       = 3;
TYPE_MEM          = 4;
TYPE_PRIME        = 9;
SUB_TYPE_NONE     = 100;
SUB_TYPE_PLATINUM = 101;
SUB_TYPE_SILVER   = 102;
SUB_TYPE_ANYTIME  = 103;
SUB_TYPE_MATINEE  = 104;
SUB_TYPE_PREVIEW  = 105;
SUB_TYPE_SERIES   = 106;
SUB_TYPE_CORP     = 107;
SUB_TYPE_PHOMT    = 108;
FP_TYPE_NORMAL    = 201;
FP_TYPE_30U       = 202;
FP_TYPE_STUDENT   = 203;
MEM_TYPE_STUDENT  = 301;
MEM_TYPE_30U      = 302;


function Item(qty, type, subtype, number, price, note) {
	this.quantity = qty;
	this.type = type;
	this.subtype = subtype;
	this.number = number;
	this.price = price;
	this.note = note;

	this.isStudent = Item_isStudent;
	this.is30U = Item_is30U;
	this.serviceCharge = Item_serviceCharge;
	this.description = Item_description;
	this.sameAs = Item_sameAs;
}

function Item_isStudent() {
	return ((this.type == TYPE_FP && this.subtype == FP_TYPE_STUDENT) ||
            (this.type == TYPE_MEM && this.subtype == MEM_TYPE_STUDENT));
}

function Item_is30U() {
	return ((this.type == TYPE_FP && this.subtype == FP_TYPE_30U) ||
	        (this.type == TYPE_MEM && this.subtype == MEM_TYPE_30U));
}

function Item_serviceCharge() {
	if (this.type == TYPE_PRIME)
		return 0;
	if (this.isStudent() || this.is30U())
		return 0;
	if (this.type == TYPE_SUB && this.subtype == SUB_TYPE_PLATINUM)
		return 0;
	return (6 * this.quantity);
}

function Item_description() {
	if (this.type == TYPE_PRIME) {
		return "PlaywrightsPRIME";
	} else if (this.type == TYPE_SUB) {
		switch (this.subtype) {
			case SUB_TYPE_PLATINUM:
				return "Platinum Patron (" + this.number + " shows, 2 seats each)";
			case SUB_TYPE_SILVER:
				return "Silver Pass (" + this.number + " shows plus PlaywrightsPRIME)";
			case SUB_TYPE_ANYTIME:
				if (this.number == 4)
					return "Anytime Subscription (4 shows: " + this.note + ")";
				return "Anytime Subscription (" + this.number + " shows)";
			case SUB_TYPE_MATINEE:
				return "Matinees Subscription (" + this.number + " shows)";
			case SUB_TYPE_PREVIEW:
				return "PreviewsPLUS Subscription (" + this.number + " shows)";
			case SUB_TYPE_CORP:
				return "Corporate (" + this.number + " shows)";
			case SUB_TYPE_SERIES:
				if (this.number == 2 || this.number == 3)
					return "Subscription (" + this.number + " shows: " + this.note + ")";
				return "Subscription (" + this.number + " shows)";
			case SUB_TYPE_PHOMT:
				return "PH On My Terms: " + this.note;
			default:
				return "invalid item";
		}
	} else if (this.type == TYPE_FP) {
		switch (this.subtype) {
			case FP_TYPE_NORMAL:
				return "FlexPass (" + this.number + " tickets)";
			case FP_TYPE_30U:
				return "30&Under FlexPass (" + this.number + " tickets)";
			case FP_TYPE_STUDENT:
				return "Student FlexPass (" + this.number + " tickets)";
			default:
				return "invalid item";
		}
	} else if (this.type == TYPE_SERIES) {
		return  "Series (" + this.number + " shows)";
	} else if (this.type == TYPE_MEM) {
		switch (this.subtype) {
			case MEM_TYPE_STUDENT:
				return "Student Membership";
			case MEM_TYPE_30U:
				return "30&Under Membership";
			default:
				return "invalid item";
		}
	} else
		return "invalid item";
}

function Item_sameAs(that) {
	return (this.type == that.type &&
		this.subtype == that.subtype &&
		this.number == that.number &&
		this.note == that.note);
}

/******************************/

function Order() {
	this.addItem = Order_addItem;
	this.deleteItem = Order_deleteItem;
	this.clear = Order_clear;
	this.size = Order_size;
	this.total = Order_total;
	this.shippingCharge = Order_shippingCharge;
	this.containsMembership = Order_containsMembership;
	this.containsPhomt = Order_containsPhomt;

	this.clear();
}

function Order_addItem(it) {
	for (var i = 0; i < this.items.length; ++i) {
		if (it.sameAs(this.items[i])) {
			this.items[i].quantity += it.quantity;
			return;
		}
	}
	this.items.push(it);
	if (it.isStudent())
		this.contains_stu = true;
	else if (it.is30U())
		this.contains_30u = true;
}

function Order_deleteItem(index) {
	var item = this.items[index];
	if (item) {
		if (item.isStudent())
			this.contains_stu = false;
		else if (item.is30U())
			this.contains_30u = false;
		this.items.splice(index, 1);
	}
}

function Order_clear() {
	this.items = new Array();
	this.contains_stu = false;
	this.contains_30u = false;
}

function Order_size() {
	return this.items.length;
}

function Order_total() {
	var sum = 0;
	for (var i = 0; i < this.items.length; ++i) {
		sum += this.items[i].price * this.items[i].quantity;
	}
	return sum;
}

function Order_shippingCharge() {
	if (WAIVE_SHIPPING_CHARGE)
		return 0;

	var charge = 0;
	for (var i = 0; i < this.items.length; ++i)
		charge += this.items[i].serviceCharge();
	return charge;
}

function Order_containsMembership() {
	return this.contains_stu || this.contains_30u;
}

function Order_containsPhomt() {
	for (i in this.items) {
		if (this.items[i].type == TYPE_SUB && this.items[i].subtype == SUB_TYPE_PHOMT)
			return true;
	}
	return false;
}
