/////////////////// Begin EIP Calculator ///////////////////

function checkNumber(input, min, max, msg) 
{
	msg = " Please reenter item price. ";
	var str = input.value;
for (var i = 0; i < str.length; i++) 
	{
	var ch = str.substring(i, i + 1)
	if ((ch < "0" || "9" < ch) && ch != '.') 
		{
		alert(msg);
		return false;
      	}
   }
   
var num = parseFloat(str)
if (num < min || max < num) 
	{	
	alert(msg + " BD " + min + " to " + max + "");
	return false;
 	}
		input.value = str;
		return true;
}

function computeField(input) 
{
	if (input.value != null && input.value.length != 0)
	input.value = "" + eval(input.value);
	computeForm(input.form);
}

/////////////////// End EIP Calculator ///////////////////

function showtip(tip)
{
document.EPP_Calc.tip.value=tip
}

function showottip(ottxt)
{
document.EPP_Calc.ottxt.value=ottxt
}

function showmip(mip)
{
document.EPP_Calc.mip.value=mip
}

function goTo(e)
{
	/*
	if (e.options[e.selectedIndex].value == 'other')
	{
   		eip.otenor.focus();
   		showtip('Enter Other Tenor');
   		showottip('Other Tenor in Months');
   		eip.mthcont.value = "";
   		showmip('');
   	}
   	else
   	{
   		showmip('');
   		showottip('');
   		showtip('Ready - Click on Calculate');
   		eip.otenor.value="";
   		eip.mthcont.value = "";
   		eip.Submit.focus();
   	}
	*/
	if (e.options[e.selectedIndex].value == 'other')
	{
   		EPP_Calc.otenor.value = "";
		EPP_Calc.otenor.style.visibility = "visible";
		EPP_Calc.otenor.focus();
   	}
	else
	{
		EPP_Calc.otenor.style.visibility = "hidden";
	}
}


// convert string to integer
function toInt(str)
{
	str = str.replace(" ", "")
	str = str.replace(".", "")
	str = str.replace(",", "")
	if ( str )  {  ff = parseInt( str, 10 )  }
	if ( !str || isNaN(str) || isNaN(ff) || !ff )  {  ff = 0  }
	return ff
}

function clear_all()
{
	for( i=1; i<=7; i++ )
	{
		obj = document.getElementById( "bar"+i  )
		obj.height = 0
		obj = document.getElementById( "sum"+i  )
		obj.innerHTML = ""
		obj = document.getElementById( "tsum"+i  )
		obj.innerHTML = ""
	}
}

function format(str)
{
	len = str.length
	if ( len > 3 )
	{
		str = str.substring(0, len-3) + "," + str.substring( len-3, len )
	}
	return str
}

function Calc()
{

	clear_all()
	obj = document.getElementById("amount")
	itemprs = obj.value;
	LA = toInt( itemprs )
	if ( LA <= 49 )
	{
		alert("Minimum loan amount is BD 50 - Please enter a value greater than or equal to 50")
		obj = document.getElementById("loan_amount")
		obj.innerHTML = format( LA + "" ) + " BD"
		return
	}

	if ( LA > 999999 )
	{
		alert("Please enter a vlaue below 999999")
		obj = document.getElementById("loan_amount")
		obj.innerHTML = "XXXXXXX BD"
		return
	}

	obj = document.getElementById("loan_amount")
	obj.innerHTML = format( LA + "" ) + " BD."
	
	obj = document.getElementById("graph");
	obj.style.display = "block";

	ARR_PMT = new Array();
	ARR_Tenor = new Array( 0, 3, 6, 9, 12, 18, 24, 36 );
	maxPMT = 0;
	IR = 0.011; var intcal=0;
	for( i=1; i<ARR_Tenor.length; i++ )
	{
		ARR_PMT[i] = Math.round( ( LA * IR / 12 ) / ( 1 - Math.pow( 1 + IR/12, -ARR_Tenor[i] ) ) );
		if ( ARR_PMT[i] > maxPMT )
			maxPMT = ARR_PMT[i];
	}

	if ( maxPMT )
		ky = 100 / maxPMT; else ky = 0.25;

	for( i=1; i<ARR_Tenor.length; i++ )
	{
		if ( ARR_PMT[i]  > 0 )
		{
			tenorMonth = parseInt( ARR_Tenor[i] );
			
			intcal = itemprs * IR * tenorMonth;
			mresult = ( parseInt(intcal) + parseInt(itemprs) ) / parseInt(tenorMonth);
			var calculatedAmount = Math.round( mresult*100 ) / 100;
			installmentAmount = "BD<br>" + calculatedAmount.toFixed(2);
				
			obj = document.getElementById( "bar"+i )
			obj.height = Math.round( ARR_PMT[i] * ky )
			obj = document.getElementById( "sum"+i )
			obj.innerHTML = "&nbsp; <b>" + installmentAmount  + "</b>"
			obj = document.getElementById( "tsum"+i )
			obj.innerHTML = "&nbsp; <b>" + installmentAmount  + "</b>"
		}
		else
		{
			obj = document.getElementById( "bar"+i )
			obj.height = 0
			obj = document.getElementById( "sum"+i )
			obj.innerHTML = ""
			obj = document.getElementById( "tsum"+i )
			obj.innerHTML = "-"
		}
	}

	window.location.href="#calc"
}
