/**********************************************************************
| Function Name: fnTrim
***********************************************************************
| Purpose:
| 	Removes leading and trailing white space.
| Arguments:
|	str - the string to be trimmed
| Return Value:
|	The trimmed string.
***********************************************************************/
function fnTrim(str)
{
  return str.replace(/^\s+|\s+$/g, '');
}

/**********************************************************************
| Function Name: fnValidateShortContactDetails
***********************************************************************
| Purpose:
| 	Validates that all contact details for newsletter requests.
| Arguments:
|	None
| Return Value:
|	True if form passes validation, false otherwise.
***********************************************************************/
function fnValidateShortContactDetails() 
{
	var iErrs = 0;

	// validate name
	if (fnTrim(document.frmGetDetails.Name.value) == "") {
		document.frmGetDetails.Name.className="textBoxRequiredError";
		iErrs++;
	}	
	else {
		document.frmGetDetails.Name.className = "textBoxRequired";
	};

	
	// validate company
	if (fnTrim(document.frmGetDetails.Company.value) == "") {
		document.frmGetDetails.Company.className = "textBoxRequiredError";
		iErrs++;
	}	
	else {
		document.frmGetDetails.Company.className = "textBoxRequired";
	};
	
		// validate telephone number
	if (fnTelephoneFaxValidate(document.frmGetDetails.Telephone.value, true) == false) {
		document.frmGetDetails.Telephone.className = "textBoxRequiredError";
		iErrs++;
	}	
	else {
		document.frmGetDetails.Telephone.className = "textBoxRequired";
	};
	
	// validate email address
	if (fnEmailValidate(document.frmGetDetails.Email.value) == false) {
		document.frmGetDetails.Email.className = "textBoxRequiredError";
		iErrs++;
	}
	else {
		document.frmGetDetails.Email.className = "textBoxRequired";
	};	
	
	// validate referral choice
	if (fnValidateReferalChoice() == false) {
		iErrs++;
	}
	
	if (iErrs > 0) {
		return false;
	}
	else {
		return true;
	};
}	

/**********************************************************************
| Function Name: fnValidateReferalChoice
***********************************************************************
| Purpose:
| 	Validates that the referal choice has been made.
| Arguments:
|	None
| Return Value:
|	True if form passes validation, false otherwise.
***********************************************************************/
function fnValidateReferalChoice()
{
	var iErrs = 0;
	
	if (document.frmGetDetails.Search.value == 1) {
		document.frmGetDetails.Search.className = "textBoxRequiredError";
		iErrs++;
	}	
	else {
		document.frmGetDetails.Search.className = "textBoxRequire";
	};
	
	if (document.frmGetDetails.Search.value == 7 && fnTrim(document.frmGetDetails.Other.value) == "") {
		document.frmGetDetails.Other.className = "textBoxRequiredError";
		iErrs++;
	}	
	else {
		if (document.frmGetDetails.Search.value == "Other") {
			document.frmGetDetails.Other.className = "textBoxRequired";
		}
		else {
			document.frmGetDetails.Other.className = "textBoxDisabled";
		};
	};
	
	if (iErrs > 0) {
		return false;
	}
	else {
		return true;
	};
}

/**********************************************************************
| Function Name: fnValidateFullContactDetails
***********************************************************************
| Purpose:
| 	Validates that all contact items for PDF Requests form.
| Arguments:
|	None
| Return Value:
|	True if form passes validation, false otherwise.
***********************************************************************/
function fnValidateFullContactDetails() {
	var iErrs = 0;
	
	if (fnValidateShortContactDetails() == false) {
		iErrs++;
	}
	
	if (fnValidateReferalChoice() == false) {
		iErrs++;
	}
	
	// validate position
	if (fnTrim(document.frmGetDetails.Position.value) == "") {
		document.frmGetDetails.Position.className = "textBoxRequiredError";
		iErrs++;
	}	
	else {
		document.frmGetDetails.Position.className = "textBoxRequired";
	};
	
	// validate country
	if (document.frmGetDetails.country.value == 0) {
		document.frmGetDetails.country.className = "textBoxRequiredError";
		iErrs++;
	}	
	else {
		document.frmGetDetails.country.className = "textBoxRequired";
	};

	if (iErrs > 0) {
		return false;
	}
	else {
		return true;
	};
}

/**********************************************************************
| Function Name: fnAddressValidate
***********************************************************************
| Purpose:
| 	To check that the email address entered is formed correctly.
| Arguments:
|	sStreet - street
|	sTownCity - Town or City
|	sCountyState - County or State
|	sPostZipCode - Post or Zip code.
|	sCountry - Country
| Return Value:
|	Error text
***********************************************************************/
function fnAddressValidate(sStreet, sTownCity, sCountyState, sPostZipCode, sCountry) {
	var sErrorText = ""
	
	// validate address
	if (sStreet == "") {
		sErrorText = sErrorText + "\t" + "Street 1 entry must be completed" + "\n"
	}
	
	// validate town or city
	if (sTownCity == "") {
		sErrorText = sErrorText + "\t" + "Town/City entry must be completed" + "\n"
	}
	
	// validate county or state
	if (sCountyState == "") {
		sErrorText = sErrorText + "\t" + "County/State entry must be completed" + "\n"
	}
	
	// validate post or zip code
	if (sPostZipCode == "") {
		sErrorText = sErrorText + "\t" + "Post/Zip Code entry must be completed" + "\n"
	}
	
	// validate country
	if (sCountry == "") {
		sErrorText = sErrorText + "\t" + "Country entry must be completed" + "\n"
	}
	
	return sErrorText
}

/**********************************************************************
| Function Name: fnEmailValidate
***********************************************************************
| Purpose:
| 	To check that the email address entered is formed correctly.
| Arguments:
|	sEmail - Email address
| Return Value:
|	True is valid, false otherwise
***********************************************************************/
function fnEmailValidate(sEmail) {
	var sMatch = "";
	var bValid = true;
	var emailRegExp = /^[A-Za-z0-9._%-]+@[A-Za-z0-9._%-]+\.[A-Za-z]{2,6}$/
	
	sMatch = emailRegExp.exec(sEmail)
	
	if (sMatch == null) {
		bValid = false;
	}

	return bValid;
}

/**********************************************************************
| Function Name: fnTelephoneFaxValidate
***********************************************************************
| Purpose:
| 	To check that the telephone or fax number entered is formed correctly.
| Arguments:
|	sNumber - Telephone or Fax number
| Return Value:
|	True if valid, false otherwise
***********************************************************************/
function fnTelephoneFaxValidate(sNumber) {
	var sMatch = "";
	var bValid = true;
	
	if (sNumber != "") {
		var myRe = /^[ 0-9\)\(\-]+$/
		sMatch = myRe.exec(sNumber)
		
		if (sMatch == null) {
			bValid = false;
		}
	}
	else {
		bValid = false;
	}

	return bValid;
}

/**********************************************************************
| Function Name: fnOtherOption()
***********************************************************************
| Purpose:
| 	Resets form text box colours.
| Arguments:
|	None
| Return Value:
|	None.
***********************************************************************/
function fnOtherOption()
{
	if (document.frmGetDetails.Search.value == 7) {
		document.frmGetDetails.Other.disabled = false;
		document.frmGetDetails.Other.className = "textBoxRequired";
	}
	else {
		document.frmGetDetails.Other.disabled = true;
		document.frmGetDetails.Other.className = "textBoxDisabled";
	};
}

/**********************************************************************
| Function Name: fnNewsletterRequest()
***********************************************************************
| Purpose:
| 	Validates that all items of the newsletter request form have been
|	entered.
| Arguments:
|	None
| Return Value:
|	True is forms passes validation, false otherwise.
***********************************************************************/
function fnNewsletterRequest() 
{
	if (fnValidateShortContactDetails() == false) {
		document.getElementById("errorMessage").style.display = "block";
		return false;
	} else {
		document.getElementById("errorMessage").style.display = "none";
		return true;
	}
}