/**********************************************************************
	This function checks if the user has entered the required data
	and if the format of the data is correct
**********************************************************************/
function check_form() {
	var error = "";	// contains the error message
	var form_name = "contact_form";	// name of the form
	var fields = Array("name", "email", "topic", "message");	// form fields that are required
	
	// Checking if all the fields have been enetered
	for (var i=0; i<fields.length; i++) {
		var temp = trim(eval("document." + form_name + "." + fields[i] + ".value"));
		if (temp == "")
			error += title_case(fields[i]) + " field is empty.\n";
		else if (fields[i] == "email") {	// Check if email address is of valid format
			if ( validate_email(temp) === false)
				error += "Email address is invalid.\n";
		}
	}
	
	if (error != "")
		alert(error);
	else {	// Submitting the form
		eval("document." + form_name + ".submit()");
	}
}

/**********************************************************************
	This function converts a string to title case.
**********************************************************************/
function title_case(str) {
	var ignore_array = Array("and", "etc.", "of", "or");
	var return_str = "";
	var words = str.split(" ");
	for(keyvar in words)
		return_str += ' ' + words[keyvar].substr(0,1).toUpperCase() + words[keyvar].substr(1,words[keyvar].length);
	
	return trim(return_str);
}

/**********************************************************************
	This function trims the string.
**********************************************************************/
function trim(str) {
	return str.replace(/^\s+|\s+$/g,"");
}

/**********************************************************************
	This function validates the email address.
**********************************************************************/
function validate_email(address) {
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	if (reg.test(address) == false)
		return false;
   else
   	return true;
}