function initValidator(fID) {
	// Grab the ID of the form passed and extend it as a jQuery enabled object
	var vForm = $('#'+fID);
	// Find the required fields for this form
	var vRequired = vForm.find("input[name='required']").val().split(",");
	var valid = true;
	// Do an initial validation of the whole form
	checkAllValid(vForm);	
	vForm.submit(function(){
		return checkAllValid(vForm);
	})

	// Loop through each required field specified
	for(c=0;c<vRequired.length;c++) {

		// Get the field itself and extend to jQuery
		var field = $('#id_'+vRequired[c]);

		// Get the label for the field and add an asteriks to indicate it as required
		var label = $('#for_'+vRequired[c]);

		// Check case for the type of field we are validating, this is only here to
		// add the possibility of validating checkboxes (not included in this validator)
		if (field.is(":input")) {

			// Add a validation for the field when the it looses focus
			field.blur(function(){

				var field = $(this);
				var fName = field.attr('name');
				var fClass = field.attr('class');

				// Get the message the corresponds to the field name
				var message = $("input[name='message_" + fName + "']").val();

				var valid = true;

				// Check if the field is an email field and validate for proper formatting
				if (fName.indexOf('email') != -1) {
					if (!/^\w+([\+\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(field.val())) {
						valid = false;
					}
				}
				
				// Check if the field is number only  and validate 
				if (fClass.indexOf('numberOnly') != -1) {
					if (!/^[0-9.k,]*$/.test(field.val())) {
						valid = false;
					}
				}

				// Check if the field is empty
				if (field.val() == "") {
					valid = false;
				}

				// Process the validation
				processError(fName, message, valid);
			});
		}
	}
}




// This function will process the validation sent from the blur and display an
// error message if it is invalid (or remove an error message if it has been corrected)
function processError(fID,message,valid) {

	if (valid == false) {
		// Validation passed as failed, check to see if an error message exists by
		// looking up the index of the error message that would be assigned to this
		// field. The index will return -1 if it doesn't exist.
		if ($('*').index($('#error_' + fID)[0]) == -1) {
			// Create the error DOM element
			var errorMsg = $(document.createElement('DIV'));
				errorMsg.addClass('error');
				errorMsg.attr('id', 'error_' + fID);
				errorMsg.css('display','none');
				errorMsg.html(message);
			$('#id_'+fID).after(errorMsg);

				errorMsg.slideDown("fast");
			$('#id_'+fID).parents('li').addClass('error');
		}
	} else {
		// Validation passed, remove errors if they exist, we check if they exist
		// by checking the index of the id of the error message, if it exists,
		// then gracefully slide it up and remove it from the DOM, then remove the
		// error class from its parent LI
		if ($('*').index($('#error_' + fID)[0]) != -1) {
			$('#error_'+fID).slideUp("fast",function(){
				$('#error_'+fID).remove();
			})
			//$('#id_'+fID).parents('li').removeClass('error');
		}
		$('#id_'+fID).parents('li').removeClass('error');
	}

	// After validating the field, do a quick check to see if the whole form
	// is valid for submission.
	checkAllValid($('#id_'+fID).parents('form'));
}

// Whole form validator
function checkAllValid(f) {
	// Grab the form and extend it into jQuery
	var vForm = $(f);

	// Get the required fields from the <input name="required"> tag to parse
	// through and validate against
	var vRequired = vForm.find("input[name='required']").val().split(",");
	var allValid = true;
	formID = $(f).attr('id');
	
	if(formID=='form_advertiser'){
		if(checkBoxValidate('#li_objective')==false){
			allValid = false;
		}
		
		if(checkBoxValidate('#li_sites')==false){
			allValid = false;
		}
		$('input[type=checkbox]').click(function(){
				if(checkBoxValidate('#li_formats')==false){
					allValid = false;
					$('#li_formats .select_all').text("Select All");
					$('#li_formats').addClass("unselected");
				}else{
					allValid = true;					
				}
				
				if(checkBoxValidate('#li_sites')==false){
					allValid = false;
					$('#li_sites .select_all').text("Select All");
					$('#li_sites').addClass("unselected");
				}else{
					allValid = true;
				}
				
				if(checkBoxValidate('#li_locations')==false){
					allValid = false;
					$('#li_locations .select_all').text("Select All");
					$('#li_locations').addClass("unselected");
				}else{
					allValid = true;
				}
				
				
				
				if(checkBoxAllValidate('#li_formats')==true){
					$('#li_formats .select_all').text("Select None");
					$('#li_formats').removeClass("unselected");
				}else{
					$('#li_formats .select_all').text("Select All");
					$('#li_formats').addClass("unselected");
				}
				
				if(checkBoxAllValidate('#li_sites')==true){
					$('#li_sites .select_all').text("Select None");
					$('#li_sites').removeClass("unselected");
				}else{
					$('#li_sites .select_all').text("Select All");
					$('#li_sites').addClass("unselected");
				}
				
				if(checkBoxAllValidate('#li_locations')==true){
					$('#li_locations .select_all').text("Select None");
					$('#li_locations').removeClass("unselected");
				}else{
					$('#li_locations .select_all').text("Select All");
					$('#li_locations').addClass("unselected");
				}
				
				
			});
	}
	
	for(c=0;c<vRequired.length;c++) {
		var field = $("#id_"+vRequired[c]);

		// This is the same basic validation that is done in the individual field
		// validator, but it does not do the error processing (as that would cause
		// an infinite loop)
		if(field.is(":input")) {
			if(field.attr('name').indexOf('email') != -1) {
				if (!/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(field.val())) {
					allValid = false;
				}
			}
			if (field.val() == "") {
				allValid = false;
			}
		}
	}

	if(allValid == false) {
		// Validation has failed somewhere, disable the submit button
		vForm.find('input#id_submit').attr('disabled','disabled');
		vForm.find('input#id_submit').attr('src','http://usarealestateloan.com/images/btn_submit_d.gif');
		return false;
	} else {
		// Validation has passed! Re-enable the submit button for submission
		vForm.find('input#id_submit').removeAttr('disabled');
		vForm.find('input#id_submit').attr('src','http://usarealestateloan.com/images/btn_submit_i.gif');
		return true;
	}
}
