

///////////////////////////////////////////////////////////////
//
// Function isEmpty():
//
//
function isEmpty(str) {
  return str.match(/^\s*$/) != null;
}

///////////////////////////////////////////////////////////////
//
// Function isEmptyIter():
//
//
function isEmptyIter(val, index) {
	
	return $F(val).match(/^\s*$/);
}

///////////////////////////////////////////////////////////////
//
// Function isAlpha():
//
//
function isAlpha(str) {
  return str.match(/^[a-zA-Z]+$/) != null;
}


///////////////////////////////////////////////////////////////
//
// Function isAlphaNum():
//
//
function isAlphaNum(str) {
  return str.match(/^\w+$/) != null;
}


///////////////////////////////////////////////////////////////
//
// Function isEmail():
//
//
function isEmail(str) {
  // pattern from bugzilla editparams.cgi
  return str.match(/^[\w\.\+\-=]+@[\w\.\-]+\.[\w\-]+$/) != null;
}


///////////////////////////////////////////////////////////////
//
// Function isNum():
//
//
function isNum(str) {
  return str.match(/^\d+$/) != null;
}


///////////////////////////////////////////////////////////////
//
// Function isCurr():
//
//
function isCurr(str) {
  return str.match(/^(\d{1,3}(\.\d{3})*|\d+)(\,\d{2})?$/) != null;
}


///////////////////////////////////////////////////////////////
//
// Function isTimeStr():
//
//
function isTimeStr(str) {
	return str.match(/^([01]?[0-9]|2[0-3]):[0-5][0-9]$/) != null;
}

///////////////////////////////////////////////////////////////
//
// Function trim():
//
//
function trim(inputStringTrim) {
	fixedTrim = "";
	lastCh = " ";
	for (x=0; x < inputStringTrim.length; x++) {
		ch = inputStringTrim.charAt(x);
		if ((ch != " ") || (lastCh != " ")) { fixedTrim += ch; }
		lastCh = ch;
	}
	if (fixedTrim.charAt(fixedTrim.length - 1) == " ") {
		fixedTrim = fixedTrim.substring(0, fixedTrim.length - 1); 
	}
	return fixedTrim;
}



///////////////////////////////////////////////////////////////
//
// Function formValidate():
//
//
function formValidate(form) {

	// company_name
	if (isEmpty(form.company_name.value)) {
		alert("The company name field is required.");
		form.company_name.focus();
		return false;
	}

	// address_line_1
	if (isEmpty(form.address_line_1.value)) {
		alert("The first line of your address is required.");
		form.address_line_1.focus();
		return false;
	}

	// city
	if (!isAlphaNum(form.city.value)) {
		alert("The city field is required.");
		form.city.focus();
		return false;
	}

	// country
	var country = form.country.options[form.country.selectedIndex].value;
	if (isEmpty(country)) {
		alert("Please select your country before submitting the form.");
		form.country.focus();
		return false;
	}

	// contact_name
	if (isEmpty(form.contact_name.value)) {
		alert("Please enter your name in the Contact Name field.");
		form.contact_name.focus();
		return false;
	}

	// telephone
	if (isEmpty(form.telephone.value) || (isNum(form.number_of_employees.value) && form.telephone.length < 6)) {
		alert("The phone number field is required - you have either left it blank or entered an invalid number.");
		form.telephone.focus();
		return false;
	}

	// email
	if (!isEmail(form.email.value)) {
		alert("The email address you entered was missing or invalid.");
		form.email.focus();
		return false;
	}

	// company_turnover
	if (isEmpty(form.company_turnover.value)) {
		alert("Please enter your company turnover before submitting the form.");
		form.company_turnover.focus();
		return false;
	}

	// number_of_employees
	if (!isNum(form.number_of_employees.value)) {
		alert("Please enter your approximate number of employees.");
		form.number_of_employees.focus();
		return false;
	}

	// business_type
	if (isEmpty(form.business_type.value)) {
		alert("Please specify your business type.");
		form.business_type.focus();
		return false;
	}

	return true;

}

