﻿
	
	
//Calendar functions
//START
	//<summary>update the date</summary>
	function selected(cal, date) {
		cal.sel.value = date; // just update the date in the input field.
		cal.callCloseHandler();
	}
	
	//<summary>hide the calendar</summary>
	function closeHandler(cal) {
		cal.hide();                        
	}
	
	//<summary>find the previous input item and show the calendar for it</summary>
	function showCalendarForPrevious(link)
	{
		var el = PrevInputHandle(link);
		showCalendarForElement(el);
	}
	
	//<summary>show calendar for item with id that is passed</summary>
	function showCalendar(id) 
	{
		var el = document.getElementById(id);
		showCalendarForElement(el);
		
	}
	
	//<summary>show calendar for element that is passed</summary>
	function showCalendarForElement(el)
	{
		var fullElement;
		
		if (calendar != null) {
			calendar.hide();                 // so we hide it first.
		} else {
			var cal = new Calendar(false, null, selected, closeHandler);
			calendar = cal;                // remember it in the global var
			cal.setRange(1900, 2020);        // min/max year allowed.
			cal.create();
		}

		//calendar.setDateFormat();    // set the specified date format
		//alert(document.getElementById('_ctl0_ContentPlaceHolder1_' + el +'').value);
		
		fullElement = document.getElementById('ctl00_ContentPlaceHolder1_' + el +'');       
		calendar.parseDate(fullElement.value);      // try to parse the text in field
		calendar.sel = fullElement;                 // inform it what input field we use
		calendar.showAtElement(fullElement);        // show the calendar below it
		// return false;
	}
	
	//<summary>NOT SURE WHY THIS IS HERE</summary>
	function isDisabled(date) {
		var today = new Date();
		return (Math.abs(date.getTime() - today.getTime()) / DAY) > 10;
	}

//<summary>create a date object from the passed input fields or today if wrong fields passed</summary>
//<input type="string">target date input element</input>
//<input type="string">target time input element</input>
//<returns>javascript Date() object containing the new date</returns>
function getDateFromInputs(targetDateHandle,targetTimeHandle)
{
	var activeDate;
	var today = new Date();
	//if these objects have been correctly passed
	if(targetDateHandle != null && targetTimeHandle != null)
	{
		if(targetDateHandle.value.length == 10)	
		{//if the date is a 10 character data (assume yyyy/mm/dd)
			if(targetTimeHandle.value.length == 5) 
			{//if the time is a 5 character time (assume hh:mm)
				//create new date passing year, month - 1 (it is 0 based), day, hour, minute
				activeDate = new Date(targetDateHandle.value.substring(0,4), targetDateHandle.value.substring(5,7) - 1, targetDateHandle.value.substring(8,10), targetTimeHandle.value.substring(0,2), targetTimeHandle.value.substring(3,5));
			}
			else
			{//the time is not correctly formatted get Date object based on date only
				//create new date passing year, month - 1 (it is 0 based), day, today's time hour, today's time minutes
				activeDate = new Date(targetDateHandle.value.substring(0,4), targetDateHandle.value.substring(5,7) - 1, targetDateHandle.value.substring(8,10), today.getHours(), today.getMinutes());
			}
		}
		else	
		{//the date is not long enough (probably not correctly formatted). get generic date object
			activeDate = new Date();
		}
	}
	else
	{//the date and time handles were not passed correctly. get the generic date object
		activeDate = new Date();
	}
	
	return activeDate;	
}

//<summary>create the string representation of the current time</summary>
//<input type="element">element containing desired date</input>
//<input type="element">element containing desired time</input>
//<input type="string">hours offset (for defaults)</input>
//<returns>string time from dates passed or today</returns>
function getCurrentTimeString(targetDate, targetTime, offsetHours) 
{
	var today = getDateFromInputs(targetDate, targetTime);
	
	var date = new Date(today.getYear(), today.getMonth(), today.getDate(), today.getHours() + offsetHours, today.getMinutes(), today.getSeconds());
	var hours = date.getHours();
	var minutes = date.getMinutes();
	hours = ((hours < 10) ? "0" : "") + hours;
	minutes = ((minutes < 10) ? "0" : "") + minutes;
		
	var timeValue = "" + hours + ":" + minutes;
	
	return timeValue;
}

function getCurrentDateString(targetDate, targetTime, offsetHours)
{
	var today = getDateFromInputs(targetDate, targetTime);
	
	var date = new Date(today.getYear(), today.getMonth(), today.getDate(), today.getHours() + offsetHours, today.getMinutes(), today.getSeconds());
	var month = date.getMonth() + 1;
	var day = date.getDate();
	var year = date.getYear();
	year = ((year < 10) ? "0" : "") + year;
	month = ((month < 10) ? "0" : "") + month;
	day = ((day < 10) ? "0" : "") + day;
	
	var dateValue = "" + year + "-" + month + "-" + day;
	
	return dateValue;
}

function SetPreviousTwoInputs(currentElement,value1, value2)
{
	//alert("SetPreviousTwoInputs");
	//get previous input to current element
	var input2 = PrevInputHandle(currentElement);
	//get previous input to input2
	var input1 = PrevInputHandle(input2);
	//update inputs
	input1.value = value1;
	input2.value = value2;
}
//END Calendar functions


