//requires initialisation of the following variables:
//curDay, curMonth (0-11), curYear

//date validation
function validDate(date, ctlDay) {
	return date.getDate() == getVal(ctlDay)
}


//clear options of select control
function clearSelect(ctl, preservedOpt) {
	for (var i=ctl.options.length-1; i>preservedOpt; i--) ctl.options[i] = null
}


//populate and set select boxes
function populateDates(frm, startBlank) {
	var preservedOpt

	if (startBlank) preservedOpt = 0
	else preservedOpt = -1

	populateDay(frm.inDay, preservedOpt)
	populateMonth(frm.inMonth, preservedOpt)
	populateYear(frm.inYear, preservedOpt)
	populateDay(frm.outDay, preservedOpt)
	populateMonth(frm.outMonth, preservedOpt)
	populateYear(frm.outYear, preservedOpt)

	setDefDates(frm, startBlank)
}


//populate day select box
function populateDay(ctl, preservedOpt) {
	clearSelect(ctl, preservedOpt)
	
	var newText
	
	for (var i=1+preservedOpt; i<32+preservedOpt; i++) {
		newText = i-preservedOpt
		if (newText < 10) newText = "0" + newText
		ctl[i] = new Option(newText, i-preservedOpt)
	}
}


//populate month select box
function populateMonth(ctl, preservedOpt) {
	var monthArray = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")
	clearSelect(ctl, preservedOpt)
	for (var i=1+preservedOpt; i<13+preservedOpt; i++) ctl[i] = new Option(monthArray[i-1-preservedOpt], i-preservedOpt)
}


//populate year select box
function populateYear(ctl, preservedOpt) {
	clearSelect(ctl, preservedOpt)
	for (var i=1+preservedOpt; i<4+preservedOpt; i++) ctl[i] = new Option(i+curYear-1-preservedOpt, i+curYear-1-preservedOpt)
}


//set default dates
function setDefDates(frm, startBlank) {
	var defInDay, defInMonth, defInYear, defOutDay, defOutMonth, defOutYear
	var noCookie = true

	//extract dates from cookie
	var cookieArray = document.cookie.split("|")

	for (var i=0; i<cookieArray.length; i++) {
		if (cookieArray[i] == "SearchEng") {
			defInDay = parseInt(cookieArray[i+4])
			defInMonth = parseInt(cookieArray[i+5])
			defInYear = parseInt(cookieArray[i+6])
			defOutDay = parseInt(cookieArray[i+7])
			defOutMonth = parseInt(cookieArray[i+8])
			defOutYear = parseInt(cookieArray[i+9])

			noCookie = false

			break
		}
	}

	//use (today+20) for defaults if no cookie set
	if (noCookie && !startBlank) {
		var defInDate = new Date(curYear, curMonth, curDay + 20)
		var defOutDate = new Date(curYear, curMonth, curDay + 21)

		defInDay = defInDate.getDate()
		defInMonth = defInDate.getMonth() + 1
		defInYear = defInDate.getFullYear()
		defOutDay = defOutDate.getDate()
		defOutMonth = defOutDate.getMonth() + 1
		defOutYear = defOutDate.getFullYear()
	}

	//set dates
	if (!noCookie || !startBlank) {
		setVal(frm.inDay, defInDay)
		setVal(frm.inMonth, defInMonth)
		setVal(frm.inYear, defInYear)
		setVal(frm.outDay, defOutDay)
		setVal(frm.outMonth, defOutMonth)
		setVal(frm.outYear, defOutYear)
	}
}


//set select box value
function setVal(ctl, newVal) {
	for (var i=0; i<ctl.length; i++) {
		if (ctl[i].value == newVal) {
			ctl.selectedIndex = i
			break
		}
	}
}


//get select box value
function getVal(ctl) {
	return ctl[ctl.selectedIndex].value
}


//reset out date to (in date + 1)
function resetOutDate(frm) {
	var newOutDate = new Date(getVal(frm.inYear), getVal(frm.inMonth) - 1, parseInt(getVal(frm.inDay)) + 1)

	var newOutDay = newOutDate.getDate()
	var newOutMonth = newOutDate.getMonth() + 1
	var newOutYear = newOutDate.getFullYear()

	setVal(frm.outDay, newOutDay)
	setVal(frm.outMonth, newOutMonth)
	setVal(frm.outYear, newOutYear)
}


//check whether selected value of select box is empty string
function emptySel(ctl) {
	if (getVal(ctl) == "") {
		alert("Please enter your Travel Dates.")
		ctl.focus()
		return true
	}

	return false
}


//validate dates and save them to cookie
function validateDates(frm) {
	if (emptySel(frm.inDay) || emptySel(frm.inMonth) || emptySel(frm.inYear) || emptySel(frm.outDay) || emptySel(frm.outMonth) || emptySel(frm.outYear))
		return false

	var checkinDate = new Date(getVal(frm.inYear), getVal(frm.inMonth) - 1, getVal(frm.inDay))
	var checkoutDate = new Date(getVal(frm.outYear), getVal(frm.outMonth) - 1, getVal(frm.outDay))

	//validate checkin date
	if (!validDate(checkinDate, frm.inDay)) {
		alert("Please enter a valid Checkin Date.")
		frm.inDay.focus()
		return false
	}

	var curDate = new Date(curYear, curMonth, curDay)

	if (checkinDate - curDate < 0) {
		alert("The Checkin Date you have entered is in the past.")
		frm.inDay.focus()
		return false
	}

	//validate checkout date
	if (!validDate(checkoutDate, frm.outDay)) {
		alert("Please enter a valid Checkout Date.")
		frm.outDay.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.")
		frm.outDay.focus()
		return false
	}

	if (checkoutDate - checkinDate <= 0) {
		alert("The Checkout Date should be greater than the Checkin Date.")
		frm.outDay.focus()
		return false
	}

	//save dates to cookie
	setDatesCookie(getVal(frm.inDay), getVal(frm.inMonth), getVal(frm.inYear), getVal(frm.outDay), getVal(frm.outMonth), getVal(frm.outYear))

	return true
}


//save dates to cookie
function setDatesCookie(inDay, inMonth, inYear, outDay, outMonth, outYear) {
	var arrCookie = document.cookie.split("|")
	var i

	var defCountry = ""
	var defCity = ""
	var defSuburb = ""

	for (i=0; i<arrCookie.length; i++) {
		if (arrCookie[i] == "SearchEng") {
			defCountry = arrCookie[i+1]
			defCity = arrCookie[i+2]
			defSuburb = arrCookie[i+3]

			break
		}
	}

	document.cookie = "|SearchEng|" + defCountry + "|" + defCity + "|" + defSuburb + "|"
		+ inDay + "|" + inMonth + "|" + inYear + "|"
		+ outDay + "|" + outMonth + "|" + outYear + "|"
		+ ";path=/"
}
