
/**
 * Form handler for the first portion the contact us form
 * displayed on the "programs" pages. Included are routines
 * for setting Omniture form tracking events and form content
 * validation.
 * 
 * Author: gbellucci
 * Date: 11/28/2009
 * Ver: 1.0
 * Copyright (c) 2009 - ECPI College of Technology
 */
$(document).ready(function() {

	// setup the form validation engine
	$("#form1of2").validationEngine( {
		inlineValidation : true,
		success : false
	});
	
	// Clicking in the form clears any error fields displayed
	$("input.text, div.form-wrapper")
		.click( function () {
			$("div.formError").fadeOut("slow", function() {
			$(this).remove();
		});
	});
	
	// input field focus/blur
	$("input:text")
		.focus(function() {
			$(this).addClass('active');
		})
		.blur( function() {
			$(this).removeClass('active');
		});

	// Capture the submit [next] button
	$("#form1of2").submit(function() {
		var form_id = $('#form_id').val();
		var zip = $("#Zip").val();
		var state = $("#State").val();
		s.linkTrackVars = 'eVar1,eVar11,eVar12'; // lead type, state, zip
		s.linktrackVars += (form_id == 'programs' || form_id == 'coned') ? ',prop9,eVar9,prop10,eVar10': ''; // add campus info
		s.eVar1 = (form_id == 'programs' || form_id == 'coned') ? 'program': 'general';
		s.eVar11 = state;
		s.eVar12 = zip;
		e.events = "event1";
		if( form_id == 'programs' || form_id == 'coned' ) {
			var campus_id = $("campusID").val();
			s.prop9 = get_campus_name(campus_id);
			s.prop10 = (campus == "ol") ? "online": "on-campus";
		}
		// send it
		s.tl(this, 'o', 'Request Info Step 1 Submission');
	});

	$("#form1of2 #resetForm").click(function() {
		// clear the form
		$("#form1of2").clearForm();
	});
	
	$("#asd_select, #bsd_select").change(function() {
		var url = $(this).val();
		window.location = url;
	});

	// Window unload
	$(window).unload(function() {
		if(chkFormSettings("form1of2")) {
			s.formList="form1of2";
			s.trackFormList = true;
			s_doPlugins(s);
		}
	});
});

/*
 * 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);
}

/**
 * Return the name of campus
 * @param campus_id
 * @return string
 */
function get_campus_name(campus_id) {
	var nm = {
		'rs' : 'Richmond (South Side)',
		'news' : 'Newport News',
		'ol' : 'Online',
		'ri' : 'Richmond/Innsbrook (West End)',
		'vab' : 'Virginia Beach',
		'ral' : 'Raleigh',
		'nva' : 'Northern Virginia',
		'cha' : 'Charlotte',
		'gso' : 'Greensboro',
		'rics' : 'Richmond (South Side)',
		'ricw' : 'Richmond/Innsbrook (West End)',
		'roa' : 'Roanoke',
		'cn' : 'Charleston',
		'cl' : 'Columbia',
		'cd' : 'Charlotte (Concord)',
		'mil' : 'Military',
		'rw' : 'Richmond (West End)',
		'mcinews' : 'Newport News',
		'mcivab' : 'Virginia Beach',
		'ativab' : 'Virginia Beach',
		'civnor' : 'Norfolk',
		'mcirs' : 'Richmond (South Side)'
	};

	return nm[campus_id];
}