var NumOfDays = [31,28,31,30,31,30,31,31,30,31,30,31];
if (!current_language) var current_language = 'HE';
if (!min_days) var min_days = 1;
switch (current_language) {
  case 'FR':
    var MonthsArrHeb = new Array ( "janvier", "fиvrier", "mars", "avril", "mai", "juin", "juillet", "ao?t", "septembre", "octobre", "novembre", "dиcembre" );
    var WeekDaysArrHeb = new Array ("S","V","J","M","M","L","D");
    var select_depart_date = 'Sйlectionner la date de dйpart';
    var select_arrival_date = 'Choisissez la date d\'arrivйe';
    var cancel_calendar = "Annuler";
    var language_direction = 'rtl';
    break;
  case 'EN':
    var MonthsArrHeb = new Array ( "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "Decmebr" );
    var WeekDaysArrHeb = new Array ("S","F","T","W","T","M","S");
    var select_depart_date = 'Select departure date';
    var select_arrival_date = 'Select arrival date';
    var cancel_calendar = "Cancel";
    var language_direction = 'ltr';
    break;
  case 'RU':
    var MonthsArrHeb = new Array ( "Январь", "Февраль", "Март", "Апрель", "Май", "Июнь", "Июль", "Август", "Сентябрь", "Октябрь", "Ноябрь", "Декабрь" );
    var WeekDaysArrHeb = new Array ("С","П","Ч","С","В","П","В");
    var select_depart_date = 'Выберите дату вылета';
    var select_arrival_date = 'Выберите дату прилета';
    var cancel_calendar = "Отмена";
    var language_direction = 'rtl';
    break;
  case 'AR':
    var MonthsArrHeb = new Array (  );
    var WeekDaysArrHeb = new Array ("'щ", "'е", "'д", "'г", "'в", "'б", "'а");
    var select_depart_date = 'бзш ъашйк йцйад';
    var select_arrival_date = 'бзш ъашйк зжшд';
    var cancel_calendar = "бйием";
    var language_direction = 'rtl';
    break;
  default:
    var MonthsArrHeb = new Array ("йреаш", "фбшеаш", "ошх", "афшйм", "оай", "йерй", "йемй", "аевеси", "сфиобш", "аечиебш", "ребобш", "гцобш");
    var WeekDaysArrHeb = new Array ("'щ", "'е", "'д", "'г", "'в", "'б", "'а");
    var select_depart_date = 'бзш ъашйк йцйад';
    var select_arrival_date = 'бзш ъашйк зжшд';
    var cancel_calendar = "бйием";
    var language_direction = 'rtl';
  }

var WaitForResponse = false;

var ARR_DATES = new Array();

function Str2Int(str, start, length)
{
	var retval = 0;
	for (var i = start; i < start + length; i++)
	{
        retval = retval*10 + parseInt(str.substr(i, 1), 10);
	}
	return parseInt(retval, 10);
}

var holidays = { '':1 };

function holiday(day,month,year) {
  if (day<10) day = '0'+day
  if (month<10) month = '0'+month
  return holidays[year+month+day] == 1;
  }

function calendar(owner, pid, isDep)
{
	this.pid = pid;
	this.owner = owner;

	this.parent = document.getElementById(pid);
	this.parent.calendar = this;

	this.today = new Date();
	this.currentMonth = this.today.getMonth();
	this.currentYear = this.today.getYear();

	// Main calendar span
	this.MainSpan = document.createElement("SPAN");
	this.MainSpan.className = (isDep) ? "depCalendar" : "retCalendar";
	this.MainSpan.style.position = "absolute";
	this.MainSpan.style.direction = "ltr";
	this.MainSpan.style.display = "inline";
	this.MainSpan.style.overflow = "hidden"

	this.Title = document.createElement("SPAN");
	this.Title.className = "calendarTitle";
	// Close
	this.CloseSpan = document.createElement("SPAN");
	this.CloseSpan.className = "close";
	this.CloseSpan.style.position = "absolute";
	this.CloseSpan.onclick = function() {
		obj = document.getElementById(pid);
		obj.calendar.MainSpan.style.display = "none";
	}
	this.CloseSpan.onmouseover = function() {this.className = "closeHover";}
	this.CloseSpan.onmouseout = function() {this.className = "close";}
	// Main Table
	this.MainTable = document.createElement("TABLE");
	this.MainTable.cellSpacing = "0";
	this.MainTable.cellPadding = "0";
	this.MainTable.className = "mainTable";
	if (language_direction.toLowerCase() != 'rtl') this.MainTable.style.direction='rtl';
	var tHead = document.createElement("THEAD");
	var tHeadRow = document.createElement("TR");
	var leftTh = document.createElement("TH");
	var centerTh = document.createElement("TH");
	var rightTh = document.createElement("TH");
	var tBody = document.createElement("TBODY");
	var tBodyRow = document.createElement("TR");
	var tBodyCell = document.createElement("TD")

    // Left Button
	this.LeftButton = document.createElement("SPAN");
	this.LeftButton.className = "leftButton";
	this.LeftButton.onmouseover = function () {this.style.cursor="pointer";}
	this.LeftButton.onclick = function () {
		obj = document.getElementById(pid);
		var calendar = obj.calendar;
		calendar.currentMonth++;
		if (calendar.currentMonth==12)
		{
			calendar.currentMonth = 0;
			calendar.currentYear++;
		}
		calendar.UpdateHeader();
		calendar.destination = parseInt(calendar.destination,10) + parseInt(calendar.scrollingLength,10);
		//alert(calendar.currentPosition +'/'+ calendar.destination)
		calendar.Scroll();
	}

	// Right Button
	this.RightButton = document.createElement("SPAN");
	this.RightButton.className = "rightButton";
	this.RightButton.onmouseover = function () {this.style.cursor="pointer";}
	this.RightButton.onclick = function () {
		obj = document.getElementById(pid);
		var calendar = obj.calendar;
		calendar.currentMonth--;
		//alert(calendar.currentMonth+'/'+calendar.currentYear)
		if (calendar.currentMonth<0)
		{
			calendar.currentMonth = 11;
			calendar.currentYear--;
		}
		calendar.UpdateHeader();
		calendar.destination = parseInt(calendar.destination,10) - parseInt(calendar.scrollingLength,10);
		//alert(calendar.currentPosition +'/'+ calendar.destination)
		calendar.Scroll();
	}

	// Month Year header
	this.MonthYear = document.createElement("SPAN");
	this.MonthYear.className = "monthYearHeader";
	this.MonthYear.innerText = MonthsArrHeb[this.currentMonth] + ' ' + this.currentYear;

	// Content span
	this.Content = document.createElement("SPAN");
//	this.Content.className = "content";
	this.Content.style.position = "absolute";

    this.parent.appendChild(this.MainSpan);
    this.MainSpan.appendChild(this.Title);
    this.MainSpan.appendChild(this.MainTable);
	this.MainTable.appendChild(tHead);
	tHead.appendChild(tHeadRow);
	tHeadRow.appendChild(leftTh);tHeadRow.appendChild(centerTh);tHeadRow.appendChild(rightTh);
	leftTh.appendChild(this.LeftButton);centerTh.appendChild(this.MonthYear);rightTh.appendChild(this.RightButton);

	this.MainTable.appendChild(tBody);
	tBody.appendChild(tBodyRow);tBodyRow.appendChild(tBodyCell);tBodyCell.appendChild(this.Content);
	this.MainSpan.appendChild(this.CloseSpan);
	// methods
    this.Init = function()
    {
    	this.Title.style.width = this.MainSpan.offsetWidth + "px";
    	this.Title.style.textAlign = "center";
    	this.CloseSpan.style.width = this.MainSpan.offsetWidth + "px";
	    this.CloseSpan.style.textAlign = "center";
	    this.CloseSpan.innerHTML = cancel_calendar;

	    this.CloseSpan.style.top = this.MainSpan.offsetHeight - this.CloseSpan.offsetHeight + "px";
		this.CloseSpan.style.left = "0px";
        this.MainTable.style.width = this.MainSpan.offsetWidth + "px";
        this.scrollingLength = this.MainSpan.offsetWidth - 2;
        this.currentPosition = this.destination = (-7) * this.scrollingLength;
    	leftTh.width = (this.LeftButton.offsetWidth) +"px";
    	rightTh.width = (this.RightButton.offsetWidth) + "px";
     	centerTh.width =  (this.MainSpan.offsetWidth - this.LeftButton.offsetWidth - this.RightButton.offsetWidth) + "px";
        this.Content.style.left = this.MainSpan.offsetWidth * (-7);
    	this.RightButton.style.display = "none";
    	this.scrollingLength = this.MainSpan.offsetWidth;
    }
	this.CreateCalendar = function()
    {
    	var CalendarTable = document.createElement("Table");
			CalendarTable.cellPadding = CalendarTable.cellSpacing = 0;
			CalendarTable.style.width = 8 * this.MainSpan.offsetWidth + "px";
		CalendarTable.CalendarTableBody = document.createElement("TBody");
		CalendarTable.CalendarTableBody.CalendarTableRow = document.createElement("TR");

        CalendarTable.appendChild(CalendarTable.CalendarTableBody);
        CalendarTable.CalendarTableBody.appendChild(CalendarTable.CalendarTableBody.CalendarTableRow);
        this.Monthes = new Array();
        for (var i = 0; i < 8; i++)
        {
         	var CalendarTableMonth = document.createElement("TD");
			CalendarTableMonth.style.verticalAlign = "top";
			this.Monthes[i] = document.createElement("Table");
			this.Monthes[i].style.width = this.MainTable.offsetWidth + "px";
			this.Monthes[i].cellPadding = this.Monthes[i].cellSpacing = "0px";

			CalendarTable.CalendarTableBody.CalendarTableRow.appendChild(CalendarTableMonth);
			CalendarTableMonth.appendChild(this.Monthes[i]);

			this.Monthes[i].header = document.createElement("THead");
			this.Monthes[i].headerRow = document.createElement("TR");
			this.Monthes[i].appendChild(this.Monthes[i].header);
			this.Monthes[i].header.appendChild(this.Monthes[i].headerRow);
			this.Monthes[i].headerRow.Cells = new Array();

      		for (var weekDay = 0; weekDay < 7; weekDay++)
      		{
         		this.Monthes[i].headerRow.Cells[weekDay] = document.createElement("TH");
				if (i==6) {this.Monthes[i].headerRow.Cells[weekDay].className = "saturdayHeader";}
				else {this.Monthes[i].headerRow.Cells[weekDay].className = "daysHeader";}
				this.Monthes[i].headerRow.Cells[weekDay].innerHTML = WeekDaysArrHeb[weekDay];
				this.Monthes[i].headerRow.Cells[weekDay].style.textAlign = "center";
				this.Monthes[i].headerRow.appendChild(this.Monthes[i].headerRow.Cells[weekDay]);
      		}
			CalendarTable.CalendarTableBody.CalendarTableRow.appendChild(CalendarTableMonth);
            this.Monthes[i].tBody = document.createElement("TBody");
			this.Monthes[i].tBodyRows = new Array();
			for (var row = 0; row < 7; row++)
			{
				this.Monthes[i].tBodyRows[row] = document.createElement("TR");
				this.Monthes[i].tBodyRows[row].Cells = new Array();
				for (var col = 0; col < 7; col++)
				{
					this.Monthes[i].tBodyRows[row].Cells[col] = document.createElement("TD");
					this.Monthes[i].tBodyRows[row].Cells[col].style.textAlign = "center";
					this.Monthes[i].tBodyRows[row].Cells[col].span = document.createElement("SPAN");
					this.Monthes[i].tBodyRows[row].Cells[col].appendChild(this.Monthes[i].tBodyRows[row].Cells[col].span);
					this.Monthes[i].tBodyRows[row].appendChild(this.Monthes[i].tBodyRows[row].Cells[col]);
				}
				this.Monthes[i].tBody.appendChild(this.Monthes[i].tBodyRows[row]);
			}
			this.Monthes[i].appendChild(this.Monthes[i].tBody);
        }
        this.Content.appendChild(CalendarTable);
        this.FillCalendar();
    }
    this.FillCalendar = function()
    {
    	var cellWidth = parseInt(this.MainTable.offsetWidth / 9, 10) + "px";
    	var month = this.today.getMonth();
		var year = this.today.getYear();
		var past;

        for (i = 7; i >= 0; i--)
        {
        	var d = new Date(year, month, 1);
        	var day = 1 - d.getDay();
            var numOfDaysInThisMonth = NumOfDays[month];
        	for (var row = 0; row < 7; row++)
			{
				for (var col = 6; col >= 0; col--)
				{
					past = false;
					//HTML
					if (day > 0 && day <= numOfDaysInThisMonth)
					{
						if (month == this.today.getMonth() && (day-min_days) < this.today.getDate())
						{this.Monthes[i].tBodyRows[row].Cells[col].span.className="pastDays";past=true;}
						else if (month == this.today.getMonth() && day == this.today.getDate())
						{this.Monthes[i].tBodyRows[row].Cells[col].span.className="today";}
						else if (col==0)
						{this.Monthes[i].tBodyRows[row].Cells[col].span.className = "saturdayCell";}
						else if (holiday(day,1+month,year))
						{this.Monthes[i].tBodyRows[row].Cells[col].span.className = "holidayCell";}
						else
						{this.Monthes[i].tBodyRows[row].Cells[col].span.className = "cell";}

						this.Monthes[i].tBodyRows[row].Cells[col].span.style.width = cellWidth;

						if (!past && !holiday(day,1+month,year))
						{
							this.Monthes[i].tBodyRows[row].Cells[col].span.date = this.AddZero(day) + "/" + this.AddZero(month + 1) + "/" + year;
							this.Monthes[i].tBodyRows[row].Cells[col].span.onmouseover = function()
							{
								if (!showAvailability || this.className=='depCell' || this.className=='retCell') {
									this.previousClassName = this.className;
									this.className = "onMouseOver";
								}
							}
							this.Monthes[i].tBodyRows[row].Cells[col].span.onmouseout = function() {
							if (!showAvailability || this.className=='depCell' || this.className=='retCell' || this.className=='onMouseOver')
								{this.className = this.previousClassName;}
							}
							this.Monthes[i].tBodyRows[row].Cells[col].span.onclick = function()
							{
								if(!showAvailability || this.className=='onMouseOver') {
									obj = document.getElementById(pid);
									obj.calendar.MainSpan.style.display = "none";
									obj.calendar.owner.SetDates(this.date, obj.calendar.isDep);
								}
							}
						}

						this.Monthes[i].tBodyRows[row].Cells[col].span.innerHTML = day;
					}
			    	day++;
				}
			}
			month++;
			if (month==12)
			{
				year++;
				month = 0;
			}
        }
    }
    this.AddZero = function(val)
	{
		if (!val || val > 9)
		{
			return val;
		}
		return '0' + val;
	}
	this.Scroll = function ()
	{
	//alert(this.currentPosition +'/'+ this.destination)
		if (this.currentPosition == this.destination) return;
		var speed = parseInt((Math.abs(this.currentPosition - this.destination))/10 + 1, 10)
		var direction = (this.currentPosition < this.destination)? -1: 1
		this.Content.style.left = this.currentPosition + speed*(direction==1?-1:1) + "px"
		this.currentPosition = this.currentPosition + speed*(direction==1?-1:1)

		functionName = "window."+pid+".calendar.Scroll()";
		setTimeout(functionName, 5);
	}
	this.UpdateHeader = function()
	{
		if (this.isDep)
    	{
	    	this.Title.innerHTML = select_depart_date;
	    }
	    else
	    {
	    	this.Title.innerHTML = select_arrival_date;
	    }
		this.MonthYear.innerText = MonthsArrHeb[this.currentMonth] + ' ' + this.currentYear;
		if (this.currentMonth==this.today.getMonth())
		{
			this.RightButton.style.display = "none";
			this.LeftButton.style.display = "inline";
		}
		else if (this.currentMonth == (this.today.getMonth() + 7) % 12)
		{
			this.RightButton.style.display = "inline";
			this.LeftButton.style.display = "none";
		}
		else
		{
			this.RightButton.style.display = this.LeftButton.style.display = "inline";
		}
	}
    this.Open = function(depFlag, goDate, showAvailability)
    {
// alert('-'+depFlag+','+goDate+','+showAvailability+'-')
		if (document.getElementById('timerdiv')) { document.getElementById('timerdiv').style.display='inline'; }
    	if (showAvailability && WaitForResponse)
    	{
    		var functionName = "window."+this.pid+".calendar.Open("+depFlag+",'"+goDate+"',"+showAvailability+")";
    		setTimeout(functionName, 75);
    		return;
    	}

    	this.isDep = depFlag;

		if (this.goDate)
		{
			this.MarkDate(this.goDate, 1);
		}
		this.goDate = goDate;
		this.MarkDate(this.goDate);

        this.MainSpan.style.display = "inline";
        this.currentMonth = Str2Int(this.goDate, 3, 2) - 1;
        if (language_direction=='ltr') {
			this.currentMonth = (this.today.getMonth() + 7) % 12;
			}
		else {
			this.currentYear = Str2Int(this.goDate, 6, 4);
			}

		var dt=this.currentMonth >= this.today.getMonth()?this.currentMonth - this.today.getMonth():this.currentMonth - this.today.getMonth()+12;
    	this.Content.style.left = this.MainSpan.offsetWidth * ((-7 + dt)%12);
        //alert(this.Content.style.left);
		this.scrollingLength = this.MainSpan.offsetWidth;


        this.currentPosition = this.destination = (-7 + dt) * this.scrollingLength;
	   // alert(this.currentMonth +'/'+this.today.getMonth()+'/'+this.scrollingLength)
		if (showAvailability)
		{
			this.MarkDays();
		}
		this.UpdateHeader();
		if (document.getElementById('timerdiv')) { document.getElementById('timerdiv').style.display='none'; }
    }
    this.MarkedCells = new Array();
    this.MarkDays = function()
    {
    	try{
	    	this.MarkedCells = new Array();
	    	var total = 0;
	      	for (var city in ARR_DATES);

	      	if (!this.isDep)
	      	{
	        	var retDatesArr = new Array();
		        var goDateIndex = ''+this.AddZero(parseInt(this.goDate.substr(6, 4), 10)-2000) + this.AddZero(parseInt(this.goDate.substr(3, 2), 10)) + this.AddZero(parseInt(this.goDate.substr(0, 2), 10));
	 	       	for (x in ARR_DATES[city][goDateIndex])
	  		   		for (y in ARR_DATES[city][goDateIndex][x])
	    	    		retDatesArr[y] = 1;
	    	}
	       	var month = this.today.getMonth();
			var year = this.today.getYear();
			var past;
		 	for (i = 7; i >= 0; i--)
		    {
		       	var d = new Date(year, month, 1);
		       	var day = 1 - d.getDay();
		        var numOfDaysInThisMonth = NumOfDays[month];
		       	for (var row = 0; row < 7; row++)
				{
					for (var col = 6; col >= 0; col--)
					{
						if (day < this.today.getDate() && month == this.today.getMonth() && year == this.today.getYear())
						{
							day++;
							continue;
						}
						var index = ''+this.AddZero(year-2000)+this.AddZero(month+1)+this.AddZero(day);
						if (this.isDep)
						{
							if (ARR_DATES[city][index] != null)
							{
								if (this.Monthes[i].tBodyRows[row].Cells[col].span.className!="selectedDay")
								{
									this.Monthes[i].tBodyRows[row].Cells[col].span.prevClassName = this.Monthes[i].tBodyRows[row].Cells[col].span.className;
									this.Monthes[i].tBodyRows[row].Cells[col].span.className = "depCell";
									this.MarkedCells[total] = new Array();
									this.MarkedCells[total] = [i, row, col];
									total++;
								}
							}
						}
						else
						{
							if (retDatesArr[index] != null)
							{
								if (this.Monthes[i].tBodyRows[row].Cells[col].span.className!="selectedDay")
								{
									this.Monthes[i].tBodyRows[row].Cells[col].span.prevClassName = this.Monthes[i].tBodyRows[row].Cells[col].span.className;
									this.Monthes[i].tBodyRows[row].Cells[col].span.className = "retCell";
									this.MarkedCells[total] = new Array();
									this.MarkedCells[total] = [i, row, col];
									total++;
								}
							}
						}
				    	day++;
					}
				}
				month++;
				if (month==12)
				{
					year++;
					month = 0;
				}
			}
		}
		catch(ex) {}
	}
    this.ClearMarkedDays = function()
    {
		for (var index in this.MarkedCells)
		{
			this.Monthes[this.MarkedCells[index][0]].tBodyRows[this.MarkedCells[index][1]].Cells[this.MarkedCells[index][2]].span.className = this.Monthes[this.MarkedCells[index][0]].tBodyRows[this.MarkedCells[index][1]].Cells[this.MarkedCells[index][2]].span.prevClassName;
		}
    }
    this.MarkDate = function(date, clearFlag)
    {
    	var d = Str2Int(date, 0 , 2);
		var m = Str2Int(date, 3 , 2) - 1;
		var y = Str2Int(date, 6 , 4);
		if (m == -1)
		{
			m = 11;
			y--;
		}
		var tempDate = new Date(y, m, d);
		var tempFirstMonthDay = new Date(y, m, 1);

		var monthIndex = (7 - m + this.today.getMonth() ) % 12;
        var colIndex = 6 - tempDate.getDay();
		var rowIndex = parseInt((d + tempFirstMonthDay.getDay()) / 7, 10);
		rowIndex -= ((d + tempFirstMonthDay.getDay()) % 7)==0 ? 1 : 0;
		//alert(monthIndex+" "+rowIndex+" "+colIndex);
		//alert(this.today.getMonth())
		if (clearFlag)
		{
			this.Monthes[monthIndex].tBodyRows[rowIndex].Cells[colIndex].span.className = this.prevCellClassName;
		}
		else
		{
			this.prevCellClassName = this.Monthes[monthIndex].tBodyRows[rowIndex].Cells[colIndex].span.className;

			this.Monthes[monthIndex].tBodyRows[rowIndex].Cells[colIndex].span.className = "selectedDay";
		}
    }

    this.Init();
    this.CreateCalendar();
    this.MainSpan.style.display = "none";
}


function ScrollingCalendar(goParentId, goInputId, retParentId, retInputId, intervalId, dealtypeId, citiesSelectId)
{
	this.goInput = document.getElementById(goInputId);
    var check = this.goInput.offsetLeft;
    if (check==0)
    {
    	throw("error");
    }
    this.retInput = document.getElementById(retInputId);
    this.intervalSelect = document.getElementById(intervalId);

    this.defaultDate = this.startDate = this.goInput.value;
    this.returnDate = this.retInput.value;
    this.interval = parseInt(this.intervalSelect.value, 10);

    this.dealtype = document.all.Pform.dealtype;//document.getElementById(dealtypeId);
    this.citiesSelect = document.getElementById(citiesSelectId);

	this.goCalendar = new calendar(this, goParentId, 1);
	this.retCalendar = new calendar(this, retParentId);

	this.Open = function(retFlag)
	{
        try
        {
			for (var i = 0; i < this.dealtype[i] != null && !this.dealtype[i].checked; i++);
		}
		catch (e)
		{
			if (this.dealtype.type!='hidden') for (var i = 0; i < this.dealtype[i] != null && !this.dealtype[i].selected; i++);
		}
		if (this.dealtype.value!='SCEDUALE' && this.dealtype.value!='WSFLIGHT' && this.dealtype.value!='HOTEL') {
			if (this.dealtype.value!='SCEDUALE' && this.dealtype.value!='WSFLIGHT' && (this.dealtype.type=='hidden' && this.dealtype.value=='CHARTER' || this.dealtype.value=='NOFSHON') || (this.dealtype.type!='hidden' && (this.dealtype[i].value == 'CHARTER' || this.dealtype[i].value == 'NOFSHON')))
			{
				GetDealsArray(this.citiesSelect.value, (this.dealtype.type=='hidden' ? this.dealtype.value : this.dealtype[i].value) );
				WaitForResponse = true;
				showAvailability = true;
			}
			else
			{
				WaitForResponse = false;
				showAvailability = false;
			}
		} else {
			WaitForResponse = false;
			showAvailability = false;
		  }
		if (retFlag==null || retFlag==0)
		{
			this.retCalendar.MainSpan.style.display = "none";
			this.goCalendar.ClearMarkedDays();
			this.goCalendar.Open(true, this.startDate, showAvailability);
		}
		else
		{
			this.goCalendar.MainSpan.style.display = "none";
			this.retCalendar.ClearMarkedDays();
			this.retCalendar.MainSpan.className = "retCalendar";
			this.retCalendar.Open(false, this.startDate, showAvailability);
		}
	}
	this.SetDates = function(date, isDep)
	{
		if (isDep)
		{
			this.startDate = date;
			this.goInput.value = this.startDate;
			this.returnDate = this.AddDays(this.startDate, this.interval);
			this.retInput.value = this.returnDate;

			this.Open(1);
			return;
		}
		else
		{
			this.returnDate = date;
			this.retInput.value = this.returnDate;
            var tmp = this.GetDaysDifference();
            this.intervalSelect.selectedIndex = (0 < tmp && tmp < this.intervalSelect.length) ? tmp : 0;
            this.interval = (0 < tmp && tmp < 11) ? tmp : 0;

			if (tmp < 0)
			{
				this.startDate = this.returnDate;
				this.goInput.value = this.startDate;
				this.intervalSelect.selectedIndex = 0;
			}
		}
	}
	this.GetDaysDifference = function()
	{
		var d1 = Str2Int(this.startDate, 0, 2);
		var m1 = Str2Int(this.startDate, 3, 2) - 1;
		var y1 = Str2Int(this.startDate, 6, 4);
		if (m1 == -1) {m1 = 11; y1--;}
		var d2 = Str2Int(this.returnDate, 0, 2);
		var m2 = Str2Int(this.returnDate, 3, 2) - 1;
		var y2 = Str2Int(this.returnDate, 6, 4);
		if (m2 == -1) {m2 = 11; y2--;}
        var retval = (y2 - y1) * 365;
        for (var i = 0; i < m2; i++)
        	retval += NumOfDays[i];
        for (var i = 0; i < m1; i++)
        	retval -= NumOfDays[i];
        retval += d2 - d1;
        return retval;
	}
	this.AddZero = function(val)
	{
		if (!val || val > 9)
		{
			return val;
		}
		return '0' + val;
	}
	this.AddDays = function(date, numOfDays)
	{
		if(!numOfDays) numOfDays = 3;
		var d = Str2Int(date, 0, 2) + parseInt(numOfDays, 10);
		var m = Str2Int(date, 3, 2) - 1;
		var y = Str2Int(date, 6, 4);

		if (m == -1){m = 11;y--;}
		if (d > NumOfDays[m]) {
			d = d - NumOfDays[m];
			m++;
			if (m == 12)
			{
				m = 0;
				y++;
			}
		}
		return this.AddZero(d) + "/" + this.AddZero(m + 1) + "/" + y;
	}
    this.ChangeInterval = function(numOfDays)
    {
        this.interval = isNaN(parseInt(numOfDays, 10)) ? 0 : parseInt(numOfDays, 10);
        this.returnDate = this.AddDays(this.startDate, this.interval);
        this.retInput.value = this.returnDate;
    }
	this.CloseAll = function()
	{
		this.goInput.value = this.startDate = this.defaultDate;
		this.goCalendar.ClearMarkedDays();
		this.retCalendar.ClearMarkedDays();
		this.retCalendar.MainSpan.style.display = "none";
		this.goCalendar.MainSpan.style.display = "none";
	}
}


function GetDealsArray(citycode, dealtype) {
    if (WaitForResponse)
    {
    	return;
    }
    WaitForResponse = true;
	var req = "<request><function>get_array</function><value>"+citycode+"</value><argument>"+dealtype+"</argument></request>";
	var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	xmlhttp.open("POST", "/midoffice/checkval.php", true);
	xmlhttp.setRequestHeader("content-type", "application/xml");
  	xmlhttp.onreadystatechange = function ()
  	{
  		var THIS_ARR_DATES = new Array();
	 	if (xmlhttp.readyState == 4)
	 	{
		 	WaitForResponse = false;
			try
			{
				eval(xmlhttp.responseText);
			}catch(e){}
		}
	}
	xmlhttp.send(req);
}
