/*	=========================	*/
//	This script is for the No. Golfers select bug on 
//	the 3rd page of the price quote form.
//	basically pear isn't setting defaults to the elements
//	in the element grid if its within a controller page (multipage form controller)
//	What this script does is check if all the select boxes have the same index
//	ie. (they're all the same)  If they are, then it sets their default state to
//	the highest # of golfers available.  If they are not all the same
//	then that means the user has submitted this form once and changed the #
//	of golfers on one day and has since returned to this form to correct something
//	or is passing through to get to a previous or later form page.

//	Gaslight Media (c) 2008
//	04/21/2008
//	Author: Jamie Kahgee <jamie.kahgee@gmail.com>
/*	=========================	*/
var golfersSelect =
{
	selects: null,
	length: null,
	target: null,

	init: function()
	{
		if ($('viewCourses'))
		{
			golfersSelect.selects = $$('table.elementGrid select');
			golfersSelect.length = golfersSelect.selects.length;
			golfersSelect.target = golfersSelect.selects[0].options.length - 1;
			
			//	Only make the elements all the same if the user
			//	hasn't passed through here before.
			if (golfersSelect.allTheSame())
			{
				golfersSelect.makeTheSame();
			}
		}
	},

	allTheSame: function()
	{
		var initIndex = golfersSelect.selects[0].selectedIndex;
		var allTheSame = true;
		for (var i = 0, j = 0; i < golfersSelect.length; ++i, ++j)
		{
			if (j == 3)
			{
				j = 0;
			}

			if (j == 0)
			{
				if (golfersSelect.selects[i].selectedIndex != initIndex)
				{
					allTheSame = false;
				}
			}
		}

		return allTheSame;
	},

	makeTheSame: function()
	{
		for (var i = 0, j = 0; i < golfersSelect.length; ++i, ++j)
		{
			if (j == 3)
			{
				j = 0;
			}

			if (j == 0)
			{
				golfersSelect.selects[i].selectedIndex = golfersSelect.target;
			}
		}
	}
};

Event.observe(window, 'load', golfersSelect.init);
