function filterApts() {
	var category = document.getElementById('category').value;
	$('#apts_content').load("../apts_admin/filterApts/"+category);
}

function filterAdminApts() {
	var category = document.getElementById('category').value;
	$('#apts_content').load("../apts_admin/filterAdminApts/"+category);
}

function showForm(apt_id, slotsleft) {
 	$('#apts_content').load("../apts_admin/showForm/"+apt_id+"/"+slotsleft);
}

function ShowQty() {
	var qty = document.getElementById('qty_div');
	qty.style.display = 'inline';
}

function HideQty() {
	var qty = document.getElementById('qty_div');
	qty.style.display = 'none';
}

function submit_FOO(apt_id, category) {
	var first = document.getElementById('signup_first').value;
 	var last = document.getElementById('signup_last').value;
 	var email = document.getElementById('signup_email').value;
 	var phone = document.getElementById('signup_phone').value;
 	
 	var category = document.getElementById('category').value;

	$.post("/evsched/apts_admin/addMe", {first: first, last: last, email: email, phone: phone, apt_id: apt_id, category: category}, function() {
		$('#apts_content').load("../apts_admin/filterApts/"+category, {}, function() {$("#flashMessage").show("normal",
        function()
        {
            $("#flashMessage").fadeOut(10000);
        });
        });
		});
	}
	

	// function to check empowerme field
	function check(cat_id) {
		var pass = document.getElementById('pass').value;
		var category = document.getElementById('category').value;
		
		$.post("/evsched/apts_admin/checkPass", {pass: pass, cat_id: cat_id}, function() {
			$('#apts_content').load("/evsched/apts_admin/filterApts/"+category, {}, function() {$("#flashMessage").show("normal",
        function()
        {
            $("#flashMessage").fadeOut(10000);
        });
        });
		});
	}
	
	// function to check admin passfield
	function checkA() {
		var pass = document.getElementById('pass').value;
		
		$.post("/evsched/apts_admin/checkPassA", {pass: pass}, function() {
			window.location.href = "/evsched/apts_admin";
		});
	}
	
	

	
	
	// validation junk

function trim(s)
	{
	 return s.replace(/^\s+|\s+$/, '');
	}

function submit(apt_id, category, haystack) {

	// get all the 'regular' info
	var first = document.getElementById('signup_first').value;
 	var last = document.getElementById('signup_last').value;
 	var email = document.getElementById('signup_email').value;
 	var phone = document.getElementById('signup_phone').value;
 	
 	var slotsleft = document.getElementById('slotsleft').value; 	
 	
 	// each one of these chunks checks if a Q is checked (admin)
 	// if so, we get the info, if not we set it to NA so they at least pass validation
 	var pos=String(haystack).indexOf("0")
		if (pos>=0) {
			// do stuff here for Q0
			var course_name = document.getElementById('course_name').value;
			var tcourse_name = trim(course_name);
		} else {
			var course_name = 'NA';
			var tcourse_name = 'NA';
		}
		
	var pos=String(haystack).indexOf("1")
		if (pos>=0) {
			// do stuff here for Q1
			var course_number = document.getElementById('course_number').value;
			var tcourse_number = trim(course_number);
		} else {
			var course_number = 'NA';
			var tcourse_number = 'NA';
		}
		
	var pos=String(haystack).indexOf("2")
		if (pos>=0) {
			// do stuff here for Q2
			var program = document.getElementById('program').value;
			var tprogram = trim(program);
		} else {
			var program = 'NA';
			var tprogram = 'NA';
		}
		
	var pos=String(haystack).indexOf("3")
		if (pos>=0) {
			// do stuff here for Q3
			var purpose = document.getElementById('purpose').value;
			var tpurpose = trim(purpose);
		} else {
			var purpose = 'NA';
			var tpurpose = 'NA';
		}
	
	var pos=String(haystack).indexOf("4")
		if (pos>=0) {
			// do stuff here for Q4
			var employer = document.getElementById('employer').value;
			var temployer = trim(employer);
		} else {
			var employer = 'NA';
			var temployer = 'NA';
		}
		
	// alittle different beacuse it is a radio button array
	var pos=String(haystack).indexOf("5")
		if (pos>=0) {
			// do stuff here for Q5
			var radios = document.getElementsByName('compass');
			for(var i = 0; i < radios.length; i++) {
				if ( radios[0].checked ) {
					var compass = "Yes";
				} else if (radios[1].checked){
					var compass = "No";
				} else {
					var compass = "";
				}
			}
		} else {
			var compass = 'NA';
		}
		
		
	// alittle different beacuse it is a radio button array
	// AND it pops open another field if YES... we want that too
	var pos=String(haystack).indexOf("6")
		if (pos>=0) {
			// do stuff here for Q7
			var radios = document.getElementsByName('group');
			for(var i = 0; i < radios.length; i++) {
				if ( radios[0].checked ) {
					var group = "Yes";
					var qty = document.getElementById('qty').value;
					var tqty = trim(qty);
				} else if (radios[1].checked){
					var group = "No";
					var qty = "1";
					var tqty = "1";
				} else {
					var group = "";
				}
			}
		} else {
			var group = 'NA';
			var qty = "1";
			var tqty = "1";
		}
		
		
	var pos=String(haystack).indexOf("7")
		if (pos>=0) {
			// do stuff here for Q7
			var discipline = document.getElementById('discipline').value;
			var tdiscipline = trim(discipline);
		} else {
			var discipline = 'NA';
			var tdiscipline = 'NA';
		}
		
	var pos=String(haystack).indexOf("8")
		if (pos>=0) {
			// do stuff here for Q8
			var address = document.getElementById('address').value;
			var city = document.getElementById('city').value;
			var state = document.getElementById('state').value;
			var zip = document.getElementById('zip').value;
			var taddress = trim(address);
			var tcity = trim(city);
			var tstate = trim(state);
			var tzip = trim(zip);
		} else {
			var address = 'NA';
			var city = 'NA';
			var state = 'NA';
			var zip = 'NA';
			var taddress = 'NA';
			var tcity = 'NA';
			var tstate = 'NA';
			var tzip = 'NA';
		}
		
			
	// get and keep the category (for the cancel button)
	var category = document.getElementById('category').value;
	
	var error="";
	
	// trim all the regular junk (to eliminate bogus empty spaces
	var tfirst = trim(first);
	var tlast = trim(last);
	var tphone = trim(phone);
	var temail = trim(email);
	
	//alert ("QTY: "+tqty+"\nSLOTS: "+slotsleft);
	
	// our email conditions
	var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
	// our illegals
	var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
	var phone_illegalChars= /[\<\>\,\;\:\\\"\[\]]/ ;
		
	 // bounce through ALL the fields and validate
	 // if they ALL pass, send the data, create the appt. and repopulate the view
	 if (tfirst == "") {
			window.scrollTo(0,0);
			document.getElementById('errorText').innerHTML = 'Your first name is required.';
		} else if (tfirst.match(illegalChars)) {
			window.scrollTo(0,0);
			document.getElementById('errorText').innerHTML = 'Your first name contains illegal characters.';
		} else if (tlast == "") {
			window.scrollTo(0,0);
			document.getElementById('errorText').innerHTML = 'Your last name is required.';
		} else if (tlast.match(illegalChars)) {
			window.scrollTo(0,0);
			document.getElementById('errorText').innerHTML = 'Your last name contains illegal characters.';
		} else if (temail == "") {
			window.scrollTo(0,0);
			document.getElementById('errorText').innerHTML = 'Your email address is required.';
		} else if (!emailFilter.test(temail)) {
			window.scrollTo(0,0);
			document.getElementById('errorText').innerHTML = 'Your email address must be a valid address.';
		} else if (temail.match(illegalChars)) {
			window.scrollTo(0,0);
			document.getElementById('errorText').innerHTML = 'Your email address contains illegal characters.';
		} else if (tphone == "") {
			window.scrollTo(0,0);
			document.getElementById('errorText').innerHTML = 'Your phone number is required.';
		} else if (tphone.match(phone_illegalChars)) {
			window.scrollTo(0,0);
			document.getElementById('errorText').innerHTML = 'Your phone number contains illegal characters.';
		} 
		
		else if (tcourse_name == "") {
			window.scrollTo(0,0);
			document.getElementById('errorText').innerHTML = 'Course Name field is required.';
		} else if (tcourse_name.match(illegalChars)) {
			window.scrollTo(0,0);
			document.getElementById('errorText').innerHTML = 'Course Name field contains illegal characters.';
		}
		
		else if (tcourse_number == "") {
			window.scrollTo(0,0);
			document.getElementById('errorText').innerHTML = 'Course Number field is required.';
		} else if (tcourse_number.match(illegalChars)) {
			window.scrollTo(0,0);
			document.getElementById('errorText').innerHTML = 'Course number field contains illegal characters.';
		} 
		
		else if (tprogram == "") {
			window.scrollTo(0,0);
			document.getElementById('errorText').innerHTML = 'Program field is required.';
		} else if (tprogram.match(illegalChars)) {
			window.scrollTo(0,0);
			document.getElementById('errorText').innerHTML = 'Program field contains illegal characters.';
		} 
		
		else if (tpurpose == "") {
			window.scrollTo(0,0);
			document.getElementById('errorText').innerHTML = 'Purpose field is required.';
		} else if (tpurpose.match(illegalChars)) {
			window.scrollTo(0,0);
			document.getElementById('errorText').innerHTML = 'Purpose field contains illegal characters.';
		} 
		
		else if (temployer == "") {
			window.scrollTo(0,0);
			document.getElementById('errorText').innerHTML = 'Employer field is required.';
		} else if (temployer.match(illegalChars)) {
			window.scrollTo(0,0);
			document.getElementById('errorText').innerHTML = 'Employer field contains illegal characters.';
		} 
		
		else if (compass == "") {
			window.scrollTo(0,0);
			document.getElementById('errorText').innerHTML = 'Please check Yes or No. (Compass)';
		}  
		
		else if (group == "") {
			window.scrollTo(0,0);
			document.getElementById('errorText').innerHTML = 'Please check Yes or No. (Group)';
		} 
		
		else if (tqty == "") {
			window.scrollTo(0,0);
			document.getElementById('errorText').innerHTML = 'Please indicate the number in your group.';
		} else if (tqty.match(illegalChars)) {
			window.scrollTo(0,0);
			document.getElementById('errorText').innerHTML = 'Group Number field contains illegal characters.';
		} else if (tqty > slotsleft) {
			if (slotsleft != 1) {
				window.scrollTo(0,0);
				document.getElementById('errorText').innerHTML = 'There are only '+slotsleft+' available slots for this appointment.';
			} else {
				window.scrollTo(0,0);
				document.getElementById('errorText').innerHTML = 'There is only '+slotsleft+' available slot for this appointment.';
			}
		} 
		
		else if (tdiscipline == "") {
			window.scrollTo(0,0);
			document.getElementById('errorText').innerHTML = 'Discipline field is required.';
		} else if (tdiscipline.match(illegalChars)) {
			window.scrollTo(0,0);
			document.getElementById('errorText').innerHTML = 'Discipline field contains illegal characters.';
		} 
		
		else if (taddress == "") {
			window.scrollTo(0,0);
			document.getElementById('errorText').innerHTML = 'Address field is required.';
		} else if (taddress.match(illegalChars)) {
			window.scrollTo(0,0);
			document.getElementById('errorText').innerHTML = 'Address field contains illegal characters.';
		}
		
		else if (tcity == "") {
			window.scrollTo(0,0);
			document.getElementById('errorText').innerHTML = 'City field is required.';
		} else if (tcity.match(illegalChars)) {
			window.scrollTo(0,0);
			document.getElementById('errorText').innerHTML = 'City field contains illegal characters.';
		}
		
		else if (tstate == "") {
			window.scrollTo(0,0);
			document.getElementById('errorText').innerHTML = 'State field is required.';
		} else if (tstate.match(illegalChars)) {
			window.scrollTo(0,0);
			document.getElementById('errorText').innerHTML = 'State field contains illegal characters.';
		}
		
		else if (tzip == "") {
			window.scrollTo(0,0);
			document.getElementById('errorText').innerHTML = 'Zip field is required.';
		} else if (tzip.match(illegalChars)) {
			window.scrollTo(0,0);
			document.getElementById('errorText').innerHTML = 'Zip field contains illegal characters.';
		}
		
		else { 
			$.post("/evsched/apts_admin/addMe", {first: first, last: last, email: email, phone: phone, apt_id: apt_id, category: category, tcourse_name: tcourse_name, tcourse_number: tcourse_number, tprogram: tprogram, tpurpose: tpurpose, temployer: temployer, compass: compass, group: group, tqty: tqty, tdiscipline: tdiscipline, taddress: taddress, tcity: tcity, tstate: tstate, tzip: tzip}, function() {
		$('#apts_content').load("../apts_admin/filterApts/"+category, {}, function() {$("#flashMessage").show("normal",
        function()
        {
            $("#flashMessage").fadeOut(10000);
        });
        });
		});
				
		
		} 
}
