/************************************Define the Audi card types***************************************************/
var cards= new Array();
//Structure of a question answer object (Card id, Card Name,weight,image)
cards[0]=new Array(1,'American Express Blue',0,'amex_blue.gif','amexblue.htm');
cards[1]=new Array(2,'American Express Gold',0,'amex_gold.gif','amexgold.htm');
cards[2]=new Array(3,'American Express Green',0,'amex_green.gif','amexgreen.htm');
cards[3]=new Array(4,'American Express Platinum',0,'amex_s.gif','amex_platinum.htm');
cards[4]=new Array(5,'MasterCard Business',0,'master_business.gif','business.htm');
cards[5]=new Array(6,'MasterCard Cedar Miles',0,'master_mea.gif','mea_card.htm');
cards[6]=new Array(7,'MasterCard EuroZone',0,'master_euro.gif','eurozone.htm');
cards[7]=new Array(8,'MasterCard Horizon Plus Blue',0,'hb.gif','horizon_blue.htm');
cards[8]=new Array(9,'MasterCard Horizon Plus Orange',0,'ho.gif','horizon_orange.htm');
cards[9]=new Array(10,'MasterCard Indigo',0,'master_indigo.gif','mastercard_indigo.htm');
cards[10]=new Array(11,'MasterCard Sagesse',0,'master_sagesse.gif','sagesse.htm');
cards[11]=new Array(12,'MasterCard Shine',0,'shine.gif','shine.htm');
cards[12]=new Array(13,'MasterCard Titanium',0,'titanium.gif','titanium.htm');
cards[13]=new Array(14,'MasterCard World',0,'wmc.gif','world_MasterCard.htm');
cards[14]=new Array(15,'MasterCard/Visa Embark',0,'master_embark.gif','embark.htm');
cards[15]=new Array(16,'Montecristo Visa Platinum',0,'monte_s.gif','monte_cristo.htm');
cards[16]=new Array(17,'Visa Infinite',0,'infinite_s.gif','infinite.htm');
cards[17]=new Array(18,'Visa Platinum Montecristo',0,'monte_s.gif','monte_cristo.htm');
cards[18]=new Array(19,'Visa/MasterCard Gold',0,'master_gold.gif','visagold.htm');
cards[19]=new Array(20,'Visa/MasterCard Silver',0,'master_silver.gif','visasilver.htm');
cards[20]=new Array(21,'MasterCard World',0,'wmc.gif','world_MasterCard.htm');
/************************************End of Defining the Audi card types***************************************************/


/************************************Define the question answers***************************************************/
var questionAnswers= new Array();
//Structure of a question answer object (question id, answer id, question, answer,input type, minumim num of answers, max num of answers)
//Question 1
questionAnswers[0]=new Array(1,1,'You need a card that helps you to: (select up to two)','Control your expenses with low interest rates and balance transfers','checkbox',1,2);
questionAnswers[1]=new Array(1,2,'You need a card that helps you to: (select up to two)','Redeem points for FREE rewards and airline tickets','checkbox',1,2);
questionAnswers[2]=new Array(1,3,'You need a card that helps you to: (select up to two)','Earn cash back on purchases','checkbox',1,2);
questionAnswers[3]=new Array(1,4,'You need a card that helps you to: (select up to two)','Pay in equal monthly installments with 0% interest','checkbox',1,2);
questionAnswers[4]=new Array(1,5,'You need a card that helps you to: (select up to two)','Collect travel rewards such as airline miles','checkbox',1,2);
questionAnswers[5]=new Array(1,6,'You need a card that helps you to: (select up to two)','Travel to Europe without worrying about Euro exchange rates','checkbox',1,2);
questionAnswers[6]=new Array(1,7,'You need a card that helps you to: (select up to two)','Get luxury treatment and exclusive privileges','checkbox',1,2);
questionAnswers[7]=new Array(1,8,'You need a card that helps you to: (select up to two)','Have payment flexibility and credit facilities','checkbox',1,2);
questionAnswers[8]=new Array(1,9,'You need a card that helps you to: (select up to two)','Distinguish yourself with a modern product','checkbox',1,2);
//Question 2
questionAnswers[9]=new Array(2,1,'Are you a student currently enrolled in an accredited college or university? (Note: You must be 18 or older to apply for a card)','No','radio',1,1);
questionAnswers[10]=new Array(2,2,'Are you a student currently enrolled in an accredited college or university? (Note: You must be 18 or older to apply for a card)','Yes','radio',1,1);
//Question 3
questionAnswers[11]=new Array(3,1,'Are you a small business owner looking for a product to help you manage your business expenses?','No','radio',1,1);
questionAnswers[12]=new Array(3,2,'Are you a small business owner looking for a product to help you manage your business expenses?','Yes','radio',1,1);
//Question 4
questionAnswers[13]=new Array(4,1,'Are you a lady looking for a product offering you distinguished benefits?','No','radio',1,1);
questionAnswers[14]=new Array(4,2,'Are you a lady looking for a product offering you distinguished benefits?','Yes','radio',1,1);
//Question 5
questionAnswers[15]=new Array(5,1,'Are you an internet savvy who likes to shop online?','No','radio',1,1);
questionAnswers[16]=new Array(5,2,'Are you an internet savvy who likes to shop online?','Yes','radio',1,1);

/************************************End of Defining the question answers****************************************/


/************************************Link the answers to the Cards***************************************************/
var questionAnswerCards= new Array();
//Structure of a question answer Cards object (question id, answer id, card id)
//Question 1 Answer 1
questionAnswerCards[0]=new Array(1,1,9);
//Question 1 Answer 2
questionAnswerCards[1]=new Array(1,2,1);
questionAnswerCards[2]=new Array(1,2,2);
questionAnswerCards[3]=new Array(1,2,3);
questionAnswerCards[4]=new Array(1,2,4);
questionAnswerCards[5]=new Array(1,2,5);
questionAnswerCards[6]=new Array(1,2,7);
questionAnswerCards[7]=new Array(1,2,8);
questionAnswerCards[8]=new Array(1,2,9);
questionAnswerCards[9]=new Array(1,2,12);
questionAnswerCards[10]=new Array(1,2,15);
questionAnswerCards[11]=new Array(1,2,16);
questionAnswerCards[12]=new Array(1,2,17);
questionAnswerCards[13]=new Array(1,2,19);
questionAnswerCards[14]=new Array(1,2,20);
questionAnswerCards[15]=new Array(1,2,21);
//Question 1 Answer 3
questionAnswerCards[16]=new Array(1,3,1);
questionAnswerCards[17]=new Array(1,3,8);
questionAnswerCards[18]=new Array(1,3,9);
//Question 1 Answer 4
questionAnswerCards[19]=new Array(1,4,8);
//Question 1 Answer 5
questionAnswerCards[20]=new Array(1,5,6);
//Question 1 Answer 6
questionAnswerCards[21]=new Array(1,6,7);
//Question 1 Answer 7
questionAnswerCards[22]=new Array(1,7,4);
questionAnswerCards[23]=new Array(1,7,17);
questionAnswerCards[24]=new Array(1,7,18);
questionAnswerCards[25]=new Array(1,7,21);
//Question 1 Answer 8
questionAnswerCards[26]=new Array(1,8,1);
questionAnswerCards[27]=new Array(1,8,5);
questionAnswerCards[28]=new Array(1,8,6);
questionAnswerCards[29]=new Array(1,8,7);
questionAnswerCards[30]=new Array(1,8,8);
questionAnswerCards[31]=new Array(1,8,9);
questionAnswerCards[32]=new Array(1,8,10);
questionAnswerCards[33]=new Array(1,8,11);
questionAnswerCards[34]=new Array(1,8,12);
questionAnswerCards[35]=new Array(1,8,15);
questionAnswerCards[36]=new Array(1,8,16);
questionAnswerCards[37]=new Array(1,8,19);
questionAnswerCards[38]=new Array(1,8,20);
//Question 1 Answer 9
questionAnswerCards[39]=new Array(1,9,12);
questionAnswerCards[40]=new Array(1,9,13);
//Question 2 Answer 1
questionAnswerCards[41]=new Array(2,2,10);
//Question 3 Answer 1
questionAnswerCards[42]=new Array(3,2,5);
//Question 4 Answer 1
questionAnswerCards[43]=new Array(4,2,12);
//Question 5 Answer 1
questionAnswerCards[44]=new Array(5,2,15);
/************************************End of linking the answers to cards*********************************************/

//Reset the variables of the form
function resetFields()
{
	//Loop through all the cards to reset their weight
	for(var counter=0;counter<cards.length;counter++)
	{
		//Reset the weight of the selected card if it matches the id sent by parameter
		cards[counter][2]=0;
	}
}

//Fill the HTML content of the form dynamically
function fillForm()
{
	//Reset all variables
	resetFields();

	//Get the main cell to fill the content in
	var mainCell = document.getElementById('tdMainCell');
	
	//Specify the minimum and maximum number of answers per question
	var minAnswers=0;
	var maxAnswers=0;

	//Loop through all the questions
	var HTMLContent = "<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">"
		             +"	<tr>"
					 +"		<td class=\"Orange\" style=\"padding-top:24px; padding-left:5px; font-weight:bold; font-size:11px;\" id=\"Wrong\"><strong>"
					 +"		</td>"
					 +"	</tr>"
					 +"	<tr>"
					 +"		<td class=\"Maps2\" style=\"padding-top:24px; padding-left:5px;\"><strong>"
					 +"			Answer the following few questions and let us recommend the card that’s best suited to your needs."
					 +"		</strong></td>"
					 +"	</tr>";
	var oldQuestion="";
	for(var counter=0;counter<questionAnswers.length;counter++)
	{
		var QuestionAnswerObj=questionAnswers[counter];
		var checked="";
		//Show a new radio button in case of a new question
		if(QuestionAnswerObj[0]!=oldQuestion)
		{
			//Add a new row for the new question
			HTMLContent+="<tr>"
						+"	<td  style=\"height:15px;font-size:1px;\">"
						+"		"
			            +"	</td>"
						+"</tr>"
						+"<tr>"
						+"	<td class=\"tdQuestion\">"
						+"		"+QuestionAnswerObj[2]
			            +"	</td>"
						+"</tr>";

			checked="Checked";
			
			//Specify the minimum and maximum number of answers per question
			var minAnswers=QuestionAnswerObj[5];
			var maxAnswers=QuestionAnswerObj[6];
		}
				
		//Case showing the answers' options for the questions
		HTMLContent+="<tr>"
					+"	<td class=\"tdAnswer\">"
					+"		<input type=\""+QuestionAnswerObj[4]+"\" id=\"rd"+QuestionAnswerObj[0]+"\" name=\"rd"+QuestionAnswerObj[0]+"\" value=\""+QuestionAnswerObj[1]+"\" "+checked+" "+(QuestionAnswerObj[4]=='checkbox'?"onclick='return checkLimit(this,"+minAnswers+","+maxAnswers+")';":"")+"> "+QuestionAnswerObj[3]
					+"	</td>"
					+"</tr>";
		oldQuestion=QuestionAnswerObj[0];
	}

	//Add the button to the HTML content
	HTMLContent+="<tr>"
				+"	<td align=\"center\">"
			/*	+"		<input type=\"button\" value=\"Submit\" class=\"submit\" onclick=\"calculate();\">"*/
			    +"<img src=\"images/sub.jpg\" class=\"hand\" onclick=\"calculate();\">"
				+"	</td>"
				+"</tr>";
	
	//Close the HTML table tag
	HTMLContent+="</table>";

	//Fill the content in the main cell
	mainCell.innerHTML=HTMLContent;
}


//Calculate which is the most suitable card based on the selected values
function calculate()
{
	var oldQuestion="";
	//Loop through all the selected questions to parse their selected values
	for(var counter=0;counter<questionAnswers.length;counter++)
	{
		var QuestionAnswerObj=questionAnswers[counter];

		//Get the value of each question once even if there are multiple answers to it
		if(QuestionAnswerObj[0]!=oldQuestion)
		{
			//Parse the value for the selected answer
			var questionRadioButton=eval('document.frmCards.rd'+QuestionAnswerObj[0]);
			
			var selectedAnswers=getSelectedAnswers(questionRadioButton);

			//loop through all selected values to upadte the card weights
			var loopCounter=0;
			for(loopCounter=0;loopCounter<selectedAnswers.length;loopCounter++)
			{
				//Calculate the weight of the cards based on the answer selected
				calculateCardWeights(QuestionAnswerObj[0],selectedAnswers[loopCounter]);
			}
		}
		oldQuestion=QuestionAnswerObj[0];
	}

	//Get the highest weight amongst the cards
	var highest=getHighestWeight();

	//Show the cards that the user can use based on the selected criteria
	showResults(highest);
	top.location.href='comparecards.htm#top';
}

//Get the selected value of a certain radio button
function getSelectedAnswers(radioObject)
{
	var selectedAnswers=new Array();
	//Loop through all the options of the current radio button
	for(var i=0;i<radioObject.length;i++)
	{
		//If the option is checked then return its result
		if(radioObject[i].checked)
		{
			//return radioObject[i].value;
			selectedAnswers.push(radioObject[i].value);
		}
	}
	return selectedAnswers;
}

//Calculate the weight of the cards based on the question and answer id
function calculateCardWeights(questionid, answerid)
{
	//Loop through all the card/questions/answers links
	for(var counter=0;counter<questionAnswerCards.length;counter++)
	{
		//Update the weights if the question and answer id match
		if(questionAnswerCards[counter][0]==questionid && questionAnswerCards[counter][1]==answerid)
		{
			//Add the weight of th card/s that matches the question and answer
			addCardWeight(questionAnswerCards[counter][2]);
		}
	}
}

//Update the weight of a card based on the card id sent as a parameter
function addCardWeight(cardId)
{
	//Loop through all the cards
	for(var counter=0;counter<cards.length;counter++)
	{
		//Increment the weight of the selected card if it matches the id sent by parameter
		if(cards[counter][0]==cardId)
		{
			cards[counter][2]=cards[counter][2]+1;
		}
	}
}

//Get the higest weight of all the cards
function getHighestWeight()
{
	var highest=0;
	//Loop through all the cards
	for(var counter=0;counter<cards.length;counter++)
	{
		//Check if the weight of this card is the highest
		if(cards[counter][2]>highest)
			highest=cards[counter][2];
	}

	return highest;
}

//Check the minimum and maximum answers per question
function checkLimit(answer, minAns, maxAns)
{
	//Get reference to the answer object
	var answerObj=eval('document.frmCards.'+answer.id);
	//Get the selected answers of the current question
	var selectedAnswers=getSelectedAnswers(answerObj);
	//Get the number of selected answers
	var selectedAnswersNum=selectedAnswers.length;
	
	//Check if below minimum
	if(selectedAnswersNum<minAns)
	{
		document.getElementById('Wrong').innerHTML="You should select at least "+minAns+" answers for this question.";
		return false;
	}
	
	if(selectedAnswersNum>maxAns)
	{
		document.getElementById('Wrong').innerHTML="You can select maximum "+maxAns+" answers for this question.";
		return false;
	}
		
	return true;
	
}


//Display the suitable cards based on the selected answers
function showResults(highest)
{
	var HTMLContent="<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">"
	               +"<tr>"
						+"	<td  style=\"height:15px;font-size:1px;\">"
						+"		"
			            +"	</td>"
						+"</tr>"
				   +"	<tr>"
				   +"		<td class=\"tdSearchResultTitle\"><strong>"
				   +"			Based on your answers, we have selected the following products just for you:"
				   +"		</strong></td>"
				   +"	</tr>";

	//Loop through all the cards
	for(var counter=0;counter<cards.length;counter++)
	{
		//Check if the weight of this card is the highest
		if(cards[counter][2]==highest)
		{
			HTMLContent+="<tr>"
					    +"	<td class=\"tdSearchResultText\" style=\"padding-top:15px;\">"
						+"		<a href=\""+cards[counter][4]+"\"><img class=\"tdSearchResultImage\" src=\"images/cards/"+cards[counter][3]+"\">"+cards[counter][1]+"</a>"
						+"	</td>"
			            +"</tr>"
		}
	}

	//Add the back button to the HTML content
	HTMLContent+="<tr>"
				+"	<td align=\"center\" style=\"padding-top:15px;\">"
				/*+"		<input type=\"button\" value=\"Look for another card\" class=\"submit\" onclick=\"fillForm();\">"*/
				+"<img src=\"images/look.jpg\" class=\"hand\" onclick=\"fillForm();\">"
				+"	</td>"
				+"</tr>";
	
	//Close the HTML table tag
	HTMLContent+="</table>";

	//Get the main cell to fill the content in
	var mainCell = document.getElementById('tdMainCell');

	//Fill the results in the main cell
	mainCell.innerHTML=HTMLContent;
}