function saveComplete (data, form)
{
	if (data.status === 200)
	{
		$(form).find("input[type='submit']").removeAttr('disabled').val('Save');
		
		$(form).find("span.status").text(data.message);
		
		setTimeout(function () {
			$(form).find("span.status").fadeOut('fast', function (){
				$(this).empty();
			});
		}, 2000);
	}
	else
	{
		$(form).find("span.status").text(data.message);
		$(form).find("input[type='submit']").removeAttr('disabled').val('Save');
	}
}

function showSmallBox () {
	$('div.smallBox').fadeIn(600);
}

function showNotifyBox () {
	$('div#help_box').fadeIn(600);
}

function instructPrevious (data){
	$("input#ReferenceNumber").val(data.reference_number);
	$("input#VendorsName").val(data.vendors_name);
	$("textarea#VendorsAccessDetails").val(data.vendors_access_details);
	$("input#VendorsAddress").val(data.vendors_address);
	$("input#VendorsTown").val(data.vendors_town);
	$("select#VendorsCounty option[value="+data.vendors_county+"]").attr("selected", "selected");
	$("input#VendorsPostcode").val(data.vendors_postcode);
	$("select#PropertyType option[value="+data.property_type+"]").attr("selected", "selected");
	$("input#ApproxAge").val(data.approx_age);
}

function instructorPrevious (data)
{
	$("input[name='instructors_email']").val(data.instructors_email);
	$("input[name='instructors_name']").val(data.instructors_name);
	$("input[name='instructors_company']").val(data.instructors_company);
	$("textarea[name='instructors_address']").val(data.instructors_address);
}

$(document).ready(function () {
	
	$("select#previous").change(function () {
		$.post(window.href, 'instruction='+this.value, instructPrevious, 'json');
	});
	
	$("select#previousinstructors").change(function () {
		$.post(window.href, 'previousinstructors='+this.value, instructorPrevious, 'json');
	});

	$('div.advanced-options').hide();
	$('div#help_box').hide();
	$('div.smallBox').show();
	$('a.close').click(function() {
		$('div#help_box').fadeOut(600, showSmallBox);
		return false;
	});
	
	$('div.smallBox').css('cursor', 'pointer').click(function () {
		$('div.smallBox').fadeOut(600, showNotifyBox);
	});
	
	$('a.advanced').click(function () {
		if ($('div.advanced-options').hasClass('shown')) {
			$('div.advanced-options').removeClass('shown').slideUp(500);
		} else {
			$('div.advanced-options').addClass('shown').slideDown(400);
		}
		return false;
	});
	
	$("a.view-details").click(function(e){
		// prevent the default action
		e.preventDefault();
		
		// get the surveyor_id from the link attribute
		var surveyor_id = $(this).attr("href");
		
		var d = new Date();
		var t = d.getTime();
		
		// check if there is the open class in the link
		if ($(this).hasClass('open'))
		{
			// remove the open class from the link
			$(this).removeClass('open');
			
			// remove the more details section
			$("#more-details-"+surveyor_id).remove();
		}
		else
		{
			// add the open class to the link, select the parents and add the next row after the clicked row.
			$(this).addClass('open').parents("tr").after('<tr class="more_details" id="more-details-'+surveyor_id+'"></tr>');
			
			// load the remote page into the more-details div.
			$("#more-details-"+surveyor_id).load(base_url+"vals/viewdetails/" +surveyor_id+ "/?" + t);
		}
	});
	
	$("a.showmore").click(function (e) {
		e.preventDefault();
		
		// show the tr with the id matching the links hash
		$(this.hash).toggle();
	});
	
	$("a.activate-account").click(function (e) {
		e.preventDefault();
		var that = this;
		$.get(this.href, null, function (data) {
			$(that).parent('li').text(data.message);
		}, 'json');
	});
	
	$("a.cancel-admin").click(function (e) {
		e.preventDefault();
		var that = this;
		$.get(this.href, null, function (data) {
			$(that).parent('li').text(data.message).prev('li').remove();
		}, 'json');
	});
	
	$("a.delete-instruction").click(function (e) {
		
		e.preventDefault();
		if (confirm('Are you sure you wish to delete this instruction?'))
		{
			$(this).text('Deleting...');
			var that = this;
			$.get(this.href, null, function(data) {
				if ("message" in data)
				{
					$(that).parents('tr').fadeOut("slow", function() {
						$(this).remove();
					});
				}
			}, 'json');
		}
	});
	
	$("form.private-notes").submit(function (e) {
		e.preventDefault();
		
		$("span.status", this).show();
		
		$(this).find('input[type="submit"]').attr('disabled', 'disabled').val('Saving...');
		
		var that = this;

		$.post(window.location.pathname, $(this).serialize(), function (data) {
			saveComplete(data, that);
		}, 'json');
	});
});

