/*

pirean.contactForm.js
Created prior to 01.11.2010

Description:
	Controls the more flash elements of the contact form: creates the popup modal, validates the fields and causes sliding labels over the fields.

Requirements:
	jQuery
	liveValidation
	reCaptcha

Changelog:
	2011.04.24 - Added a click through trick for certain labels (BSL)
	2011.04.14 - Reset now triggers at refresh, and added captcha (BSL)
	2011.02.08 - Adding a fix to the RESET button, clearing the whole form (BSL)
	2011.02.02 - Added the sliding contact field js (BSL)
	2010.11.01 - Moved the livevalidation data into this file from _include/java-includes (BSL)
	2010.11.01 - Modified to use the new livevalidation.js (BSL)
	2010.11.01 - Started commenting/documenting external javascript files by Ben Leveritt (BSL)
	2010.11.01 - Created by Simon Hagger (SH)
	
*/

$(document).ready(function() {

	// The popup modal dialogue box stuff goes here - SH
	var theOverlay = $(".modalInput").overlay({
		// some mask tweaks suitable for modal dialogs - SH
		top: "center",
		left: "center",
		mask: {
			color: "#000000",
			loadSpeed: 200,
			opacity: 0.6
		},

		closeOnClick: true
	});

	//Modified to use javavalidate - BSL[01.11.10]

	/*var v = $("#frmLead").validate({
	focusInvalid: true ,
	errorElement: "div"
	});
	$("#frmLead #reset").click(function () { 
	v.resetForm();
	});*/

	//Moved the liveValidation data into this file from _include/java-includes - BSL[01.11.2010]

	var dom_salutation = document.getElementById('salutation');
	var node_salutation = document.getElementById('required_field_salutation');
	var val_salutation = new LiveValidation(dom_salutation, {onlyOnSubmit: true, insertAfterWhatNode: node_salutation, validMessage: " "});
	val_salutation.add(Validate.Presence, {failureMessage: " ",	validMessage: " "});

	var dom_first_name = document.getElementById('first_name');
	var node_first_name = document.getElementById('required_field_first_name');
	var val_first_name = new LiveValidation(dom_first_name, {onlyOnSubmit: true, insertAfterWhatNode: node_first_name, validMessage: " "});
	val_first_name.add(Validate.Presence, {failureMessage: " ", validMessage: " "});

	var dom_last_name = document.getElementById('last_name');
	var node_last_name = document.getElementById('required_field_last_name');
	var val_last_name = new LiveValidation(dom_last_name, {onlyOnSubmit: true, insertAfterWhatNode: node_last_name,	validMessage: " "});
	val_last_name.add(Validate.Presence, {failureMessage: " "});

	var dom_email = document.getElementById('email');
	var node_email = document.getElementById('required_field_email');
	var val_email = new LiveValidation(dom_email, {onlyOnSubmit: true,	insertAfterWhatNode	: node_email, validMessage : " "});
	val_email.add(Validate.Presence, {failureMessage: " "});
	val_email.add(Validate.Email, {failureMessage: " "});

	var dom_enquiry = document.getElementById('description');
	var node_enquiry = document.getElementById('required_field_enquiry');
	var val_enquiry = new LiveValidation(dom_enquiry, {onlyOnSubmit: true, insertAfterWhatNode: node_enquiry, validMessage: " "});
	val_enquiry.add(Validate.Presence, {failureMessage	: " "});
	
	//Captcha system put in place (using Google's ReCaptcha). - BSL[14.04.2011]
	
	Recaptcha.create("6LdANMMSAAAAAI1y122sB_kd9e4Q1ZN-gOVnTJud", 'formcaptcha', {
	theme: "custom",
	custom_theme_widget: 'formcaptcha',
	callback: createCaptchaValidation});
	
	function createCaptchaValidation() {
		var dom_recaptcha = document.getElementById('recaptcha_response_field');
		var val_recaptcha = new LiveValidation(dom_recaptcha, {onlyOnSubmit: true});
		val_recaptcha.add(Validate.Presence);
		val_recaptcha.add(Validate.Custom, {failure_message: "", against: function(value,args) {
			post_response = $.post('/js/pirean.recaptchatest.php',
			{
				recaptcha_challenge_field: 	$('#recaptcha_challenge_field').val(),
				recaptcha_response_field: 	$('#recaptcha_response_field').val()
			}, function(data){
				if (data == "1") {
					val_recaptcha.disable();
					$('#formcaptcha').removeClass("LV_invalid_field");
					$('#formcaptcha').children().fadeTo(300, 0.1);
					$('#recaptcha_challenge_field').val("...");
					$('#formcaptcha').prepend('<div id="captcha-complete"><img src="/images/captcha-complete-46x46.png"></div>');
					$('#submit-button').click();
					return true;
				} else {
					$('#formcaptcha').addClass("LV_invalid_field");
					Recaptcha.reload();
					return false;
				}
			});
		}});
	};
	
	//Putting all js relating to the contact form in this file. - BSL[01.11.2010]	

	$('form.frmLead input.text').bind('blur', function() {	
		if( $(this).val() == '') {
	    	$(this).prev().animate({'left': 5, 'opacity': 1}, 700);
	    }
	}).bind('focus', function() { 
		$(this).prev().animate({'left':$(this).width() - $(this).prev().width(),'opacity':0.3}, 700);
	});
	
	$('form.frmLead select').bind('blur', function() {
		if( $(this).val() == '') {
			$(this).prev().animate({'opacity': 1}, 500);
	    }
	}).bind('focus', function() { 
	    	$(this).prev().animate({'opacity': 0}, 300);
	});
	$('form.frmLead textarea').bind('blur', function() {
		if( $(this).val() == '') {
	    	$(this).prev().animate({'opacity': 1}, 500);
	    }
	}).bind('focus', function() { 
    	$(this).prev().animate({'opacity': 0}, 300);
	});
	
	$('form.frmLead #recaptcha_response_field').bind('blur', function(){
		if( $(this).val() == '') {
			$('.recaptcha_label').animate({'opacity': 1},500);
		}
	}).bind('focus', function() {
		$('.recaptcha_label').animate({'opacity': 0}, 300);
	});
	
	//Have to use a click through trick to hide labels in the way. - BSL[24.04.2011]
	
	$('#label_salutation').click(function(){
		$('#salutation').focus();
	});
	$('#label_main_software_interest').click(function(){
		$('#software').focus();
	});
	$('#label_main_services_interest').click(function(){
		$('#services').focus();
	});
	$('#label_description').click(function(){
		$('#description').focus();
	});
	$('.recaptcha_label').click(function(){
		$('form.frmLead #recaptcha_response_field').focus();
	});
	
	//Adding a fix to the RESET button, clearing the whole form - BSL[08.01.2010]
	
	reset_form(); //Reset form on refresh. Slow down repeat requests. - BSL[14.04.2011]
	
	$('form.frmLead input[type="reset"]').click(function(){
		reset_form();
	});
	
	function reset_form() {
		$('.LV_invalid_field').removeClass('LV_invalid_field');
		
		$('form.frmLead input.text').val('');
		$('form.frmLead select').val('');
		$('form.frmLead textarea').val('');
		
		$('form.frmLead input.text').trigger('blur');
		$('form.frmLead select').trigger('blur');
		$('form.frmLead textarea').trigger('blur');
	};
});

