var g_resultFld;

function replyComment(ParentID, ReplyTitle)
{
	var frm = document.forms[0];

	// find title control
	for (var x = 0; x < frm.length; x++)
	{
		if (frm[x].name.substr(frm[x].name.indexOf(":")) == ":txtTitle")
		{
			var titleField = frm[x];
		}
	}

	// skip to entry form
	document.location.hash='newcomment';

	// set parent ID and reply title
	document.forms[0].txtParentID.value = ParentID;
	if (titleField)
	{
		if (ReplyTitle.search(/re:/i) > -1)
			titleField.value = ReplyTitle;
		else
			titleField.value = "re: " + ReplyTitle;
		titleField.focus();
	}
}

/*
===============================================
			openCalendar
===============================================
PURPOSE:	Opens the calendar pop-up 
			
INPUTS:		resultFld - reference to HTML object
			to receive results of date selection
			
RETURNS:	NONE.
===============================================
*/	
function openCalendar(resultFld)
{
	var ourdate;
	var iDay;
	var iMonth;
	var iYear;

	//alert(' Debug 1 ');
	cal.style.display = "";
	cal.style.pixelTop = window.event.y + document.body.scrollTop;
	cal.style.pixelLeft = window.event.x + document.body.scrollLeft;

	//alert(' Debug 2 ');
	if (resultFld.value != "")
	{
		//alert(' Debug 2.1 (resultFld.value != "")');
		
		// assume d/mm/yyyy date format and parse string directly
		// rather than relying on client-side format
		var dateval = resultFld.value;
		//alert(' Debug 2.1.1 dateval:' + dateval);
		
		var iSlashOne = dateval.indexOf("/");
		
		//alert('iSlashOne: ' + iSlashOne);
		
		if(iSlashOne==-1)
		{
			//alert('iSlashOne==-1');
			var newDate = new Date(dateval)
			//alert('newDate: ' + newDate);
			
			iDay = newDate.getDate();
			//alert('iDay: ' + iDay);
			
			iMonth = newDate.getMonth()+1;
			//alert('iMonth: ' + iMonth);
			
			iYear = newDate.getFullYear();
			//alert('iYear: ' + iYear);
			
		} else {
			//alert(' Debug 2.1.2 iSlashOne:' + iSlashOne);
			//alert(' Debug 2.1.3 iSlashTwo:' + iSlashTwo);
			//alert(' Debug 2.1.4 iDay:' + iDay);
			//alert(' Debug 2.1.5 sMonth:' + sMonth);
			//alert(' Debug 2.1.6 iMonth:' + iMonth);
			//alert(' Debug 2.1.7 iYear:' + iYear);
			var iSlashTwo = dateval.indexOf("/", iSlashOne + 1);
			iDay = dateval.substr(0,iSlashOne);
			sMonth = dateval.substr(iSlashOne + 1, iSlashTwo - iSlashOne - 1);
			iMonth = eval(sMonth);
			iYear = resultFld.value.substr(iSlashTwo + 1,4);
		}

		}
	else
	{
		//alert(' Debug 2.2.1 (resultFld.value = ""): ' + resultFld.value);
		
		ourdate = new Date();
		//alert(' Debug 2.2.2 ourdate: ' + ourdate);
		
		iDay = ourdate.getDate();
		//alert(' Debug 2.2.3 iDay: ' + iDay);
		
		iMonth = ourdate.getMonth() + 1;
		//alert(' Debug 2.2.4 iMonth: ' + iMonth);
		
		iYear = ourdate.getFullYear();
		//alert(' Debug 2.2.5 iYear: ' + iYear);
		
	}		

	//alert(' Debug 3 ');
	cal.day = iDay;
	
	//alert(' Debug 3.1 iMonth: ' + iMonth);
	cal.month = iMonth;
	
	//alert(' Debug 3.2 iYear: ' + iYear);
	cal.year = iYear;
	
	//alert(' Debug 3.3 ');
	//alert(' Debug 4 ');

	g_resultField = resultFld;
	
//alert(' Debug 5 ');


}

/*
===============================================
			closeCal
===============================================
PURPOSE:	sets the relevant field to the
			chosen date and closes the calendar
			
INPUTS:		NONE. Uses global g_resultField var.

RETURNS:	NONE.
===============================================
*/	
function closeCal()
{
	g_resultField.value = dateFormat("dd MMM yyyy", cal.year, cal.month - 1, cal.day);
	cal.style.display = "none";
}

/*
===============================================
			dateFormat
===============================================
PURPOSE:	returns a passed date in a desired format
			
INPUTS:		format - format to set date
	
			yr, mth, dy - year, month and day to format

RETURNS:	formatted date / ""
===============================================
*/	
function dateFormat(format, yr, mth, dy) {
	var Months = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
	var ShortMonths = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");


	if (yr == null) date = new Date();
	else date = new Date(yr, mth, dy);

	// get date
	var d_dd, d_MM, d_MMMM, d_yyyy;
	d_dd = pad(date.getDate(), 2, 0, true);
	d_MMMM = Months[date.getMonth()];
	d_MMM = ShortMonths[date.getMonth()];
	d_MM = pad(date.getMonth() + 1, 2, 0, true);
	d_yyyy = pad(date.getFullYear(), 4, 0, true);

	// get time
	var t_hh, t_mm, t_ss;
	t_hh = pad(date.getHours(), 2, 0, true);
	t_mm = pad(date.getMinutes(), 2, 0, true);
	t_ss = pad(date.getSeconds(), 2, 0, true);
		
	// do the replacing
	var str = new String(format);
	var re = /dd/;
	str = str.replace(re, d_dd);
		
	str = str.replace(/MMMM/, d_MMMM);
	str = str.replace(/MMM/, d_MMM);
	str = str.replace(/MM/, d_MM);
	str = str.replace(/yyyy/, d_yyyy);

	str = str.replace(/mm/, t_mm);
	str = str.replace(/hh/, t_hh);
	str = str.replace(/ss/, t_ss);
	
	if (date.valueOf()) {
		return str;
	} else {
		return "";
	}
		
}


/*
===============================================
			pad
===============================================
PURPOSE:	pads a passed string with specified 
			character to specified length
			
INPUTS:		str - string to be padded
			len - desired output length (default = 2)
			ch - character to use for padding (default = "0")
			pre - boolean to prepend/append (default = true => prepend)

RETURNS:	Padded result
===============================================
*/	
function pad(str, len, ch, pre) {
	var newstr = new String(str);
	if (len == null) len = 2;
	if (ch == null) ch = 0;
	if (pre == null) pre = true;
	
	var diff = len - newstr.length;

	for (i = 0; i < diff; i++) {
		if (pre) newstr = ch + newstr;
		else newstr = newstr + ch;
	}

	return newstr;
}