Event.observe(window, 'load', function() {
  Event.observe('cform', 'submit', checkForm);
});

String.prototype.isEmail = function() {
    /* boolean, returns true or false if the string passed through is an email
       example use: var myEmail = 'christoff@securemail.com';
       if (myEmail.isEmail()) { do someting } else { do something else }
    */
    var validEmailRegex = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;

    return this.match(validEmailRegex); // regex defined above but it can be local.
};

function notice (message) {
	$("notice").update(message);
	
}

function checkForm (event) {
	var noSubmit = false

	if ($F('to-menu') == "Select a recipient") {
		notice("Please Select A Recipient")
		noSubmit = true;
	};
	if ($F('name-field').strip() == "") {
		notice("Please enter your name")
		noSubmit = true;
	};
	email = $F('email-field');
	if (!(email.isEmail())) {
		Event.stop(event);
		notice("Please enter a valid email address")
		noSubmit = true;
	};
	if (!noSubmit) {
		var data = $('cform').serialize(true)
		// new Ajax.Request("/contact.php", {
		// 	method: 'post',
		// 	parameters: data,
		// 	onSuccess: function(response) {
		// 		notice("Yay" + response.responseText)
		// 	},
		// 	onFailure: function(response) {
		// 		notice("Fail")
		// 	}
		// });
		notice(Object.toJSON(data))
	}
};
