NCFormControler = Class.create();
NCFormControler.prototype = {

	mticket: 0,
	pticket: 0,
	tprice:  0,

	initialize: function() {

		// deals with the membership ticket types
		$$('input:[name="MemberTicket"]').each(function(fInput){
			Event.observe(fInput, 'change', this.ticket_checked.bindAsEventListener(this));
		}.bind(this));

		//deals with the parking ticket types
		$$('input:[name="Parking"]').each(function(fInput){
			Event.observe(fInput, 'change', this.parking_checked.bindAsEventListener(this));
		}.bind(this));

		// opens and closes the text field depending on the membership type selected
		$$('input:[name="MemberType"]').each(function(fInput){
			Event.observe(fInput, 'change', function(e){
				var tmp = fInput.readAttribute('value');
				if(tmp == "NCAHU-Local Chapter"){
					$('ChapterName').enable();
				}
				else{
					$('ChapterName').clear();
					$('ChapterName').disable();
				}
			}, false);
		});

		//The following deals with the page refresh

		//Sets the value of the current membership ticket selected on page load
		$$('input:[name="MemberTicket"]').each(function(fInput){
			if(fInput.checked) this.get_ticket_checked(fInput);
		}.bind(this));

		//Sets the current parking ticket selected on page load
		$$('input:[name="Parking"]').each(function(fInput){
			if(fInput.checked) this.get_parking_checked(fInput);
		}.bind(this));

		// fills in the total box on page load
		this.total_cost();
	},

	get_ticket_checked: function(data){
		this.ticket(data.readAttribute('value').split(" "));
	},

	get_parking_checked: function(data){
		this.parking(data.readAttribute('value').split(" "));
	},

	ticket_checked: function (event){
		// this event only fires while tabbing after changing content
		var data = Event.element(event).readAttribute('value').split(" ");
		this.ticket(data);

	},

	ticket: function (data){
		this.add_ticket(data[1]);
		this.set_1(data[0]);
		if(data[0] != 'M'){
			$$('input:[name="MemberType"]').each(function(e){e.checked = false;});
			$('ChapterName').clear();
			$('ChapterName').disable();
		}
	},

	parking: function (data){
		this.add_parking(data[1]);
		this.set_2(data[0]);
	},

	parking_checked: function (event){
		// this event only fires while tabbing after changing content
		this.parking(Event.element(event).readAttribute('value').split(" "));
	},

	add_ticket: function (value){
		this.mticket = value;
		this.total_cost();
	},

	add_parking: function (value){
		this.pticket = value;
		this.total_cost();
	},

	total_cost: function (){
		this.tprice = parseInt(this.mticket) + parseInt(this.pticket);
		$('mtotal').writeAttribute('value', this.tprice + ".00").show();
	},

	set_1: function (item){
		var tmp = "2010 NCAHU Symposium: Member Ticket";
		if(item == 'N'){tmp = "2010 NCAHU Symposium: Non AHU Member Ticket";}
		if(item == 'T'){tmp = "2010 NCAHU Symposium: Tuesday Only"}
		if(item == 'W'){tmp = "2010 NCAHU Symposium: Wednesday Only"}
		$('item_name_1').writeAttribute('value', tmp);
		$('item_description_1').writeAttribute('value', "2010 NCAHU Symposium Event Ticket");
		$('item_price_1').writeAttribute('value', this.mticket);
	},

	set_2: function (item){
		var tmp = "";
		if(item == 'B'){tmp = "2010 NCAHU Symposium: One Day Parking Ticket";}
		if(item == 'C'){tmp = "2010 NCAHU Symposium: Two Day Parking Ticket";}
		$('item_name_2').writeAttribute('value', tmp);
		$('item_description_2').writeAttribute('value', "2010 NCAHU Symposium Parking Ticket");
		$('item_price_2').writeAttribute('value', this.pticket);	
	}
}
