/*
 *	Deals with ipQuote Form
 *  Validates that all Questions have been answered
 *  and then submits answers to investorprofile.jsp for calculation
 */
  var msg = "";
  var tally = 0;

  // Validates Form input fields
  function validateForm() {
	//Initialise values
	var allAnswered = true;
	var answered1 = false;
	var answered2 = false;
	var answered3 = false;
	var answered4 = false;
	var answered5 = false;
	var answered6 = false;
	var answered7 = false;
	var answered8 = false;
	msg = "All Questions must be answered.";
	tally = 0;
	
	// Check if any button chosen for Question 1
 	for (var i=0;i<document.ipQuote.question1.length; i++){
		if (document.ipQuote.question1[i].checked){
			answered1 = true;
			if(i==0){
				tally++;
			}else if (i==1){
				tally+=2;
			}else if (i==2){
				tally+=5;
			}else if (i==3){
				tally+=10;
			}else if (i==4){
				tally+=20;
			}
		}
	}
	// Check if any button chosen for Question 2
 	for (var i=0;i<document.ipQuote.question2.length; i++){
		if (document.ipQuote.question2[i].checked){
			answered2 = true;
			if(i==0){
				tally++;
			}else if (i==1){
				tally+=2;
			}else if (i==2){
				tally+=5;
			}else if (i==3){
				tally+=10;
			}else if (i==4){
				tally+=20;
			}
		}
	}
	// Check if any button chosen for Question 3
 	for (var i=0;i<document.ipQuote.question3.length; i++){
		if (document.ipQuote.question3[i].checked){
			answered3 = true;
			if(i==0){
				tally++;
			}else if (i==1){
				tally+=2;
			}else if (i==2){
				tally+=5;
			}else if (i==3){
				tally+=10;
			}else if (i==4){
				tally+=20;
			}
		}
	}
	// Check if any button chosen for Question 4
 	for (var i=0;i<document.ipQuote.question4.length; i++){
		if (document.ipQuote.question4[i].checked){
			answered4 = true;
			if(i==0){
				tally++;
			}else if (i==1){
				tally+=2;
			}else if (i==2){
				tally+=5;
			}else if (i==3){
				tally+=10;
			}else if (i==4){
				tally+=20;
			}
		}
	}
	// Check if any button chosen for Question 5
 	for (var i=0;i<document.ipQuote.question5.length; i++){
		if (document.ipQuote.question5[i].checked){
			answered5 = true;
			if(i==0){
				tally++;
			}else if (i==1){
				tally+=2;
			}else if (i==2){
				tally+=5;
			}else if (i==3){
				tally+=10;
			}else if (i==4){
				tally+=20;
			}
		}
	}
	// Check if any button chosen for Question 6
 	for (var i=0;i<document.ipQuote.question6.length; i++){
		if (document.ipQuote.question6[i].checked){
			answered6 = true;
			if(i==0){
				tally++;
			}else if (i==1){
				tally+=2;
			}else if (i==2){
				tally+=5;
			}else if (i==3){
				tally+=10;
			}else if (i==4){
				tally+=20;
			}
		}
	}
	// Check if any button chosen for Question 7
 	for (var i=0;i<document.ipQuote.question7.length; i++){
		if (document.ipQuote.question7[i].checked){
			answered7 = true;
			if(i==0){
				tally++;
			}else if (i==1){
				tally+=2;
			}else if (i==2){
				tally+=5;
			}else if (i==3){
				tally+=10;
			}else if (i==4){
				tally+=20;
			}
		}
	}
	// Check if any button chosen for Question 8
 	for (var i=0;i<document.ipQuote.question8.length; i++){
		if (document.ipQuote.question8[i].checked){
			answered8 = true;
			if(i==0){
				tally++;
			}else if (i==1){
				tally+=2;
			}else if (i==2){
				tally+=5;
			}else if (i==3){
				tally+=10;
			}else if (i==4){
				tally+=20;
			}
		}
	}
	// Set message according to Missing answers
	if (answered1==false){
		msg += "\n Please answer Question 1.";
		allAnswered = false;	
	}
	if (answered2==false){
		msg += "\n Please answer Question 2.";
		allAnswered = false;	
	}
	if (answered3==false){
		msg += "\n Please answer Question 3.";
		allAnswered = false;	
	}
	if (answered4==false){
		msg += "\n Please answer Question 4.";
		allAnswered = false;	
	}
	if (answered5==false){
		msg += "\n Please answer Question 5.";
		allAnswered = false;	
	}
	if (answered6==false){
		msg += "\n Please answer Question 6.";
		allAnswered = false;	
	}
	if (answered7==false){
		msg += "\n Please answer Question 7.";
		allAnswered = false;	
	}
	if (answered8==false){
		msg += "\n Please answer Question 8.";
		allAnswered = false;	
	}
	return allAnswered;
}

function valAndCal()
{
  document.getElementById("profileResult").innerHTML =  "";
  if (!validateForm()){
	// Alert user of unanswered questions
	alert(msg);
	return;
  }
  // Tally up Result
  var results ="<p><Strong>Result</Strong></p>";
  if(tally<27){
	results +="<p><Strong>Prudent profile</Strong></p>"
			+"<p> Capital security is important to you. You have a low tolerance of volatility. "
			+"You are primarily seeking investments that offer a regular income "
			+"and capital preservation. We recommend the following asset allocation.</p>"
			+"<br><p><img src='http://www.timetoinvest.ca/InvestorProfile/images/prudent.gif' width='252'></p>";
  }else if (tally<56){
	results +="<p><Strong>Moderate profile</Strong></p>"
			+"<p> You are seeking a certain level of capital appreciation " 
        	+"and your tolerance to risk is moderate. You favour investments that offer " 
        	+"a relatively stable income. We recommend the following asset allocation.</p>"
			+"<br><p><img src='http://www.timetoinvest.ca/InvestorProfile/images/moderate.gif' width='252'></p>";
  }else if (tally<95){
	results +="<p><Strong>Balanced profile</Strong></p>"
			+"<p> You are seeking a balance between income and capital appreciation. "
        	+"Your risk tolerance level is average. You are targeting medium- and long-term " 
        	+"capital appreciation. We recommend the following asset allocation.</p>"
			+"<br><p><img src='http://www.timetoinvest.ca/InvestorProfile/images/balanced.gif' width='252'></p>";
  }else if (tally<130){
	results +="<p><Strong>Growth profile</Strong></p>"
			+"<p> You are seeking above-average growth and are ready to accept a high level of risk. "
			+"You are a patient investor and you do not allow yourself to be influenced "
			+"by fluctuations in your portfolio. We recommend the following asset allocation.</p>"
			+"<br><p><img src='http://www.timetoinvest.ca/InvestorProfile/images/growth.gif' width='252'></p>";
  }else if (tally<170){
	results +="<p><Strong>Aggressive profile</Strong></p>"
			+"<p> You have a strong tolerance of risk and market fluctuations do not worry you. "
			+"You are seeking superior portfolio growth and are willing to accept "
			+"substantial variations in the value of your portfolio from one year to the next. "
			+"We recommend the following asset allocation.</p>"
			+"<br><p><img src='http://www.timetoinvest.ca/InvestorProfile/images/aggressive.gif' width='252'></p>";
  }
  //Display Result
  document.getElementById("profileResult").innerHTML =  results;
}
	

