$(function() 
{

	// hide hidden text boxes
	$(".hidden").slideUp();
	
	var loader = jQuery('<div id="loader"><img src="/images/admin/indicator.gif" alt="loading..." /></div>').css({position: "relative", top: "1em", left: "25em"}).appendTo("body").hide();
	
	jQuery().ajaxStart(function() {
			loader.show();
		}).ajaxStop(function() {
			loader.hide();
		}).ajaxError(function(a, b, e) {
			throw e;
	});
	
	var v = jQuery("#apply_form").validate({
		rules: {
			txt_position: "required",
			mnu_title: "required",
			txt_name: "required",
			txt_address: "required",
			txt_postcode: "required",
			txt_email: {required: true,email: true},
			chk_comfirm: "required"
		},
		messages: {
			txt_position: "Please enter the position you are applying for",
			mnu_title: "Please select your title",
			txt_name: "Please enter your full name",
			txt_address: "Please enter your address",
			txt_postcode: "Please enter your postcode",
			txt_email: "Please enter a valid email address",
			chk_comfirm: "Please confirm the above information is correct"
		},
		submitHandler: function(form) {
			jQuery(form).ajaxSubmit({
				target: "#form_result",
				success: function(data){
                    document.location.href = '/about/our_people/current_vacancies/job_apply_equal_opps';
					//$("#debug).html(data);
				}
			});
			$('#confirmation').hide();
			
			$('#cmd_submit_education').hide();
			$('#cmd_submit_past_employment').hide();
			$('#cmd_submit_job_application').hide();
		}
	});
	
	jQuery("#reset").click(function() {
		v.resetForm();
	});
	
	
	$("#cmd_submit_past_employment").click(function()
	{
		
		if ($("#txt_past_employer_name").val().length > 0)
		{
		
			$.post("/files/ajax/apply_add_past_employer.php", { 
			app_ref: $("#txt_app_ref").val(),
			txt_past_employer_name: $("#txt_past_employer_name").val(),
			txt_past_employer_address: $("#txt_past_employer_address").val(),
			txt_past_job_title: $("#txt_past_job_title").val(),
			txt_past_from_date: $("#txt_past_from_date").val(),
			txt_past_to_date: $("#txt_past_to_date").val(),
			txt_past_salary: $("#txt_past_salary").val(),
			txt_past_reason: $("#txt_past_reason").val()},
			  function(data)
			  {
				if ( $("#txt_app_ref").val() == 0)
				{
					$("#txt_app_ref").val(data.app_ref);
				}
				
				$("#tb_employment > tbody").append('<tr><td>'+data.employer_name+'</td><td>'+data.job_title+'</td><td>'+data.dates+'</td><td>'+data.salary+'</td><td><a href="#" class="remove_employer" ref="'+data.emp_id+'"><img src="/images/structure/delete.png" border="0" /></a></td></tr>');
				
				$(".remove_employer").click(function()
				{
					$.post("/files/ajax/apply_remove_past_employer.php", {remove_ref: $(this).attr('ref')},function(data){ });
					$(this).parent().parent().remove();
					return false;
				});
				
				$("#txt_past_employer_name").val('');
				$("#txt_past_employer_address").val('');
				$("#txt_past_job_title").val('');
				$("#txt_past_from_date").val('');
				$("#txt_past_to_date").val('');
				$("#txt_past_salary").val('');
				$("#txt_past_reason").val('');
				
				//alert(data.status);
				
			  }, "json");
		}
		
		return false;

	});
	
	
	
	
	$("#cmd_submit_education").click(function()
	{ 
		if ($("#txt_education_name").val().length > 0)
		{
			$.post("/files/ajax/apply_add_qual.php", { 
			app_ref: $("#txt_app_ref").val(),
			txt_education_name: $("#txt_education_name").val(),
			txt_education_qualification: $("#txt_education_qualification").val()},
			  function(data)
			  {
				if ( $("#txt_app_ref").val() == 0)
				{
					$("#txt_app_ref").val(data.app_ref);
				}
				
				$("#tb_qualifications > tbody").append('<tr><td>'+data.txt_education_name+'</td><td>'+data.txt_education_qualification+'</td><td><a href="#" class="remove_qual" ref="'+data.qual_id+'"><img src="/images/structure/delete.png" border="0" /></a></td></tr>');
		
				$(".remove_qual").click(function()
				{
					$.post("/files/ajax/apply_remove_qual.php", {remove_ref: $(this).attr('ref')},function(data){ });
					$(this).parent().parent().remove();
					return false;
				});
		
				$("#txt_education_name").val('');
				$("#txt_education_qualification").val('');
				
				//alert(data.status);
				
			  }, "json");
		}
	
		return false;
	});

}); // jquery
