/*
	ALL the site's javascript is here, probably ought to separate it and stuff but it's simpler
	this way.  Well it's simpler at the moment, the future on the other hand...
*/

$(document).ready(function(){
	// Check to see if any page javascript needs things loading.
	if (typeof(loadLocal) == 'function') loadLocal();
});

// destination dropdown handling stuff
var dest_dropdown_closer = false;
var dest_dropdown_locked = false;
$(document).ready(function() {
	if($("#destinations_link").length) {
		// make the destination dropdown stuff work
		$("#destinations_link,#destinations_dropdown").mouseenter(dest_dropdown_open);
		$("#destinations_link,#destinations_dropdown").mouseleave(dest_dropdown_close);
	}
	
	// if they're typing into the search box then don't close it on them
	$("#destinations_dropdown_search").focus(function() { dest_dropdown_locked = true; });
	$("#destinations_dropdown_search").blur(function() { dest_dropdown_locked = false; });
});

// opens the destinations dropdown menu
function dest_dropdown_open() {
	if(!dest_dropdown_locked) {
		// get the position and size of the destination link
		offset = $("#destinations_link").offset();
		height = $("#destinations_link").outerHeight();
		newtop = (parseInt(offset.top) + parseInt(height)) - 1;
		newleft = offset.left;
		
		// shift the dropdown content into place
		$("#destinations_dropdown").css("top", newtop);
		$("#destinations_dropdown").css("left", newleft);
		
		clearTimeout(dest_dropdown_closer);
		$("#destinations_link").addClass("hover");
		$("#destinations_dropdown").show();
	}
}

// closes it, or will shortly
function dest_dropdown_close() {
	if(!dest_dropdown_locked) {
		dest_dropdown_closer = setTimeout(function() {
			$("#destinations_dropdown").hide();
			$("#destinations_link").removeClass("hover");
			$("#debug").append("<br/>Closed!");
		}, 500);
	}
}

// validates the destination dropdown destination search box
function toolbar_search_validate() {
	f = document.toolbar_search_form;
	
	if((f.search.value == "")  || (f.search.value == "Enter destination here") || (f.search.value == "No match found!")) {
		return false;
	}
	else {
		return true;
	}
}

// a really awful function for sending them to the destination page when they select something
function hijack_search(f, u) {
	d = f[f.selectedIndex].value;
	if(d != "") {
		document.location = u + d + "-hotels/";
	}
}

// submits a form X, default of 0
function submit(x) {
	if(isNaN(x)) {
		x = 0;
	}
	document.forms[x].submit();
}

// a basic function to open a basic window with user specified url, width and heights.
function popup(url, width, height) {
	featurestr = "directories=0,height=" + height + ",location=0,menubar=0,resizable=1,scrollbars=1,status=0,toolbar=0,width=" + width;
	open(url, "_blank", featurestr);
}

// pops up a window for selecting a date
function get_date(month, year, form_no, fields) {
	month = month[month.selectedIndex].value;
	year = year[year.selectedIndex].value;
	url = "get_date.php?month=" + month + "&year=" + year + "&form=" + escape(form_no) + "&fields=" + escape(fields);
	popup(url, 250, 200);
}

// this one is a function that makes sure that a field (this) is an integer.
function make_int(f) {
	if(isNaN(f.value) || (f.value == "") || (f.value < 0)) {
		f.value = "0";
	}
	else {
		f.value = Math.round(f.value);
	}
}

// this checks their booking after they fill in a quanitity of people
function hotel_people(f) {
	f = f.form;
	
	// trundle through the fields
	people = sleeps = rooms = false;
	for(i=0;i<f.elements.length;i++) {
		// if they're fields we're interested in
		if(f.elements[i].name == "people[]") { people = f.elements[i]; }
		if(f.elements[i].name == "sleeps[]") { sleeps = f.elements[i]; }
		if(f.elements[i].name == "rooms[]") { rooms = f.elements[i]; }
		
		// once we've got one of each, do maths
		if(people && sleeps && rooms) {
			rooms.value = Math.max(Math.ceil(people.value / sleeps.value), rooms.value);
			people = sleeps = rooms = false;
		}
	}
}

// this checks their booking after they fill in a quanitity of rooms
function hotel_rooms(f) {
	f = f.form;
	
	// trundle through the fields
	people = sleeps = rooms = false;
	for(i=0;i<f.elements.length;i++) {
		// if they're fields we're interested in
		if(f.elements[i].name == "people[]") { people = f.elements[i]; }
		if(f.elements[i].name == "sleeps[]") { sleeps = f.elements[i]; }
		if(f.elements[i].name == "rooms[]") { rooms = f.elements[i]; }
		
		// once we've got one of each, do maths
		if(people && sleeps && rooms) {
			people.value = Math.min((rooms.value * sleeps.value), people.value);
			people = sleeps = rooms = false;
		}
	}
}

// this function looks through to see if they're actually booking anything and if they're not then we bitch about it.
function hotel_validate(f) {
	// trundle through the fields
	people = rooms = false;
	for(i=0;i<f.elements.length;i++) {
		if(f.elements[i].name == "people[]") { people = f.elements[i]; }
		if(f.elements[i].name == "rooms[]") { rooms = f.elements[i]; }
		
		// see if they've booked anything
		if(people && rooms) {
			if(people.value > 0 && rooms.value > 0) {
				f.submit();
				return true;
			}
			else {
				people = rooms = false;
			}
		}
	}
	
	// still here?  bitch about it
	alert("Please make sure you have booked at least 1 room.");
}

// a fairly basic e-mail validating function that isn't as extensive as the old one, but cleaner.
// since it uses regular expressions it's a version 4+ deal only, sorry.
function valid_email(email) {
	// this is a regular expression that verifies the address.
	var email_re = "^[^@]+@[^@]+[\.][^@]+$";
	if((email.value != "") && (!email.value.match(email_re))) {
		alert("Invalid e-mail address!");
		email.focus();
		email.select();
	}
}

