/*
 *
 * Make sure your submit button uses: onclick="validateForm();"
 *
 */

//edit these
var filter = ":not(:submit)"; //which input types to filter out. always include ":not(:submit)"

// do not edit below
var errors = false;
var selected = false;

$(function(){

	$('input' + filter + ', textarea').each(function(){
		$(this).val($(this).attr('placeholder-text'));
	});
	
	$('input' + filter + ', textarea').focus(function(){
		if($(this).val()==$(this).attr('placeholder-text')){
			$(this).val('');
		}
	});
	$('input' + filter + ', textarea').blur(function(){
		if($((this)).val()==''){
			$((this)).val($(this).attr('placeholder-text'));
		}
		$('.error-tip').fadeOut('fast', function(){
			$(this).remove();
		});
	});

});
 
function validateForm(){
	
	$('form').submit(function(){
		
		$('.error-tip').remove();
		
		errors = false;
		selected = false;
		
		$('input' + filter + ', textarea').each(function(){
			if($(this).val()==$(this).attr('placeholder-text')){
				$(this).val('');
			}
			
			if($(this).attr('required-field')=="required" && $(this).val()==''){
				errors = true;
				
				$(this).after('<span class="error-tip" style="display:none;">This field is required</span>');
				$('.error-tip').fadeIn('fast');
				
				if(selected == false){
					$(this).focus();
					selected = true;
					return false
				}
			} else if($(this).val()=='') {
				$(this).val($(this).attr('placeholder-text'));
			}
			
		});
		
		if(errors==false){
			
			$('input' + filter + ', textarea').each(function(){
				if($(this).val()==$(this).attr('placeholder-text')){
					$(this).val('');
				}
			});
			
			$('form').submit();
		}
		
		return false;
		
	});
	
	return false;
	
}

function clearError() {
	$('.error-tip').fadeOut('fast', function(){
		$(this).remove();
	});
}
