/*
Created 09/27/09										
Questions/Comments: jorenrapini@gmail.com						
COPYRIGHT NOTICE		
Copyright 2009 Joren Rapini
*/

	

$(document).ready(function(){
	// Place ID's of all required fields here.
	required = ["fname","lname", "email", "phone","amount","attorney","state","frmChk1"];
	// If using an ID other than #email or #error then replace it here
	email = $(".email");
	errornotice = $("#error");
	amount= $(".amount");
	// The text to show up within a field when it is incorrect
	emptyerror = ""; //Please fill out this field.
	emailerror = ""; //Please enter a valid e-mail.

	$("#theform").submit(function(){	
		//Validate required fields
		
		for (i=0;i<required.length;i++) {
			var input = $('.'+required[i]);
			if ((input.val() == "") || (input.val() == emptyerror)) {
				input.addClass("needsfilled");
				input.val(emptyerror);
				errornotice.fadeIn(750);
			} else {
				input.removeClass("needsfilled");
			}
		}
		// Example of checkbox validation 
		/*
		var checked=$(".frmChk1:checked").length;
		if(checked==0){
		alert('Please accept the terms & condition before submitting the form');
		return false;
		} */
		
		// Validate the e-mail.
		if (!/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(email.val())) {
			email.addClass("needsfilled");
			email.val(emailerror);
		}
		
		// Valid Amount noumer(integer) with 2 decimal poing Valid number = 1,2563,253.00 etc
		if(!/(?!^0*$)(?!^0*\.0*$)^\d{1,5}(\.\d{1,2})?$/.test(amount.val())){
			amount.addClass("needsfilled");
			amount.val(emailerror);
		}

		//if any inputs on the page have the class 'needsfilled' the form will not submit
		if ($(":input").hasClass("needsfilled")) {
			return false;
		} else {
			errornotice.hide();
			return true;
		}
		
		
	});
	
	// Clears any fields in the form when the user clicks on them
	$(":input").focus(function(){		
	   if ($(this).hasClass("needsfilled") ) {
			$(this).val("");
			$(this).removeClass("needsfilled");
	   }
	});
	
	
});	
