//Risk Capacity value
var vRiskCapacity=0;
//Risk Behavaior value
var vRiskBehaviour = 0;
var vEquityVal = 0;
var vDebtVal = 0;
var vDescRiskBehavior = "";
var vDescRiskCapacity = "";
var arrSuggMsgs = new Array([
	['Very High',[
	['Aggressive','Current Asset Allocation may be maintained'],
	['Very Aggressive','Current Asset Allocation may be maintained'],
	['Balanced','Equity Exposure may be increased marginally'],
	['Conservative','Equity Exposure may be increased'],
	['Very Conservative','Equity Exposure may be increased']]],
	['High',[
	['Aggressive','Current Asset Allocation may be maintained'],
	['Very Aggressive','Current Asset Allocation may be maintained'],
	['Balanced','Equity Exposure may be increased marginally'],
	['Conservative','Equity Exposure may be increased'],
	['Very Conservative','Equity Exposure may be increased']]],
	['Medium',[
	['Aggressive','Equity exposure may be reduced'],
	['Very Aggressive','Equity Exposure may be reduced'],
	['Balanced','Current Asset Allocation may be maintained'],
	['Conservative','Equity Exposure may be increased'],
	['Very Conservative','Equity Exposure may be increased']]],
	['Low',[
	['Aggressive','Equity exposure may be reduced'],
	['Very Aggressive','Equity Exposure may be reduced'],
	['Balanced','Equity Exposure may be reduced'],
	['Conservative','Current Asset Allocation may be maintained'],
	['Very Conservative','Equity Exposure may be increased marginally']]],
	['Very Low',[
	['Aggressive','Equity exposure may be reduced'],
	['Very Aggressive','Equity Exposure may be reduced'],
	['Balanced','Equity Exposure may be reduced'],
	['Conservative','Equity Exposure may be decreased marginally'],
	['Very Conservative','Current Asset Allocation may be maintained']]]
	]);
//alert(arrSuggMsgs[0][0][0][0].length);
function getSuggestionMsg(vRiskCapacity, vRiskBehavior)
{
	//capacity round
	for(i = 0; i < arrSuggMsgs[0].length; i++)
	{
		//behavior round
		//alert("Matching " + arrSuggMsgs[0][i][0] + " with " + vRiskCapacity);
		if(arrSuggMsgs[0][i][0] == vRiskCapacity)
		{
			//alert("Matching behavior");
			arrRiskBeh = arrSuggMsgs[0][i][1];
			for(j=0; j < arrRiskBeh.length; j++)
			{
				//alert("Matching " + vRiskBehavior + " with " + arrRiskBeh[j][0]);
				if(arrRiskBeh[j][0] ==  vRiskBehavior)
				{
					//alert("Behavior Matched");
					return arrRiskBeh[j][1];
				}
			}
		}//for behavior round
	}//for capacity round
}//getSuggestionMsg

// Risk Analyzer Calculator 
function getCapacity(fieldQuestion1,fieldQuestion2,fieldQuestion3,fieldQuestion4,fieldQuestion5)
{
	var vQuestion1 = fieldQuestion1.value;
	var vQuestion2 = fieldQuestion2.value;
	var vQuestion3 = fieldQuestion3.value;
	var vQuestion4 = fieldQuestion4.value;
	var vQuestion5 = fieldQuestion5.value;	
			
	var vRiskCapacityValue = document.getElementById('RiskCapcity');
	var vRiskCapacityMsg = document.getElementById('RiskCapacityMsg');
	
	vRiskCapacity = ( parseInt(vQuestion1) + parseInt(vQuestion2) + parseInt(vQuestion3) + parseInt(vQuestion4) +
	parseInt(vQuestion5) )/10; 
	//vRiskCapacityValue.innerHTML = vRiskCapacity;	

	if(vRiskCapacity !=0)
	{		
		if(vRiskCapacity < 21)
		{
			vDescRiskCapacity = new String("Very Low");
		}
		else if(vRiskCapacity >=21 && vRiskCapacity < 27)
		{
			vDescRiskCapacity = new String("Low");
		}
		else if(vRiskCapacity >=27 && vRiskCapacity < 34)
		{
			vDescRiskCapacity = new String("Medium");
		}
		else if(vRiskCapacity >=34 && vRiskCapacity < 40)
		{
			 vDescRiskCapacity = new String("High");
		}
		else 			
		{
			vDescRiskCapacity = new String("Very High");
		}
		vRiskCapacityMsg.innerHTML = vDescRiskCapacity;
	}
	else
	{
		vRiskCapacityValue.innerHTML = "";//vRiskCapacity;
	}
	refreshGraph();
}

function refreshGraph()
{
	var vTdSuggestion = document.getElementById('SuggestionMsg');

	if(vEquityVal > 0 || vDebtVal > 0)
		drawPieChart(vEquityVal, vDebtVal);
	//alert("Behaviour :"+ vRiskBehaviour + " Risk Capacity :" + vRiskCapacity);
	//if(vRiskBehaviour > 0 || vRiskCapacity > 0)
	//	drawBar((vRiskBehaviour/280)*100, (vRiskCapacity/47)*100);
	//alert("Capacity " + vDescRiskCapacity + " Behavior " +vDescRiskBehavior);
	vMsg = getSuggestionMsg(vDescRiskCapacity, vDescRiskBehavior);//"Current asset allocation may be maintained";
	if(vMsg == null || vMsg == 'undefined')
		vTdSuggestion.innerHTML = "";
	else
		vTdSuggestion.innerHTML = vMsg;
}

/*
	This mehtod calculates the risk Behaviour based on answers of first six questions 
*/
function getRiskBehavior(fieldQuestion6,fieldQuestion7,fieldQuestion8,fieldQuestion9,fieldQuestion10,fieldQuestion11)
{
	lstQuestion6 = fieldQuestion6.value;
	lstQuestion7 = fieldQuestion7.value;
	lstQuestion8 = fieldQuestion8.value;
	lstQuestion9 = fieldQuestion9.value;
	lstQuestion10 = fieldQuestion10.value;
	lstQuestion11 = fieldQuestion11.value;
	
	var vRiskBehaviourValue = document.getElementById('RiskBehaviour');
	var vRiskBehaviourMsg = document.getElementById('RiskBehaviourMsg');
	
	vRiskBehaviour = parseInt(lstQuestion6) + parseInt(lstQuestion7) + parseInt(lstQuestion8) + parseInt(lstQuestion9) +
		parseInt(lstQuestion10) + parseInt(lstQuestion11);
	
	if(vRiskBehaviour !=0)
	{
		if(vRiskBehaviour >= 236 )//&& vRiskBehaviour > 236)
		{
			vDescRiskBehavior = new String("Very Aggressive");
		}
		else if(vRiskBehaviour >= 192)// && vRiskBehaviour > 192)
		{
			vDescRiskBehavior = new String("Aggressive");
		}
		else if( vRiskBehaviour >= 148 )//&& vRiskBehaviour > 148)
		{
			vDescRiskBehavior = new String("Balanced");
		}
		else if(vRiskBehaviour >= 104)
		{
			vDescRiskBehavior = new String("Conservative");
		}
		else
		{
			 vDescRiskBehavior = new String("Very Conservative");
		}
		vRiskBehaviourMsg.innerHTML = vDescRiskBehavior;
	}
	 
	//shows Risk behaviour Value in bar chart
	//calls the equity function
	getEquityDebt();
	refreshGraph();
}

//This function returs the equity amount
function getEquityDebt()
{
	var vRiskCapacityIndex = 0;
	var vRiskBehaviorIndex = 0;
	var vScore = 0;
			
	//Calculates the Risk capacity Index
	if(vRiskCapacity != 0)
	{
		if( vRiskCapacity > 17.75)
		{
			vRiskCapacityIndex = ((vRiskCapacity-14.5)/32.5) * 100;
			vRiskCapacityIndex = Math.round(vRiskCapacityIndex * 1000) /1000;
		}
		else
		{
			vRiskCapacityIndex = ((10/100) + (vRiskCapacity-14.5)/3250 ) * 100;
			vRiskCapacityIndex = Math.round(vRiskCapacityIndex * 1000) /1000;
		}
	}
	
	//Calculates the risk behavior index
	if(vRiskBehaviour != 0)
	{
		if(vRiskBehaviour > 82)
		{
			vRiskBehaviorIndex = ((vRiskBehaviour-60)/220) * 100;
			vRiskBehaviorIndex = Math.round(vRiskBehaviorIndex * 1000) /1000;
		}
		else
		{		
			vRiskBehaviorIndex = ( 10/100 + parseInt((vRiskBehaviour-60) / (100 * (280-60)))) * 100;
			vRiskBehaviorIndex = Math.round(vRiskBehaviorIndex * 1000) /1000;	
		}
	}
	
	vScore = parseInt(vRiskCapacityIndex * 0.3) + parseInt(vRiskBehaviorIndex * 0.7);
	//calculates the equity value
	vEquityVal = 20 + parseInt(((vScore -10) * 40 ) / 90);	
	//calculates the debt value
	vDebtVal = 100 - vEquityVal;
}


/* Code added for new Risk Analyzer starts */
function calculateRisk(fieldQuestion1,fieldQuestion2,fieldQuestion3,fieldQuestion4,fieldQuestion5,fieldQuestion6,fieldQuestion7){
	var vEquity;
	var vDebt;
	var arrQuestion = new Array(40,5,10,25,10,5,5);

	var vQuestion1Weight = fieldQuestion1.value*arrQuestion[0]/100;
	var vQuestion2Weight = fieldQuestion2.value*arrQuestion[1]/100;
	var vQuestion3Weight = fieldQuestion3.value*arrQuestion[2]/100;
	var vQuestion4Weight = fieldQuestion4.value*arrQuestion[3]/100;
	var vQuestion5Weight = fieldQuestion5.value*arrQuestion[4]/100;
	var vQuestion6Weight = fieldQuestion6.value*arrQuestion[5]/100;
	var vQuestion7Weight = fieldQuestion7.value*arrQuestion[6]/100;

	var vTotalScore = vQuestion1Weight+vQuestion2Weight+vQuestion3Weight+vQuestion4Weight+
			vQuestion5Weight+vQuestion6Weight+vQuestion7Weight;
			
	var arrScoreBand = new Array(4.75,8,12,16,20,24,29,34,40.75);

	if(vTotalScore < arrScoreBand[1] && vTotalScore >= arrScoreBand[0]){
		vEquity = 20;
		vDebt = 80;
	}else if(vTotalScore < arrScoreBand[2] && vTotalScore >= arrScoreBand[1]){
		vEquity = 30;
		vDebt = 70;
	}else if(vTotalScore < arrScoreBand[3] && vTotalScore >= arrScoreBand[2]){
		vEquity = 40;
		vDebt = 60;
	}else if(vTotalScore < arrScoreBand[4] && vTotalScore >= arrScoreBand[3]){
		vEquity = 50;
		vDebt = 50;
	}else if(vTotalScore < arrScoreBand[5] && vTotalScore >= arrScoreBand[4]){
		vEquity = 60;
		vDebt = 40;
	}else if(vTotalScore < arrScoreBand[6] && vTotalScore >= arrScoreBand[5]){
		vEquity = 70;
		vDebt = 30;
	}else if(vTotalScore < arrScoreBand[7] && vTotalScore >= arrScoreBand[6]){
		vEquity = 80;
		vDebt = 20;
	}
	else if(vTotalScore <= arrScoreBand[8] && vTotalScore >= arrScoreBand[7]){
		vEquity = 90;
		vDebt = 10;
	}
	drawPieChart(vEquity, vDebt);
}

function refreshAssetGraph(fieldQuestion1,fieldQuestion2,fieldQuestion3,fieldQuestion4,fieldQuestion5,fieldQuestion6,fieldQuestion7){
	
	if(fieldQuestion1.value != -1 && fieldQuestion2.value != -1 && fieldQuestion3.value != -1 && 
	fieldQuestion4.value != -1 && fieldQuestion5.value != -1 && fieldQuestion6.value != -1 && fieldQuestion7.value != -1)

		calculateRisk(fieldQuestion1,fieldQuestion2,fieldQuestion3,fieldQuestion4,
		fieldQuestion5,fieldQuestion6,fieldQuestion7);
}
/* Code added for new Risk Analyzer ends */

