// JavaScript Document


jQuery.Validate = {

	messages : {
		'required' : "$ is required",
		'email' : "Email address invalied"
	},
	
	required : function(val){
		return $.trim(val) == "" ? false : true;
	},
	
	email: function(val){
		var regexp = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
		return regexp.test(val);
	}
}

$(document).ready( function(){
							
	$('input[placeholder]').each( function(){
		$(this).focus( function(){
			if( this.value == $(this).attr('placeholder') ) this.value = '';						
		});
		$(this).blur( function(){
			if( jQuery.trim(this.value) == '' ) this.value = $(this).attr('placeholder');						
		});
		if( jQuery.trim(this.value) == '' ){
			this.value = $(this).attr('placeholder');			
		}
	});
	
	$('textarea[placeholder]').each( function(){
		$(this).focus( function(){
			if( this.value == $(this).attr('placeholder') ) this.value = '';						
		});
		$(this).blur( function(){
			if( jQuery.trim(this.value) == '' ) this.value = $(this).attr('placeholder');						
		});
		if( jQuery.trim(this.value) == '' ){
			this.value = $(this).attr('placeholder');			
		}
	});
	
	$('form').submit( function(e){
		e.preventDefault();
		$('.validationError').remove();
		var passed = true;
		$(this).find('input[validate], textarea[validate]').each( function(){
			$(this).removeClass('fieldWithErrors');
			var val;
			val = this.value == $(this).attr('placeholder') ? '' : this.value;

			if( jQuery.Validate[$(this).attr('validate')](val) === false ){
				passed = false;
				var msg = jQuery.Validate.messages[$(this).attr('validate')];
				var name = $(this).attr('name').replace('_', ' ');
				msg = msg.replace('$', name);
				var html = '<p class="validationError">'+ msg +'</p>';
				$(this).before(html);
				$(this).addClass('fieldWithErrors');
			}
		});
		if( passed ) this.submit();
	});
	
								
							
});



function footer(){
	var h = $(window).height() - $('#header').height() - $('#footer').height() - 10;
	if ( $('#content').height() < h ) $('#content').height(h);
}


$(window).load( footer);
$(window).resize( footer );


