/*
requires initialisation of the following variables:
curDay, curMonth (0-11), curYear
short month name array arrMonth

requires setting dates cookie as below BEFORE instantializing the TravDates object:
SetDatesCookie(<%=Day(varCheckin)%>, <%=Month(varCheckin)%>, <%=Year(varCheckin)%>, <%=Day(varCheckout)%>, <%=Month(varCheckout)%>, <%=Year(varCheckout)%>)
*/


// TRAVEL DATES OBJECT START

//travel dates object constructor
function TravDates(inYCtl, inMCtl, inDCtl, outYCtl, outMCtl, outDCtl, startBlank, numYears) {
	if (numYears == null) numYears = 3  //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

	if (
		GetQSVal("inDay") != "" && GetQSVal("inMonth") != "" && GetQSVal("inYear") != "" &&
		GetQSVal("outDay") != "" && GetQSVal("outMonth") != "" && GetQSVal("outYear") != ""
	) {  //if all dates were passed in querystring
		defInD = GetQSVal("inDay")
		defInM = GetQSVal("inMonth")
		defInY = GetQSVal("inYear")
		defOutD = GetQSVal("outDay")
		defOutM = GetQSVal("outMonth")
		defOutY = GetQSVal("outYear")
	} else if (GetQSVal("Checkin") != "" && GetQSVal("Checkout") != "") {  //if all dates were passed in querystring
		var dateParts = GetQSVal("Checkin").split("-")
		defInD = dateParts[2]
		defInM = dateParts[1]
		defInY = dateParts[0]

		dateParts = GetQSVal("Checkout").split("-")
		defOutD = dateParts[2]
		defOutM = dateParts[1]
		defOutY = dateParts[0]
	} else {
		//extract dates from cookie
		var oSECookie = GetDatesCookie()

		if (oSECookie.inDate == "") {  //there are no dates in cookie
			if (!startBlank) {
				//set default dates as follows:
				var defInDate = new Date(curYear, curMonth, curDay + 18)
				var defOutDate = new Date(curYear, curMonth, curDay + 20)

				defInD = defInDate.getDate()
				defInM = defInDate.getMonth() + 1
				defInY = defInDate.getFullYear()
				defOutD = defOutDate.getDate()
				defOutM = defOutDate.getMonth() + 1
				defOutY = defOutDate.getFullYear()
			}
		} else {
			//get defaults from cookie
			var inDArray = oSECookie.inDate.split("-")
			var outDArray = oSECookie.outDate.split("-")

			defInY = inDArray[0]
			defInM = inDArray[1]
			defInD = inDArray[2]
			defOutY = outDArray[0]
			defOutM = outDArray[1]
			defOutD = outDArray[2]
		}
	}

	//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, curDay)

	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 in milliseconds
		alert("Your period of stay should be not longer than 25 nights.\n\nIf you wish to book for more than 25 nights,\nplease send us an e-mail with your request.")
		this.outDCtl.focus()
		return false
	}

	if (checkoutDate - checkinDate <= 0) {
		alert("Please ensure that the Check-out Date is after the Check-in Date.")
		this.outDCtl.focus()
		return false
	}

	//save dates to cookie
	SetDatesCookie(GetSelVal(this.inDCtl), GetSelVal(this.inMCtl), GetSelVal(this.inYCtl), GetSelVal(this.outDCtl), GetSelVal(this.outMCtl), GetSelVal(this.outYCtl))

	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) + " "	
}

// TRAVEL DATES OBJECT END


//save dates to cookie
function SetDatesCookie(inD, inM, inY, outD, outM, outY) {
	var oSECookie = GetDatesCookie()

	if (isNaN(inD) || isNaN(inM) || isNaN(inY) || isNaN(outD) || isNaN(outM) || isNaN(outY)) {
		oSECookie.inDate = ""
		oSECookie.outDate = ""
	} else {
		oSECookie.inDate = inY + "-" + inM + "-" + inD
		oSECookie.outDate = outY + "-" + outM + "-" + outD
	}

	document.cookie = 
		escape("|SearchEng|") + "=" +
		escape("|" + oSECookie.country + "|" + oSECookie.city + "|" + oSECookie.suburb + "|" + oSECookie.inDate + "|" + oSECookie.outDate + "|") +
		";path=/"
}


//returns object containing search engine cookie elements
function GetDatesCookie() {
	var oSECookie = new Object()

	oSECookie.country = ""
	oSECookie.city = ""
	oSECookie.suburb = ""
	oSECookie.inDate = ""
	oSECookie.outDate = ""

	var cookieArray = URLDecode(document.cookie).split("|")

	for (var i=0; i<cookieArray.length; i++) {
		if (cookieArray[i] == "SearchEng") {
			oSECookie.country = cookieArray[i+2]
			oSECookie.city = cookieArray[i+3]
			oSECookie.suburb = cookieArray[i+4]
			oSECookie.inDate = cookieArray[i+5]
			oSECookie.outDate = cookieArray[i+6]

			break
		}
	}

	return oSECookie
}

