/*
 * Deprecated: See jquery-base.js
 */

var dateFormat = "yyyy-mm-dd";
var dom = document.getElementById
var ie=document.all
var ns4=document.layers
var dateWindow = null;
var dateInput = null;
var dateIgnoreNextOnClick = false;

var imageDirectory = "/images/";

var monthWindow;
var yearWindow;
var monthSelected;
var yearSelected;
var dateSelected;
var defaultMonth;
var defaultYear;
var defaultDate;
var monthConstructed;
var yearConstructed;
var intervalID1;
var intervalID2;
var timeoutID1;
var timeoutID2;
var startingYear;

var today = new Date()
var dateNow  = today.getDate()
var monthNow = today.getMonth()
var yearNow  = today.getFullYear()

function swapImage(sourceImage, destinationImage)
{
	if (ie)
	{
		document.getElementById(sourceImage).setAttribute(
			"src",
			imageDirectory + destinationImage);
	}
}

if (dom)
{
	document.write("<div onclick='dateIgnoreNextOnClick=true' id='dateWindow' style='z-index:+999;position:absolute;visibility:hidden;'><table width='220' style='font-family:arial;font-size:11px;border-width:1;border-style:solid;border-color:#000066;font-family:arial; font-size:11px;}' bgcolor='#ffffff'><tr bgcolor='#000066'><td><table width='218'><tr><td style='padding:2px;font-family:arial; font-size:11px;'><font color='#ffffff'><B><span id='caption'></span></B></font></td><td align=right><a href='javascript:hideDateWindow()'><IMG SRC='"+imageDirectory+"pop-close.gif' WIDTH='15' HEIGHT='13' BORDER='0' ALT='Close the Calendar'></a></td></tr></table></td></tr><tr><td style='padding:5px' bgcolor=#ffffff><span id='content'></span></td></tr>")

	document.write ("</table></div><div id='selectMonth' style='z-index:+999;position:absolute;visibility:hidden;'></div><div id='selectYear' style='z-index:+999;position:absolute;visibility:hidden;'></div>");
}

var monthName = new Array(
	"January",
	"February",
	"March",
	"April",
	"May",
	"June",
	"July",
	"August",
	"September",
	"October",
	"November",
	"December");

var dayName = new Array(
	"Mon",
	"Tue",
	"Wed",
	"Thu",
	"Fri",
	"Sat",
	"Sun");

var styleAnchor = "text-decoration:none;color:black;"
var styleLightBorder = "border-style:solid;border-width:1px;border-color:#a0a0a0;"

function padZero(num)
{
	return num < 10 ? '0' + num : num;
}

function constructDate(d,m,y)
{
	temporary = dateFormat
	temporary = temporary.replace("dd", "<e>")
	temporary = temporary.replace("d", "<d>")
	temporary = temporary.replace("<e>", padZero(d))
	temporary = temporary.replace("<d>", d)
	temporary = temporary.replace("mmm", "<o>")
	temporary = temporary.replace("mm", "<n>")
	temporary = temporary.replace("m", "<m>")
	temporary = temporary.replace("<m>", m+1)
	temporary = temporary.replace("<n>", padZero(m+1))
	temporary = temporary.replace("<o>", monthName[m])
	return temporary.replace("yyyy", y)
}

function closeCalendar()
{
	hideDateWindow();
	dateInput.value = constructDate(dateSelected,monthSelected,yearSelected);
}

function StartDecMonth()
{
	intervalID1=setInterval("decMonth()", 80);
}

function StartIncMonth()
{
	intervalID1=setInterval("incMonth()", 80);
}

function incMonth()
{
	monthSelected++;

	if (monthSelected > 11)
	{
		monthSelected = 0;
		yearSelected++;
	}

	constructCalendar();
}

function decMonth()
{
	monthSelected--;

	if (monthSelected < 0)
	{
		monthSelected = 11;
		yearSelected--;
	}

	constructCalendar();
}

function constructMonth()
{
	popDownYear();

	if (!monthConstructed)
	{
		html = "";

		for (var i = 0; i < 12; i++)
		{
			sName = monthName[i];

			if (i==monthSelected)
			{
				sName = "<B>" + sName + "</B>";
			}

			html += "<tr><td id='m" + i + "' onmouseover='this.style.backgroundColor=\"#FFCC99\"' onmouseout='this.style.backgroundColor=\"\"' style='cursor:pointer' onclick='monthConstructed=false;monthSelected=" + i + ";constructCalendar();popDownMonth();event.cancelBubble=true'>&nbsp;" + sName + "&nbsp;</td></tr>";
		}

		document.getElementById("selectMonth").innerHTML = "<table width=70 style='font-family:arial; font-size:11px; border-width:1; border-style:solid; border-color:#a0a0a0;' bgcolor='#FFFFDD' cellspacing=0 onmouseover='clearTimeout(timeoutID1)' onmouseout='clearTimeout(timeoutID1);timeoutID1=setTimeout(\"popDownMonth()\",100);event.cancelBubble=true'>" + html + "</table>";

		monthConstructed=true;
	}
}

function popUpMonth()
{
	constructMonth()
	monthWindow.visibility = (dom||ie) ? "visible" : "show"
	monthWindow.left = parseInt(dateWindow.left) + 50
	monthWindow.top = parseInt(dateWindow.top) + 26
	hideAllElementsBehind("selectMonth");
}

function popDownMonth()
{
	monthWindow.visibility= "hidden";
}

function incYear()
{
	for (var i = 0; i < 7; i++)
	{
		newYear = (i+startingYear)+1

		if (newYear == yearSelected)
		{
			txtYear = "&nbsp;<B>" + newYear + "</B>&nbsp;"
		}
		else
		{
			txtYear = "&nbsp;" + newYear + "&nbsp;"
		}

		document.getElementById("y"+i).innerHTML = txtYear
	}

	startingYear ++;
	dateIgnoreNextOnClick=true;
}

function decYear()
{
	for (var i = 0; i < 7; i++)
	{
		newYear = (i+startingYear)-1;

		if (newYear==yearSelected)
		{
			txtYear = "&nbsp;<B>" + newYear + "</B>&nbsp;";
		}
		else
		{
			txtYear = "&nbsp;" + newYear + "&nbsp;";
		}

		document.getElementById("y"+i).innerHTML = txtYear;
	}

	startingYear --;
	dateIgnoreNextOnClick=true;
}

function selectYear(nYear)
{
	yearSelected = parseInt(nYear+startingYear);
	yearConstructed = false;
	constructCalendar();
	popDownYear();
}

function constructYear()
{
	popDownMonth();
	html = "";

	if (!yearConstructed)
	{
		html = "<tr><td align='center' onmouseover='this.style.backgroundColor=\"#FFCC99\"' onmouseout='clearInterval(intervalID1);this.style.backgroundColor=\"\"' style='cursor:pointer' onmousedown='clearInterval(intervalID1);intervalID1=setInterval(\"decYear()\",30)' onmouseup='clearInterval(intervalID1)'>-</td></tr>";

		j = 0;
		startingYear = yearSelected-3;

		for (var i = yearSelected - 3; i <= yearSelected + 3; i++)
		{
			sName = i;

			if (i==yearSelected)
			{
				sName = "<B>" + sName + "</B>";
			}

			html += "<tr><td id='y" + j + "' onmouseover='this.style.backgroundColor=\"#FFCC99\"' onmouseout='this.style.backgroundColor=\"\"' style='cursor:pointer' onclick='selectYear("+j+");event.cancelBubble=true'>&nbsp;" + sName + "&nbsp;</td></tr>";
			j ++;
		}

		html += "<tr><td align='center' onmouseover='this.style.backgroundColor=\"#FFCC99\"' onmouseout='clearInterval(intervalID2);this.style.backgroundColor=\"\"' style='cursor:pointer' onmousedown='clearInterval(intervalID2);intervalID2=setInterval(\"incYear()\",30)' onmouseup='clearInterval(intervalID2)'>+</td></tr>";

		document.getElementById("selectYear").innerHTML = "<table width=44 style='font-family:arial; font-size:11px; border-width:1; border-style:solid; border-color:#a0a0a0;' bgcolor='#FFFFDD' onmouseover='clearTimeout(timeoutID2)' onmouseout='clearTimeout(timeoutID2);timeoutID2=setTimeout(\"popDownYear()\",100)' cellspacing=0>" + html + "</table>";

		yearConstructed = true;
	}
}

function popDownYear()
{
	clearInterval(intervalID1);
	clearTimeout(timeoutID1);
	clearInterval(intervalID2);
	clearTimeout(timeoutID2);
	yearWindow.visibility= "hidden"
}

function popUpYear()
{
	var leftOffset;
	constructYear();
	yearWindow.visibility = (dom||ie)? "visible" : "show";
	leftOffset = parseInt(dateWindow.left) + document.getElementById("spanYear").offsetLeft;

	if (ie)
	{
		leftOffset += 6
	}

	yearWindow.left = leftOffset;
	yearWindow.top = parseInt(dateWindow.top) + 26;
}

function constructCalendar()
{
	var aNumDays = Array(31,0,31,30,31,30,31,31,30,31,30,31)
	var dateMessage
	var startDate = new Date (yearSelected,monthSelected,1)
	var endDate

	if (monthSelected == 1)
	{
		endDate = new Date (yearSelected,monthSelected+1,1);
		endDate = new Date (endDate - (24*60*60*1000));
		numDaysInMonth = endDate.getDate()
	}
	else
	{
		numDaysInMonth = aNumDays[monthSelected];
	}

	datePointer = 0
	dayPointer = startDate.getDay() - 1;

	if (dayPointer < 0)
	{
		dayPointer = 6
	}

	html = "<table  border=0 style='font-family:verdana;font-size:10px;'><tr>"

	for (var i = 0; i < 7; i++)
	{
		html += "<td width='27' align='right'><B>"+ dayName[i]+"</B></td>"
	}
	html +="</tr><tr>"

	for (var i = 1; i <= dayPointer;i++)
	{
		html += "<td>&nbsp;</td>"
	}

	for (datePointer=1; datePointer<=numDaysInMonth; datePointer++ )
	{
		dayPointer++;
		html += "<td align=right onmouseover='this.style.backgroundColor=\"#FFCC99\"' onmouseout='this.style.backgroundColor=\"\"'>"
		sStyle=styleAnchor;

		if ((datePointer==defaultDate) && (monthSelected==defaultMonth) && (yearSelected==defaultYear))
		{
			sStyle+=styleLightBorder;
		}

		sHint = "";
		var regexp= /\"/g;
		sHint=sHint.replace(regexp,"&quot;");

		if ((datePointer==dateNow)&&(monthSelected==monthNow)&&(yearSelected==yearNow))
		{
			html += "<b><a title=\"" + sHint + "\" style='"+sStyle+"' href='javascript:dateSelected="+datePointer+";closeCalendar();'><font color=#ff0000>&nbsp;" + datePointer + "</font>&nbsp;</a></b>";
		}
		else if (dayPointer % 7 == 0)
		{
			html += "<a title=\"" + sHint + "\" style='"+sStyle+"' href='javascript:dateSelected="+datePointer + ";closeCalendar();'>&nbsp;<font color=#909090>" + datePointer + "</font>&nbsp;</a>";
		}
		else
		{
			html += "<a title=\"" + sHint + "\" style='"+sStyle+"' href='javascript:dateSelected="+datePointer + ";closeCalendar();'>&nbsp;" + datePointer + "&nbsp;</a>";
		}

		html += "";

		if (dayPointer % 7 == 0)
		{
			html += "</tr><tr>" ;
		}
	}

	document.getElementById("content").innerHTML   = html
	document.getElementById("spanMonth").innerHTML = "&nbsp;" + monthName[monthSelected] + "&nbsp;<IMG id='changeMonth' SRC='"+imageDirectory+"pop-drop-off.gif' WIDTH='12' HEIGHT='10' BORDER=0>"
	document.getElementById("spanYear").innerHTML = "&nbsp;" + yearSelected + "&nbsp;<IMG id='changeYear' SRC='"+imageDirectory+"pop-drop-off.gif' WIDTH='12' HEIGHT='10' BORDER=0>"
}

function popUpDate(button, input)
{
	if (dom)
	{
		if (dateInput == input)
		{
			if (isDateWindowVisible())
			{
				hideDateWindow();
			}
			else
			{
				showDateWindow();
			}
		}
		else
		{
			dateInput = input;

			if (isDateWindowVisible())
			{
				hideDateWindow();
			}

			displayDateWindow();
		}
	}
}

function isDateWindowVisible()
{
	if (!dateWindow)
	{
		return false;
	}
	else if (dateWindow.visibility == "hidden")
	{
		return false;
	}
	else
	{
		return true;
	}
}

function parseTimeInput()
{
	var formatSeparator = " ";
	var formatFragments = dateFormat.split(formatSeparator);

	if (formatFragments.length < 3)
	{
		formatSeparator = "/";
		formatFragments = dateFormat.split(formatSeparator);

		if (formatFragments.length < 3)
		{
			formatSeparator = ".";
			formatFragments = dateFormat.split(formatSeparator);

			if (formatFragments.length < 3)
			{
				formatSeparator = "-";
				formatFragments = dateFormat.split(formatSeparator);

				if (formatFragments.length<3)
				{
					formatSeparator = ""
				}
			}
		}
	}

	var tokensChanged = 0;

	if (formatSeparator != "")
	{
		dataFragments = dateInput.value.split(formatSeparator);

		for (var i = 0; i < 3; i++)
		{
			if ((formatFragments[i] == "d") || (formatFragments[i] == "dd"))
			{
				dateSelected = parseInt(dataFragments[i], 10);
				tokensChanged++;
			}
			else if ((formatFragments[i] == "m") || (formatFragments[i] == "mm"))
			{
				monthSelected = parseInt(dataFragments[i], 10) - 1;
				tokensChanged++;
			}
			else if (formatFragments[i] == "yyyy")
			{
				yearSelected = parseInt(dataFragments[i], 10);
				tokensChanged++;
			}
			else if (formatFragments[i] == "mmm")
			{
				for (var j = 0; j < 12; j++)
				{
					if (dataFragments[i] == monthName[j])
					{
						monthSelected = j;
						tokensChanged++;
					}
				}
			}
		}
	}

	if ((tokensChanged != 3) || isNaN(dateSelected) || isNaN(monthSelected) || isNaN(yearSelected))
	{
		dateSelected = dateNow;
		monthSelected = monthNow;
		yearSelected = yearNow;
	}

	defaultDate = dateSelected;
	defaultMonth = monthSelected;
	defaultYear  = yearSelected;
}

function displayDateWindow()
{
	dateWindow=(dom)?document.getElementById("dateWindow").style : ie? document.all.dateWindow : document.dateWindow

	monthWindow=(dom)?document.getElementById("selectMonth").style : ie? document.all.selectMonth : document.selectMonth

	yearWindow=(dom)?document.getElementById("selectYear").style : ie? document.all.selectYear : document.selectYear

	monthConstructed=false;
	yearConstructed=false;

	html="<span id='spanLeft' style='border-style:solid;border-width:1;border-color:#3366FF;cursor:pointer' onmouseover='swapImage(\"changeLeft\",\"pop-left-on.gif\");this.style.borderColor=\"#88AAFF\"' onclick='javascript:decMonth()' onmouseout='clearInterval(intervalID1);swapImage(\"changeLeft\",\"pop-left-off.gif\");this.style.borderColor=\"#3366FF\"' onmousedown='clearTimeout(timeoutID1);timeoutID1=setTimeout(\"StartDecMonth()\",500)' onmouseup='clearTimeout(timeoutID1);clearInterval(intervalID1)'>&nbsp<IMG id='changeLeft' SRC='"+imageDirectory+"pop-left-off.gif' width=10 height=11 BORDER=0>&nbsp</span>&nbsp;"
	html+="<span id='spanRight' style='border-style:solid;border-width:1;border-color:#3366FF;cursor:pointer' onmouseover='swapImage(\"changeRight\",\"pop-right-on.gif\");this.style.borderColor=\"#88AAFF\"' onmouseout='clearInterval(intervalID1);swapImage(\"changeRight\",\"pop-right-off.gif\");this.style.borderColor=\"#3366FF\"' onclick='incMonth()' onmousedown='clearTimeout(timeoutID1);timeoutID1=setTimeout(\"StartIncMonth()\",500)' onmouseup='clearTimeout(timeoutID1);clearInterval(intervalID1)'>&nbsp<IMG id='changeRight' SRC='"+imageDirectory+"pop-right-off.gif' width=10 height=11 BORDER=0>&nbsp</span>&nbsp"
	html+="<span id='spanMonth' style='border-style:solid;border-width:1;border-color:#3366FF;cursor:pointer' onmouseover='swapImage(\"changeMonth\",\"pop-drop-on.gif\");this.style.borderColor=\"#88AAFF\"' onmouseout='swapImage(\"changeMonth\",\"pop-drop-off.gif\");this.style.borderColor=\"#3366FF\"' onclick='popUpMonth()'></span>&nbsp;"
	html+="<span id='spanYear' style='border-style:solid;border-width:1;border-color:#3366FF;cursor:pointer' onmouseover='swapImage(\"changeYear\",\"pop-drop-on.gif\");this.style.borderColor=\"#88AAFF\"' onmouseout='swapImage(\"changeYear\",\"pop-drop-off.gif\");this.style.borderColor=\"#3366FF\"' onclick='popUpYear()'></span>&nbsp;"

	document.getElementById("caption").innerHTML  = html

	var element = dateInput;
	var offsetLeft = 0;
	var offsetTop = 0;

	do
	{
		element = element.offsetParent;
		offsetLeft  += element.offsetLeft;
		offsetTop += element.offsetTop;
	}
	while (element.tagName != "BODY");

	dateWindow.left = dateInput.offsetLeft + offsetLeft;

	dateWindow.top =
		dateInput.offsetTop +
		offsetTop +
		dateInput.offsetHeight +
		2;

	showDateWindow();
}

function showDateWindow()
{
	parseTimeInput();
	constructCalendar(1, monthSelected, yearSelected);

	if (document.onclick)
	{
		document.onclick();
	}

	hideAllElementsBehind("dateWindow");
	dateWindow.visibility = (dom||ie) ? "visible" : "show";
	dateIgnoreNextOnClick = true;
	document.onclick = dateOnClick;
	document.onkeypress = dateOnKeyPress;
}

function hideDateWindow()
{
	dateWindow.visibility = "hidden";

	if (monthWindow != null)
	{
		monthWindow.visibility = "hidden";
	}

	if (yearWindow != null)
	{
		yearWindow.visibility = "hidden";
	}

	showAllElements();
	document.onclick = null;
	document.onkeypress = null;
}

function dateOnKeyPress()
{
	if (event.keyCode == 27)
	{
		if (isDateWindowVisible())
		{
			hideDateWindow();
		}
	}
}

function dateOnClick()
{
	if (dateIgnoreNextOnClick)
	{
		dateIgnoreNextOnClick = false;
	}
	else if (isDateWindowVisible())
	{
		hideDateWindow();
	}
}
