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();
	}
	
	if(jQuery(section).parent().parent().parent().parent().parent().parent().parent().parent().parent().attr('class') == 'banner_left')
	{
	//	jQuery('.banner_left').css({'height':'430px' })
	}
	
	try
	{
		jQuery('#fancybox-content', top.document).css({'height':'720px' })
		parent.centerFancyBox();
		
	}
	catch(err)
	{
	}

}

function findPostCode(section)
{

	$section = jQuery(section).parent().parent();
	if(jQuery($section).is('fieldset') || jQuery($section).is('.fieldset'))
	{
		
	}
	else
	{
		$section = jQuery(section).parent().parent().parent().parent();
	}
	
	if(jQuery('.postcode',jQuery($section)).val() == '')
	{
		alert('No PostCode');
	}
	else
	{
		if(jQuery('.house',jQuery($section)).val() == '')
		{
			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; } )
		}
		else
		{
			/*
			var html = jQuery.ajax({ 
				type: "POST",
				url: '/includes/postCodeLookup.php?findNumberPostcode',
				async: false,
				data: {'postCode' : jQuery('.postcode',jQuery($section)).val(), 'building': jQuery('.house',jQuery($section)).val() }	
			}).responseText;	
			
			if(html != 'error')
			{
				eval('theData = ' + html);
				jQuery('.house',jQuery($section)).val(theData[0]['BuildingNumber']);
				jQuery('.street',jQuery($section)).val(theData[0]['PrimaryStreet']);
				jQuery('.town',jQuery($section)).val(theData[0]['PostTown']);
				jQuery('.county',jQuery($section)).val(theData[0]['County']);
				jQuery('.addressFields',jQuery($section)).show();
			jQuery('.enter_manually',jQuery($section)).hide();
			}
			*/
		}
	}
}

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('.house',jQuery($section)).val(theData[0]['BuildingNumber']);
		jQuery('.street',jQuery($section)).val(theData[0]['PrimaryStreet']);
		jQuery('.town',jQuery($section)).val(theData[0]['PostTown']);
		jQuery('.county',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();
		}
		
		if(jQuery($section).parent().attr('class') == 'banner_left')
		{
	//		jQuery('.banner_left').css({'height':'430px' })
		}
		
		try
		{
			jQuery('#fancybox-content', top.document).css({'height':'720px' })
			parent.centerFancyBox();
			
		}
		catch(err)
		{
		}
	}
}
