netdirector.util.namespace('netdirector.codeweavers.insurance');

netdirector.codeweavers.insurance = {
	formElId : '#cwInsuranceForm',
	vehicleList: {},
	hasVehicles: false,
	genericErrorMsg: 'Insurance is not currently available on this product.',

	onReady: function() {
		this.submit();
	},

	submit: function(formId) {
		if (!this.hasVehicles) {
			$('.cwInsuranceDetailsNeeded').hide();
			$('.cwInsuranceDetailsUpdate').hide();
			$('.cwInsuranceQuotes table').hide();
			$('.cwInsuranceQuotes .message').text(this.genericErrorMsg);
			$('.cwInsuranceQuotes').show();
			return;
		}


		// If no formId is defined, use the default
		if (typeof formId == 'undefined') {
			formId = this.formElId;
		} else {
			formId = '#'+ formId;
		}

		$(formId +' .cwVehicleDetails').val(JSON.stringify(this.vehicleList));

		var target = netdirector.baseUrl + '/' + netdirector.franchiseUrl + 'frontend-operations/codeweavers-insurance/';
		var params = $(formId).serialize();

		var self = this;
		$.ajax({
			url: target,
			dataType: 'json',
			data: params,
			type: "post",
			success: function(data){
				if (data.status != 'ok') {
//					alert("Sorry! The insurance quotation service is not available at this time.\n Please call us for more information on our insurance options.");

					$('.cwInsuranceDetailsNeeded').hide();
					$('.cwInsuranceDetailsUpdate').hide();
					$('.cwInsuranceQuotes table').hide();
					$('.cwInsuranceQuotes .message').text(self.genericErrorMsg);
					$('.cwInsuranceQuotes').show();

					return;
				}

				if (data.customerDetails.needed) {
					$('.cwInsuranceDetailsNeeded').show();
					$('.cwInsuranceDetailsUpdate').hide();
				} else {
					$('.cwInsuranceDetailsNeeded').hide();
					$('.cwInsuranceDetailsUpdate').show();
				}
				$('.cwInsuranceDetailsLinkText').text(data.customerDetails.text);
				$('.cwInsuranceDetailsLink').attr('href', data.customerDetails.url);

				if (data.resultList.length < 1) {
					$('.cwInsuranceQuotes table').hide();
					$('.cwInsuranceQuotes').hide();
					return;
				}

				for (var i = 0; i < data.resultList.length; i++) {
					self.processResult(data.resultList[i]);
				}
			},
			error: function(objRequest) {
				$('.cwInsuranceDetailsNeeded').hide();
				$('.cwInsuranceDetailsUpdate').hide();
				$('.cwInsuranceQuotes table').hide();
				$('.cwInsuranceQuotes .message').text(self.genericErrorMsg);
				$('.cwInsuranceQuotes').show();
			}
		});
	},

	processResult: function(result) {
		var selector = '.cwInsuranceEntry_'+ result.Id;

		var errorMsg = this.genericErrorMsg;
		if (result.HasError) {
			errorMsg = result.Error;
			$(selector +' .cwInsuranceDetailsNeeded').hide();
			$(selector +' .cwInsuranceDetailsUpdate').hide();
			$(selector +' .cwInsuranceQuotes table').hide();
			$(selector +' .cwInsuranceQuotes .message').text(errorMsg);
			$(selector +' .cwInsuranceQuotes').show();
			return;
		}

		if (result.Companies.length < 1) {
//			$(selector +' .cwInsuranceDetailsNeeded').hide();
//			$(selector +' .cwInsuranceDetailsUpdate').hide();
			$(selector +' .cwInsuranceQuotes table').hide();
			$(selector +' .cwInsuranceQuotes .message').text('No quotes were returned for this vehicle.');
			$(selector +' .cwInsuranceQuotes').show();
			return;
		}

		$(selector +' .cwInsuranceQuotes .message').text('').hide();
		$(selector +' .cwInsuranceQuotes table').show();
		$(selector +' .cwInsuranceQuotes').show();

		var self = this;
		for (var index = 0; index < result.Companies.length; index++) {
			var entry = result.Companies[index];
			var htmlCopy = self.html;
			var $html = $(htmlCopy);
			$html.find('.cwInsuranceCompany').addClass('cwInsuranceCompany_'+ index);
			$html.find('.cwInsuranceCompanyName').text(entry.Name);
			$html.find('.cwInsuranceAnnualPremium').text(entry.AnnualPremium);
			$html.find('.cwInsuranceMonthlyPremium').text(entry.MonthlyPremium);
			$html.find('.cwInsuranceMonthlyDeposit').text(entry.MonthlyPremiumDetails.Deposit);
			$html.find('.cwInsuranceMonthlyNumber').text(entry.MonthlyPremiumDetails.NumberOfPayments);
			$html.find('.cwInsuranceMonthlyTotal').text(entry.MonthlyPremiumDetails.TotalAmountPayable);
			$html.find('.cwInsuranceMonthlyApr').text(entry.MonthlyPremiumDetails.Apr);
			$html.find('.cwInsuranceImageLogo').attr('src', entry.Urls.Logo);
			$html.find('.cwInsuranceLinkBuy').attr('href', entry.Urls.ReviewAndBuy);
			$html.find('.cwInsuranceLinkFacts').attr('href', entry.Urls.KeyFacts);

			$(selector +' .cwInsuranceQuotesList').append($html);

			if ((entry['!bestquote'] == 'true') || (entry['!bestquote'] == true)) {
				$(selector +' .cwInsuranceBestQuote').append($html);
			}
		}
	},

	addVehicle: function(vehicleObj) {
		// Don't bother adding vehicles with no cap details
		if ((vehicleObj.capId == '') && (vehicleObj.capId == '')) {
			return;
		}
		this.hasVehicles = true;
		this.vehicleList['entry'+ vehicleObj.id] = vehicleObj;
	},

	setHtml: function(html) {
		this.html = html;
	}
}

$().ready(function () {
	netdirector.codeweavers.insurance.onReady();
});

