// contactus.js

function resetRespond(){	//reply no was checked, clear all selected repsond radio button
	for (var i = 0; i < document.feedback.respond.length; i++)
		document.feedback.respond[i].checked = false;
	return true;
}

function resetReply(thisMethod){	//some respond radio button was seleced, set focus to the corresponding input field, check the reply yes radio button
	if (thisMethod == 'email')
		document.feedback.email.focus();
	else if (thisMethod == 'letter')
		document.feedback.post1.focus();
	else if (thisMethod == 'fax')
		document.feedback.faxnumber.focus();
	document.feedback.yes.checked = true;
	return true;
}

function trim(thisString){	//use a regular expression to replace leading and trailing spaces with the empty string
  	return thisString.replace(/(^\s*)|(\s*$)/g,"");
}

function validateEmailAddress(thisEmailAddress){	//top level domain names have two to four characters only
	var regExp = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})$/;
	return regExp.test(thisEmailAddress);
}

function filterFaxNumber(thisFaxNumber){	//extract integers only
	return thisFaxNumber.replace(/[^0-9]/g, '');	//[^0-9] matches any non-digit, replacing it by '' globally
}

function filterPhoneNumber(thiPhoneNumber){	//extract integers only
	return thisPhoneNumber.replace(/[^0-9]/g, '');	//[^0-9] matches any non-digit, replacing it by '' globally
}

function validateContactUsForm(){

	//trim all text fields	
	document.feedback.Name.value = trim(document.feedback.Name.value);
	document.feedback.Phone.value = trim(document.feedback.Phone.value);
	document.feedback.Location.value = trim(document.feedback.Location.value);
	document.feedback.Length.value = trim(document.feedback.Length.value);
	document.feedback.Width.value = trim(document.feedback.Width.value);
	document.feedback.TSFloor.value = trim(document.feedback.TSFloor.value);

	
	//check name is filled out
	if (document.feedback.Name.value == ''){
		alert('Please enter your Name in the Name box.');
		return false;
	}
	
	//check phone number is filled out
	if (document.feedback.Phone.value == ''){
		alert('Please enter your Phone Number in the Phone Number box.');
	return false;
	}
	
	//check width is filled out
	if (document.feedback.Location.value == ''){
		alert('Please enter a town.');
		return false;
	}

	//check loading is filled out
	if (document.feedback.Loading.selectedIndex == 0){
		alert('Please select loading from the drop down list.');
		return false;
	}
	
	//check length is filled out
	if (document.feedback.Length.value == ''){
		alert('Please enter a Length (in metres) in the Length box.');
		return false;
	}

	//check width is filled out
	if (document.feedback.Width.value == ''){
		alert('Please enter a Width (in metres) in the Width box.');
		return false;
	}

	//check width is filled out
	if (document.feedback.TSFloor.value == ''){
		alert('Please enter a Top side of Floor (in metres) in the Top side of Floor box.');
		return false;
	}

	return true;
}