var errorMessage = "";

function validateForm() {

if (document.getElementById('givenNames').value == ''){
	errorMessage += "<li>First name field is required</li>";
}

if (document.getElementById('surname').value == '') {
	errorMessage += "<li>Last name field is required</li>";
}

if (document.getElementById('email').value == '') {
	errorMessage += "<li>Email address field is required</li>";
} else {
	if (!checkEmail(document.getElementById('email').value) ) {
		errorMessage += "<li>Email address field does not contain a valid email address</li>";
	}
}

if (document.getElementById('endCity').value == '') {
	errorMessage += "<li>Travel destination is required</li>";
}

if (document.getElementById('region').value == '') {
	errorMessage += "<li>Region is required</li>";
}

if(document.getElementById('enq-phone').value == '') {
	errorMessage += "<li>Phone number field is required</li>";
}else{
    var phoneNumber = trim(document.getElementById('enq-phone').value);
    phoneLength = phoneNumber.replace(/[^0-9]/g,'');
    phoneLength = phoneLength.length;
    if (phoneLength < 7) {
        errorMessage += "<li>Phone number entered is too short</li>";
    }
	}

//end checks
if (errorMessage !="") errorMessage="<p>Please review the following issues<p><ul>"+errorMessage+"</ul>"
var errorMSG = document.getElementById('ErrorDiv');
//show area message area

	if (errorMessage == '') {
		hideLayer('ErrorDiv');
		errorMessage = "";
		//return false;
		return true;
	} else {
		showLayer('ErrorDiv');
		errorMSG.innerHTML = errorMessage;
		errorMessage = "";
		return false;
	}
}

//check for null values

function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

function checkEmail(str) {
		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   //alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    
		    return false
		 }

 		 return true					
	}
	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 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;
}
