/*****************************************************************************************
****	By: James Gibson - Liquifusion Studios, Inc.
****	For: The reservoir website
****	
****	:O)
*/

(function(){
	
	window.com = window.com || {};
	liq = com.liquifusion = com.liquifusion || {};
	
	liq.InstructionTexts = {
		
		texts: {
			// list out all instruction texts here
			Contact0FirstName: 'First name',
			Contact0LastName: 'Last name',
			Contact0Email: 'Email address'
		},
		
		find: function(value) {
			for (var text in this.texts) {
				if (value == this.texts[text]) {
					return true;	
				}
			}
			return false;	
		}
	}	  
})();

$(document).ready(function() {
	
	var liq = com.liquifusion,
		IT;
		
	IT = liq.InstructionTexts;
	
	$('input.instructions, textarea.instructions').focus(function(event) {
							   
		if (IT.find($(this).val())) {
			
			$(this).val('');	
			$(this).removeClass('instructions');	
			
		}
	}).blur(function(event) {
		
		if ($(this).val() == '') {
			
			$(this).addClass('instructions');
			$(this).val(eval("IT.texts." + $(this).attr('id')));
		}
	}).each(function(i) {
		
		if ($(this).val() != eval("IT.texts." + $(this).attr('id'))) {
			
			$(this).removeClass('instructions');
		}
	});
	
	
	$('#report-actor').click(function(event) {
		
		event.preventDefault();
		
		if ($('#report-form').is(':hidden')) {
			
			var reportActor = $(this);
			
			$('#report-form')
				.fadeIn(350)
				.oneTime(500, function() {
					reportActor.addClass('open');							   
			});
		} else {
			
			$(this).removeClass('open');
			$('#report-form').hide();
		}
		
	});	
	

	$.validator.addMethod('state', function (value) {
		return /^([a-z]|[A-Z]){2}$/.test(value);
	}, 'Please enter a valid two-character state code.');
	
	$.validator.addMethod('phone', function (value, el, params) {
		return this.optional(el) || /^[01]?[- .]?\(?[2-9]\d{2}\)?[- .]?\d{3}[- .]?\d{4}$/.test(value);
	}, 'Please enter a valid US or Canadian phone number.');
	
	// add new validation methods
	$.validator.addMethod('instructions', function (value) {
		return IT.find(value) == false;
	}, 'This field is required');
	
	// add custom validation rules
	$.validator.addClassRules({
		state: {
			required: true,
			state: true
		},
		postalCode: {
			required: true,
			postalCode: true
		},
		instructions: {
			required: true,
			instructions:true
		}	
	});

	$('form.free-report').validate({
		debug: true,
		errorClass: "invalid",
		messages: {
			Contact0FirstName:"",
			Contact0LastName:"",
			Contact0Email:""
		},
		submitHandler: function(form) {
			$(this).find('[type=submit]').addClass('disabled');
			$(this).find('[type=submit]').attr("disabled", true);
			form.submit();
		}
    });
});






