$(document).ready(function() {
	function hideForm(){
		$("#thirdpart").fadeOut('1000', 
			function(){
				$("#form_animate").animate({height : "115px"},"fast");
				$('#desiredmajor').css("margin-bottom", "42px");
			});
		var arr = ["#firstnameerror","#lastnameerror", "#mierror",  "#address1error", "#address1error", "#address2error", "#cityerror", "#stateerror", "#ziperror", "#countryerror", "#homephoneerror", "#cellphoneerror", "#emailerror"]
		//loop over the error balloons to close them all.
		$.each(arr, function(n, val){				 
			$(val).remove();
		});
		
	};
	
	function IsNumeric(sText){
		var ValidChars = "0123456789.";
		var IsNumber=true;
		var Char;

		for (i = 0; i < sText.length && IsNumber == true; i++) 
		  { 
		  Char = sText.charAt(i); 
		  if (ValidChars.indexOf(Char) == -1){
			 	IsNumber = false;
			 }
		  }
		return IsNumber;
	};
	
	function updateErrors(){
		var arr = ["#firstname","#lastname", "#mi",  "#address1", "#address2", "#city", "#state", "#zip", "#country", "#homephone", "#cellphone", "#email"]
		$.each(arr, function(n, val){
			var caller = $(val);
			var updateThisPrompt =  $(caller).attr("id");
			if( $("."+updateThisPrompt).attr("id") == "countryerror" || $("."+updateThisPrompt).attr("id") == "stateerror" || $("."+updateThisPrompt).attr("id") == "termerror" || $("."+updateThisPrompt).attr("id") == "highestlevelerror" || $("."+updateThisPrompt).attr("id") == "programerror"){
				if ($.browser.safari) 
						callerTopPosition = $(caller).parent().offset().top - 1;
				else
					callerTopPosition = $(caller).parent().offset().top;
				}
				else{
					if ($.browser.safari) 
						callerTopPosition = $(caller).offset().top  + 10;
					else
						callerTopPosition  = $(caller).offset().top;
				};
				
				inputHeight = $("."+updateThisPrompt).height()
				
				callerTopPosition = callerTopPosition  - inputHeight - 10
				$("."+updateThisPrompt).animate({
					top:callerTopPosition
				});
		});
		
	};
	function showForm(){
		if ($('#thirdpart').css("display") == "none"){
			$("#form_animate").animate({height : "+=290"}, "500", 
			function(){
				$("#thirdpart").fadeIn('1000');
				$('#firstname').focus().trigger('click');
				$('#firstnameerror').hide('fast').remove();	
				$('#desiredmajor').css("margin-bottom", "5px");
			});
		} else
		setTimeout(function () {updateErrors();},300)
			
		
	}
	function showNurse(selectedValue){
		if(selectedValue == "NURS-BSN-D") {
			$("#nurseconfirm").attr('checked', false);
			$("#nurseconfirm").removeClass('jqTransformChecked');
			$("#submit").attr('disabled', true);
			$("#submit").css('color', '#73b0d9');
			$("#nursing_confirm").show('fast');

		 }
		 else{
			$("#nursing_confirm").hide('fast');
			$("#nurseconfirm").attr('checked', false);
			$("#nurseconfirm").removeClass('jqTransformChecked');	 
		}
	}
	function counselingError(stateValue, selectValue, message){
		if(stateValue == "MO" || stateValue == "KS" || stateValue == "IL" || stateValue == "CA" || stateValue == "AR") {
			if(selectValue == "COEX-MA-D" || selectValue == "COMF-MA-D" || selectValue == "COUN-MA-D") {
				if(message == "yes")
					alert("Unfortunately, your state does not accept either external schools or online degrees for licensure.\n\nPlease check with your state licensure board for further clarification.");
			$("#submit").attr('disabled', true).css('color', '#73b0d9');
			} else {$("#submit").removeAttr('disabled').css('color', '#0496FC');}
		} else {$("#submit").removeAttr('disabled').css('color', '#0496FC');}	
	}
	$("label").inFieldLabels();
	$("#highestlevel").sexyCombo({	
		emptyText: "Select Level",
		changeCallback: function(){
			var passFile = this.getHiddenValue();
			$.getJSON("/admissions/inquiryform/getDegrees.cfm",{"path":"/media/9932/requestinformation/","value": passFile},function(cfmResponse){
				$("#programs_container").fadeOut(300, function(){
				$("#programs_container").html('');
				$("#programs_container").fadeIn(300)
				$("#programs_container").html("<select name='program_online' id='program_online' style='position: absolute; left: 30000px;' class='validate[required]'></select>")
				$("#program_online").html(cfmResponse.programselect);
				$("#program_online").siblings(".list-wrapper").html(cfmResponse.programil);
				$("#program_online").sexyCombo({
				emptyText: "Select Major", 
				skin: "big_combo",
				changeCallback: function(){
					var stateValue = $('#state option:selected').val();
					var selectedValue = this.getHiddenValue();
					showNurse(selectedValue);
					if(selectedValue != "")
						showForm();
					else
						hideForm()
					counselingError(stateValue, selectedValue, 'yes');
				}
				})
				$("#program_online").css("position","static")
				if(passFile == "")
					$("#nursing_confirm").hide('fast');
					$("#nurseconfirm").attr('checked', false);
					$("#nurseconfirm").removeClass('jqTransformChecked');
					hideForm()
				})
			})
		}
	});
	$("#program_online").sexyCombo({emptyText: "Select Major"});
	$("#major_select").sexyCombo({emptyText: "Select Major", triggerSelected: true, skin: "big_combo"});
	$("#country").sexyCombo({
		emptyText: "Select a Country", 
		changeCallback: function(){
			var selectedValue  = this.getHiddenValue();
			if (selectedValue != "united states" && selectedValue != "canada"){
				var val = "**";
				var v = "No State"
				$("#state__sexyCombo").removeClass("empty").val(v);
				$("[name=state__sexyComboHidden]").val(val);
				$('#state').attr('selected','selected');
			}
		},
		triggerSelected: true
	});
	$("#state").sexyCombo({
		emptyText: "Select a State", 
		changeCallback: function(){
			var programValue = $("#program_online :selected").val()
			var selectedValue  = this.getHiddenValue();
			var radio = $('#interest_radio_0').attr("checked");
			if (radio){
				counselingError(selectedValue, programValue, 'yes');
			}
		}
	});
	$("#term").sexyCombo({
		emptyText: "Select Term",
		changeCallback: function(){
			var term = this.getHiddenValue()
			if(term != "")
				showForm()
			else
				hideForm()  	
		}
	});
	$("#firstpart").hide();
	$("#secondpart").hide();
	$("#militarysection").hide();
	$("#thirdpart").hide();
	$("#country__sexyCombo").siblings(".list-wrapper").css("text-transform","capitalize")
	$("#country__sexyCombo").css("text-transform","capitalize")

	$("#interest_radio_0").click(function() {   
		$("#secondpart").hide('1000');
		$("#firstpart").show('1000');
		$("#militarysection").show('1000');
		$("#major_select").removeClass("validate[required]");
		$("#highestlevel").addClass("validate[required]");
		$("#program_online").addClass("validate[required]");
		$("#term").removeClass("validate[required]");
		$("#passcode").val("dlp2liberty");
		$("#type").val("dlp");
		if($("#thirdpart").css("display") == "none") 
			$("#form_animate").animate({height : "115px"}, "fast");
		$("#all").css("position","relative").css("top","0px").css("left","0px");
		var programValue = $("#program_online :selected").val()
		var stateValue  = $("#state :selected").val();
		counselingError(stateValue, programValue, 'yes');
		showNurse($('[name=program_online__sexyComboHidden]').val())
		if($("#program_online option").index($("#program_online option:selected")) > 0){
			showForm()
		}
		else{
			hideForm()		
		}
	});

	$("#interest_radio_1").click(function() {    
		$("#firstpart").hide('1000');
		$("#secondpart").show('1000');
		$("#militarysection").hide('1000');
		$("#major_select").addClass("validate[required]");
		$("#highestlevel").removeClass("validate[required]");
		$("#program_online").removeClass("validate[required]");
		$("#passcode").val("resident2liberty");
		$("#type").val("res");
		if($("#thirdpart").css("display") == "none") {
			$("#form_animate").animate({height : "115px"}, "fast");}
		$("#nursing_confirm").hide('fast');
		$("#nurseconfirm").attr('checked', false).removeClass('jqTransformChecked');
		$("#all").css("position","relative").css("top","0px").css("left","0px");
		$("#submit").removeAttr('disabled').css('color', '#0496FC');
		if($("#term option").index($("#term option:selected")) > 0){
			showForm()
		}
		else{
			hideForm();	
		}
	});
	
	$(".online").click(function() {
		if ($.browser.msie) {
		   $("#interest_radio_0").siblings(".jqTransformRadio").addClass('jqTransformChecked');
		   $("#interest_radio_0").trigger('click')
		}
		$("#interest_radio_1").siblings(".jqTransformRadio").removeClass('jqTransformChecked');

	});
	$(".resident").click(function() {
		if ($.browser.msie) {
		   $("#interest_radio_1").siblings(".jqTransformRadio").addClass('jqTransformChecked');
		   $("#interest_radio_1").trigger('click')
		}						  
		$("#interest_radio_0").siblings(".jqTransformRadio").removeClass('jqTransformChecked');
	});
	
	$('#homephone').numeric({allow:"-()+"});
							   
	$("#cellphone").numeric({allow:"-()+"});

	$('#zip').alphanumeric({allow:"-"});

	$("#submit").click(function() { 
		if ($("#type").val() == "res") {
			$("#program").val($("#major_select").val());
		}
		else {
			$("#program").val($("#program_online").val());
		}
		if($('#urlRedirect').val() != ""){
			$('#redirect').val($('#urlRedirect').val())
		}
		else{
			if($("#interest_radio_0").attr("checked")){
				$('#redirect').val($('#onlineRedirect').val())
			}
			else{
				$('#redirect').val($('#residentRedirect').val())
			}
		};
		return true;
	});
	
	$("#nurseconfirm").click(function() {
		var selectedValue = $("#program_online :selected").val();
		if(selectedValue == "NURS-BSN-D") {
			if($("#nurseconfirm:checked").val() != null){$("#submit").removeAttr('disabled'); $("#submit").css('color', '#0496FC');}
			else {$("#submit").attr('disabled', true); $("#submit").css('color', '#73b0d9');}
		} else {$("#submit").removeAttr('disabled'); $("#submit").css('color', '#0496FC');}
	});
});