<!--
function removeCurrencyFormat(entry)
{
	entry = entry.replace('$','');
	entry = entry.replace(',','');
	entry = entry.replace('-','');
	return entry;
}

function replaceChar(entry) {
	out = "$"; 
	add = ""; 
	temp = "" + entry;
	while (temp.indexOf(out)>-1) {
		pos= temp.indexOf(out);
		temp = "" + (temp.substring(0, pos) + add + 
		temp.substring((pos + out.length), temp.length));
	}
	return temp;
}
	
function replaceChars(entry) {
	out = ","; 
	add = ""; 
	temp = "" + entry;
	while (temp.indexOf(out)>-1) {
		pos= temp.indexOf(out);
		temp = "" + (temp.substring(0, pos) + add + 
		temp.substring((pos + out.length), temp.length));
	}
	return temp;
}

function checkForm(toCheck) {
	var dotcount = 0;
	isNum = true;
	for (j = 0; j < toCheck.length; j++) {
	if (toCheck.substring(j,j+1) == ".")
	{
		dotcount = dotcount + 1;
		if (dotcount > 1) isNum = false;
	}
	if ((toCheck.substring(j,j+1) < "0") && (toCheck.substring(j,j+1) != "-") && (toCheck.substring(j,j+1) != ".") || (toCheck.substring(j,j+1) > "9")) {
      isNum = false;
      }
    }
  if ((isNum == false) || (toCheck.length == 0) || (toCheck == null)) {
  alert("Please enter only numerical data in all fields.");
  return false;
  }
  else {
  return true;
  }
}
function formatCurrency(num) 
{			
	num=replaceChar(num);
	num=replaceChars(num);
	
	if ( checkForm(num));
	{
				num = removeCurrencyFormat(num.toString());
				if(isNaN(num)) num = "0";
				num = Math.floor(num).toString();
			for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++) 
				num = num.substring(0,num.length-(4*i+3))+','+num.substring(num.length-(4*i+3));
				 
		return ('$' + num );	
	}
}


function formatCurrencyThousand(num) 
{			
	num=replaceChar(num);
	num=replaceChars(num);
	
	if ( checkForm(num));
	{
		num = removeCurrencyFormat(num.toString());
		if(isNaN(num)) num = "0";
		num = Math.floor(num).toString();
			
		if (num >= 1000)		
		{
		for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++) 
			num = num.substring(0,num.length-(4*i+3))+','+num.substring(num.length-(4*i+3));
		}
		else
		{
			num = num + ",000";
		}
				
		return ('$' + num );	
	}
}


function formatCurrencyThousandWithoudDollar(num) 
{			
	num=replaceChar(num);
	num=replaceChars(num);
	
	if ( checkForm(num));
	{
		num = removeCurrencyFormat(num.toString());
		if(isNaN(num)) num = "0";
		num = Math.floor(num).toString();
			
		if (num >= 1000)		
		{
		for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++) 
			num = num.substring(0,num.length-(4*i+3))+','+num.substring(num.length-(4*i+3));
		}
		else
		{
			num = num + ",000";
		}
				
		return ( num );	
	}
}

function formatCurrencyWithoutDollar(num) 
{			
	num=replaceChar(num);
	num=replaceChars(num);
	
	if ( checkFormWithoutDollar(num));
	{
		num = removeCurrencyFormat(num.toString());
		if(isNaN(num)) num = "0";
		num = Math.floor(num).toString();
		
		for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++) 
		num = num.substring(0,num.length-(4*i+3))+','+num.substring(num.length-(4*i+3));
			
		return (num);	
	}		
}

function removeSpaces(string) {
	var tstring = "";
	string = '' + string;
	splitstring = string.split(" ");
	for(i = 0; i < splitstring.length; i++)
	tstring += splitstring[i];
	return tstring;
}


function changedValue(id, num0) 
{				
	num=replaceChar(num0);
	num=replaceChars(num);
	num=removeSpaces(num);
				
	if (checkFormWithoutDollar(num))
	{					
		num = removeCurrencyFormat(num.toString());
		//if(isNaN(num)) num = "0";
		//num = Math.floor(num).toString();					
		
		for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++) 
			num = num.substring(0,num.length-(4*i+3))+','+num.substring(num.length-(4*i+3));
			
		num0 = num;		
					
		var loanAmount = removeCurrencyFormat(document.getElementById("txtLoanAmount").value);								
		var secondaryLoanAmount = removeCurrencyFormat(document.getElementById("txtPiggyAmount").value);				
		var propertyValue = removeCurrencyFormat(document.getElementById("txtPropertyValue").value);			
		var downPayment = removeCurrencyFormat(document.getElementById("txtDownPayment").value);			
				
		if (id == 1)
		{				
			downPayment = removeCurrencyFormat(propertyValue) - removeCurrencyFormat(loanAmount) - removeCurrencyFormat(secondaryLoanAmount);										
			downPayment = replaceChars(downPayment);
		
			if (downPayment == "" || isNaN(downPayment) || (downPayment- 0) < 0) downPayment = "0";
			
			calculateCLTV(loanAmount, secondaryLoanAmount, propertyValue);
			
			for (var j = 0; j < Math.floor((downPayment.length-(1+j))/3); j++) 
			downPayment = downPayment.substring(0,downPayment.length-(4*j+3))+','+downPayment.substring(downPayment.length-(4*j+3));								
			document.getElementById("txtDownPayment").value = downPayment;									
		}
		else 
		{
			loanAmount = removeCurrencyFormat(propertyValue) - removeCurrencyFormat(downPayment) - removeCurrencyFormat(secondaryLoanAmount);																
			loanAmount = replaceChars(loanAmount);
		
			if (loanAmount == "" || isNaN(loanAmount) || (loanAmount - 0) < 0) 
			{
				loanAmount = "0";
			}
			
			calculateCLTV(loanAmount, secondaryLoanAmount, propertyValue);
			
			for (var j = 0; j < Math.floor((loanAmount.length-(1+j))/3); j++) 
			loanAmount = loanAmount.substring(0,loanAmount.length-(4*j+3))+','+loanAmount.substring(loanAmount.length-(4*j+3));								
			document.getElementById("txtLoanAmount").value = loanAmount;															
		}																											
	}		
	ShowBottomMessage(1);			
	return (num0);
}


function calculateCLTV(loanAmount, secondaryLoanAmount, propertyValue)
{
	var LTV = removeCurrencyFormat(document.getElementById("txtLTV").value);	
	var CLTV = removeCurrencyFormat(document.getElementById("txtCLTV").value);										
		
	LTV = Math.round((loanAmount/propertyValue*100)*100)/100;
	if (LTV < 0 || LTV > 100 || isNaN(LTV))
	{
		document.getElementById("txtLTV").value = "Incorrect";																																													
	}
	else
	{
		document.getElementById("txtLTV").value = LTV + " %";	
	}
		
	CLTV = Math.round((((loanAmount - 0) + (secondaryLoanAmount - 0))/propertyValue*100)*100)/100;
	if (CLTV < 0 || CLTV > 100 || isNaN(CLTV))
	{	
		document.getElementById("txtLTV").value = "Incorrect";		
	}
	else
	{
		document.getElementById("txtCLTV").value = CLTV + " %";	
	}
}


function checkFormWithoutDollar(toCheck) {	
	isNum = true;
	for (j = 0; j < toCheck.length; j++) {
	if ((toCheck.substring(j,j+1) < "0") && (toCheck.substring(j,j+1) != ".") || (toCheck.substring(j,j+1) > "9")) {
      isNum = false;
      }
    }
  if ((isNum == false) || (toCheck == null)) {
  alert("Please enter only numerical data in all fields.");
  return false;
  }
  else {
  return true;
  }
}

function calcRound(num)
      {
      result="$"+Math.floor(num)+"." 
      n = result.length
      if (num>1000 && num<999999)
        result="$"+result.substring(1,n-4)+","+result.substring(n-4,n);
      if (num>1000000)
        result = "$"+result.substring(1,n-7)+","+result.substring(n-7,n-4)+","+result.substring(n-4,n);
      var cents=100*(num-Math.floor(num))+0.5
      result += Math.floor(cents/10)
      result += Math.floor(cents%10)
      return(result)
      }



function BeforeSumbmit() {

	var LoanAmount;
	LoanAmount = document.form1.loanAmount.value;	
	LoanAmount = removeCurrencyFormat(LoanAmount);
	var PropValue;
	PropValue = document.form1.purchasePrice.value;
	PropValue = removeCurrencyFormat(PropValue);
	
	if (LoanAmount <= 0) {
		alert("Loan amount must be greater than 0.");
		return false;
	} else if (PropValue <= 0) {
		alert("Property value must be greater than 0.");
		return false;
	} else if ((document.form1.propertyType.selectedIndex == 3)||(document.form1.propertyType.selectedIndex == 4)){
		alert("Please call GMAC Mortgage at 1-877-941-4622 for a current quote on this option.");
		return false;
	} else if ((document.form1.occupancyType.selectedIndex == 2)||(document.form1.occupancyType.selectedIndex == 3)){
		alert("Please call GMAC Mortgage at 1-877-941-4622 for a current quote on this option.");
		return false;
	} else if (document.form1.loanPurpose.selectedIndex == 0) {
		alert("You must select a loan purpose.");
		return false		
	} else if (document.form1.loanProgram.selectedIndex == 0) {
		alert("You must select a loan program.");
		return false		
	} else if (document.form1.propertyType.selectedIndex == 0) {
		alert("You must select a property type.");
		return false		
	} else if (document.form1.occupancyType.selectedIndex == 0) {
		alert("You must select an occupancy type.");
		return false		
	} else if (document.form1.state.selectedIndex == "") {
		alert("You must select property location.");
		return false		
	} else {
		return true;
	}
}

function printable()
{
	document.temps.submit();
	
}

function floor(number)
{
  return Math.floor(number*Math.pow(10,2))/Math.pow(10,2);
}

function computeForm2(form){
	basicedit();
	
	var m = document.getElementById("YR").value*12;
	if (m< 70)
	{			
		alert ("Term in Years must be at least " + Math.round(70/12));
		document.getElementById("YR").value='50';	
	}
	else
	if (document.getElementById("YR").value > 50)
	{
		alert ("Term in Years must not be greater than " + 50);			
		document.getElementById("YR").value='50';		
	}		
		
	if (replacePct(document.getElementById("IR").value) > 20 )
	{
		alert ("Interest Rate must not be greater than " + 20 + " percent");			
		document.getElementById("IR").value=addPct('20');				
	}
	else
	if (replacePct(document.getElementById("IR").value) < 1 )
	{
		alert ("Interest Rate must be greater than " + 1 + " percent");
		document.getElementById("IR").value=addPct('1');				
	}		
		
	var IR = replacePct(document.getElementById("IR").value);
	var mi = IR / 1200;	
	var base = 1;
	var mbase = 1 + mi;
	var loanA = replaceChars(document.getElementById("LA").value);
	var loanA = replaceChar(loanA);
	if ( checkForm(IR) && checkForm(loanA) && checkForm(document.getElementById("YR").value));
{
	for (i=0; i<document.getElementById("YR").value * 12; i++)  
    base = base * mbase	
  }	
  
  //FORMULA !!!!!!!!!!!!
  
	//form.PI.value = calcRound(floor(loanA * mi / ( 1 - (1/base))));  	
	var PI_value = (loanA*((IR/(1200))/(1-(Math.pow(1+(IR/(1200)),((m)*-1))))));
	document.getElementById("PI").value =  calcRound(PI_value);
	
    document.getElementById("TI").value = calcRound(((PI_value*(m))-loanA));    
	  
}

function replaceChar(entry) {
	out = "$"; 
	add = ""; 
	temp = "" + entry;
	while (temp.indexOf(out)>-1) {
		pos= temp.indexOf(out);
		temp = "" + (temp.substring(0, pos) + add + 
		temp.substring((pos + out.length), temp.length));
	}
	return temp;
}

function replacePct(entry) {
	out = "%"; 
	add = ""; 
	temp = "" + entry;
	while (temp.indexOf(out)>-1) {
		pos= temp.indexOf(out);
		temp = "" + (temp.substring(0, pos) + add + 
		temp.substring((pos + out.length), temp.length));
	}
	return temp;
}	
	
function replaceChars(entry) {
	out = ","; 
	add = ""; 
	temp = "" + entry;
	while (temp.indexOf(out)>-1) {
		pos= temp.indexOf(out);
		temp = "" + (temp.substring(0, pos) + add + 
		temp.substring((pos + out.length), temp.length));
	}
	return temp;
}

function formatCurrency(num) {	
			num=replaceChar(num);
			num=replaceChars(num);
		if ( checkForm(num));
{
			num = num.toString().replace(/$|,/g,'');
			if(isNaN(num)) num = "0";
			num = Math.floor(num).toString();
		for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++) 
			num = num.substring(0,num.length-(4*i+3))+','+num.substring(num.length-(4*i+3));
			 
	return ('$' + num );	
}
}

function basicedit()
{
	if 	(!IsMoney(document.getElementById("LA").value) || document.getElementById("LA").value.length ==0)
		document.getElementById("LA").value='$200,000';

	if 	(!IsPct(document.getElementById("IR").value) || document.getElementById("IR").value.length ==0)
		document.getElementById("IR").value='6.5%';
		
	if 	(!IsNumber(document.getElementById("YR").value) || document.getElementById("YR").value.length ==0)
		document.getElementById("YR").value='50';				
}	

function IsMoney(val)
	{
		var number="0123456789$,.";

		for (var i=0;i<val.length;i++)
		{
			if (number.indexOf(val.charAt(i)) == -1)
			{
				alert('e');
				return false;
			}
		}
		return true;
	}

function IsNumber(val)
	{
		var number="0123456789.";

		for (var i=0;i<val.length;i++)
		{
			if (number.indexOf(val.charAt(i)) == -1)
			{
				return false;
			}
		}
		return true;
	}
	
function IsPct(val)
	{
		var number="0123456789.%,";

		for (var i=0;i<val.length;i++)
		{
			if (number.indexOf(val.charAt(i)) == -1)
			{
				return false;
			}
		}
		return true;
	}

function addPct(pct) {
			pct=replacePct(pct);
			pct=parseFloat(pct);		
		if (checkForm(pct));
	{
			return (pct + '%');
		}
}

function checkForm(toCheck) {
	isNum = true;
	for (j = 0; j < toCheck.length; j++) {
	if ((toCheck.substring(j,j+1) < "0") && (toCheck.substring(j,j+1) != ".") || (toCheck.substring(j,j+1) > "9")) {
      isNum = false;
      }
    }
  if ((isNum == false) || (toCheck.length == 0) || (toCheck == null)) {
  alert("Please enter only numerical data in all fields.");
  return false;
  }
  else {
  return true;
  }
}

function formatNum(Vnum) {

   if (isNaN(Vnum)) {Vnum = 0;}

   if(Vnum > 99000000) {
   alert("Sorry, this will not generate numbers larger that 99 million.");
   focus();
   } else {
   
   var V10million = parseInt(Vnum / 10000000);

   var V1million = (Vnum % 10000000)  / 1000000;
      if(V1million / 1000000 == 1) {
      V1million = 1;
      } else
      if(V1million < 1) {
      V1million = "0";
     } else {
      V1million = parseInt(V1million,10);
     }

    var V100thousand = (Vnum % 1000000)  / 100000;
      if(V100thousand / 100000 == 1) {
      V100thousand = 1;
      } else
      if(V100thousand < 1) {
      V100thousand = "0";
     } else {
      V100thousand = parseInt(V100thousand,10);
     }

   var V10thousand = (Vnum % 100000)  / 10000;
      if(V10thousand / 10000 == 1) {
      V10thousand = 1;
      } else
      if(V10thousand < 1) {
      V10thousand = "0";
      } else {
      V10thousand = parseInt(V10thousand,10);
      }

   var V1thousand = (Vnum % 10000)  / 1000;
      if(V1thousand / 1000 == 1) {
      V1thousand = 1;
      } else
      if(V1thousand < 1) {
      V1thousand = "0";
     } else {
      V1thousand = parseInt(V1thousand,10);
     }

   var Vhundreds = (Vnum % 1000)  / 100;
      if(Vhundreds / 100 == 1) {
      Vhundreds = 1;
      } else
      if(Vhundreds < 1) {
      Vhundreds = "0";
     } else {
      Vhundreds = parseInt(Vhundreds,10);
     }

   var Vtens = (Vnum % 100)  / 10;
      if(Vtens / 10 == 1) {
      Vtens = 1;
      } else
      if(Vtens < 1) {
      Vtens = "0";
     } else {
      Vtens = parseInt(Vtens,10);
     }

   var Vones = (Vnum % 10)  / 1;
      if(Vones / 1 == 1) {
      Vones = 1;
      } else
      if(Vones < 1) {
      Vones = "0";
     } else {
      Vones = parseInt(Vones,10);
     }

  //START UPGRADE
var Vcents = 0;

if(Vnum % 1 * 100 < 1) {
   Vcents = 0;
   } else {
   Vcents = parseInt(((eval(Vnum % 1) * 100)),10);
   }
//END UPGRADE

 if(Vcents < 1) {
  Vcents = "00";
  }
  else
  if(Vcents % 10 == 0) {
  Vcents = Vcents + "0";
  }
  else
  if(Vcents % 10 == Vcents) {
  Vcents = "0" + Vcents;
  } else {
  Vcents = Vcents;
  }

  if(Vcents == "900") {
  Vcents = "90";
  } else
  if(Vcents == "800") {
  Vcents = "80";
  } else 
  if(Vcents == "700") {
  Vcents = "70";
  } else 
  if(Vcents == "600") {
  Vcents = "60";
  } else 
  if(Vcents == "500") {
  Vcents = "50";
  } else 
  if(Vcents == "400") {
  Vcents = "40";
  } else 
  if(Vcents == "300") {
  Vcents = "30";
  } else
  if(Vcents == "200") {
  Vcents = "20";
  } else
  if(Vcents == "100") {
  Vcents = "10";
  } else {
  Vcents = Vcents;
  }

   var Vformat = "";

   if(Vnum >= 10000000) {
   Vformat = (V10million + "" + V1million + "," + V100thousand + "" + V10thousand + "" + V1thousand + "," + Vhundreds + "" + Vtens + "" + Vones + "." + Vcents);
   }
   else
   if(Vnum >= 1000000) {
   Vformat = (V1million + "," + V100thousand + "" + V10thousand + "" + V1thousand + "," + Vhundreds + "" + Vtens + "" + Vones + "." + Vcents);
   }
   else
   if(Vnum >= 100000) {
   Vformat = (V100thousand + "" + V10thousand + "" + V1thousand + "," + Vhundreds + "" + Vtens + "" + Vones + "." + Vcents);
   }
   else
   if(Vnum >= 10000) {
   Vformat = (V10thousand + "" + V1thousand + "," + Vhundreds + "" + Vtens + "" + Vones + "." + Vcents);
   }
   else
   if(Vnum >= 1000) {
   Vformat = (V1thousand + "," + Vhundreds + "" + Vtens + "" + Vones + "." + Vcents);
   }
   else
   if(Vnum >= 100) {
   Vformat = (Vhundreds + "" + Vtens + "" + Vones + "." + Vcents);
   }
   else
   if(Vnum >= 10) {
   Vformat = (Vtens + "" + Vones + "." + Vcents);
   }
   else
   if(Vnum >= 1) {
   Vformat = (Vones + "." + Vcents);
   } else {
   Vformat = ("0." + Vcents);
   }

  return Vformat;

  }
}

//-->
