﻿
function createCalendar(sControlId, iTimePeriod, sDirection) 
{
    //Strip day string off of input control because of the ct100 business
    sControlId = sControlId.replace(/Day/i, ""); 
    
	var oSelectDay = $("select#"+sControlId+"Day");
	var oSelectMonth = $("select#"+sControlId+"Month");
	var oSelectYear = $("select#"+sControlId+"Year");
	var dToday = new Date();
	var date = dToday.getDate();
	var month = dToday.getMonth() +1;
	dToday.setHours(23,59,59,999);
				
	if (sDirection == "forward")
	{
		var iYearMin = $(oSelectYear).find("option:eq(1)").val();
		var iYearMax = $(oSelectYear).find("option:last").val();
	} else 
	{
	    var iYearMin = $(oSelectYear).find("option:eq(70)").val();
		var iYearMax = $(oSelectYear).find("option:eq(1)").val();
	}
			
	var sStartDate = prependZeros(date, 2)  + "/" + prependZeros(month, 2) + "/" + iYearMin;
	var sEndDate = prependZeros(dToday.getDate(), 2) + "/" + prependZeros(dToday.getMonth()+1, 2) + "/" + iYearMax;
	    	
	// add the calendar icon:
	$(oSelectYear).after("<img src=\"images/calendar.png\" width=\"16\" height=\"16\" alt=\"calendar\" title=\"Click for a pop-up calendar\" style=\"cursor: pointer; vertical-align: middle; margin-left: 5px;\" class=\"" + sControlId + "_calendar dp-applied\" />");

	// initialise the "Select date" link
	$("img." + sControlId + "_calendar")
		.datePicker({
			createButton:false, startDate:sStartDate, endDate:sEndDate
			})
		.bind("click", function(){
			$(this).dpDisplay();
			})
		.bind("dateSelected", function(e, selectedDate, $td, state){
			
			updateSelects(selectedDate);// when a date is selected update the SELECTs

			/*if (validateDateControl(sControlId))
			{			
				hideDateControlWarning("p#"+sControlId+"Warning", function(){});
			}
			
			if (validateDateTimePeriodControl(sControlId, iTimePeriod, sDirection))
			{			
				hideDateControlWarning("p#"+sControlId+"TimePeriodWarning", function(){});
			}*/
			 })
		.bind("dpClosed", function(e, selected){
								  
			});

	// listen for when the selects are changed and update the picker:
	//$("select[id^='"+sControlId+"']").bind("blur, change, focus, keypress, click", function(){
	$("select[id^='"+sControlId+"']").bind("change", function(){
													  
		var d = new Date($(oSelectYear).val(), $(oSelectMonth).val()-1, $(oSelectDay).val());				

		$("img." + sControlId + "_calendar").dpSetSelected(d.asString());		
		
		});

	function updateSelects(sDate)
		{
		var dNew = new Date(sDate);
		var d = dNew.getDate();
		var m = dNew.getMonth()+1;
		var y = dNew.getFullYear();
		document.getElementById($(oSelectDay).attr("id")).selectedIndex = $(oSelectDay).find("option").index($(oSelectDay).find("option[value='"+d+"']"));
		document.getElementById($(oSelectMonth).attr("id")).selectedIndex = $(oSelectMonth).find("option").index($(oSelectMonth).find("option[value='"+m+"']"));
		document.getElementById($(oSelectYear).attr("id")).selectedIndex = $(oSelectYear).find("option").index($(oSelectYear).find("option[value='"+y+"']"));
			
		};

	// note that this picker will add days on to invalid dates ie 31st Feb becomes 3rd March	
}

//____________________________________________________________________________________________________
function showDateControlWarning(sControlId, sOutput)
	{
	if ($(sControlId).css("display")=="block")
		{
		hideDateControlWarning(function(){showDateControlWarning(sOutput)});
		}
	else
		{
		$(sControlId).css("display","block");
		$(sControlId).html(sOutput);
		$(sControlId).css({height: "24px", opacity: 1}, 1000);
		};
	};
function hideDateControlWarning(sControlId, fnCallback)
	{ $(sControlId).fadeTo(500,0.01, function(){$(this).slideUp(500, function(){fnCallback();});}); };
	
//____________________________________________________________________________________________________

function prependZeros(uVariable, iLength)
	{ return padZeroDigits(uVariable, iLength, 0); };
function appendZeros(uVariable, iLength)
	{ return padZeroDigits(uVariable, iLength, 1); };
function padZeroDigits(uVariable, iLength, bEnd)
	{
	var sVariable = uVariable.toString();
	if (isNothing(sVariable)){sVariable = 0;};
	if (isNothing(iLength)){iLength = 2;};
	if (sVariable.length >= iLength){return sVariable;};
	var sNew = "";
 	for (var ii = 0; ii<(iLength-sVariable.length); ii++){ sNew += "0"; };
 	if (bEnd == 0){ return (sNew + sVariable); }
 		else{ return (sVariable + sNew); };
	};
	
//____________________________________________________________________________________________________
function isNothing(uVariable)
	{
	if ((uVariable == null) || (uVariable.toString().length == 0))
		{ return true; };
	return false;
	}
	

