//GLOBAL VARIABLES
var recommended_cards = new Array();
var compare_cards = new Array();
var hide = 0;

//Input Objects
var income_lt_35;
var income_35_to_60; 
var income_gt_60;
var type_rewards; 
var type_student;
var type_lowrate;
var type_nofee;
var type_business;
var feature_merchandise; 
var feature_travel; 
var feature_specialty;
var feature_usdollar;
var feature_insurance; 

//Main function -- initialise cards, check query params, display cards
function init() {
	//Initialize Cards
	initializeCards();
	//Initialize inputs
	income_lt_35 = document.getElementById('income_lt_35'); 
	income_35_to_60 = document.getElementById('income_35_to_60'); 
	income_gt_60 = document.getElementById('income_gt_60'); 
	
	type_rewards = document.getElementById('type_rewards'); 
	type_student = document.getElementById('type_student'); 
	type_lowrate = document.getElementById('type_lowrate'); 
	type_nofee = document.getElementById('type_nofee'); 
	type_business = document.getElementById('type_business'); 
	
	feature_merchandise = document.getElementById('feature_merchandise'); 
	feature_travel = document.getElementById('feature_travel'); 
	feature_specialty = document.getElementById('feature_specialty'); 
	feature_usdollar = document.getElementById('feature_usdollar'); 
	feature_insurance = document.getElementById('feature_insurance'); 
	
	//Check for Query parameter Inputs
	checkParamsForInputs();
	
	//Enable and Disable Inputs
	updateInputs();
	
	//Calculate cards from inputes
	calculateCards();
}

//These are additional logic rules outside of data matrix
function extralogic(recommended_cards){
	//Extra Logic - Concering Income
	//Rule 1: Visa Platinum Avion should only display if income is less than $60M
	for (var x=0; x < recommended_cards.length; x++) {
		obj = recommended_cards[x];	
		if (obj.id == 'platinum' && !income_gt_60.checked) recommended_cards.splice(x, 1);
	}
	//Rule 2: Rewards Visa Classic should only display if income is less than $35
	for (var x=0; x < recommended_cards.length; x++) {
		obj = recommended_cards[x];	
		if (obj.id == 'rewards_classic' && !income_lt_35.checked) recommended_cards.splice(x, 1);
	}	
	
	//Extra Logic - Concering Business
	//Rule 1: Visa Business, and Business Avion should only appear when Business is selected
	for (var x=0; x < recommended_cards.length; x++) {
		obj = recommended_cards[x];	
		if (obj.id == 'business' && !type_business.checked) recommended_cards.splice(x, 1);		
	}
	for (var x=0; x < recommended_cards.length; x++) {
		obj = recommended_cards[x];	
		if (obj.id == 'bus_avion' && !type_business.checked) recommended_cards.splice(x, 1);		
	}
	for (var x=0; x < recommended_cards.length; x++) {
		obj = recommended_cards[x];	
		if (obj.id == 'creditline' && !type_business.checked) recommended_cards.splice(x, 1);		
	}	
		
	//Extra Logic - Concering Students
	//Rule 1: Student Cards should only appear when student is selected
	for (var x=0; x < recommended_cards.length; x++) {
		obj = recommended_cards[x];	
		if (obj.id == 'classic_student' && !type_student.checked) recommended_cards.splice(x, 1);		
	}
	for (var x=0; x < recommended_cards.length; x++) {
		obj = recommended_cards[x];	
		if (obj.id == 'classic2_student' && !type_student.checked) recommended_cards.splice(x, 1);		
	}		
	
	
	/* Caused issue after automation on May 15, 2009
	//Extra Logic - Concering US Dollar
	//Rule 1: US$ should only appear when tranactions in US$ is selected
	for (var x=0; x < recommended_cards.length; x++) {
		obj = recommended_cards[x];	
		if (obj.id == 'gold_us' && !feature_usdollar.checked) recommended_cards.splice(x, 1);		
	}
	*/
	
	//Extra Logic - Concerning Low Rate
	//Rule 1: Low Rate should only appear when Low Rate is selected
	for (var x=0; x < recommended_cards.length; x++) {
		obj = recommended_cards[x];	
		if (obj.id == 'classic_lr' && !type_lowrate.checked) recommended_cards.splice(x, 1);		
	}		
	
	//Extra Logic - Concering Platinum Avion
	//Rule 1: If $60K or more, Rewards Card, Travel and Insurance selected, remove avion_p and move ba_platinum up
	for (var x=0; x < recommended_cards.length; x++) {
		obj = recommended_cards[x];
		if (income_gt_60.checked && type_rewards.checked && feature_travel.checked && feature_insurance.checked && obj.id == 'avion_p') recommended_cards.splice(x, 1);		
	}	
	//Rule 2: If $60K or more, Rewards Card, and Travel selected, remove avion_p and move ba_platinum up
	for (var x=0; x < recommended_cards.length; x++) {
		obj = recommended_cards[x];
		if (income_gt_60.checked && type_rewards.checked && feature_travel.checked && obj.id == 'avion_p') recommended_cards.splice(x, 1);		
	}	
	return recommended_cards;
}

//Collects inputs and calculates recommended cards
// if hide is set to 0 it will display the cards
// otherwise it will only update the number
function calculateCards() {
	//Clear arrays
	recommended_cards = new Array();
	compare_cards = new Array();
	
	//Income Input Logic --> Builds initial recommendation array
	var temp_income;
	for (var x=0; x< cards.length; x++) {
		temp_income = cards[x].min_income;
		temp_income = convertToNumber(temp_income);
		
		if (income_lt_35.checked && temp_income < 35000.00) recommended_cards.push(cards[x]);
		else if (income_35_to_60.checked && temp_income < 60000.00) recommended_cards.push(cards[x]);
		else if (income_gt_60.checked) recommended_cards.push(cards[x]);
	}
	
	//Card Type Input Logic --> Removes not applicable cards from array
 	var obj = new Card;
 	var remove_flag = 0;
	var temp_array = recommended_cards;
	recommended_cards = new Array();
	for (var x=0; x < temp_array.length; x++) {
		obj = temp_array[x];	
		remove_flag = 0	
		if (type_rewards.checked && obj.type_rewards != 'Y') remove_flag = 1;
		else if (type_student.checked && obj.type_student != 'Y') remove_flag = 1;
		else if (type_lowrate.checked && obj.type_lowrate != 'Y') remove_flag = 1;
		else if (type_nofee.checked && obj.type_nofee != 'Y') remove_flag = 1;
		else if (type_business.checked && obj.type_business != 'Y') remove_flag = 1;
		
		//If not removed add back into array
		if (remove_flag != 1) recommended_cards.push(obj);
	}	
	
	//Features Input Logic --> Removes not applicable cards from array
 	var obj = new Card;
 	var num_selected_flag = 0;
 	var keep_flag = 0;
	var temp_array = recommended_cards;
	recommended_cards = new Array();

	//Remove erop Options from Step 3
	step3DisableZeroOptions(temp_array);	

	//Count nuber of inputs selected for AND Logic
	if (feature_merchandise.checked) num_selected_flag++;
	if (feature_travel.checked) num_selected_flag++;
	if (feature_specialty.checked) num_selected_flag++;
	if (feature_usdollar.checked) num_selected_flag++;
	if (feature_insurance.checked) num_selected_flag++;
			
	for (var x=0; x < temp_array.length; x++) {
		obj = temp_array[x];	
		keep_flag = 0;	
		
		//AND Logic
		if (feature_merchandise.checked && obj.feature_merchandise == 'Y') keep_flag++;
		if (feature_travel.checked && obj.feature_travel == 'Y') keep_flag++;
		if (feature_specialty.checked && obj.feature_specialty == 'Y') keep_flag++;
		if (feature_usdollar.checked && obj.feature_usdollar == 'Y') keep_flag++;
		if (feature_insurance.checked && obj.feature_insurance == 'Y') keep_flag++;
		//Remove card if num selected doesnt match the keep flag
		if (num_selected_flag != keep_flag) keep_flag = 0
		
		//OR Logic
		/*
		if (feature_merchandise.checked && obj.feature_merchandise == 'Y') keep_flag++;
		if (feature_travel.checked && obj.feature_travel == 'Y') keep_flag++;
		if (feature_specialty.checked && obj.feature_specialty == 'Y') keep_flag++;
		if (feature_usdollar.checked && obj.feature_usdollar == 'Y') keep_flag++;
		if (feature_insurance.checked && obj.feature_insurance == 'Y') keep_flag++;
		*/
		
		//If no inputs are selected keep all cards
		if (!feature_merchandise.checked && !feature_travel.checked && !feature_specialty.checked && !feature_usdollar.checked && !feature_insurance.checked) keep_flag = 1;
		
		//If not removed add back into array
		if (keep_flag > 0) recommended_cards.push(obj);
	}
	recommended_cards = extralogic(recommended_cards);
		
	//Update Recommended Card Number
	document.getElementById('num_cards').innerHTML = (hide==1 && recommended_cards.length==0) ? '' : recommended_cards.length;
	//------DEBUG 
	//document.getElementById('debug').innerHTML;
	//debug();
	
	//Change the number of cards message
	if (recommended_cards.length > 0) document.getElementById('num_cards_message').innerHTML = '<strong>Cartes de cr&eacute;dit qui correspondent &agrave; vos crit&egrave;res.</strong>';
	else document.getElementById('num_cards_message').innerHTML = '<strong>Choisir une combinaison.</strong>';
	
	//Display the cards and the link to compare tool
	if (hide == 1) { 
		if (recommended_cards.length > 0) document.getElementById('num_cards_button').style.display = '';
		else document.getElementById('num_cards_button').style.display = 'none';			
	}
	else {
		document.getElementById('num_cards_button').style.display = 'none';	
		displayCards();
		compareCardsUpdate();		
	}	
	
	//Hide or Show Compare Button
	if (hide != 1) { 
		if (recommended_cards.length > 0) document.getElementById('compare_button_wrapper').style.display = '';
		else document.getElementById('compare_button_wrapper').style.display = 'none';
	}
}

//Enables, disables, and clears inputs appropriately. 
function updateInputs() {	
	var step1_selection_made = 0;
	var step2_selection_made = 0;
	var step3_selection_made = 0;
	
	//Check selections
	if (income_lt_35.checked || income_35_to_60.checked || income_gt_60.checked) step1_selection_made = 1;
	if (type_rewards.checked || type_student.checked || type_lowrate.checked || type_nofee.checked || type_business.checked) step2_selection_made = 1;
	if (feature_merchandise.checked || feature_travel.checked || feature_specialty.checked || feature_usdollar.checked || feature_insurance.checked)step3_selection_made = 1;
	//Flip Switches
	//Step 1
	if (step1_selection_made == 1) {
		step1Inputs(1);
		step2Inputs(1, 1);	
		
		//Step 2
		if (step2_selection_made == 1) {
			step3Inputs(1, 1);	
		}
		else {
			step2Inputs(1, 1);
			step3Inputs(0, 0);		
		}		
	}
	else {
		step1Inputs(1);
		step2Inputs(0, 0);
		step3Inputs(0, 0);
	}
}

//Sets the hide variable to 1 so that the cards are not displayed
function setHide(){
	hide = 1;
}

//Disables any options in step3 that result in 0 recommended cards
//Changed to automatically disable third column options on May 14 2009
function step3DisableZeroOptions(temp_array) {
	//Step 1 Inputs
	income_lt_35 = document.getElementById('income_lt_35'); 
	income_35_to_60 = document.getElementById('income_35_to_60'); 
	income_gt_60 = document.getElementById('income_gt_60'); 
	//Step 2 Inputs
	type_rewards = document.getElementById('type_rewards'); 
	type_student = document.getElementById('type_student'); 
	type_lowrate = document.getElementById('type_lowrate'); 
	type_nofee = document.getElementById('type_nofee'); 
	type_business = document.getElementById('type_business'); 
	//Step 3 Inputs
	feature_merchandise = document.getElementById('feature_merchandise'); 
	feature_travel = document.getElementById('feature_travel'); 
	feature_specialty = document.getElementById('feature_specialty'); 
	feature_usdollar = document.getElementById('feature_usdollar'); 
	feature_insurance = document.getElementById('feature_insurance'); 
	
	var mer = new Array();
	var tra = new Array();
	var spe = new Array();
	var usd = new Array();
	var ins = new Array();
	for (var x=0; x < temp_array.length; x++) {
		obj = temp_array[x];	
		if (obj.feature_merchandise == 'Y') 
			mer.push(obj);
	}
	mer = extralogic(mer);
	for (var x=0; x < temp_array.length; x++) {
		obj = temp_array[x];	
		if (obj.feature_travel == 'Y') 
			tra.push(obj);
	}		
	tra = extralogic(tra);
	for (var x=0; x < temp_array.length; x++) {
		obj = temp_array[x];	
		if (obj.feature_specialty == 'Y') 
			spe.push(obj);
	}
	spe = extralogic(spe);
	for (var x=0; x < temp_array.length; x++) {
		obj = temp_array[x];	
		if (obj.feature_usdollar == 'Y') 
			usd.push(obj);
	}
	usd = extralogic(usd);
	for (var x=0; x < temp_array.length; x++) {
		obj = temp_array[x];	
		if (obj.feature_insurance == 'Y') 
			ins.push(obj);
	}
	ins = extralogic(ins);	
	
	//alert("mer.length:"+mer.length+" +tra.length:"+tra.length+" spe.length:"+spe.length+" usd.length:"+usd.length+" ins.length:"+ins.length);
	
	if(mer.length>0){
		feature_merchandise.disabled = false;
	}else{
		feature_merchandise.disabled = true;
	}
	if(tra.length>0){
		feature_travel.disabled = false;
	}else{
		feature_travel.disabled = true;
	}
	if(spe.length>0){
		feature_specialty.disabled = false;
	}else{
		feature_specialty.disabled = true;
	}
	if(usd.length>0){
		feature_usdollar.disabled = false;
	}else{
		feature_usdollar.disabled = true;
	}
	if(ins.length>0){
		feature_insurance.disabled = false;
	}else{
		feature_insurance.disabled = true;
	}
}

//Turn Step 1 On or Off
function step1Inputs(highlight) {
	if (highlight == 1) document.getElementById('step1_num').className = 'card-select-num-on';
	else document.getElementById('step1_num').className = 'card-select-num-off';
}

//Turn Step 2 On or Off
function step2Inputs(highlight, enable) {
	//Number
	if (highlight == 1) document.getElementById('step2_num').className = 'card-select-num-on';
	else document.getElementById('step2_num').className = 'card-select-num-off';
	//Inputs
	if (enable == 1) {
		type_rewards.disabled = false;
		type_student.disabled = false;
		type_lowrate.disabled = false;
		type_nofee.disabled = false;
		type_business.disabled = false;
	}
	else {
		type_rewards.disabled = true; type_rewards.checked = false;
		type_student.disabled = true; type_student.checked = false;
		type_lowrate.disabled = true; type_lowrate.checked = false;
		type_nofee.disabled = true; type_nofee.checked = false;
		type_business.disabled = true; type_business.checked = false;	
	}
}

//Turn Step 3 On or Off
function step3Inputs(highlight, enable) {
	//Number
	if (highlight == 1) document.getElementById('step3_num').className = 'card-select-num-on';		
	else document.getElementById('step3_num').className = 'card-select-num-off';
	
	//Inputs	
	if (enable == 1) {
		feature_merchandise.disabled = false;
		feature_travel.disabled = false;
		feature_specialty.disabled = false;
		feature_usdollar.disabled = false;
		feature_insurance.disabled = false;
	}
	else {
		feature_merchandise.disabled = true; feature_merchandise.checked = false;
		feature_travel.disabled = true; feature_travel.checked = false;
		feature_specialty.disabled = true; feature_specialty.checked = false;
		feature_usdollar.disabled = true; feature_usdollar.checked = false;
		feature_insurance.disabled = true; feature_insurance.checked = false;	 
	}		
}

//Resets the given steps inputs
function resetInputs(num) {
	if (num == 1) {
		income_lt_35.checked = false;
		income_35_to_60.checked = false;
		income_gt_60.checked = false; 
	}	
	else if (num == 2) {
		type_rewards.checked = false;
		type_student.checked = false;
		type_lowrate.checked = false;
		type_nofee.checked = false;
		type_business.checked = false;	 
	}		
	else if (num == 3) {
		feature_merchandise.checked = false;
		feature_travel.checked = false;
		feature_specialty.checked = false;
		feature_usdollar.checked = false;
		feature_insurance.checked = false;	 
	}		
}

//Sorts by ranking and then displays cards held in the recommended_cards array
function displayCards() {
	//Sort Cards
	recommended_cards.sort(compareCardRankings);
	
	//------DEBUG 
	//document.getElementById('debug').innerHTML;
	//debug();
	
	//Display Top Three
	var top3_output = '';
	var bg_color = 'on';
	var top3_number = 0;
	for (var x=0; (x<3 && x<recommended_cards.length); x++) {	
		var obj = recommended_cards[x];
		top3_output += '		<tr class="contentframework-subheader">';
		top3_output += '			<th id="headerCheckBoxTitle1"><input id="compare_' + obj.id + '" value="' + obj.id + '" type="checkbox" onclick="compareCardsUpdate(this);"></th>';
		top3_output += '			<th id="headerCardTitle1" colspan="4"><h4>' + obj.name + '</h4></th>';
		top3_output += '		</tr>';		
		top3_output += '		<tr class="visaoverview-bg-' + bg_color + '">';			
		top3_output += '			<td headers="headerCheckBox headerCheckBoxTitle1">&nbsp;</td>';			
		top3_output += '			<td headers="headerCardImage headerCardTitle1"><a href="' + obj.learn_more + '"><img src="/cartes/_assets-custom/images/mid-cards/m_' + obj.id + '.gif" alt="' + obj.name + '" height="79" width="125"></a></td>';			
		top3_output += '			<td class="subcopy" headers="headerInterestRate headerCardTitle1">' + obj.interest_rate + '</td>';			
		top3_output += '			<td class="subcopy" headers="headerAnnualFee headerCardTitle1">' + obj.annual_fee;
		//if (obj.annual_fee_65 != '') top3_output += '<br />' + obj.annual_fee_65 + ' (age 65+)';	
		top3_output += '			</td>';			
				
		top3_output += '			<td headers="headerCardOverview headerCardTitle1">';				
		top3_output += '				<ul>';					
		top3_output += '					<li>' + obj.benefit + '</li>';
		top3_output += '					<li>' + obj.feature1 + '</li>';
		top3_output += '					<li>' + obj.feature2 + '</li>';		
		if (obj.special_offer != '') top3_output += '					<li>' + obj.special_offer + '</li>';								
		top3_output += '				</ul>';				
		top3_output += '				<div class="visaoverview-btnarea-wrapper">';
		top3_output += '					<div class="visaoverview-btnarea subcopy nowrap">';
		top3_output += '						<img src="uos/_assets/images/layout/bullet-link.gif" alt="" class="bullet" border="0">'; 
		top3_output += '						<a href="' + obj.learn_more + '">En savoir plus</a>';
		top3_output += '					</div>';				
		top3_output += '					<span class="button button-primary buttonfloatright">';
		
		if (obj.id == 'business' || obj.id == 'bus_avion') 
			top3_output += '						<span><a href="'+ apply_business_link+ '">Demande <img src="uos/_assets/images/buttons/chevron.gif" alt="" height="12" width="12"></a></span>';
		else if (obj.id == 'creditline') 
			top3_output += '						<span><a href="'+ apply_creditline_link+ '">Demande <img src="uos/_assets/images/buttons/chevron.gif" alt="" height="12" width="12"></a></span>';		
		else
			top3_output += '						<span><a href="' + apply_link + '?cid=' + obj.id + '">Demande <img src="uos/_assets/images/buttons/chevron.gif" alt="" height="12" width="12"></a></span>';			
		
		top3_output += '					</span>';			
		top3_output += '				</div>';			
		top3_output += '			</td>';		
		top3_output += '		</tr>';			
		top3_number++;
		
		//Switch bg color
		bg_color = (bg_color == 'on') ? 'off' : 'on';
	}
	
	var table_output = '<div><h2 class="firstline">Les ' + top3_number + ' cartes les plus recommand&eacute;es :</h2>';	
	table_output += '<table class="contentframework">';
	table_output += '	<tbody>';		
	table_output += '		<tr>';			
	table_output += '			<th class="contentframework-dataheadertop" id="headerCheckBox" width="2%"><img src="/cartes/tools/_assets-custom/images/checkbox.gif" alt="" width="13" height="13" /></th>';			
	table_output += '			<th class="contentframework-dataheadertop" id="headerCardImage" width="15%">&nbsp;</th>';			
	table_output += '			<th class="contentframework-dataheadertop" id="headerInterestRate" width="10%">Taux<br />d\'int&eacute;r&ecirc;t</th>';			
	table_output += '			<th class="contentframework-dataheadertop" id="headerAnnualFee" width="10%">Frais<br />annuels</th>';			
	table_output += '			<th class="contentframework-dataheadertop" id="headerCardOverview" width="63%">Aper&ccedil;u de la carte</th>';		
	table_output += '		</tr>';		
	table_output += top3_output;
	table_output += '	</tbody>';
	table_output += '</table>';
		
	//Display Others -- if there are any
	var other_output = '<h2>Les autres cartes les plus recommand&eacute;es :</h2>';
	other_output += '<table class="contentframework">';
	other_output += '<tbody>';
	other_output += '		<tr>';		
	other_output += '			<th class="contentframework-dataheadertop" id="headerCheckBox2" width="2%"><img src="/cartes/tools/_assets-custom/images/checkbox.gif" alt="" width="13" height="13" /></th>';			
	other_output += '			<th class="contentframework-dataheadertop" id="headerCardImage2" width="15%"><div class="visa-spacer"></div></th>';			
	other_output += '			<th class="contentframework-dataheadertop" id="headerInterestRate2" width="10%">Taux<br />d\'int&eacute;r&ecirc;t</th>';			
	other_output += '			<th class="contentframework-dataheadertop" id="headerAnnualFee2" width="10%">Frais<br />annuels</th>';			
	other_output += '			<th class="contentframework-dataheadertop" id="headerCardOverview2" width="63%">Aper&ccedil;u de la carte</th>';		
	other_output += '		</tr>';	
	
	var bg_color = 'on';
	var other_number = 0;
	for (var x=3; x<recommended_cards.length; x++) {	
		var obj = recommended_cards[x];
		other_output += '		<tr class="contentframework-subheader">';
		other_output += '			<th id="headerCheckBoxTitle5"><input id="compare_' + obj.id + '" value="' + obj.id + '" type="checkbox" onclick="compareCardsUpdate(this);"></th>';
		other_output += '			<th id="headerCardTitle5" colspan="4">';
		other_output += '				<h4><a href="javascript: toggleIcon(\'content_' + obj.id + '\',\'icon_' + obj.id + '\'); void(0);" id="icon_' + obj.id + '" class="toggleiconlink"><img src="/uos/_assets/images/contentframework/icon-expand.gif" alt="Expand">'; 
		other_output += '					<span class="toggleiconlink-label">' + obj.name + '</span></a></h4>';
		other_output += '			</th>';
		other_output += '		</tr>';
		other_output += '		<tr class="visaoverview-bg-' + bg_color + ' content_' + obj.id + ' jshide" style="display: none;">';			
		other_output += '			<td headers="headerCheckBox2 headerCheckBoxTitle4">&nbsp;</td>';			
		other_output += '			<td headers="headerCardImage2 headerCardTitle4"><a href="' + obj.learn_more + '"><img src="/cartes/_assets-custom/images/mid-cards/m_' + obj.id + '.gif" alt="' + obj.name + '" height="79" width="125"></a></td>';
		other_output += '			<td class="subcopy" headers="headerInterestRate2 headerCardTitle4">' + obj.interest_rate + '</td>';			
		other_output += '			<td class="subcopy" headers="headerAnnualFee2 headerCardTitle4">' + obj.annual_fee;	
		//if (obj.annual_fee_65 != '') other_output += '<br />' + obj.annual_fee_65 + ' (age 65+)';	
		other_output += '			</td>';					
		other_output += '			<td headers="headerCardOverview2 headerCardTitle4">';		
		other_output += '				<ul>';					
		other_output += '					<li>' + obj.benefit + '</li>';
		other_output += '					<li>' + obj.feature1 + '</li>';
		other_output += '					<li>' + obj.feature2 + '</li>';		
		if (obj.special_offer != '') other_output += '					<li>' + obj.special_offer + '</li>';								
		other_output += '				</ul>';				
		other_output += '				<div class="visaoverview-btnarea-wrapper">';					
		other_output += '					<div class="visaoverview-btnarea subcopy nowrap"><img src="uos/_assets/images/layout/bullet-link.gif" alt="" class="bullet" border="0"> <a href="' + obj.learn_more + '">En savoir plus</a></div>';		
		
		if (obj.id == 'business' || obj.id == 'bus_avion')
			other_output += '					<span class="button button-primary buttonfloatright"><span><a href="' + apply_business_link + '">Demande <img src="uos/_assets/images/buttons/chevron.gif" alt="" height="12" width="12"></a></span></span>';	
		else if (obj.id == 'creditline')
			other_output += '					<span class="button button-primary buttonfloatright"><span><a href="' + apply_creditline_link + '">Demande <img src="uos/_assets/images/buttons/chevron.gif" alt="" height="12" width="12"></a></span></span>';							
		else 
			other_output += '					<span class="button button-primary buttonfloatright"><span><a href="' + apply_link + '?cid=' + obj.id + '">Demande <img src="uos/_assets/images/buttons/chevron.gif" alt="" height="12" width="12"></a></span></span>';						
		
		other_output += '				</div>';			
		other_output += '			</td>';
		other_output += '		</tr>';				
		other_number++;
		
		//Switch bg color
		bg_color = (bg_color == 'on') ? 'off' : 'on';
	}
	
	other_output += '	</tbody>';
	other_output += '</table>';
	
	//Update HTML -- Recommended
	if (top3_number > 0) {
		//document.getElementById('recommended_top3').style.display = '';	
		document.getElementById('recommended_top3').innerHTML = table_output;
		$("#recommended_top3").show("blind", {}, 1000);	
	}
	else {
		document.getElementById('recommended_top3').style.display = 'none';
		document.getElementById('recommended_top3').innerHTML = '';
	}	
	
	//Update HTML -- Others
	if (other_number > 0) {
		//document.getElementById('recommended_others').style.display = '';
		document.getElementById('recommended_others').innerHTML = other_output;
		$("#recommended_others").show("blind", {}, 1000);
	}
	else {
		document.getElementById('recommended_others').style.display = 'none';
		document.getElementById('recommended_others').innerHTML = '';
	}
	
	//Update HTML -- Promo Box and Compare Button
	if (top3_number > 0) {
		document.getElementById('recommended_promo_box').style.display = '';
	}
	else {
		document.getElementById('recommended_promo_box').style.display = 'none';
	}
}

//Comparison function for sorting, compares card ranking value
function compareCardRankings(a, b) {
	//return b.ranking - a.ranking; //highest (20) to lowest (1)
	return a.ranking - b.ranking; //lowest (1) to highest (20)
}

//Checks and updates the compare cards array for up to three cards
//takes in checkbox object
function compareCardsUpdate(obj){
	//If Object is passed in add or remove from array
	if (obj != undefined){
		//Add to array
		if (obj.checked) {
			//If array is full disable the click, otherwise add it to array
			if (compare_cards.length >= 3) obj.checked = false;
			else {
				compare_cards.push(obj.value);
			}
		}
		//Remove from array
		else {
			for (var x=0; x < compare_cards.length; x++) {
				if (compare_cards[x] == obj.value) compare_cards.splice(x, 1);
			}	
		}
	}
	
	//Check compare button
	if (compare_cards.length > 0) {
		document.getElementById('compare_button_enabled').style.display = '';
		document.getElementById('compare_button_disabled').style.display = 'none';			
	}
	else {
		document.getElementById('compare_button_enabled').style.display = 'none';
		document.getElementById('compare_button_disabled').style.display = '';	
	}
}

//Builds the compare link from the compare_cards array 
//for the card comparison tool.
function buildCompareLink () {
	//Build link
	var link = compare_tool_link + '?';
	for (var x=0; x < compare_cards.length; x++) {
		link += 'c' + (x+1) + '=' +  compare_cards[x];
		if (x != (compare_cards.length-1)) link += '&';
	}	
	
	//Go to Link
	document.location = link;
}

//Checks parameters for the inputs and fills in the specified inputs
function checkParamsForInputs() {
	//Inputs to look for
	var inputs_list = new Array();
	inputs_list.push('income');
	inputs_list.push('type');
	inputs_list.push('feature_1');
	inputs_list.push('feature_2');
	inputs_list.push('feature_3');
	inputs_list.push('feature_4');
	inputs_list.push('feature_5');
	
	//Get params
	var temp_val = '';
	for (var x=0; x<inputs_list.length; x++) {
		temp_val = getURLParam(inputs_list[x]);		
		
		//Select Input
		if (temp_val != null) document.getElementById(temp_val).checked = true;
	}
}

//Prints out cards in recommended array
function debug() {
	var current = document.getElementById('debug').innerHTML;
	
	var bug ='';
	for (var y=0; y < recommended_cards.length; y++) {
		bug += recommended_cards[y].id;
		bug +="<br />";
	}
	
	document.getElementById('debug').innerHTML = current + bug + '=====<br />';
}