function ltrim(str) { 
	for(var k = 0; k < str.length && isWhitespace(str.charAt(k)); k++);
	return str.substring(k, str.length);
}
function rtrim(str) {
	for(var j=str.length-1; j>=0 && isWhitespace(str.charAt(j)) ; j--) ;
	return str.substring(0,j+1);
}
function trim(str) {
	return ltrim(rtrim(str));
}
function isWhitespace(charToCheck) {
	var whitespaceChars = " \t\n\r\f";
	return (whitespaceChars.indexOf(charToCheck) != -1);
}
function IsEmpty(s) {
   if ((s.length==0) || (s==null) || trim(s)=="") {
      return true;
   }
   else { return false; }
}	

function IsEmail(s){
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
	if (filter.test(s))
		testresults=true
	else{
		testresults=false
	}
	return (testresults)
}

function submitEmail(email, name, message) {
	if (!IsEmpty(email) && !IsEmpty(name) && !IsEmpty(message)) {
		if (IsEmail(email)) {
			$('btn_email').innerHTML = 'Sending...';
			var myAjax = new Ajax.Request('/send.php', {method: 'post', parameters: $('frm_email').serialize(), onComplete: function(transport) {
			  	$('frm_email').insert({ 'after' : '<h4>Thanks!</h4><p>We\'ll get back to you as soon as possible. Thanks for your interest in MetroGuild.</p><p>Metroguild will never share your personal information, send you unsolicited mail or make unsolicited phone calls.</p>' });
				$('frm_email').remove();
			}});
			_mw._trackAction('3', email +'|'+ message + '|' + name);
		} else {
			alert("That doesn't seem to be a valid email address, please check it and click on Send again.");
		}
	} else {
		alert("Please fill out all the fields and click on Send again.");
	}
}
