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 = "high";
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) {
// 	alert(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 == 6) nights_fix = 1;
	if (num_nights > 23 && num_nights < 30) nights_fix = 30 - num_nights;

	if (num_nights + nights_fix < 7) {
		var baseprice = 77;
		var extAdult = 17;
		var extChild = 8.5;
		var extJacuzzi = 20;

	}
	else if (num_nights + nights_fix < 30) {
		var baseprice = 62;
		var extAdult = 11;
		var extChild = 5.5;
		var extJacuzzi = 16;
		if (num_nights > 23) nights_fix = 30 - num_nights;
	}
	else {
		var baseprice = 51;
		var extAdult = 9;
		var extChild = 4.5;
		var extJacuzzi = 12;
	}

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

	// 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 (num_rooms > adults + childs) {
        pricespan.innerHTML = "Select total number of people";
        return;
    }

	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 = "Select number of rooms";
		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";

}

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 openAjax () {
	var xmlHttp;
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e) {
		// Internet Explorer
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			try {
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e) {
// 				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}
	return xmlHttp;
}

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


window.onload = loadpage;
document.onmousemove = getMouseXY;

var mousex = 0;
var mousey = 0;

function getMouseXY(e) {

	if (!e) e = window.event; // works on IE, but not NS (we rely on NS passing us the event)

	if (e) {
		if (e.pageX || e.pageY) { // this doesn't work on IE6!! (works on FF,Moz,Opera7)
			mousex = e.pageX;
			mousey = e.pageY;
		}
		else if (e.clientX || e.clientY) { // works on IE6,FF,Moz,Opera7
			mousex = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
			mousey = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
		}
	}
// 	document.getElementById('printdiv').innerHTML = "x:" + mousex +" y:" + mousey;
}


function callback_update(elem) {
	if(elem.value != -1 && elem.value != -2) {
		document.getElementById("callback_country_code").value = "+"+elem.value;
	}
	else {
		document.getElementById("callback_country_code").value = "+";
	}
}

function callback() {
	var country = document.getElementById("callback_country_code").value;
	var number = document.getElementById("callback_number").value;

	document.getElementById("callback_div").innerHTML = "Attempting to call... please wait.";
	
	xmlhttp.open("POST", "callback.php", true);
	xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
	xmlhttp.send("country="+country+"&number="+number);
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4) {
			document.getElementById("callback_div").innerHTML = xmlhttp.responseText;
		}
	}
}

function gettype (mixed_var) {
	// Returns the type of the variable
	//
	// version: 1102.614
	// discuss at: http://phpjs.org/functions/gettype
	// +   original by: Paulo Freitas
	// +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
	// +   improved by: Douglas Crockford (http://javascript.crockford.com)
	// +   input by: KELAN
	// +   improved by: Brett Zamir (http://brett-zamir.me)
	// -    depends on: is_float
	// %        note 1: 1.0 is simplified to 1 before it can be accessed by the function, this makes
	// %        note 1: it different from the PHP implementation. We can't fix this unfortunately.
	// *     example 1: gettype(1);
	// *     returns 1: 'integer'
	// *     example 2: gettype(undefined);
	// *     returns 2: 'undefined'
	// *     example 3: gettype({0: 'Kevin van Zonneveld'});
	// *     returns 3: 'array'
	// *     example 4: gettype('foo');
	// *     returns 4: 'string'
	// *     example 5: gettype({0: function () {return false;}});
	// *     returns 5: 'array'
	var s = typeof mixed_var,
	name;
	var getFuncName = function (fn) {
		var name = (/\W*function\s+([\w\$]+)\s*\(/).exec(fn);
		if (!name) {
			return '(Anonymous)';
		}
		return name[1];
	};
	if (s === 'object') {
		if (mixed_var !== null) { // From: http://javascript.crockford.com/remedial.html
            if (typeof mixed_var.length === 'number' && !(mixed_var.propertyIsEnumerable('length')) && typeof mixed_var.splice === 'function') {
					s = 'array';
				} else if (mixed_var.constructor && getFuncName(mixed_var.constructor)) {
					name = getFuncName(mixed_var.constructor);
					if (name === 'Date') {
						s = 'date'; // not in PHP
					} else if (name === 'RegExp') {
						s = 'regexp'; // not in PHP
					} else if (name === 'PHPJS_Resource') { // Check against our own resource constructor
                    s = 'resource';
				}
		}
} else {
	s = 'null';
}
} else if (s === 'number') {
	s = this.is_float(mixed_var) ? 'double' : 'integer';
	}
	return s;
	}


function check_availability() {
	if (document.getElementById('first_in_day').value == 0) return false;
	if (document.getElementById('first_in_month').value == 0) return false;
	if (document.getElementById('first_in_year').value == 0) return false;
	if (document.getElementById('first_out_day').value == 0) return false;
	if (document.getElementById('first_out_month').value == 0) return false;
	if (document.getElementById('first_out_year').value == 0) return false;

	if (Number(document.getElementById('num_rooms').value) != document.getElementById('num_rooms').value) return false;
	if (Number(document.getElementById('num_rooms').value) < 1) return false;

	document.getElementById('availability').innerHTML = '<div class="checking_availability"><img src="loading.gif" style="vertical-align: middle;" alt="checking availability" /> Checking availability</div>';
	var day = Number(document.getElementById('first_in_day').value);
	var month = Number(document.getElementById('first_in_month').value)-1;
	var year = Number(document.getElementById('first_in_year').value);
	var in_date = Math.round(new Date(year,month,day) / 1000);

	day = Number(document.getElementById('first_out_day').value);
	month = Number(document.getElementById('first_out_month').value)-1;
	year = Number(document.getElementById('first_out_year').value);
	var out_date = Math.round(new Date(year,month,day) / 1000);

	num_rooms = Number(document.getElementById('num_rooms').value);

	post_string = String("in_date="+in_date+"&out_date="+out_date+"&num_rooms="+num_rooms);
	setTimeout ( "do_ajax_availability(\""+post_string+"\")", 1 ); // Have to fork this to another function because of stupid IE... yet again.
	
}
function do_ajax_availability(post_string) {
	xmlhttp.open("POST", "check_availability.php", false);
	xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
	xmlhttp.send(post_string)
	if (xmlhttp.readyState==4) {
		document.getElementById('availability').innerHTML = xmlhttp.responseText;
	}
}

// Submits the check in and check out dates to the secure reservation form from the contact form
function submit_to_res_form() {
	first_in_day = (document.getElementById('first_in_day')) ? document.getElementById('first_in_day').value : false;
	first_in_month = (document.getElementById('first_in_month')) ? document.getElementById('first_in_month').value : false;
	first_in_year = (document.getElementById('first_in_year')) ? document.getElementById('first_in_year').value : false;
	first_out_day = (document.getElementById('first_out_day')) ? document.getElementById('first_out_day').value : false;
	first_out_month = (document.getElementById('first_out_month')) ? document.getElementById('first_out_month').value : false;
	first_out_year = (document.getElementById('first_out_year')) ? document.getElementById('first_out_year').value : false;

	var string = "<div><input type='hidden' name='first_in_day' value='"+first_in_day+"' />";
	string += "<input type='hidden' name='first_in_month' value='"+first_in_month+"' />";
	string += "<input type='hidden' name='first_in_year' value='"+first_in_year+"' />";
	string += "<input type='hidden' name='first_out_day' value='"+first_out_day+"' />";
	string += "<input type='hidden' name='first_out_month' value='"+first_out_month+"' />";
	string += "<input type='hidden' name='first_out_year' value='"+first_out_year+"' />";
	string += "</div></form>";
	
	var form = document.getElementById("submit_to_res_form");
	form.innerHTML = string;
	form.submit();
// 	document.forms["submit_to_res_form"].submit();
}

function close_promo() {
	elem = document.getElementById("tripadvisor_promo");
	if (elem) {
		elem.style.display = "none";
		elem.innerHTML = "";
	}
}
