/** Charting/Calculator Functions for LeadGen SEO */
function CaculateAndGraph(FormID,InsertToID,Mode){
	switch(Mode){
		case "Budget":
		case "budget":
		case "BUDGET":
			BuildBudgetPieGraph(FormID,InsertToID);
			break;
		case "CC":
		case "cc":
			CalculateCC(FormID,InsertToID);
			break;
		
	}
}
function CalculateCC(FormID,InsertToID){
	FormElm = $(FormID);
	var Balance = 0 , APR = 0 , MonthlyAPR = 0, MinPayPct = 0, MinPayAmount = 0 ;
	FormElm.getInputs().each(function(formElement) {
		switch ($(formElement).id){
		  case "Balance":
			  Balance = $(formElement).value;
			  break;
		  case "APR":
			  APR =$(formElement).value;
			  if(APR > 1.0)
				MonthlyAPR =  ((APR / 100 ) * 1) / 12;
			  else
				MonthlyAPR = APR / 12;
			  break;
		  case "MinPayPct":
			  MinPayPct = $(formElement).value;
			  if(MinPayPct > 1.0)
				  MinPayPct = (MinPayPct /100) * 1;
			  break;
		  case "MinPayAmount":
			  MinPayAmount = $(formElement).value;
			  break;
		}
	});
	var intPot = 0, BalancePort = 0, accruedInt = 0, count = 0;
	while(Balance > 0) {
	   if(eval(Balance * MinPayPct) < eval(MinPayAmount)) {pmt = eval(MinPayAmount); } else { pmt = eval(MinPayPct * Balance); }
			intPort = eval(MonthlyAPR * Balance);
			BalancePort = eval(pmt - intPort);
			Balance = eval(Balance - BalancePort);
			accruedInt = eval(accruedInt + intPort);
			count = count + 1
		   if(count > 600) { 
			ParentElm = $(InsertToID);
				var NewElm = new Element( 'span' );

				NewElm.update("Sorry your expenses are more than your income , please revise your expense column.");
				NewElm.className="Error";
				NewElm.id="GraphError";
				if(!$("GraphError"))
					 ParentElm.insert(NewElm);
				return false;
			}
	   else{
		continue;
	   }
	}
	$('TotalIntrest').value = Math.round(accruedInt);
	$('NumberOfPayments').value = Math.round(count);         
	$('NumberOfYears').value = (count / 12).round();
	
	

}
function GetCCLineGraphData(AvailableIncome,tExpense){
	Response = new Array();
	Index=0;
	tExpense.each( function (HashElement){
		Index = Index + 1;
		Data =  { };
		
		Data.data = [[Index,parseInt(HashElement.value)]];
		Data.label = HashElement.key;
		
		
		RandColor=setRandomColor();
		Data.color = RandColor;
		
		
		Response.push(Data);
	});
	return Response;
}
function BuildBudgetPieGraph(FormID,InsertToID){
	FormElm = $(FormID);
	var TotalIncome = 0 , TotalExpense = 0 , IncomeElm = null, ExpenseElm = null, BalanceElm = null;
	var tIncome = $H({ });
	var tExpense = $H({ });
	FormElm.getInputs().each(function(formElement) {
		
		if ($(formElement).hasClassName('income')){
			tIncome.set([$(formElement).name] , $(formElement).value);
		} else  if  ($(formElement).hasClassName('expense')) {
			tExpense.set([$(formElement).name] , $(formElement).value);
		}
		switch($(formElement).id){
			case "TotalIncome": 
				IncomeElm = $(formElement);
				break;
			case "TotalExpense":
				ExpenseElm = $(formElement);
				break;
			case "Balance":
				BalanceElm = $(formElement);
				break;
		}
	});
	tIncome.each(function(HashItem) {
	  TotalIncome = TotalIncome + parseInt(HashItem.value);
	});

	tExpense.each(function(HashItem) {
		TotalExpense = TotalExpense + parseInt(HashItem.value);
	});
	var AvailableIncome = TotalIncome - TotalExpense;
	IncomeElm.value 	= TotalIncome;
	IncomeElm.disabled 	= true;
	ExpenseElm.value 	= TotalExpense;
	ExpenseElm.disabled = true;
	BalanceElm.value	= AvailableIncome;
	BalanceElm.disabled = true;
	GraphData=GetBudgetPieGraphData(AvailableIncome,tExpense);
	if(TotalExpense > TotalIncome || TotalExpense == 0 && TotalIncome == 0){
		ParentElm = $(InsertToID);
		var NewElm = new Element( 'span' );

		NewElm.update("Sorry your expenses are more than your income , please revise your expense column.");
		NewElm.className="Error";
		NewElm.id="GraphError";
		if(!$("GraphError"))
			ParentElm.insert(NewElm);
		return false;
	}
	var graph = new Proto.Chart(
		$(InsertToID),
		GraphData,
		{
			legend: {
				show: true
			},
			pies: {
				show: true,
				labelWidth: 0,
				PercentInLegend: true
				
			},
			grid: {
				labelMargin: 20
			}
			
	 }
	);

};
function GetBudgetPieGraphData(AvailableIncome,tExpense){
	Response = new Array();
	Index=0;
	tExpense.each( function (HashElement){
		Index = Index + 1;
		Data =  { };
		
		Data.data = [[Index,parseInt(HashElement.value)]];
		Data.label = HashElement.key;
		
		
		RandColor=setRandomColor();
		Data.color = RandColor;
		
		
		Response.push(Data);
	});
	Data = { };
	Data.data = [[Index+1,parseInt(AvailableIncome)]];
	Data.label = "Available Income";
	Data.color = "#458B00";
	Response.push(Data);
	return Response;
}
 function setRandomColor (){
	 var rint = Math.round(0xffffff * Math.random());
	 return ('#0' + rint.toString(16)).replace(/^#0([0-9a-f]{6})$/i, '#$1');

 }
