jQuery(document).ready(function() {
								
	jQuery('.find_address').bind('click', function() { findPostCode(this); return false; } )
	jQuery('#postcode').bind('click', function() { jQuery('.address_match_list').hide() } );
	jQuery('.enter_manually').bind('click', function() { enterManually(this); return false; } );
//	
 })

function enterManually(section)
{
	$section = jQuery(section).parent().parent();
	if(jQuery($section).is('addressForm') || jQuery($section).is('.addressForm'))
	{
		
	}
	else
	{
		$section = jQuery(section).parent().parent().parent().parent();
	}
	jQuery('.addressFields',jQuery($section)).show();
	jQuery('.find_address',jQuery($section)).hide();
	if(jQuery('.enter_manually',jQuery($section)).parent().is('td'))
	{
		jQuery('.enter_manually',jQuery($section)).parent().parent().hide();
	}
	else
	{
		jQuery('.enter_manually',jQuery($section)).parent().hide();
	}

}

function findPostCode(section)
{

	$section = jQuery(section).parent().parent();
	
	
	if(jQuery('#postcode',jQuery($section)).val() == '')
	{
		alert('Please enter your postcode');
	}
	else
	{
		var html = jQuery.ajax({ 
			type: "POST",
			url: '/includes/postCodeLookup.php?getList',
			async: false,
			data: {'postCode' : jQuery('#postcode',jQuery($section)).val() }	
		}).responseText;	
		if(html != 'error')
		{
			eval('list = ' + html);
			jQuery('li', jQuery('.address_match_list',jQuery($section))).remove();
			jQuery(list).each(function(key, value) { 
				if(jQuery(".address_match_list li",jQuery($section)).length > 0)
				{
					jQuery(".address_match_list li:last",jQuery($section)).after('<li><a href="" id="'+value['Id']+'">'+value['StreetAddress']+'</a></li>');
				}
				else
				{
					jQuery(".address_match_list ",jQuery($section)).append('<li><a href="" id="'+value['Id']+'">'+value['StreetAddress']+'</a></li>');
				}
			} );
			jQuery('.address_match_list',jQuery($section)).show();
		}
		jQuery('.address_match_list li a',jQuery($section)).bind('click',function() { getAddress(this); return false; } )
	}
}

function getAddress($section)
{
	$address = jQuery($section).attr('id')
	jQuery('.address_match_list').hide();
	$section = jQuery($section).parent().parent().parent().parent();
	if(jQuery($section).is('fieldset') || jQuery($section).is('.fieldset'))
	{
		
	}
	else
	{
		$section =jQuery($section).parent().parent().parent().parent().parent();
	}
	
	var html = jQuery.ajax({ 
		type: "POST",
		url: '/includes/postCodeLookup.php?findAddress',
		async: false,
		data: {'id' : $address }	
	}).responseText;	
	
	
	if(html != 'error')
	{
		eval('theData = ' + html);
		jQuery('#address1',jQuery($section)).val(theData[0]['BuildingNumber']);
		jQuery('#address2',jQuery($section)).val(theData[0]['PrimaryStreet']);
		jQuery('#address3',jQuery($section)).val(theData[0]['PostTown']);
		jQuery('#address4',jQuery($section)).val(theData[0]['County']);
		jQuery('.addressFields',jQuery($section)).show();
		//jQuery('.enter_manually',jQuery($section)).hide();
		jQuery('.find_address',jQuery($section)).hide();
		if(jQuery('.enter_manually',jQuery($section)).parent().is('td'))
		{
			jQuery('.enter_manually',jQuery($section)).parent().parent().hide();
		}
		else
		{
			jQuery('.enter_manually',jQuery($section)).parent().hide();
		}
	}
}

						
