function trimString (str) {
  str = this != window? this : str;
  return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
}

String.prototype.trim = trimString;

function isValidEmail(str) {
//Function to validate Email Address field
	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){
//	   alert("Invalid E-mail ID")
	   return false
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
//	    alert("Invalid E-mail ID")
	    return false
	}

	 if (str.indexOf(at,(lat+1))!=-1){
//	    alert("Invalid E-mail ID")
	    return false
	 }

	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
//	    alert("Invalid E-mail ID")
	    return false
	 }

	 if (str.indexOf(dot,(lat+2))==-1){
//	    alert("Invalid E-mail ID")
	    return false
	 }
	
	 if (str.indexOf(" ")!=-1){
//	    alert("Invalid E-mail ID")
	    return false
	 }

	 return true					
}

function checkRadioButtons(list) {
/*
function loops through and test radio buttons for checked ones
returns false if none are checked and true if one is.
*/
	var isOneChecked = false;
	document.write(list[0].value);
	for (i=0;i<list.length;i++) {
		isOneChecked = list[i].checked;
		if (isOneChecked)
			break;
	}

	return isOneChecked;
}

function validate_book_tour_form(form) {
	var error_str = "";

	if (form.first_name.value.trim()=='') {
		error_str += "\tFirst Name;\n";
		form.first_name.style.backgroundColor = "red";
	} else 
		form.first_name.style.backgroundColor = "white";
	if (form.last_name.value.trim()=='') {
		error_str += "\tLast Name;\n";
		form.last_name.style.backgroundColor = "red";
	} else 
		form.last_name.style.backgroundColor = "white";
	if (!isValidEmail(form.email.value.trim())) {
		error_str += "\tEmail Address;\n";
		form.email.style.backgroundColor = "red";
	} else 
		form.email.style.backgroundColor = "white";
	if (form.country_of_residence.value.trim()=='') {
		error_str += "\tCountry of Residence;\n";
		form.country_of_residence.style.backgroundColor = "red";
	} else 
		form.country_of_residence.style.backgroundColor = "white";

	if (error_str.length > 0) {
		alert("The following required fields are blank or improperly filled in:\n"+error_str+"\nPlease enter valid data for these fields...");
		return false;
	}
}
