/* PopUp Calendar v2.1
	© PCI, Inc.,2000 • Freeware
	webmaster@personal-connections.com
	+1 (925) 955 1624
	Permission granted  for unlimited use so far
	as the copyright notice above remains intact. 
*/

/* Settings. Please read readme.html file for instructions*/
var g_pcDF = "d/m/Y";
var g_pcMN = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
var g_pcER = new Array(4);
g_pcER[0] = "Required DHTML functions are not supported in this browser.";
g_pcER[1] = "Target form field is not assigned or not accessible.";
g_pcER[2] = "Sorry, the chosen date is not acceptable. Please read instructions on the page.";
g_pcER[3] = "Unknown error occured while executing this script.";




var g_pcDebug = null;
var g_pcTimerHandle=null;
var g_pcTarget=null;
var g_pcRules=null;	
var g_pcSelection=new Date();


// The main interface : 
//		target	-	The form element to act upon, e.g. document.myform.mytextinput
//                         event      -                 The event object that caused this popup to appear.
//		rules	-	pass in a validation function (day, month, year) that returns true for valid dates, or null for all dates to be valid
//		debug	-	a textarea control to receive the html output generated.  Useful for debugging stylesheets.
//
function getCalendarFor(target, event, rules, debug) 
{
        event = event || window.event;
	if (!document.getElementById)
	{
		showError(g_pcER[0]);
		return;
	}
	if (!target)
	{
		showError(g_pcER[1]);
		return;
	}
	if (!g_pcTarget)
	{	// first time in
		var now = new Date();
		setCalendar(now.getFullYear(), now.getMonth());
		setSelectList(now.getFullYear(), now.getMonth());
	}
	g_pcTarget	=	target;
	g_pcRules	=	rules;
	g_pcDebug	=	debug
	var obj = document.getElementById('PopUpCalendar');
        if (event)
        {
            obj.style.left			= document.body.scrollLeft	+	event.clientX;
            obj.style.top			= document.body.scrollTop	+	event.clientY;
        }
        else
        {
            obj.style.left			= document.body.scrollLeft	+	200;
            obj.style.top			= document.body.scrollTop	+	200;
        }
   	obj.style.visibility	= "visible";
}

function switchMonth(param) 
{
	var tmp = param.split("|");
	setCalendar(tmp[0],tmp[1]);
}

function moveMonth(dir) 
{
	var obj = document.ppcMonthList.sItem;
	if (obj == null) 
	{
		showError(g_pcER[3]);
	}

	if ((dir.toLowerCase() == "back")&&(obj.selectedIndex > 0)) 
	{
		obj.selectedIndex--;
	}
	else if ((dir.toLowerCase() == "forward")&&(obj.selectedIndex < 12)) 
	{
		obj.selectedIndex++;
	}
	else 
	{
		obj.style.backgroundColor = "#FF0000";
		window.setTimeout("document.ppcMonthList.sItem.style.backgroundColor = '#FFFFFF'", 100);
		return;
	}
	var tmp = obj.options[obj.selectedIndex].value.split("|");
	setCalendar(tmp[0],tmp[1]);
}

function selectDate(param) 
{
	var arr   = param.split("|");
	var date  = arr[2];
	var ptr = parseInt(date);
	g_pcSelection.setDate(ptr);
	g_pcTarget.value = dateFormat(arr[0], arr[1], date);hideCalendar();
}
function setCalendar(year,month) 
{
	g_pcSelection.setFullYear(year);
	g_pcSelection.setMonth(month);
	g_pcSelection.setDate(1);
	updateContent();
}
function updateContent() 
{
	var content = generateContent();

	document.getElementById('monthDays').innerHTML = content;
	if (g_pcDebug != null)
	{
		g_pcDebug.value = content;
	}
}
function generateContent() 
{
	var year			=	g_pcSelection.getFullYear();
	var month			=	g_pcSelection.getMonth();
	var date			=	1;
	var day				=	g_pcSelection.getDay();
	var monthlengths	=	new Array(31, isLeap(year) ? 29 : 28,31,30,31,30,31,31,30,31,30,31);
	var len				=	monthlengths[month];
	var content = "<table cellspacing='1'>\n";
	for (var j = 0; j < 7 && date <= len; ++j) 
	{
		var tmp = "";
		for (var i = 0; i < 7; ++i) 
		{
			if (((j == 0)&&(i < day)) || (date > len) || !validDate(date, month, year)) 
			{
				tmp  += "    <td class='noday'>&nbsp;</td>\n";
			}
			else 
			{
				tmp  += makeCell(((i == 0)||(i == 6)),year,month,date);++date;
			}
		}
		content += "  <tr>\n" + tmp + "</tr>\n";
	}
	content += "</table>\n";
	return content;
}

function makeCell(weekend,year,month,date) 
{
	var param = "\'"+year+"|"+month+"|"+date+"\'";
	var cls = "";
	if (weekend)
	{
 		cls = " class='wend'";
	}
	var now = new Date();
	if ((now.getDate() == date) && (now.getMonth() == month) && (now.getFullYear() == year)) 
	{
		cls = " class='today'";
	}
	return "    <td><a href=\"javascript:selectDate("+param+");\"" + cls + ">" + date + "</a></td>\n";
}

function setSelectList(year,month) 
{
	var obj = document.ppcMonthList.sItem;
	for (var i = 0; i < 13; ++i)
	{
		obj.options[i].value = year + "|" + month;
		obj.options[i].text  = year + " • " + g_pcMN[month];
		month++;
		if (month == 12) 
		{
			year++;
			month = 0;
		}
	}
}

function hideCalendar() 
{
	document.getElementById('PopUpCalendar').style.visibility = "hidden";
	g_pcTimerHandle = null;
	g_pcTarget = null;
	obj = document.ppcMonthList.sItem.selectedIndex = 0;
}

function showError(message) 
{
	window.alert("[ PopUp Calendar ]\n\n" + message);
}

function isLeap(year) 
{
	return ((year%400==0)||((year%4==0)&&(year%100!=0)));
}

function validDate(date, month, year) 
{
	return g_pcRules == null ? true : g_pcRules(date);
}

function dateFormat(year,month,date) 
{
	var weeknames=new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
	if (g_pcDF == null) 
	{
		g_pcDF = "d/m/Y";
	}
	var day = g_pcSelection.getDay();
	var crt = "";
	var str = "";
	var chars = g_pcDF.length;
	for (var i = 0; i < chars; ++i) 
	{
		crt = g_pcDF.charAt(i);
		switch (crt) 
		{
			case "M": str += g_pcMN[month]; break;
			case "m": str += (month<9) ? ("0"+(++month)) : ++month; break;
			case "Y": str += year; break;
			case "y": str += year.substring(2); break;
			case "d": str += ((g_pcDF.indexOf("m")!=-1)&&(date<10)) ? ("0"+date) : date; break;
			case "W": str += weeknames[day]; break;
			default: str += crt;
		}
	}
	return unescape(str);
}
