$(document).ready(function(){
	$.preloadCssImages();
  
	//	SIDEBAR FORM 1 - FREE CASE EVALUATION
  
	$(".sidebar-form-1 [name=email]").focus(function(){
		var currentValue = $(this).val().toLowerCase();
		if(currentValue == 'your email address')
			$(this).val('');
	});
	$(".sidebar-form-1 [name=email]").blur(function(){
		var currentValue = $(this).val();
		if(currentValue == '')
			$(this).val('Your Email Address');
	});
	
	$("form#sidebar-form-1").submit(function() {
		var validated = 1;
			
		if($.trim($("#email").val()) == '' || 
			$.trim($("#email").val().toLowerCase()) == 'your email address') {
			$("#email").addClass("error");
			validated = 0;
		}
		else {
			$("#email").removeClass("error");
		}
		
		if($.trim($("#email").val()) != '') {
			if(isValidEmailAddress($.trim($("#email").val()))) { 
				$("#email").removeClass("error");
			} else { 
				$("#email").addClass("error");
				validated = 0;
			}
		}
			
		if($.trim($("#description").val()) == '') {
			$("#description").addClass("error");
			validated = 0;
		}
		else {
			$("#description").removeClass("error");
		}
		
		//	NO ERROR
		if(validated == 1) {
			
			var inputs = [];
			
			var valueEmail = $("#email").val();
			var valueDescription = $("#description").val();
			
			inputs.push('email=' + escape(valueEmail));
			inputs.push('description=' + escape(valueDescription));
			
			$.ajax({
				type: "POST",
				url: "/demo/wp/wp-content/themes/our-theme/php/ajax.php",
				data: inputs.join('&'),
				success: function(data){
					if(data.length > 0 || data != '') {
						$('#sidebar-form-1').hide();
						$("#sidebar-form-1-result").fadeIn(200);
						$('#sidebar-form-1-result').html(data);
					}
					else { 
						$('#sidebar-form-1').hide();
						$("#sidebar-form-1-result").fadeIn(200).addClass('required');
						$('#sidebar-form-1-result').html('<p>Your form cannot be submitted. We apologize for the incovenience. Please try again later.</p>');
					}
				}
			});
			
			return false;
		}
		
		return false;
	});
	
	$( "input.datePicker" ).datepicker({
		changeMonth: true,
		changeYear: true,
		minDate: "-75y", 
		maxDate: "+1m +10d"
	});
	$('input.datePicker').attr("readonly", true);
}); 
$(window).load(function() {
	
	//	LIVE CHAT
	
	$('.btn-live-chat-2').fancybox({
		'width'			:	450,
		'height'		: 	400,
		'type'			:	'iframe',
		'href'			:	'http://www.websitealive8.com/627/rRouter.asp?groupid=627&amp;websiteid=0&amp;departmentid=702&amp;dl='+escape(document.location.href)
	});
	
	$('.btn-live-chat-1').fancybox({
		'width'			:	450,
		'height'		: 	400,
		'type'			:	'iframe',
		'href'			:	'http://www.websitealive8.com/627/rRouter.asp?groupid=627&amp;departmentid=702&amp;websiteid=0&amp;loginname=Visitor&amp;loginquestion=' + document.getElementById('wsa_loginquestion').value
		
	});
	
	$('.email-broken-links').fancybox({
		'width'			:	500,
		'height'		: 	450,
		'type'			:	'iframe',
		'href'			:	'http://www.ogletreeabbott.com/email-broken-links.html'
	});
	
	$('.link-exchange').fancybox({
		'width'			:	500,
		'height'		: 	450,
		'type'			:	'iframe',
		'href'			:	'http://www.ogletreeabbott.com/link-exchange.html'
	});
});


function isValidEmailAddress(emailAddress) {
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	return pattern.test(emailAddress);
}
