


// from http://www.dustindiaz.com/ajax-contact-form/
function addEvent(elm, evType, fn, useCapture) {
	if (elm.addEventListener) { 
		elm.addEventListener(evType, fn, useCapture); 
		return true; 
	}
	else if (elm.attachEvent) { 
		var r = elm.attachEvent('on' + evType, fn); 
		// EventCache.add(elm, evType, fn);
		return r; 
	}
	else {
		elm['on' + evType] = fn;
	}
}

function init() {
	var f = $('fKontakt');
	addEvent(f, 'submit', validateFields, false);
	f.onsubmit = function() { return false; }
	$('wiadomosc').focus();
}

function validateFields() {
	$('wiadomosc').style.background = 'none';
	$('imie_nazwisko').style.background = 'none';
	$('e-mail').style.background = 'none';
	
	var whiteSpace = /^[\s]+$/;
	if ( $F('wiadomosc') == '' || whiteSpace.test($F('wiadomosc')) ) {
		$('wiadomosc').style.background = '#D1F1FF';
		$('wiadomosc').focus();
	}
	else if ( $F('imie_nazwisko') == '' || whiteSpace.test($F('imie_nazwisko')) ) {
		$('imie_nazwisko').style.background = '#D1F1FF';
		$('imie_nazwisko').focus();
	}
	else if ( ($F('e-mail') == '' || whiteSpace.test($F('e-mail'))) &&
			  ($F('telefon') == '' || whiteSpace.test($F('telefon'))) ) {
		$('e-mail').style.background = '#D1F1FF';
		$('e-mail').focus();
	}
	else {
		send();
	}
}
function send() {
	$('sending').innerHTML = '<img src="../../indicator.gif" alt="" /> Wysyłanie wiadomości...';
	Effect.SlideDown('sending');
	
	var url = "../../kontakt.php";
	
	var myAjax = 
		new Ajax.Request(
			url,
			{
				method: 'post',
				postBody: Form.serialize($('fKontakt')),
				onComplete: showResponse
			}
		);
}

function showResponse(originalRequest)
{
	if (originalRequest.responseText == '1') {
		$('sending').innerHTML = 'Wiadomość wysłana. Dziękujemy!';
	} else
	{
		$('sending').innerHTML = 'Wystąpił błąd podczas wysyłania wiadomości.<br/>Prosimy o kontakt telefoniczny.';
	}
	
	new Effect.Highlight('sending');
}
