//Function returns Amount rounded up to the nearest thousand if Amount < 100000000.0 or ten thousand if Amount >= 100000001.0
function RoundUp(Amount)
{
  NewValue = 1.0;
  Divisor = 1000.0;
  Amount = Amount / Divisor;
  NewValue = Math.ceil(Amount);
  return NewValue * Divisor;
}

//Function returns Amount without any commas
function RemoveCommas(Amount)
{
  for(Iteration = 0; Iteration < Amount.length; Iteration++)
    if(Amount.charAt(Iteration) == ",")
    {
      Amount = Amount.substring(0, Iteration) + Amount.substring(Iteration + 1, Amount.length + 1);
      Iteration--;
    }
  return Amount;
}

//Function converts Amount into currency format (i.e. xxx,xxx,xxx.xx) and returns the converted Amount.
function Currency(Amount) 
{
  if(Amount == "Invalid Amount" || Amount == "Please Call" || Amount == "")
  {}
  else
  {
    anynum=eval(Amount);
    workNum=Math.abs((Math.round(anynum*100)/100));workStr=""+workNum;
    if(workStr.indexOf(".")==-1){workStr+=".00"}
      dStr=workStr.substr(0,workStr.indexOf("."));dNum=dStr-0;
    pStr=workStr.substr(workStr.indexOf("."));
    while (pStr.length<3){pStr+="0"};

    //--- Adds comma in thousands place.
    if (dNum>=1000) 
    {
      dLen=dStr.length;
      dStr=parseInt(""+(dNum/1000))+","+dStr.substring(dLen-3,dLen);
    }

    //-- Adds comma in millions place.
    if(dNum>=1000000) 
    {
      dLen=dStr.length;
      dStr=parseInt(""+(dNum/1000000))+","+dStr.substring(dLen-7,dLen);
    }
    retval=dStr+pStr; 
    if(anynum < 0) 
    {
      retval=retval.substring(1,retval.length);
      retval="("+retval+")";        
    }
    Amount = retval;
  }
  return Amount;
}

//Function returns Premium based upon Amount for a Standard Owner's Policy 
function StandardRates(Amount)
{
  if(Amount <= 0.0)
    Premium = "Invalid Amount";
  else if(Amount > 0.0 && Amount < 15001.0)
    Premium = 180.5;
  else
  {
    Amount = RoundUp(Amount);
    if(Amount >= 15001.0 && Amount < 35001)
    {
      exeLine = "((Amount - 15000.0) / 1000.0) * 5.25 + 180.5"
      Premium = eval(exeLine);
    }
    else if(Amount >= 35001.0 && Amount < 50001)
    {
      exeLine = "((Amount - 35000.0) / 1000.0) * 5.0 + 285.5"
      Premium = eval(exeLine);
    }
    else if(Amount >= 50001.0 && Amount < 100001)
    {
      exeLine = "((Amount - 50000.0) / 1000.0) * 4.25 + 360.5"
      Premium = eval(exeLine);
    }
    else if(Amount >= 100001.0 && Amount < 250001)
    {
      exeLine = "((Amount - 100000.0) / 1000.0) * 2.39 + 573.0"
      Premium = eval(exeLine);
    }
    else if(Amount >= 250001.0 && Amount < 350001)
    {
      exeLine = "((Amount - 250000.0) / 1000.0) * 2.25 + 931.5"
      Premium = eval(exeLine);
    }
    else if(Amount >= 350001.0 && Amount < 1000001)
    {
      exeLine = "((Amount - 350000.0) / 1000.0) * 2.0 + 1156.5"
      Premium = eval(exeLine);
    }
    else if(Amount >= 1000001.0 && Amount < 5000001)
    {
      exeLine = "((Amount - 1000000.0) / 1000.0) * 1.75 + 2456.5"
      Premium = eval(exeLine);
    }
    else if(Amount >= 5000001.0 && Amount < 10000001)
    {
      exeLine = "((Amount - 5000000.0) / 1000.0) * 1.2 + 9456.5"
      Premium = eval(exeLine);
    }
    else if(Amount >= 10000001.0 && Amount < 50000001)
    {
      exeLine = "((Amount - 10000000.0) / 1000.0) * 1.15 + 15456.5"
      Premium = eval(exeLine);
    }
    else if(Amount >= 50000001.0 && Amount < 100000001)
    {
      exeLine = "((Amount - 50000000.0) / 1000.0) * 1.1 + 61456.5"
      Premium = eval(exeLine);
    }
    else if(Amount >= 100000001.0)
    {
      exeLine = "((Amount - 100000000.0) / 1000.0) * 1.05 + 116456.5"
      Premium = eval(exeLine);
    }
  }
  return Premium; 
}
function StandardOwnersPolicy(Amount)
{
    return Currency(StandardRates(Amount));
}
//Function returns Premium based upon Amount for a Standard Owner's Leasehold Policy 
function StandardOwnersLeaseholdPolicy(Amount)
{
    return Currency(StandardRates(Amount));
}

function StandardMortgageesPolicyWhenIssuedAlone(Amount)
{
    return Currency(StandardRates(Amount));
}

//Function returns Premium based upon Amount for a Standard Mortgagee's Leasehold Policy (when issued alone) 
function StandardMortgageesLeaseholdPolicyWhenIssuedAlone(Amount)
{
    return Currency(StandardRates(Amount));
}

//Function returns Premium based upon Amount for a Litigation Guarantee 
function LitigationGuarantee(Amount)
{
    return Currency(StandardRates(Amount));
}

//Function returns Premium based upon Amount for an Extended Owner's Policy 
function ExtendedOwnersPolicy(Amount)
{
    return Currency(StandardRates(Amount) * 1.5);
 
}

//Function returns Premium based upon Amount for an Extended Owner's Leasehold Policy 
function ExtendedOwnersLeaseholdPolicy(Amount)
{
    return Currency(StandardRates(Amount) * 1.5);
}

//Function returns Premium based upon Amount and Owner's Policy Amount for a Standard Mortgagee's Policy (when issued simultaneously)
function StandardMortgageesPolicyWhenIssuedSimultaneously(Amount, OwnerPolicyAmount)
{
  if(Amount <= OwnerPolicyAmount)
    Amount = 40.0;
  else
    Amount = "Please Call";
  return Currency(Amount);
}

//Function returns Premium based upon Amount and Owner's Policy Amount for a Standard Mortgagee's Leasehold Policy (when issued simultaneously)
function StandardMortgageesLeaseholdPolicyWhenIssuedSimultaneously(Amount, OwnerPolicyAmount)
{
  if(Amount <= OwnerPolicyAmount)
    Amount = 40.0;
  else
    Amount = "Please Call";
  return Currency(Amount);
}
  
//Function returns Premium based upon Amount for a Extended Mortgagee's Policy (when issued with another policy) 
function ExtendedMortgageesPolicyWhenIssuedWithAnotherPolicy(Amount)
{
    return Currency(StandardRates(Amount) * .3 + 40.0);
}

//Function returns Premium based upon Amount for a Extended Mortgagee's Leasehold Policy (when issued with another policy) 
function ExtendedMortgageesLeaseholdPolicyWhenIssuedWithAnotherPolicy(Amount)
{
    return Currency(StandardRates(Amount));
}

//Function returns Premium based upon Amount for a Trustee Sale Guarantee 
function TrusteeSaleGuarantee(Amount)
{
    return Currency(StandardRates(Amount));
}

//Function returns Premium based upon Amount for a Extended Mortgagee's Policy (when issued alone) 
function ExtendedMortgageesPolicyWhenIssuedAlone(Amount)
{
    return Currency(StandardRates(Amount) * 1.3);
}

//Function returns Premium based upon Amount for a Extended Mortgagee's Leasehold Policy (when issued alone) 
function ExtendedMortgageesLeaseholdPolicyWhenIssuedAlone(Amount)
{
    return Currency(StandardRates(Amount) * 1.3);
}

//Function returns Premium based upon Amount for a Junior Loan Policy
function JuniorLoanPolicy(Amount)
{
  if(Amount <= 0.0 || Amount > 250000.0)
    Premium = "Invalid Amount"; 
  else
  {
    if(Amount > 0.0 && Amount <= 100000.0)
      Premium = 100.0;
    else if(Amount > 100000 && Amount <= 125000.0)
      Premium = 125.0;
    else if(Amount > 125000 && Amount <= 150000.0)
      Premium = 150.0;
    else if(Amount > 150000 && Amount <= 200000.0)
      Premium = 175.0;
	else if(Amount > 200000 && Amount <= 250000.0)
      Premium = 200.0;
  }
  return Currency(Premium);
}
      
