function clickclear(thisfield, defaulttext) {
	if (thisfield.value == defaulttext) {
	thisfield.value = "";
	}
}
	
	function clickrecall(thisfield, defaulttext) {
	if (thisfield.value == "") {
	thisfield.value = defaulttext;
	}
}

function initialiseFindProperty (target) {

	target = typeof(target) != 'undefined' ? target : "#address";

	initialiseFindPropertyLink ();

	/*$("#reports").show();*/

	/*$("#find-property-button").hide().parent('form').submit(function(){
		return false;
	});*/
	
	$(target).autocomplete(
		"/ajax/find-property",
		{
			delay: 500,
			minChars: 5,
			matchSubset: 1,
			//matchContains:1 ,
			maxItemsToShow: 20,
			cacheLength: 10,
			//autoFill: true,
			formatItem: function (row) {
				return row[0];
			},
			onItemSelect: function (li) {

				if (li == null) {
					return alert ("No match!");
				}

				if (!li.extra) {
					return alert("You must choose a property");
				}
				
				var valRef = li.extra[0];
				
				location.href='/'+valRef;
				
				/*$("#find-property-title").html('Choose property reports for "' + li.extra[1] + '" or <a href="/reports" id="find-property-link">find another property</a>');
				
				$("#find-property").hide();
				
				$("#find-property").after('<p id="reports_loading"><img src="/img/template/loading.gif" /></p>');
				
				$.get(
					'/ajax/reports_availability',
					{valuation_reference: li.extra[0]},
					function(htmlResponse) {
						
						$("#reports_loading").remove();
						
						$("#find-property").after(htmlResponse);
						
						initialiseAddToCart();
				});

				initialiseFindPropertyLink ();*/

			}
		}
	)
	
	$(target).parents("form").submit(function(){
		
		query = $(target).val();
		query = query.replace(/^\s+|\s+$/g, '');
		
		match = query.match(/(^\d+$)/g);
		
		if(match) {
			
			alert("Please enter street name");
			return false;
			
		} else {
			
			trimmed_query = query.replace(/^[\d\s]+/g, '');
			
			if(trimmed_query.length > 0 && trimmed_query.length < 4) {
				
				alert("Please enter at least 4 characters for street name, suburb or city");
				return false;
				
			}
			
		}
		
	});

	initialiseAddToCart();

}

function initialiseAddToCart() {
	
	$("button.add-to-cart").click(function (){

		var valuation_reference = $(this).parents("form").children("input[name='valuation_reference']").val();
		
		if (!valuation_reference) {

			alert ("Please choose a property first.");
			return false;

		}

		$.post (
			'/ajax/cart',
			$(this).parents("form").serialize(),
			receive_mini_cart
		);

		return false;

	});
	
}

function initialiseFindPropertyLink () {

	$("#find-property-link").click(function () {
		$("#find-property").show();
		$("#reports").remove();
		return false;
	});

}

function initialiseMiniCart () {

	initialiseRemoveButtons ();

	$("input.remove-from-cart").click(function () {
		if(confirm("Are you sure you want to remove?")) {
			match = this.id.match(/report-(\d+)/);
	
			if (match != -1) {
	
				report_id = match[1];
	
				$.post (
					'/ajax/cart',
					{ action: 'remove', key: report_id },
					receive_mini_cart
				);
	
			} else {
	
				alert ("Report not set");
	
			}
		}
		
		return false;

	});

}

function receive_mini_cart (resp) {

	if (resp.search(/error/i) != -1) {
		alert (resp);
	} else {
		$("#mini-cart-container").html(resp);
	}

	initialiseMiniCart ();

}

function initialiseRemoveButtons () {

	$("input.remove-from-cart").each(function () {
		$(this).show();
	});

}

function initialiseCart() {
	
	$("#shoppingCart button.updateCartBtn").hide();
	
	$("#shoppingCart input.remove-from-cart").click(function() {
		
		if(confirm("Are you sure you want to remove?")) {
			
			match = this.id.match(/report-(\d+)/);
			
			if (match != -1) {

				report_id = match[1];

				$.post (
					'/ajax/cart',
					{ action: 'remove-cart', key: report_id },
					function(response) {
						
						$("#cart-container").html(response);
						
					}
				);
				
			} else {

				alert ("Report not set");

			}
			
		}
		
		return false;
	
	});
}