function fill(fill) {
	document.getElementById("text").value += fill;
}

var xmlhttp
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
  try {
  xmlhttp=new ActiveXObject("Msxml2.XMLHTTP")
 } catch (e) {
  try {
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
  } catch (E) {
   xmlhttp=false
  }
 }
@else
 xmlhttp=false
@end @*/
if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
 try {
  xmlhttp = new XMLHttpRequest();
 } catch (e) {
  xmlhttp=false
 }
}

var cur_rate = "low";
function car_rental(season) {
	// If clicking on the current rate die
	if (season == cur_rate) return 0;

	// AJAX stuff
	var getURL = "car_db.php?ajax=1&rate="+season;
	xmlhttp.open("GET", getURL, true);
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4) {
			document.getElementById("cardiv").innerHTML = xmlhttp.responseText

			// Swap the arrow image
			if (season == "high") {
				document.getElementById("car_rate_high_img").style.display = "inline";
				document.getElementById("car_rate_low_img").style.display = "none";
			}
			else {
				document.getElementById("car_rate_high_img").style.display = "none";
				document.getElementById("car_rate_low_img").style.display = "inline";
			}

			// Swap colors
			var objclass1 = document.getElementById("car_rate_low_text").className;
			var objclass2 = document.getElementById("car_rate_high_text").className;

			document.getElementById("car_rate_low_text").className = objclass2;
			document.getElementById("car_rate_high_text").className = objclass1;

			// Changes the current rate
			cur_rate = season;
		}
	}
	xmlhttp.send(null)
}


// Changes type of car in drop down list when clicking on the link in the car list
function changecar(car, trans) {
	var transDiv = document.getElementById("transmission_id");
	transDiv.innerHTML = "<dl style=\"margin-left:100px;\"><dt>Transmission</dt><dd><select name=\"transmission\" id=\"transSelect\"></select></dd></dl>";
	transSelect = document.getElementById("transSelect");

	if (trans == 'm' || trans == 'ma') transSelect.options[transSelect.length] = new Option('Manual', 'm');
	if (trans == 'a' || trans == 'ma') transSelect.options[transSelect.length] = new Option('Automatic', 'a');

	if (car != null) {
		var select = document.getElementById("car_select");
		select.value = car;
	}
}


function showCalendarExtended(number,what,other,lang) {

	if (document.getElementById(number + "_" + what + "_" + "month").selectedIndex != 0) {
		dateString = "&month=" + document.getElementById(number + "_" + what + "_" + "month").selectedIndex;
		if (document.getElementById(number + "_" + what + "_" + "year").selectedIndex != 0) {
			dateString += "&year=" + document.getElementById(number + "_" + what + "_" + "year").value;
		}
		if (document.getElementById(number + "_" + what + "_" + "day").selectedIndex != 0) {
			dateString += "&day=" + document.getElementById(number + "_" + what + "_" + "day").value;
		}
	}
	else {
		dateString = "";
	}
	if (document.getElementById(number + "_" + other + "_" + "day").selectedIndex != 0) {
		dateString += "&oday=" + document.getElementById(number + "_" + other + "_" + "day").value;
	}
	if (document.getElementById(number + "_" + other + "_" + "month").selectedIndex != 0) {
		dateString += "&omonth=" + document.getElementById(number + "_" + other + "_" + "month").value;
	}
	if (document.getElementById(number + "_" + other + "_" + "year").selectedIndex != 0) {
		dateString += "&oyear=" + document.getElementById(number + "_" + other + "_" + "year").value;
	}


	document.getElementById("calendar").className="calendar";

	document.getElementById("calendar").innerHTML="<iframe src='calendar.php?which=" + number + "_" + what + "_" + dateString+"&lang="+lang+"' frameborder='0' height='200' width='200'><\/iframe>";

	var posx = GetBodyOffset(number + "_" + what + "_" + "day", "x");
	var posy = GetBodyOffset(number + "_" + what + "_" + "day", "y");

	document.getElementById("calendar").style.top = posy + 23 + "px";
	document.getElementById("calendar").style.left = posx + "px";
}

function GetBodyOffset(el_name, XY) {
	var x;
	var y;
	x = 0;
	y = 0;

	var elem = document.getElementById(el_name);
	do
	{
		x += elem.offsetLeft;
		y += elem.offsetTop;
		if (elem.tagName == "BODY" || elem.tagName == "HTML")
			break;
		elem = elem.offsetParent;
	} while  (1 > 0);

	if (XY == "x") return x;
	if (XY == "y") return y;
}

function showSimple() {
	document.getElementById("tours_left").innerHTML = document.getElementById("tours_left_simple").innerHTML;
	document.getElementById("tours_right").innerHTML = document.getElementById("tours_right_simple").innerHTML;
}

function showAdv() {
	document.getElementById("tours_left").innerHTML = document.getElementById("tours_left_adv").innerHTML;
	document.getElementById("tours_right").innerHTML = document.getElementById("tours_right_adv").innerHTML;
}




function externalLinks() {
 if (!document.getElementsByTagName) return;
 var anchors = document.getElementsByTagName("a");
 for (var i=0; i<anchors.length; i++) {
   var anchor = anchors[i];
   if (anchor.getAttribute("href") &&
       anchor.getAttribute("rel") == "newwindow")
     anchor.target = "_blank";
 }
}


function hideprice() {
	if (!document.getElementById("price")) return;
	var pricerow = document.getElementById("price");
// 	alert(pricerow); // alerts "[object]"
// 	alert(pricerow.innerHTML); // alerts the current innerHTML correctly
	var replaceText = "Nightly rate (plus tax): <span id=\"pricespan\">choose dates</span>";
	pricerow.innerHTML = replaceText;

	document.getElementById("jacuzziprice").innerHTML = "(Select your dates first)";
	updateprice();
}

function updateprice() {
	if (!document.getElementById("resform")) return 0;
	var pricespan = document.getElementById("pricespan");

	// Get number of days from first stay
	var first_in_day = document.getElementById("resform").first_in_day;
	var first_in_month = document.getElementById("resform").first_in_month;
	var first_in_year = document.getElementById("resform").first_in_year;
	var first_out_day = document.getElementById("resform").first_out_day;
	var first_out_month = document.getElementById("resform").first_out_month;
	var first_out_year = document.getElementById("resform").first_out_year;

	var emptystring = "Nightly rate (plus tax): <span id=\"pricespan\">choose dates</span>";

	if (first_in_day.value == "") { document.getElementById("price").innerHTML = emptystring; return; }
	if (first_in_month.value == "") { document.getElementById("price").innerHTML = emptystring; return; }
	if (first_in_year.value == "") { document.getElementById("price").innerHTML = emptystring; return; }
	if (first_out_day.value == "") { document.getElementById("price").innerHTML = emptystring; return; }
	if (first_out_month.value == "") { document.getElementById("price").innerHTML = emptystring; return; }
	if (first_out_year.value == "") { document.getElementById("price").innerHTML = emptystring; return; }

	var first_in = datetounixtime(first_in_day.value, first_in_month.value, first_in_year.value);
	var first_out = datetounixtime(first_out_day.value, first_out_month.value, first_out_year.value);

	if (first_out <= first_in) {
		pricespan.innerHTML = "First check in must be before your first check out";
		return;
	}

	var num_nights = (first_out - first_in);


	// Get number of days from second stay
	var doSecond = 1;
	var second_in_day = document.getElementById("resform").second_in_day;
	var second_in_month = document.getElementById("resform").second_in_month;
	var second_in_year = document.getElementById("resform").second_in_year;
	var second_out_day = document.getElementById("resform").second_out_day;
	var second_out_month = document.getElementById("resform").second_out_month;
	var second_out_year = document.getElementById("resform").second_out_year;

	if (second_in_day.value == "") doSecond = 0;
	if (second_in_month.value == "") doSecond = 0;
	if (second_in_year.value == "") doSecond = 0;
	if (second_out_day.value == "") doSecond = 0;
	if (second_out_month.value == "") doSecond = 0;
	if (second_out_year.value == "") doSecond = 0;

	if (doSecond == 1) {
		var second_in = datetounixtime(second_in_day.value, second_in_month.value, second_in_year.value);
		var second_out = datetounixtime(second_out_day.value, second_out_month.value, second_out_year.value);

		if (second_out <= second_in) {
			pricespan.innerHTML = "Second check in must be before your second check out";
			return;
		}

		num_nights += (second_out - second_in);
	}

	// calculate price

	// Jacuzzi or not
// 	for (var i=0; i < document.getElementById("resform").jacuzzi.length; i++) {
// 		if (document.getElementById("resform").jacuzzi[i].checked) {
// 			var jacuzzi = document.getElementById("resform").jacuzzi[i].value;
// 		}
// 	}
	var jacuzzi = Number(document.getElementById("resform").num_jacuzzi.value);

	if (jacuzzi <= 0) jacuzzi = 0;


	var nights_fix = 0; // Adds additional nights so that the rate doesn't go down with each additional night
						// when switching e.g. from daily to weekly rates
	if (num_nights == 5 && jacuzzi > 0) nights_fix = 2;
	if (num_nights == 6) nights_fix = 1;
	if (num_nights > 23 && num_nights < 30) nights_fix = 30 - num_nights;

	if (num_nights + nights_fix < 7) {
		var baseprice = 65;
		var extAdult = 14;
		var extChild = 7;
		var extJacuzzi = 20;

	}
	else if (num_nights + nights_fix < 30) {
		var baseprice = 49;
		var extAdult = 9;
		var extChild = 4.5;
		var extJacuzzi = 10;
		if (num_nights > 23) nights_fix = 30 - num_nights;
	}
	else {
		var baseprice = 39;
		var extAdult = 7;
		var extChild = 3.5;
		var extJacuzzi = 7;
	}

	// number of paying people
	var adults = document.getElementById("resform").num_adults.value;
	var childs = document.getElementById("resform").num_children.value;

	// total number of rooms
	var num_rooms = Number(document.getElementById("resform").num_non_smoking.value) + jacuzzi;

	if (isNaN(num_rooms) || isNaN(adults) || isNaN(childs)) {
		alert("Please verify number of rooms and people and make sure you haven't included any non-numeric characters. You may only use numbers.");
	}
	if (isNaN(num_rooms) || num_rooms == 0) {
		pricespan.innerHTML = "No rooms selected";
		return;
	}

// 	if (isNaN(num_rooms)) alert("roooms error");
// 	if (isNaN(childs)) alert("childs error");
// 	if (isNaN(adults)) alert("adults error");
// 	var mathss = num_rooms + adults + childs;
// 	alert(num_rooms + adults + childs);
// 	alert(mathss);

	// calculate prices
	var price_per_day = (baseprice*num_rooms + ((adults - num_rooms)*extAdult) + childs*extChild);
	var total_price = price_per_day * (num_nights + nights_fix);

	price_per_day = Math.round((total_price / num_nights)*100)/100; // get the actual price per day adjusted for the real number of nights

	pricespan.innerHTML = "$" + price_per_day;
//	if (num_nights > 1) pricespan.innerHTML += " per night";
	if (jacuzzi > 0) {
		pricespan.innerHTML += " + $" + extJacuzzi
		if (jacuzzi > 1) pricespan.innerHTML += " each";
		pricespan.innerHTML += " for";
		if (jacuzzi > 1) pricespan.innerHTML += " "+jacuzzi;
		pricespan.innerHTML += " Jacuzzi";
		if (jacuzzi > 1) pricespan.innerHTML += " rooms";
		pricespan.innerHTML += " = $" + Math.round((price_per_day+jacuzzi*extJacuzzi)*100)/100;
	}
	if (num_rooms > 1) pricespan.innerHTML += " total (" + num_rooms + " rooms)";
//	if (num_nights > 1) pricespan.innerHTML += "<br />$" + total_price + " total for " + num_nights + " nights";

	// update Jacuzzi price
	document.getElementById("jacuzziprice").innerHTML = "($"+extJacuzzi+" extra per night)";
}

function checkForMore(element) {
	if (element.value != '-1') return;

	var focusTo = element.name;

	var adults = document.getElementById("resform").num_adults.value;
	var childs = document.getElementById("resform").num_children.value;
	var youngchilds = document.getElementById("resform").num_young_children.value;
	var num_rooms = Number(document.getElementById("resform").num_non_smoking.value);

	// The value for "more" is -1, so this should match "0" or "more"
	if (adults < 1) adults = '';
	if (childs < 1) childs = '';
	if (youngchilds < 1) youngchilds = '';
	if (num_rooms < 1) num_rooms = '';
	
	document.getElementById("num_rooms_td").innerHTML = '<input name="num_non_smoking" value="'+num_rooms+'" maxlength="3" size="3" onchange="updateprice();" />';
	document.getElementById("num_adults_td").innerHTML = '<input name="num_adults" value="'+adults+'" maxlength="3" size="3" onchange="updateprice();" />';
	document.getElementById("num_children_td").innerHTML = '<input name="num_children" value="'+childs+'" maxlength="3" size="3" onchange="updateprice();" />';
	document.getElementById("num_young_children_td").innerHTML = '<input name="num_young_children" value="'+youngchilds+'" maxlength="3" size="3" onchange="updateprice();" />';

	if (focusTo == "num_rooms") document.getElementById("resform").num_non_smoking.focus();
	if (focusTo == "num_children") document.getElementById("resform").num_children.focus();
	if (focusTo == "num_young_children") document.getElementById("resform").num_young_children.focus();
	if (focusTo == "num_adults") document.getElementById("resform").num_adults.focus();
}

function datetounixtime(day, month, year) {
		var humDate = new Date(Date.UTC(year, (month)-1, day, 0,0,0));
		return (humDate.getTime()/(1000.0*60*60*24));
}

function loadpage() {
	// Functions to load with the page
	hideprice();
	externalLinks();
}


window.onload = loadpage;
