var errorMessage = "";
function validateForm(frm) {
	var validform = true;
	errorMessage = "";
	var v1 = validateGivenName();
	var v2 = validateLastName();
	//var v3 = validateEmail();
	//var v4 = validateSpecials();
	//var v5 = validatePhone();
	var v11 = validateEmailPhone();
	
	var v12 = validateNewsletter();
	var v6 = validateStartCity();
	var v7 = validateEndCity();
	var v8 = validateRegion();
	var v9 = validateStore();
	var v10= validateComments();
	
	validform = v1 && v2 && /*v3 && v5 &&*/ v6 && v7 && v10 && v8 && v9 && v11 && v12 ;
	//validform = v1 && v2 && v3 && v5 && v6 && v7 && v10 && v8 && v9;// && v11 && v12 ;
	setRegionId();
	
	saveDestCookie();
	//saveCookie();
	//alert('validform ' + validform);
	if (validform) {
		hideLayer('ErrorDiv');
		/*
		hideLayer('FormDiv');
		hideLayer('MessageSentDiv');
		showLayer('SendingDiv');
		InitializeTimer();
		*/
		fixEmail();
		addAdditionalCommentText();
		//validform = false;
	} else {
		showLayer('ErrorDiv');
		var em = findObj('errorMessage');
		em.innerHTML = errorMessage;
		document.onlineEnquiryForm.givenNames.focus();
	}
	return validform;
}

function addAdditionalCommentText() {
	// grab the value
	commentval = document.onlineEnquiryForm.details.value;
	// newsletter
	if(document.onlineEnquiryForm.dates_flexible.checked){
		commentval += "\n\n"+"Travel Dates are Flexible.";
	}
	// preferred contact
	if(document.getElementById('contactEmail').checked){
		commentval += "\n\n"+"Preferred contact by Email.";
	} else {
		commentval += "\n\n"+"Preferred contact by Phone.";
	}
	// set if back
	document.onlineEnquiryForm.details.value = commentval;
}
function fixEmail () {
	emailval = "";
	emailval = document.onlineEnquiryForm.email.value;
	if(emailval=="") {
		document.onlineEnquiryForm.email.value = "noreply@flightcentre.co.nz";
		document.onlineEnquiryForm.emailNewsletter.value = "noreply@flightcentre.co.nz";
	}
	//alert(document.onlineEnquiryForm.email.value);
}

function setRegionId(){
	var sRegion = document.onlineEnquiryForm.region.options[document.onlineEnquiryForm.region.selectedIndex].value;
	var sStoreID = '';
	if(sRegion.indexOf('~')>-1){
		var aId = sRegion.split('~');
		if(aId.length>0){
			sStoreID = aId[1];
		}
	}else{
		if(sRegion!=''){
    		var oStore =document.onlineEnquiryForm.elements[sRegion];
    		if(oStore){
    	 		sStoreID = oStore.options[oStore.selectedIndex].value;
			}else{
				alert('couldn\'t find region'); 
			}	}
	}
	document.onlineEnquiryForm.regionid.value = sStoreID;
}
function getRegionId(sRegion){
	if(sRegion.indexOf('~')>-1){
		return(0);
	}else{
		var oStore =document.onlineEnquiryForm.elements[sRegion];
		if(oStore){
			return(oStore.selectedIndex);
		}
	}
}
function saveDestCookie(){
	var oForm = document.onlineEnquiryForm;
	var sValue =oForm.endCity.value;
	setCookie('FCEnquiryDest', sValue);
}
function saveCookie(){
	   var oForm = document.onlineEnquiryForm;
	   if (oForm.saveCookie.checked){
	   var sTitle=oForm.title.options[oForm.title.selectedIndex].value;
	   var sFname=oForm.givenNames.value;
	   var sLname=oForm.surname.value;
	   var sEmail=oForm.email.value;
	   var sPhone=oForm.phone.value;
	   var sMobile=oForm.mobile.value;
	   var sSpecialoffer=(oForm.specialOffers[0].checked)?'yes':'no';
	   var sRegion=oForm.region.selectedIndex;
	   var sStore=getRegionId(oForm.region.value);
	   var sConsultant=oForm.consultant.value;
	   saveCookieInfo(sTitle,sFname, sLname, sEmail, sPhone, sMobile, sSpecialoffer, sRegion,sConsultant,sStore);
   }
   else{
	   clearCookies();
   }
}
function validateEmailPhone () {
	var validated = false;
	if(validateEmailSelected()&&validatePhoneSelected()){
		validated = true;
	}
	return validated;
}
function validateNewsletter () {
	var validated = true;
	if(document.getElementById('specialOffersYes').checked) {
		emailval = "";
		emailval = document.onlineEnquiryForm.email.value;
		if (!emailval) {
			validated = false;
			errorMessage += "<b>Email address field is required for Specials and Updates</b><br>";
		} else {
			if (!checkEmail(emailval)) {
				validated = false;
				errorMessage += "<b>Email address field does not contain a valid email address</b><br>";
			}
		}
		//alert('validateEmail '+ validated);
	}
	return validated;
}
function validateGivenName() {
	var validated = true;
	if (!document.onlineEnquiryForm.givenNames.value) {
		validated = false;
		errorMessage += "<b>First name field is required</b><br />";
	}
	return validated;
}
function validateRegion(){
	var validated = true;
	if (document.onlineEnquiryForm.region.value=='') {
		validated = false;
		errorMessage += "<b>Region is required</b><br />";
	}
	return validated;
}
function validateStore(){
	var validated = true;
	var oForm = document.onlineEnquiryForm;
	if(oForm.region.value.indexOf('~')<0&&oForm.region.value!=''){
		    if (oForm.elements[oForm.region.value].value==''){
			    validated = false;
			    errorMessage += "<b>Store is required</b><br />";
			}
	}
	return validated;
}
function validateEndCity() {
	var validated = true;
	if (findObj('endCity')) {
		if (!document.onlineEnquiryForm.endCity.value) {
			validated = false;
			errorMessage += "<b>Arrival city field is required</b><br />";
		}
	}
	return validated;
}
function validateStartCity() {
	var validated = true;
	if (findObj('startCity')) {
		if (!document.onlineEnquiryForm.startCity.value) {
			validated = false;
			errorMessage += "<b>Departure city field is required</b><br />";
		}
	}
	return validated;
}
function validateLastName() {
	var validated = true;
	if (!document.onlineEnquiryForm.surname.value) {
		validated = false;
		errorMessage += "<b>Last name field is required</b><br />";
	}
	//alert('validateLastName '+ validated);
	return validated;
}
function validateEmailSelected () {
	var validated = true;
	if(document.onlineEnquiryForm.contactEmail.checked) {
		validated = validateEmail();
	}
	return validated;
}
function validateEmail() {
	var validated = true;
	emailval = "";
	emailval = document.onlineEnquiryForm.email.value;
	if (!emailval) {
		validated = false;
		errorMessage += "<b>Email address field is required</b><br>";
	} else {
		if (!checkEmail(emailval)) {
			validated = false;
			errorMessage += "<b>Email address field does not contain a valid email address</b><br>";
		}
	}
	//alert('validateEmail '+ validated);
	return validated;
}
function validateSpecials() {
	var validated = true;
	if ((document.onlineEnquiryForm.specialOffers[0].checked == false) && (document.onlineEnquiryForm.specialOffers[1].checked == false)) {
		validated = false;
		errorMessage += "<b>Would you like to receive email regarding special offers? field is required</b></br>";
	}
	return validated;
}
function validatePhoneSelected () {
	var validated = true;
	if(document.onlineEnquiryForm.contactPhone.checked) {
		validated = validatePhone();
	}
	return validated;
}
function validatePhone() {
	var validated = true;
	if (!document.onlineEnquiryForm.phone.value||document.onlineEnquiryForm.phone.value=="(__) ___ ____") {
		validated = false;
		errorMessage += "<b>Phone number field is required</b><br />";
	}
	//alert('validatePhone '+ validated);
	return validated;
}
function validateComments(){
	var validated = true;
	//if (!document.onlineEnquiryForm.details.value || document.onlineEnquiryForm.details.value=='Please provide as much detail about your intended holiday here. This will assist your consultant in providing the best possible price.') 
	if (!document.onlineEnquiryForm.details.value || document.onlineEnquiryForm.details.value=='Please include any details that will assist your consultant in finding you a perfect trip at a great price.') 
	{
		validated = false;
		errorMessage += "<b>Comments/Questions field is required</b><br />";
	}
	return validated;
}
function hideLayer(whichLayer) {
	var oDiv = findObj(whichLayer);
	if (oDiv) {
		if (oDiv.style) {
			oDiv = oDiv.style;
		}
		oDiv.display = 'none';
	} else {
		alert('Couldn\'t find object '+whichLayer);
	}
}
function showLayerNoScroll(whichLayer) {
	var oDiv = findObj(whichLayer);
	if (oDiv) {
		if (oDiv.style) {
			oDiv = oDiv.style;
		}
		oDiv.display = 'block';
	} else {
		alert('Couldn\'t find object '+whichLayer);
	}
}
function showLayer(whichLayer) {
	var oDiv = findObj(whichLayer);
	scroll(0,0);
	if (oDiv) {
		if (oDiv.style) {
			oDiv = oDiv.style;
		}
		oDiv.display = 'block';
	} else {
		alert('Couldn\'t find object '+whichLayer);
	}
}
function findObj(theObj, theDoc) {
	var p, i, foundObj;
	if (!theDoc) {
		theDoc = document;
	}
	if ((p=theObj.indexOf("?"))>0 && parent.frames.length) {
		theDoc = parent.frames[theObj.substring(p+1)].document;
		theObj = theObj.substring(0, p);
	}
	if (!(foundObj=theDoc[theObj]) && theDoc.all) {
		foundObj = theDoc.all[theObj];
	}
	for (i=0; !foundObj && i<theDoc.forms.length; i++) {
		foundObj = theDoc.forms[i][theObj];
	}
	for (i=0; !foundObj && theDoc.layers && i<theDoc.layers.length; i++) {
		foundObj = findObj(theObj, theDoc.layers[i].document);
	}
	if (!foundObj && document.getElementById) {
		foundObj = document.getElementById(theObj);
	}
	return foundObj;
}
function formTimeout() {
	document.getElementById('SendingDiv').innerHTML = "The enquiry could not be sent due to an unexpected technical issue. Please call us on 0800 32 66 54 for personal assistance. Thank you.";
}
var secs;
var timerID = null;
var timerRunning = false;
var delay = 1000;
function InitializeTimer() {
	// Set the length of the timer, in seconds
	secs = 30;
	StopTheClock();
	StartTheTimer();
}
function StopTheClock() {
	if (timerRunning) {
		clearTimeout(timerID);
	}
	timerRunning = false;
}
function StartTheTimer() {
	if (secs == 0) {
		StopTheClock();
		formTimeout();
	} else {
		self.status = secs;
		secs = secs-1;
		timerRunning = true;
		timerID = self.setTimeout("StartTheTimer()", delay);
	}
}
