/**
 * Calendar display and manipulation class
 * - Initiates a calendar class object
 * - outputs inside table w/ and w/o links
 * - outputs in table as div or table
 *
 * @author Derek Mebus <mebusd@treetopsolutions.com>
 * @version 1.0 10/29/2002
 * @since 0.0
 * @package calendar
 * @copyright Tree Top Solutions
 *
 */

//name of form
var f_name = "calendarForm";
//name of display date field
var displayDate = "displayDate";
//name of event action field
var eventAction = "a";
//name of start date field
var s_date = "edate";
//name of end date field
var e_date = "edate_end";

//**********************************Functions for generating calendar*****************/

//color of div popover background
var popoverColor = "#000000";

////instance of date for current date (sets as today for default)
var gCur = new Date();
var ggWinCal;

//display type (table, div, popover)
var displayType = "div";

//coords of popup
var xCoord = 0;
var yCoord = 0;

/**
 * Builds calendar inside a table with days as links
 * - initializes calendar object
 * - sets display parameters
 * - triggers show() to display with days as links
 *
 * @return string
 * @access public
 * @param string p_item form object links change
 * @param string p_month month
 * @param string p_year year
 * @param string p_format date format
 */
function Build_Calendar_Links(p_item, p_month, p_year, p_format) {
	var p_WinCal = ggWinCal;
	gCal = new Calendar(p_item, p_WinCal, p_month, p_year, p_format);

	// Customize your Calendar here..
	gCal.gBGColor="white";
	gCal.gLinkColor="black";
	gCal.gTextColor="black";
	gCal.gHeaderColor="#58B14C";
	gCal.gDepth = this.gDepth;

	// Triggers show function with links)
	return gCal.show(1);
}

/**
 * Builds calendar inside a table with days not as links
 * - initializes calendar object
 * - sets display parameters
 * - triggers show() to display with days not as links
 *
 * @return string
 * @access public
 * @param string p_item form object links change
 * @param string p_month month
 * @param string p_year year
 * @param string p_format date format
 */
function Build_Calendar(p_item, p_month, p_year, p_format) {
	var p_WinCal = ggWinCal;
	gCal = new Calendar(p_item, p_WinCal, p_month, p_year, p_format);

	// Customize your Calendar here..
	gCal.gBGColor="white";
	gCal.gLinkColor="black";
	gCal.gTextColor="black";
	gCal.gHeaderColor="darkgreen";
	gCal.gDepth = this.gDepth;

	// Triggers show function as no links
	return gCal.show();
}

/**
 * Places generated calendar table with links inside a div layer
 * - triggers popIt() from div handler
 *
 * @return string
 * @access public
 * @param string p_item form object links change
 * @param string p_month month
 * @param string p_year year
 * @param string p_format date format
 * @see Build_Calendar_Links()
 * @refer popoverColor
 */
function reBuild_Calendar_Links( p_item, p_month, p_year, p_format, displayType ) {
    if (displayType=="div") popLayer( Build_Calendar_Links(p_item, p_month, p_year, p_format), "", xCoord, yCoord );
	else popLayer( Build_Calendar_Links(p_item, p_month, p_year, p_format), popoverColor, xCoord, yCoord );
}

/* Places generated calendar table without links inside a div layer
 * - triggers popIt() from div handler
 *
 * @return string
 * @access public
 * @param string p_item form object links change
 * @param string p_month month
 * @param string p_year year
 * @param string p_format date format
 * @see Build_Calendar()
 * @refer popoverColor
 */
function reBuild_Calendar( p_item, p_month, p_year, p_format, displayType ) {
    if (displayType=="div") popLayer( Build_Calendar(p_item, p_month, p_year, p_format), popoverColor, xCoord, yCoord );
	else popLayer( Build_Calendar(p_item, p_month, p_year, p_format), popoverColor, xCoord, yCoord );
}

/**
 * Returns calendar month with days as links and displays them switched by arguments[4]
 * - triggers Build()
 * - returns as table, table inside div, popover div - popLayer()
 * - if arguments[4]==popover then sets x and y coords
 *
 * @return string
 * @access public
 * @param string arguments[0] form object links change
 * @param string arguments[1] date
 * @param string arguments[2] month
 * @param string arguments[3] year
 * @param string arguments[4] date format
 * @param string arguments[5] calendar display type (table,div,popover)
 * @param string arguments[6] depth of selection (day,week)
 * @see Build_Calendar_Links()
 * @refer popoverColor
 * @refer gDepth
 * @refer xCoord
 * @refer yCoord
 */
 function show_calendar_links() {
     /* 
	    p_date : Date of the Month
		p_month : 0-11 for Jan-Dec; 12 for All Months.
		p_year	: 4-digit year
		p_format: Date format (mm/dd/yyyy, dd/mm/yy, ...)
		p_item	: Return Item.
	*/

	p_item = arguments[0];
	if (arguments[1] == "" || arguments[1] == null)
		p_date = new String(gCur.getDate());
	else
		p_date = arguments[1];
	if (arguments[2] == "" || arguments[2] == null)
		p_month = new String(gCur.getMonth());
	else
		p_month = arguments[2] - 1;
	if (arguments[3] == "" || arguments[3] == null)
		p_year = new String(gCur.getFullYear().toString());
	else
		p_year = arguments[3];
	if (arguments[4] == "" || arguments[4] == null)
		p_format = "MM/DD/YYYY";
	else
		p_format = arguments[4];
	if (arguments[5] == "" || arguments[5] == null)
		displayType = "div";
	else
		displayType = arguments[5];
	if (arguments[6] == null)
		this.gDepth = "day";
	else
		this.gDepth = arguments[6];

	//Set current date
	gCur.setDate(p_date); gCur.setMonth(p_month); gCur.setFullYear(p_year);

    switch (displayType) {
	    case "table" : default : return Build_Calendar_Links(p_item, p_month, p_year, p_format); break;
		case "div" : return Build_Calendar_Links(p_item, p_month, p_year, p_format); break;
		case "popover" : this.xCoord = x - 30;
	                     this.yCoord = y - 20; 
		                 return popMouseLayer( Build_Calendar_Links("hideLayer(1);"+p_item, p_month, p_year, p_format),popoverColor ); 
						 break;
	}
 }
 
/**
 * Returns calendar month with days not as links and displays them switched by arguments[4]
 * - triggers Build()
 * - returns as table, table inside div, popover div - popLayer()
 * - if arguments[4]==popover then sets x and y coords
 *
 * @return string
 * @access public
 * @param string arguments[0] form object links change
 * @param string arguments[1] date
 * @param string arguments[2] month
 * @param string arguments[3] year
 * @param string arguments[4] date format
 * @param string arguments[5] calendar display type (table,div,popover)
 * @param string arguments[6] depth of selection (day,week)
 * @see Build_Calendar()
 * @refer popoverColor
 * @refer gDepth
 * @refer xCoord
 * @refer yCoord
 */
 function show_calendar_nolinks() {
     /* 
	    p_date : Date of the Month
		p_month : 0-11 for Jan-Dec; 12 for All Months.
		p_year	: 4-digit year
		p_format: Date format (mm/dd/yyyy, dd/mm/yy, ...)
		p_item	: Return Item.
	*/

	p_item = arguments[0];
	if (arguments[1] == "" || arguments[1] == null)
		p_date = new String(gCur.getDate());
	else
		p_date = arguments[2];
	if (arguments[2] == "" || arguments[2] == null)
		p_month = new String(gCur.getMonth());
	else
		p_month = arguments[2] - 1;
	if (arguments[3] == "" || arguments[3] == null)
		p_year = new String(gCur.getFullYear().toString());
	else
		p_year = arguments[3];
	if (arguments[4] == "" || arguments[4] == null)
		p_format = "MM/DD/YYYY";
	else
		p_format = arguments[4];
	if (arguments[5] == "" || arguments[5] == null)
		displayType = "div";
	else
		displayType = arguments[5];
	if (arguments[6] == null)
		this.gDepth = "day";
	else
		this.gDepth = arguments[6];
	
	//Set current date
	gCur.setDate(p_date); gCur.setMonth(p_month); gCur.setFullYear(p_year);

	switch (displayType) {
	    case "table" : default : return Build_Calendar(p_item, p_month, p_year, p_format); break;
		case "div" : return Build_Calendar(p_item, p_month, p_year, p_format); break;
		case "popover" : xCoord = x;
	                     yCoord = y; 
						 return popMouseLayer( Build_Calendar("hideLayer(1);"+p_item, p_month, p_year, p_format),popoverColor ); 
						 break;
	}
}

//**********************************Functions for triggering calendar generation*****************/

/**
 * Changes view of calendar
 * - change values (month,week,day)
 *
 * @return Boolean
 * @access public
 * @param string sw calendar view switch value
 * @param string action name of form containing calendar values or function for popover if applicable
 * @param string links bit flag for displaying days as links
 * @param string disp determines display type (0 for default)
 * @param string arguments[4] name of display date element (not applicable for popover)
 * @param string arguments[5] name of display view element (not applicable for popover)
 * @param string arguments[6] color of border, if set create an ouline and makes border = 0
 */
function showCalendar(div,action,links,disp) {
    //set popover div layer to use
    divLayer = div;
	
	//if viewing in div popover => display date is Today
	if (disp == "popover") {
	    calendarWidth = "190";
		//set form reference
	    var f = ( action.match(/\(/) ) ? action : "document."+action;
		//return generated calendar
	    if (links == 1) return show_calendar_links(f,"","","","","popover");
		else return show_calendar(f,"","","","","popover");
    //if not viewing in popover => set up display date
	} else {
	    //grab date from form
	    displayDate = eval("document."+showCalendar.arguments[4]+".value");
		var date = displayDate.split("/");
	}

	var display = (disp==0) ? "" : disp;
	var sw = eval ("document."+showCalendar.arguments[5]+".value");

	//if there are to be links
	if (links == 1) var calendar = show_calendar_links(action,date[1],date[0],date[2],"",display,sw);
	//if there are to be no links
	else var calendar = show_calendar(action,date[1],date[0],date[2],"",display,sw);
	
	//create an outline border if triggered
	if (arguments[6] != null) {
	    popoverColor = arguments[6];
	    var output = "<TABLE BORDER=\"0\" BGCOLOR=\""+popoverColor+"\" CELLPADDING=\"0\" CELLSPACING=\"0\" WIDTH=\""+(calendarWidth + 2)+"\">";
		output += "<TR><TD>";
		output += "<TABLE BORDER=\"0\" CELLPADDING=\"3\" CELLSPACING=\"0\" WIDTH=\"100%\"><TR><TD>"; 
		output += calendar + "</TD></TR></TABLE>";
		output += "</TD></TR></TABLE>";
		return output;
	} else return calendar;
}

/**
 * Pops up a layer containing calendar for selecting date
 * - triggers popLayer()
 *
 * @return Boolean
 * @access public
 * @param string sw calendar view switch value
 * @param string form name of form containing calendar values or function for popover if applicable
 * @param string links bit flag for displaying days as links
 * @param string xPtr x-coordinate for popover layer
 * @param string yPtr y-coordinate for popover layer
 */
function showPopoverCalendar(div,form,links,xPtr,yPtr) {
    divLayer = div;
	//set form reference
	var f = ( form.match(/\(/) ) ? form : "document."+form;
	//generate calendar
	var calendar = (links == 1) ? show_calendar_links(f,"","","","","table")
	                            : show_calendar(f,"","","","","table");
    
	//set coordinates of calendar poup
	xCoord = xPtr;
	yCoord = yPtr;
	
	//pop layer with calendar
	return popLayer(calendar,popoverColor,xPtr,yPtr);
}

//**********************************Functions for modifying calendar*****************/

/**
 * Controls interaction with month navigation
 * - triggers Build()
 * - returns as table, table inside div, popover div - popLayer()
 * - if arguments[4]==popover then sets x and y coords
 *
 * @return string
 * @access public
 * @param string ReturnItem form item effected by calendar interaction
 * @param string Month display month
 * @param string Year display year
 * @param string Format display format
 * @param string Links whether to rebuild calendar with or without links
 * @see reBuild_Calendar_Links()
 * @see reBuild_Calendar()
 * @refer displayType
 */
 function changeMonth(ReturnItem,Month,Year,Format,Links) {
     //if resetting to today
     if (Month==0 && Year==0) {
	     gCur = new Date();
	     Month = gCur.getMonth();
		 Year = gCur.getFullYear().toString();
	 }
	 //rebuilding calendar
     if (Links == 1) reBuild_Calendar_Links(ReturnItem,Month,Year,Format,displayType);
	 else reBuild_Calendar(ReturnItem,Month,Year,Format,displayType);
 }
 
/**
 * Changes view of calendar
 * - change values (month,week,day)
 *
 * @return Boolean
 * @access public
 * @param string sw calendar view switch value
 * @param string form datePtrEl of form containing calendar values
 * @param string arguments[>1] any addition commands to be executed
 */
function changeDateSW(sw,dateSW_El) {
    //set date value
    eval("document." + f_name + "." + dateSW_El + ".value = '" + sw + "'");
	//submit
	eval("document."+f_name+".submit()");
	return;
}

/**
 * Changes display date
 * - change value
 *
 * @return Boolean
 * @access public
 * @param string date new display date
 * @refer eventAction
 */
function changeDate(date) {
    //set date value
    eval("document."+f_name+"."+eventAction+".value = 'd-"+date+"'");
    //submit
	eval("document."+f_name+".submit()");
    return;
}

/******************Non-Calendar Functions for Calendar Page****************************/
/**
 * Sets id for viewing event's full information
 *
 * @return string
 * @access public
 * @refer f_name
 * @refer eventAction
 */
function getInfo(id) {
    eval("document."+f_name+"."+eventAction+".value='i-"+id+"';");
	eval("document."+f_name+".submit()");
	return;
}

/**
 * Triggers update of calendar view for filters
 *
 * @return Boolean
 * @access public
 * @param
 */
function filterAction(filter,val,url) {
    switch (filter) {
	    case "type" : var a = "t"; break;
	}
	document.location.href = url + a + "-" + val;
	return;
}

/**
 * Changes end date to new value of event date
 *
 * @return string
 * @access public
 * @param string $s_date start date field
 * @param string $e_date end date field
 * @refer f_name
 * @refer s_date
 * @refer e_date
 */
function changeStartDate(date) {
    eval("document."+f_name+"."+s_date+".value = '"+date+"'");
	var start_arr = date.split("/");
    var end = eval("document."+f_name+"."+e_date+".value");
	
	//hide calendar popup
	overdiv=0;
	setTimeout('hideLayer(0)',100);
	
	//if no end date is set => set as start
	if (end == "") {
	    eval("document."+f_name+"."+e_date+".value = '"+date+"'");
	//if end date is less than start date, set it to start date
	} else {
    	var end_arr = end.split("/"); 
		var end_arg = end_arr[2] + end_arr[0] + end_arr[1];
		var start_arg = start_arr[2] + start_arr[0] + start_arr[1];
	    if (end_arg < start_arg)
		    eval("document."+f_name+"."+e_date+".value = '"+date+"'");
	}
   	return;
}

/**
 * Checks end date
 * - that end date is not before Today's Date
 * - that end date is not less than start date
 *
 * @return string
 * @access public
 * @param string $s_date start date field
 * @param string $e_date end date field
 * @refer f_name 
 * @refer s_date
 * @refer e_date
 * @refer gCur
 */
function changeEndDate(val) {	
	//hide calendar popup
	overdiv=0;
    setTimeout('hideLayer(0)',100);

    //check that end date is not before Today
	var val_arr = val.split("/");
	var val_arg = val_arr[2] + val_arr[0] + val_arr[1];
	var today_arg = "" + gCur.getFullYear() +  
	                ( (gCur.getMonth() + 1 > 9) ? (gCur.getMonth() + 1) : "0" + (gCur.getMonth() + 1) ) +
					( (gCur.getDate() > 9) ? gCur.getDate() : "0" + gCur.getDate() );
	
	/*if (val_arg < today_arg) {
	    alert("You have entered an end date that is before today");
   		return;
	}*/
	
    //if no start date has been entered, set it to end date
	var start = eval("document."+f_name+"."+s_date+".value");
	if (start == "") {
	    eval("document."+f_name+"."+s_date+".value = '"+val+"'");
		eval("document."+f_name+"."+e_date+".value = '"+val+"'");
		return;
	}

    //check that end date is not before start date
	var start_arr = start.split("/");
	var start_arg = start_arr[2] + start_arr[0] + start_arr[1];
    if (val_arg < start_arg)  {
        alert("You have entered an end date that is before the event date");
		eval("document."+f_name+"."+e_date+".value = '"+start+"'");
	} else {
	    eval("document."+f_name+"."+e_date+".value = '"+val+"'");
	}
	return;
}
