function ToggleAllDay( value )
{
	if( value )
	{
		if( divStartTime = document.getElementById('startTime') )
		{
			divStartTime.style.display = 'none';
		}
		if( divEndTime = document.getElementById('endTime') )
		{
			divEndTime.style.display = 'none';
		}
	}
	else
	{
		if( divStartTime = document.getElementById('startTime') )
		{
			divStartTime.style.display = 'inline';
		}
		if( divEndTime = document.getElementById('endTime') )
		{
			divEndTime.style.display = 'inline';
		}
	}

}

function ToggleRecurrance( value )
{
	if( !value )
	{
		if( objRecurDays = document.getElementById('tblRecurDays') )
		{
			objRecurDays.style.display = 'none';
		}
		if( objEnds = document.getElementById('recurranceEnds') )
		{
			objEnds.style.display = 'none';
		}
		if( lblRecurTitle = document.getElementById('lblRecur') )
		{
			lblRecurTitle.innerHTML = "Recurs";
		}
	}
	else
	{
		if( objRecurDays = document.getElementById('tblRecurDays') )
		{
			objRecurDays.style.display = 'block';
		}
		if( objEnds = document.getElementById('recurranceEnds') )
		{
			objEnds.style.display = 'block';
		}
		if( lblRecurTitle = document.getElementById('lblRecur') )
		{
			lblRecurTitle.innerHTML = "Recurs, every";
		}
	}

}

function ToggleRecurranceEnd( value )
{
	if( value == undefined ) return;

	if( value == 'until' )
	{
		if( objRecurEndDate = document.getElementById('cntrRecurEnd') )
		{
			objRecurEndDate.style.display = 'block';
		}
	}
	else if( value == 'never' )
	{
		if( objRecurEndDate = document.getElementById('cntrRecurEnd') )
		{
			objRecurEndDate.style.display = 'none';
		}
	}
}

function EventRecur( day, recurEvent )
{
	var recurEvent = recurEvent == undefined ? true : recurEvent;

	if( dayCheck = document.getElementById( 'recur' + day ) )
	{
		dayCheck.checked = recurEvent;
	}
}


function TimeSelector( inputId, objId, defaultTime, comparison )
{
	this.objId = objId;
	this.inputId = inputId;

	this.startHour = 0;
	this.endHour = 23;

	if( !( obj = document.getElementById( this.objId ) ) )
	{
		return;
	}
	if( !( input = document.getElementById( this.inputId ) ) )
	{
		return;
	}
	if( comparison != undefined && !( comparison = document.getElementById( comparison ) ) )
	{
		return;
	}

	this.obj = obj;
	this.input = input;
	this.comparison = comparison;

	this.defaultTime = defaultTime != undefined ? defaultTime : "";

	this.isOver = false;

	obj.className			= "timeContainer";

	// make sure the drowp down is hidden
	this.Hide();

	// Set obj caller instance
	obj.caller = this;

	// Setup events for the drop down object
	obj.onmouseover = function()
	{
		this.caller.isOver = true;
	}
	obj.onmouseout = function()
	{
		this.caller.isOver = false;
		this.caller.input.focus();
	}
	obj.onblur = function()
	{
		this.caller.Hide();
	}


	// Set input caller instance
	input.caller = this;
	input.className = "timeInput";
	input.value 	= this.defaultTime;

	// Setup events for the input
	input.onclick = function() { this.caller.Show(); }
	input.onmouseover = function() { this.caller.isOver = true; }
	input.onmouseout = function() { this.caller.isOver = false; }
	input.onkeydown = function() { this.caller.Hide( true ); }
	input.onkeyup = function() { this.caller.CheckTime();  }
	input.onblur = function() { this.caller.Hide(); }
}

TimeSelector.prototype.DrawTimes = function()
{
	this.obj.innerHTML = '';

	// check for a compar value
	var compareValue = null;

	if( this.comparison )
	{
		var intHour = 0;
		var validTime = ValidateTime( this.comparison.value );
		if( validTime[0] != 0 )
		{
			intHour = parseInt(validTime[0]);
			intHour += ( validTime[2] == 'pm' ) ? 12 : 0;
		}
		compareValue = intHour;
	}

	for( i = this.startHour; i <= this.endHour	; i++ )
	{
		var hourDisplay = this.GetTimeString( i );

		var difference = "";
		if( compareValue != null )
		{
			difference = (i-compareValue) > 0 ? " (" + (i-compareValue) + " hour" + ((i-compareValue != 1 ) ? "s" : "") + ")" : "";
		}

		timeDiv = document.createElement('div');
		timeDiv.className 	= 'timeDisplay';
		timeDiv.caller 		= this;
		timeDiv.hourValue 	= i;
		timeDiv.innerHTML 	= hourDisplay + difference;
		timeDiv.clickValue 	= hourDisplay;

		timeDiv.onmouseover = function() { this.className = 'timeDisplayOver' };
		timeDiv.onmouseout = function() { this.className = 'timeDisplay' };
		timeDiv.onclick = function()
		{
			this.caller.input.value = this.clickValue;
			this.caller.Hide( true );
		};

		this.obj.appendChild(timeDiv);

		var hourDisplay = this.GetTimeString( i + 0.5 );

		var difference = "";
		if( compareValue != null )
		{
			difference = ((i + 0.5)-compareValue) > 0 ? " (" + ((i + 0.5)-compareValue) + " hour" + (((i + 0.5)-compareValue != 1 ) ? "s" : "") + ")" : "";
		}

		timeDiv = document.createElement('div');
		timeDiv.className 	= 'timeDisplay';
		timeDiv.caller 		= this;
		timeDiv.hourValue 	= i + 0.5;
		timeDiv.innerHTML 	= hourDisplay + difference;
		timeDiv.clickValue 	= hourDisplay;

		timeDiv.onmouseover = function() { this.className = 'timeDisplayOver' };
		timeDiv.onmouseout = function() { this.className = 'timeDisplay' };
		timeDiv.onclick = function()
		{
			this.caller.input.value = this.clickValue;
			this.caller.Hide( true );
		};

		this.obj.appendChild(timeDiv);
	}
}

TimeSelector.prototype.Show = function()
{
	this.obj.style.display = "block";

	this.DrawTimes();

	var validTime = ValidateTime( this.input.value );
	if( validTime[0] != 0 )
	{
		var intHour = parseInt(validTime[0]);
		intHour += ( validTime[2] == 'pm' ) ? 12 : 0;

		this.scrollTop = intHour * 37;
	}

	// I nominate this the best fix for IE ever!
	// yeah, remove one of these and see how it will
	// preselect default time values

	this.obj.scrollTop = this.scrollTop;
	this.obj.scrollTop = this.scrollTop;

}

TimeSelector.prototype.Hide = function( forceHide )
{
	if( forceHide == undefined || forceHide == false )
	{
		if( this.isOver ) return;
	}

	this.obj.style.display = "none";
}
TimeSelector.prototype.CheckTime = function( )
{
	var validTime = ValidateTime( this.input.value );

	if( !validTime )
	{
		this.input.className = 'timeInputInvalid';
		return false;
	}

	this.input.className = 'timeInput';
	return true;
}


function ValidateTime( Q )
{
	var T;
  	if( ( T = /^(\d?\d):(\d\d)\s?(([ap])?m?)?$/i.exec(Q)) == null) { return false; } // bad format

  	if( T[1] > 12 ) { return false; }
  	if( T[2] > 59 ) { return false; }

  	return [ T[1], T[2], T[3] ];
}

TimeSelector.prototype.GetTimeString = function( hour )
{
	var intHour = parseInt( hour );
	var intDec	= hour - intHour;		// this will hold the decimal places, if any

	var ap = "am";

	if( intHour > 11 )
	{
		intHour -= 12;
		ap = "pm";
	}
	intHour = intHour == 0 ? 12 : intHour;

	var minutes = ( intDec > 0 ) ? 60 * intDec : "00";

	return intHour + ":" + minutes + ap;
}