/* Movie Strip Slide Show */
var maxWidth;

function show(num) {
 var stats = document.getElementById("interactiveStats");
 var elements = stats.getElementsByTagName("div");
 for (var a = 0; a < elements.length; a++) {
  var element = elements[a];
  if (element) {
   if (element.id == num) {
    element.style.display = "block";
   } else if (element.className != 'leftColDesc') {
    element.style.display = "none";
   }
  }
 }
}


function next(dir) {
 var delta = dir * -135;
 var element = document.getElementById('slideShow');
 var position = element.style.left;
 if (position.indexOf('px') != -1) {
  position = position.substring(0, position.length - 2);
  position = parseInt(position);
  position += delta;
 } else {
  position = delta;
 }

 if (position < -maxWidth) {
  position = -maxWidth;
 }

 if (position > 0) {
  position = 0;
 }
 
 position += 'px';
 element.style.left = position;
}

function setNumImages(numImages) {
 maxWidth = numImages * 135;
 maxWidth -= 378;
}

function setFullImage(src) {
 var fullImage = document.getElementById("full_image");
 fullImage.src = src;
}

// Mortgage Calculor
// FUNCTION: FormatAsMoney
// Formats a number into a string representing currency
// note that it truncates (instead of rounding) to cents
function FormatAsMoney (number) {
    var stringmoney = ""
    var dollars = ""
    var cents = ""

    dollars = parseInt(number);

    cents = parseInt((number - dollars)*100);

    if ( cents<10 ){
        stringmoney = dollars + ".0" + cents;
    } else {
        stringmoney = dollars + "." + cents;
    }
    return stringmoney
}

function validateNumber(number, min, max, msg) {
    var st = "";
    var str = "";
    var i  = 0;
    var ch = "";

    msg = msg + " contains an invalid value. It must be a number between " + min + " and " + max;
    st = number.value;

    for (i = 0; i < st.length; i++)
    {
        ch = st.substring(i, i + 1);
        if (ch != ',') {
          str = str + ch;
        }
    }

    for (i = 0; i < str.length; i++) {
        ch = str.substring(i, i + 1);
        if ((ch < "0" || "9" < ch) && ch != '.') {
            alert(msg + " You entered: " + str);
            return false;
        }
    }

    var num = 0 + str;
    if (num < min || max < num) {
        alert(msg + " You entered: " + str);
        return false;
    }

    number.value = str;
    return true;
}

function AmountCanBorrow() {

  form = document.LoanCalc;

  form.price.value = "";

  if (validateNumber(form.downpayment,0,10000000,"The down payment field")

    && validateNumber(form.interest,1,100,"The interest rate field")

    && validateNumber(form.payment,1,100000,"The monthly payment field")

    && validateNumber(form.period,1,100,"The amortization period")) {
    DP = form.downpayment.value;
    I = form.interest.value;
    MP = form.payment.value;
    N = form.period.value;
    I = Math.pow(1 + (I / 200), 1 / 6) - 1;
    N = N * 12
    CanBorrow = MP*(Math.pow(1 + I, N) - 1) / (I * Math.pow(1 + I, N))
    form.price.value = FormatAsMoney(CanBorrow + DP * 1)
    form.payment.value = FormatAsMoney(MP)
  }

}

function MustPutDown() {

  form = document.LoanCalc

  form.downpayment.Value = ""

  if (validateNumber(form.price, 0, 10000000, "The down payment field")
    && validateNumber(form.interest, 1, 100, "The interest rate field")
    && validateNumber(form.payment, 1, 100000, "The monthly payment field")
    && validateNumber(form.period, 1, 100, "The amortization period")) {

    P = form.price.value

    I = form.interest.value

    MP = form.payment.value

    N = form.period.value

    I = Math.pow(1 + (I / 200), 1 / 6) - 1

    N = N * 12

    CanBorrow = MP * (Math.pow(1 + I, N) - 1) / (I * Math.pow(1 + I, N))

    form.downpayment.value = FormatAsMoney(P - CanBorrow * 1)

    form.payment.value = FormatAsMoney(MP)
  }

}

function AmountOfPayment() {
  form = document.LoanCalc

  form.payment.Value = ""

  if ( validateNumber(form.downpayment, 0, 10000000, "The down payment field")
    && validateNumber(form.interest, 1, 100, "The interest rate field")
    && validateNumber(form.price, 1, 10000000, "The price of home field")
    && validateNumber(form.period, 1, 100, "The amortization period")) {

    DP = form.downpayment.value

    I = form.interest.value

    P = form.price.value

    N = form.period.value

    I = Math.pow(1+(I/200),1/6)-1

    N = N*12

    Payment = (P - DP) * I / (1 - Math.pow(1 + I,0 - N))

    form.payment.value = FormatAsMoney(Payment)
  }
}

function PeriodOfLoan() {

  form = document.LoanCalc

  form.period.Value = ""

  if (validateNumber(form.downpayment, 0, 10000000, "The down payment field")
    && validateNumber(form.interest, 1, 100, "The interest rate field")
    && validateNumber(form.payment, 1, 100000, "The monthly payment field")
    && validateNumber(form.price, 1, 10000000, "The price of home field")) {

    DP = form.downpayment.value

    I = form.interest.value

    I = Math.pow(1 + (I / 200),1 / 6) - 1

    MP = form.payment.value

    P = form.price.value

    var PrdNum = 1

    if (P*I > MP) {

      alert("The monthly payment is less than the monthly interest. Increase the monthly payment.")
    } else {

      P = P - DP

      while (P > 0) {
        P = (P * (1 + I))
        P = (P - MP)
        PrdNum = PrdNum + 1
      }

      if (P < 0) {
        PrdNum = PrdNum - 1
      }
      form.period.value = Math.round(PrdNum / 12, 0)
    }
  }
}

/* Quick Search */
function swapSearch(id) {
 var qsAspen = document.getElementById('qsAspen');
 var qsVail = document.getElementById('qsVail');
 if (id == 'vail') {
  qsAspen.style.display = 'none';
  qsVail.style.display = 'block';
 } else {
  qsAspen.style.display = 'block';
  qsVail.style.display = 'none';
 } 
}

function homePageFlash() {
 document.writeln('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" height="272" width="722">  <param name="movie" value="/images/banners/flash_intro.swf"> <param name="quality" value="best"> <param name="wmode" value="transparent">  <param name="play" value="true">  <embed height="272" pluginspage="http://www.macromedia.com/go/getflashplayer" src="/images/banners/flash_intro.swf" type="application/x-shockwave-flash" width="722" quality="best" play="true" wmode="transparent"></object>');
}