document.observe('dom:loaded', function () { 
  var send_button   = $('MsgSendButton');
  var contact_forms = $$('input.contactForms');
  
  if (send_button != null) {
    send_button.observe('click', send_message);
	}
	
	if (contact_forms != null) {
    contact_forms.invoke('observe', 'focus', clear_field);
    contact_forms.invoke('observe', 'blur', set_default);
  }

});

function send_message(event) {
  event.stop();
  
	var url = 'fm.php';
	var params = '';
	if ($('MsgEmail') != null) {
	  params+= 'email=' + encodeURIComponent($('MsgEmail').value)
	}
	if ($('MsgMessage') != null) {
	  params+= '&message=' + encodeURIComponent($('MsgMessage').value);
	}
	if ($('MsgPhone') != null) {
	  params+= '&phone=' + encodeURIComponent($('MsgPhone').value);
	}

  $('contactNotification').update('<img src="contact/load.gif" border="0" alt="loading..." /> Ihre Nachricht wird &uuml;bertragen.');
	Effect.BlindDown('contactNotification'); 

	var ajax = new Ajax.Updater({success: 'contactNotification'},url,{method: 'post', parameters: params, onFailure: report_error,  onSuccess: appear_on_success });
}

function report_error(request) {
	$('contactNotification') = "Error";
}

function appear_on_success(request) {
	if ($('MsgEmail') != null) {
	  $('MsgEmail').value = $('MsgEmail').defaultValue;
	}
	if ($('MsgMessage') != null) {
	  $('MsgMessage').value = $('MsgEmail').defaultValue;
	}
	if ($('MsgPhone') != null) {
	  $('MsgPhone').value = $('MsgEmail').defaultValue;
	}  
}

function clear_field(event) {
	if (this.value == this.defaultValue) {
		this.value = "";
	}
}

function set_default(event) {
	if (this.value == "") {
		this.value = this.defaultValue;
	}
}