
$(document).ready(function(){
	
	
	$('input[id*=addressline]').keyup(function(e) {
		checkCharsAfter($(this).attr('id')," -'1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz","capitalize");
	});
	
	$('#proposer_address_postCode').keyup(function(e) {
		checkCharsAfter($(this).attr('id')," 1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz","upper");
	});
	
	$('#getAddressBtn').bind('click', function(e) {
		e.preventDefault();
		$('#addressLines').hide();
		// need to get the post code from the page 
		// check its not blank and do an ajax call to get the address details
		var postcode = $('#proposer_address_postCode').val();
		var nameOrNumber = $('#proposer_address_nameOrNumber').val();

		//  validate that the postcode is in correct format 
		// if it is put in correct upper case and add space if necessary
		var validPostCode = checkPostCode(postcode);
		
		if (validPostCode && $.Len($.Trim(nameOrNumber))) {	
			
		    // update the screen with the correctly formatted value
		    $('#proposer_address_postCode').val(validPostCode);

		    
		    // update the session variable with the new correctly formatted value
		    updateSession('proposer_address_postcode',validPostCode);
		    
		     //add spinning image
			$('div[@name=loader_getAddressBtn]').addClass('loading');
			
			if ( postcode.trim() != ''){
				$.ajax({
					url: 'index.cfm?go=ajax.getPostalAddress&postcode=' + validPostCode + '&nameOrNumber='+ nameOrNumber,
					cache: false,
				    dataType: "json",
					success: function(data) {
						//add the option to the proposer_address_selector selection list
						if( data != ''){
							var options='';
							options += '<option value="' + data + '">' + data + '</option>';
							
								//$('#proposer_address_selector').html(options);
								$('#proposer_address_addressline1').val(data.ADDRESSLINE1);
								//trigger events for validation and session updating
								$('#proposer_address_addressline1').trigger('keyup');
								$('#proposer_address_addressline1').trigger('blur');
								$('#proposer_address_addressline2').val(data.ADDRESSLINE2);
								$('#proposer_address_addressline2').trigger('keyup');
								$('#proposer_address_addressline2').trigger('blur');
								
								$('#proposer_address_addressline3').val(data.ADDRESSLINE3);
								$('#proposer_address_addressline3').trigger('keyup');
								$('#proposer_address_addressline3').trigger('blur');
								
								$('#proposer_address_addressline4').val(data.ADDRESSLINE4);
								$('#proposer_address_addressline4').trigger('keyup');
								$('#proposer_address_addressline4').trigger('blur');
								
								if(data.NAMEORNUMBER=='Not Found') {
									showModal('Address lookup error','Sorry, we could not find your address, please try again or enter manually');
								}
									
							    // show the address lines
							    $('#addressLines').show();
							    $('#getAddressBtn').focus();
							    // update the hidden value with the new postcode
		    					$('#hidden_proposer_address_postCode').val(validPostCode);
						}		
				
					},
					complete: function(){
						//remove the spinning loading image
					    $('div[@name=loader_getAddressBtn]').removeClass('loading');
					}
				});
			}
			else{
				//remove the spinning loading image
				$('div[@name=loader_getAddressBtn]').removeClass('loading');
			}
		}
		else{
		// not a valid postcode
			var chrErrorMessage = "<b>Please correct the following:</b><br>";	
			if(!validPostCode) {
				chrErrorMessage = $.ListAppend(chrErrorMessage,'The postcode entered is not valid','<br />');
			}
			if (!$.Len($.Trim(nameOrNumber))) {
				chrErrorMessage = $.ListAppend(chrErrorMessage,'Please enter a house number or name','<br />');
			}
			showModal('Address lookup error',chrErrorMessage);
		}
	});
	
	$('#ridersureLoginBtn').bind('click', function(e) {
		e.preventDefault();
		var message='';
		if(!$.Len($.Trim($('#ridersureBrokerRef').val()))){	
			message = $.ListAppend(message,'Please enter an valid Agency Number','<br />');
		}
		
		if(!$.Len($.Trim($('#ridersureUserName').val()))){
			message = $.ListAppend(message,'Please enter a username','<br />');
		}
		if(!$.Len($.Trim($('#ridersurePassword').val()))){
			message = $.ListAppend(message,'Please enter a password','<br />');
		}
		
		if($.Len(message)){
			showModal("Error",message);
		}
		else{
			//window.location = 'index.cfm?go=quote.step1';
			$('#ridersureLoginForm').submit(); 
		}
		//validateField();
		//validateField();
		
	});
	
	$('#ridersureFindQuoteBtn').bind('click', function(e) {
		e.preventDefault();
		var message='';
		if(!$.Len($.Trim($('#ridersureWebRef').val())) || $.Len($.Trim($('#ridersureWebRef').val())) < 11 ){	
			message = $.ListAppend(message,'Please enter an valid quote reference','<br />');
		}
		
		if($('#ridersureDateOfBirth_dd').val() == "dd" || $('#ridersureDateOfBirth_mm').val() == 'mm' || $('#ridersureDateOfBirth_yyyy').val() == 'yyyy' ){
			message = $.ListAppend(message,'Please enter a date of birth in the format dd mm yyyy','<br />');
		}
		
		if(!$.Len($.Trim($('#ridersureDateOfBirth').val()))){
			message = $.ListAppend(message,'Please enter a valid date of birth in the format dd mm yyyy','<br />');
		}
		if(!$.Len($.Trim($('#ridersurePostcode').val()))){
			message = $.ListAppend(message,'Please enter a postcode','<br />');
		}
		
		if($.Len(message)){
			showModal("Error",message);
		}
		else{
			//window.location = 'index.cfm?go=quote.step1';
			$('#ridersureFindQuoteForm').submit(); 
		}
		//validateField();
		//validateField();
		
	});
	
    $('#ridersureNewQuoteBtn').bind('click', function(e) {
		e.preventDefault();
			window.location = 'index.cfm?go=quote.step1&start=yes';
	});
	
	$('#proposer_bikeLicence_type').bind('change', function(e) {
		e.preventDefault();
		if ($('#proposer_bikeLicence_type').val() == "P"){
			//need to make an ajax call to set the session variable vehicle_excludesPillion to Y
			$.get( 'index.cfm?go=ajax.setVehicle_excludesPillion', {value:"Y"}, function(data){})
		}
		else{
			$.get( 'index.cfm?go=ajax.setVehicle_excludesPillion', {value:"N"}, function(data){})
		}
		
		
	});
	
	

});


