// JavaScript Document
jQuery(document).ready(function() {
						
	jQuery('.find_address').bind('click', function() { findPostCode(this); return false; } )
	jQuery('#cnt_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();
		}
		
		jQuery('#cnt_Postcode').attr({ 'placeholder' : '' });
		jQuery('#cnt_Postcode').parent().removeClass('no_label').removeClass('full').addClass('addressField').addClass('float').show();//.css({'display': 'inline-block' });
		
		jQuery('#cnt_Postcode',jQuery('.visitForm')).parent().css({'float':'left', 'width':'97%','margin-bottom':'5px'})
		jQuery('.addressField',jQuery($section)).show();
		jQuery('.enterManually',jQuery($section)).hide();
		jQuery('.inlineButtons',jQuery($section)).hide();
		jQuery('.find_address').hide();
		jQuery('.hidethis').hide();
		jQuery('.showthis').show();
		jQuery('.addressField').show();//.css({'display':'inline-block' })
		
		if ( jQuery.browser.msie ) {
			if( parseInt(jQuery.browser.version) < 8)
			{
				jQuery('.postcode').css({'margin-bottom':'0px' })
			}
		}

		//jQuery('.postcode').css({'margin-bottom':'0px' })
		jQuery('.tohideaftercomplete',jQuery($section)).hide();
		
		
	}
	
 function findPostCode(section)
 {
	
	if(jQuery('#cnt_Postcode').val() == '')
	{
		alert('No PostCode');
	}	
	else
	{	
		var reg = new RegExp("^([A-PR-UWYZ0-9][A-HK-Y0-9][AEHMNPRTVXY0-9]?[ABEHMNPRVWXY0-9]{1,2}[0-9][ABD-HJLN-UW-Z]{2}|GIR 0AA)$");
		$check = reg.test(jQuery("#cnt_Postcode").val().toUpperCase().replace(/ /gi,''));
		if($check)
		{
			if(jQuery('#cnt_House_Number').val() == '')
			{
				var html = jQuery.ajax({ 
					type: "POST",
					url: '/includes/postCodeLookup.php?getList',
					async: false,
					data: {'postCode' : jQuery('#cnt_Postcode').val() }	
				}).responseText;	
				
				if(html != 'error')
				{
					eval('list = ' + html);
					jQuery('li', jQuery('.address_match_list')).remove();
					jQuery(list).each(function(key, value) { 
						if(jQuery(".address_match_list li").length > 0)
						{
							jQuery(".address_match_list li:last").after('<li><a href="" id="'+value['Id']+'">'+value['StreetAddress']+'</a></li>');
						}
						else
						{
							jQuery(".address_match_list").append('<li><a href="" id="'+value['Id']+'">'+value['StreetAddress']+'</a></li>');
						}
					} );
					jQuery('.address_match_list').show();
					jQuery('.address_match_list li a').bind('click',function() { getAddress(jQuery(this).attr('id')); return false; } )
				}
				else
				{	
					alert('The postcode you entered was not found');
				}
				
			}
		}
		else
		{
			alert('The postcode you entered does not seem to be valid');	
		}
	}
 }

function getAddress($address)
{
	jQuery('.address_match_list').hide();

		var html = jQuery.ajax({ 
			type: "POST",
			url: '/includes/postCodeLookup.php?findAddress',
			async: false,
			data: {'id' : $address }	
		}).responseText;	
		
		if(html != 'error')
		{
			eval('theData = ' + html);
			$houseNumName = (theData[0]['BuildingNumber'] != '') ? theData[0]['BuildingNumber'] : theData[0]['BuildingName'];
			jQuery('#cnt_House_Number').val($houseNumName);
			jQuery('#cnt_street').val(theData[0]['PrimaryStreet']);
			jQuery('#cnt_town').val(theData[0]['PostTown']);
			jQuery('#cnt_county').val(theData[0]['County']);
			if(jQuery('.addressField',jQuery('.visitForm')).length > 0)
			{
				jQuery('.addressField',jQuery('.visitForm')).show();//.css({ 'display':'inline-block' } )//.show();
			}
			else
			{
				jQuery('.addressField').show();
			}
			jQuery('.inlineButtons').hide();
			
			jQuery('.tohideaftercomplete').hide();
				jQuery('#cnt_Postcode').parent().removeClass('no_label').removeClass('full').addClass('addressField').addClass('float').show();//.css({'display': 'inline-block' });
		
		
			jQuery('.enterManually').hide();
			jQuery('.inlineButtons').hide();
			jQuery('.find_address').hide();
		jQuery('.hidethis').hide();
		
		jQuery('#cnt_Postcode',jQuery('.visitForm')).parent().css({'float':'left', 'width':'97%','margin-bottom':'5px'})
	
		jQuery('.showthis').show();
			if ( jQuery.browser.msie ) {
			if( parseInt(jQuery.browser.version) < 8)
			{
				jQuery('.postcode').css({'margin-bottom':'0px' })
			}
		}


			//jQuery('.postcode').css({'margin-bottom':'0px' })
	
		
		}
}
