﻿var CSISchedule = new __Schedule;

function __Schedule()
{
	this.HiddenDateId = "";
	this.TableId = "";
	this.CurrentMonth = 0;
	this.CurrentYear = 0;
	this.MinDate = "";
	this.MaxDate = "";
	this.ControlId = "";
	this.ModelId = "";
	
	var HiddenDate = null;
	var Table = null;
	var Controls = null;
	var Model = null;
	
	this.Init = function()
	{
		if (HiddenDate == null)
			HiddenDate = document.getElementById(this.HiddenDateId);
		if (Table == null)
			Table = document.getElementById(this.TableId);
		if (Controls == null)
			Controls = document.getElementById(this.ControlId);
		if (Model == null)
			Model = document.getElementById(this.ModelId);
	}
	
	this.LoadModels = function(pCbo)
	{
		this.Init();

		if (Model != null)
		{		
			var Make = pCbo[pCbo.selectedIndex].value;
			var Params = "?type=GetModels&value=" + Make;
			var AllModels = __DoCallBack(Params, false);
			var Models = AllModels.split(",");
			
			Model.options.length = 0;
			for (var iLoop = 0; iLoop < Models.length; iLoop++)
			{
				Model.options.add(new Option(Models[iLoop], Models[iLoop]));
			}
		}
	}
	
	this.LoadCalendar = function(pDiff)
	{
		this.Init();
		
		this.CurrentMonth += pDiff;
		if (this.CurrentMonth == 13)
		{
			this.CurrentMonth = 1;
			this.CurrentYear += 1;
		}
		if (this.CurrentMonth == 0)
		{
			this.CurrentMonth = 12;
			this.CurrentYear -= 1;
		}
		var CurrentDate = this.ConvertDateToString(this.CurrentMonth, this.CurrentYear);
		if (parseInt(CurrentDate) > parseInt(this.MaxDate))
		{
			this.CurrentMonth = parseInt(this.MaxDate.substring(4, 6));
			this.CurrentYear = parseInt(this.MaxDate.substring(0, 4));
		}
		if (parseInt(CurrentDate) < parseInt(this.MinDate))
		{
			this.CurrentMonth = parseInt(this.MinDate.substring(4, 6));
			this.CurrentYear = parseInt(this.MinDate.substring(0, 4));
		}

		this.SetMonths();
		
		HiddenDate.value = this.CurrentMonth.toString() + "/01/" + this.CurrentYear.toString();
		
		if (Table != null)
		{
			var Params = "?month=" + this.CurrentMonth;
			Params += "&year=" + this.CurrentYear;
			Params += "&type=GetMonth";
		
			var MonthHTML = __DoCallBack(Params, false);
			Table.innerHTML = MonthHTML;
		}
	}
	
	this.SetMonths = function()
	{
		if (Controls != null)
		{
			var Cells = Controls.getElementsByTagName("TD");
			var Months = new Array;
			Months[0] = "Dec,Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec,Jan".split(",");
			Months[1] = "December,January,February,March,April,May,June,July,August,September,October,November,December,January".split(",");
		
			for (var iCell = 0; iCell < Cells.length; iCell++)
			{
				if (iCell != 1)
				{
					Cells[iCell].childNodes[0].innerText = Months[0][this.CurrentMonth + (iCell - 1)];
				}
				else
				{
					Cells[1].childNodes[0].innerText = Months[1][this.CurrentMonth];
				}
			}
		}
	}
	
	this.SelectDate = function(pDate, thisCell)
	{
		this.Init();
		
		if (HiddenDate != null && Table != null)
		{
			HiddenDate.value = pDate;
			
			var objCells = Table.getElementsByTagName("TD");
			
			if (objCells != null)
			{
				for (var iLoop = 0; iLoop < objCells.length; iLoop++)
				{
					if (objCells[iLoop].className == "Selected")
						objCells[iLoop].className = "Active";
				}
			}
			
			thisCell.className = "Selected";
		}
	}
	
	this.ConvertDateToString = function(pMonth, pYear)
	{
		var strDate = pYear.toString();
		var strMonth = pMonth.toString();
		
		if (strMonth.length == 1)
			strDate += "0";
		strDate += strMonth;
		
		return strDate;
	}
}
