/*----------------------*/
/* Calculator functions */
/*----------------------*/
function calculate()
{
  var amt  = $F("LA");
  var base = 1;
  var ir   = $F("IR") / 1200;
  var term = $F("YR");
  
  for(var i = 0; i < term * 12; i++) {
    base = base * (1 + ir);
  }
  
  var payment = amt * ir / ( 1 - (1 / base)) + $F("AT") / 12 + $F("AI") / 12;
  
  $("PI").value = floor(amt * ir / ( 1 - (1 / base)));
  $("MT").value = floor($F("AT") / 12);
  $("MI").value = floor($F("AI") / 12);
  $("MP").value = floor(payment);
}

function floor(number)
{
  return Math.floor(number * Math.pow(10, 2)) / Math.pow(10, 2);
}


/*------------------*/
/* Filter functions */
/*------------------*/
function showHideFilter(id)
{
  var el  = $(id);
  if(el) {
    if(el.visible()) {
      new Effect.SlideUp(el);
    }
    else {
      new Effect.SlideDown(el);
    }
  }
}

/**
 * Switches out the price range select boxes when you select "For Rent" or "For Sale"
 */
function switchPriceRange(el)
{
  var maxPrice = $('sMaxPrice');
  var minPrice = $('sMinPrice');
  var maxLeasePrice = $('sMaxLeasePrice');
  var minLeasePrice = $('sMinLeasePrice');
  var maxRentalPrice = $('sMaxRentalPrice');
  var minRentalPrice = $('sMinRentalPrice');
  
  if(el.value == 'For Rent' && el.checked) {
    if(maxRentalPrice) {
      maxRentalPrice.show();
      minRentalPrice.show();
      maxRentalPrice.name = 'maxPrice';
      minRentalPrice.name = 'minPrice';
    }
    if(maxPrice) {
      maxPrice.hide();
      minPrice.hide();
      maxPrice.name = 'maxSalePrice';
      minPrice.name = 'minSalePrice';
    }
    if(maxLeasePrice) {
      maxLeasePrice.hide();
      minLeasePrice.hide();
      maxLeasePrice.name = 'maxLeasePrice';
      minLeasePrice.name = 'minLeasePrice';
    }
  }
  else if(el.value == 'For Lease' && el.checked) {
    if(maxLeasePrice) {
      maxLeasePrice.show();
      minLeasePrice.show();
      maxLeasePrice.name = 'maxPrice';
      minLeasePrice.name = 'minPrice';
    }
    if(maxPrice) {
      maxPrice.hide();
      minPrice.hide();
      maxPrice.name = 'maxSalePrice';
      minPrice.name = 'minSalePrice';
    }
    if(maxRentalPrice) {
      maxRentalPrice.hide();
      minRentalPrice.hide();
      maxRentalPrice.name = 'maxRentalPrice';
      minRentalPrice.name = 'minRentalPrice';
    }
  }
  else {
    if(maxPrice) {
      maxPrice.show();
      minPrice.show();
      maxPrice.name = 'maxPrice';
      minPrice.name = 'minPrice';
    }
    if(maxLeasePrice) {
      maxLeasePrice.hide();
      minLeasePrice.hide();
      maxLeasePrice.name = 'maxLeasePrice';
      minLeasePrice.name = 'minLeasePrice';
    }
    if(maxRentalPrice) {
      maxRentalPrice.hide();
      minRentalPrice.hide();
      maxRentalPrice.name = 'maxRentalPrice';
      minRentalPrice.name = 'minRentalPrice';
    }
  }
}
