// Sam's Patented Cookie Code for those who are a little hungry
// Stolen from: http://www.webreference.com/js/column8/functions.html
// name - name of the cookie
// value - value of the cookie
// [expires] - expiration date of the cookie (defaults to end of current session)
// [path] - path for which the cookie is valid (defaults to path of calling document)
// [domain] - domain for which the cookie is valid (defaults to domain of calling document)
// [secure] - Boolean value indicating if the cookie transmission requires a secure transmission
// * an argument defaults when it is assigned null as a placeholder
// * a null placeholder is not required for trailing omitted arguments
function setCookie(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}

// name - name of the desired cookie
// * return string containing value of specified cookie or null if cookie does not exist
function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}

// name - name of the cookie
// [path] - path of the cookie (must be same as path used to create cookie)
// [domain] - domain of the cookie (must be same as domain used to create cookie)
// * path and domain default if assigned null or omitted if no explicit argument proceeds
function deleteCookie(name, path, domain) {
  if (getCookie(name)) {
    document.cookie = name + "=" + 
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

// date - any instance of the Date object
// * hand all instances of the Date object to this function for "repairs"
function fixDate(date) {
  var base = new Date(0);
  var skew = base.getTime();
  if (skew > 0)
    date.setTime(date.getTime() - skew);
}

function loadData() {
	// Loads the data
	// Get date and fix
	var now = new Date();
	fixDate(now);
	var jsreturntrip = getCookie("returntrip");
//	alert(jsreturntrip); alert(getCookie("returntrip"));
	if(jsreturntrip) {
		// we have something to load
		// lets get the traveller details first
		//var jstrtitle = getcookie("trtitle"); 
/*		var jstrfname = getCookie("trfname");
		var jstrsname = getCookie("trsname");
		var jstrphone = getCookie("trphone");
		var jstremail = getCookie("tremail");
		if(jstrfname != null) { this.document.bookingform.trfname.value = jstrfname; }
		if(jstrsname != null) { this.document.bookingform.trsname.value = jstrsname; }
		if(jstrphone != null) { this.document.bookingform.trphone.value = jstrphone; }
		if(jstremail != null) { this.document.bookingform.tremail.value = jstremail; }*/
		
		// Next break point - Pickup
		// They have one way no matter what
		var jsFputype = getCookie("Fputype");
		this.document.bookingform.Fputype.selectedIndex = jsFputype;
		showDetailsForSelection(this.document.bookingform.Fputype, "F");
		switch(this.document.bookingform.Fputype.options[this.document.bookingform.Fputype.selectedIndex].value) {
			case "airport":	this.document.bookingform.Fairport.selectedIndex = getCookie("Fairport");
							this.document.bookingform.Fterminal.selectedIndex = getCookie("Fterminal");
							this.document.bookingform.Ftraveltime.selectedIndex = getCookie("Ftraveltime");
							if(getCookie("Fflightnum") != null) { this.document.bookingform.Fflightnum.value = getCookie("Fflightnum"); }
							break;

			case "resort" :	this.document.bookingform.Fbuildingpu.selectedIndex = getCookie("Fbuildingpu");
							this.document.bookingform.Faltphone.value = getCookie("Faltphone");
							this.document.bookingform.Froomnum.value = getCookie("Froomnum");
							break;
			case "address":	if(getCookie("Fstreetnum") != null) { this.document.bookingform.Fstreetnum.value = getCookie("Fstreetnum"); }
							if(getCookie("Fstreetname") != null) { this.document.bookingform.Fstreetname.value = getCookie("Fstreetname"); }
							if(getCookie("Fsuburb") != null) { this.document.bookingform.Fsuburb.value = getCookie("Fsuburb"); }
							if(getCookie("Fpostcode") != null) { this.document.bookingform.Fpostcode.value = getCookie("Fpostcode"); }
							if(getCookie("Fstreettype") != null) { this.document.bookingform.Fstreettype.selectedIndex = getCookie("Fstreettype"); }

		}

		// Next break point - Dropoff
		var jsFdotype = getCookie("Fdotype");
//		alert(getCookie("Fdotype"));
		this.document.bookingform.Fdotype.selectedIndex = jsFdotype;
		showDetailsForSelection(this.document.bookingform.Fdotype, "Fdo");
		switch(this.document.bookingform.Fdotype.options[this.document.bookingform.Fdotype.selectedIndex].value) {
			case "airport":	this.document.bookingform.Fdoairport.selectedIndex = getCookie("Fdoairport");
							this.document.bookingform.Fdoterminal.selectedIndex = getCookie("Fdoterminal");
							this.document.bookingform.Fdotraveltime.selectedIndex = getCookie("Fdotraveltime");
							if(getCookie("Fdoflightnum") != null) { this.document.bookingform.Fdoflightnum.value = getCookie("Fdoflightnum"); }
							break;

			case "resort" :	this.document.bookingform.Fdobuildingpu.selectedIndex = getCookie("Fdobuildingpu");
							this.document.bookingform.Fdoaltphone.value = getCookie("Fdoaltphone");
							this.document.bookingform.Fdoroomnum.value = getCookie("Fdoroomnum");
							break;
			case "address":	if(getCookie("Fdostreetnum") != null) { this.document.bookingform.Fdostreetnum.value = getCookie("Fdostreetnum"); }
							if(getCookie("Fdostreetname") != null) { this.document.bookingform.Fdostreetname.value = getCookie("Fdostreetname"); }
							if(getCookie("Fdosuburb") != null) { this.document.bookingform.Fdosuburb.value = getCookie("Fdosuburb"); }
							if(getCookie("Fdopostcode") != null) { this.document.bookingform.Fdopostcode.value = getCookie("Fdopostcode"); }
							if(getCookie("Fdostreettype") != null) { this.document.bookingform.Fdostreettype.selectedIndex = getCookie("Fdostreettype"); }
							break;
		}

		// Set the dates up again
		var returndate = getCookie("returndate");
		var traveldate = getCookie("traveldate");
		var x = 0;
		var found = 0;
		for (x = 0; x <= this.document.bookingform.traveldate.length-1; x = x + 1) {
			if(traveldate == this.document.bookingform.traveldate.options[x].value) {					
				this.document.bookingform.traveldate.selectedIndex = x;
				found = 1;		
			}
		}
		if(found == 0) {
			// haven't found the date, so it can't be there, lets add it!
			AddToOptionList(this.document.bookingform.traveldate, traveldate, traveldate);
			this.document.bookingform.traveldate.selectedIndex = this.document.bookingform.traveldate.length-1;
		}

		var x = 0;
		var found = 0;
		for (x = 0; x <= this.document.bookingform.returndate.length-1; x = x + 1) {
			if(returndate == this.document.bookingform.returndate.options[x].value) {
				this.document.bookingform.returndate.selectedIndex = x;
				found = 1;
			}
		}
		if(found == 0) {
			// haven't found the date, so it can't be there, lets add it!
			AddToOptionList(this.document.bookingform.returndate, returndate, returndate);
			this.document.bookingform.returndate.selectedIndex = this.document.bookingform.returndate.length-1;
		}

				// Last but not least, store the passengers
		if(getCookie("Fdopaxadults") !=  null) { this.document.bookingform.Fdopaxadults.value = getCookie("Fdopaxadults"); }
		if(getCookie("Fdopaxchildren") != null) { this.document.bookingform.Fdopaxchildren.value = getCookie("Fdopaxchildren"); }
		if(getCookie("Fdopaxconcession") != null) {	this.document.bookingform.Fdopaxconcession.value = getCookie("Fdopaxconcession"); }
		if(getCookie("Fdopaxinfants") != null) { this.document.bookingform.Fdopaxinfants.value = getCookie("Fdopaxinfants"); }

//		alert(jsFputype);
//		alert(this.document.bookingform.Fputype.options[this.document.bookingform.Fputype.selectedIndex].value);
					
		// but only if they are returning do we parse the other elements
		if(jsreturntrip == "yes") {
			// This was changed because data kept disappearing - now just calls a copy function ;)
			populateReturn();
		} else {
		// jsreturntrip = no, simple
		// which means we don't worry about an entire section
		// yippee!
		}

	} else {
		// We don't have something to load, because it isn't there!
		
	}

}

function saveData() {
	// Saves data
	var now = new Date();
	fixDate(now);
	// Give us a year or so, that'll do
	now.setTime(now.getTime() + 8 * 24 * 60 * 60 * 1000);
	//setCookie("trtitle", trtitle.value, now);
/*	setCookie("trfname", this.document.bookingform.trfname.value, now);
	setCookie("trsname", this.document.bookingform.trsname.value, now);
	setCookie("trphone", this.document.bookingform.trphone.value, now);
	setCookie("tremail", this.document.bookingform.tremail.value, now);*/
	setCookie("returntrip", this.document.bookingform.returntrip.value, now);
//	alert(this.document.bookingform.returntrip.value);
//	alert(getCookie("returntrip"));
	// Next Break point - Pick up
	// They have a one way fare, no matter what
	// So lets go and save that first.     
	setCookie("traveldate", this.document.bookingform.traveldate.options[this.document.bookingform.traveldate.selectedIndex].value, now);
//	setCookie("Fputype", this.document.bookingform.Fputype.options[this.document.bookingform.Fputype.selectedIndex].value, now);
	setCookie("Fputype", this.document.bookingform.Fputype.selectedIndex, now);
//	alert(this.document.bookingform.Fputype.value);
	switch(this.document.bookingform.Fputype.options[this.document.bookingform.Fputype.selectedIndex].value) {
		case "airport": setCookie("Fairport", this.document.bookingform.Fairport.selectedIndex, now);
						setCookie("Fterminal", this.document.bookingform.Fterminal.selectedIndex, now);
						setCookie("Ftraveltime", this.document.bookingform.Ftraveltime.selectedIndex, now);
						setCookie("Fflightnum", this.document.bookingform.Fflightnum.value, now);
						break;
		case "resort":	setCookie("Fbuildingpu", this.document.bookingform.Fbuildingpu.selectedIndex, now);
						setCookie("Faltphone", this.document.bookingform.Faltphone.value, now);
						setCookie("Froomnum", this.document.bookingform.Froomnum.value, now);
						break;
		case "address":	setCookie("Fstreetnum", this.document.bookingform.Fstreetnum.value, now);
						setCookie("Fstreetname", this.document.bookingform.Fstreetname.value, now);
						setCookie("Fsuburb", this.document.bookingform.Fsuburb.value, now);
						setCookie("Fpostcode", this.document.bookingform.Fpostcode.value, now);
						setCookie("Fstreettype", this.document.bookingform.Fstreettype.selectedIndex, now);
						break;
	}

	// Next break point - Drop off
	setCookie("Fdotype", this.document.bookingform.Fdotype.selectedIndex, now); //options[this.document.bookingform.Fdotype.selectedIndex].value
	switch(this.document.bookingform.Fdotype.options[this.document.bookingform.Fdotype.selectedIndex].value) {
		case "airport": setCookie("Fdoairport", this.document.bookingform.Fdoairport.selectedIndex, now);
						setCookie("Fdoterminal", this.document.bookingform.Fdoterminal.selectedIndex, now);
						setCookie("Fdotraveltime", this.document.bookingform.Fdotraveltime.selectedIndex, now);
						setCookie("Fdoflightnum", this.document.bookingform.Fdoflightnum.value, now);
						break;
		case "resort":	setCookie("Fdobuildingpu", this.document.bookingform.Fdobuildingpu.selectedIndex, now);
						setCookie("Fdoaltphone", this.document.bookingform.Fdoaltphone.value, now);
						setCookie("Fdoroomnum", this.document.bookingform.Fdoroomnum.value, now);
						break;
		case "address":	setCookie("Fdostreetnum", this.document.bookingform.Fdostreetnum.value, now);
						setCookie("Fdostreetname", this.document.bookingform.Fdostreetname.value, now);
						setCookie("Fdosuburb", this.document.bookingform.Fdosuburb.value, now);
						setCookie("Fdopostcode", this.document.bookingform.Fdopostcode.value, now);
						setCookie("Fdostreettype", this.document.bookingform.Fdostreettype.selectedIndex, now);
						break;
	}

	// Last but not least, store the passengers
	setCookie("Fdopaxadults", this.document.bookingform.Fdopaxadults.value, now);
	setCookie("Fdopaxchildren", this.document.bookingform.Fdopaxchildren.value, now);
	setCookie("Fdopaxconcession", this.document.bookingform.Fdopaxconcession.value, now);
	setCookie("Fdopaxinfants", this.document.bookingform.Fdopaxinfants.value, now);

	// but only if they are returning, do we parse the other elements
	setCookie("returndate", this.document.bookingform.returndate.options[this.document.bookingform.returndate.selectedIndex].value, now);
	if (this.document.bookingform.returntrip.value == "yes") {
/*		//setCookie("returndate", this.document.bookingform.returndate.value, now); // DEVILS FOOD
		setCookie("Rputype", this.document.bookingform.Rputype.options[this.document.bookingform.Rputype.selectedIndex].value, now);
		switch(this.document.bookingform.Rputype.options[this.document.bookingform.Rputype.selectedIndex].value) {
			case "airport": setCookie("Rairport", this.document.bookingform.Rairport.value, now);
							setCookie("Rterminal", this.document.bookingform.Rterminal.value, now);
							setCookie("Rtraveltime", this.document.bookingform.Rtraveltime.value, now);
							setCookie("Rflightnum", this.document.bookingform.Rflightnum.value, now);
							break;
			case "resort":	setCookie("Rbuildingpu", this.document.bookingform.Rbuildingpu.value, now);
							setCookie("Raltphone", this.document.bookingform.Raltphone.value, now);
							setCookie("Rroomnum", this.document.bookingform.Rroomnum.value, now);
							break;
			case "address":	setCookie("Rstreetnum", this.document.bookingform.Fstreetnum.value, now);
							setCookie("Rstreetname", this.document.bookingform.Fstreetname.value, now);
							setCookie("Rsuburb", this.document.bookingform.Fsuburb.value, now);
							setCookie("Rpostcode", this.document.bookingform.Fpostcode.value, now);
							break;
		}

		// Next break point - Drop off
		setCookie("Fdotype", this.document.bookingform.Fdotype.options[this.document.bookingform.Fdotype.selectedIndex].value, now);
		switch(this.document.bookingform.Fputype.options[this.document.bookingform.Fputype.selectedIndex].value) {
			case "airport": setCookie("Rdoairport", this.document.bookingform.Rdoairport.value, now);
							setCookie("Rdoterminal", this.document.bookingform.Rdoterminal.value, now);
							setCookie("Rdotraveltime", this.document.bookingform.Rdotraveltime.value, now);
							setCookie("Rdoflightnum", this.document.bookingform.Rdoflightnum.value, now);
							break;
			case "resort":	setCookie("Rdobuilding", this.document.bookingform.Rdobuilding.value, now);
							setCookie("Rdoaltphone", this.document.bookingform.Rdoaltphone.value, now);
							setCookie("Rdoroomnum", this.document.bookingform.Rdoroomnum.value, now);
							break;
			case "address":	setCookie("Rdostreetnum", this.document.bookingform.Rdostreetnum.value, now);
							setCookie("Rdostreetname", this.document.bookingform.Rdostreetname.value, now);
							setCookie("Rdosuburb", this.document.bookingform.Rdosuburb.value, now);
							setCookie("Rdopostcode", this.document.bookingform.Rdopostcode.value, now);
							break;
		}

		// Last but not least, store the passengers
		setCookie("Rdopaxadults", this.document.bookingform.Rdopaxadults.value, now);
		setCookie("Rdopaxchildren", this.document.bookingform.Rdopaxchildren.value, now);
		setCookie("Rdopaxconcession", this.document.bookingform.Rdopaxconcession.value, now);
		setCookie("Rdopaxinfants", this.document.bookingform.Rdopaxinfants.value, now);
	} else {
		// jsreturntrip = no, so we don't worry about it!		*/
	}
//	alert(getCookie("returntrip"));

}