//Select Option Generator Function
var rowNumber=0;
function addOption(e,text,value)
{
	e[e.length] = new Option(text, value, false, false)
}

//MultiSelect Option Transfer functions
function addOption(e,text,value){e[e.length] = new Option(text, value, false, false)}
function dropOption(e,index){e[index]=null;}
function pushAll(form,fieldName)
{
	var sourceName = fieldName+"_all";
	var targetName = fieldName+"_on";
	sendAll(form,sourceName,targetName);
	refreshList(form,fieldName,targetName);
}
function pullAll(form,fieldName)
{
	var sourceName = fieldName+"_on";
	var targetName = fieldName+"_all";
	sendAll(form,sourceName,targetName);
	refreshList(form,fieldName,sourceName);
}

function sendAll(form,sourceName,targetName)
{
	var source = form.elements[sourceName]
	var target = form.elements[targetName];
	for (var i=0; i<source.options.length; i++)
	{
		var value = source.options[i].value;
		var text = source.options[i].text;
		addOption(target,text,value);
	}
	source.length = 0;
	edited=true;
}
function pushObject(form,fieldName)
{
	var sourceName = fieldName+"_all";
	var targetName = fieldName+"_on";
	sendObject(form,sourceName,targetName);
	refreshList(form,fieldName,targetName);
}
function pullObject(form,fieldName)
{
	var sourceName = fieldName+"_on";
	var targetName = fieldName+"_all";
	sendObject(form,sourceName,targetName);
	refreshList(form,fieldName,sourceName);
}
function sendObject(form,sourceName,targetName)
{
	var source = form.elements[sourceName]
	var target = form.elements[targetName];
	var i = source.selectedIndex;
	if (i>-1)
	{
		var value = source.options[i].value;
		var text = source.options[i].text;
		addOption(target,text,value);
		dropOption(source,i);
	}
	edited=true;
}
function refreshList(form,fieldName, listName)
{
	var idList=""
	var field = form.elements[fieldName]
	var list = form.elements[listName]
	for (var i=0; i<list.options.length; i++)
		idList = idList+(i>0?",":"")+list.options[i].value;
	field.value = idList;
}
//Calendar UI functions and window generator
IE = false;
NS = false;
if (document.all) IE = true;
if (document.layers) NS = true;
MONTH = new Array('January','February','March','April','May','June','July','August','September','October','November','December');
DAY = new Array('S','M','T','W','T','F','S');
theDate = new Date(document.lastModified);
var	currentForm = null;
var	currentElem = null;
var	pulldown = false;

function overDay(d,withinMonth)
{
	if (withinMonth)
		d.style.backgroundColor='#9abcde';
	else
		d.style.backgroundColor='#abcdef';
	d.style.border = '1px white solid';
	d.style.color = '#ffffff';
}

function outDay(d,withinMonth)
{
	if (withinMonth)
	{
		d.style.backgroundColor='#DFDDD0';
		d.style.border = '1px #DFDDD0 solid';
	}
	else
	{
		d.style.backgroundColor='#EAE7D4';
		d.style.border = '1px #EAE7D4 solid';
	}

	d.style.color = '#000000';
}
function readDate()
{
	if (pulldown)
	{
		yElem = eval('document.forms[currentForm].elements["'+currentElem+'_y"]');
		mElem = eval('document.forms[currentForm].elements["'+currentElem+'_m"]');
		dElem = eval('document.forms[currentForm].elements["'+currentElem+'_d"]');
		var year = yElem.options[yElem.selectedIndex].value;
		var month = mElem.options[mElem.selectedIndex].value;
		var day = dElem.options[dElem.selectedIndex].value;
		theDate = new Date(year,month-1,day);
	}
	else
	{
		var date = (document.forms[currentForm].elements[currentElem].value).toString;
//		var i = date.indexOf('/');
//		var m = date.substring(1,i-1);
//		var i0 = i;
//		i = = date.indexOf('/',i+1);
//		var d = date.substring(i0,i-1);
//		var y = date.substring(i,date.length);
//		theDate = new Date(y,m,d);
	}
}
function chooseDay(day,month,year)
{
	if (pulldown)
	{
		yElem = eval('document.forms[currentForm].elements["'+currentElem+'_y"]');
		mElem = eval('document.forms[currentForm].elements["'+currentElem+'_m"]');
		dElem = eval('document.forms[currentForm].elements["'+currentElem+'_d"]');
		yElem.value = year;
		mElem.value = month;
		dElem.value = day;
		edited=true;
		hideWindow("calendarPopup");
	}
	else
	{
		document.forms[currentForm].elements[currentElem].value = month + '/'+day+'/'+year;
		edited=true;
		if (rowNumber > 0)
		{
			e = eval('document.forms[currentForm].elements["'+currentForm+'_edited_'+rowNumber+'"]');
			e.value = true;
		}
		hideWindow("calendarPopup");
	}
}
function prevMonth(){ theDate.setMonth(theDate.getMonth()-1);buildCalendar();}
function nextMonth(){ theDate.setMonth(theDate.getMonth()+1);buildCalendar();}

function buildCalendar()
{
	var cal = "<table cellpadding=0 cellspacing=1 style='border: 2px outset gold;background-color:909090;' >";
	cal += "<tr><td class='TableTitle'><a href='javascript:prevMonth();'><small><font color='white'>[<]</small></a></td><td colspan=5 class='TableTitle'>"+MONTH[theDate.getMonth()]+" ("+theDate.getYear()+")</td><td class='TableTitle'><a href='javascript:nextMonth();'><small><font color='white'>[>]</small></a></td></tr>";
	cal += "<tr>";for (i=0; i<7; i++) cal += "<td width=14 class='TableHeader'>"+DAY[i]+"</td>";cal += "</tr>";
	thisD = theDate.getDate();
	thisM = theDate.getMonth();
	thisY = theDate.getYear();
	firstDay = new Date(theDate.getYear(),theDate.getMonth(),1)
	if (firstDay.getDay() > 0) firstDay.setDate(-firstDay.getDay()+1);
	thisMonth = theDate.getMonth();
	theDay = firstDay;
	for (var r=0; r<6; r++)
	{
		cal += "<tr>";
		for (var c=0; c<7; c++)
		{
			d = theDay.getDate();
			m = theDay.getMonth();
			y = theDay.getYear();
			withinMonth = (m == thisMonth)?"0":"1";
			onDay = (d==thisD && y == thisY && m ==thisM);
			if (onDay)
				bg = '#9abcde';
			else	bg = (withinMonth==0?'#EAE7D4':'#DFDDD0');
			cal += "<td style='height:12;font-size:8pt;color:black;background-color:"+bg+";cursor:hand;border:1px "+(withinMonth?"#DFDDD0":"#EAE7D4")+" solid' onclick='chooseDay("+d+","+(m+1)+","+y+");'";
			if (!onDay) cal += "onmouseover='overDay(this,"+withinMonth+");' onmouseout='outDay(this,"+withinMonth+");'";
			cal += "align=center>";
			if (NS) cal +="<a href='javascript:chooseDay("+d+","+(m+1)+","+y+");'>"+theDay.getDate()+"</a></td>";
			else 	cal += theDay.getDate()+"</td>";
			theDay.setDate(theDay.getDate()+1);
		}
		cal += "</tr>";
	}
	cal += "</table>";
	innerHTML = cal;
	if (IE)	theCalendar.innerHTML = innerHTML;
	else	{theCalendar.document.open();theCalendar.document.write(innerHTML);theCalendar.document.close();}

	return cal;
}
var theCalendar = null;
var timer = null;
windowOffsetX = 25;
windowOffsetY = 15;
function overWindow()
{
	clearTimeout(timer);
}

function outWindow()
{
	closeWindow("calendarPopup");
}

function closeWindow(winName) {
	timer = setTimeout("hideWindow('"+winName+"');",500);
}

function hideWindow(winName) {
	if (IE)
	{
		win = document.all[winName];
		win.style.display = 'none';
	}
	else
	{
		win = document.layers[winName];
		win.visibility = 'hidden';
	}
}

function showCalendar(obj,form,elem,isPulldown,rowNum)
{
	if (isPulldown) pulldown = true;
	if (rowNum) rowNumber = rowNum;
	currentForm = form;
	currentElem = elem;
	x = window.event.clientX+document.body.scrollLeft;
	y = window.event.clientY+document.body.scrollTop;
//	x = obj.absX+25;
//	y = obj.absY+10;
	windowH = document.body.offsetHeight;
	windowW = document.body.offsetWidth;
	if (IE)
	{
		theCalendar = document.all["calendarPopup"];
		if (!theCalendar)
		{
			theCalendar = document.createElement('DIV');
			theCalendar.style.zIndex=1;
			theCalendar.style.position = 'absolute';
			theCalendar.id = "calendarPopup";
			theCalendar.onmouseover = overWindow;
			theCalendar.onmouseout = outWindow;
			theCalendar = document.body.appendChild(theCalendar);
//			if (calendar.absY2 > windowH) calendar.style.top = windowH-calendar.offsetHeight;
//			if (calendar.absX2 > windowW) calendar.style.left = windowW-calendar.offsetWidth-15;
		}
		theCalendar.style.display = 'block';
		theCalendar.style.left=x;
		theCalendar.style.top=y;
	}
	else
	{
		theCalendar = document.layers["calendarPopup"];
		if (!theCalendar)
		{
			theCalendar = new Layer('Calendar',window);
			theCalendar.onmouseover = overWindow;
			theCalendar.onmouseout = outWindow;
			theCalendar.left=x;
			theCalendar.top=y;
			theCalendar.zIndex=1;
		}
		theCalendar.visibility = 'show';
		theCalendar.left=x;
		theCalendar.top=y;
	}
	theDate = new Date();
	if (elem) readDate();
	buildCalendar();

//	df_hideElement("SELECT",calendar);
//	df_hideElement("OBJECT",calendar);
//	df_hideElement("IFRAME",calendar);
}
