//GLOBAL VARIABLES
var recommended_cards = new Array();
var recommended_cards_weight = new Array();

var compare_cards = new Array();
var hide = 0;
var num_rec_cards = 0;
var num_comp_cards = 0;

//Input Objects
var goal_for_emer;
var goal_fut_purch; 
var goal_reg_inc;
var goal_growth;
var amt_lt_4999; 
var amt_gt_5000;
var access_yes;
var access_no;
var term_lt_1;
var term_1_3; 
var term_gt_3; 

//Main function -- initialise cards, check query params, display cards
function init() {
	//Initialize Cards
	initializeCards();
	//Initialize inputs
	goal_for_emer = document.getElementById('goal_for_emer'); 
	goal_fut_purch = document.getElementById('goal_fut_purch'); 
	goal_reg_inc = document.getElementById('goal_reg_inc'); 
	goal_growth = document.getElementById('goal_growth'); 
	
	amt_lt_4999 = document.getElementById('amt_lt_4999'); 
	amt_gt_5000 = document.getElementById('amt_gt_5000'); 
	
	access_yes = document.getElementById('access_yes'); 
	access_no = document.getElementById('access_no'); 
	
	term_lt_1 = document.getElementById('term_lt_1'); 
	term_1_3 = document.getElementById('term_1_3'); 
	term_gt_3 = document.getElementById('term_gt_3'); 
	
	//Check for Query parameter Inputs
	checkParamsForInputs();
	
	//Enable and Disable Inputs
	updateInputs();
	
	//Calculate cards from inputes
	calculateCards();
}


//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_weight = new Array(0,0,0,0,0,0,0,0,0,0,0,0,0);
	recommended_cards = new Array();
	//Income Input Logic --> Builds initial recommendation array
	for (var x=0; x < cards.length; x++) {

		recommended_cards.push(cards[x]);
		recommended_cards[x].weight = recommended_cards_weight[x];
	
		if (goal_for_emer.checked){
			recommended_cards_weight[x] += convertToNumber(cards[x].goal1);
		}else if (goal_fut_purch.checked){
			recommended_cards_weight[x] += convertToNumber(cards[x].goal2);
		}else if (goal_reg_inc.checked){
			recommended_cards_weight[x] += convertToNumber(cards[x].goal3);
		}else if (goal_growth.checked){
			recommended_cards_weight[x] += convertToNumber(cards[x].goal4);
		}
		
		if (amt_lt_4999.checked){
			recommended_cards_weight[x] += convertToNumber(cards[x].amount1);
		}else if (amt_gt_5000.checked){
			recommended_cards_weight[x] += convertToNumber(cards[x].amount2);
		}
		
		if (access_yes.checked){
			recommended_cards_weight[x] += convertToNumber(cards[x].access1);
		}else if (access_no.checked){
			recommended_cards_weight[x] += convertToNumber(cards[x].access2);
		}

		if (term_lt_1.checked){
			recommended_cards_weight[x] += convertToNumber(cards[x].term1);
		}else if (term_1_3.checked){
			recommended_cards_weight[x] += convertToNumber(cards[x].term2);
		}else if (term_gt_3.checked){
			recommended_cards_weight[x] += convertToNumber(cards[x].term3);
		}	

		recommended_cards[x].weight = recommended_cards_weight[x];

	}

	num_rec_cards = 0;
	for (var x=0; x < recommended_cards_weight.length; x++) {
		if(recommended_cards_weight[x] > 0){
			num_rec_cards++;
		}		
	}

	if ((num_rec_cards > 0)&&((term_lt_1.checked == true)||(term_1_3.checked == true)||(term_gt_3.checked == true))){
		document.getElementById('num_cards_message1').innerHTML = '';
	}else{
		document.getElementById('num_cards_message1').innerHTML = '';
	}
	
	if((term_lt_1.checked == true)||(term_1_3.checked == true)||(term_gt_3.checked == true)){
		document.getElementById('num_cards').innerHTML = (hide==1 && num_rec_cards==0) ? '' : num_rec_cards;
	}else{
		document.getElementById('num_cards').innerHTML = '';
	}

	if ((num_rec_cards > 0)&&((term_lt_1.checked == true)||(term_1_3.checked == true)||(term_gt_3.checked == true))) document.getElementById('num_cards_message2').innerHTML = '<strong>produits qui correspondent &agrave; vos crit&egrave;res.</strong>';
	else document.getElementById('num_cards_message2').innerHTML = '<strong></strong>';
	
	//Display the cards and the link to compare tool
	if (hide == 1) { 
		if ((num_rec_cards > 0)&&((term_lt_1.checked == true)||(term_1_3.checked == true)||(term_gt_3.checked == true))) 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 (num_rec_cards > 0) document.getElementById('compare_button_wrapper').style.display = '';
		else document.getElementById('compare_button_wrapper').style.display = 'none';
	}	
	
	if (((goal_for_emer.checked == true)||(goal_fut_purch.checked == true)||(goal_reg_inc.checked == true)||(goal_growth.checked == true))&&((amt_lt_4999.checked == false)&&(amt_lt_4999.checked == false))&&((access_yes.checked == false)&&(access_no.checked == false))){
		triggerPixel(10893);
	}
	

}

// Bridgetrack
function triggerPixel (pixelID) {
	var img = new Image();
	img.src = window.location.protocol + "//rbc.bridgetrack.com/track/?id=" + pixelID + "&r=" + Math.random();
	return false;
}

//Enables, disables, and clears inputs appropriately. 
function updateInputs() {	
	var step1_selection_made = 0;
	var step2_selection_made = 0;
	var step3_selection_made = 0;
	var step4_selection_made = 0;
	
	//Check selections
	if (goal_for_emer.checked || goal_fut_purch.checked || goal_reg_inc.checked || goal_growth.checked) step1_selection_made = 1;
	if (amt_lt_4999.checked || amt_gt_5000.checked) step2_selection_made = 1;
	if (access_yes.checked || access_no.checked) step3_selection_made = 1;
	if (term_lt_1.checked || term_1_3.checked || term_gt_3.checked) step4_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);	
			if (step3_selection_made == 1) {
				step4Inputs(1, 1);
			}
			else {
				step3Inputs(1, 1);		
				step4Inputs(0, 0);
			}
		}
		else {
			step2Inputs(1, 1);
			step3Inputs(0, 0);		
			step4Inputs(0, 0);
		}		
	}
	else {
		step1Inputs(1);
		step2Inputs(0, 0);
		step3Inputs(0, 0);
		step4Inputs(0, 0);
	}
}

//Sets the hide variable to 1 so that the cards are not displayed
function setHide(){
	hide = 1;
}


//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) {
		amt_lt_4999.disabled = false;
		amt_gt_5000.disabled = false;
		access_yes.disabled = false;
		access_no.disabled = false;
		term_lt_1.disabled = false;
		term_1_3.disabled = false;
		term_gt_3.disabled = false;		
	}
	else {
		amt_lt_4999.disabled = true; amt_lt_4999.checked = false;
		amt_gt_5000.disabled = true; amt_gt_5000.checked = false;
		access_yes.disabled = true; access_yes.checked = false;
		access_no.disabled = true; access_no.checked = false;
		term_lt_1.disabled = true; term_lt_1.checked = false;	
		term_1_3.disabled = true; term_1_3.checked = false;	
		term_gt_3.disabled = true; term_gt_3.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) {
		access_yes.disabled = false;
		access_no.disabled = false;
		term_lt_1.disabled = false;
		term_1_3.disabled = false;
		term_gt_3.disabled = false;		
	}
	else {
		access_yes.disabled = true; access_yes.checked = false;
		access_no.disabled = true; access_no.checked = false;
		term_lt_1.disabled = true; term_lt_1.checked = false;	
		term_1_3.disabled = true; term_1_3.checked = false;	
		term_gt_3.disabled = true; term_gt_3.checked = false;	
	}		
}

//Turn Step 4 On or Off
function step4Inputs(highlight, enable) {
	//Number
	if (highlight == 1) document.getElementById('step4_num').className = 'card-select-num-on';		
	else document.getElementById('step4_num').className = 'card-select-num-off';
	
	//Inputs	
	if (enable == 1) {
		term_lt_1.disabled = false;
		term_1_3.disabled = false;
		term_gt_3.disabled = false;		
	}
	else {
		term_lt_1.disabled = true; term_lt_1.checked = false;	
		term_1_3.disabled = true; term_1_3.checked = false;	
		term_gt_3.disabled = true; term_gt_3.checked = false;	
	}		
}

//Resets the given steps inputs
function resetInputs(num) {
	if (num == 1) {
		goal_for_emer.checked = false;
		goal_fut_purch.checked = false;
		goal_reg_inc.checked = false; 
	}	
	else if (num == 2) {
		amt_lt_4999.checked = false;
		amt_gt_5000.checked = false;
	}		
	else if (num == 3) {
		access_yes.checked = false;
		access_no.checked = false;
	}
	else if (num == 4) {
		term_lt_1.checked = false;
		term_1_3.checked = false;
		term_gt_3.checked = false;
	}		
}

function convertWhitespace(str){
	var finish = '';
	finish = str.replace(/ /g, "_");
	
	return finish;
}

function clearTags(str){
	var finish = '';
	finish = str.replace(/<sup>&reg;<\/sup>/g, "");
	finish = finish.replace(/<sup>mc<\/sup>/g, "");
	return finish;
}


//Sorts by ranking and then displays cards held in the recommended_cards_weight array
function displayCards() {
	//Sort Cards
	recommended_cards.sort(compareCardRankings);

	//------DEBUG 
	//document.getElementById('debug').innerHTML;
	//debug();
	
	//Display Top Three
	var top3_output = '';
	var bg_color = 'contentframework-altrow';
	var top3_number = 0;
	var total_number = 0;
	for (var x=0; x < recommended_cards.length; x++) {
		if(recommended_cards[x].weight > 0){
			total_number++;
		}
	}
	
	if((term_lt_1.checked == true)||(term_1_3.checked == true)||(term_gt_3.checked == true)){
	for (var x=0; (x<3 && x<total_number); x++) {	
		var obj = recommended_cards[x];
		if(obj.weight > 0){	
			top3_output += '		<tr class="'+bg_color+'">';			
			top3_output += '			<td headers="headerGIC" id="' + convertWhitespace(clearTags(obj.name)) + '"><ul class="bullets-arrow" style="position:relative;left:-18px;"><li><a href="' + obj.learn_more + '" title="' + obj.name + '">' + obj.name + '</a></li></ul></td>';			
			top3_output += '			<td headers="headerDetails ' + convertWhitespace(clearTags(obj.name)) + '"><label for="compare_' + obj.id + '">' + obj.desc + '</label></td>';			
			top3_output += '			<td headers="headerCompare ' + convertWhitespace(clearTags(obj.name)) + '" style="text-align:center;"><input title="Comparer ' + clearTags(obj.name) + '" id="compare_' + obj.id + '" value="' + obj.id + '" type="checkbox" onclick="compareCardsUpdate(this);"></th>';
			top3_output += '		</tr>';			
			top3_number++;
			
			//Switch bg color
			bg_color = (bg_color == 'contentframework-altrow') ? '' : 'contentframework-altrow';
		}	
	}
	
	var table_output = '<div><h2>Les ' + top3_number + ' CPG les plus recommand&eacute;es :</h2>';	
	table_output += '<table class="contentframework">';
	table_output += '	<tbody class="contentframework-stripedtable">';		
	table_output += '		<tr>';			
	table_output += '			<th class="contentframework-dataheadertop" id="headerGIC" width="30%"><strong>CPG</strong></th>';			
	table_output += '			<th class="contentframework-dataheadertop" id="headerDetails" width="53%"><strong>D&eacute;tails</strong></th>';		
	table_output += '			<th class="contentframework-dataheadertop" id="headerCompare" width="17%"><strong>Comparer</strong></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 CPG les plus recommand&eacute;es :</h2>';
	other_output += '<table class="contentframework">';
	other_output += '<tbody class="contentframework-stripedtable">';		
	other_output += '		<tr>';		
	other_output += '			<th class="contentframework-dataheadertop" id="headerGIC2" width="30%"><strong>CPG</strong></th>';			
	other_output += '			<th class="contentframework-dataheadertop" id="headerDetails2" width="53%"><strong>D&eacute;tails</strong></th>';		
	other_output += '			<th class="contentframework-dataheadertop" id="headerCompare2" width="17%"><strong>Comparer</strong></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];
		if(obj.weight > 0){	
			other_output += '		<tr class="contentframework-subheader">';
			other_output += '			<th id="' + convertWhitespace(clearTags(obj.name)) + '" colspan="3">';
			other_output += '				<h4><a onclick="toggleIconFrench(\'content_' + obj.id + '\',\'icon_' + obj.id + '\');return false" id="icon_' + obj.id + '" href="#" class="toggleiconlink"><img src="/uos/_assets/images/contentframework/icon-expand.gif" alt="D&eacute;velopper">'; 
			other_output += '					<span class="toggleiconlink-label">' + obj.name + '</span></a></h4>';
			other_output += '			</th>';
			other_output += '		</tr>';
			other_output += '		<tr class="contentframework-altrow ' + convertWhitespace(clearTags(obj.name)) + ' jshide content_' + obj.id + '" style="display: none;">';			
			other_output += '			<td headers="headerGIC2 ' + convertWhitespace(clearTags(obj.name)) + '"><ul class="bullets-arrow" style="position:relative;left:-18px;"><li><a href="' + obj.learn_more + '">' + obj.name + '</a></li></ul></td>';			
			other_output += '			<td headers="headerDetails2 ' + convertWhitespace(clearTags(obj.name)) + '"><label for="compare_' + obj.id + '">' + obj.desc + '</label></td>';			
			other_output += '			<td headers="headerCompare2 ' + convertWhitespace(clearTags(obj.name)) + '" style="text-align:center;"><input title="Comparer ' + clearTags(obj.name) + '" id="compare_' + obj.id + '" value="' + obj.id + '" type="checkbox" onclick="compareCardsUpdate(this);"></th>';
			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);	
		triggerPixel(10894);
	}
	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 = '';
	}
	
	
	}else{
		document.getElementById('recommended_top3').style.display = 'none';
		document.getElementById('recommended_top3').innerHTML = '';
		document.getElementById('recommended_others').style.display = 'none';
		document.getElementById('recommended_others').innerHTML = '';
	}
}

//Comparison function for sorting, compares card ranking value
function compareCardRankings(a, b) {
	//return b.ranking - a.ranking; //highest (20) to lowest (1)
	return b.weight - a.weight; //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('goal');
	inputs_list.push('amount');
	inputs_list.push('access');
	inputs_list.push('termlength');
	
	//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 />';
}
