

function popup(url, width, height) {
  newWin = window.open(url, 'newWin1', 'width=' + width + ',height=' + height + ',scrollbars=yes,resizable=yes,location =1');
}

function popApp() {
  newWin = window.open("https://www.studentbenefitservices.com/application/welcome.php", 'newWin2', 'width=506,height=562,scrollbars=yes,status=yes,toolbar=no,menubar=no,resizable=yes');
}

function outRate(s,d)
{
   var i = s; //first a number
   i = "" + i; //now a string
   var parts = i.split(".");
   var left = "";
   var right = "";
   if (parts.length == 1)
   {
      left = parts[0]; 
      right = "";
   }
   else //numbers on both sides of decimal place
   {
      left = parts[0]; 
      right = parts[1];
   }
   if (parts.length == 1)
   {
      if (left.search(".") == -1) //integer with no decimal
      {
          right = left;
         left  = "";
      }

      else
      {
         if (d== 2)
            return left + ".00" + "%";
         else
             return left + ".000" + "%";      
         
      }

   }

   if (right.length == 1)
   {
      if (d== 2)
         return left + "." + right + "0%";
      else
         return left + "." + right + "00%";
   }
   if (right.length == 2 )
   {
      if (d == 2)
         return left + "." + right + "%";
      else
         return left + "." + right + "0%";
   }
   if (right.length >= 3 )
         return left + "." + right.substring(0,3) + "%";


}

function placeComma(s)
{
   var i,j;
   var temp = "";
   var finale = "";
 
   j = 0;
   for (i = s.length-1; i > -1; i--)
   {
      if (j ==3 || j == 6)
         temp = temp + ",";
         
      temp = temp + s.charAt(i);
      j++;
   }
     
   for (i = temp.length; i > -1; i--)
   {
      finale = finale + temp.charAt(i);
   }
   
   return finale;
}

function outCurr(s,d)
{
   var i = s; //first a number
   i = "" + i; //now a string
   var parts = i.split(".");
   var left = "";
   var right = "";
   if (parts.length == 1)
   {
      left = parts[0]; 
      right = "";
   }
   else //numbers on both sides of decimal place
   {
      left = parts[0]; 
      right = parts[1];
   }
   
   if (parts.length == 1)
   {
  //    alert("entered where parts is 1");
      if (left.search(".") == -1) //integer with no decimal
      {
          right = left;
         left  = "";
      }

      else
      { 
         if (d== 2)
            return "$" + placeComma(left) + ".00";
         else
             return "$" + placeComma(left) + ".000";      
         
      }

   }

   return  "$" + placeComma(left) + "." + (right + "00").substring(0,2);

}

function IsNumeric(sText) {
	var ValidChars = "0123456789.";
	var index;
	for (index = 0; index < sText.length; index++) {
		if (ValidChars.indexOf(sText.charAt(index)) == -1) {
			return false;
		}
	}
	return true;
}

//Regular expressions
function balance_valid(text)
{
   var exp = /^[0-9]{1,6}\.[0-9]{0,2}/;
   if (exp.test(text) || IsNumeric(text))
   {
		return true;
   }
}

function interest_valid(text)
{
   var exp = /^[1-9]\d{0,1}((\.\d+)?)$/i;
   return (exp.test(text));
}
function alternate_valid(text)
{
   var exp = /^[1-9]\d{1,}((\.\d+)?)$/i;
   return (exp.test(text));
}

function custom_valid(text)
{
   var exp = /^[1-9](\d{1,})$/i;
   return (exp.test(text));
}

function sigWA(x)
{ 
   var rnd = Math.floor(x);
   var dif = x - rnd;
   dif = dif*1000;
   dif = Math.round(dif);
   dif = dif*.001;
   rnd = rnd + dif + 1.000;
   return rnd - 1.000;
}
function sigWATest(x)
{ 
   var rnd,dif;
   var y = "" + x;
   var parts = y.split(".");

   if (parts.length == 2 && parts[0] == "0") //No digit on left of decimal spot
   {
      dif = parseFloat(parts[0]);
      rnd = 0;
   }
   else
   {
      if (parts.length == 1) //Integer
      {
         rnd = parseInt(parts[0]);
         dif = 0;
      }
      else
      {
         rnd = parseInt(parts[0]);
         dif = parseFloat("0." + parts[1]);
      }
   }

 //  alert(rnd);
   
 //  alert(dif);

   dif = dif*100;
   dif = Math.round(dif);
   dif = dif*.01;
 //  alert(dif);
   
   var i,skip;
   var str = "";
   var temp = "" + dif;
   temp = temp.replace("0.","");
   //alert("temp " + temp);
   skip = 1;
   for (i = temp.length-1; i > -1 ; i--)
   {
      if (temp.charAt(i)  == "0" && skip == 1)
      {}
      else
      {
         if (temp.charAt(i)  != "0")
         {
            skip = 0;
            str = str + temp.charAt(i);
         }
         else
            str = str + temp.charAt(i);
      }
   }
   if (str == "")
      temp = "00";
   else
      temp = str;
   //alert(temp);
   str = "";
   
   for (i = temp.length-1; i > -1 ; i--)
      str = str + temp.charAt(i);
   rnd = "" + rnd;
      //alert(str); 
   return rnd + "." + str.substring(0,2);
}

function IsNumeric(sText)
{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
   }
  // All characters are numbers.

   
function dispDate(dateVal) 
{
	DaystoAdd=dateVal
	TodaysDate = new Date();
	TodaysDay = new Array('Sunday', 'Monday', 'Tuesday','Wednesday', 'Thursday', 'Friday', 'Saturday');
	TodaysMonth = new Array('January', 'February', 'March','April', 'May','June', 'July', 'August', 'September','October', 'November', 'December');
	DaysinMonth = new Array('31', '28', '31', '30', '31', '30', '31', '31', '30', '31', '30', '31');

	function LeapYearTest (Year) 
	{
	if (((Year % 400)==0) || (((Year % 100)!=0) && (Year % 4)==0)) 
		{
		return true;
		}	
		else {
		return false;
		}
	}

CurrentYear = TodaysDate.getYear();
if (CurrentYear < 2000) 
CurrentYear = CurrentYear + 1900;
currentMonth = TodaysDate.getMonth();
DayOffset = TodaysDate.getDay();
currentDay = TodaysDate.getDate();
month = TodaysMonth[currentMonth];

if (month == 'February') 
{
if (((CurrentYear % 4)==0) && ((CurrentYear % 100)!=0) || ((CurrentYear % 400)==0)) 
	{
	DaysinMonth[1] = 29;
	}
	else {
		DaysinMonth[1] = 28;
		}
}

days = DaysinMonth[currentMonth];
currentDay += DaystoAdd;
if (currentDay > days) 
{
	if (currentMonth == 11) 
	{
	currentMonth = 0;
	month = TodaysMonth[currentMonth];
	CurrentYear = CurrentYear + 1
	}
	else {
	month =
	TodaysMonth[currentMonth+1];
	}
currentDay = currentDay - days;
}

DayOffset += DaystoAdd;

function offsettheDate (offsetCurrentDay) {
if (offsetCurrentDay > 6) {
offsetCurrentDay -= 6;
DayOffset = TodaysDay[offsetCurrentDay-1];
offsettheDate(offsetCurrentDay-1);
}
else {
DayOffset = TodaysDay[offsetCurrentDay];
return true;
}
}
offsettheDate(DayOffset);TheDate = DayOffset + ', ';
TheDate += month + ' ';
TheDate += currentDay + ', '; 
if (CurrentYear<100) CurrentYear="19" + CurrentYear;
TheDate += CurrentYear;
document.write(' '+TheDate);
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function getItem(id, context) {
  var itm = false;

  if (context == 'parent' && parent.document.getElementById) {
    itm = parent.document.getElementById(id);
  } else if (document.getElementById) {
    itm = document.getElementById(id);
  } else if (document.all) {
    itm = document.all[id];
  } else if (document.layers) {
    itm = document.layers[id];
  }

  return itm;
}
function removeSpaces(sStr) 
{
	sStr = String(sStr);	// without this, fails if input type is numeric
    var s = '';
    for (var i = 0; i < sStr.length; i++) 
    {
        if (sStr.charAt(i) != ' ') s += sStr.charAt(i);
	} 
	return s;
}

function removeChar(sStr, c)
{
	sStr = String(sStr);	// without this, fails if input type is numeric
	var s = '';
	for (var i = 0; i < sStr.length; i++)
	{
		if (sStr.charAt(i) != c)
			s += sStr.charAt(i); 
	}
	return s;
}

function replaceChar(sStr, oldChar, newChar)
{
	sStr = String(sStr);	// without this, fails if input type is numeric
	var s = '';
	for (var i = 0; i < sStr.length; i++)
	{
		var thisChar = sStr.charAt(i);
		if (thisChar == oldChar)
			thisChar = newChar;
		s += thisChar;
	}
	return s;
}
function ToCurr(c)
{
	//-- Returns passed number as string in $xxx,xxx.xx format.
	c = removeChar(c, ',');		// remove all commas
	c = c.replace('$', '');			
	if (isNaN(c))
	{		
		return '';			
	}
	workNum = Math.abs((Math.round(c*100)/100));		// round to cents (1/100)
	workStr = "" + workNum;
	if (workStr.indexOf(".") < 0)
	{
		workStr += ".00"
	}
	dStr = workStr.substr(0,workStr.indexOf("."));
	dNum = dStr - 0;
	pStr = workStr.substr(workStr.indexOf("."));
	while (pStr.length < 3)
		pStr += "0";

	//  Add comma in thousands place.
	if (dNum >= 1000) 
	{
		dLen = dStr.length;
		dStr = parseInt(""+(dNum/1000)) + "," + dStr.substring(dLen-3,dLen);
	}
	// Add comma in millions place.
	if (dNum >= 1000000) 
	{
		dLen = dStr.length;
		dStr = parseInt(""+(dNum/1000000)) + "," + dStr.substring(dLen-7,dLen);
	}
	retval = dStr + pStr;
	//-- Put numbers in parentheses if negative.
	if (c < 0) 
		retval = "(" + retval + ")";
	return retval;
}

function ToCurrControl(ctrl, errorCtrl)
{	
	var value = "";
	ctrl.value = removeChar(ctrl.value, ",");		// remove all commas
	ctrl.value = ctrl.value.replace('$', '');
	
	for (var i = 0; i < ctrl.value.length; i++)
	{
		if (ctrl.value.substr(i, 1) == '.' || isNaN(ctrl.value.substr(i, 1)) == false)
			value += ctrl.value.substr(i, 1);
	}	
	
	ctrl.value = value;
	
	if (isNaN(ctrl.value))
	{
		if (errorCtrl != null)
		{
			errorCtrl.innerText = "* Invalid entry *";
			errorCtrl.style.display = '';
		}		
		
		if (ctrl != null)
		{		
				ctrl.value = '0.00';
		}
	}
	else
	{
		if (errorCtrl != null)			
			errorCtrl.style.display = 'none';				
			
		value = ToCurr(ctrl.value);
		ctrl.value = value;	
	}		
}


function OmitPT(aTerm, maxTerm)
{
	return (aTerm > maxTerm) ? maxTerm : aTerm;
}       

function round( a, b )
{
	return ( Math.round( a * Math.pow( 10, b ) ) / Math.pow( 10, b ) );
}
function RemComma( value )
{

    return value.replace(",","");
}
function ToNumeric( value )
{

    if ( value == null || value == "" )
        value = 0;
    else
    {
        if ( isNaN( value ) )
            value = 0;
    }
    var out = new Number( value );
    return out;
}

function ceiling( n1, n2 )
{
	return Math.floor((n1+n2 - 0.0000001)/n2)*n2;
}

function formatDecimal(argvalue, addzero, decimaln) {
  var numOfDecimal = (decimaln == null) ? 2 : decimaln;
  var number = 1;

  number = Math.pow(10, numOfDecimal);

  argvalue = Math.round(parseFloat(argvalue) * number) / number;
  // If you're using IE3.x, you will get error with the following line.
  // argvalue = argvalue.toString();
  // It works fine in IE4.
  argvalue = "" + argvalue;

  if (argvalue.indexOf(".") == 0)
    argvalue = "0" + argvalue;

  if (addzero == true) {
    if (argvalue.indexOf(".") == -1)
      argvalue = argvalue + ".";

    while ((argvalue.indexOf(".") + 1) > (argvalue.length - numOfDecimal))
      argvalue = argvalue + "0";
  }

  return argvalue;
}


function formatDecimals(c, decimalPositions)
{			
	if (isNaN(c)) 
	{
		return c;
	}
	else
	{
		var b = round( c, decimalPositions );
		c = b.toString();
		//errorCtrl.style.display = 'none';			
		var decimalPos = c.indexOf(".");			

		var ii = 3;
		if (decimalPos != -1)
			ii = (decimalPositions - (c.length - decimalPos - 1));
		else
			c += ".";
			
		
		for (var i = 1; i <= ii; i++)
			c += "0";	

		return c;		
	}			
}

function FormatDecimalsControl(ctrl, errorCtrl, decimalPositions)
{
	var value = "";		
	ctrl.value = ctrl.value.replace('%', '');

	for (var i = 0; i < ctrl.value.length; i++)
	{
		if (ctrl.value.substr(i, 1) == '.' || isNaN(ctrl.value.substr(i, 1)) == false)
			value += ctrl.value.substr(i, 1);
	}
	
	value = value.replace(' ', '');		
	ctrl.value = value;
	if (isNaN(ctrl.value))
	{		
		if (errorCtrl != null)
		{
			errorCtrl.innerText = "* Invalid entry*";
			errorCtrl.style.display = '';
		}
		
		if (ctrl != null)
		{
			ctrl.value = '0.00';
		}
	}
	else
	{
		if (errorCtrl != null)			
			errorCtrl.style.display = 'none';				
		
		value = formatDecimals(ctrl.value, decimalPositions);
		ctrl.value = value;
	}
	
}	

function ceiling( n1, n2 )
{
	return Math.floor((n1+n2 - 0.0000001)/n2)*n2;
}

var pageImages = "'/images/menus/tnav-students_on.gif','/images/menus/tnav-parents_on.gif','/images/menus/tnav-home_on.gif','/images/menus/tnav-about_on.gif','/images/menus/tnav-contact_on.gif','/images/menus/tnav-sitemap_on.gif','/images/menus/home_on.gif','/images/menus/loan_on.gif','/images/menus/login_on.gif','/images/menus/site_map_on.gif','/images/menus/am_i_eligible_on.gif','/images/menus/how_much_on.gif','/images/menus/recommend_on.gif','/images/menus/factors_on.gif','/images/menus/how_benefit_on.gif','/images/menus/qualify_on.gif','/images/menus/how_apply_on.gif','/images/menus/elig_term_on.gif','/images/menus/repay_option_on.gif','/images/menus/forms_page_on.gif','/images/menus/alumni_partners_on.gif','/images/menus/noc_option_on.gif','/images/menus/life_after_college_on.gif','/images/menus/inthenews_on.gif','/images/menus/faq_on.gif','/images/menus/glossary_on.gif','/images/menus/contactinfo_on.gif'";
function preloadMenuImages() {
	MM_preloadImages(pageImages);
}
function MM_preloadImages() { //v3.0
	var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
	var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
	if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
//-----------------------------------------------------------------------------------------
function MM_swapImage() { //v3.0
	window.status='';
	var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
	if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
//-----------------------------------------------------------------------------------------
function MM_swapImgRestore() { //v3.0
	var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
//-----------------------------------------------------------------------------------------
function MM_findObj(n, d) { //v3.0
	var p,i,x;
	if(!d) d=document;
	if((p=n.indexOf("?"))>0&&parent.frames.length) {
      d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);
    }
	if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
	for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document); return x;
}

//----------------------------------------------------------
// Main functions used to navigate to each page in the site.
//----------------------------------------------------------
function DisplayWaitMessage() {
	if (navigator.appName == "Microsoft Internet Explorer"){
		document.all.PageContent.style.display = "none";
		document.all.WaitMsg.style.display = "inline";
	}else{
		document.getElementById("PageContent").style.display = "none";
		document.getElementById("WaitMsg").style.display = "inline";
	}
	clearTimeout();
}

function PleaseWait(){
	setTimeout('DisplayWaitMessage()', 2000);
}

//-----------------------------------------------------------------------------------------
function Navigate(form,strURL) {
	form.action = strURL;
	form.submit();
}
//-----------------------------------------------------------------------------------------
function NavigateSecure(form,strURL) {
	var host = window.location.hostname;
	strURL = "https://" + host + strURL;
	//alert(strURL);
	Navigate(form, strURL);
}
//-----------------------------------------------------------------------------------------
function gosecure(page){
	try {
		NavigateSecure(document.forms[0], page);
	}
	catch (e) {
		var host = window.location.hostname;
		window.location.href = "https://" + host + page;
	}
}
//-----------------------------------------------------------------------------------------
function gounsecure(page){
	var host = window.location.hostname;
	if (page.indexOf("http") != 0) {
		try {
			Navigate(document.forms[0],"http://" + host + page);
		}
		catch (err) {
			window.location.href = "http://" + host + page;
		}
	}
	else {
		try {
			Navigate(document.forms[0],page);
		}
		catch (err) {
			window.location.href = page;
		}
	}
}


//-----------------------------------------------------------------------------------------
// eliminate whitespace from start and end of the specified string
function trim(aString) {
  while (aString.substring(0,1) == ' ') {
    aString = aString.substring(1,aString.length);
  }
  while (aString.substring(aString.length-1,aString.length) == ' ') {
    aString = aString.substring(0,aString.length-1);
  }
  return aString;
}

function formatRate(s,d)
{
   var i = s; //first a number
   i = "" + i; //now a string
   var parts = i.split(".");
   var left = "";
   var right = "";
   if (parts.length == 1)
   {
      left = parts[0]; 
      right = "";
   }
   else //numbers on both sides of decimal place
   {
      left = parts[0]; 
      right = parts[1];
   }
   if (parts.length == 1)
   {
      if (left.search(".") == -1) //integer with no decimal
      {
          right = left;
         left  = "";
      }

      else
      {
         if (d== 2)
            return left + ".00" + "%";
         else
             return left + ".000" + "%";      
         
      }

   }

   if (right.length == 1)
   {
      if (d== 2)
         return left + "." + right + "0%";
      else
         return left + "." + right + "00%";
   }
   if (right.length == 2 )
   {
      if (d == 2)
         return left + "." + right + "%";
      else
         return left + "." + right + "0%";
   }
   if (right.length >= 3 )
         return left + "." + right.substring(0,3) + "%";


}

function placeComma(s)
{
   var i,j;
   var temp = "";
   var finale = "";
 
   j = 0;
   for (i = s.length-1; i > -1; i--)
   {
      if (j ==3 || j == 6)
         temp = temp + ",";
         
      temp = temp + s.charAt(i);
      j++;
   }
     
   for (i = temp.length; i > -1; i--)
   {
      finale = finale + temp.charAt(i);
   }
   
   return finale;
}

function formatDollar(s,d)
{
   var i = s; //first a number
   i = "" + i; //now a string
   var parts = i.split(".");
   var left = "";
   var right = "";
   if (parts.length == 1)
   {
      left = parts[0]; 
      right = "";
   }
   else //numbers on both sides of decimal place
   {
      left = parts[0]; 
      right = parts[1];
   }
   
   if (parts.length == 1)
   {
  //    alert("entered where parts is 1");
      if (left.search(".") == -1) //integer with no decimal
      {
          right = left;
         left  = "";
      }

      else
      { 
         if (d== 2)
            return "$" + placeComma(left) + ".00";
         else
             return "$" + placeComma(left) + ".000";      
         
      }

   }

   return  "$" + placeComma(left) + "." + (right + "00").substring(0,2);

}

function IsNumeric(sText) {
	var ValidChars = "0123456789.";
	var index;
	for (index = 0; index < sText.length; index++) {
		if (ValidChars.indexOf(sText.charAt(index)) == -1) {
			return false;
		}
	}
	return true;
}

//Regular expressions
function balance_valid(text)
{
   var exp = /^[0-9]{1,6}\.[0-9]{0,2}/;
   if (exp.test(text) || IsNumeric(text))
   {
		return true;
   }
}

function interest_valid(text)
{
   var exp = /^[1-9]\d{0,1}((\.\d+)?)$/i;
   return (exp.test(text));
}
function alternate_valid(text)
{
   var exp = /^[1-9]\d{1,}((\.\d+)?)$/i;
   return (exp.test(text));
}

function custom_valid(text)
{
   var exp = /^[1-9](\d{1,})$/i;
   return (exp.test(text));
}

function FormatCrr(x)
{ 
   var rnd = Math.floor(x);
   var dif = x - rnd;
   dif = dif*100;
   dif = Math.round(dif);
   dif = dif*.01;
   rnd = rnd + dif + 1.00 - 1.00;
   return rnd;
}

function sigWA(x)
{ 
   var rnd = Math.floor(x);
   var dif = x - rnd;
   dif = dif*1000;
   dif = Math.round(dif);
   dif = dif*.001;
   rnd = rnd + dif + 1.000;
   return rnd - 1.000;
}
function sigWATest(x)
{ 
   var rnd,dif;
   var y = "" + x;
   var parts = y.split(".");

   if (parts.length == 2 && parts[0] == "0") //No digit on left of decimal spot
   {
      dif = parseFloat(parts[0]);
      rnd = 0;
   }
   else
   {
      if (parts.length == 1) //Integer
      {
         rnd = parseInt(parts[0]);
         dif = 0;
      }
      else
      {
         rnd = parseInt(parts[0]);
         dif = parseFloat("0." + parts[1]);
      }
   }

 //  alert(rnd);
   
 //  alert(dif);

   dif = dif*100;
   dif = Math.round(dif);
   dif = dif*.01;
 //  alert(dif);
   
   var i,skip;
   var str = "";
   var temp = "" + dif;
   temp = temp.replace("0.","");
   //alert("temp " + temp);
   skip = 1;
   for (i = temp.length-1; i > -1 ; i--)
   {
      if (temp.charAt(i)  == "0" && skip == 1)
      {}
      else
      {
         if (temp.charAt(i)  != "0")
         {
            skip = 0;
            str = str + temp.charAt(i);
         }
         else
            str = str + temp.charAt(i);
      }
   }
   if (str == "")
      temp = "00";
   else
      temp = str;
   //alert(temp);
   str = "";
   
   for (i = temp.length-1; i > -1 ; i--)
      str = str + temp.charAt(i);
   rnd = "" + rnd;
      //alert(str); 
   return rnd + "." + str.substring(0,2);
}

var IR;
var totalLoanAmt;
var weightAvgRate;


function Calculate(totalAmt)
{ 
   var PMT1 = 0.0;
   var PMT2 = 0.0;
   var IR1 = ((weightAvgRate-.25)*.01)/12;
   var IR3 = (weightAvgRate*.01)/12;
   var IR2 = IR3;
   var PV = totalLoanAmt;
   var totalLoans = totalAmt;
   var TermYears = getItem("RTerm").value;
   var ExtTermMonths;
   if (totalLoans >=30000.00 && totalLoans <=39999.99)
   {
		ExtTermMonths = 300;
   }

   var termMonths = TermYears*12;
   var pwr = Math.pow((1+IR2),termMonths);
   var pwr2 = Math.pow((1+IR2),termMonths);
   var pwr3 = Math.pow((1+IR3),termMonths);
   
   PMT1 = (PV*pwr*IR2)/((pwr - 1));
   PMT2 = (PV*pwr2*IR2)/((pwr2 - 1));
   var PMT3 = (PV*pwr3*IR3)/((pwr3 - 1));

   if (PMT1 < 50)
   {
      PMT1 = 50.00;
   }
   if (PMT2 < 50)
   {
		PMT2 = 50.00;
   }
   
   var PMT1Out = formatDollar(FormatCrr(PMT1), 2);
   var PMT2Out = formatDollar(FormatCrr(PMT2), 2);
   var PMT3Out = formatDollar(FormatCrr(PMT3), 2);

  document.getElementById("STDMonthly").innerHTML = PMT1Out;
    document.getElementById("STDMonthlyNoDisc").innerHTML = PMT3Out;
   
   if (ExtTermMonths == 300)
   {
		var EXPwr1 = Math.pow((1+IR2),ExtTermMonths);
		var EXPwr2 = Math.pow((1+IR2),ExtTermMonths);
		var EXPwr3 = Math.pow((1+IR3),ExtTermMonths);
	
		var EXMonthly1 = (PV*EXPwr1*IR2)/((EXPwr1 - 1));
		var EXMonthly2 = (PV*EXPwr2*IR2)/((EXPwr2 - 1));
		var EXMonthly3 = (PV*EXPwr3*IR3)/((EXPwr3 - 1));
		
		var EXPMT1Out = formatDollar(FormatCrr(EXMonthly1), 2);
		var EXPMT2Out = formatDollar(FormatCrr(EXMonthly2), 2);
		var EXPMT3Out = formatDollar(FormatCrr(EXMonthly3), 2);
		
		var EXInt1;	
		var EXInt2;	
		var EXInt3;	
		var EXPer1 = 1;
		var EXPer2 = 1;
		var EXPer3 = 1;
	
		var EXIR1;
		var EXIR2;
		var EXIR3;
	
		var EXMonthInt1 = IR1*PV;
		var EXMonthInt2 = IR2*PV;
		var EXMonthInt3 = IR3*PV;
	
		var EXRem1 = PV-(EXMonthly1-EXMonthInt1);
		var EXRem2 = PV-(EXMonthly2-EXMonthInt2);
		var EXRem3 = PV-(EXMonthly3-EXMonthInt3);
	
		EXInt1 = EXMonthInt1;
		EXInt2 = EXMonthInt2;
		EXInt3 = EXMonthInt3;
		
		document.getElementById("EXMonthly").innerHTML = EXPMT1Out;
		document.getElementById("EXMonthlyNoDisc").innerHTML = EXPMT3Out;
		
		while (EXPer1 <= ExtTermMonths)
		{
		   if (EXPer1 < 36)
		   {
				EXIR1 = ((weightAvgRate-.25)*(.01))/12;
		   } else {
				EXIR1 = ((weightAvgRate-1.25)*(.01))/12;
			
		   }
		   EXMonthInt1= EXIR1*EXRem1;
		   EXRem1 = EXRem1-(EXMonthly1-EXMonthInt1);
		   if (EXRem1 <= 0.001)
		   {
		      EXPer1++;
		      EXInt1 += EXMonthInt1;
		      break;   
		   }   
		   EXInt1 += EXMonthInt1;
		   EXPer1++;
		}	   
	    var costExt = PV + EXInt1;
	    
		while (EXPer2 <= ExtTermMonths)
		{
		    EXIR2 = ((weightAvgRate-.25)*(.01))/12;
			if (EXPer2 == 30)
			{
				EXRem2 = EXRem2 - getItem("PRed").value;
				var xpvExt = PV - getItem("PRed").value;
				}

		    EXMonthInt2 = EXIR2*EXRem2;
			EXRem2 = EXRem2-(EXMonthly2-EXMonthInt2);
			
			if (EXRem2 <= 0.001)
			{
			   EXPer2++;
			   EXInt2 += EXMonthInt2;
			   break;   
			}   
			EXInt2 += EXMonthInt2;
			EXPer2++;
		}
	    var costExt2 = xpvExt + EXInt2;

		while (EXPer3 <= ExtTermMonths)
		{
		   EXMonthInt3 = IR3*EXRem3;
		   EXRem3 = EXRem3-(EXMonthly3-EXMonthInt3);
			  	  
		   if (EXRem3 <= 0.001)
		   {
		      EXPer3++;
		      EXInt3 += EXMonthInt3;
		      break;   
		   }   
		   EXInt3 += EXMonthInt3;
		   EXPer3++;
		}	   
 	    var costExt3 = PV + EXInt3;

  		document.getElementById("EXInterest").innerHTML = formatDollar(FormatCrr(EXInt1),2);
		document.getElementById("EXInterestNoDisc").innerHTML = formatDollar(FormatCrr(EXInt3),2);
		document.getElementById("EXTerm").innerHTML = EXPer1;
		document.getElementById("EXTermNoDisc").innerHTML = EXPer3;
		document.getElementById("EXTotalDiff").innerHTML = formatDollar(FormatCrr(EXInt3 - EXInt1),2);  
    	document.getElementById("EXTermDiff").innerHTML = EXPer3 - EXPer1;
 
   	 	var sOut = "<br>By consolidating with SBS, you'll reduce your number of payments by " + document.getElementById("EXTermDiff").innerHTML; 
 
 		document.getElementById("StringOut3").innerHTML =  sOut + " and you'll pay " + document.getElementById("EXTotalDiff").innerHTML  + " less in interest charges.**<br>";

   }
   else
   {
   		document.getElementById("EXMonthly").innerHTML = "";
		document.getElementById("EXMonthlyNoDisc").innerHTML = "";
   
   		document.getElementById("EXInterest").innerHTML = "";
		document.getElementById("EXInterestNoDisc").innerHTML = "";
		document.getElementById("EXTerm").innerHTML = "";
		document.getElementById("EXTermNoDisc").innerHTML = "";
		document.getElementById("EXTotalDiff").innerHTML = "";  
	    document.getElementById("EXTermDiff").innerHTML = "";
		document.getElementById("StringOut3").innerHTML = "";
   
   }
		
   var Int1;	
   var Int2;
   var Int3;	
   var Per1 = 1;
   var Per2 = 1;
   var Per3 = 1;
   var MonthlyInt1 = IR1*PV;
   var MonthlyInt2 = IR2*PV;
   var MonthlyInt3 = IR3*PV;
   var Per1Remainder = PV-(PMT1-MonthlyInt1);
   var Per2Remainder = PV-(PMT2-MonthlyInt2);
   var Per3Remainder = PV-(PMT3-MonthlyInt3);
   
   Int1 = MonthlyInt1;
   Int2 = MonthlyInt2;
   Int3 = MonthlyInt3;

   while (Per1 <= termMonths)
   {
      if (Per1 < 36)
      {
			IR1 = ((weightAvgRate-.25)*(.01))/12;
	  } else {
			IR1 = ((weightAvgRate-1.25)*(.01))/12;
			/*if (Per1 == 36)
			{
				pwr = Math.pow((1+rate),(termMonths-Per1));
				PMT1 = (Per1Remainder*pwr*rate)/((pwr - 1));
				document.getElementById("StdPmtAmt2").innerHTML = formatDollar(FormatCrr(PMT1), 2);
			}*/
	  }

      MonthlyInt1 = IR1*Per1Remainder;
	  Per1Remainder = Per1Remainder-(PMT1-MonthlyInt1);
      
      if (Per1Remainder <= 0.001)
      {
         Per1++;
         Int1 += MonthlyInt1;
         break;   
      }   
	  Per1++;
      Int1 += MonthlyInt1;
   }	   
   var totalC = PV + Int1;

   while (Per2 <= termMonths)
   {
	  IR2 = ((weightAvgRate-.25)*(.01))/12;
      if (Per2 == 30)
      {
		Per2Remainder = Per2Remainder - getItem("PRed").value;
		var xpv = PV - getItem("PRed").value;
		/*pwr2 = Math.pow((1+rate2),(termMonths-Per2));
		PMT2 = (Per2Remainder*pwr2*rate2)/((pwr2 - 1));
		document.getElementById("Opt2StdPmtAmt2").innerHTML = formatDollar(FormatCrr(PMT2), 2);*/
	  }

      MonthlyInt2 = IR2*Per2Remainder;
	  Per2Remainder = Per2Remainder-(PMT2-MonthlyInt2);
	  
      if (Per2Remainder <= 0.001)
      {
         Per2++;
         Int2 += MonthlyInt2;
         break;   
      }   
      Int2 += MonthlyInt2;
	  Per2++;
   }	   
   var cost2 = xpv + Int2;

   while (Per3 <= termMonths)
   {
      MonthlyInt3= IR3*Per3Remainder;
      Per3Remainder = Per3Remainder-(PMT3-MonthlyInt3);
	  	  
      if (Per3Remainder <= 0.001)
      {
         Per3++;
         Int3 += MonthlyInt3;
         break;   
      }   
      Int3 += MonthlyInt3;
	  Per3++;
   }	   
   var TotalC3 = PV + Int3;

   document.getElementById("TotalInterest").innerHTML = formatDollar(FormatCrr(Int1),2);
    document.getElementById("TotalInterestNoDisc").innerHTML = formatDollar(FormatCrr(Int3),2);
    document.getElementById("TotalMonths").innerHTML = Per1;
    document.getElementById("TotalMonthsNoDisc").innerHTML = Per3;

	document.getElementById("payoff").innerHTML =   parseInt(Per3 / 12);
 document.getElementById("payoff_months").innerHTML =   Per3;


   document.getElementById("InterestDifference").innerHTML = formatDollar(FormatCrr(Int3 - Int1),2);  


document.getElementById("MonthlyPaymentsDifference").innerHTML =  Per3 - Per1;
 
   var sOut; 
 sOut = "<br>By consolidating with SBS, you'll reduce your number of payments by " + document.getElementById("MonthlyPaymentsDifference").innerHTML; 
 
 document.getElementById("StringOut1").innerHTML =  sOut + " and you'll pay " + formatDollar(FormatCrr(Int3 - Int1),2) + " less in interest charges.**<br>";
 
  CalcGRAD();
  
  document.getElementById("GRADMonthlyDiff").innerHTML = Per3 - document.getElementById("GRADTotalAmt").innerHTML;
  
  sOut = "<br>By consolidating with SBS, you'll reduce your number of payments by " + document.getElementById("GRADMonthlyDiff").innerHTML; 
 
 document.getElementById("StringOut2").innerHTML =  sOut + " and you'll pay " + document.getElementById("GRADInterestDiff").innerHTML  + " less in interest charges.**<br>";
 

}


function CalcGRAD()
{ 
   var GDPer1 = 24;
   var GDTerm;
   
   var GDNum;
   var GDPc;
   var PayTotal,A,B,C;
   var PayDiff = 0;
   var p = new Array();
   var IR = (weightAvgRate*.01)/12;
   var P = totalLoanAmt;
   var TermYears = getItem("RTerm").value;
   var termMonths = TermYears*12;
   
   /* First must get GDTerm, GDNum, GDPc */
   
   GDNum = parseInt((termMonths-1)/24);
   if (termMonths%24 == 0)
   {
      GDTerm = 24;
   }
   else
   {
	  GDTerm = termMonths%24;
   }

   GDPc = parseInt((Math.pow(3,1.0/GDNum)-1)*10000)/10000;
      
   A = (1-Math.pow((1+IR),-GDPer1))/IR;
   B = (1-Math.pow((1+GDPc)/Math.pow((1 + IR),GDPer1),GDNum))/(1-((1+GDPc)/Math.pow((1 + IR),GDPer1)));
   C = (Math.pow((1+GDPc),GDNum)*(1-Math.pow((1+IR),-GDTerm)))/(IR*Math.pow((1+IR),GDPer1*GDNum));

   //alert(GDNum + " " + GDTerm + " " + GDPc);
   //alert(A);
   //alert(B);
   //alert(C);

   PayTotal = P/(A*B+C);

   /* Round PayTotal up to nearest penny ...watch this, since they may not round 100.000 to 100.010,
   but instead to 100.000.  Remember, 100.009 to 100.01 is standard*/
   
   PayTotal = Math.ceil(PayTotal*100)/100;

   var MonthlyInt1 = IR*P;
   var basedOnMonthlyInt = 0;
   var basedOnFifty = 0;
   var basedOnUser = 0;
   if (PayTotal < MonthlyInt1)
   {
      basedOnMonthlyInt = 1;
      PayTotal = MonthlyInt1;
      PayTotal = Math.ceil(MonthlyInt1*100)/100;;
   }

   var firstpayment = PayTotal; 
   var Per1 = 1;
   var Per1Remainder = P-(PayTotal-MonthlyInt1);
   var Int1 = MonthlyInt1;
   var totalC = 0;
   var transition = -1;
   /* Now start amortization */
   var firstAmortization = 1;
   var i;
   while (Per1 <= termMonths)
   {   
      if (Per1%24 == 0)
      {
         PayTotal =  PayTotal*(1 + GDPc);
      }
      MonthlyInt1 = IR*Per1Remainder;

      /* Termination of amortization has occured if... */
      if (Per1Remainder <= PayTotal) 
      {
         PayTotal = Per1Remainder + MonthlyInt1;
         Per1++;
         Int1 += MonthlyInt1;
         if (firstAmortization == 1)
         {
            firstAmortization = 0;
         }   
         if (basedOnFifty == 1)
         {
            break;
         }
         if (basedOnUser == 1)
         {
            break; 
         }       

         if (Per1 < termMonths)
         {
            if (basedOnMonthlyInt == 1)
            {
               Per1 = 1;
               MonthlyInt1 = IR*P;
               PayTotal = firstpayment;
               Per1Remainder = P-(PayTotal-MonthlyInt1);
               Int1 = MonthlyInt1;
               GDPc = GDPc - .0001;
               totalC = 0;     
               continue;   
            } 
            else  // Payment must be decreased by 1, adjustment 4
            {
 
               Per1 = 1;
               MonthlyInt1 = IR*P;
               if (transition == -1 || transition == 0)
               {
                  transition = 0;
                  firstpayment = firstpayment - 1;
               }
               else
               {
                  transition = 2;
                  firstpayment = firstpayment - .01;
               }

               PayTotal = firstpayment;
               Per1Remainder = P-(PayTotal-MonthlyInt1);
               Int1 = MonthlyInt1;
               totalC = 0;     
               continue;                  
            }
         }
         else  
         {
            if (Per1 == termMonths)
            {
               if (basedOnMonthlyInt == 1)   // Deal with G% first
               {
                  if (PayDiff > firstpayment || 50.0 > firstpayment)
                  {
                     if (PayDiff > 50.0)
                     {
                        firstpayment = PayDiff;

                        PayTotal = PayDiff;
                        basedOnUser = 1;
                     }
                     else
                     {
                        firstpayment = 50.0;
                        PayTotal = 50.0;
                        basedOnFifty = 1;
                     }
                     Per1 = 1;
                     MonthlyInt1 = IR*P;

                     Per1Remainder = P-(PayTotal-MonthlyInt1);
                     Int1 = MonthlyInt1;
                     totalC = 0;     
                     continue;
                  }
               }
               if (transition == 0 || transition == 1)
               {
                  transition = 1;
                  firstpayment = firstpayment + .01;
               }
               else
               {
                  if (PayDiff > firstpayment || 50.0 > firstpayment)
                  {
                     if (PayDiff > 50.0)
                     {
                        firstpayment = PayDiff;
                        PayTotal = PayDiff;
                        basedOnUser = 1;
                     }
                     else
                     {
                        firstpayment = 50.0;
                        PayTotal = 50.0;
                        basedOnFifty = 1;
                     }
                     Per1 = 1;
                     MonthlyInt1 = IR*P;

                     Per1Remainder = P-(PayTotal-MonthlyInt1);
                     Int1 = MonthlyInt1;
                     totalC = 0;     
                     continue;
                  }
               }
            }
            else // Per1 > term
            {
               if (transition == 0 || transition == 1)
                  transition = 1;
               Per1 = 1;
               MonthlyInt1 = IR*P;
               firstpayment = firstpayment + .01;
               PayTotal = firstpayment;
               Per1Remainder = P-(PayTotal-MonthlyInt1);
               Int1 = MonthlyInt1;
               totalC = 0;     
               continue;                     
            }
         } // End 3 decisions
      } // end situation where amortization terminated
 
      Per1Remainder = Per1Remainder-(PayTotal-MonthlyInt1);
   
      Per1++;
      Int1 += MonthlyInt1;   
   } // end while
   
   totalC = P + Int1;
   
   var Regularcost = totalC;
   var Regularinterest = Int1;
   var Regularnum = Per1;
   /* Prepare for running the lender schedule, since now we have the G%, the firstpayment */
   IR = ((weightAvgRate-.25)*.01)/12;
   Per1 = 1;
   PayTotal = firstpayment;
   MonthlyInt1 = IR*P;
   Per1Remainder = P-(PayTotal-MonthlyInt1);

   Int1 = MonthlyInt1;
   totalC = 0;
   
   while (Per1 <= termMonths)
   {   
      if (Per1%24 == 0)
      {
         PayTotal =  PayTotal*(1 + GDPc);
      }
      
      if (Per1 < 36)
      {
		IR = ((weightAvgRate-.25)*.01)/12;
	  } else {
		IR = ((weightAvgRate-1.25)*.01)/12;
	  }

      MonthlyInt1 = IR*Per1Remainder;
      /* Termination of amortization has occured if... */
      if (Per1Remainder <= PayTotal) 
      {
         PayTotal = Per1Remainder + MonthlyInt1;
         Per1++;
         Int1 += MonthlyInt1;
         break;
      }
      
      Per1Remainder = Per1Remainder-(PayTotal-MonthlyInt1);
      Per1++;
      Int1 += MonthlyInt1;   
   } // end while
   
   totalC = P + Int1;

   document.getElementById("GRADPMonthly").innerHTML = formatDollar(FormatCrr(firstpayment),2);
    document.getElementById("GRADInterest").innerHTML = formatDollar(FormatCrr(Int1),2);
   document.getElementById("GRADTotalAmt").innerHTML = Per1;
   
   
   CalcGRAD2();
}

function CalcGRAD2()
{ 
   var GDPer1 = 24;
   var GDTerm;
   var GDNum;
   var GDPc;
   var PayTotal,A,B,C;
   var PayDiff = 0;
   var p = new Array();
   var IR = (weightAvgRate*.01)/12;
   var P = totalLoanAmt;
   var TermYears = getItem("RTerm").value;
   var termMonths = TermYears*12;
   
   /* First must get GDTerm, GDNum, GDPc */
   
   GDNum = parseInt((termMonths-1)/24);

   if (termMonths%24 == 0)
   {
      GDTerm = 24;
   }
   else
   {
	  GDTerm = termMonths%24;
   }

   GDPc = parseInt((Math.pow(3,1.0/GDNum)-1)*10000)/10000;
      
   A = (1-Math.pow((1+IR),-GDPer1))/IR;
   B = (1-Math.pow((1+GDPc)/Math.pow((1 + IR),GDPer1),GDNum))/(1-((1+GDPc)/Math.pow((1 + IR),GDPer1)));
   C = (Math.pow((1+GDPc),GDNum)*(1-Math.pow((1+IR),-GDTerm)))/(IR*Math.pow((1+IR),GDPer1*GDNum));
   
   PayTotal = P/(A*B+C);

   /* Round PayTotal up to nearest penny ...watch this, since they may not round 100.000 to 100.010,
   but instead to 100.000.  Remember, 100.009 to 100.01 is standard*/
   
   PayTotal = Math.ceil(PayTotal*100)/100;
   var MonthlyInt1 = IR*P;
   var basedOnMonthlyInt = 0;
   var basedOnFifty = 0;
   var basedOnUser = 0;
   if (PayTotal < MonthlyInt1)
   {
      basedOnMonthlyInt = 1;
      PayTotal = MonthlyInt1;
      PayTotal = Math.ceil(MonthlyInt1*100)/100;
   }

   var firstpayment = PayTotal; 
   var Per1 = 1;
   var Per1Remainder = P-(PayTotal-MonthlyInt1);
   var Int1 = MonthlyInt1;
   var totalC = 0;
   var transition = -1;
   /* Now start amortization */
   var firstAmortization = 1;
   var i;
   while (Per1 <= termMonths)
   {   
      if (Per1 == 1)
      {
          p = new Array();
          for (i = 0; i < 15; i++)
            p[i] = "";
           p[0] = formatDollar(FormatCrr(firstpayment),2);
      }
      if (Per1%24 == 0)
      {
         PayTotal =  PayTotal*(1 + GDPc);
         p[Per1/24] = formatDollar(FormatCrr(PayTotal),2);
      }
      MonthlyInt1 = IR*Per1Remainder;

      /* Termination of amortization has occured if... */
      if (Per1Remainder <= PayTotal) 
      {

         PayTotal = Per1Remainder + MonthlyInt1;
         Per1++;
         Int1 += MonthlyInt1;
         if (firstAmortization == 1)
         {
            firstAmortization = 0;
         }   
         if (basedOnFifty == 1)
         {
            break;
         }
         if (basedOnUser == 1)
         {
            break; 
         }       

         if (Per1 < termMonths)
         {
            if (basedOnMonthlyInt == 1)
            {
               Per1 = 1;
               MonthlyInt1 = IR*P;
               PayTotal = firstpayment;
               Per1Remainder = P-(PayTotal-MonthlyInt1);
               Int1 = MonthlyInt1;
               GDPc = GDPc - .0001;
               totalC = 0;     
               continue;   
            } 
            else  // Payment must be decreased by 1, adjustment 4
            {
 
               Per1 = 1;
               MonthlyInt1 = IR*P;
               if (transition == -1 || transition == 0)
               {
                  transition = 0;
                  firstpayment = firstpayment - 1;
               }
               else
               {
                  transition = 2;
                  firstpayment = firstpayment - .01;
               }

               PayTotal = firstpayment;
               Per1Remainder = P-(PayTotal-MonthlyInt1);
               Int1 = MonthlyInt1;
               totalC = 0;     
               continue;                  
            }
         }
         else  
         {
            if (Per1 == termMonths)
            {
               if (basedOnMonthlyInt == 1)   // Deal with G% first
               {
                  if (PayDiff > firstpayment || 50.0 > firstpayment)
                  {
                     if (PayDiff > 50.0)
                     {
                        firstpayment = PayDiff;
                        PayTotal = PayDiff;
                        basedOnUser = 1;
                     }
                     else
                     {
                        firstpayment = 50.0;
                        PayTotal = 50.0;
                        basedOnFifty = 1;
                     }
                     Per1 = 1;
                     MonthlyInt1 = IR*P;

                     Per1Remainder = P-(PayTotal-MonthlyInt1);
                     Int1 = MonthlyInt1;
                     totalC = 0;     
                     continue;
                  }

               }
               if (transition == 0 || transition == 1)
               {
                  transition = 1;
                  firstpayment = firstpayment + .01;
               }
               else
               {
                  if (PayDiff > firstpayment || 50.0 > firstpayment)
                  {
                     if (PayDiff > 50.0)
                     {
                        firstpayment = PayDiff;
                        PayTotal = PayDiff;
                        basedOnUser = 1;
                     }
                     else
                     {
                        firstpayment = 50.0;
                        PayTotal = 50.0;
                        basedOnFifty = 1;
                     }
                     Per1 = 1;
                     MonthlyInt1 = IR*P;

                     Per1Remainder = P-(PayTotal-MonthlyInt1);
                     Int1 = MonthlyInt1;
                     totalC = 0;     
                     continue;
                  }
               }
            }
            else // Per1 > term
            {
               if (transition == 0 || transition == 1)
                  transition = 1;
               Per1 = 1;
               MonthlyInt1 = IR*P;
               firstpayment = firstpayment + .01;
               PayTotal = firstpayment;
               Per1Remainder = P-(PayTotal-MonthlyInt1);
               Int1 = MonthlyInt1;
               totalC = 0;     
               continue;                     
            }
         } // End 3 decisions
      } // end situation where amortization terminated
      Per1Remainder = Per1Remainder-(PayTotal-MonthlyInt1);
   
      Per1++;
      Int1 += MonthlyInt1;   
   } // end while
   
   totalC = P + Int1;
   
   var Regularcost = totalC;
   var Regularinterest = Int1;
   var Regularnum = Per1;
   /* Prepare for running the lender schedule, since now we have the G%, the firstpayment */
   IR = ((weightAvgRate-.25)*.01)/12;
   Per1 = 1;
   PayTotal = firstpayment;
   MonthlyInt1 = IR*P;
   Per1Remainder = P-(PayTotal-MonthlyInt1);

   Int1 = MonthlyInt1;
   totalC = 0;
   
   while (Per1 <= termMonths)
   {   
      if (Per1%24 == 0)
      {
         PayTotal =  PayTotal*(1 + GDPc);
      }
      
      /* Termination of amortization has occured if... */
      if (Per1Remainder <= PayTotal) 
      {
         PayTotal = Per1Remainder + MonthlyInt1;
         Per1++;
         Int1 += MonthlyInt1;
         break;
      }
      
      if (Per1 == 31)
      {
		Per1Remainder = Per1Remainder - getItem("PRed").value;
		var GradPV = P - getItem("PRed").value;
	  }
	  
      MonthlyInt1 = IR*Per1Remainder;
     /* Termination of amortization has occured if... */
      if (Per1Remainder <= PayTotal) 
      {
         PayTotal = Per1Remainder + MonthlyInt1;
         Per1++;
         Int1 += MonthlyInt1;
         break;
      }
 
      Per1Remainder = Per1Remainder-(PayTotal-MonthlyInt1);
      Per1++;
      Int1 += MonthlyInt1;   
  } // end while
   
   totalC = GradPV + Int1;

   document.getElementById("GRADPMonthly2").innerHTML = formatDollar(FormatCrr(firstpayment),2);
   document.getElementById("GRADInterest2").innerHTML = formatDollar(FormatCrr(Int1),2);
   document.getElementById("GRADTotalAmt2").innerHTML = Per1;
 
   CalcGRAD3();
}

function CalcGRAD3()
{ 
   var GDPer1 = 24;
   var GDTerm;
   var GDNum;
   var GDPc;
   var PayTotal,A,B,C;
   var PayDiff = 0;
   var p = new Array();
   var IR = (weightAvgRate*.01)/12;
   var P = totalLoanAmt;
   var TermYears = getItem("RTerm").value;
   var termMonths = TermYears*12;
   
   /* First must get GDTerm, GDNum, GDPc */
   
   GDNum = parseInt((termMonths-1)/24);
   if (GDNum == 0)
   {
		GDNum = (termMonths-1)/24;
   }
   if (termMonths%24 == 0)
   {
      GDTerm = 24;
   }
   else
   {
	  GDTerm = termMonths%24;
   }

   GDPc = parseInt((Math.pow(3,1.0/GDNum)-1)*10000)/10000;
      
   A = (1-Math.pow((1+IR),-GDPer1))/IR;
   B = (1-Math.pow((1+GDPc)/Math.pow((1 + IR),GDPer1),GDNum))/(1-((1+GDPc)/Math.pow((1 + IR),GDPer1)));
   C = (Math.pow((1+GDPc),GDNum)*(1-Math.pow((1+IR),-GDTerm)))/(IR*Math.pow((1+IR),GDPer1*GDNum));
   
   PayTotal = P/(A*B+C);

   /* Round PayTotal up to nearest penny ...watch this, since they may not round 100.000 to 100.010,
   but instead to 100.000.  Remember, 100.009 to 100.01 is standard*/
   
   PayTotal = Math.ceil(PayTotal*100)/100;
   var MonthlyInt1 = IR*P;
   var basedOnMonthlyInt = 0;
   var basedOnFifty = 0;
   var basedOnUser = 0;
   if (PayTotal < MonthlyInt1)
   {
      basedOnMonthlyInt = 1;
      PayTotal = MonthlyInt1;
      PayTotal = Math.ceil(MonthlyInt1*100)/100;
   }

   var firstpayment = PayTotal; 
   var Per1 = 1;
   var Per1Remainder = P-(PayTotal-MonthlyInt1);
   var Int1 = MonthlyInt1;
   var totalC = 0;
   var transition = -1;
   /* Now start amortization */
   var firstAmortization = 1;
   var i;
   while (Per1 <= termMonths)
   {   
      if (Per1 == 1)
      {
          p = new Array();
          for (i = 0; i < 15; i++)
            p[i] = "";
           p[0] = formatDollar(FormatCrr(firstpayment),2);
      }
      if (Per1%24 == 0)
      {
         PayTotal =  PayTotal*(1 + GDPc);
         p[Per1/24] = formatDollar(FormatCrr(PayTotal),2);
      }
      MonthlyInt1 = IR*Per1Remainder;

      /* Termination of amortization has occured if... */
      if (Per1Remainder <= PayTotal) 
      {

         PayTotal = Per1Remainder + MonthlyInt1;
         Per1++;
         Int1 += MonthlyInt1;
         if (firstAmortization == 1)
         {
            firstAmortization = 0;
         }   
         if (basedOnFifty == 1)
         {
            break;
         }
         if (basedOnUser == 1)
         {
            break; 
         }       

         if (Per1 < termMonths)
         {
            if (basedOnMonthlyInt == 1)
            {
               Per1 = 1;
               MonthlyInt1 = IR*P;
               PayTotal = firstpayment;
               Per1Remainder = P-(PayTotal-MonthlyInt1);
               Int1 = MonthlyInt1;
               GDPc = GDPc - .0001;
               totalC = 0;     
               continue;   
            } 
            else  // Payment must be decreased by 1, adjustment 4
            {
 
               Per1 = 1;
               MonthlyInt1 = IR*P;
               if (transition == -1 || transition == 0)
               {
                  transition = 0;
                  firstpayment = firstpayment - 1;
               }
               else
               {
                  transition = 2;
                  firstpayment = firstpayment - .01;
               }

               PayTotal = firstpayment;
               Per1Remainder = P-(PayTotal-MonthlyInt1);
               Int1 = MonthlyInt1;
               totalC = 0;     
               continue;                  
            }
         }
         else  
         {
            if (Per1 == termMonths)
            {
               if (basedOnMonthlyInt == 1)   // Deal with G% first
               {
                  if (PayDiff > firstpayment || 50.0 > firstpayment)
                  {
                     if (PayDiff > 50.0)
                     {
                        firstpayment = PayDiff;
                        PayTotal = PayDiff;
                        basedOnUser = 1;
                     }
                     else
                     {
                        firstpayment = 50.0;
                        PayTotal = 50.0;
                        basedOnFifty = 1;
                     }
                     Per1 = 1;
                     MonthlyInt1 = IR*P;

                     Per1Remainder = P-(PayTotal-MonthlyInt1);
                     Int1 = MonthlyInt1;
                     totalC = 0;     
                     continue;
                  }
               }
               if (transition == 0 || transition == 1)
               {
                  transition = 1;
                  firstpayment = firstpayment + .01;
               }
               else
               {
                  if (PayDiff > firstpayment || 50.0 > firstpayment)
                  {
                     if (PayDiff > 50.0)
                     {
                        firstpayment = PayDiff;
                        PayTotal = PayDiff;
                        basedOnUser = 1;
                     }
                     else
                     {
                        firstpayment = 50.0;
                        PayTotal = 50.0;
                        basedOnFifty = 1;
                     }
                     Per1 = 1;
                     MonthlyInt1 = IR*P;

                     Per1Remainder = P-(PayTotal-MonthlyInt1);
                     Int1 = MonthlyInt1;
                     totalC = 0;     
                     continue;
                  }
               }
            }
            else // Per1 > term
            {
               if (transition == 0 || transition == 1)
                  transition = 1;
               Per1 = 1;
               MonthlyInt1 = IR*P;
               firstpayment = firstpayment + .01;
               PayTotal = firstpayment;
               Per1Remainder = P-(PayTotal-MonthlyInt1);
               Int1 = MonthlyInt1;
               totalC = 0;     
               continue;                     
            }
         } // End 3 decisions
      } // end situation where amortization terminated
      Per1Remainder = Per1Remainder-(PayTotal-MonthlyInt1);
   
      Per1++;
      Int1 += MonthlyInt1;   
   } // end while
   
   totalC = P + Int1;
   
   var Regularcost = totalC;
   var Regularinterest = Int1;
   var Regularnum = Per1;
  
   Per1 = 1;
   PayTotal = firstpayment;
      
   MonthlyInt1 = IR*P;
   Per1Remainder = P-(PayTotal-MonthlyInt1);

   Int1 = MonthlyInt1;
   totalC = 0;
   
   while (Per1 <= termMonths)
   {   
      if (Per1 == 1)
      {
          p = new Array();
          for (i = 0; i < 15; i++)
            p[i] = "";
           p[0] = formatDollar(FormatCrr(firstpayment),2);
      }
      if (Per1%24 == 0)
      {
         PayTotal =  PayTotal*(1 + GDPc);
         p[Per1/24] = formatDollar(FormatCrr(PayTotal),2);
      }
      
      MonthlyInt1 = IR*Per1Remainder;

      /* Termination of amortization has occured if... */
      if (Per1Remainder <= PayTotal) 
      {
         PayTotal = Per1Remainder + MonthlyInt1;
         Per1++;
         Int1 += MonthlyInt1;
         break;
      }
      
      Per1Remainder = Per1Remainder-(PayTotal-MonthlyInt1);
      Per1++;
      Int1 += MonthlyInt1;
         
   } // end while
   
   totalC = P + Int1;

   document.getElementById("GRADMonthly").innerHTML = formatDollar(FormatCrr(firstpayment),2);
   document.getElementById("GRADTotalInterestNoDisc").innerHTML = formatDollar(FormatCrr(Int1),2);
   document.getElementById("GRADTermNoDisc").innerHTML = Per1;

    var strCompTotInt = document.getElementById("GRADTotalInterestNoDisc").innerHTML;
   var strTotInt = document.getElementById("GRADInterest").innerHTML;
 
    var re = /\$/gi;
   var re2 = /,/gi;
   strCompTotInt = strCompTotInt.replace(re, '');
   strCompTotInt = strCompTotInt.replace(re2, '');
   strTotInt = strTotInt.replace(re, '');
   strTotInt = strTotInt.replace(re2, '');
   var intSavings = strCompTotInt - strTotInt;
   var pmtSavings = document.getElementById("GRADTermNoDisc").innerHTML - document.getElementById("GRADTotalAmt").innerHTML;
   
   document.getElementById("GRADInterestDiff").innerHTML = formatDollar(FormatCrr(intSavings),2);  
   var strTotInt2 = document.getElementById("GRADInterest2").innerHTML;
   var re = /\$/gi;
   var re2 = /,/gi;
   strTotInt2 = strTotInt2.replace(re, '');
   strTotInt2 = strTotInt2.replace(re2, '');
    var intSavings2 = strCompTotInt - strTotInt2;
    var pmtSavings2 = document.getElementById("GRADTermNoDisc").innerHTML - document.getElementById("GRADTotalAmt2").innerHTML;
   
   document.getElementById("GRADInterestDiff2").innerHTML = formatDollar(FormatCrr(intSavings2),2);  
   
   

}

