$(document).ready(function(){
	
	$('input[name=vehicle_regNumberKnown]').bind('click', function(e) {
		resetBike();
		if ($(this).val() == 'Y') {
			$('#regKnownInput').show();
			$('#vehicle_yearOfManufacture2').val('');
			deleteSession('vehicle_yearOfManufacture2');
			$('#regNotKnownInput').hide();
		} else {
			$('#vehicle_yearOfManufacture1').val('');
			deleteSession('vehicle_yearOfManufacture1');
			$('#regKnownInput').hide();
			$('#regNotKnownInput').show();
			$('#vehicle_regNumber').val('');
		}
	});
	
	// uppercase the reg number
	$('input[name=vehicle_regNumber]').bind('keyup', function(e) {
		$('#vehicle_regNumber').val($('#vehicle_regNumber').val().toUpperCase());
	});
});

$(document).ready(function(){
	$('#getVehicleBtn').bind('click', function(e) {
		e.preventDefault();
		$('#vehicleSelector').hide();
		// Is the vehicle reg number known ie Y N
		// if so retrieve it from the page and get the bike details
		
		//remove 4" the quote link from the breadcrumb trail so they have to validate again
		
		 // remove the link on the quote trail to step 4
		$('map>area[coords="291,0,393,62"]').remove();
		updateSession('stepcompletion_4',false);
	    updateSession('stepcompletion_3',false);
	    
	    //if they have had a quote change the breadcrumb image

			mysrc = $('img[alt="Quote progression"]').attr('src');
			theend  = mysrc.length;
			thepart = mysrc.substr(0,(theend-5));
			thepart = thepart+'3.gif';
			//change image
			$('img[alt="Quote progression"]').attr('src',thepart);
	
		
		var regNumberKnown = ($("input[@name='vehicle_regNumberKnown']:checked").val());
		//add spinning image
		$('div[@name=loader_getVehicleBtn]').addClass('loading');
		if (regNumberKnown == "Y"){
			
			// need to get the vehicle reg number from the page 
			// check its not blank and do an ajax call to get the address details
			var vehicleRegNumber = $.Replace($('#vehicle_regNumber').val(),' ','','ALL');
			
			if (vehicleRegNumber.trim() != ''){
				$.ajax({
					url: 'index.cfm?go=ajax.getBike&registration=' + vehicleRegNumber.toUpperCase(), 
					cache: false,
				    dataType: "json",
					success: function(data) {
						//DP_Debug.dump(data);
						//add the option to the vehicle_selector selection list
						if(data.MATCHES == 1){
							var options = '<option value="">' + 'Please select' + '</option>';
							optionText = data.MAKE + ' ' + data.MODEL + ' ' + data.ENGINE_CC + 'cc ' + data.FROM_TO;
							options += '<option value="' + data.ABI_CODE + '">' + optionText + '</option>';
							//store single item returned in session for re-rendering of the drop down
							updateSession('vehicleOption',data.ABI_CODE);
							updateSession('vehicleOptionText',optionText);
							$('#vehicle_selector').html(options);
							$('#vehicleSelector').show();
							$('#vehicle_selector').trigger('change');
							$('#getVehicleBtn').focus();
						}
						else{
							resetBike();							
							showModal('Bike lookup error','Sorry, we could not find your bike, please try again');
						}
					},
					complete: function(){
						$('div[@name=loader_getVehicleBtn]').removeClass('loading');
					}
				});
			}
			else{
				resetBike();
				showModal('Bike lookup error','Please enter a registration number and try again');
				$('div[@name=loader_getVehicleBtn]').removeClass('loading');
			}
		
		}
		else{
			var manufacturer = $('#vehicle_manufacturer').val();
			var model = $('#vehicle_model').val();
			var vehicle_engineSizeCC = $('#vehicle_engineSizeCC').val();
			var vehicle_yearOfManufacture = $('#vehicle_yearOfManufacture1').val();
			//all items must be supplied in order to do the lookup
			if ( $.Len($.Trim(manufacturer)) && $.Len($.Trim(model)) ) {	
				// get the coldfusion query back
				$.ajax({
					url: 'index.cfm?go=ajax.getBikeUsingManfandModel&manufacturer='+ manufacturer +'&model=' + model + '&vehicle_engineSizeCC=' + vehicle_engineSizeCC + '&yearOfManufacture=' + vehicle_yearOfManufacture,
					cache: false,
				    dataType: "json",
					success: function(data) {
						var options = '<option value="">' + 'Please select' + '</option>';
						if(data.VEHICLEDETAIL.recordcount > 0){
							//store single item returned in session for re-rendering of the drop down
							
							lstVehicleOptions = '';
							lstVehicleOptionsText = '';
				         	//DP_Debug.dump(data.VEHICLEDETAIL);
					     	for(x=0; x<data.VEHICLEDETAIL.recordcount; x++) {
					     		optionText = data.VEHICLEDETAIL.data.manufacturer[x] + ' ' + data.VEHICLEDETAIL.data.model[x] + ' ' + data.VEHICLEDETAIL.data.enginecc[x] + 'cc ' + data.VEHICLEDETAIL.data.productionyears[x];
					        	options += '<option value="' + data.VEHICLEDETAIL.data.abicode[x] + '">' + optionText + '</option>';
					        	lstVehicleOptions = $.ListAppend(lstVehicleOptions,data.VEHICLEDETAIL.data.abicode[x],'*^*');
					        	lstVehicleOptionsText = $.ListAppend(lstVehicleOptionsText,optionText,'*^*');
					     	}
					     	
					     	updateSession('lstVehicleOptions',lstVehicleOptions);
					     	//IE can only handle a certain amount of info via 'get'
					     	if ($.Len(lstVehicleOptionsText) > 1500) {
					     		var numExtraChars = $.Len(lstVehicleOptionsText) - 1500;
								updateSession('lstVehicleOptionsText',$.Left(lstVehicleOptionsText,1500));
					     		updateSession('lstVehicleOptionsText2',$.Right(lstVehicleOptionsText,numExtraChars));
					     	}
					     	else {
					     		updateSession('lstVehicleOptionsText',lstVehicleOptionsText);
					     		updateSession('lstVehicleOptionsText2','');
					     	}
					     	$('#vehicle_selector').html(options);
					     	$('#vehicleSelector').show();
					     	$('#getVehicleBtn').focus();
					     	//remove the spinning loading image
							$('div[@name=loader_getVehicleBtn]').removeClass('loading');
						}
						else{
							resetBike();							
							showModal('Bike lookup error','Sorry, we could not find your bike, please try again');
						}
					},
					complete: function(){
						$('div[@name=loader_getVehicleBtn]').removeClass('loading');
					}
				});
				
			} else {
				$('div[@name=loader_getVehicleBtn]').removeClass('loading');
				showModal('Bike lookup error','Please enter manufacturer and model');
			}
		}
			
	});

});

function resetBike() {
	var options = '<option value="">' + 'Please select' + '</option>';
	$('#vehicle_selector').html(options);
	deleteSession('vehicleOption');
	deleteSession('vehicleOptionText');
	deleteSession('lstVehicleOptions');
	deleteSession('lstVehicleOptionsText');
	$('#vehicle_selector').trigger('change');
	//$('#vehicle_regNumber').val('');
	$('#vehicleSelector').hide();
}

// Renewal page diplay new total premium
$(document).ready(function(){
	var form = ($('div[@class=boxOuter]'));
	if (form[0].id == 'step22'){	
		$('.metaData+p>input[type=checkbox]').click(function(event) {

		  // set the new premium to the original one ( this is set in dsp_renewalStep2)
		  var newPremium = originalPremium;

		  //Loop over all the checkboxes if checked add to premium
		  $("input[type=checkbox]").each( 
		    function() { 
			  	if(this.id == 'renewal_addLegal' && $('#'+this.id).attr('checked') == true )
			  		newPremium = newPremium+legal;
			    if(this.id == 'renewal_addUKBreakdown' && $('#'+this.id).attr('checked') == true)
			    	newPremium = newPremium+breakdownUK;
			  	if(this.id == 'renewal_addPersonal' && $('#'+this.id).attr('checked') == true)
			  		newPremium = newPremium+personalAccident;	
			  	if(this.id == 'renewal_addHelmet' && $('#'+this.id).attr('checked') == true)
			  		newPremium = newPremium+helmetAndLeathers;
			  	if(this.id == 'renewal_key1' && $('#'+this.id).attr('checked') == true)
			  		newPremium = newPremium+key1;
			  	if(this.id == 'renewal_key2' && $('#'+this.id).attr('checked') == true)
			  		newPremium = newPremium+key2;
		   } 
		  );
		  	
		  thepremium=Math.round(newPremium*100)/100;
		  
		  // set the premium on the screen to the new premium
		  $('#premium').text(thepremium.toFixed(2));
		});
	}
});


