//Forms
function checkContactForm(){
	var msg = "";
	var fields = new Array("name", "email_address", "comments");
	var rep = RegExp('_','gi');
	for(i=0; i < fields.length; i++){
		if(eval("document.contactform." + fields[i] + ".value") == ""){
			msg += " - Please enter your " + fields[i].replace(rep, ' ') + "\n";
		}
	}
	
	msg += checkEmail(document.contactform.email_address.value, "");
	
	if (msg != ""){
		msg = "Please fill in the following required fields:\n" + msg;
		alert(msg);
		return false;
	}
	return true;	
}

function checkBooking(formname){
	var msg = "";
	var fields = new Array("first_name", "last_name", "email_address", "number_of_adults", "number_of_children");
	var rep = RegExp('_','gi');
	for(i=0; i < fields.length; i++){
		if((document.booking_form.country.value == "Australia") && (fields[i] == "postcode") && (eval("document."+formname+"."+fields[i]+".value") != "")){
			if((eval("document."+formname+"."+fields[i]+".value.length") != 4) || (isNaN(eval("document."+formname+"."+fields[i]+".value")))){
				msg += " - Please enter a valid Australian postcode\n";
			}
		}else{
		
			if((document.booking_form.country.value == "Australia") && (fields[i] == "work_phone") && (eval("document."+formname+"."+fields[i]+".value") != "")){
				msg += checkPhone(eval("document."+formname+".work_phone.value"), "Work phone number");
			}else if((fields[i] == "email_address") && (eval("document."+formname+"."+fields[i]+".value") != "")){
				msg += checkEmail(eval("document."+formname+".email_address.value"), "");
			}else if(eval("document."+formname+"."+fields[i]+".value") == ""){
				if(fields[i].indexOf("number_of_") == -1){
					msg += " - Please enter your " + fields[i].replace(rep, ' ') + "\n";
				}else{
					msg += " - Please enter the " + fields[i].replace(rep, ' ') + "\n";
				}
			}
		}
	}
	
	if((document.booking_form.enquiry_type[0].checked == false) && (document.booking_form.enquiry_type[1].checked == false)){
		msg += " - Please select an enquiry type\n";
	}

	//Check fax format if Australia
	if(document.booking_form.country.value == "Australia"){
		msg += checkPhone(eval("document."+formname+".fax.value"), "Fax number");
	}
	
	msg += validateDateSequences(document.getElementById("arrival_date_day").value, document.getElementById("arrival_date_month").value, document.getElementById("arrival_date_year").value, "Arrival Date", document.getElementById("departure_date_day").value, document.getElementById("departure_date_month").value, document.getElementById("departure_date_year").value, "Departure Date");
	
	if (msg != ""){
		msg = "Please fill in the following required fields:    \n" + msg;
		alert(msg);
		return false;
	}
	return true;	
}

function checkTariffDates(){
	var msg = validateFullDate(document.edit_accom.valid_from_date_day.value, document.edit_accom.valid_from_date_month.value, document.edit_accom.valid_from_date_year.value, "Valid From Date");
	msg += validateFullDate(document.edit_accom.valid_to_date_day.value, document.edit_accom.valid_to_date_month.value, document.edit_accom.valid_to_date_year.value, "Valid To Date", false);	

	if (msg != ""){
		alert(msg);
		return false;
	}
	return true;	
}

//Validation
function validateDateSequences(day1, month1, year1, desc1, day2, month2, year2, desc2){
	var msg = validateFullDate(day1, month1, year1, desc1, false);
	msg += validateFullDate(day2, month2, year2, desc2, false);	

	if(msg == ""){
		//Validate dates
		var dateok = true;
		if (eval(year2) < eval(year1)){
			dateok = false;
	
		}else if (eval(year1) == eval(year2)){
			if(eval(month2) < eval(month1)){  
				dateok = false;
			
			}else if(eval(month2) == eval(month1)){
				if (eval(day2) < eval(day1)){
					dateok = false;
				}
			}
		}
	}

	if(dateok == false){
		msg += " - " + desc1 + " must be before " + desc2 + "\n";
	}
	
	return msg;
}

function checkEmail(email, fieldname){
	if(email){
		var regex = RegExp(/^([a-zA-Z0-9_\-\.]{2,})@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/);
		if ((email.search(regex) == -1) && (email != "")){
			if ((fieldname == "undefined") || (fieldname == "")){
				return " - Email address incorrect format\n";
			}else{
				return " - " + fieldname + " incorrect format\n";			
			}
		}
	}
	return "";
}

function checkPhone(phone, fieldname){
	if(phone){
		phone = phone.replace(/\(/gi, "");
		phone = phone.replace(/\)/gi, "");
		phone = phone.replace(/ /gi, "");
		regex = RegExp(/(^0[1-9]{1}[0-9]{8}$)|(^13\d{4}$)|(^04\d{2,3}\d{6}$)/);
		if ((phone.search(regex) == -1) && (phone != "")){
			return " - " + fieldname + " incorrect format - please include the area code and only use numbers, brackets and spaces \n";;
		}
	}
	return "";
}

function checkNumField(evt){
	var chCode = (evt.which)?evt.which:evt.keyCode;
	if (typeof document.getElementById!="undefined" && typeof document.all=="undefined"){
		if ((34<evt.charCode && evt.charCode<41) || evt.charCode==46) return false;
		if ((34<chCode && chCode<41) || chCode==46) return true;
	}
	if((chCode<48 || 57<chCode) && chCode>31) return false;
	return true;
}

function checkDecField(evt){
	var chCode = (evt.which)?evt.which:evt.keyCode;
	if(evt.charCode==46 || chCode==46) return true;
	if (typeof document.getElementById!="undefined" && typeof document.all=="undefined"){
		if ((34<evt.charCode && evt.charCode<41) || evt.charCode==46) return false;
		if ((34<chCode && chCode<41) || chCode==46) return true;
	}
	if((chCode<48 || 57<chCode) && chCode>31) return false;
	return true;
}

//Dates
function validateFullDate(theDay, theMonth, theYear, testItem, pastok){
	var complete = true;
	var error = "";
	
	if (theDay == ""){
		error += " - Please enter " + testItem.toLowerCase() + " day\n";
		complete = false;	
	}
	
	if (theMonth == ""){
		error += " - Please enter " + testItem.toLowerCase() + " month\n";
		complete = false;
	}	
	
	if (theYear == ""){
		error += " - Please enter " + testItem.toLowerCase() + " year\n";
		complete = false;
	}
		
	if (complete != false){
		if (theMonth == 2){  
	
			if ((theYear > 2000) && ((theYear % 4) == 0)){ 
				if (theDay > 29){
					error += " - " + testItem + " invalid - there are only 29 days in February in " + theYear + "\n";
				}
			}
			else if (theDay > 28){
				error +=  " - " + testItem + " invalid - there are only 28 days in February in " + theYear + "\n";
			}
		}
		
		if ((theMonth == "9") || (theMonth == "4") || (theMonth == "6") || (theMonth == "11")){
			if (theDay > 30) {
				error +=  " - " + testItem + " invalid - there are only 30 days in " + convertMonthToString(theMonth) + "\n";
			}
		}
		else if (theDay > 31){
			error +=  " - " + testItem + " invalid\n";
		}
	
		if(pastok == false){
			var today = new Date();
			var tday = today.getDate();
			var tmonth = today.getMonth() + 1;
			var tyear = today.getFullYear();
	
			if (theYear < tyear){
				error +=  " - " + testItem + " is in the past\n";
		
			}else if (theYear == tyear){
				if(theMonth < tmonth){  
					error +=  " - " + testItem + " is in the past\n";
				
				}else if(theMonth == tmonth){
					if (theDay < tday){
						error +=  " - " + testItem + " is in the past\n";
					}
				}
			}
		}
	}
	return error;
}

function convertMonthToString(month){
	switch(month){
		case "1":
			return "January";
			break;

		case "2":
			return "February";
			break;

		case "3":
			return "March";
			break;

		case "4":
			return "April";
			break;

		case "5":
			return "May";
			break;

		case "6":
			return "June";
			break;

		case "7":
			return "July";
			break;

		case "8":
			return "August";
			break;

		case "9":
			return "September";
			break;

		case "10":
			return "October";
			break;

		case "11":
			return "November";
			break;

		case "12":
			return "December";
			break;
		
		default:
			return "";
	}
}
