	$(document).ready(function () {
		$('#ppcForm').setupValidation();
	});

	jQuery.fn.setupValidation = function() {
		
		$(".validation_notice").hide();
		
		this.submit(function() { 

			var errors = new Array();
		
			$("input, select, textarea").removeClass("invalid");

			// all other required form elements
			$(".required").each(function() {
				// detect if it's been modified
				if ( $(this).val() == "") {					
					// it hasn't, so show it's related LI
					errors.push(this.id);
					$(this).addClass("invalid");				
				}
				else if ($(this).hasClass('email')) {
					var emailVal = $(this).val().toString();

					if ((emailVal.split("@").length == 2) && (emailVal.split("@")[1].split(".").length > 1 )) {
						// ok
					}
					else { 
						errors.push("email");
						$(this).addClass("invalid");					
					}
				}

			});

			if ( errors.length > 0 ) {
				drawErrors(errors);
				return false;
			} else {
				return true;
			}
		});
	};	
	
	
	drawErrors = function(errors) {
		var notice = "";
		notice += "	<div class=\"validation_inside\">";
		notice += "		<h3>Please correct the following errors:</h3>";
		// notice += "<p>" + errors + "</p>";
		notice += "		<ul class=\"traditional\">";
		
		// lead form
		( jQuery.inArray("first_name", errors) >= 0 ) ? 
			notice += "<li><strong>First Name</strong> is a required field.</li>" : null ; 
		( jQuery.inArray("last_name", errors) >= 0 ) ?	
			notice += "<li><strong>Last Name</strong> is a required field.</li>" : null ; 
		( jQuery.inArray("company_name", errors) >= 0 ) ? 
			notice += "<li><strong>Practice Name</strong> is a required field.</li>" : null ; 
		( jQuery.inArray("numberOfDocs", errors) >= 0 ) ? 
			notice += "<li><strong>Number of physicians in your practice</strong> is a required field.</li>" : null ; 
		( jQuery.inArray("phone", errors) >= 0 ) ? 
			notice += "<li><strong>Phone</strong> is a required field.</li>" : null ; 
		( jQuery.inArray("state", errors) >= 0 ) ? 
			notice += "<li><strong>State</strong> is a required field.</li>" : null ; 
		( jQuery.inArray("job_title", errors) >= 0 ) ? 
			notice += "<li><strong>Job Title</strong> is a required field.</li>" : null ; 
		( jQuery.inArray("email", errors) >= 0 ) ? 
			notice += "<li><strong>Email</strong> is a required field.</li>" : null ; 

		notice += "		</ul>";
		
		notice += '<a href="#" onclick="$(\'.validation_notice\').hide(); return false;">OK</a>';
		
		notice += "	</div>";
		
		$('.validation_notice').html(notice).show();
		
	};	
