var otherDistrictSelected = false;
var priceComponents = $H();
var skipComponents = $H();
Event.observe(window,'load',function()
{
	skipComponents.update({selfattend: true});
	
	Event.observe("district_id","change",function(event) {
		if ($(this).getValue() == "1050")
		{
			otherDistrictSelected = true;
			skipComponents.update({selfattend: true});
			adjustPrice({otherdistrict: 225});
		}
		else
		{
			if (isFreeAttendance() == false) skipComponents.update({selfattend: false});
			adjustPrice({otherdistrict: 0});
		}
	});

	Event.observe('club_position_id','change',function(event) {
		if (isFreeAttendance() == false)
		{
			skipComponents.update({selfattend: false});
		}
		else
		{
			skipComponents.update({selfattend: true});
		}

		adjustPrice({});
	});

	Event.observe('district_position_id','change',function(event) {
		if (isFreeAttendance() == false)
		{
			skipComponents.update({selfattend: false});
		}
		else
		{
			skipComponents.update({selfattend: true});
		}

		if ($(this).getValue() == "2376")
		{
			skipComponents.update({selfthurs: true});
		}
		else
		{
			skipComponents.update({selfthurs: false});
		}

		adjustPrice({});
	});

	Event.observe('section_a','click',function(event) {
		if (isFreeAttendance() == false)
		{
			skipComponents.update({selfattend: false});
		}
		else
		{
			skipComponents.update({selfattend: true});
		}

		skipComponents.update({selfthurs: false});

		adjustPrice({});
	});

	Event.observe('section_b','click',function(event) {
		if (isFreeAttendance() == false)
		{
			skipComponents.update({selfattend: false});
		}
		else
		{
			skipComponents.update({selfattend: true});
		}

		if ($("district_position_id").getValue() == "2376")
		{
			skipComponents.update({selfthurs: true});
		}
		else
		{
			skipComponents.update({selfthurs: false});
		}

		adjustPrice({});
	});


	Event.observe('person_rotary_self_thursday_dinner','click',function(event) {
		if($(this).checked == true)
		{
			adjustPrice({selfthurs: 23});
		}
		else
		{
			adjustPrice({selfthurs: 0});
		}
	});

	Event.observe('person_rotary_guest_thursday_dinner','click',function(event) {
		if($(this).checked == true)
		{
			adjustPrice({guestthurs: 23});
		}
		else
		{
			adjustPrice({guestthurs: 0});
		}
	});

	Event.observe('person_rotary_guest_attendance_selection','change',function(event) {
		if($(this).getValue() == "Friday") 
		{
			adjustPrice({guestattend: 45});
		}

		if($(this).getValue() == "Friday/Saturday") 
		{
			adjustPrice({guestattend: 88});
		}
	});

	Event.observe("person_detail_spouse_name","change",function(event) {
		if ($(this).value.length == 0)
		{
			$("person_rotary_guest_thursday_dinner").disabled = true;
			$("person_rotary_guest_attendance_selection").disabled = true;
			skipComponents.update({guestthurs: true, guestattend: true});
		}
		else
		{
			$("person_rotary_guest_thursday_dinner").disabled = false;
			$("person_rotary_guest_attendance_selection").disabled = false;
			skipComponents.update({guestthurs: false, guestattend: false});
		}

		adjustPrice({});
	});

	Event.observe('person_rotary_self_attendance_selection','change',function(event) {

		if($(this).getValue() == "Friday") 
		{
			adjustPrice({selfattend: 45});
		}

		if($(this).getValue() == "Friday/Saturday") 
		{
			adjustPrice({selfattend: 88});
		}
	});


	var ctrlPriceHash = $("price_hash");
	var ctrlSkipHash = $("price_hash");
	var priceHash = $H(ctrlPriceHash.getValue().evalJSON());
	var skipHash = $H(ctrlSkipHash.getValue().evalJSON());
	priceComponents.update(priceHash);
	skipComponents.update(skipHash);
	adjustPrice({});
});

function isFreeAttendance()
{
	if ($("club_position_id").disabled == false)
	{
		if ($("club_position_id").getValue() == "2094") return true;
	}

	if ($("district_position_id").disabled == false)
	{
		if ( $("district_position_id").getValue() == "2103" || $("district_position_id").getValue() == "2104" || $("district_position_id").getValue() == "2105" || $("district_position_id").getValue() == "2107" || $("district_position_id").getValue() == "2109" || $("district_position_id").getValue() == "2376" || $("district_position_id").getValue() == "2266" || $("district_position_id").getValue() == "2379") return true;
	}

	return false;
}


function adjustPrice(inputHash)
{
	var displayCost = $("cost");
	var ctrlCost = $("person_rotary_cost");
	var ctrlPriceHash = $("price_hash");
	var ctrlSkipHash = $("skip_hash");
	var price = 0;

	priceComponents.update(inputHash);

	var addValue;
	priceComponents.each(function(pair) {
		addValue = true;
		skipComponents.each(function(skipPair) {
			if (pair.key == skipPair.key && skipPair.value == true)
			{
				addValue = false;
			}
		});
		if (addValue == true) price = price + pair.value;
	});

	ctrlCost.value = price;
	displayCost.update(price);
	//alert(priceComponents.toQueryString());
	//alert(skipComponents.toQueryString());
	//
	ctrlPriceHash.setValue(priceComponents.toJSON());
	ctrlSkipHash.setValue(skipComponents.toJSON());
}

