
ArgusCalendarView = function (index, calendar)
{
	this.index = index;
	this.calendar = calendar;
	this.yearsCombo = findElementByNamePart('ac_years_' + index, this.calendar.container);
	this.monthsCombo = findElementByNamePart('ac_months_' + index, this.calendar.container);
	this.dateLabel = findElementByNamePart('ac_selected_' + index, this.calendar.container);
	this.dateInput = findElementByNamePart('ac_date_' + index, this.calendar.container);
	this.dateInputStore = findElementByNamePart('ac_date_store_' + index, this.calendar.container); 
	this.daysLabel = findElementByNamePart('ac_days_label_' + index, this.calendar.container); 
	
	addEvent(this.yearsCombo, "change", ArgusCalendarView.onYearComboChanged);
	addEvent(this.monthsCombo, "change", ArgusCalendarView.onMonthComboChanged);
	addEvent(this.dateInput, "change", ArgusCalendarView.onDateInputChanged);
	addEvent(this.dateInput, "keydown", ArgusCalendarView.onDateInputKeyDown);
	
	this.date = this.calendar.ParseDate(this.dateInputStore.value);
	this.ChangeDateIfNotValid();
	
	this.color = this.dateInput.style.color;
}

ArgusCalendarView.prototype.changeState = function(isDisableState)
{
		this.yearsCombo.disabled = isDisableState;
		this.monthsCombo.disabled = isDisableState;
		this.dateInput.disabled = isDisableState;
		this.daysLabel.disabled = isDisableState;
		
		if(isDisableState){ this.dateInput.style.color = "#aca899"; 
		}
		else{	this.dateInput.style.color = this.color; }
};

ArgusCalendarView.prototype.Disable = function(){ this.changeState(true) };
ArgusCalendarView.prototype.Enable = function(){ this.changeState(false) };

ArgusCalendarView.prototype.IsEmptyDateSelected = function()
{
	return this.dateInputStore.value == '';
}

ArgusCalendarView.prototype.NeedClearButton = function()
{
	if (this.calendar.allowEmptyDate)
	{	
		return !this.IsEmptyDateSelected();
	}
}

ArgusCalendarView.prototype.ChangeDateIfNotValid = function()
{
	if (ArgusCalendar.CompareDates(this.date, this.calendar.nullDateValue))
	{
		this.date = new Date(this.calendar.todayValue);
		this.notifyDateChanged();
	}
	
	var d;
	if (!this.calendar.ValidateDateObject(this.date, true))
	{
		if (this.calendar.availableRange == ArgusCalendar.AvailableRangeRestrictToDates)
		{
			d = this.findClosestDateForRestrictedDates(this.date);
		}
		else if (this.calendar.availableRange == ArgusCalendar.AvailableRangeRestrictToRange)
		{
			d = this.findClosestDateForRestrictedRange(this.date);
		}
		else
		{
			d = this.findClosestDateInMonth(this.date);
		}
	}
	
	if (d != null && !ArgusCalendar.CompareDates(d, this.date))
	{
		this.date = new Date(d);
		this.notifyDateChanged();
	}
}


ArgusCalendarView.prototype.Show = function(dateToShow) {

    if (dateToShow && dateToShow != null && typeof (dateToShow) != 'undefined') {
        this.date = dateToShow;
    }
    else {
        this.date = this.calendar.ParseDate(this.dateInputStore.value);
    }
    
    this.ChangeDateIfNotValid();
    
    this.fillYearsCombo();
    this.fillMonthsCombo();
    this.printDays();

    this.showSelectedDateInLabel();

    visible(findElementByNamePart('ac_dayscontainer_' + this.index, this.calendar.container).id,
		this.calendar.dateSelectionType == ArgusCalendar.MonthYear ? '0' : '1');
}


ArgusCalendarView.prototype.showSelectedDateInLabel = function()
{
	if (this.calendar.ValidateDateObject(this.date, true))
	{
		this.lastCorrectSelectedDate = new Date(this.date);
		this.dateLabel.innerHTML = this.calendar.FormatDate(this.date);
	}
	else
	{
		try
		{
			this.dateLabel.innerHTML = this.calendar.FormatDate(this.lastCorrectSelectedDate);
		}
		catch (e) {}
	}
}

ArgusCalendarView.prototype.processDateInputChanged = function()
{
  if (!this.calendar.ValidateDatesInInputs(true))
  {
		this.dateInput.value = this.calendar.FormatDate(this.date);
		this.showSelectedDateInLabel();
	}
}

ArgusCalendarView.prototype.processDateChanged = function()
{
  var isValid = this.calendar.ValidateDateObject(this.date, true);
  this.printDays();
  if (this.calendar.mode == ArgusCalendar.FlatMode)
  {
		this.showSelectedDateInLabel();
		this.notifyDateChanged();
  }
  else
  {
		if (isValid)
			this.showSelectedDateInLabel();
  }
}

ArgusCalendarView.prototype.processYearChanged = function()
{
	try{eval(ClientOnYearComboChanged);}
	catch(ex){}
  this.changeMonthYear(this.yearsCombo.options[this.yearsCombo.selectedIndex].value, this.date.getMonth());
  //this.showSelectedDateInLabel();
}

ArgusCalendarView.prototype.processMonthChanged = function()
{
	this.changeMonthYear(this.date.getFullYear(), this.monthsCombo.options[this.monthsCombo.selectedIndex].value);
	//this.showSelectedDateInLabel();
}

ArgusCalendarView.prototype.notifyDateChanged = function()
{
	this.dateInput.value = this.dateInputStore.value = this.calendar.FormatDate(this.date);
	this.showSelectedDateInLabel();
	this.calendar.showClearButton();
}

ArgusCalendarView.prototype.changeMonthYear = function(newYear, newMonth)
{
  var maxDays = ArgusCalendar.MaxDaysInMonth(newYear, newMonth);
  if (maxDays < this.date.getDate())
  {
		this.date.setDate(maxDays);
  }
  
  this.date.setMonth(newMonth);
  this.date.setYear(newYear);
  
  /*
  if (this.calendar.availableRange == ArgusCalendar.AvailableRangeRestrictToRange)
  {
    if (this.date < this.calendar.restrictRangeStartDate)
 			this.date = new Date(this.calendar.restrictRangeStartDate);
 		if (this.date > this.calendar.restrictRangeEndDate)
 			this.date = new Date(this.calendar.restrictRangeEndDate);
  }
  */
 	
  //this.fillMonthsCombo();
	this.printDays();
}


ArgusCalendarView.prototype.clearCombo = function(combo)
{
	var maxLen = combo.options.length;
	for (var i = 0; i < maxLen; i++)
 {
  combo.options[0] = null;
 }
}
 
ArgusCalendarView.prototype.fillYearsCombo = function()
{
 this.clearCombo(this.yearsCombo);
  
 var minYear = ArgusCalendar.MinYear;
 var maxYear = ArgusCalendar.MaxYear;
 if (this.calendar.availableRange == ArgusCalendar.AvailableRangeRestrictToRange)
 {
	minYear = this.calendar.restrictRangeStartDate.getFullYear();
	maxYear = this.calendar.restrictRangeEndDate.getFullYear();
 }
  
 //for (var i = minYear; i <= maxYear; i++)
 for (var i = maxYear; i >= minYear; i--)
 {
	var op = new Option(i, i);
	this.yearsCombo.options.add(new Option(i, i));
	if (i == this.date.getFullYear())
		this.yearsCombo.selectedIndex = this.yearsCombo.options.length - 1;
 }
}
 
ArgusCalendarView.prototype.fillMonthsCombo = function()
{
 this.clearCombo(this.monthsCombo);
  
 var minMonth = 0;
 var maxMonth = 11;
  
 if (this.calendar.availableRange == ArgusCalendar.AvailableRangeRestrictToRange)
 {
   if (this.date.getFullYear() == this.calendar.restrictRangeStartDate.getFullYear())
   	minMonth = this.calendar.restrictRangeStartDate.getMonth();
	if (this.date.getFullYear() == this.calendar.restrictRangeEndDate.getFullYear())
		maxMonth = this.calendar.restrictRangeEndDate.getMonth();
 }
  
 for (var i = minMonth; i <= maxMonth; i++)
 {
	var op = new Option(ArgusCalendar.MonthNames[i], i);
	this.monthsCombo.options.add(op);
	if (i == this.date.getMonth())
		this.monthsCombo.selectedIndex = this.monthsCombo.options.length - 1;
 }
}


 
ArgusCalendarView.prototype.printDays = function()
{
	var firstDay = (new Date(this.date.getFullYear(), this.date.getMonth(), 1)).getDay() + 1;
	var maxDaysInMonth = ArgusCalendar.MaxDaysInMonthByDate(this.date);

	var minDay = 1;
	var maxDay = ArgusCalendar.MaxDaysInMonthByDate(this.date);
	var availableDates = new Array();

	if (this.calendar.availableRange == ArgusCalendar.AvailableRangeRestrictToDates)
	{
		for (var i = 0; i < this.calendar.restrictRangeDates.length; i++)
		{
			var d = this.calendar.restrictRangeDates[i]
			if (this.date.getFullYear() == d.getFullYear() && this.date.getMonth() == d.getMonth())
			{
				availableDates.length++;
				availableDates[availableDates.length - 1] = this.calendar.restrictRangeDates[i].getDate();
			}
		}
	}
	else 
	{
		if (this.calendar.availableRange == ArgusCalendar.AvailableRangeRestrictToRange)
		{
			if (this.date.getFullYear() == this.calendar.restrictRangeStartDate.getFullYear() && 
				this.date.getMonth() == this.calendar.restrictRangeStartDate.getMonth())
					minDay = this.calendar.restrictRangeStartDate.getDate();
			if (this.date.getFullYear() == this.calendar.restrictRangeEndDate.getFullYear() && 
				this.date.getMonth() == this.calendar.restrictRangeEndDate.getMonth())
					maxDay = this.calendar.restrictRangeEndDate.getDate();
		}

		for (var i = minDay; i <= maxDay; i++)
		{
			availableDates.length++;
			availableDates[availableDates.length - 1] = i;
		}
	}
	
	var res = "<table class='calendar' width='100%'>\n<tr><th>S</th><th>M</th><th>T</th><th>W</th><th>T</th><th>F</th><th>S</th></tr>\n";
	
	var selectedDay = -1;
	var closestDate = null;
	if (this.calendar.availableRange == ArgusCalendar.AvailableRangeRestrictToDates)
		closestDate = this.findClosestDateInMonthForRestrictedDates(this.date);
	else
		closestDate = this.findClosestDateInMonth(this.date);
		
	if (closestDate != null)
	{
		this.date = closestDate;
		selectedDay = closestDate.getDate();
	}
			
	
	var curDay = 0;
	var cellNum = 0;
	
	for (var j = 1; j <= 6; j++)
	{
		res += "<tr>\n";
		for (var i = 1; i <= 7; i++)
		{
			cellNum++;
			res += "<td";
			
			var isMonthDate = ((cellNum >= firstDay) && (cellNum - firstDay < maxDaysInMonth));
			  		  
			if (isMonthDate)
			{
				curDay++;
				
				var className = "";
				var isValidDate = false; //(curDay >= minDay && curDay <= maxDay);
				
				for (var k = 0; k < availableDates.length; k++)
				{
					if (availableDates[k] == curDay)
					{
						isValidDate = true;
						break;
					}
				}
								
				var onClickFunc = "";
				if (isValidDate)
				 onClickFunc = " onclick='javascript:ArgusCalendarView.onDayClicked(this, " + curDay + ");'"
				
				if (curDay == selectedDay)
				{
					className = "selectedday";
				}
				else if (isValidDate)
				{
					if(((i == 1 || i == 7) && !this.calendar.allowWeekendSelect ) 
						&& this.calendar.availableRange != ArgusCalendar.AvailableRangeRestrictToDates)
					{
						className = "holiday";
						onClickFunc = "";
					}
					else
						className = "pubdate";
				}
				
				if (className != "")
					res += " class='" + className + "'";
					
				res += onClickFunc + ">"
				
				if (curDay != selectedDay)	
					res += curDay;
				else
					res += "<div>" + curDay + "</div>";
			}
			else
			{
				res += ">&nbsp;";
			}
			res += "</td>\n"
		}	
		
		res += "</tr>\n";
		
		/*if(!this.calendar.allowRangeSelection)
			if (curDay >= maxDaysInMonth && 
				(!this.calendar.allowRangeSelection || (this.calendar.allowRangeSelection && this.calendar.mode != ArgusCalendar.FlatMode)))
				break;//*/
	}
	res += "</table>";
	this.daysLabel.innerHTML = res;
}

ArgusCalendarView.prototype.findClosestDateForRestrictedDates = function(date)
{
	var minDiff = Number.MAX_VALUE;
	var curDiff;
	var resultDate = null;
	for (var i = 0; i < this.calendar.restrictRangeDates.length; i++)
	{
		curDiff = Math.abs(this.calendar.restrictRangeDates[i] - date);
		if (curDiff < minDiff)
		{
			minDiff = curDiff;
			resultDate = this.calendar.restrictRangeDates[i];
		}
	}
	return resultDate;
}

ArgusCalendarView.prototype.findClosestDateForRestrictedRange = function(date)
{
	if (date < this.calendar.restrictRangeStartDate)	
		return this.calendar.restrictRangeStartDate;
	if (date > this.calendar.restrictRangeEndDate)
		return this.calendar.restrictRangeEndDate;
	return date;
}

ArgusCalendarView.prototype.findStartEndDatesInxInMonth = function(testDate)
{
	var maxDaysInMonth = ArgusCalendar.MaxDaysInMonthByDate(testDate);
	var startInx = -1;
	var endInx = -1;
	var tmp = 0;
	
	for (var i = 0; i < this.calendar.restrictRangeDates.length; i++)
	{
		if (this.calendar.restrictRangeDates[i].getFullYear() == testDate.getFullYear() 
			&& this.calendar.restrictRangeDates[i].getMonth() == testDate.getMonth())
		{
			if (startInx == -1)
			{
				startInx = i;
				tmp = 1;
			}
			if (endInx < i)
				endInx = i;
		}
		
		if (tmp != 0)
			tmp++;
			
		if (tmp > maxDaysInMonth)
			break;
	}
	return new Array(startInx,endInx);
}

ArgusCalendarView.prototype.findClosestDateInMonth = function(startDate)
{
	var minDay = 1;
	var maxDay = ArgusCalendar.MaxDaysInMonthByDate(startDate);
			
	var d;
	for (var j = startDate.getDate(); j <= maxDay; j++)
	{
		d = new Date(startDate.getFullYear(), startDate.getMonth(), j);
		if (this.calendar.ValidateDateObject(d, true))
		{
			return d;
		}
		
	}
	
	for (var j = startDate.getDate() - 1; j >= minDay; j--)
	{
		d = new Date(startDate.getFullYear(), startDate.getMonth(), j);
		if (this.calendar.ValidateDateObject(d, true))
		{
			return d;
		}
	}
	
	return null;
	
}


ArgusCalendarView.prototype.findClosestDateInMonthForRestrictedDates = function(startDate)
{
	var beforeInx = -1;
	var afterInx = -1;
	var beforeMinDiff = Number.MAX_VALUE;
	var afterMinDiff = Number.MAX_VALUE;
	var curDiff;
	var inxs = this.findStartEndDatesInxInMonth(startDate);
	
	if (inxs[0] != -1 && inxs[1] != -1)
	{
		for (var i = inxs[0]; i <= inxs[1]; i++)
		{
			curDiff = Math.abs(this.calendar.restrictRangeDates[i] - startDate);
			if (this.calendar.restrictRangeDates[i] <= startDate)
			{
				if (curDiff <= beforeMinDiff)
				{
					beforeMinDiff = curDiff;
					beforeInx = i;
				}
			}
			else
			{
				if (curDiff <= afterMinDiff)
				{
					afterMinDiff = curDiff;
					afterInx = i;
				}				
			}
		}
	}
	if (beforeInx != -1)
		return new Date(this.calendar.restrictRangeDates[beforeInx]);
	if (afterInx != -1)
		return new Date(this.calendar.restrictRangeDates[afterInx]);
	
	return null;
	
}



 

