//travel dates object constructor
function TravDates(inYCtl, inMCtl, inDCtl, outYCtl, outMCtl, outDCtl, startBlank, numYears) {
	if (numYears == null) numYears = 2  //default number of years for the calendar

	//properties
	this.inYCtl = inYCtl
	this.inMCtl = inMCtl
	this.inDCtl = inDCtl
	this.outYCtl = outYCtl
	this.outMCtl = outMCtl
	this.outDCtl = outDCtl

	//methods
	this.SetDef = SetDefDates
	this.Validate = ValidateDates
	this.GetDate = GetSelDate
	this.SetDate = SetSelDate
	this.SetLenStay = SetLenStay

	//populate select boxes
	if (startBlank) {
		var presOpt = 0
	} else {
		var presOpt = -1
	}

	populateDay(inDCtl, presOpt)
	populateMonth(inMCtl, presOpt)
	populateYear(inYCtl, presOpt, numYears)
	populateDay(outDCtl, presOpt)
	populateMonth(outMCtl, presOpt)
	populateYear(outYCtl, presOpt, numYears)

	//set default values
	this.SetDef(startBlank)

	//instatialise calendar object
	if (!document.layers) {
		cal = new Calendar(numYears, SetCalDate)
		window.onfocus = new Function("if (cal.win) cal.win.close()")
	}


	//-----------------------


	//populate day select box
	function populateDay(ctl, presOpt) {
		clearSelect(ctl, presOpt)

		var newOpt

		for (var i = 1 + presOpt; i < 32 + presOpt; i++) {
			newOpt = i - presOpt
			ctl[i] = new Option(newOpt, newOpt)
		}
	}


	//populate month select box
	function populateMonth(ctl, presOpt) {
		clearSelect(ctl, presOpt)
		for (var i = 1 + presOpt; i < 13 + presOpt; i++) ctl[i] = new Option(arrMonth[i-1-presOpt], i-presOpt)
	}


	//populate year select box
	function populateYear(ctl, presOpt, numYears) {
		clearSelect(ctl, presOpt)

		var newOpt

		for (var i = 1 + presOpt; i <= numYears + presOpt; i++) {
			newOpt = i + curYear - 1 - presOpt
			ctl[i] = new Option(newOpt, newOpt)
		}
	}

	//clear options of select control
	function clearSelect(ctl, presOpt) {
		for (var i = ctl.options.length - 1; i > presOpt; i--) ctl.options[i] = null
	}
}


//set default dates
function SetDefDates(startBlank) {
	var defInD, defInM, defInY, defOutD, defOutM, defOutY

  down = getDownLimit() 

	//set default dates as follows:
	var defInDate = new Date(curYear, curMonth-1, curDay + down)
	var defOutDate = new Date(curYear, curMonth-1, curDay + down+3)

	defInD = defInDate.getDate()
	defInM = defInDate.getMonth() + 1
	defInY = defInDate.getFullYear()
	defOutD = defOutDate.getDate()
	defOutM = defOutDate.getMonth() + 1
	defOutY = defOutDate.getFullYear()

	//set dates
	if (!isNaN(defInD)) {
		this.SetDate(defInY, defInM, defInD, "in")
		this.SetDate(defOutY, defOutM, defOutD, "out")
	}
}

//validate dates and save them to cookie
function ValidateDates() {
	//check that both dates are selected
	if (SelEmpty(this.inDCtl) || SelEmpty(this.inMCtl) || SelEmpty(this.inYCtl) || SelEmpty(this.outDCtl) || SelEmpty(this.outMCtl) || SelEmpty(this.outYCtl)) {
		return false
	}

	var checkinDate = this.GetDate("in")
	var checkoutDate = this.GetDate("out")

	//validate checkin date
	if (!validDate(checkinDate, this.inDCtl)) {
		alert("Please select a valid Check-in Date.")
		this.inDCtl.focus()
		return false
	}

	var curDate = new Date(curYear, curMonth-1, curDay)
	var minDate = new Date(curYear, curMonth-1, curDay + down)
	var maxDate = new Date(curYear, curMonth-1, curDay + down + 90)

	if (checkinDate - curDate < 0) {
		alert("The Check-in Date you have selected is in the past.")
		this.inDCtl.focus()
		return false
	}

	//validate checkout date
	if (!validDate(checkoutDate, this.outDCtl)) {
		alert("Please select a valid Check-out Date.")
		this.outDCtl.focus()
		return false
	}

	//validate checkin - checkout difference
	if (checkoutDate - checkinDate > 2160000000) {  //25 days max difference in milliseconds
		alert("Your period of stay should be not longer than 25 nights.")
		this.outDCtl.focus()
		return false
	}

	if (checkoutDate <= checkinDate ) {  // 1 days min difference in milliseconds
		alert("Please note that minimal tour duration is 1 days.\n\nPlease check and change your Start Date and End Date accordingly.")
		this.outDCtl.focus()
		return false
	}

	if (checkinDate - minDate < 0) {
		alert("There is not enough time left to process your booking.\n\nPlease specify a date at last 5 business days ahead of today.")
		this.inDCtl.focus()
		return false
	}
  
	return true

	//check that date select box has a not empty selection
	function SelEmpty(ctl) {
		if (GetSelVal(ctl) == "") {
			alert("Please select your Travel Dates.")
			ctl.focus()
			return true
		}

		return false
	}
}


//get date selected in dropdown boxes
function GetSelDate(inOut) {
	var y = GetSelVal(eval("this." + inOut + "YCtl"))
	var m = GetSelVal(eval("this." + inOut + "MCtl"))
	var d = GetSelVal(eval("this." + inOut + "DCtl"))

	if (y == "" || m == "" || d == "") {
		return new Date()
	} else {
		return new Date(y, m - 1, d)
	}
}


//set date into dropdown boxes (month 1 to 12)
function SetSelDate(year, month, day, inOut) {
	SetSelVal(eval("this." + inOut + "YCtl"), Number(year))
	SetSelVal(eval("this." + inOut + "MCtl"), Number(month))
	SetSelVal(eval("this." + inOut + "DCtl"), Number(day))

	//set length of stay
	this.SetLenStay()
}


//set length of stay
function SetLenStay() {
 	if (document.all) {
		var stay = document.all.lenStay
	} else if (document.getElementById) {
		var stay = document.getElementById("lenStay")
	}

	if (stay) stay.innerHTML = Math.round((this.GetDate("out") - this.GetDate("in")) / 86400000) + " "	
}

// get min time booking
function getDownLimit() {
  
  var LimitDate = "";
  var result=0;
  var d = ""
  
  for (i=0; i<14; i++) {
    LimitDate = new Date(curYear, curMonth-1, curDay+i)
	  d = LimitDate.getDay()
	  if ((d=="1") || (d=="2") || (d=="3") || (d=="4") || (d=="5")) { //business days
	    if (!CheckHolidays(LimitDate)) { // check for holidays on business days from arrHolidays array
        result += 1
        if (result == 6) {
          return i
        }
      }
    }
  }
  return 0
}

// check for holidays on business days from arrHolidays array
function CheckHolidays(ld) {
  for (j=0; j<arrHolidays.length; j++) {
    var str = (arrHolidays[j]);
    var yr = str.charAt(0);
    yr += str.charAt(1);
    yr += str.charAt(2);
    yr += str.charAt(3);
    var m = str.charAt(4);
    m += str.charAt(5);
    m = eval(m-1);
    var d = str.charAt(6);
    d += str.charAt(7);
    var h = new Date(yr, m, d);
    if ((ld - h) == 0) {
      return true;
    }
  } 
  return false;
}
