// JavaScript Document
$(document).ready(function() {
						   
	$(window).load(function() {
		fixHeights();
	});

							   
	$('.clearField').clearField({
		blurClass: 'listBlurred',
		activeClass: 'listActive'
	});
	
	var form = $("#form-mail-list");
	var name = $("#mail-list-name");
	var email = $("#mail-list-email");
	var city = $("#mail-list-city");
	var state = $("#mail-list-state");
	var country = $("#mail-list-country");	
	
	form.submit(function(){
		if( validateName() && validateEmail() ) {
			var cityval;
			var stateval;
			var countryval;
			if(city.val()==city.attr('rel')){
				cityval = 'No City Entered';
			} else {
				cityval = city.val();
			}
			if(state.val()==state.attr('rel')){
				stateval = 'No State Entered';
			} else {
				stateval = state.val();
			}
			if(country.val()==country.attr('rel')){
				countryval = 'No Country Entered';
			} else {
				countryval = country.val();
			}
			var dataString = 'name='+ name.val() + '&email=' + email.val() + '&city=' + cityval + '&state=' + stateval + '&country=' + countryval ;
			var oldheight = $('#mail-list-wrapper').height();
			//alert (dataString);return false;
			$.ajax({
				type: "POST",
				url: "/data/process.php",
				data: dataString,
				success: function() {
					$('#mail-list-wrapper').hide()
						.html("Thank you for joining the list!")
						.height(oldheight)
						.fadeIn(1500);
				}
			});
			return false;
		} else {
			return false;
		}
	});
	
	function validateEmail(){
		//testing regular expression
		var a = email.val();
		var filter = /^[a-zA-Z0-9]+[a-zA-Z0-9_.-]+[a-zA-Z0-9_-]+@[a-zA-Z0-9]+[a-zA-Z0-9.-]+[a-zA-Z0-9]+.[a-z]{2,4}$/;
		//if it's valid email
		if(filter.test(a)){
			email.removeClass("form-error");
			return true;
		}
		//if it's NOT valid
		else{
			email.addClass("form-error");
			alert('Please enter a valid Email');
			return false;
		}
	}
	
	function validateName(){
		//if it's NOT valid
		if(name.val()==name.attr('rel')){
			name.addClass("form-error");
			alert('Please enter your Name');
			return false;
		}
		//if it's valid
		else{
			name.removeClass("form-error");
			return true;
		}
	}
	
	function fixHeights() {
		var hcontent = $('#content').height();
		var hside = $('#side-wrapper').height();
		
		if (hcontent < hside) {
			$('#content').height(hside+6);
		}
	}
	
});

