// Voice The Union
// Perspektiv Purple, 2010
// James Allen
// Online Application Javascript

function initOnlineApplication() {	
	/*
	 *	Professions Form	 
	 */ 
	if($("input[name=introduced_by_member]").length != 0) {
		$("input[name=introduced_by_member][value=Yes]").click(function() { toggleMemberIntroduction("yes"); });
		$("input[name=introduced_by_member][value=No]").click(function() { toggleMemberIntroduction("no"); });
		toggleMemberIntroduction($("input[name=introduced_by_member]:checked").val());		
	
		$("input[name=profession],input[name=student]").click(function() { professionChange(this,1); });
		
		if($("input[name=profession]:checked").val()) {
			professionChange($("input[name=profession]").get(0));
		} else if($("input[name=student]:checked").val()) {
			professionChange($("input[name=student]").get(0));
		}

	}

	/*
	 *	Personal Details Form	 
	 */ 
	if($("#formname").val() == 'onlineApplicationPersonal') {
		$(".field_othertitle").hide();
		$("input[name=othertitle]").removeClass("required");
		
		$("input[name=Title]").click(function() {				
			if($("input[name=Title][value=Other]:checked").val() != 'Other') {
				$(".field_othertitle").hide();
				$("input[name=othertitle]").removeClass("required");
				$("input[name=othertitle]").val("");
			} else {
				$(".field_othertitle").show();
				$("input[name=othertitle]").addClass("required");
			}
		});
		
		$("input[name=Type_of_course]:checked").click();
				
		if($("input[name=Type_of_course]").length) {
			$(".field_othertypeofcourse").hide();
			$("input[name=othertypeofcourse]").removeClass("required");
			
			$("input[name=Type_of_course]").click(function() {				
				if($("input[name=Type_of_course][value=Other]:checked").val() != 'Other') {
					$(".field_othertypeofcourse").hide();
					$("input[name=othertypeofcourse]").removeClass("required");
					$("input[name=othertypeofcourse]").val("");
				} else {
					$(".field_othertypeofcourse").show();
					$("input[name=othertypeofcourse]").addClass("required");
				}
			});
			
			$("input[name=Type_of_course]:checked").click();
		}
		
		$("input[name=Title]:checked").click();
		
		/*$(".uniForm").submit(function(){
			if($("#password").val() != $("#password2").val()) {
				$("#password2").val("");
			}
		});*/
	}
	
	/*
	 *	Employment Details Form	 
	 */ 
	if($("#formname").val() == 'onlineApplicationEmployment') {
		
		$("input[name=Type_of_workplace]").click(function() {						
			if($("input[name=Type_of_workplace][value=Other]:checked").val() != 'Other') {
				$(".field_othertypeofworkplace").hide();
				$("input[name=othertypeofworkplace]").removeClass("required");
				$("input[name=othertypeofworkplace]").val("");
			} else {
				$(".field_othertypeofworkplace").show();
				$("input[name=othertypeofworkplace]").addClass("required");
			}
		});
		
		$("input[name=Type_of_workplace]:checked").click();		
	}
	
	// Progress bar
	$(".progress a").each(function() { $(this).click( function(e) 
		{ 
			e.preventDefault();
			destURL = $(this).attr("href");
			updateCurrentStep(0,function(){
				go(destURL);
			});
		}) 
	});
}

function go(url) {
	location.href = url;
}

function updateCurrentStep(reload,callBack) {
	if(reload) {
		$.post("index.cfm?event=onlineapplication&cid=" + $("#cid").val(), $(".uniForm").serialize(),
			function(data) {
				location.reload();
			});
	} else if(callBack) {
		$.post("index.cfm?event=onlineapplication&cid=" + $("#cid").val(), $(".uniForm").serialize(), callBack);	
	} else {
		$.post("index.cfm?event=onlineapplication&cid=" + $("#cid").val(), $(".uniForm").serialize());	
	}
}

function reloadProgressBar() {
	$("#progress_bar_area").load("index.cfm?event=view.onlineapplication.progressbar&cid=" + $("#cid").val());
}

function professionChange(el,reload) {
	var profession = el.name;
	
	if(profession == "profession") {
		$("input[name=student]").attr('checked',false);
	} else {
		$("input[name=profession]").attr('checked',false);
	}
		
	$(".field_profession em,.field_student em").hide();
	$("input[name=profession],input[name=student]").removeClass('required');
	$(".field_profession,.field_student").removeClass('error');
	$(".field_profession p.errorField,.field_student p.errorField").remove();
	
	$(".uniForm").validate().reset();
	
	if(reload) {
		$("#progress_bar_area ul").html("Loading new progress bar  ...");
		updateCurrentStep(0,reloadProgressBar);
	}
}

function toggleMemberIntroduction(choice) {		
	var req = $("input[name=required]").val();
					
	if(choice && choice.toUpperCase() == "YES") {
		$(".field_name_of_member").show();
		$(".field_member_no").show();
		
		$("#name_of_member").addClass("required");
		//$("#member_no").addClass("required");
		
		//$(".uniForm").validate();
	} else {
		$("#name_of_member").removeClass("required");
		$("#member_no").removeClass("required");
		$("#member_no").val("");
		$("#name_of_member").val("");
		
		$(".field_name_of_member").hide();
		$(".field_member_no").hide();
	}
}
