var Qas = Class.create();   

Qas.prototype = {

	qasLine1FieldId : null,
	qasLine2FieldId : null,
	qasTownFieldId : null,
	qasCountyFieldId : null,
	qasPostcodeFieldId : null,

	initialize : function(qasLine1FieldId, qasLine2FieldId, qasTownFieldId, qasCountyFieldId, qasPostcodeFieldId)
	{
		this.qasLine1FieldId = qasLine1FieldId;
		this.qasLine2FieldId = qasLine2FieldId;
		this.qasTownFieldId = qasTownFieldId;
		this.qasCountyFieldId = qasCountyFieldId;
		this.qasPostcodeFieldId = qasPostcodeFieldId;
    },

	addressSearch : function()
	{
		var errors   = false;
		var number   = $F('qasNumber');
		var postcode = $F('qasPostcode');
		if (number == "") {
			$('qasError').style.display = "block";
			$('qasErrorNumber').style.display = "block";
			errors = true;
		}
		if (postcode == "") {
			$('qasError').style.display = "block";
			$('qasErrorPostcode').style.display = "block";
			errors = true;
		}
		if (!errors) {
			$('qasError').style.display = "none";
			$('qasErrorNumber').style.display = "none";
			$('qasErrorPostcode').style.display = "none";
			$('qasPickList').style.display = "none";
			var qasAjax = new Ajax.Request(
				'/qas/search/'+number+'/'+postcode,
				{
					method: 'get',
					onLoading: this.addressSearchOnLoading.bindAsEventListener(this),
					onComplete: this.addressSearchOnComplete.bindAsEventListener(this),
					onFailure: this.addressSearchOnFailure.bindAsEventListener(this)
				}
			);
		}
	},
	
	addressSearchOnLoading : function(transport)
	{
		this.showLoading();
		this.setMessage("Searching for address...");
	},
	
	addressSearchOnComplete : function(transport)
	{
		$('qasNumber').value = "";
		$('qasPostcode').value = "";
		
		this.hideLoading();
		this.setMessage("");

		var response  = transport.responseXML.documentElement;
		var addresses = response.getElementsByTagName("address");

		if (addresses.length == 1) {
			moniker = addresses[0].getElementsByTagName("moniker");
			moniker = moniker[0].firstChild.data;
			this.addressPick(moniker);

		} else if (addresses.length > 1) {
			var ul = new Element('ul');
			var options = new Array();
			for ( i=0 ; i < addresses.length ; i++ ) {
				try {
					moniker = addresses[i].getElementsByTagName("moniker");
					moniker = moniker[0].firstChild.data;
					picklist = addresses[i].getElementsByTagName("picklist");
					picklist = picklist[0].firstChild.data;
					new Insertion.Bottom(ul, '<li><a href="#" onclick="qas.addressPick(\''+moniker+'\')">'+picklist+'</a></li>');
				} catch (er) {
					this.addressSearchOnFailure(transport);
				}
			}
			
			$('qasPickList').style.display = "block";
			$('qasPickList').innerHTML = "";
			
			var span = document.createElement('span');
			new Insertion.Bottom(span, 'Please select your address:');
			$('qasPickList').appendChild(span);
			$('qasPickList').appendChild(ul);

		} else {
			this.addressSearchOnFailure(transport);
		}	
	},
	
	addressSearchOnFailure : function(transport)
	{	
		this.hideLoading();
		this.setMessage("Address could not be found.");
	},
	
	addressPick : function(moniker)
	{
		var qasAjax = new Ajax.Request(
			'/qas/pick/'+moniker, 
			{
				method: 'get', 
				onLoading: this.addressPickOnLoading.bindAsEventListener(this),
				onComplete: this.addressPickOnComplete.bindAsEventListener(this)
			}
		);
	},
	
	addressPickOnLoading : function(transport)
	{
		$('qasPickList').innerHTML = "";
		$('qasPickList').style.display = "none";
		
		this.showLoading();
		this.setMessage("Retrieving address...");
	},

	addressPickOnComplete : function(transport)
	{
		var response = transport.responseXML.documentElement;
		var address  = response.getElementsByTagName("address");

		try {

			line1 = address[0].getElementsByTagName("line1");
			if (line1[0].firstChild != null) $(this.qasLine1FieldId).value = line1[0].firstChild.data;
			
			line2 = address[0].getElementsByTagName("line2");
			if (line2[0].firstChild != null) $(this.qasLine2FieldId).value = line2[0].firstChild.data;
			
			town = address[0].getElementsByTagName("town");
			if (town[0].firstChild != null) $(this.qasTownFieldId).value = town[0].firstChild.data;
			
			county = address[0].getElementsByTagName("county");
			if (county[0].firstChild != null) $(this.qasCountyFieldId).value = county[0].firstChild.data;
			
			postcode = address[0].getElementsByTagName("postcode");
			if (postcode[0].firstChild != null) $(this.qasPostcodeFieldId).value = postcode[0].firstChild.data;

		} catch (er) {}

		this.hideLoading.defer();
		this.setMessage.defer("");
		return true;
	},
	
	showLoading : function()
	{
		$('qasLoading').style.display = "block";
		return true;
	},
	
	hideLoading : function()
	{
		$('qasLoading').style.display = "none";
		return true;
	},

	setMessage : function(message)
	{
		if ($('qasMessage').firstChild != null) {
			Element.remove($('qasMessage').firstChild);
		}
		$('qasMessage').appendChild(document.createTextNode(message));
		return true;
	}

};
