function strpos( haystack, needle, offset)
{
	var len = haystack.length;
	var nedLen = needle.length;
	for(var i=0; i < len; i++)
	{
		if(haystack.substr(i,nedLen) == needle)
		{
			 return i;
		}
	}
	return false;
}

function resetForm(){
		document.forms["contactForm"].reset();
		window.location = 'contact.php';
	}

	function submitForm(){
		if (validateRequest()){
			document.forms["contactForm"].submit();
		}
	}
		
	var arrValidateNotBlank = new Array('firstname~first name', 'surname~surname', 'comments~enquiry');
		
	function validateRequest(){
		var errorFieldStyle = 'url(/images/requiredField.gif) no-repeat #fcfcfc';
		var sErrorMesg = '';
			
		//Not blank fields.
		for(var i=0;i < arrValidateNotBlank.length;i++){
			idxDelimiter = arrValidateNotBlank[i].indexOf('~');
			elmField = document.getElementById(arrValidateNotBlank[i].substr(0,idxDelimiter));
			if (elmField.value == ''){
				sErrorMesg = sErrorMesg + '<br>- Please enter a value for ' + arrValidateNotBlank[i].substr(idxDelimiter + 1,arrValidateNotBlank[i].length) + '.';
				elmField.style.background = errorFieldStyle;
			}else{
				elmField.style.background = '';
			}
		}
		
		// Check for url's in comments
		var elmComments = document.getElementById('comments');
		if ((strpos(elmComments.value, 'href', 0) !== false ) || (strpos(elmComments.value, 'www', 0) !== false ) || (strpos(elmComments.value, 'http', 0) !== false ))
		{
			sErrorMesg = sErrorMesg + '<br />- URLs are not allowed in comments.';
			elmComments.style.background = errorFieldStyle;
		}
		
		//At least one of email or phone must have a value. 
		var elmEmail = document.getElementById('email');
		var elmPhone = document.getElementById('phone');
		if (elmEmail.value == '' && elmPhone.value == ''){
			sErrorMesg = sErrorMesg + '<br>- Please enter either a phone number or email address.';
			elmEmail.style.background = errorFieldStyle;
			elmPhone.style.background = errorFieldStyle;
			
		}else{
			elmEmail.style.background = '';
			elmPhone.style.background = '';
		}
		
		 if ((elmEmail.value != '') && (elmEmail.value.length < 3 || elmEmail.value.indexOf('@') < 1 || elmEmail.value.indexOf('.') < 2)){
			sErrorMesg = sErrorMesg + '<br>- Please enter a valid e-mail address.';
			elmEmail.style.background = errorFieldStyle;
		}
			
		if (sErrorMesg == ''){
			//do submit
			return true;
		}else{
			//show error return false.
			document.getElementById('confirmBox').style.display='none';
			document.getElementById('errorBox').style.display='block';
			document.getElementById('boxFooter').style.display='block';
			document.getElementById('errorBox').innerHTML = '<strong>There were problems with your request:</strong><br>' + sErrorMesg + '<div class="br"></div>';
			
			idxOfHash = toString(window.location).indexOf('#');
			if(idxOfHash){
				window.location = toString(window.location).substr(0,idxOfHash) + '#formStart';
			}else{
				window.location = window.location + '#formStart';
			}
			return false;
		}
	}