/*
	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();
});

// 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=" + 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
	for(i=6;i<=f.length;i+=4) {
		// pick out relevant fields
		people = f.elements[i - 3];
		sleeps = f.elements[i - 2];
		rooms = f.elements[i - 1];
		
		// make the rooms value good
		rooms.value = Math.max(Math.ceil(people.value / sleeps.value), rooms.value);
	}
}

// this checks their booking after they fill in a quanitity of rooms
function hotel_rooms(f) {
	f = f.form;
	
	// trundle through the fields
	for(i=6;i<=f.length;i+=4) {
		// pick out relevant fields
		people = f.elements[i - 3];
		sleeps = f.elements[i - 2];
		rooms = f.elements[i - 1];
		
		// make the people value good
		people.value = Math.min((rooms.value * sleeps.value), people.value);
	}
}

// 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
	for(i=6;i<=f.length;i+=4) {
		// pick out relevant fields
		people = f.elements[i - 3];
		rooms = f.elements[i - 1];
		
		// see if they've booked anything
		if(people.value > 0 || rooms.value > 0) {
			f.submit();
			return true;
		}
	}
	
	// still here?  bitch about it
	alert("Please make sure you have booked at least 1 room.");
}

// this function checks to make sure that they've filled in the relevant parts of the form.
function checkout_validate(f) {
	f = f.form;
	errorstr = "";
	
	if(f.surname.value == "") {
		errorstr += "\tSurname\n";
	}
	if(f.email.value == "") {
		errorstr += "\tEmail\n";
	}
	if(f.email2.value == "") {
		errorstr += "\tConfirm E-Mail\n";
	}
	if(f.email.value != f.email2.value) {
		errorstr += "\tE-mail addresses do not match.\n";
	}
	
	if(f.country[f.country.selectedIndex].value == "") {
		errorstr += "\tCountry\n";
	}
	
	if(f.heard_about[f.heard_about.selectedIndex].value == "") {
		errorstr += "\tHow Did You Hear About Us\n";
	}
	
	if(errorstr == "") {
 		if((f.card_number.value == "") || (f.card_expires.value == "")) {
	 		if(window.confirm("Credit card details incomplete:\nYou may send this form without credit card details\nHowever Credit Card details are required to make a confirmed booking.\nClick OK to continue or cancel to complete your details.")) {
	 			f.submit();
	 		}
			else {
	 			f.card_number.focus();
	 		}
		} else {
			f.submit();
		}
	}
	
	if(errorstr) {
		errorstr = "Please fill in the following to allow us to process your request\n\n" + errorstr;
		alert(errorstr);
	}
}

// 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();
	}
}
