/* 
Javascript Document
www.electricaltrainingcourse.co.uk
Copyright 2009 PASS Ltd
$Id: postcode-finder.js 172 2009-08-06 16:31:34Z john.miller $
*/

/*
# Init Vars
*/
var te = null;
var isSearch = false;
var urlRoot = "";

/*
#Postcode format validation
#
# validates postcode format
*/
function isPostcode(value)
{
  	return /\b([A-PR-UWYZ][A-HK-Y0-9][A-HJKSTUW0-9]?[ABEHMNPRVWXY0-9]?)*[0-9][ABD-HJLN-UW-Z]{2}\b/.test(value);
}

/*
# request address matches from postcode service
*/
function getAddressFromPostcode()
{
	
	/*
	# overide tooltip
	*/
	isSearch = true;
	clearTimeout(te);
	
	/*
	# get values from user form
	*/
	var postcode = $('#cf-postcode').val().toUpperCase();
	
	/*
	# logic check values
	*/
	if(postcode.length<1)
	{
		$('#cf-postcode').focus();
		$('.js-response-error').html('Please enter your postcode to continue');
		isSearch = false;
		$('#cf-postcode').blur();
		return false;
	}
	if(!isPostcode(postcode))
	{
		$('#cf-postcode').focus();
		$('.js-response-error').html('Please enter a valid postcode to continue');
		isSearch = false;
		$('#cf-postcode').blur();
		return false;
	}
	
	/*
	# send address lookup request
	*/
	$.ajax({
		type: "POST",
		url: urlRoot+"/ajax/get-address-data-by-postcode/"+postcode,
		success: function(res){
			try
			{
				var str = '';
				var target = $('#address-selector');
				var recordCount = parseInt($(res).find("recordcount").text());
				if(recordCount>0)
				{
					target.html('<p>Please <strong>select your address from the list below</strong>. If your address is not shown please check the postcode is correct or enter the address manually below.</p>');
				}
				else
				{
					target.html('<p>Sorry, your address was not found. Please check the postcode is correct or enter the address manually below.</p>');
				}
				
				$(res).find("record").each(function()
				{
					var record = $(this);
					var lineid = record.find("id").text();
					var line = record.find("line").text();
					target.append('<a href="#" onclick="getAddressFromId(\'' + lineid + '\');return false;" title="Set my address to ' + line + '" >' + line + '</a>');
				});
				target.slideDown(200);
				
				$('div.hidden-address').show();
				
				return true;
			} 
			catch(er)
			{
				$('.js-response-error').html(er.message);
				$('#address-selector').slideUp(200);
				return false;
			}
		}
	});	
}

/*
# get address details by selected index entry 
# from postcode search
*/
function getAddressFromId(addressId)
{
	
	/*
	# handle error in service
	*/
	if(addressId.length<1)
	{
		$('.js-response-error').html('Sorry, there was a problem. Please enter your details manually');
		$('#address-selector').slideUp(200);
		return false;
	}
	
	/*
	# send address detail request
	*/
	$.ajax({
		type: "POST",
		url: urlRoot+"/ajax/get-full-address-by-id/"+addressId,
		success: function(res){
			try 
			{
				$(res).find("record").each(function()
				{
					var record = $(this);
					
					$('#cf-postcode').val(record.find("postcode").text());
					$('#cf-add1').val(record.find("line1").text());
					$('#cf-add2').val(record.find("line2").text());
					$('#cf-add3').val(record.find("line3").text());
					
					var tn = record.find("town").text();
					if(tn.length<1)tn=record.find("county").text();
					$('#cf-city').val(tn);
					
					$('#cf-country').val(record.find("country").text());
					
				});
				$('#address-selector').slideUp(200);
				return true;
			} 
			catch(er)
			{
				$('.js-response-error').html(er.message);
				$('#address-selector').slideUp(200);
				return false;
			}
		}
	});	
}

/*
# we need to trigger all the timeout calls throuigh one 
# finction so we can check the script status at acrual call 
# time, rather than when the timeout is set
*/
function doSlideUp(time)
{
	if(isSearch==false)
	$('div#address-selector').slideUp(time)
}

/*
# initially, hide address elements and prepare tooltip
# listners
*/
$(document).ready(function()
{
	
	/*
	# hide the address detail fields
	*/
	$('div.hidden-address').hide();
	
	/*
	# populate tooltip, we do this in javascript so the tips are hidden from
	# older browsers, where they will be meaningless...
	*/
	$('div#address-selector').html('<p><strong>Did you know?</strong> You can enter your postcode, then use the Find my Address link to complete your address details automatically.</p>');
	
	/*
	# attatch watchers to hover states
	*/
	$('input.postcode').hover(
		function(){
			if(isSearch==false)
			{
				$('div#address-selector').slideDown(150);
				clearTimeout(te);
			}
		},
		function()
		{
			te = setTimeout("doSlideUp(150)",1500);
		}
	);
	
	/*
	# attatch watchers on focus
	*/
	$('input.postcode').focus(
		function(){
			if(isSearch==false)
			{
				$('div#address-selector').slideDown(150);
				clearTimeout(te);
			}
		}
	);
	
	/*
	# attatch watchers on blur
	*/
	$('input.postcode').blur(
		function()
		{
			if(isSearch==false)te = setTimeout("doSlideUp(150)",1);
		}						 
	);
	
});