/* Written by Barry Gervais October 15, 2002 - freely distributable
	This file Calendar.js, is used to provide client side functionality for
	the calendar that is included as part of the dcforum.
*/

// Global Strings
var MSG_ERR_DATES_NOT_SELECTED = "You must select both a month and a year prior to submitting\nPlease try again.";
var MSG_ERR_INCOMPLETE_NOTE_INFORMATION = "All required fields must be supplied, please provide information for the ";
var STR_INCOMPLETE_DAY = "day";
var STR_INCOMPLETE_TITLE = "title";
var STR_INCOMPLETE_MESSAGE = "message";
function setMonth(sDirection,iCurrentMonth,iCurrentYear)
{
	var iNewMonth = iCurrentMonth;
	var sURL = "";
	if( sDirection == "previous" )
	{	
		if( iCurrentMonth == 1 )iCurrentYear--;
	}
	else if( iCurrentMonth == 12 )iCurrentYear++;
	
	sURL += sServerCGIPath + "/dcboard.pl?az=" + azValue + "&sDirection=" + sDirection + "&dd=";
	if (iNewMonth < 10 ) sURL += "0" + iNewMonth;
	else sURL += iNewMonth;
	sURL += "_01_" + iCurrentYear;
	location.href = sURL;

}
function setCalendar()
{
	var sURL = "";
	var sUID = "";
	if( document.frm_calendar.select_month.selectedIndex > 0 && document.frm_calendar.select_year.selectedIndex > 0 )
	{
	
		sURL += sServerCGIPath + "/dcboard.pl?az=" + azValue;
		sURL += "&dd=" + document.frm_calendar.select_month.options[document.frm_calendar.select_month.selectedIndex].value + "_01_";
		sURL += document.frm_calendar.select_year.options[document.frm_calendar.select_year.selectedIndex].value;
		location.href = sURL;
	}
	else alert( MSG_ERR_DATES_NOT_SELECTED);
}
// calls calendar_add_event.pl
function addNote()
{
	var sURL = "";
	sURL += sServerCGIPath + "/dcboard.pl?az=calendar_add_event&notableMonth=" + iMonth + "&notableYear=" + iYear;	
	location.href = sURL;
}

// adds note to database
function addNoteToDataBase( sMode )
{
	var oForm = eval( document.frm_add_note );
	var bIsValidNote = true;
	var strMissingInformation = "";
	if ( oForm.select_day.selectedIndex < 0 )
	{
		 bIsValidNote = false;
		 strMissingInformation = STR_INCOMPLETE_DAY;
	}
	if ( trimString( oForm.frm_add_note_title.value ).toString().length < 1 )
	{
		bIsValidNote = false;
		strMissingInformation = STR_INCOMPLETE_TITLE;
	}
	if ( trimString( oForm.frm_add_note_message.value ).toString().length < 1 )
	{
		bIsValidNote = false;
		strMissingInformation = STR_INCOMPLETE_MESSAGE;
	}
	if( bIsValidNote )
	{
		sUID = new Date();
		if( sMode == "new" )
			oForm.frm_add_note_uid.value = ( sUID.getYear().toString() + sUID.getMonth().toString() + sUID.getDay().toString() + sUID.getSeconds().toString() + sUID.getMilliseconds().toString() );
		
		oForm.submit();
	}
	else
	{
		alert( MSG_ERR_INCOMPLETE_NOTE_INFORMATION + strMissingInformation );
	}
}
// deletes the item from the database
// by reusing modify and adding a flag.
function deleteNoteFromDataBase()
{
	 var oForm = eval( document.frm_add_note );
	oForm.frm_add_note_delete.value = "delete";

}
// shows the calendar detail
function showCalendarDetail(sURL)
{
	launchWindow( sURL );
}
//shows the project detail
function showProject(iRow)
{
	var sURL = "";
	sURL += sServerCGIPath + "/dcboard.pl?az=calendar_project&iRecord=" + iRow;	
	launchWindow( sURL );
}

// Projects to be launched in a new window.
var sWin;
function launchWindow( sURL )
{
	var FEATURES = "width=600,toolbars=no,scrolling=yes,top=50,left=50,height=200";
	 if(sWin == null)
	 	sWin = window.open(sURL,'',FEATURES);
	else
	{
		sWin.close();
		sWin = window.open(sURL,'',FEATURES);
	}
}

// Utility functions
//-----------------
// Function removes leading and trailing blank spaces from strings
function trimString(sStr)
{
	sStr = sStr.replace(/(^\s+)/,""); //leading
	sStr = sStr.replace(/(\s+$)/,""); //trailing
	return sStr;
}

