function dateEdit(name, date, time)
{		
	this.name = name;
	this.date = date;
	this.time = time;
	this.input = null;
	this.inputTime = null;
	this.button = null;
	this.validate = null;
	
	this.isNumeric = function(n)
	{
		var num = parseInt(n,10);
		return !isNaN(num);
	}

	this.padZero = function(n)
	{
		if (n<10)
		{
			return ('0'+n);
		}
		return n;
	}
	
	this.getDate = function()
	{
		return this.date;
	}
	
	this.setDate = function(date)
	{					
		this.date = date;
		this.getInput().value = this.getDateString();
	}
	
	this.getInput = function()
	{
		if (this.input == null)
		{
			this.input = document.getElementById(this.name);
			this.input.parent = this;
			this.input.style.width = "80px";
			this.input.className = "formbox"; //CRAP
			this.input.value = this.getDateString();
			
			this.input.onblur = function()
			{
				var date = parseDate(this.value);
				
				if (date == null)
				{
					this.parent.resetDate();
					this.select();
					alert("Please enter a valid date.");
					return;
				}
				
				var valid = true;
				
				if (this.parent.validate != null)
				{
					valid = this.parent.validate();
				}
				
				if (valid)
				{
					this.parent.setDate(date);
				}
				else
				{
					this.parent.resetDate();
					this.select();
				}
			}
		}
		
		return this.input;
	}
	
	this.getInputTime = function()
	{
		if (this.inputTime == null)
		{
			this.inputTime = document.getElementById(this.name + "Time");
			this.inputTime.parent = this;
			this.inputTime.style.width = "50px";
			this.inputTime.className = "textBoxStyle";
			this.inputTime.value = this.getTimeString();

			this.inputTime.onblur = function()
			{
				var t = this.value.toUpperCase();
				
				t = t.replace(" ","");
				t = t.replace(".",":");
				t = t.replace("-","");

				if ((this.parent.isNumeric(t)) && (t.length==4))
				{
					t = t.charAt(0) + t.charAt(1) + ":" + t.charAt(2) + t.charAt(3);
				}

				t = new String(t);
				tl = t.length;

				if (tl == 1)
				{
					if (this.parent.isNumeric(t))
					{
						this.value = t + ":00 PM";
					}
					else
					{
						return false;
					}
				}
				else if (tl == 2)
				{
					if (this.parent.isNumeric(t))
					{
						if (parseInt(t,10) < 13)
						{
							if (t.charAt(1) != ":")
							{
								this.value = t + ':00 PM';
							}
							else
							{
								this.value= t + '00 PM';
							}
						}
						else if (parseInt(t,10) == 24)
						{
							this.value = "12:00 AM";
						}
						else if (parseInt(t,10) < 24)
						{
							if (t.charAt(1) != ":")
							{
								this.value = (t-12) + ':00 PM';
							} 
							else
							{
								this.value = (t-12) + '00 PM';
							}
						}
						else if (parseInt(t,10) <= 60)
						{
							this.value = '12:' + this.parent.padZero(t) + ' AM';
						}
						else
						{
							this.value= '1:' + this.parent.padZero(t%60) + ' PM';
						}
					}
					else
					{
						if ((t.charAt(0) == ":") && (isNaN(t.charAt(1))))
						{
							this.value = "12:" + this.parent.padZero(parseInt(t.charAt(1),10)) + " PM";
						}
						else
						{
							return false;
						}
					}
				}
				else if (tl >= 3)
				{
					var arr = t.split(":");
					var mode = "";
					
					if (t.indexOf(":") > 0)
					{
						hr = parseInt(arr[0],10);
						mn = parseInt(arr[1],10);

						if (t.indexOf("PM") > 0)
						{
							mode = "PM";
						}
						else
						{
							mode = "AM";
						}

						if (isNaN(hr))
						{
							hr = 0;
						}
						else
						{
							if (hr > 24)
							{
								return false;
							}
							else if (hr == 24)
							{
								mode = "AM";
								hr = 0;
							}
							else if (hr > 12)
							{
								mode = "PM";
								hr -= 12;
							}
						}
						if (isNaN(mn))
						{
							mn = 0;
						}
						else
						{
							if (mn > 60)
							{
								mn = mn % 60;
								hr += 1;
							}
						}
					}
					else
					{
						hr = parseInt(arr[0],10);

						if (isNaN(hr))
						{
							hr = 0;
						}
						else
						{
							if (hr > 24)
							{
								return false;
							}
							else if (hr == 24)
							{
								mode = "AM";
								hr = 0;
							}
							else if (hr > 12)
							{
								mode = "PM";
								hr -= 12;
							}
						}
						mn = 0;
					}
					if (hr == 24)
					{
						hr = 12;
						mode = "AM";
					}
					this.value =hr + ":" + this.parent.padZero(mn) + " " + mode;
				}
			}
		}
		
		return this.inputTime;
	}
	
	this.getButton = function()
	{
		if (this.button == null)
		{
			this.button = document.getElementById(this.name + "Button");
			this.button.parent = this;
			
			this.button.onmouseover = function()
			{
				try
				{
					this.style.cursor = "pointer";
				}
				catch(e)
				{
					this.style.cursor = "hand";
				}
			}
			
			this.button.onclick = function()
			{
				var input = this.parent.input;
			
				if (popupCalendar != null)
				{
					popupCalendar.hide();
				}
				else
				{
					popupCalendar = new Calendar(1, null, this.parent.selected, this.parent.close);
					
					popupCalendar.setRange(1900, 2070);
					popupCalendar.create();
				}
				
				popupCalendar.setDateFormat("%m/%d/%Y");
				popupCalendar.parseDate(input.value);
				popupCalendar.sel = input;

				popupCalendar.showAtElement(this, "Br");
			}
		}
		
		return this.button;
	}
	
	this.getContainer = function()
	{
		this.getInput();
		this.getButton();
		
		if (time != null)
		{
			this.getInputTime();
		}
	}
	
	this.getDateString = function()
	{				
		var result = "";
		var dd = this.date.getDate();
		var mm = this.date.getMonth() + 1;
		var yyyy = this.date.getFullYear();
		
		if(dd > 1 || mm > 1 || yyyy > 1901)
		{
			if(dd < 10) dd = "0" + dd;
			if(mm < 10) mm = "0" + mm;
			
			result = mm + "/" + dd + "/" + yyyy;
		}
		
		return result;
	}
	
	this.getTimeString = function()
	{
		var result = "";
		var mer = "AM";
		var hh = this.date.getHours();
		var mm = this.date.getMinutes();
		
		if (hh > 1 || mm > 1)
		{
			if (hh > 12)
			{
				hh -= 12;
				mer = "PM";
			}
			else if (hh == 12)
			{
				mer = "PM";
			}
			
			if (mm < 10) mm = "0" + mm;
			
			result = hh + ":" + mm + " " + mer;
		}
		else
		{
			result = "12:00 PM";
		}
		
		return result;
	}
	
	this.selected = function(calendar, date)
	{
		calendar.sel.value = date;
	
		if (calendar.dateClicked)
		{
			calendar.callCloseHandler();
		}
	}
	
	this.close = function(calendar)
	{
		calendar.hide();
		popupCalendar = null;
	}
	
	this.resetDate = function()
	{
		this.setDate(this.getDate());
	}
	
	this.render = function()
	{
		this.getContainer();
	}
}