/*
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)%>)
*/

if (typeof(dateMask) == "undefined")
	dateMask = "d|m|y|-"

if (typeof(wkdArray) == "undefined")
	var wkdArray = new Array("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat")


// TRAVEL DATES OBJECT START

//travel dates object constructor
function TravDates(inYCtl, inMCtl, inDCtl, inDayCtl, outYCtl, outMCtl, outDCtl, outDayCtl, lenStayID, 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.inDayCtl = inDayCtl

	this.outYCtl = outYCtl
	this.outMCtl = outMCtl
	this.outDCtl = outDCtl	
	this.outDayCtl = outDayCtl
	
	this.lenStayID = lenStayID

	//methods
	this.SetDef = SetDefDates
	this.Validate = ValidateDates
	this.GetDate = GetSelDate
	this.SetDate = SetSelDate
	this.SetLenStay = SetLenStay
	this.SetDaysOfWeek = SetDay
	this.resetDate = ResetDate

	//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)) {
		if (typeof (Common_Scripts_TravelDates_VldCheckIn) == "undefined")
			Common_Scripts_TravelDates_VldCheckIn = "Please select a valid Check-in Date."
			
		alert(Common_Scripts_TravelDates_VldCheckIn)
		this.inDCtl.focus()
		return false
	}

	var curDate = new Date(curYear, curMonth, curDay)

	if (checkinDate - curDate < 0) {
		if (typeof (Common_Scripts_TravelDates_CheckInPast) == "undefined")
			Common_Scripts_TravelDates_CheckInPast = "The Check-in Date you have selected is in the past."
			
		alert(Common_Scripts_TravelDates_CheckInPast)
		this.inDCtl.focus()
		return false
	}

	//validate checkout date
	if (!validDate(checkoutDate, this.outDCtl)) {
		if (typeof (Common_Scripts_TravelDates_VldCheckOut) == "undefined")
			Common_Scripts_TravelDates_VldCheckOut = "Please select a valid Check-out Date."
			
		alert(Common_Scripts_TravelDates_VldCheckOut)
		this.outDCtl.focus()
		return false
	}

	//validate checkin - checkout difference
	if (checkoutDate - checkinDate > 2160000000) {  //25 days in milliseconds
		if (typeof (Common_Scripts_TravelDates_StayTooLong) == "undefined")
			Common_Scripts_TravelDates_StayTooLong = "Your period of stay should be not longer than 25 nights.\n\nIf you wish to book for more than 25 nights, please send us an e-mail with your request."
			
		alert(Common_Scripts_TravelDates_StayTooLong)
		this.outDCtl.focus()
		return false
	}

	if (checkoutDate - checkinDate <= 0) {
		if (typeof (Common_Scripts_TravelDates_OutAfterIn) == "undefined")
			Common_Scripts_TravelDates_OutAfterIn = "Please ensure that the Check-out Date is after the Check-in Date."
			
		alert(Common_Scripts_TravelDates_OutAfterIn)
		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) == "") {
			if (typeof (Common_Scripts_TravelDates_SelectDates) == "undefined")
				Common_Scripts_TravelDates_SelectDates = "Please select your Travel Dates."
				
			alert(Common_Scripts_TravelDates_SelectDates)
			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, bDoReset) {
	if (bDoReset == null) bDoReset = true

	SetSelVal(eval("this." + inOut + "YCtl"), Number(year))
	SetSelVal(eval("this." + inOut + "MCtl"), Number(month))
	SetSelVal(eval("this." + inOut + "DCtl"), Number(day))

	if (bDoReset) {
		//reset the other date
		if (inOut == "in")
			this.resetDate("out")
		else
			this.resetDate("in")
	}
}

//set days of the week
function SetDay() {
	if (this.inDayCtl != null) this.inDayCtl.value = "[" + wkdArray[this.GetDate("in").getDay()] + "]"
	if (this.outDayCtl != null) this.outDayCtl.value  = "[" + wkdArray[this.GetDate("out").getDay()] + "]"	
}

//set length of stay
function SetLenStay() {
	if (this.lenStayID != null) {
	
		if (document.all)
			var stay = document.all[this.lenStayID]
		else if (document.getElementById)
			var stay = document.getElementById(this.lenStayID)
		
		if (stay) // Ask Eugene, for backwards affiliate capabilities
			stay.innerHTML = Math.round((this.GetDate("out") - this.GetDate("in")) / 86400000) + " "
	}

	//set days of the week
	this.SetDaysOfWeek()
}

//reset out date to (in date + 2) if out date <= in date AND reset in date to (out date - 1) if in date >= out date
function ResetDate(InOut) {
	var checkinDate = this.GetDate("in")
	var checkoutDate = this.GetDate("out")

	var checkDate, incremet

	if (InOut == "in") {
		checkDate = checkoutDate
		incremet = -1
	} else {
		checkDate = checkinDate
		incremet = 2	
	}

	if (checkinDate >= checkoutDate) {
		var newDate = new Date(checkDate.getFullYear(), checkDate.getMonth(), checkDate.getDate() + incremet)
		this.SetDate(newDate.getFullYear(), newDate.getMonth() + 1, newDate.getDate(), InOut, false)
	}

	//set length of stay
	this.SetLenStay()
}

// 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
}