/*
	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...
*/

// 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 function checks to make sure that they've filled in the relevant parts of the form.
function vehicle_book(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(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();
	}
}

// a little function that handles the submitting of the various search forms throughout the site
// (NOTE: this superior and more user-friendly thing has been replaced with the crappy and marketing-centric thing below)
/*function vehicles_search(f) {
	f = document.forms[f];
	
	// if they've selected newzealand then pipe them through to newzealand-motorhomes.com
	if(f.d[f.d.selectedIndex].value == "newzealand") {
		f.action = "http://www.newzealand-motorhomes.com/vehicles.php";
	}
	
	// otherwise as normal
	else {
		f.action = "vehicles.php";
	}
	
	f.submit();
}*/

// reroute them to different places when they select a destination in the dropdown
// Yeah, I know this is the stupidest bit of user-interaction EVER, but I'm Just Following Orders
function hijack_search(f) {
	d = f[f.selectedIndex].value; 
	if(d == "newzealand") {
		document.location = "http://www.newzealand-motorhomes.com";
	}
	else {
		document.location = "http://" + d + ".motorhome-holidays.com";
	}
}