
function show_gen_enq() {
	var element = $('#ge_form_small');
	element.animate({height:"770px"},800);
}

function show_small_enq() {
	var element = $('#ge_form_small');
	if (element[0].style.height == '810px') {
		element.animate({height:"1px"},800);
	}
	else {
		element.animate({height:"810px"},800);
	}
}

function form_validate_listing(){
	var invalid_fields = false;
	var required_fields = ["ecf_daytimeph","ecf_postcode","ecf_email","ecf_newsletter","ecf_newsletter","ecf_contactmethod","ecf_departdate","ecf_returndate","ecf_nooftravellers"];
	
	//- name validation (3 fields required)
	var required_name_fields = ["ecf_title","ecf_firstname","ecf_lastname"];
	var message = "all fields required";
	var name_error = false;

	//- title required	
	if (document.email_listing_frm.ecf_title.value == "") {
		name_error = true;
	}
	
	//- first name required
	if (document.email_listing_frm.ecf_firstname.value == "" || document.email_listing_frm.ecf_firstname.value == "First name") {
		name_error = true;
	}

	//- last name required
	if (document.email_listing_frm.ecf_lastname.value == "" || document.email_listing_frm.ecf_lastname.value == "Last name") {
		name_error = true;
	}
	
	if (name_error) {
		$("#name_error").html(message);
		invalid_fields = true;
	}
	else {
		$("#name_error").html("");
	}

	//- daytime phone required	
	if (document.email_listing_frm.ecf_daytimeph.value == "") {
		invalid_fields = true;
		$("#phone_error").html("phone number required");
	}
	else {
		$("#phone_error").html("");
	}
	
	//- postcode required
	if (document.email_listing_frm.ecf_postcode.value == "") {
		invalid_fields = true;
		$("#postcode_error").html("postcode requried");
	}
	else if (!isNaN(document.email_listing_frm.ecf_postcode.value) && (document.email_listing_frm.ecf_postcode.value.length == 4)) {
		$("#postcode_error").html("");
	}
	else {
		invalid_fields = true;
		$("#postcode_error").html("invalid postcode");
	}
	
	//- email validation (required field and format check)
	var email_val = document.email_listing_frm.ecf_email.value;
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email_val) && email_val != "") {
		$("#email_error").html("");
	}
	else {
		$("#email_error").html("invalid email address");
		invalid_fields = true;
	}
	
	//- departure date (required field)
	var depart_date = document.email_listing_frm.ecf_departdate.value;
	if(/^\d{1,2}\/\d{1,2}\/\d{4}$/.test(depart_date)) {
		$("#depart_error").html("");
	}
	else if ((depart_date == "dd/mm/yyyy") || (depart_date == "")) {
		$("#depart_error").html("departure date required");
		invalid_fields = true;
	}
	else {
		$("#depart_error").html("invalid departure date");
		invalid_fields = true;
	}

	//- return date (required field)
	var return_date = document.email_listing_frm.ecf_returndate.value;
	if(/^\d{1,2}\/\d{1,2}\/\d{4}$/.test(return_date)) {
		$("#return_error").html("");
		// make sure depart date isn't past return date
		if(/^\d{1,2}\/\d{1,2}\/\d{4}$/.test(depart_date)) {
			var split_depart_date =  depart_date.split("/");
			var split_return_date =  return_date.split("/");
			var depart_date_val = new Date();
			var return_date_val = new Date();
			depart_date_val.setFullYear(split_depart_date[2],split_depart_date[1],split_depart_date[0]);
			return_date_val.setFullYear(split_return_date[2],split_return_date[1],split_return_date[0]);
			if(depart_date_val > return_date_val){
				$("#return_error").html("Return date must be after departure date");
				invalid_fields = true;
			}
			else if(split_depart_date[0]==0||split_depart_date[1]==0||split_depart_date[2]==0){
				$("#depart_error").html("invalid depart date");
				invalid_fields = true;
			}
			else if(split_return_date[2]==0||split_return_date[1]==0||split_return_date[0]==0){
				$("#return_error").html("invalid return date");
				invalid_fields = true;
			}
			else{
				$("#return_error").html("");
				$("#depart_error").html("");
			}
		}
	}
	else if ((return_date == "dd/mm/yyyy") || (return_date == "")) {
		$("#return_error").html("return date required");
		invalid_fields = true;
	}
	else {
		$("#return_error").html("invalid return date");
		invalid_fields = true;
	}

	//- preferred contact method (required field)	
	if ((document.email_listing_frm.ecf_contactmethod[0].checked == false) && (document.email_listing_frm.ecf_contactmethod[1].checked == false)) {
		$("#preferred_error").html("please select an option");
		invalid_fields = true;
	}
	else {
		$("#preferred_error").html("");
	}
	
	//- email newsletter (required field)	
	if ((document.email_listing_frm.ecf_newsletter[0].checked == false) && (document.email_listing_frm.ecf_newsletter[1].checked == false)) {
		$("#newsletter_error").html("please select an option");
	}
	else {
		$("#newsletter_error").html("");
	}
	
	//- number of travellers (required field)
	if (document.email_listing_frm.ecf_nooftravellers.value == "") {
		invalid_fields = true;
		$("#travellers_error").html("no. of travellers required");
	}
	else {
		$("#travellers_error").html("");
	}

	if (invalid_fields) {
		alert('please correct any errors and resubmit');
		return false;
	}
	else {
		return true;
	}
}

function form_validate(){
	var invalid_fields = false;
	
	//- name validation (3 fields required)
	var message = "all fields req'd";
	var name_error = false;

	//- title required	
	if (document.general_enq.ecf_title.value == "") { 
		name_error = true;
	}
	
	//- first name required
	if (document.general_enq.ecf_firstname.value == "" || document.general_enq.ecf_firstname.value == "First name") {
		name_error = true;
	}

	//- last name required
	if (document.general_enq.ecf_lastname.value == "" || document.general_enq.ecf_lastname.value == "Last name") {
		name_error = true;
	}
	
	if (name_error) {
		$("#name_error").html(message);
		invalid_fields = true;
	}
	else {
		$("#name_error").html("");
	}

	//- daytime phone validation (required field)
	if (document.general_enq.ecf_daytimeph.value  == "") {
		$("#phone_error").html("invalid phone");
		invalid_fields = true;
	}
	else {
		$("#phone_error").html("");
	}
	
	//- email validation (required field and format check)
	var email_val = document.general_enq.ecf_email.value;
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email_val) && email_val != "") {
		$("#email_error").html("");
	}
	else {
		$("#email_error").html("invalid email address");
		invalid_fields = true;
	}
	
	//- postcode validation (required field and length check)
	var postcode_val = document.general_enq.ecf_postcode.value;
	if (!isNaN(postcode_val) && (postcode_val.length == 4)) {
		$("#postcode_err").html("");
	}
	else {
		$("#postcode_err").html("invalid postcode");
		invalid_fields = true;
	}

	//- departure date validation (not required field but need to stop non-existent dates)
	var depart_day = document.general_enq.depart_day.value;
	var depart_monthyear = document.general_enq.depart_monthyear.value;
	
	//- empty depart date is ok
	if (depart_day == "" && depart_monthyear == "") {
		$("#depart_error").html("");
		var valid_depart_date = true;
		var empty_depart_date = true;
	}

	//- validate depart date if both parts are present
	else if ((date_validation(depart_day, depart_monthyear)) == "invalid") {
		$("#depart_error").html("invalid date");
		$("#return_error").html("");
		invalid_fields = true;
		var valid_depart_date = false; //- runs / disables the return date validation
	}
	else {
		$("#depart_error").html("");
		var valid_depart_date = true;
	}
	
	//- return date validation (not required field but need to stop non-existent dates)
	if (valid_depart_date) {
		var return_day = document.general_enq.return_day.value;
		var return_monthyear = document.general_enq.return_monthyear.value;
		
		//- empty return date is ok if the depart date was blank too
		if ((return_day == "" && return_monthyear == "") && empty_depart_date) {
			$("#return_error").html("");
		}
		else if ((date_validation(return_day, return_monthyear)) == "invalid") {
			$("#return_error").html("invalid date");
			invalid_fields = true;
		}
		else {
			//- can only perform date comparisson if depart date is set
			if (!empty_depart_date) {

				//- check to make sure return date is after departure date
				var split_departmonthyear = depart_monthyear.split("/");
				var depart_month = split_departmonthyear[0];
				var depart_year = split_departmonthyear[1];
				
				//- only validate return date if one supplied (must be supplied when depart date is set)
				if (return_day != "" && return_monthyear != "") {
					var split_returnmonthyear = return_monthyear.split("/");
					var return_month = split_returnmonthyear[0];
					var return_year = split_returnmonthyear[1];
					
					if (new Date(return_year, return_month, return_day) >= new Date(depart_year, depart_month, depart_day)) {
						$("#return_error").html("");
					}
					else {
						//- return date can only conflict if departure date is not null
						if (depart_year != "" && depart_month != "" && depart_day != "") {
							$("#return_error").html("dates conflict");
							invalid_fields = true;
						}
					}
				}
				else {
					$("#return_error").html("incomplete dates");
					invalid_fields = true;
				}
			}
			//- depart date was empty, incomplete date set
			else {
				$("#depart_error").html("incomplete dates");
				invalid_fields = true;
			}
		}
	}

	//- only submit form if no errors reported
	if (!invalid_fields) {
		return true;
	}
	else {
		alert('please correct any errors and resubmit');
		return false;
	}
}


function form_validate_skinny() {

	var invalid_fields = false;
	
	//- name validation (3 fields required)
	var message = "all fields req'd";
	var name_error = false;

	//- title required	
	if (document.general_enq.ecf_title.value == "") { 
		name_error = true;
	}
	
	//- first name required
	if (document.general_enq.ecf_firstname.value == "" || document.general_enq.ecf_firstname.value == "First name") {
		name_error = true;
	}

	//- last name required
	if (document.general_enq.ecf_lastname.value == "" || document.general_enq.ecf_lastname.value == "Last name") {
		name_error = true;
	}
	
	if (name_error) {
		$("#name_error").html(message);
		invalid_fields = true;
	}
	else {
		$("#name_error").html("");
	}

	//- daytime phone validation (required field)
	if (document.general_enq.ecf_daytimeph.value  == "") {
		$("#phone_error").html("invalid phone");
		invalid_fields = true;
	}
	else {
		$("#phone_error").html("");
	}
	
	//- email validation (required field and format check)
	var email_val = document.general_enq.ecf_email.value;
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email_val) && email_val != "") {
		$("#email_error").html("");
	}
	else {
		$("#email_error").html("invalid email address");
		invalid_fields = true;
	}
	
	//- postcode validation (required field and length check)
	var postcode_val = document.general_enq.ecf_postcode.value;
	if (!isNaN(postcode_val) && (postcode_val.length == 4)) {
		$("#postcode_err").html("");
	}
	else {
		$("#postcode_err").html("invalid postcode");
		invalid_fields = true;
	}

	//- departure date validation (not required field but need to stop non-existent dates)
	var depart_day = document.general_enq.depart_day.value;
	var depart_monthyear = document.general_enq.depart_monthyear.value;
	
	//- empty depart date is ok
	if (depart_day == "" && depart_monthyear == "") {
		$("#depart_error").html("");
		var valid_depart_date = true;
		var empty_depart_date = true;
	}

	//- validate depart date if both parts are present
	else if ((date_validation(depart_day, depart_monthyear)) == "invalid") {
		$("#depart_error").html("invalid date");
		$("#return_error").html("");
		invalid_fields = true;
		var valid_depart_date = false; //- runs / disables the return date validation
	}
	else {
		$("#depart_error").html("");
		var valid_depart_date = true;
	}
	
	//- return date validation (not required field but need to stop non-existent dates)
	if (valid_depart_date) {
		var return_day = document.general_enq.return_day.value;
		var return_monthyear = document.general_enq.return_monthyear.value;
		
		//- empty return date is ok if the depart date was blank too
		if ((return_day == "" && return_monthyear == "") && empty_depart_date) {
			$("#return_error").html("");
		}
		else if ((date_validation(return_day, return_monthyear)) == "invalid") {
			$("#return_error").html("invalid date");
			invalid_fields = true;
		}
		else {
			//- can only perform date comparisson if depart date is set
			if (!empty_depart_date) {

				//- check to make sure return date is after departure date
				var split_departmonthyear = depart_monthyear.split("/");
				var depart_month = split_departmonthyear[0];
				var depart_year = split_departmonthyear[1];
				
				//- only validate return date if one supplied (must be supplied when depart date is set)
				if (return_day != "" && return_monthyear != "") {
					var split_returnmonthyear = return_monthyear.split("/");
					var return_month = split_returnmonthyear[0];
					var return_year = split_returnmonthyear[1];
					
					if (new Date(return_year, return_month, return_day) >= new Date(depart_year, depart_month, depart_day)) {
						$("#return_error").html("");
					}
					else {
						//- return date can only conflict if departure date is not null
						if (depart_year != "" && depart_month != "" && depart_day != "") {
							$("#return_error").html("dates conflict");
							invalid_fields = true;
						}
					}
				}
				else {
					$("#return_error").html("incomplete dates");
					invalid_fields = true;
				}
			}
			//- depart date was empty, incomplete date set
			else {
				$("#depart_error").html("incomplete dates");
				invalid_fields = true;
			}
		}
	}

	//- only submit form if no errors reported
	if (!invalid_fields) {
		return true;
	}
	else {
		alert('please correct any errors and resubmit');
		return false;
	}
}

function form_validate_mini(){
	var invalid_fields = false;
	
	//- name validation (3 fields required)
	var message = "all fields req'd";
	var name_error = false;

	//- first name required
	if (document.general_enq.ecf_firstname.value == "" || document.general_enq.ecf_firstname.value == "Your name") {
		name_error = true;
	}

	if (name_error) {
		$("#ecf_firstname").css({"border":"1px solid #EE0000", "color":"#EE0000"});
		invalid_fields = true;
	}
	else {
		$("#ecf_firstname").css({"border":"1px solid #444444", "color":"#444444"});
	}

	//- daytime phone validation (required field)
	if (document.general_enq.ecf_daytimeph.value  == "" || document.general_enq.ecf_daytimeph.value  == "Phone no.") {
		$("#ecf_daytimeph").css({"border":"1px solid #EE0000", "color":"#EE0000"});
		invalid_fields = true;
	}
	else {
		$("#ecf_daytimeph").css({"border":"1px solid #444444", "color":"#444444"});
	}
	
	//- email validation (required field and format check)
	var email_val = document.general_enq.ecf_email.value;
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email_val) && email_val != "") {
		$("#ecf_email").css({"border":"1px solid #444444", "color":"#444444"});
	}
	else {
		$("#ecf_email").css({"border":"1px solid #EE0000", "color":"#EE0000"});
		invalid_fields = true;
	}
	
	//- postcode validation (required field and length check)
	var postcode_val = document.general_enq.ecf_postcode.value;
	if (!isNaN(postcode_val)) {// && (postcode_val.length == 4)
		$("#ecf_postcode").css({"border":"1px solid #444444", "color":"#444444"});
	}
	else {
		$("#ecf_postcode").css({"border":"1px solid #EE0000", "color":"#EE0000"});
		invalid_fields = true;
	}

	//- only submit form if no errors reported
	if (!invalid_fields) {
		return true;
	}
	else {
		alert('please correct any errors and resubmit');
		return false;
	}
}

function date_validation(day, monthyear) { //- parameter example (02, 102009) for 02 Oct 2009
	//- incomplete dates should return an error
	if (((day != "") && (monthyear == "")) || ((day == "") && (monthyear != ""))) {
		return("invalid");
	}
	//- begin actual validation
	else {
		var split_monthyear = monthyear.split("/");
		var month = split_monthyear[0];
		var year = split_monthyear[1];
		
		//- determine how many days in the selected month
		//- leap year check
		if (month == "02") {
			if ((year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0))) {
				var days_in_month = 29;
			}
			else {
				var days_in_month = 28;
			}
		}
		else if (month == "04" || month == "06" || month == "09" || month == "11") {
			var days_in_month = 30;
		}
		else {
			var days_in_month = 31;
		}

		if (day <= days_in_month) {
			return("valid");
		}
		else {
			return("invalid");
		}
	}
}