if(typeof(cchTab) == "undefined") {
	cchTab = 'Business';
}
if(typeof(cchURL) == "undefined") {
	cchURL = 'Comcar';
}
// create array to store rates
ratesArr = new Array(1);
// default conditions
cchTerm = 36;
cchMileage = 10000;
cchType = "Business";
cchMain = "No";
$(function(){
	$('#cchContent').before('<span class="cchTab" id="bTab">Business</span><span class="cchTab" id="pTab">Personal</span>');
	$('span.cchTab').click(function() {
		changeType(this);
	});
	// display loading gif
	$('#cchContent').html('<span id="cchmess">Loading, please wait</span><img style="margin-left:20px" src="/newcar/companycar/widget/cch/image/load.gif" alt="Loading..." />');
	// get car id from url
	var url = window.location.href;
	var num = url.search(/(\?|&)id=/i);
	var ccid = url.substr(num+4);
	num = ccid.search(/&|$/i);
	ccid = ccid.substring(0,num);	
	loadRates(ccid);
});
// load data
function loadRates(ccid) {
	$.get('/newcar/companycar/widget/cch/cch.cfm',{ccid:ccid},function(data) {
		$('#cchmess').html('Processing data');
		processRates(jQuery.trim(data));
	});	
}
// read data into rates array
function processRates(data) {
	// each rate becomes array entry
	ratesArr = data.split('~');
	for(x=0;x<ratesArr.length;x++) {
		temp = new Array();
		temp = ratesArr[x].split(',');		
		rate = new Object();
		rate.term 	= temp[0];
		rate.mileage= temp[1];
		rate.main 	= temp[2];
		rate.type 	= temp[3];
		rate.price1 = temp[4];
		rate.url1	= temp[5];		
		rate.comp1 	= temp[6];
		rate.price2 = temp[7];
		rate.url2	= temp[8];		
		rate.comp2 	= temp[9];
		rate.model 	= temp[10];
		rate.derv	= temp[11];				
		ratesArr[x] = rate;
	}
	showBody();		
}
// display rate object
// change to update a prerendered table, instead of appending a string ie $('td#comp'+num).html(evaluate('temp.comp'+num));
function showRate() {
	for(x=0;x<ratesArr.length;x++) {
		var temp = ratesArr[x];
		if(
			cchTerm == temp.term &&
			cchMileage == temp.mileage &&
			cchMain == temp.main &&
			temp.type.search(cchType) != -1
		) {
			// prep image var
			img1 = temp.comp1;
			img2 = temp.comp2;
			img1 = img1.replace('n/a','na');
			img2 = img2.replace('n/a','na');
			// logo src and alt
			$('#cchRate1 td:eq(0) img').attr('src','/newcar/companycar/widget/cch/image/'+img1+'.jpg');
			$('#cchRate2 td:eq(0) img').attr('src','/newcar/companycar/widget/cch/image/'+img2+'.jpg');
			$('#cchRate1 td:eq(0) img').attr('alt',temp.comp1);
			$('#cchRate2 td:eq(0) img').attr('alt',temp.comp2);
			// rate 1
			priceText = temp.price1;
			if (temp.price1 != 'n/a') priceText = '&pound;'+temp.price1;
			$('#cchRate1 td:eq(1)').html(priceText);
			$('#cchRate1 td:eq(2)').html('<a href="'+temp.url1.replace(/src=ComCar/gi,"src="+cchURL)+'">Request quote</a>');
			if (temp.url1 == 'n/a') $('#cchRate1 td:eq(2)').html('n/a');
			// rate 2
			priceText = temp.price2;
			if (temp.price2 != 'n/a') priceText = '&pound;'+temp.price2;
			$('#cchRate2 td:eq(1)').html(priceText);
			$('#cchRate2 td:eq(2)').html('<a href="'+temp.url2.replace(/src=ComCar/gi,"src="+cchURL)+'">Request quote</a>');
			if (temp.url2 == 'n/a') $('#cchRate2 td:eq(2)').html('n/a');			
			// update basis text
			$('#cchLength').html(cchTerm-1);
			break;
		}
	}
}

// change default variables and load updated rates
function updateRate() {
	cchTerm 	= $('#cchTerm option:selected').val();
	cchMileage	= $('#cchMileage option:selected').val();
	cchMain	= $('#cchMain option:selected').val();	
	showRate();
}

// switch between business and personal
function changeType(e) {
	cchType=$(e).html();
	if (cchType == 'Business') {
		$('#incEx').html('Exc');
		$('#pTab').css({borderBottom:"1px solid #006"});
		$('#bTab').css({borderBottom:"1px solid #DDD"});		
		$('#cchContent').css({backgroundColor:"#DDD"});
	} else {
		$('#incEx').html('Inc');
		$('#pTab').css({borderBottom:"1px solid #FFF"});		
		$('#bTab').css({borderBottom:"1px solid #006"});		
		$('#cchContent').css({backgroundColor:"#FFF"});
		$('#cchMain option:contains("Yes")').attr('selected','selected');
	}
	$('span.cchType').html(cchType);
	showRate();	
}

// pull in widget template
function showBody(){
	$.get('/newcar/companycar/widget/cch/template.html',function(data){
		$('#cchContent').html(jQuery.trim(data));
		$('#cchContent h4').html('<span>'+ratesArr[1].model+' </span>');
		$('#cchContent h4 span').after(ratesArr[1].derv);
		changeType($('span.cchTab:contains("'+cchTab+'")'));
	});
}
