/**
 * New Account JavaScript file
 *
 * @author DJR
 * @package javascript
 * @version 1.0
 * @copyright Copyright © 2010 Perceptis Limited
 *
 * @depends javascript/jquery.js
 * @depends javascript/jquery-ui.js
 */


jQuery(document).ready(function($) {
	// handle post code lookup events
	function postCodeLookup() {
		$("#btnPostCodeLookup").val('Searching...');	//TODO better notification that the system is working
		$.post("ajax.php", {action: 'property_search', post_code: $("#txtPostCodeLookup").val()}, function(data) {
			handlePostCodeLookup(data);
			$("#btnPostCodeLookup").val('Search');
		}, "json");
	}
	
	
	// handle response from post code lookup
	function handlePostCodeLookup(data) {
		// check response
		if (!data || !data.post_code || !data.properties || data.error) {
			showErrorDialog(data ? data.error : null);
			return;
		}
		
		switch (data.properties.length) {
			case 0:
				showNewAddressDialog(data.post_code);
				break;
			case 1:
				// single address
				showConfirmAddressDialog(data.properties[0].id, data.properties[0].address, data.post_code);
				break;
			default:
				// multiple addresses
				showSelectAddressDialog(data.properties, data.post_code);
				break;
		}
	}
	
	
	function selectProperty(id, address) {
		$("#fldPropertyID").val(id);
		$("#AddressInfo").text(address);
	}
	
	
	function showNewAddressDialog(post_code) {
		$("#txtPostCode").val(post_code);
		
		$("#dlgNewAddress").dialog({
			modal: true,
			title: "New Address",
			buttons: {
				"Save": function() {
					var dialog = $(this);		// reference for callback function to close dialog
					
					$.post("ajax.php", {
						action: 'new_property',
						address1: $('#txtAddress1').val(),
						address2: $('#txtAddress2').val(),
						address3: $('#txtAddress3').val(),
						town: $('#txtTown').val(),
						county: $('#txtCounty').val(),
						post_code: $('#txtPostCode').val()
					}, function(data) {
						// check for errors
						if (!data || data.error || !data.id || !data.address) {
							showErrorDialog(data ? data.error : null);
						} else {
							selectProperty(data.id, data.address);
							dialog.dialog('close');
						}
					}, 'json');
				},
				"Cancel": function() {
					$(this).dialog("close");
				}
			},
			width: 380,
			height: 350
		});
	}
	
	
	function showConfirmAddressDialog(id, address, post_code) {
		$("#dlgConfirmAddress .Address").text(address);
		
		$("#dlgConfirmAddress").dialog({
			modal: true,
			title: "Confirm Address",
			buttons: {
				"No, enter manually": function() {
					$(this).dialog('close');
					showNewAddressDialog(post_code);
				},
				"Yes, use this address": function() {
					selectProperty(id, address);
					$(this).dialog('close');
				}
			},
			width: 440,
			height: 280
		});
	}
	
	
	function showSelectAddressDialog(properties, post_code) {
		var ul = $("#ulProperties"),
			li, lbl, rb, i;
		
		// set the property count labels
		$("#dlgSelectAddress .PropertyCount").text(properties.length);
		$("#dlgSelectAddress .PostCode").text(post_code);
		
		// create the list of addresses and radio buttons
		// note that the value of the radio button is the *index* of the property, not the property_id
		// also note that radio name and type is set when created as IE6/7 have issues when setting the type after creation
		ul.html("");
		for (i in properties) {
			rb = $('<input type="radio" name="property_id" />').attr({value: i});
			lbl = $("<label></label>").append(rb).append(properties[i].address);
			li = $("<li></li>").append(lbl);
			ul.append(li);
		}
		// none of the above
		rb = $('<input type="radio" name="property_id" checked="checked" />').attr({value: -1});
		lbl = $("<label></label>").append(rb).append('None of the above');
		li = $("<li></li>").append(lbl).addClass('TextGeneralBold');
		ul.append(li);
//		rb.attr({checked: true});	// done after adding to DOM for IE7
		
		
		// show the dialog
		$("#dlgSelectAddress").dialog({
			modal: true,
			title: "Select Address",
			buttons: {
				"OK": function() {
					var idx;
					
					if (!$('input:checked').length) {
						$(this).dialog('close');
						return;
					}
					
					idx = $('input:checked', this).val();
					if (idx < 0) {
						// none of the above
						$(this).dialog('close');
						showNewAddressDialog(post_code);
					} else {
						selectProperty(
							properties[idx].id,
							properties[idx].address
						);
						$(this).dialog('close');
					}
				},
				"Cancel": function() {
					$(this).dialog("close");
				}
			},
			width: 500,
			height: 400
		});
	}
	
	
	function showErrorDialog(message, title) {
		message = message ? message : 'An unknown error occured.';
		title = title ? title : 'Oops!';
		
		$("<div></div>")
			.attr("id", "ErrorDialog")
			.addClass("Dialog")
			.append($("<p></p>").html(message))
			.dialog({
				modal: true,
				title: title,
				buttons: {
					"OK": function() {
						$(this).dialog("close");
					}
				}
			});
	}
	
	
	// set up postcode lookup button click event
	$("#btnPostCodeLookup").click(function(event) {
		event.preventDefault();
		postCodeLookup($("#txtPostCodeLookup").val());
	});
	
	
	// also check for enter press in text box
	$("#txtPostCodeLookup").keypress(function(event) {
		if (event.keyCode == 13) {
			event.preventDefault();
			$("#btnPostCodeLookup").click();
		}
	});
});
