$(document).ready(function(){

	//This function will listen for changes to the program of interest drop-down box, and build a list of campuses
	//based on whether that program is offered there or not. Campus list will not have any options until a program
	//is selected.

	//REQUIRES jquery1.3.2.min.js (or better) to be included PRIOR to this script being included.

	// Campus select values to campus names map:
	// 20 = Philadelphia, PA
	// 95 = Upper Darby, PA
	// 19 = Cherry Hill, NJ
	// 2 = Egg Harbor Twp
	var campus_map = {
		'20' : 'Philadelphia, PA',
		'95' : 'Upper Darby, PA',
		'19' : 'Cherry Hill, NJ',
		'2'  : 'Egg Harbor Twp'
	};

	// Program names to campus select values that carry program
	var program_campus = {
		'Paralegal': 				[/*'20',*/'95'/*,'19','2'*/],
		'Legal Secretary': 			['95'],
		'Legal Accounting': 			['95'],
		'Health Information Tech': 		['95'],
		'Medical Billing & Coding': 		['95'],
		'Medical Assistant': 			['20','95','19','2'],
		'Early Childhood Education': 		['95'],
		'Business Accounting':	 		['95'],
		'Computer Office Specialist': 		['95'],
		'Patient Care Assistant': 		[/*'19',*/'2'],
		'Practical Nursing': 			['20','19'],
		'I\'m not yet sure': 			['20','95','19','2']
		//Add new program values here
	};

	var program_select	=	$('#CustomFields_223_99');
	var campus_select 	= 	$('#campus_list');

	// Listen for changes to the program select
	program_select.change(function(){
		// clear current campus select
		campus_select.empty();

		// Add 'Please Choose' option
		campus_select.append('<option value="">Please Choose</option>');

		// Get the currently selected value from the program select.
		var cur_prog = program_select.val();
		// Get all campuses that provide the current program
		var campuses = program_campus[cur_prog];

		//Go through each campus offering the program, and add an option for that campus to the campus select
		jQuery.each(campuses, function(i,val) {
			campus_select.append('<option value="' + val + '">' + campus_map[val] + '</option>');
		});

	}); //end program_select listener

}); //end document ready callback function


//Checks for valid positive integer (i.e. no decimal, non-integer, or negative numbers allowed). Good for
//zip codes and other numbers that must be positive integers.
function isPositiveInt(value) {
	//List of valid chars
	var validChars = "0123456789";
	var strChar;
	var result = true;
	if (value.length == 0) return false;
	//Check each character in value that it is valid
	for (i = 0; i < value.length && result == true; i++) {
		strChar = value.charAt(i);
		if (validChars.indexOf(strChar) < 0) {
			result = false;
		}
	}
	return result;
}

//REQUIRES jquery1.3.2.min.js (or better) to be included PRIOR to this script being included.
//Ensures that the given zip code is valid by:
//1) Checking that the given zip code exists, is a number, and is 5 digits.
//2) Checking that the given zip code exists in the list of valid zip codes based on the campus and program chosen by the user.
//This function requires that the user has input a campus and a program.
function checkValidZip(zip) {

	// These are the valid zips arranged as campus -> program (if necessary) -> valid zips (array).
	// The common list of zip codes for a campus have a program key of 'common'. Otherwise, if there
	// is a specific list of zip codes for a program within a campus, the program key will be the
	// select option value for that program.
	//
	// The campus is the value from the campus select option. They are as follows:
	// 20 = Philadelphia, PA
	// 95 = Upper Darby, PA
	// 19 = Cherry Hill, NJ
	// 2 = Egg Harbor Twp
	//
	// These zip codes are derived from the document
	var valid_zips = {
		'20'	:	{
					'common':			['19020', '19027', '19038', '19111', '19114', '19115', '19116', '19117', '19118', '19119', '19120', '19121', '19122', '19123', '19124', '19125', '19126', '19127', '19128', '19129', '19130', '19131', '19132', '19133', '19134', '19135', '19136', '19137', '19138', '19139', '19140', '19141', '19142', '19143', '19144', '19145', '19146', '19147', '19148', '19149', '19150', '19151', '19152', '19153', '19154'],
					'Medical Assistant':	['19027', '19038', '19111', '19114', '19115', '19116', '19117', '19118', '19119', '19120', '19121', '19122', '19123', '19124', '19125', '19126', '19129', '19130', '19132', '19133', '19134', '19135', '19136', '19137', '19138', '19140', '19141', '19144', '19149', '19150', '19152', '19154'],
					'Practical Nursing':		['19020', '19027', '19038', '19111', '19114', '19115', '19116', '19117', '19118', '19119', '19120', '19121', '19122', '19123', '19124', '19125', '19126', '19127', '19128', '19129', '19130', '19131', '19132', '19133', '19134', '19135', '19136', '19137', '19138', '19139', '19140', '19141', '19142', '19143', '19144', '19145', '19146', '19147', '19148', '19149', '19150', '19151', '19152', '19153', '19154']
				},
		'95'	:	{
					'common':			['19003', '19004', '19008', '19013', '19014', '19015', '19017', '19018', '19022', '19023', '19026', '19027', '19029', '19032', '19033', '19036', '19038', '19041', '19043', '19050', '19060', '19061', '19063', '19064', '19070', '19072', '19073', '19074', '19076', '19078', '19079', '19082', '19083', '19086', '19094', '19114', '19115', '19116', '19117', '19118', '19119', '19120', '19121', '19122', '19123', '19124', '19125', '19126', '19127', '19128', '19129', '19130', '19131', '19132', '19133', '19134', '19135', '19136', '19137', '19138', '19139', '19140', '19141', '19142', '19143', '19144', '19145', '19146', '19147', '19148', '19149', '19150', '19151', '19152', '19153', '19154', '19320', '19380', '19381', '19382', '19401', '19402', '19403', '19404', '19405', '19428'],
					'Medical Assistant':		['19003', '19004', '19008', '19013', '19014', '19015', '19017', '19018', '19022', '19023', '19026', '19029', '19032', '19033', '19036', '19041', '19043', '19050', '19060', '19061', '19063', '19064', '19070', '19072', '19073', '19074', '19076', '19078', '19079', '19082', '19083', '19086', '19094', '19127', '19128', '19131', '19139', '19142', '19143', '19145', '19146', '19147', '19148', '19151', '19153', '19320', '19380', '19381', '19382', '19401', '19402', '19403', '19404', '19405', '19428'],
					'Early Childhood Education':	['19003', '19004', '19008', '19013', '19014', '19015', '19017', '19018', '19022', '19023', '19026', '19027', '19029', '19032', '19033', '19036', '19038', '19041', '19043', '19050', '19060', '19061', '19063', '19064', '19070', '19072', '19073', '19074', '19076', '19078', '19079', '19082', '19083', '19086', '19094', '19111', '19114', '19115', '19116', '19117', '19118', '19119', '19120', '19121', '19122', '19123', '19124', '19125', '19126', '19127', '19128', '19129', '19130', '19131', '19132', '19133', '19134', '19135', '19136', '19137', '19138', '19139', '19140', '19141', '19142', '19143', '19144', '19145', '19146', '19147', '19148', '19149', '19150', '19151', '19152', '19153', '19154', '19320', '19380', '19381', '19382', '19401', '19403', '19404', '19405', '19428'],
					'Health Information Tech':		['19003', '19004', '19008', '19013', '19014', '19015', '19017', '19018', '19022', '19023', '19026', '19027', '19029', '19032', '19033', '19036', '19038', '19041', '19043', '19050', '19060', '19061', '19063', '19064', '19070', '19072', '19073', '19074', '19076', '19078', '19079', '19082', '19083', '19086', '19094', '19111', '19114', '19115', '19116', '19117', '19118', '19119', '19120', '19121', '19122', '19123', '19124', '19125', '19126', '19127', '19128', '19129', '19130', '19131', '19132', '19133', '19134', '19135', '19136', '19137', '19138', '19139', '19140', '19141', '19142', '19143', '19144', '19145', '19146', '19147', '19148', '19149', '19150', '19151', '19152', '19153', '19154', '19320', '19380', '19381', '19382', '19401', '19403', '19404', '19405', '19428'],
					'Business Accounting':			['19003', '19004', '19008', '19013', '19014', '19015', '19017', '19018', '19022', '19023', '19026', '19027', '19029', '19032', '19033', '19036', '19038', '19041', '19043', '19050', '19060', '19061', '19063', '19064', '19070', '19072', '19073', '19074', '19076', '19078', '19079', '19082', '19083', '19086', '19094', '19111', '19114', '19115', '19116', '19117', '19118', '19119', '19120', '19121', '19122', '19123', '19124', '19125', '19126', '19127', '19128', '19129', '19130', '19131', '19132', '19133', '19134', '19135', '19136', '19137', '19138', '19139', '19140', '19141', '19142', '19143', '19144', '19145', '19146', '19147', '19148', '19149', '19150', '19151', '19152', '19153', '19154', '19320', '19380', '19381', '19382', '19401', '19403', '19404', '19405', '19428'],
					'Paralegal':							['19003', '19004', '19008', '19013', '19014', '19015', '19017', '19018', '19022', '19023', '19026', '19027', '19029', '19032', '19033', '19036', '19038', '19041', '19043', '19050', '19060', '19061', '19063', '19064', '19070', '19072', '19073', '19074', '19076', '19078', '19079', '19082', '19083', '19086', '19094', '19111', '19114', '19115', '19116', '19117', '19118', '19119', '19120', '19121', '19122', '19123', '19124', '19125', '19126', '19127', '19128', '19129', '19130', '19131', '19132', '19133', '19134', '19135', '19136', '19137', '19138', '19139', '19140', '19141', '19142', '19143', '19144', '19145', '19146', '19147', '19148', '19149', '19150', '19151', '19152', '19153', '19154', '19320', '19380', '19381', '19382', '19401', '19403', '19404', '19405', '19428'],
					'Medical Billing & Coding':		['19003', '19004', '19008', '19013', '19014', '19015', '19017', '19018', '19022', '19023', '19026', '19027', '19029', '19032', '19033', '19036', '19038', '19041', '19043', '19050', '19060', '19061', '19063', '19064', '19070', '19072', '19073', '19074', '19076', '19078', '19079', '19082', '19083', '19086', '19094', '19111', '19114', '19115', '19116', '19117', '19118', '19119', '19120', '19121', '19122', '19123', '19124', '19125', '19126', '19127', '19128', '19129', '19130', '19131', '19132', '19133', '19134', '19135', '19136', '19137', '19138', '19139', '19140', '19141', '19142', '19143', '19144', '19145', '19146', '19147', '19148', '19149', '19150', '19151', '19152', '19153', '19154', '19320', '19380', '19381', '19382', '19401', '19403', '19404', '19405', '19428']
				},
		'19'	:	{
					'common':			['08002', '08004', '08007', '08010', '08012', '08016', '08020', '08021', '08026', '08027', '08028', '08029', '08030', '08031', '08033', '08034', '08035', '08043', '08045', '08046', '08049', '08052', '08053', '08054', '08059', '08060', '08063', '08065', '08066', '08071', '08075', '08077', '08080', '08081', '08083', '08084', '08086', '08088', '08093', '08094', '08096', '08099', '08101', '08102', '08103', '08104', '08105', '08106', '08107', '08108', '08109', '08110', '08118', '08119', '08122']
				},
		'2'	:	{
					'common':			['08037', '08050', '08087', '08201', '08203', '08205', '08210', '08213', '08214', '08215', '08217', '08220', '08221', '08223', '08224', '08225', '08226', '08230', '08231', '08232', '08234', '08240', '08241', '08243', '08244', '08248', '08250', '08270', '08310', '08317', '08319', '08330', '08332', '08340', '08342', '08346', '08350', '08360', '08401', '08402', '08403', '08404', '08405', '08406']
				}
	};

	//Make sure the user entered zip is actually a valid zip code.
	if (zip.length != 5) {
		//no valid zip code entered
		return false;
	}
	else if (!isPositiveInt(zip)) {
		//not a valid zip code
		return false;
	}

	var campus 	= 	$('#campus_list').val();
	var program	=	$('#CustomFields_223_99').val();

	var program_zips = valid_zips[campus];
	if (program_zips.length == 0) {
		//couldn't find the campus key in the valid_zips map. We cannot continue.
		return false;
	}

	var zipcodes = program_zips[program];
	if (zipcodes == undefined || zipcodes.length == 0) {
		//couldn't find the program in the program to zip codes mapping. Default to 'common'.
		zipcodes = program_zips['common'];
	}

	//Search the valid zipcodes for the campus and program. Check if the zip code entered by the user is valid.
	if (jQuery.inArray(zip, zipcodes) >= 0) {
		//Found it! Valid zip code.
		return true;
	}

	//The user didn't enter a valid zip code.
	return false;

}
