/*--------------------------------------------------
	Project:	The November Group - Date Time Select
	Script:		dts.js
	Includes:	DTS
	Description:
		insert() takes an id an id for an input element and inserts drop-down boxes to select the date and time, updating the input element when they are changed.
--------------------------------------------------*/

var DTS={
	
	intTypeFull:0,
	intTypeDateOnly:1,
	intTypeDateOptional:2,
	
	insert:function(strId,intType,objSettings)
	{
		if(document.getElementById(strId))
		{
			var input=document.getElementById(strId);
			var dtmDate=DTS.getDateObject(strId);
			
			//create select elements
			var eleSelectYear=document.createElement("select");
			eleSelectYear.name="DTS_year";
			eleSelectYear.id="DTS_year_"+strId;
			eleSelectYear.className="input9";
			eleSelectYear.onchange=this.change;
			
			var eleSelectMonth=document.createElement("select");
			eleSelectMonth.name="DTS_month";
			eleSelectMonth.id="DTS_month_"+strId;
			eleSelectMonth.className="input9";
			eleSelectMonth.onchange=this.change;
			
			var eleSelectDate=document.createElement("select");
			eleSelectDate.name="DTS_date";
			eleSelectDate.id="DTS_date_"+strId;
			eleSelectDate.className="input7";
			eleSelectDate.onchange=this.change;
			
			var eleSelectHours=document.createElement("select");
			eleSelectHours.name="DTS_hours";
			eleSelectHours.id="DTS_hours_"+strId;
			eleSelectHours.className="input6";
			eleSelectHours.onchange=this.change;
			
			var eleSelectMinutes=document.createElement("select");
			eleSelectMinutes.name="DTS_minutes";
			eleSelectMinutes.id="DTS_minutes_"+strId;
			eleSelectMinutes.className="input7";
			eleSelectMinutes.onchange=this.change;
			
			var eleSelectAm=document.createElement("select");
			eleSelectAm.name="DTS_am";
			eleSelectAm.id="DTS_am_"+strId;
			eleSelectAm.className="input7";
			eleSelectAm.onchange=this.change;
			
			//create container
			var eleContainer=document.createElement("span");
			//append elements to container
			eleContainer.appendChild(eleSelectYear);
			eleContainer.appendChild(eleSelectMonth);
			eleContainer.appendChild(eleSelectDate);
			//check type before including hours and minutes
			if(intType!=this.intTypeDateOnly)
			{
				eleContainer.appendChild(document.createTextNode(" "));
				eleContainer.appendChild(eleSelectHours);
				eleContainer.appendChild(eleSelectMinutes);
				eleContainer.appendChild(eleSelectAm);
			}
			
			//insert container before input element
			input.parentNode.insertBefore(eleContainer,input);

			//populate select elements
			DTS.clearOptions(eleSelectYear);
			var intYear=dtmDate.getFullYear();
			if(objSettings && objSettings.intYearStart && objSettings.intYearEnd)
			{
				if(objSettings.intYearStart<objSettings.intYearEnd)
				{
					for(var i=objSettings.intYearStart;i<=objSettings.intYearEnd;i++)
					{
						var eleOpt=document.createElement("option");
						eleOpt.text=i;
						eleOpt.value=i;
						eleSelectYear.options[eleSelectYear.length]=eleOpt;
						if(eleOpt.value==dtmDate.getFullYear())
						{
							eleOpt.selected=true;
						}
					}
				}
				else
				{
					for(var i=objSettings.intYearStart;i>=objSettings.intYearEnd;i--)
					{
						var eleOpt=document.createElement("option");
						eleOpt.text=i;
						eleOpt.value=i;
						eleSelectYear.options[eleSelectYear.length]=eleOpt;
						if(eleOpt.value==dtmDate.getFullYear())
						{
							eleOpt.selected=true;
						}
					}
				}
			}
			else
			{
				for(var i=-7;i<9;i++)
				{
					var eleOpt=document.createElement("option");
					eleOpt.text=dtmDate.getFullYear()+i;
					eleOpt.value=dtmDate.getFullYear()+i;
					eleSelectYear.options[eleSelectYear.length]=eleOpt;
					if(eleOpt.value==dtmDate.getFullYear())
					{
						eleOpt.selected=true;
					}
				}
			}
			
			DTS.clearOptions(eleSelectMonth);
			for(var i=0;i<12;i++)
			{
				var eleOpt=document.createElement("option");
				//eleOpt.text=(1+i < 10 ? "0" : "")+(1+i).toString();
				eleOpt.text=DTS.monthAbbrev[i];
				eleOpt.value=i;
				eleSelectMonth.options[eleSelectMonth.length]=eleOpt;
				if(eleOpt.value==dtmDate.getMonth())
				{
					eleOpt.selected=true;
				}
			}
			
			DTS.monthChange(strId);
	
			DTS.clearOptions(eleSelectHours);
			if(intType==this.intTypeDateOptional)
			{
				var eleOpt=document.createElement("option");
				eleOpt.text="--";
				eleOpt.value=-1;
				eleSelectHours.options[eleSelectHours.length]=eleOpt;
			}
			for(var i=0;i<12;i++)
			{
				var eleOpt=document.createElement("option");
				eleOpt.text=(i==0 ? i+12 : i);
				eleOpt.value=i;
				eleSelectHours.options[eleSelectHours.length]=eleOpt;
				if(eleOpt.value==dtmDate.getHours() || eleOpt.value==(dtmDate.getHours()-12))
				{
					eleOpt.selected=true;
				}
			}
			
			DTS.clearOptions(eleSelectMinutes);
			if(intType==this.intTypeDateOptional)
			{
				var eleOpt=document.createElement("option");
				eleOpt.text="--";
				eleOpt.value=-1;
				eleSelectMinutes.options[eleSelectMinutes.length]=eleOpt;
			}
			for(var i=0;i<4;i++)
			{
				var eleOpt=document.createElement("option");
				eleOpt.text=(i*15 < 10 ? "0" : "")+(i*15).toString();
				eleOpt.value=i*15;
				eleSelectMinutes.options[eleSelectMinutes.length]=eleOpt;
				if(eleOpt.value <= dtmDate.getMinutes() && eleOpt.value > dtmDate.getMinutes()-15)
				{
					eleOpt.selected=true;
					dtmDate.setMinutes(eleOpt.value); //update date object to currently selected value
				}
			}
			
			DTS.clearOptions(eleSelectAm);
			if(intType==this.intTypeDateOptional)
			{
				var eleOpt=document.createElement("option");
				eleOpt.text="--";
				eleOpt.value=-1;
				eleSelectAm.options[eleSelectAm.length]=eleOpt;
				eleOpt.selected=true;
			}
			var eleOptA=document.createElement("option");
			eleOptA.text="AM";
			eleOptA.value=0;
			eleSelectAm.options[eleSelectAm.length]=eleOptA;
			var eleOptP=document.createElement("option");
			eleOptP.text="PM";
			eleOptP.value=12;
			eleSelectAm.options[eleSelectAm.length]=eleOptP;
			if(dtmDate.getHours()<12)
			{
				eleOptA.selected=true;
			}
			else
			{
				eleOptP.selected=true;
			}
			
			//change time selection based on type
			if(intType==this.intTypeDateOptional)
			{
				//check for default time
				if(eleSelectHours.value==0 && eleSelectMinutes.value==0 && eleSelectAm.value==0)
				{
					//select default options
					eleSelectHours.options[0].selected=true;
					eleSelectMinutes.options[0].selected=true;
					eleSelectAm.options[0].selected=true;
				}
			}
			
			dtmDate.setSeconds(0);
			
			//update input value
			document.getElementById(strId).value=Math.floor(dtmDate.getTime()/1000);
		}
	},
	
	monthChange:function(strId)
	{
		var eleDateSelect=null;
		var dtmDate=DTS.getDateObject(strId);
		var dtmDay=new Date();
		
		dtmDay.setTime(dtmDate.getTime());
		eleSelectDate=document.getElementById("DTS_date_"+strId);
		
		DTS.clearOptions(eleSelectDate);
		
		//add first day of month
		var eleOpt=document.createElement("option");
		dtmDay.setDate(1);
		eleOpt.text=dtmDay.getDate();
		eleOpt.value=dtmDay.getDate();
		eleSelectDate.options[eleSelectDate.length]=eleOpt;
		//add remaining days of month
		var dtmPrevDay=new Date();
		dtmPrevDay.setTime(dtmDay.getTime());
		dtmDay.setDate(2);
		for(var i=3;dtmDay.getDate() > dtmPrevDay.getDate();i++)
		{
			var eleOpt=document.createElement("option");
			eleOpt.text=dtmDay.getDate();
			eleOpt.value=dtmDay.getDate();
			eleSelectDate.options[eleSelectDate.length]=eleOpt;
			if(eleOpt.value==dtmDate.getDate())
			{
				eleOpt.selected=true;
			}
			
			dtmDay.setDate(i);
			dtmPrevDay.setDate(i-1);
		}
	},
	
	change:function(event)
	{
		var eleSelect=null;
		var strId="";
		var intType=0;
		
		try
		{
			eleSelect=event.target;
		}
		catch(e)
		{
			eleSelect=window.event.srcElement; //IE
		}
		strId=DTS.getId(eleSelect.id);
		
		//determine type
		if(document.getElementById("DTS_hours_"+strId) && document.getElementById("DTS_hours_"+strId).options[0].value==-1)
			intType=this.intTypeDateOptional;
		
		if(intType==this.intTypeDateOptional) //check for date optional type
		{
			if(eleSelect.id.indexOf("DTS_hours_")==0 || eleSelect.id.indexOf("DTS_minutes_")==0 || eleSelect.id.indexOf("DTS_am_")==0) //check if changed element was a time select box
			{
				if(Number(eleSelect.value)==-1) //check if default value was selected
				{
					//change other time values to default values
					document.getElementById("DTS_hours_"+strId).options[0].selected=true;
					document.getElementById("DTS_minutes_"+strId).options[0].selected=true;
					document.getElementById("DTS_am_"+strId).options[0].selected=true;
				}
				else
				{
					//change other time values to non-default values
					if(Number(document.getElementById("DTS_hours_"+strId).value)==-1)
						document.getElementById("DTS_hours_"+strId).options[1].selected=true;
					if(Number(document.getElementById("DTS_minutes_"+strId).value)==-1)
						document.getElementById("DTS_minutes_"+strId).options[1].selected=true;
					if(Number(document.getElementById("DTS_am_"+strId).value)==-1)
						document.getElementById("DTS_am_"+strId).options[1].selected=true;
				}
			}
		}
	
		var dtmDateLastDayOfMonth=DTS.getDateObject(strId);
		dtmDateLastDayOfMonth.setDate(1); //set to first day of month
		dtmDateLastDayOfMonth.setYear(document.getElementById("DTS_year_"+strId).value);
		dtmDateLastDayOfMonth.setMonth(Number(document.getElementById("DTS_month_"+strId).value)+1); //set to next month
		dtmDateLastDayOfMonth.setDate(0); //set back to last day of current month
		if(document.getElementById("DTS_hours_"+strId))
			dtmDateLastDayOfMonth.setHours((Number(document.getElementById("DTS_hours_"+strId).value)==-1 ? 0 : Number(document.getElementById("DTS_hours_"+strId).value)) + (Number(document.getElementById("DTS_am_"+strId).value)==-1 ? 0 : Number(document.getElementById("DTS_am_"+strId).value)));
		else
			dtmDateLastDayOfMonth.setHours(0);
		if(document.getElementById("DTS_minutes_"+strId))
			dtmDateLastDayOfMonth.setMinutes((Number(document.getElementById("DTS_minutes_"+strId).value)==-1 ? 0 : Number(document.getElementById("DTS_minutes_"+strId).value)));
		else
			dtmDateLastDayOfMonth.setMinutes(0);
		dtmDateLastDayOfMonth.setSeconds(0);
		
		var dtmDate=DTS.getDateObject(strId);
		dtmDate.setDate(1);
		dtmDate.setYear(document.getElementById("DTS_year_"+strId).value);
		dtmDate.setMonth(document.getElementById("DTS_month_"+strId).value);
		//check if date is past last day of month (and always set date before setting month)
		dtmDate.setDate((document.getElementById("DTS_date_"+strId).value <= dtmDateLastDayOfMonth.getDate() ? document.getElementById("DTS_date_"+strId).value : dtmDateLastDayOfMonth.getDate()));
		if(document.getElementById("DTS_hours_"+strId))
			dtmDate.setHours((Number(document.getElementById("DTS_hours_"+strId).value)==-1 ? 0 : Number(document.getElementById("DTS_hours_"+strId).value)) + (Number(document.getElementById("DTS_am_"+strId).value)==-1 ? 0 : Number(document.getElementById("DTS_am_"+strId).value)));
		else
			dtmDate.setHours(0);
		if(document.getElementById("DTS_minutes_"+strId))
			dtmDate.setMinutes((Number(document.getElementById("DTS_minutes_"+strId).value)==-1 ? 0 : Number(document.getElementById("DTS_minutes_"+strId).value)));
		else
			dtmDate.setMinutes(0);
		dtmDate.setSeconds(0);
		
		document.getElementById(strId).value=Math.floor(dtmDate.getTime()/1000);
		
		//update date select when month or year changes (when year changes, in case february is selected and it switches between a leap year and a non-leap year)
		if(eleSelect.id.indexOf("DTS_month_")==0 || eleSelect.id.indexOf("DTS_year_")==0)
		{
			DTS.monthChange(strId);
			DTS.update(strId);
		}
	},
	
	//update select elements from input value
	update:function(strId)
	{
		var dtmDate=DTS.getDateObject(strId);
		var intType=0;
		
		var eleSelectYear=document.getElementById("DTS_year_"+strId);
		for(var i=0;i<eleSelectYear.options.length;i++)
		{
			if(eleSelectYear.options[i].value==dtmDate.getFullYear())
			{
				eleSelectYear.options[i].selected=true;
				break;
			}
		}
		
		var eleSelectMonth=document.getElementById("DTS_month_"+strId);
		for(var i=0;i<eleSelectMonth.options.length;i++)
		{
			if(eleSelectMonth.options[i].value==dtmDate.getMonth())
			{
				eleSelectMonth.options[i].selected=true;
				break;
			}
		}
		
		var eleSelectDate=document.getElementById("DTS_date_"+strId);
		for(var i=0;i<eleSelectDate.options.length;i++)
		{
			if(eleSelectDate.options[i].value==dtmDate.getDate())
			{
				eleSelectDate.options[i].selected=true;
				break;
			}
		}
		
		if(document.getElementById("DTS_hours_"+strId))
		{
			var eleSelectHours=document.getElementById("DTS_hours_"+strId);
			for(var i=0;i<eleSelectHours.options.length;i++)
			{
				if(eleSelectHours.options[i].value>=0)
				{
					if(eleSelectHours.options[i].value==dtmDate.getHours() || eleSelectHours.options[i].value==(dtmDate.getHours()-12))
					{
						eleSelectHours.options[i].selected=true;
						break;
					}
				}
			}
		}
		
		if(document.getElementById("DTS_minutes_"+strId))
		{
			var eleSelectMinutes=document.getElementById("DTS_minutes_"+strId);
			for(var i=0;i<eleSelectMinutes.options.length;i++)
			{
				if(eleSelectMinutes.options[i].value>=0)
				{
					if(eleSelectMinutes.options[i].value <= dtmDate.getMinutes() && eleSelectMinutes.options[i].value > dtmDate.getMinutes()-15)
					{
						eleSelectMinutes.options[i].selected=true;
						break;
					}
				}
			}
		}
		
		if(document.getElementById("DTS_am_"+strId))
		{
			var eleSelectAm=document.getElementById("DTS_am_"+strId);
			for(var i=0;i<eleSelectAm.options.length;i++)
			{
				if(eleSelectAm.options[i].value>=0)
				{
					if(eleSelectAm.options[i].value <= dtmDate.getHours() && eleSelectAm.options[i].value > dtmDate.getHours()-12)
					{
						eleSelectAm.options[i].selected=true;
						break;
					}
				}
			}
		}
		
		//determine type
		if(eleSelectHours && eleSelectHours.options[0].value==-1)
			intType=this.intTypeDateOptional;
		
		//check type
		if(intType==this.intTypeDateOptional)
		{
			//check for default time
			if(eleSelectHours.value==0 && eleSelectMinutes.value==0 && eleSelectAm.value==0)
			{
				//select default options
				eleSelectHours.options[0].selected=true;
				eleSelectMinutes.options[0].selected=true;
				eleSelectAm.options[0].selected=true;
			}
		}
	},
	
	getDateObject:function(strId)
	{
		var input=document.getElementById(strId);
		var dtmDate=new Date();
		
		dtmDate.setTime(input.value.valueOf()*1000);
		
		return dtmDate;
	},
	
	getId:function(strId)
	{
		//remove all possible prefixes
		strId=strId.replace("DTS_year_","");
		strId=strId.replace("DTS_month_","");
		strId=strId.replace("DTS_date_","");
		strId=strId.replace("DTS_hours_","");
		strId=strId.replace("DTS_minutes_","");
		strId=strId.replace("DTS_am_","");
		
		return strId;
	},
	
	clearOptions:function(eleSelect)
	{
		while(eleSelect.options.length>0)
		{
			eleSelect.remove(0);
		}
	},
	
	disabled:function(strId,blnDisabled)
	{
		if(document.getElementById("DTS_year_"+strId))
			document.getElementById("DTS_year_"+strId).disabled=blnDisabled;
		if(document.getElementById("DTS_month_"+strId))
			document.getElementById("DTS_month_"+strId).disabled=blnDisabled;
		if(document.getElementById("DTS_date_"+strId))
			document.getElementById("DTS_date_"+strId).disabled=blnDisabled;
		if(document.getElementById("DTS_hours_"+strId))
			document.getElementById("DTS_hours_"+strId).disabled=blnDisabled;
		if(document.getElementById("DTS_minutes_"+strId))
			document.getElementById("DTS_minutes_"+strId).disabled=blnDisabled;
		if(document.getElementById("DTS_am_"+strId))
			document.getElementById("DTS_am_"+strId).disabled=blnDisabled;
	},
	
	month:["January","February","March","April","May","June","July","August","September","October","November","December"],
	monthAbbrev:["Jan.","Feb.","Mar.","Apr.","May","June","July","Aug.","Sept.","Oct.","Nov.","Dec."]
}
