﻿var Collection = function()
{
	var curIndex = -1;
	this.List = new Array();
	this.Count = 0;

	// Scope: public
	// Adds an item to the collection.
	// params:
	//  item - the item to be added to the collection.
	this.Add = function(item)
	{
		this.List[++curIndex] = item;
		this.Count++;
	}

	// Scope: public
	// Gets the index of the item in the collection.
	// params:
	//  item - the item to find the index of.
	// return:
	//	The index of the item specfied.
	this.IndexOf = function(item)
	{
		var result = 0;

		if (curIndex > 0)
		{
			for (var i = 0; i < curIndex; i++)
			{
				if (this.List[i] == item)
				{
					result = i;
					break;
				}
			}
		}

		return result;
	}

	// Scope: public
	// Removes an item from the collection.
	// params:
	//  item - the item to be removed from the collection.
	// return:
	//  The item removed from the collection.
	this.Remove = function(item)
	{
		return this.RemoveAt(this.IndexOf(item));
	}

	// Scope: public
	// Removes an item from the collection at the specified index.
	// params:
	//  index - the index of the item to be removed from the collection.
	// return:
	//  The item removed from the collection.
	this.RemoveAt = function(index)
	{
		var item = null;

		if (this.Count > index && index > -1)
		{
			item = this.List[index];
			var counter = 0;
			var list = new Array();

			for (var i = 0; i < this.Count; i++)
			{
				if (counter != index)
				{
					list[counter] = this.List[i];
					counter++;
				}
				else
				{
					index = -1;
				}
			}

			this.List = list;
			this.Count = list.length;
			curIndex = this.Count - 1;
		}

		return item;
	}
}

var PrizeLogic =
{
	Controls: new Collection(),
	IsPostBack: true,
	IsInitialized: false,

	// Scope: public / static
	// Initializes the body tag's onload function to run PrizeLogic.InitializeControls
	// and will only set that event if IsPostBack = true and the object has not
	// already been initialized. (default = true)
	Init: function()
	{
		if (!this.IsInitialized && this.IsPostBack)
		{
			window.onload = this.InitializeControls;
			this.IsInitialized = true;
		}
	},

	// Scope: public / static
	// Iterates through the collection of controls and calls each 
	// controls Init function.
	InitializeControls: function()
	{
		var length = PrizeLogic.Controls.Count;

		for (var i = 0; i < length; i++)
		{
			PrizeLogic.Controls.List[i].Init();
		}
	}
};

var BirthdayControl = function(serverTxtBoxId, clientDdlMonthId, clientDdlDayId, clientDdlYearId, minAge)
{
	this.ddlMonthId = clientDdlMonthId;
	this.ddlDayId = clientDdlDayId;
	this.ddlYearId = clientDdlYearId;
	this.txtBirthdayId = serverTxtBoxId;
	this.MinAge = minAge;
	this.SetOnPostBack = true;

	// Scope: private
	// Gets the number of days in the specified month.
	// params:
	//	selectedIndex - the selected index of the month dropdown.
	//	ddlDay - the day dropdown list object.
	var GetDayCount = function(selectedIndex, ddlDay)
	{
		var result = 0;

		if ((selectedIndex > 0) && (ddlDay != null))
		{
			ClearItems(ddlDay);

			if (selectedIndex == 2)
			{
				result = 28;
			}
			else if ((selectedIndex == 4) || (selectedIndex == 6) || (selectedIndex == 11) || (selectedIndex == 13))
			{
				result = 30;
			}
			else
			{
				result = 31;
			}
		}

		return result;
	}

	// Scope: private
	// Clears the options in the day dropdown list.
	// params:
	//	ddlDay - the day dropdown list object.
	var ClearItems = function(ddlDay)
	{
		if (ddlDay != null)
		{
			ddlDay.options.length = 0;

			var option = new Option("DD", "-1", null);
			ddlDay.options.add(option);
		}
	}

	// Scope: public
	// Initializes this object's controls to the proper values
	// will only run if SetOnPostBack = true. (default = true)
	this.Init = function()
	{
		if (this.SetOnPostBack)
		{
			var ddlDay = document.getElementById(this.ddlDayId);
			var ddlMonth = document.getElementById(this.ddlMonthId);
			var ddlYear = document.getElementById(this.ddlYearId);
			var txtBirthday = document.getElementById(this.txtBirthdayId);

			if ((txtBirthday != null) && (ddlDay != null) && (ddlMonth != null) && (ddlYear != null))
			{
				var mdy = txtBirthday.value.split("/");

				if (mdy.length > 2)
				{
					ddlMonth[mdy[0]].selected = true;
					this.AddDaysToDropDown(ddlMonth);
					
					ddlDay[mdy[1]].selected = true;
					this.AddYearsToDropDown(mdy[2], this.MinAge);
					//this.SetHiddenField("/");
				}
			}
		}
	}

	// Scope: public
	// Adds the number of days to the day dropdown list for the current month.
	// params:
	//	ddlMonth - the month dropdown list object.
	this.AddDaysToDropDown = function(ddlMonth)
	{
		var selectedIndex = ddlMonth.selectedIndex;
		var ddlDay = document.getElementById(this.ddlDayId);

		if ((ddlMonth != null) && (selectedIndex > 0))
		{
			var option = null;
			var dayCount = GetDayCount(selectedIndex, ddlDay);

			if ((ddlDay != null) && (ddlDay.length < 2))
			{
				for (var i = 1; i <= dayCount; i++)
				{
				    if(i < 10)
				    {
					    option = new Option("0"+i, "0"+i, null);
					    ddlDay.options.add(option);
					}
					else
					{
					    option = new Option(i, i, null);
					    ddlDay.options.add(option);
					}
				}
			}
		}
		else
		{
			this.ClearItems(ddlDay);
		}

		this.SetHiddenField();
	}

	// Scope: public
	// Adds the past 100 years to the year dropdown list.
	// params:
	//	valueToSelect - the value to select in the year dropdown. (optional)
	this.AddYearsToDropDown = function(valueToSelect, minAge)
	{
		var ddlYear = document.getElementById(this.ddlYearId);

        if(minAge > 0)
            minAge = minAge - 1;
            
		if ((ddlYear != null) && (ddlYear.length == 1))
		{
			var option = null;
			var year = new Date().getFullYear() - minAge;

			for (var i = 0; i < 100; i++)
			{
				if (year == valueToSelect)
				{
					option = new Option(year, year, null, true);
				}
				else
				{
					option = new Option(year, year, null);
				}

				ddlYear.options.add(option);
				year--;
			}
		}

		this.SetHiddenField();
	}

	// Scope: public
	// Sets the selected values in the month, day, and year dropdowns to
	// the hidden server control.
	// params:
	//	seperator - the seperator to be used between the date values. (examples: '/', '-', '.')
	this.SetHiddenField = function(seperator)
	{
		var ddlDay = document.getElementById(this.ddlDayId);
		var ddlMonth = document.getElementById(this.ddlMonthId);
		var ddlYear = document.getElementById(this.ddlYearId);
		var txtBirthday = document.getElementById(this.txtBirthdayId);
		seperator = (seperator || '/');

		if ((txtBirthday != null) && (ddlDay != null) && (ddlMonth != null) && (ddlYear != null))
		{
			txtBirthday.value = ddlMonth[ddlMonth.selectedIndex].value + seperator + ddlDay[ddlDay.selectedIndex].value + seperator + ddlYear[ddlYear.selectedIndex].value;
		}
	}
}

var PhoneNumberControl = function(serverTxtBoxId, clientPcs1Id, clientPcs2Id, clientPcs3Id)
{
	this.Pcs1Id = clientPcs1Id;
	this.Pcs2Id = clientPcs2Id;
	this.Pcs3Id = clientPcs3Id;
	this.TxtPhoneNumberId = serverTxtBoxId;
	this.SetOnPostBack = true;

	// Scope: public
	// Initializes this object's controls to the proper values
	// will only run if SetOnPostBack = true. (default = true)
	this.Init = function()
	{
		if (this.SetOnPostBack)
		{
			var txtPhoneNumber = document.getElementById(this.TxtPhoneNumberId);
			var pcs1 = document.getElementById(this.Pcs1Id);
			var pcs2 = document.getElementById(this.Pcs2Id);
			var pcs3 = document.getElementById(this.Pcs3Id);

			if (txtPhoneNumber != null && pcs1 != null && pcs2 != null && pcs3 != null)
			{
				if (txtPhoneNumber.value.length >= 10)
				{
					var pcs = txtPhoneNumber.value.split('-');

					if (pcs.length == 3)
					{
						pcs1.value = pcs[0];
						pcs2.value = pcs[1];
						pcs3.value = pcs[2];
					}
					else
					{
						pcs1.value = txtPhoneNumber.value.subString(0, 2);
						pcs1.value = txtPhoneNumber.value.subString(3, 5);
						pcs1.value = txtPhoneNumber.value.subString(6, 9);
					}
				}
			}
		}
	}

	// Scope: public
	// Checks the number of characters in the textbox is 3 and sets the
	// hidden server controls value equal to the value of the 3 textboxes.
	// params:
	//	currentId - the id of the textbox that currently has focus.
	//	nextId - the id of the textbox to gain focus next.
	this.ThreeTab = function(currentId, nextId)
	{
		var next = document.getElementById(nextId);
		var current = document.getElementById(currentId);

		if (current.value.length == 3)
		{
			this.BuildPCS();
			next.focus();
		}
	}

	// Scope: public
	// Sets the value of the server control to the values of the 3 textboxes.
	// params:
	//	seperator - the seperator to be used between the date values. (examples: '-', '.')
	this.BuildPCS = function(seperator)
	{
		var hdnPCS = document.getElementById(this.TxtPhoneNumberId);
		var pcs1 = document.getElementById(this.Pcs1Id);
		var pcs2 = document.getElementById(this.Pcs2Id);
		var pcs3 = document.getElementById(this.Pcs3Id)

		if (hdnPCS != null && pcs1 != null && pcs2 != null && pcs3 != null)
		{
			hdnPCS.value = pcs1.value + seperator + pcs2.value + seperator + pcs3.value;
		}
	}
}
