$(function(){
	
	//create dynamic select boxes
	function dynamicSelect(selectID) {
		var select1 = $("<select><option>-- Select State --</option></select>");
		var select2 = $("<select id=" + selectID + " name=" + selectID + "><option value=''>-- Select College --</option></select>");
		var myArray = [];
		$("#" + selectID + " optgroup").each(function(i){
			$("<option value=" + this.label + ">" + this.label + "</option>").appendTo(select1);
			myArray[i] = $.makeArray($(this).children());
		});
		$(select1).change(function(e){
			$(select2).children(":not(:first-child)").remove();
			if(this.selectedIndex > 0) {
				for(var i = 0; i < myArray[this.selectedIndex - 1].length; i++) {
					$("<option value='" + myArray[this.selectedIndex - 1][i].value + "'>" + myArray[this.selectedIndex - 1][i].text + "</option>").appendTo(select2);
				}
			}
		});
		$("#" + selectID).after(select1).remove();
		$(select1).after(select2).after(" ")
		$(select2).css("width","300px").css("margin","8px 0");
	}
	dynamicSelect("cc");
	
	$("#studentTypeDiv").hide();
	$("#trans1").click(function(){
		$("#studentTypeDiv").slideDown("fast");
	});
	
	$("#trans2").click(function(){
		$("#studentTypeDiv").slideUp("fast");
	});
	
	//show/hide sections
	$("#conditionalDiv").hide();
	$("#studentType").change(function(){
		$("#conditionalDiv").slideUp("fast",function(){
			$("#conditionalDiv div").hide();
			if($("#studentType").val() == "Career/Technical Center Student") {
				$("#technicalCenterSection").show();
				$("#conditionalDiv").slideDown("fast");
			}
			if($("#studentType").val() == "Community College Student") {
				$("#ccSection").show();
				$("#conditionalDiv").slideDown("fast");
			}
			if($("#studentType").val() == "International Student") {
				$("#internationalSection").show();
				$("#conditionalDiv").slideDown("fast");
			}
			if($("#studentType").val() == "Military Service Member or Family Member") {
				$("#militarySection").show();
				$("#conditionalDiv").slideDown("fast");
			}
			
		});
	});
	$("#state").change(function(){
		if($("#state").val().match(/oh/i) && $("#studentType").val() == "Career/Technical Center Student") {
			$("#technicalCenterSection").show();
			$("#conditionalDiv").slideDown("fast");
		}
		else {
			if($("#studentType").val() == "Career/Technical Center Student") $("#conditionalDiv").slideUp("fast");
			$("#technicalCenterSection").hide();
		}
		
	});
	
	$("div.continue input")[0].msg = "Please indicate where you would prefer to take classes, whether you are transferring from another school, and when you intend to start before submitting.";
	$("div.continue input").blur(function(){
		hideInfo("validation");
	});
	
	$("form")[0].customFunction = function() {
		if((!$("#classLoc1")[0].checked && !$("#classLoc2")[0].checked) ||
		   (!$("#trans1")[0].checked && !$("#trans2")[0].checked) ||
		   (!$("#term1")[0].checked && !$("#term2")[0].checked && !$("#term3")[0].checked)) {
				showInfo($("div.continue input")[0],"validation");
		   }
		else $("form")[0].submit();
	}
	
	//set validation
	setValidation("firstName","Please enter a first name.");
	setValidation("lastName","Please enter a last name.");
	setValidation("street","Please enter a street address.");
	setValidation("city","Please enter a city.");
	setValidation("state","Please enter a state.");
	setValidation("zip","Please enter a five digit zip code.",/^\d{5}$/);
	setValidation("email","Please enter a valid email address.",/^[^@\s]+@{1}[^@\s\.]+\.{1}[^@\s]+$/);
	setValidation("primaryPhone","Please enter a 10 digit phone number.",validatePhone);
	
	function validatePhone(field){
		$(field).val($(field).val().replace(/\D/g,''));
		if($(field).val().length == 10) $(field).val("(" + $(field).val().substr(0,3) + ") " + $(field).val().substr(3,3) + "-" + $(field).val().substr(6,4));
		$(field).val().match(/^\(\d{3}\)\s\d{3}-\d{4}/) ? makeValid(field) : makeInvalid(field);
	}
});