/*
 * Eye Entertainment Form Checker
 * Version: 1.3
 * Last modified: 29-10-2009 01:44 UTC
 * Author: Jesse Linthorst
 * Notes: This scripts depends on Mootools More: Chain.Wait
 * Function: Checks and sends data using AJAX
 */

/*
 * The following lines let the script init

   <script type="text/javascript">
	window.addEvent('domready', function() {
		formCheck();
	});
	</script>
	
 * 
 */

/*
 * This is the formCheck is the core function of the form checking.
 */

function formCheck() {

	hideCaptcha();

	// Global vars
	var nameInput 	= $('name');
	var emailInput 	= $('email');
	var contactFrm	= $('contactFrm');
	var errorName	= nameInput.getPrevious('div');
	var errorEmail	= emailInput.getPrevious('div');
	var errorArray	= new Array(true, true);
	var submitBtn 	= $$('input.submit');
	
	/*
	 * nameInput Event
	 * Focus: Sets the state of the submit button to disabled
	 * Blur: Checks if user input is correct, enables submit button
	 */
	
	
	nameInput.addEvents({
		'focus' : function() {
			submitBtn.setProperty('disabled', true);
			
		},
		'blur': function(){
			errorArray[0] = nameInput.get('value').length <= 1 ? generateError( nameInput, 'Geen naam opgegeven.' ) : removeError( nameInput );
		}
	});
	
	/*
	 * emailInput Event
	 * Focus: Sets the state of the submit button to disabled
	 * Blur: Checks if user input is correct, enables submit button
	 */
	
	emailInput.addEvents({
		'focus' : function() {
			submitBtn.setProperty('disabled', true);
		},
		'blur': function(){
			var pattern = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
			
			errorArray[1] = !emailInput.get('value').test(pattern) ? generateError( emailInput, 'Incorrect e-mail adres.' ) : removeError( emailInput );
		}
	});
	
	/*
	 * submitBtn Event
	 * Checks if no errors found in userinput enables and disables submit button.
	 * Sends JSON data to sendmail.ajax.php and calls the function generateSendMsg.
	 */
	
	submitBtn.addEvents({
		'mouseenter': function(){
			if ( errorArray[0] || errorArray[1] || errorArray[2] ) {
				submitBtn.setProperty('disabled', true);
			}
			else {
				submitBtn.setProperty('disabled', false);
			}
		},
		'click': function(){
			
			var mailData = JSON.encode({name: nameInput.value, email: emailInput.value, phone: $('phone').value, uri: $('url').value, msg: $('message').value, copy: $('copy').value});
			var requestHandler = new Request.JSON({
				url: 'http://eyeentertainment.nl/system/controllers/sendmail.ajax.php', 
				method: 'post',
				onSuccess: function(mailInfo) {
					submitBtn.setProperty('disabled', true);
					generateSendMsg( mailInfo.status, mailInfo.msg );
					errorArray[0] = mailInfo.status ? true : false;
				}
			}).send("data="+ mailData);

			return false;
			
			
		}
	});

}

/*
 * Function that generates the div errorBox
 */

function generateError( inputField, errorMsg ) {
	inputField.setStyle('background-color', '#ffcccc');
	
	if ( inputField.getPrevious('div') == null ) {
		var errorBox = new Element('div', {
			'class': 'errorBox',
			'opacity': '0'
		});
		errorBox.appendText(errorMsg);
		inputField.getParent().grab(errorBox, 'top');
	}
	else
	{
		var errorBox = inputField.getPrevious('div');
	}
	
	errorBox.fade('in');
	return true;
}

/*
 * Function that 'removes' the div errorBox
 */

function removeError(inputField){

	var submitBtn = $$('input.submit');
	if (inputField.getPrevious('div') != null) {
		inputField.setStyle('background-color');
		inputField.getPrevious('div').fade('out');
	}
	submitBtn.setProperty('disabled', false);
	return false;
}

/*
 * Function that popups a message from the ajax generated source
 * Status: True or False
 * Msg: Message from phpMailer
 */

function generateSendMsg(status, msg) {
	
	var topColor = status ? '#4d79a7' : '#b33232';
	var topMessage = status ? 'Bericht is verstuurd.' : 'Bericht kon niet worden verstuurd.';
	var waitTime = status ? '2500' : '5000';
	
	var messageBoxElmDiv = new Element('div').set('styles', {
		'position' : 'absolute',
		'id' : 'messagebox',
		'top' : '50%',
		'left' : '50%',
		'margin' : '0px 0 0 -250px',
		'opacity' : '.0',
		'z-index' : '3',
		'background' : '#f9f9f9',
		'width' : '500px',
		'height' : 'auto',
		'border' : '1px solid #000',
		'font-size' : '11px'
	});
	
	var messageBoxElmSpan = new Element('span').set('styles', {
		'display' : 'block',
		'padding': '3px 0 0 6px',
		'width' : '494px',
		'height' : '20px',
		'background' : topColor,
		'color' : '#fff',
		'font-size' : '11px',
		'font-weight' : 'bold'
	});
	
	messageBoxElmSpan.appendText(topMessage);
	
	var messageBoxElmP = new Element('p').setStyle('padding', '5px');
	messageBoxElmP.appendText(msg);
	
	messageBoxElmDiv.grab(messageBoxElmSpan);
	messageBoxElmDiv.grab(messageBoxElmP);
	
	$(document.body).grab(messageBoxElmDiv, 'top');
	
	new Fx.Tween(messageBoxElmDiv, {
		link: 'chain',
		property: 'opacity',
		duration: 500
	}).start(1).wait(waitTime).start(0);
	
}

function hideCaptcha() {
	
	$('captcha').getParent().setStyle('display', 'none');
}


