/* prayam.com */
var DOMEvents = {

	// Border hovering
	'.textfield' : function(input) {
		input.onfocus = function() { this.parentNode.className = "kontakt_form hovered"; }
		input.onblur = function() { this.parentNode.className = "kontakt_form"; }
	},
	// Send button
	'input#send' : function(input) {
		input.onclick = function() { ContactForm.Send(); }
	}
}

/* ContactForm scripting */
var ContactForm = {

	Send : function() {

		if(!this.working) {

			// working now
			this.working = true;

			// validate first
			if( $F('name') && $F('email') && $F('inquiry') ) { this.AjaxCall(); }

			// ERROR: not all fields
			else { MessageBox.Bad($F('lang_contact_not_all')); }
		}
	},

	AjaxCall : function() {

		url = "controllers/kontakt_ajax.php";
		fields = new Array('name', 'email', 'subject', 'inquiry');

		pars = "";
		firstField = true;
		fields.each( function(field) {

			if(!firstField) { pars += "&"; }
			pars += field + "=" + encodeURIComponent($F(field));
			if(firstField) { firstField = false; }
		});

		MessageBox.Loading($F('lang_contact_wait'));

		theCall = new Ajax.Request(
			url,
			{
				method: 'post',
				parameters: pars,
				onFailure: this.AjaxFail,
				onComplete: this.AjaxCallback
			});
	},

	AjaxFail : function(request) {

		MessageBox.Bad($F('lang_contact_error'));
	},

	AjaxCallback : function(request) {

		if(request.responseText.substring(0,5) == 'error') {
			if(request.responseText == "error_bademail") { MessageBox.Bad($F('lang_contact_bad_email')); }
			else { MessageBox.Bad($F('lang_contact_try_again')); }
		}
		else {

			MessageBox.Good($F('lang_contact_sent'));
			$('subject').value = "";
			$('inquiry').value = "";
		}
	}

}

/* MessageBox scripting */
var MessageBox = {

	Loading : function(message) { this.Update('loading', $F('lang_contact_img_alt_sending'), message); },

	Good : function(message) { this.Update('ok', $F('lang_contact_img_alt_ok'), message, 1450); },

	Bad : function(message) { this.Update('error', $F('lang_contact_img_alt_error'), message, 2150); },

	Update : function(image,alt,text,delay) {

		// Kinda global setting
		directory = 'images/layout/MessageBox/';

		// Put correct HTML in the message box
		$('MessageBox').innerHTML = '<p><img src="' + directory + image + '.gif" alt="' + alt + '" /><br />' + text + '</p>';

		// Check if we need to show the message box or is it already visible
		if($('MessageBox').style.display == 'none') { new Effect.Appear('MessageBox'); }

		// Hide the messagebox if any delay is set
		if(delay) { setTimeout("Effect.DropOut('MessageBox'); ContactForm.working = false;", delay); }
	}
}

/* Runtime */
Behaviour.register(DOMEvents);

