//GLOBAL VARIABLES
var cards = new Array();
var apply_link = 'https://' + document.domain + '/cgi-bin/cards/cardapp/apply.cgi';
var apply_business_link = 'https://' + document.domain + '/cgi-bin/business/credit/apply.cgi?exclusiveItem=apClassBus';
var apply_business_avion_link = 'https://' + document.domain + '/cgi-bin/business/credit/apply.cgi?exclusiveItem=apAvionBus';
var apply_creditline_link = 'https://' + document.domain + '/cgi-bin/business/credit/apply.cgi?exclusiveItem=apCreditLine';
var compare_tool_link = 'http://' + document.domain + '/cards/tools/compare/index.html';
var select_tool_link = 'http://' + document.domain + '/cards/tools/select/index.html';
var xmlhttp;
// Card object constructor
//Fields correspond to excel card data file
function Card(id, name, ranking, interest_rate, grace_period, annual_fee, annual_fee_65, add_card_fee, min_income, type_cashback, type_rewards, type_student, type_lowrate, type_nofee,type_business, feature_merchandise, feature_travel, feature_specialty, feature_usdollar, feature_insurance, benefit, feature1, feature2, special_offer, learn_more) {
	this.id=id;
	this.name=name;
	this.ranking=ranking;
	this.interest_rate=interest_rate;
	this.grace_period=grace_period;
	this.annual_fee=annual_fee;
	this.annual_fee_65=annual_fee_65;
	this.add_card_fee=add_card_fee;
	this.min_income=min_income;
	this.benefit=benefit;
	this.feature1=feature1;
	this.feature2=feature2;
	this.special_offer=special_offer;	
	this.feature_merchandise=feature_merchandise;
	this.feature_travel=feature_travel;
	this.feature_specialty=feature_specialty;
	this.feature_usdollar=feature_usdollar;
	this.feature_insurance=feature_insurance;
	this.learn_more=learn_more;
	this.type_rewards=type_rewards;
	this.type_student=type_student;
	this.type_lowrate=type_lowrate;
	this.type_nofee=type_nofee;
	this.type_business=type_business;
	
	
	
	this.type_cashback=type_cashback;
}
//Main function -- initialise cards, check query params, display cards
function initializeCards() {
	//Initialise cards
	cards.push(new Card("avion_inf", 		"Visa Infinite Avion", 				"1", 	"19.99%", 			"21", 	"$120", 	"", 	"$50", 		"$60,000", "N","Y", "N", "N", "N", "N", "Y", "Y", "N", "N", "Y", "Premium travel rewards & benefits", "No blackout periods or seat restrictions", "Earn 1 RBC Rewards point for each $1 spent", "15,000 welcome points upon enrollment<sup><a href='/cards/_assets-custom/includes/pop-up/terms-and-condition/avion_inf.html' title='Disclaimer (opens new window)' target='_blank' onclick='return popupHelp(this.href)'>&dagger;</a></sup>", "/cards/rewards-cards/avion-infinite.html"));
	cards.push(new Card("avion_p", 			"Visa Platinum Avion", 				"2", 	"19.99%", 			"21", 	"$120", 	"", 	"$50", 		"n/a", "N","Y", "N", "N", "N", "N", "Y", "Y", "N", "N", "Y", "Redeem points for flights on any airline", "No blackout periods or seat restrictions", "Earn 1 RBC Rewards point for each $1 spent", "15,000 welcome points upon enrollment<sup><a href='/cards/_assets-custom/includes/pop-up/terms-and-condition/avion_p.html' title='Disclaimer (opens new window)' target='_blank' onclick='return popupHelp(this.href)'>&dagger;</a></sup>", "/cards/rewards-cards/avion-platinum.html"));
	cards.push(new Card("gold_p", 			"Visa Gold Preferred", 				"5", 	"19.99%", 			"21", 	"$110", 	"$70", 	"$40", 		"n/a", "N","Y", "N", "N", "N", "N", "Y", "Y", "N", "N", "Y", "Comprehensive insurance coverage", "Earn 1 RBC Rewards point for each $1 spent", "Redeem points for travel, merchandise & more", "2,500 bonus points upon enrollment<sup>&dagger;</sup>", "/cards/rewards-cards/gold-preferred.html"));
	cards.push(new Card("classic2", 		"Visa Classic II", 					"7", 	"19.99%", 			"21", 	"$35", 		"", 	"Free", 	"n/a","N", "Y", "N", "N", "N", "N", "Y", "Y", "N", "N", "N", "The perfect shopping companion card", "Collect rewards points with every purchase", "Earn 1 RBC Rewards point for each $1 spent", "", "/cards/rewards-cards/classic-2.html"));
	//cards.push(new Card("classic2_student", "Visa Classic II Student", 			"15",	"19.99%", 			"21", 	"$15", 		"", 	"Free", 	"n/a","N", "Y", "N", "N", "N", "N", "Y", "Y", "N", "N", "N", "The perfect student credit card", "Collect rewards points with every purchase", "Earn 1 RBC Rewards point for each $1 spent", "", "/cards/student-cards/classic-2-student.html"));
	cards.push(new Card("rewards_gold", 	"RBC Rewards Visa Gold", 			"8", 	"19.99%", 			"21", 	"$0", 		"", 	"Free", 	"n/a", "N","Y", "Y", "N", "Y", "N", "Y", "Y", "N", "N", "Y", "A premium no annual fee credit card", "Earn 1 RBC Rewards point for each $2 spent", "Redeem points for travel, merchandise, gift certicates/cards & more", "", "/cards/rewards-cards/rewards-gold.html"));	
	cards.push(new Card("gold_us", 			"U.S. Dollar Visa Gold", 			"12",	"19.99%", 			"21", 	"$65 USD", 	"", 	"$30 USD", 	"n/a", "N","Y", "N", "N", "N", "N", "Y", "Y", "N", "Y", "Y", "The perfect option for U.S. travellers", "Make transactions in U.S. funds", "Earn 1 RBC Rewards point for each $1 U.S. spent", "", "/cards/rewards-cards/us-gold.html"));
	cards.push(new Card("ba_platinum", 		"British Airways Visa Platinum", 	"6", 	"20.50%", 			"21", 	"$165", 	"", 	"$75", 		"n/a","N", "Y", "N", "N", "N", "N", "N", "Y", "Y", "N", "Y", "31-day Out of Province/Country Emergency Medical Insurance", "One BA Mile for every dollar you spend on the card<sup>&Dagger;</sup>", "Double BA Miles for every dollar you spend on British Airways flights purchased with the card.<sup>&sect;</sup>", "10,000 bonus BA Miles upon enrollment<sup>&dagger;</sup>", "/cards/travel-cards/british-airways-platinum.html"));
	cards.push(new Card("cp_platinum", 		"Cathay Pacific Visa Platinum", 	"11", 	"20.50%", 			"21", 	"$150", 	"", 	"$75", 		"n/a", "N","Y", "N", "N", "N", "N", "Y", "Y", "Y", "N", "Y", "Earn Asia miles towards flights and more", "Earn Asia Miles for dollars spent on the card", "Complimentary enrollment in The Marco Polo Club", "5,000 bonus Asia Miles upon enrollment<sup>&dagger;</sup>", "/cards/travel-cards/cathay-pacific-platinum.html"));
	cards.push(new Card("esso", 			"Esso Visa Card", 					"9", 	"19.99%", 			"21", 	"$0", 		"", 	"Free", 	"n/a", "N","Y", "N", "N", "Y", "N", "Y", "N", "Y", "N", "N", "Earn points towards free gas at Esso stations", "No annual fee credit card", "Redeem points for car washes, gas and more", "", "/cards/no-annual-fee-cards/esso.html"));
	//cards.push(new Card("starbucks", 		"Starbucks Duetto Visa Card", 		"9", 	"19.99%", 			"21", 	"$0", 		"", 	"Free", 	"$15,000","N", "Y", "N", "N", "Y", "N", "Y", "N", "Y", "N", "N", "Receive $25 to spend at Starbucks after your first Visa purchase<sup>(+)</sup>", "Earn up to 3% of Visa spend on their Starbucks Card Account", "The flexibility and security of a Visa Credit Card, plus the speed and convenience of a reloadable Starbucks Card", "", "/cards/no-annual-fee-cards/starbucks.html"));
	cards.push(new Card("platinum", 		"Visa Platinum", 					"14", 	"19.99%", 			"21", 	"$0", 		"", 	"Free", 	"n/a", "N","N", "N", "N", "Y", "N", "N", "N", "N", "N", "Y", "No annual fee platinum credit card", "Essential travel and purchase insurance", "Credit limit of up to $50,000", "", "/cards/no-annual-fee-cards/platinum-no-fee.html"));
	cards.push(new Card("classic_lr", 		"Visa Classic Low Rate Option", 	"13", 	"11.99%", 			"21", 	"$20", 		"", 	"Free", 	"n/a","N", "N", "N", "Y", "N", "N", "N", "N", "N", "N", "N", "Value, convenience, and a low interest rate", "Save when you carry a balance on your card", "Free additional card", "", "/cards/low-interest-rate-cards/classic-low-rate.html"));
	//cards.push(new Card("classic_student", 	"Visa Classic Student", 			"16", 	"19.99%", 			"21", 	"$0", 		"", 	"Free", 	"n/a", "N","N", "N", "N", "Y", "N", "N", "N", "N", "N", "N", "Basic, everyday student Visa card", "Access to credit with no annual fee", "Free additional card", "", "/cards/student-cards/classic-student.html"));
	cards.push(new Card("westjet", 			"WestJet RBC MasterCard", 			"4", 	"19.99%", 			"21", 	"$39", 		"$39", 	"$19", 		"$15,000", "N","Y", "N", "N", "N", "N", "N", "Y", "Y", "N", "N", "Earn WestJet Dollars toward WestJet flights and WestJet Vacations.", "Earn 1% of credit card spend in WestJet Dollars ", "Earn additional 0.5% of net purchase on WestJet flights and vacations.", "$25 WestJet Dollars upon enrollment", "/cards/travel-cards/westjet.html"));
	cards.push(new Card("westjet_world", 	"WestJet RBC World MasterCard", 	"3", 	"19.99%", 			"21", 	"$79", 		"$79", 	"$39", 		"$70,000", "N",	"Y", "N", "N", "N", "N", "N", "Y", "Y", "N", "Y", "Earn WestJet Dollars toward WestJet flights and WestJet Vacations.", "Earn 1.5% of credit card spend in WestJet Dollars", "Comprehensive Insurance coverage.", "$100 WestJet Dollars upon enrollment.", "/cards/travel-cards/westjet-world-mastercard.html"));
	cards.push(new Card("business", 		"Visa Business", 					"19", 	"19.99%", 			"21",	"$12", 		"", 	"", 		"n/a", "N","N", "N", "N", "N", "Y", "N", "N", "N", "N", "N", "Flexible and affordable business card", "Low annual fee", "Additional security and protection", "", "/cards/business/visa_business.html"));
	cards.push(new Card("bus_avion", 		"Visa Business Avion", 				"17", 	"19.99%", 			"17", 	"$120", 	"", 	"$50", 		"n/a", "N","Y", "N", "N", "N", "Y", "Y", "Y", "N", "N", "Y", "Separate business and personal expenses", "Manage employee spending", "Earn 1 RBC Rewards point for each $1 spent", "15,000 welcome points upon enrollment<sup><a href='/cards/_assets-custom/includes/pop-up/terms-and-condition/bus_avion.html' title='Disclaimer (opens new window)' target='_blank' onclick='return popupHelp(this.href)'>&dagger;</a></sup>", "/cards/business/visa_business_avion.html"));
	cards.push(new Card("creditline", 		"CreditLine for Small Business", 	"20", 	"Prime + premium", 	"0", 	"$0", 		"", 	"Free", 	"n/a", "N","Y", "N", "Y", "Y", "Y", "Y", "Y", "N", "N", "N", "Line of credit with the convenience of a credit card", "Low rate, no annual fee", "Earn RBC Rewards points", "", "/cards/business/creditline.html"));
	cards.push(new Card("cashback", 		"Cash Back Card", 					"21", 	"19.99%", 			"21", 	"$19", 		"", 	"Free", 	"$15,000", "Y","N", "Y", "N", "N", "N", "N", "N", "N", "N", "N", "Earn 1% cash back on all qualifying purchases<sup>1</sup>", "Use our free CustomSwitch<sup>&reg;</sup> service", "", "", "/cards/cash-back-cards/cash-back-cards.html"));
}
function initializeCardsFromXML(file) {
	if( window.XMLHttpRequest){
		xhttp =  new XMLHttpRequest();
	}
	else{ // IE 5/6
		xhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	xhttp.open("GET", file, false);
	xhttp.send("");
	var xmlDoc = xhttp.responseXML;
	var card_nodes = xmlDoc.getElementsByTagName("card");
	for(var i = 0; i < card_nodes.length; i++){
		cards.push(
			new Card(
				card_nodes[i].getElementsByTagName("id")[0].childNodes[0].nodeValue,
				card_nodes[i].getElementsByTagName("name")[0].childNodes[0].nodeValue,
				card_nodes[i].getElementsByTagName("ranking")[0].childNodes[0].nodeValue,
				card_nodes[i].getElementsByTagName("interest_rate")[0].childNodes[0].nodeValue,
				card_nodes[i].getElementsByTagName("grace_period")[0].childNodes[0].nodeValue,
				card_nodes[i].getElementsByTagName("annual_fee")[0].childNodes[0].nodeValue,
				card_nodes[i].getElementsByTagName("annual_fee_65")[0].childNodes[0].nodeValue,
				card_nodes[i].getElementsByTagName("add_card_fee")[0].childNodes[0].nodeValue,
				card_nodes[i].getElementsByTagName("min_income")[0].childNodes[0].nodeValue,
				card_nodes[i].getElementsByTagName("type_rewards")[0].childNodes[0].nodeValue,
				card_nodes[i].getElementsByTagName("type_student")[0].childNodes[0].nodeValue,
				card_nodes[i].getElementsByTagName("type_lowrate")[0].childNodes[0].nodeValue,
				card_nodes[i].getElementsByTagName("type_nofee")[0].childNodes[0].nodeValue,
				//card_nodes[i].getElementsByTagName("type_lowfee")[0].childNodes[0].nodeValue,
				card_nodes[i].getElementsByTagName("type_business")[0].childNodes[0].nodeValue,
				card_nodes[i].getElementsByTagName("feature_merchandise")[0].childNodes[0].nodeValue,
				card_nodes[i].getElementsByTagName("feature_travel")[0].childNodes[0].nodeValue,
				card_nodes[i].getElementsByTagName("feature_specialty")[0].childNodes[0].nodeValue,
				card_nodes[i].getElementsByTagName("feature_usdollar")[0].childNodes[0].nodeValue,
				card_nodes[i].getElementsByTagName("feature_insurance")[0].childNodes[0].nodeValue,
				card_nodes[i].getElementsByTagName("benefit")[0].childNodes[0].nodeValue,
				card_nodes[i].getElementsByTagName("feature1")[0].childNodes[0].nodeValue,
				card_nodes[i].getElementsByTagName("feature2")[0].childNodes[0].nodeValue,
				card_nodes[i].getElementsByTagName("special_offer")[0].childNodes[0].nodeValue,
				card_nodes[i].getElementsByTagName("learn_more")[0].childNodes[0].nodeValue	
			)
		);
	}
}
//will not out <tags> or html &characters; properly
function cardsToXML(){
	document.write("<pre>");
	document.write("&lt;?xml version=\"1.0\"?>\n");
	document.write("&lt;cards><br>");
	for(var i in cards){
		document.write("\t&lt;card><br>");
		document.write("\t\t&lt;id>"+cards[i].id+"&lt;/id>\n");
		document.write("\t\t&lt;name>"+cards[i].name+"&lt;/name>\n");
		document.write("\t\t&lt;ranking>"+cards[i].ranking+"&lt;/ranking>\n");
		document.write("\t\t&lt;interest_rate>"+cards[i].interest_rate+"&lt;/interest_rate>\n");
		document.write("\t\t&lt;grace_period>"+cards[i].grace_period+"&lt;/grace_period>\n");
		document.write("\t\t&lt;annual_fee>"+cards[i].annual_fee+"&lt;/annual_fee>\n");
		document.write("\t\t&lt;annual_fee_65>"+cards[i].annual_fee_65+"&lt;/annual_fee_65>\n");
		document.write("\t\t&lt;add_card_fee>"+cards[i].add_card_fee+"&lt;/add_card_fee>\n");
		document.write("\t\t&lt;min_income>"+cards[i].min_income+"&lt;/min_income>\n");
		document.write("\t\t&lt;type_rewards>"+cards[i].type_rewards+"&lt;/type_rewards>\n");
		document.write("\t\t&lt;type_student>"+cards[i].type_student+"&lt;/type_student>\n");
		document.write("\t\t&lt;type_lowrate>"+cards[i].type_lowrate+"&lt;/type_lowrate>\n");
		document.write("\t\t&lt;type_nofee>"+cards[i].type_nofee+"&lt;/type_nofee>\n");
		document.write("\t\t&lt;type_lowfee>"+cards[i].type_lowfee+"&lt;/type_lowfee>\n");
		document.write("\t\t&lt;type_business>"+cards[i].type_business+"&lt;/type_business>\n");
		document.write("\t\t&lt;feature_merchandise>"+cards[i].feature_merchandise+"&lt;/feature_merchandise>\n");
		document.write("\t\t&lt;feature_travel>"+cards[i].feature_travel+"&lt;/feature_travel>\n");
		document.write("\t\t&lt;feature_specialty>"+cards[i].feature_specialty+"&lt;/feature_specialty>\n");
		document.write("\t\t&lt;feature_usdollar>"+cards[i].feature_usdollar+"&lt;/feature_usdollar>\n");
		document.write("\t\t&lt;feature_insurance>"+cards[i].feature_insurance+"&lt;/feature_insurance>\n");
		document.write("\t\t&lt;benefit>"+cards[i].benefit+"&lt;/benefit>\n");
		document.write("\t\t&lt;feature1>"+cards[i].feature1+"&lt;/feature1>\n");
		document.write("\t\t&lt;feature2>"+cards[i].feature2+"&lt;/feature2>\n");
		document.write("\t\t&lt;special_offer>"+cards[i].special_offer+"&lt;/special_offer>\n");
		document.write("\t\t&lt;learn_more>"+cards[i].learn_more+"&lt;/learn_more>\n");
		document.write("\t&lt;/card><br>");
	}
	document.write("&lt;/cards><br>");
	document.write("</pre>");
}
//Takes out "$", ",", "N/A", "n/a" from string to make it a valid number
function convertToNumber(str){
	var number = 0;
	number = str.replace(/\$/, "");
	number = number.replace(/,/, "");
	number = number.replace(/N\/A/, "");
	number = number.replace(/n\/a/, "");
	
	if (number == '') number = 0.00;
	
	return Number(number);
}
//Retrieves card object with specific id
function getCardObjectById(cid) {
	var temp;
	for (var x=0; x< cards.length; x++) {
		temp = cards[x].id;
		if (cid == temp) return cards[x];
	}
	
	return null;
}
//Retrieves specified parameter from URL string
function getURLParam(strParamName) {	
	// GetURL Params value from querystring
	var qs = location.search.split('?');
	if (qs.length > 1) {
	var pairs = qs[1].split("&");
		for (var i=0; i<pairs.length; i++) {
			var namevalue = pairs[i].split("=", 2);
			var name = unescape(namevalue[0]);
			var value = unescape(namevalue[1]);
			if (name.toUpperCase() == strParamName.toUpperCase()) {
				return value;
				//break;
			}
		}
	}
	return null;
}
