//calendar
var DHTML_SUITE_THEME = 'blue';
var DHTML_SUITE_THEME_FOLDER = '/core/javascript/calendar/themes/';
var DHTML_SUITE_JS_FOLDER = '/core/javascript/calendar/js/';

function displayCalendar(strId, objCal, intOffsetX, intOffsetY)
{
	if(typeof(intOffsetX) == 'undefined')
		intOffsetX = 0;

	if(typeof(intOffsetY) == 'undefined')
		intOffsetY = 0;

	objInput 		= document.getElementById(strId+'_date');
	objInputHidden 	= document.getElementById(strId);
	objCal.setCalendarPositionByHTMLElement(strId+'_date',0+intOffsetX,objInput.offsetHeight+2+intOffsetY);	// Position the calendar right below the form input
	objCal.setInitialDateFromInput(objInputHidden,'yyyymmddhhii');	// Specify that the calendar should set it's initial date from the value of the input field.
	if(objCal.isVisible()){
		objCal.hide();
	}else{
		objCal.resetViewDisplayedMonth();	// This line resets the view back to the inital display, i.e. it displays the inital month and not the month it displayed the last time it was open.
		objCal.display();
	}
}	

/* arrInput is an associative array with the properties
year
month
day
hour
minute
calendarRef - Reference to the DHTMLSuite.calendar object.
*/
function getDateFromCalendar(arrInput)
{
	objInput = document.getElementById(arrInput.calendarRef.id+'_date');
	objInputHidden = document.getElementById(arrInput.calendarRef.id);
			
	objInput.value = arrInput.month + '-' + arrInput.day + '-' + arrInput.year;
	objInputHidden.value = arrInput.year + arrInput.month + arrInput.day;
	
	update_date_time_field(arrInput.calendarRef.id);
	
	if(objInput.onchange)
		objInput.onchange();
	if(objInputHidden.onchange)
		objInputHidden.onchange();
	
	arrInput.calendarRef.hide();			
}	

function prefil_date_time_field(strHiddenDateTimeFieldId)
{
	if(typeof(document.getElementById(strHiddenDateTimeFieldId+'_date')) != 'undefined')
	{
		__prefil_date_field(strHiddenDateTimeFieldId);
	}
	
	if(__has_time_field(strHiddenDateTimeFieldId))
	{
		__prefil_time_field(strHiddenDateTimeFieldId);
	}	
}


function __prefil_date_field(strHiddenDateTimeFieldId, strFormat)
{
	if(typeof(strFormat) == 'undefined')
	{
		strFormat = "m-d-Y";
	}

	strDateTime = document.getElementById(strHiddenDateTimeFieldId).value;

	if ( strDateTime.length >= 8 )
	{
		if(strFormat == "Y-m-d")
		{
			strDate = strDateTime.substr(0,4) + '-' + strDateTime.substr(4,2) + '-' + strDateTime.substr(6,2);
		}
		else if(strFormat == "m-d-Y")
		{
			strDate = strDateTime.substr(4,2) + '-' + strDateTime.substr(6,2) + '-' + strDateTime.substr(0,4);
		}
		
		document.getElementById(strHiddenDateTimeFieldId+'_date').value = strDate;
	}
}


function __prefil_time_field(strHiddenDateTimeFieldId)
{
	strDateTime = document.getElementById(strHiddenDateTimeFieldId).value;
	
	if ( strDateTime.length >= 8 )
	{
		strTimeHour = strDateTime.substr(8,2);
		strTimeMin 	= strDateTime.substr(10,2);

		if ( strTimeHour >= 12 )
		{
			if ( strTimeHour != 12 )
				strTimeHour = strTimeHour - 12;
			strTimeAmPm = 'pm';
		}
		else
		{
			if ( strTimeHour == 0 )
				strTimeHour = 12;
				
			strTimeAmPm = 'am';
		}
		
		select_dropdown_value(document.getElementById(strHiddenDateTimeFieldId + '_time_hh'), strTimeHour);
		select_dropdown_value(document.getElementById(strHiddenDateTimeFieldId + '_time_mm'), strTimeMin);
		select_dropdown_value(document.getElementById(strHiddenDateTimeFieldId + '_time_ampm'), strTimeAmPm);
	}
}


function update_date_time_field(strHiddenDateTimeFieldId)
{
	if(__has_time_field(strHiddenDateTimeFieldId))
	{
		strFieldTimeHour  = strHiddenDateTimeFieldId + '_time_hh';	
		strFieldTimeMin	  = strHiddenDateTimeFieldId + '_time_mm';	
		strFieldTimeAmPm  = strHiddenDateTimeFieldId + '_time_ampm';	
		
		strTimeHour	 = parseFloat(document.getElementById(strFieldTimeHour).value);
		strTimeMin	 = parseFloat(document.getElementById(strFieldTimeMin).value);
		strTimeAmPm  = document.getElementById(strFieldTimeAmPm).value;		
		
		if ( strTimeAmPm == 'pm' )
		{
			if ( strTimeHour < 12 )
				strTimeHour += 12;
		}
		else if ( strTimeAmPm == 'am' )
		{
			if ( strTimeHour == 12 )
				strTimeHour = 0;
		}
		
		strTimeHour = strTimeHour.toString();
		if ( strTimeHour.length < 2 )
			strTimeHour = '0' + strTimeHour;
	
		strTimeMin	= strTimeMin.toString();
		if ( strTimeMin.length < 2 )
			strTimeMin = '0' + strTimeMin;
		
		strTime	= strTimeHour + strTimeMin + '00';		
	}	
	else
	{
		strTime = '000000';
	}
	
	document.getElementById(strHiddenDateTimeFieldId).value = document.getElementById(strHiddenDateTimeFieldId).value.substr(0,8) + strTime;	
}

function __has_time_field(strHiddenDateTimeFieldId)
{
	return (document.getElementById(strHiddenDateTimeFieldId+'_time_hh') != null && document.getElementById(strHiddenDateTimeFieldId+'_time_mm') != null)?true:false;
}

function clear_date_field(strId)
{
	document.getElementById(strId).value='';
	document.getElementById(strId + '_date').value='';
}
