
/**
 * Form handler for the contact us form displayed for campus pages.
 * 
 * Author: gbellucci
 * Date: 12/4/2009
 * Ver: 1.0
 * Copyright (c) 2009 - ECPI College of Technology
 */
$(document).ready(function() {
						   
	// setup the form validation engine
	$("#cform").validationEngine();

		// education level selection
	$("#education_level").change(function() {
		var selection = $(this).val();
		var isHidden = $('#school-data').is(':hidden');
		if( selection != "6" ) {
			if(selection == '1') { // college graduate
				$("#label-school").html("Name of College:");
				$("#use-school-label").val("Name of College:");
				$("#label-yr").html("Year graduated:");
				$("#use-yr-label").val("Year graduated:");
			}
			else if(selection == '2') { //attended college
				$("#label-school").html("Name of College:");
				$("#use-school-label").val("Name of College:");
				$("#label-yr").html("Years:");
				$("#use-yr-label").val("Years:");
			}
			else if(selection == '3') { // high school grad 
				$("#label-school").html("Name of High School:");
				$("#use-school-label").val("Name of High School:");
				$("#label-yr").html("Year graduated:");
				$("#use-yr-label").val("Year graduated:");
			}
			else if(selection == '4') { // has GED 
				$("#label-school").html("Name of High School:");
				$("#use-school-label").val("Name of High School:");
				$("#label-yr").html("GED Year:");
				$("#use-yr-label").val("GED Year:");
			}
			else if(selection == '5') { // attending high school 
				$("#label-school").html("Name of High School:");
				$("#use-school-label").val("Name of High School:");
				$("#label-yr").html("Graduating Year:");
				$("#use-yr-label").val("Graduating Year:");
			}
			if(isHidden) {
				$('#school-data').fadeIn("slow");
			}
		}
		else {
			if(!isHidden) {
				$('#school-data').fadeOut("slow");
			}
		}
	});

	// Clicking in the form clears any error fields displayed
	$("input:text, #form-wrapper, body.contactpage")
		.click( function() {
			$("div.formError").fadeOut("slow", function() {
			$(this).remove();
		});
	});
	
	// input field focus/blur
	$("input:text, textarea")
		.focus(function() {
			$(this).addClass('active');
		})
		.blur( function() {
			$(this).removeClass('active');
		});

	$("#resetForm").click(function() {
		// clear the form
		$("#cform").clearForm();
	});
	
	// Window unload
	$(window).unload(function() {
		if(chkFormSettings("cform")) {
			s.formList="cform";
			s.trackFormList = true;
			s_doPlugins(s);
		}
	});
	
	$("input:checkbox").click( function() { 
		var id = $(this).attr("id")+"cell";
		var par = $(this).parent();
		$(par).attr("id", id);
		if( $(this).attr("checked") == true) {
			$("#"+id).addClass("selected");
		}
		else {
			$("#"+id).removeClass("selected");
		}
	});

	
	$("#cform").submit(function() {
		$("#cform").validationEngine({promptPosition: "topLeft"});
		var asd_cb = (-1), bsd_cb = (-1) ,retcode = false;
		var has_asd = ($("input#has_as_checkboxes").val() == 0) ? 0: 1;
		var has_bsd = ($("input#has_bs_checkboxes").val() == 0) ? 0: 1;
		
		if( has_asd ) {
			asd_cb = $("input:checkbox.ad-checkbox:checked").length; // returns any checked as degree boxes
		}
		if( has_bsd ) {
			bsd_cb = $("input:checkbox.bs-checkbox:checked").length; // returns any checked bs degree boxes
		}
		if( asd_cb != (-1) && bsd_cb != (-1) ) {
			// offers both
			if( !asd_cb && !bsd_cb ) {
				$.validationEngine.buildPrompt('#all_programs',
				"Select at least one program that interests you from any of the degree programs shown.","error");
			}
			else {
				// at least one program was selected
				retcode = true;
			}
		}
		else if( asd_cb != (-1) ) {
			// offers only as degrees
			if( !asd_cb ) {
				$.validationEngine.buildPrompt('#ad-header',
				"Select at least one item from this program group that interests you.","error");
			}
			else {
				// at least one program was selected
				retcode = true;
			}
		}
		else {
			// offers only bs degrees
			if( !bsd_cb ) {
				$.validationEngine.buildPrompt('#bd-header',
				"Select at least one item from this program group that interests you.","error");
			}
			else {
				// at least one program was selected
				retcode = true;
			}
		}
		if( retcode == true ) {
			s.linkTrackVars = 'eVar1,eVar11,eVar12,prop9,eVar9';
			s.events = "event1";
			s.eVar1 = "campuses";
			s.eVar11 = $("#State").val();
			s.eVar12 = $("#Zip").val();
			s.prop9 = $("#campus_name").val();
			s.tl(this, 'o', 'Request Info Submission');
		}
		
		return(retcode);
	});
	
});

/*
 * jQuery add-on for clearing a form
 * Borrowed from Mike Alsup (http://www.learningjquery.com/2007/08/clearing-form-data)
 */
$.fn.clearForm = function() {
	return this.each(function() {
		var type = this.type, tag = this.tagName.toLowerCase();
		if (tag == 'form')
			return $(':input', this).clearForm();
		if (type == 'text' || type == 'password' || tag == 'textarea')
			this.value = '';
		else if (type == 'checkbox' || type == 'radio')
			this.checked = false;
		else if (tag == 'select')
			this.selectedIndex = -1;
	});
};

/**
 * Determine if any input fields in a form contain data.
 * Returns 1 if any field contains data entered by the user otherwise 0.
 * @param form_id - the form id of the form to check
 * @return boolean
 */
function chkFormSettings(form_id) {
	var b = 0;
	var count = $("#"+form_id+" input.required, #"+form_id+" select.required").each( function() {
		var type = this.type, tag = this.tagName.toLowerCase();
		if( (tag == 'input' && type == 'text') || tag == 'select' ) {
			b += (this.value == '' ? 1: 0);
		}
	});
	return(b != count.length);
}