// This function runs when the page is loaded, put all your other onload stuff in here too.
$( function() {
	postCode.init(); 
	$('#Country').change(function() 
    {
		if($('#Country').val()!='GB'){
			$('#lookupAddress').hide();
			$('.required').hide();
		}
		else{
			$('#lookupAddress').show();
			$('.required').show();
		}
    });	
	
});



function validateBrochureRequest( frmId ) {
	
	var errors = new Array();
	var errorNum = 0;
	if ( document.getElementById ) {
		var emailForm = document.getElementById( frmId );
		
		var selectValues = emailForm.getElementsByTagName( "SELECT" );
		var inputValues = emailForm.getElementsByTagName( "INPUT" );
		for ( i = 0; i < selectValues.length; i++ ) {
			if ( selectValues[i].value == "" ) {
				errors[errorNum] = "Please select your " + selectValues[i].name + "";
				errorNum++
			}
		} 
		for ( i = 0; i < inputValues.length; i++ ) {
			if(inputValues[i].name!="address2" ){
				if ( ( inputValues[i].value == "" ) || ( inputValues[i].value == " " ) ) {
					
					if ( inputValues[i].type=="text" ) {
						
						
						errors[errorNum] = "Please enter your " + inputValues[i].name + "";
						errorNum++;
						
						
					}
				}
			}	
		}
		
		if ( errors.length > 0 ) {
			var errorMsg = "";
			for ( i = 0; i < errors.length; i++ ) {
				errorMsg += errors[i] + "\n";
			}
			alert( errorMsg );
			return false;
		}
	}
}




/* requires /js/general/jquery.js */
var postCode = {
	// Didn't think this through. Address1 is of course house number so this file doesn't
	// touch that input...
	options: {	
				Address1:':input[name=Address0],#Address0,#address0',
				Address2:':input[name=Address1],#Address1,#address1',
				Address3:':input[name=Address2],#Address2,#address2',
				Town:':input[name=Town],#Town,#town',
				County:':input[name=County],#County,#county',
				errorArea:'.invalidPostcode',
				lookUpButton:'.lookUpAddress',
				PostCode:'#postcode',
				linkTitle: false,
				useMagic: false 
		},
	lang: { 
			invalidPostcode:'We did not find any matching addresses. Please double check your postcode and try again, or <b>manually enter your address.</b>',
			lookUpAddress:'Find address'
			},
	init: function( params, lang ) {
	
		$.extend( this.options, params );
		$.extend( this.lang, lang );
		
		// Find postcode on keyup - not recommended, off by default
		if( this.options.useMagic)
		{
			$( this.options.PostCode ).change( function () {
				postCode.lookup();
			} );
		}

		// Place the error area span if we don't have one already
		if( !$( this.options.errorArea ).length )
		{
			$( this.options.PostCode ).after( ' <span class="invalidPostcode"></span>' );
		}
		$( this.options.errorArea ).show().click( this.lookup );

		// Place the lookup link if we don't have one already
		if (	! $( this.options.lookUpButton ).length )
		{
			$( this.options.PostCode ).after( ' <a class="lookUpAddress" ' + ((this.options.linkTitle) ? 'title="' + this.options.linkTitle + '"' : '') + '>' + this.lang['lookUpAddress'] + '</a>' );
		}
		$( this.options.lookUpButton ).show().click( this.lookup );	

	},
	lookup: function() {
		//console.log('look up');
		$(postCode.options.lookUpButton).addClass('loadingPostCode');
		//console.log(postCode.options.PostCode );
		$.getJSON( 'http://oldcrone/lookup/postcodeSeo.php?jsoncallback=?', { postcode: $( postCode.options.PostCode ).val() }, function( addr ) { 
		//$.json( 'http://secure.holidayextras.co.uk/lookup/postcode.php?postcode=CT214JF', function( addr ) {
			//var addr2=eval('('+addr+')');
			if(  addr.Address2 )
			{
				for( i in addr)
				{
					$( postCode.options[i]).val( addr[i]);
				}
				$( postCode.options.errorArea ).html('');
			}
			else
			{
				$( postCode.options.errorArea ).html( '  ' + postCode.lang['invalidPostcode'] );
			}
			$(postCode.options.lookUpButton).removeClass('loadingPostCode');
		} );
		return false;
	}
};