// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

var infoDialogOpen = false;
var defaultTitle = "Sorry there was a problem!";

var medRent;
var vacancyRate;
var interestRate;
var years;
var insurance;
var taxes;
var mgmt;
var other;
var fixUp;
var downPayment;
var isDownPaymentPercent;
var disputeAmt;

var otherArray = new Array();
var fixUpArray = new Array();

function showWorkingSearchNextPage()
{
  $('propertyListCell').innerHTML = $('working').innerHTML;
  $('paginationCell').hide();
}

function openAddingUserDialog()
{
  if (!infoDialogOpen)
  {
    infoDialogOpen = true;
    Dialog.info($('adding_user').innerHTML, {className: "alphacube", width:540});
  }
}

function openClosingUserAccountDialog()
{
  if (!infoDialogOpen)
  {
    infoDialogOpen = true;
    Dialog.info($('closing_account').innerHTML, {className: "alphacube", width:540});
  }
}

function openUpdateAccountDialog()
{
  if (!infoDialogOpen)
  {
    infoDialogOpen = true;
    Dialog.info($('update_account').innerHTML, {className: "alphacube", width:540});
  }
}

function openFetchDataDialog()
{
  $('metaDiv').innerHTML = $('please_wait').innerHTML;
}

function openWaitDialog()
{
  if (!infoDialogOpen)
  {
    infoDialogOpen = true;
    Dialog.info($('please_wait').innerHTML, {className: "alphacube", width:540});
  }
}

function closeFetchDataDialog()
{
  if (infoDialogOpen)
  {
    Dialog.closeInfo();
    infoDialogOpen = false;
  }
}

function showStreetView()
{
  $('property_image').hide();
  $('pano').show();          
}
        
function showImage()
{
  $('pano').hide();        
  $('property_image').show();          
}
        
function addNewExpense(siblingBefore, prefix, arrayName)
{
  var table = siblingBefore.parentNode;
  var index = siblingBefore.rowIndex;
  
  var newRow = table.insertRow(index);
  var cell0 = newRow.insertCell(0);
  var cell1 = newRow.insertCell(1);
  var cell2 = newRow.insertCell(2);    
  
  var keyId = prefix + "_label_" + index;
  var valueId = prefix + "_value_" + index;
  
  cell0.id= prefix + "_cell_" + index;
  cell0.innerHTML = "<img src=\"/images/txtbox_left.gif\" alt=\"\" width=\"6\" height=\"29\" class=\"float_left\">";
  cell0.innerHTML += "<input id=\"" + keyId + "\" style=\"width:100\" class=\"login_textbox_per\" type=\"text\" onblur=\"setNewExpenseLabel('" + cell0.id + "', this.id)\">";
  cell0.innerHTML += "<img src=\"/images/txtbox_right.gif\" alt=\"\" width=\"6\" height=\"29\" class=\"float_left\">";
  cell1.innerHTML = "<strong>:</strong>";
  cell2.innerHTML = "<img src=\"/images/txtbox_left.gif\" alt=\"\" width=\"6\" height=\"29\" class=\"float_left\">";
  cell2.innerHTML += "<input type=\"text\" style=\"width:100\" class=\"login_textbox_per\" onblur=\"" + arrayName + "['" + keyId + "'] = this.value\" id=\"" + valueId + "\">";
  cell2.innerHTML += "<img src=\"/images/txtbox_right.gif\" alt=\"\" width=\"6\" height=\"29\" class=\"float_left\">";
  cell2.innerHTML += "&nbsp;&nbsp;<a href=\"#\" onclick=\"deleteExpense('" + cell0.id + "', '" + keyId + "', " + arrayName + ")\">Delete</a>";
}

function deleteExpense(cellId, inputId, keyValArray)
{
  var name = document.getElementById(inputId).value;
  
  if (confirm("Are you sure you want to delete " + name + "?"))
  {
    var row = document.getElementById(cellId).parentNode;
    var table = row.parentNode;
  
    keyValArray[inputId] = null;    
    table.deleteRow(row.rowIndex);
  }
}

function setNewExpenseLabel(cellId, inputId)
{
  var cell = document.getElementById(cellId);
  var input = document.getElementById(inputId);
  
  cell.innerHTML = "<strong>" + input.value + "</strong>" + "<input type=\"hidden\" id=\"" + input.id + "\" value=\"" + input.value + "\">";
  
  return true;
}

function updateHiddenInput(keyValArray, hiddenInputId)
{
  var allValues = "";
  
  for (var key in keyValArray)
  {
    var val = keyValArray[key];
    if ( (val != null) && (!isNaN(val)) )
    {
      var label = $(key).value;
      allValues += label + "=" + keyValArray[key] + "|";
    }
  }
  
  $(hiddenInputId).value = allValues;
}

function showComps()
{
  if (!infoDialogOpen)
  {
    infoDialogOpen = true;
    Dialog.info($('comps').innerHTML, {className: "alphacube", width:650, height:375});
  }
}

function populateRent()
{
  var totalFinanced = 0;
  var numberOfPayments = years * 12;
          
  disputeAmt = parseFloat(disputeAmt);
  fixUp = parseFloat(fixUp);
  downPayment = parseFloat(downPayment);
  other = parseFloat(other);
  
  if (downPayment == 0)
  {
    totalFinanced = disputeAmt + fixUp;
  }
  else if (isDownPaymentPercent)
  {
    totalFinanced = (disputeAmt + fixUp) * (1-(downPayment/100));
  }
  else
  {
    totalFinanced = disputeAmt + fixUp - downPayment;
  }
  
  var monthlyInterest = interestRate/1200;
  
  var interestPrincipal = totalFinanced * (monthlyInterest/(1 - Math.pow(1 + monthlyInterest, numberOfPayments * -1)));

  var totalOther = other;
  var highTotalMonthlyExpenses = (insurance/12) + (taxes/12) + ((mgmt/100) * medRent) + ((vacancyRate/100) * medRent) + interestPrincipal + totalOther;

  var highIncome = Math.floor(medRent - highTotalMonthlyExpenses);   

  if (document.getElementById("rents"))                 
  {
    document.getElementById("rents").innerHTML = ("$" + highIncome);
  }
}

function showRentalWorksheet()
{
  if (!infoDialogOpen)
  {
    if ($('pano').visible)
    {
      $('pano').hide();
    }
    
    infoDialogOpen = true;
    var height = (screen.height * .62);
    Dialog.info($('rental_worksheet').innerHTML, {className: "alphacube", width:350, height:height});
  }
}

function validateRentalWorksheet()
{
  if (isPositiveDigit(medRent, 'Rental income must be a number that is zero or larger!') && 
      isPositivePercent(vacancyRate, 'Vacancy rate must be a number that is larger than zero and less than 100!') &&
      isPositiveDigit(insurance, 'Insurance must be a number that is zero or larger!') && 
      isPositiveDigit(taxes, 'Taxes must be a number that is zero or larger!') && 
      isPositivePercent(mgmt, 'Management must be a number that is larger than zero and less than 100!') && 
      isPositiveDigit(other, 'Other expenses must be a number that is zero or larger!') && 
      isPositivePercent(interestRate, 'Interest rate must be a number that is larger than zero and less than 100!') && 
      isPositiveDigit(years, 'Years must be a number that is zero or larger!') && 
      isPositiveDigit(disputeAmt, 'Purchase price must be a number that is zero or larger!') && 
      isPositiveDigit(fixUp, 'Fix up costs must be a number that is zero or larger!'))
    {
      if (isDownPaymentPercent && isPositivePercent(downPayment, 'Down payment must be a number that is larger than zero and less than 100!')) 
        return true;
      else if (!isDownPaymentPercent && isPositiveDigit(downPayment, 'Down payment must be a number that is zero or larger!'))
        return true;
    }
  
  return false;
}

function updateRentalWorksheet()
{
  $('rent_cell').innerHTML = "<input type=\"text\" onblur=\"if (isPositiveDigit(this.value, 'Rental income must be a number that is zero or larger!')) medRent = this.value;\" value=\"" + medRent + "\" id=\"rent\" />";
  $('vacancy_cell').innerHTML = "<input type=\"text\" onblur=\"if (isPositivePercent(this.value, 'Vacancy rate must be a number that is larger than zero and less than 100!')) vacancyRate = this.value;\" value=\"" + vacancyRate + "\" id=\"vacancy\" />";  
  $('insurance_cell').innerHTML = "<input type=\"text\" onblur=\"if (isPositiveDigit(this.value, 'Insurance must be a number that is zero or larger!')) insurance = this.value;\" value=\"" + insurance + "\" id=\"ins\"/>";
  $('tax_cell').innerHTML = "<input type=\"text\" onblur=\"if (isPositiveDigit(this.value, 'Taxes must be a number that is zero or larger!')) taxes = this.value;\" value=\"" + taxes + "\" id=\"taxes\"/>";
  $('mgmt_cell').innerHTML = "<input type=\"text\" onblur=\"if (isPositivePercent(this.value, 'Management must be a number that is larger than zero and less than 100!')) mgmt = this.value;\" value=\"" + mgmt + "\" id=\"mgmt\"/>";
  $('other_cell').innerHTML = "<input type=\"text\" onblur=\"if (isPositiveDigit(this.value, 'Other expenses must be a number that is zero or larger!')) other = this.value;\" value=\"" + other + "\" id=\"otherExpenses\" />";
  $('irate_cell').innerHTML = "<input type=\"text\" onblur=\"if (isPositivePercent(this.value, 'Interest rate must be a number that is larger than zero and less than 100!')) interestRate = this.value;\" value=\"" + interestRate + "\" id=\"irate\"/>";
  $('years_cell').innerHTML = "<input type=\"text\" onblur=\"if (isPositiveDigit(this.value, 'Years must be a number that is zero or larger!')) years = this.value;\" value=\"" + years + "\" id=\"years\"/>";
  $('pur_cell').innerHTML = "<input type=\"text\" onblur=\"if (isPositiveDigit(this.value, 'Purchase price must be a number that is zero or larger!')) disputeAmt = this.value;\" value=\"" + disputeAmt + "\" id=\"purchase_price\" />";
  $('dp_cell').innerHTML = "<input type=\"text\" onblur=\"if ($('isPercent').checked) {if (isPositivePercent(this.value, 'Down payment must be a number that is larger than zero and less than 100!')) downPayment = this.value;} else {if (isPositiveDigit(this.value, 'Down payment must be a number that is zero or larger!')) downPayment = this.value;}\" value=\"" + downPayment + "\" id=\"downPayment\" size=\"6\"/>";
  if (isDownPaymentPercent)
  {
    $('dp_cell').innerHTML += "<input type=\"checkbox\" onblur=\"isDownPaymentPercent = this.checked\" id=\"isPercent\" checked=\"true\" />%";
  }
  else
  {
    $('dp_cell').innerHTML += "<input type=\"checkbox\" onblur=\"isDownPaymentPercent = this.checked\" id=\"isPercent\" />%";
  }
  
  $('fix_cell').innerHTML = "<input type=\"text\" onblur=\"if (isPositiveDigit(this.value, 'Fix up costs must be a number that is zero or larger!')) fixUp = this.value;\" value=\"" + fixUp + "\" id=\"fixUpCosts\" />";
}

function getSavedSearchName()
{
  if (!infoDialogOpen)
  {
    infoDialogOpen = true;
    Dialog.info($('saved_search_name').innerHTML, {className: "alphacube", width:220});
  }
}

function updateSearchForm()
{
  var selectedIndex = document.getElementById("selected_saved_search").selectedIndex;
  var value = document.getElementById("selected_saved_search").options[selectedIndex].value;
      
  if (value == "")
  {
    document.getElementById("search_form").reset();
    document.getElementById("search_name").value = "";
    document.getElementById("save_search_link").onclick = new Function("getSavedSearchName()");
  }
  else 
  {
    // reset the search fields
    if (document.getElementById("start_date_month") != null)
      document.getElementById("start_date_month").selectedIndex = 0;
      
    if (document.getElementById("start_date_day") != null)
      document.getElementById("start_date_day").selectedIndex = 0;
      
    if (document.getElementById("start_date_year") != null)
      document.getElementById("start_date_year").selectedIndex = 0;
      
    if (document.getElementById("end_date_month") != null)
      document.getElementById("end_date_month").selectedIndex = 0;
      
    if (document.getElementById("end_date_day") != null)
      document.getElementById("end_date_day").selectedIndex = 0;
    
    if (document.getElementById("end_date_year") != null)
      document.getElementById("end_date_year").selectedIndex = 0;
      
    if (document.getElementById("property_type") != null)
      document.getElementById("property_type").selectedIndex = 0;
      
    if (document.getElementById("zip_code") != null)
      document.getElementById("zip_code").value = "";
      
    if (document.getElementById("max_price") != null)
      document.getElementById("max_price").selectedIndex = 0;
      
    if (document.getElementById("percent_county") != null)
      document.getElementById("percent_county").selectedIndex = 0;
    
    parseSavedSearchTerms(value);
    document.getElementById("save_search_link").onclick = new Function("confirmUpdateSavedSearch()");
    document.getElementById("search_name").value = document.getElementById("selected_saved_search").options[selectedIndex].text;
  }
}

function parseSavedSearchTerms(terms)
{
  var keyValArray = terms.split("|");
  
  for (var i = 0; i < keyValArray.length; i++)
  {
    var keyVal = keyValArray[i].split("=");
    setSearchFormField(keyVal[0], keyVal[1]);
  }
}

function saveSearch(controller)
{
  if (document.getElementById("search_name").value == "")
  {
    closeFetchDataDialog(); 
    
    if (!infoDialogOpen)
    {
      infoDialogOpen = true;
      Dialog.info($('missing_search_name').innerHTML, {className: "alphacube", width:240});
    }    
  }
  else
  {
    closeFetchDataDialog(); 
    showWorkingSearchNextPage();
    new Ajax.Updater('propertyListBody', '/' + controller + '/save_search', {asynchronous:true, evalScripts:true, onComplete:function(request){closeFetchDataDialog()}, parameters:Form.serialize($('search_form'))}); return false;
  }
}

function updateSavedSearch(controller)
{
  closeFetchDataDialog(); 
  showWorkingSearchNextPage();
  
  var selectedIndex = document.getElementById("selected_saved_search").selectedIndex;
  var terms = document.getElementById("selected_saved_search").options[selectedIndex].value;  
  var keyValArray = terms.split("|");
  var id;
  
  for (var i = 0; i < keyValArray.length; i++)
  {
    var keyVal = keyValArray[i].split("=");
    
    if (keyVal[0] == "id")
    {
      id = keyVal[1];
      break;
    }
  }
  
  new Ajax.Updater('propertyListBody', '/' + controller + '/update_saved_search/' + id, {asynchronous:true, evalScripts:true, parameters:Form.serialize($('search_form'))}); return false;
}

function confirmUpdateSavedSearch()
{
  var selectedIndex = document.getElementById("selected_saved_search").selectedIndex;
  var value = document.getElementById("selected_saved_search").options[selectedIndex].text;
  
  document.getElementById("confirm_search_name").innerHTML = value;
  
  if (!infoDialogOpen)
  {
    infoDialogOpen = true;
    Dialog.info($('confirm_update').innerHTML, {className: "alphacube", width:250});
  }  
}

function setSearchFormField(key, val)
{
  if (key == "start_date")
  {
    var dateArray = val.split("-");
    var year  = parseInt(dateArray[0], 10);
    var month = parseInt(dateArray[1], 10);
    var day  = parseInt(dateArray[2], 10);

    setSelectionSearchBox("start_date_month", month);
    setSelectionSearchBox("start_date_day", day);
    setSelectionSearchBox("start_date_year", year);
  }
  else if (key == "end_date")
  {
    var dateArray = val.split("-");
    var year  = parseInt(dateArray[0], 10);
    var month = parseInt(dateArray[1], 10);
    var day  = parseInt(dateArray[2], 10);  
    
    setSelectionSearchBox("end_date_month", month);
    setSelectionSearchBox("end_date_day", day);
    setSelectionSearchBox("end_date_year", year);
  }
  else if (key == "property_type")
  {
    setSelectionSearchBox("property_type", val);
  }
  else if (key == "zip_code")
  {
    document.getElementById("zip_code").value = val;
  }
  else if (key == "area")
  {
    //setSelectionSearchBox("area", val);
  }
  else if (key == "max_price")
  {
    setSelectionSearchBox("max_price", val);
  }
  else if (key == "percent_county")
  {
    setSelectionSearchBox("percent_county", val);
  }
}

function setSelectionSearchBox(objStr, val)
{
  var obj = document.getElementById(objStr);
  
  if (obj != null)
  {
    for (var i = 0; i < obj.options.length; i++)
    {
      if (obj.options[i].value == val)
      {
        obj.selectedIndex = i;
        break;
      }
    }
  }
}

function setMembershipType(type)
{
  document.getElementById("status").value = type;
}

function researchSearchComplete()
{
  closeFetchDataDialog();
  var resultsList = document.getElementById("results_list");
  var error = document.getElementById("error");
  
  if (resultsList != null)
  {
    // open the dialog
    var t=setTimeout("Dialog.alert($('results_list').innerHTML, {className:'alphacube', width:600, okLabel: 'Cancel', onOk:function(win){Dialog.closeInfo(); return false;}})", 700);
  }
  else if (error != null)
  {
    var t=setTimeout("Dialog.alert($('error').innerHTML, {className:'alphacube', width:600, okLabel: 'OK', onOk:function(win){Dialog.closeInfo(); return false;}})", 700);
  }
}

function validateAddUserForm()
{
  var isValid = false;
  
  var emailError = "Please enter a value for your email address!";  
  var passMatchError = "Password and password confirmation must match!";
  var firstNameError = "Please enter your first name!";
  var lastNameError = "Please enter your last name!";
  var ccNumberError = "Please enter a valid credit card number!";
  
  if ((document.getElementById("user_password") != null) && (document.getElementById("user_password_confirmation") != null))
  // Adding user for the first time
  {
    if (document.getElementById("first_name_row").style.display == 'none')
    {
      if (validateEmail("user_email_address", emailError) &&
          validateMatch("user_password", "user_password_confirmation", passMatchError))      
      {
        isValid = true;
      }    
    }
    else
    {
      if (validateEmail("user_email_address", emailError) &&
          validateMatch("user_password", "user_password_confirmation", passMatchError) &&
          validateExists("firstname", firstNameError) &&
          validateExists("lastname", lastNameError) && 
          validateExists("cc_number_1", ccNumberError) && validateIsDigit("cc_number_1", ccNumberError) && validateLength("cc_number_1", 4, ccNumberError) &&
          validateExists("cc_number_2", ccNumberError) && validateIsDigit("cc_number_2", ccNumberError) && validateLength("cc_number_2", 4, ccNumberError) &&
          validateExists("cc_number_3", ccNumberError) && validateIsDigit("cc_number_3", ccNumberError) && validateLength("cc_number_3", 4, ccNumberError) &&
          validateExists("cc_number_4", ccNumberError) && validateIsDigit("cc_number_4", ccNumberError) && validateLength("cc_number_4", 4, ccNumberError))
      {
        isValid = true;
      }    
    }
  }
  else
  // upgrading existing user
  {
    if (validateEmail("user_email_address", emailError) &&
        validateExists("firstname", firstNameError) &&
        validateExists("lastname", lastNameError) && 
        validateExists("cc_number_1", ccNumberError) && validateIsDigit("cc_number_1", ccNumberError) && validateLength("cc_number_1", 4, ccNumberError) &&
        validateExists("cc_number_2", ccNumberError) && validateIsDigit("cc_number_2", ccNumberError) && validateLength("cc_number_2", 4, ccNumberError) &&
        validateExists("cc_number_3", ccNumberError) && validateIsDigit("cc_number_3", ccNumberError) && validateLength("cc_number_3", 4, ccNumberError) &&
        validateExists("cc_number_4", ccNumberError) && validateIsDigit("cc_number_4", ccNumberError) && validateLength("cc_number_4", 4, ccNumberError))
    {
      isValid = true;
    }
  }
  
  return isValid;
}

function validateResetPasswordForm()
{
  var isValid = false;
  
  var emailError = "Please enter a value for your email address!";  
  
  if (validateExists("user_email_address", emailError))      
  {
    isValid = true;
  }
  
  return isValid;
}

function validateEditPersonalForm()
{
  var isValid = false;

  var fnameError = "Please enter a value for your first name!";
  var lnameError = "Please enter a value for your last name!";  
  var addressError = "Please enter a value for your address!";
  var cityError = "Please enter a value for your city!";
  var stateError = "Please enter a value for your state!";    
  var zipError = "Please enter a valid value for your zip!";
  var telephoneError = "Please enter a valid value for your telephone!";  
    
  if (validateExists("user_first_name", fnameError) &&
      validateExists("user_last_name", lnameError) && validateExists("user_address_1", addressError) &&
      validateExists("user_city", cityError) && validateExists("user_state", stateError) &&
      validateIsDigit("user_zip", zipError) && validateLength("user_zip", 5, zipError) &&
      validateIsDigit("user_area_code", telephoneError) && validateIsDigit("user_prefix", telephoneError) && 
      validateIsDigit("user_number", telephoneError) && validateLength("user_area_code", 3, telephoneError) &&
      validateLength("user_prefix", 3, telephoneError) && validateLength("user_number", 4, telephoneError))
  {
    isValid = true;
  }
  
  return isValid;
}

function validateEditAccountForm()
{
  var isValid = false;

  var emailError = "Please enter a value for your email address!";  
  var oldPassError = "Please enter your current password!";
  var passMatchError = "Password and password confirmation must match!";
    
  if (validateExists("user_email_address", emailError) &&
      validateExists("old_password", oldPassError))
  {
    isValid = true;
    
    var pass = document.getElementById("user_password");
    var conf = document.getElementById("user_password_confirmation");
    
    if (((pass != null) && (pass.value.replace(" ", "").length > 0)) || ((conf != null) && (conf.value.replace(" ", "").length > 0)))
    {
      isValid = false;
      
      if (validateMatch("user_password", "user_password_confirmation", passMatchError))
      {
        isValid = true;
      }
    }
  }
  
  return isValid;
}

function validateCashFlowDefaultsForm()
{
  var isValid = false;
  
  if (validateIsDigit('user_rental_assumptions_vacancy_rate', "Please enter a numerical value for 'Vacancy Rate (%)'") &&
      validateIsDigit('user_rental_assumptions_insurance', "Please enter a numerical value for 'Insurance (Yearly)'") &&
      validateIsDigit('user_rental_assumptions_taxes', "Please enter a numerical value for 'Taxes (Yearly)'") &&
      validateIsDigit('user_rental_assumptions_mgt_percent', "Please enter a numerical value for 'Managment (Monthly Percentage)'") &&
      validateIsDigit('user_rental_assumptions_interest_rate', "Please enter a numerical value for 'Interest Rate'") &&
      validateIsDigit('user_rental_assumptions_years', "Please enter a numerical value for 'Years'") &&
      validateIsDigit('user_rental_assumptions_down_payment', "Please enter a numerical value for 'Down Payment'"))
  {
    isValid = true;
  }
  else
  {
    return isValid;
  }
  
  for (var key in otherArray)
  {
    if ($(key) != null)
    {
      var val = otherArray[key];
      var errorMessage = "Please enter a numerical value for '" + $(key).value + "'";
    
      if (isNaN(val))
      {
        Dialog.alert(getErrorHtml(defaultTitle, errorMessage), {className:'alphacube', width:300, okLabel: 'OK', onOk:function(win){Dialog.closeInfo(); return false;}});
        isValid = false;
        break;
      }
    }
  }  
  
  if (isValid)
  {
    for (var key in fixUpArray)
    {
      if ($(key) != null)
      {
        var val = fixUpArray[key];
        var errorMessage = "Please enter a numerical value for '" + $(key).value + "'";
     
        if (isNaN(val))
        {
          Dialog.alert(getErrorHtml(defaultTitle, errorMessage), {className:'alphacube', width:300, okLabel: 'OK', onOk:function(win){Dialog.closeInfo(); return false;}});
          isValid = false;
          break;      
        }
      }
    }  
  }
  
  return isValid;
}

function showAgentInfoText(name, number, company, address, email)
{
  var infoDiv = document.getElementById("importantInfo");
  var textDiv = document.getElementById("importantInfoText");
  
  if (infoDiv && textDiv)
  {
    var html = "Below is the contact information for the listing agent.<br/><br/>";
    if (name)
    {
      html += name + "<br/>"
    }
    
    if (number)
    {
      html += number + "<br/>";
    }
    
    if (company)
    {
      html += company + "<br/>"
    }
    
    if (address)
    {
      html += address + "<br/>"
    }
    
    if (email)
    {
      html += email + "<br/>"
    }
            
    html += "<br/><br/>Tell them you saw it on<br/> Flip-Start.com!"
    
    textDiv.innerHTML = html;
    infoDiv.style.display = "block";
  }
}

function showInvestorInfoText(name, number)
{
  var infoDiv = document.getElementById("importantInfo");
  var textDiv = document.getElementById("importantInfoText");
  
  if (infoDiv && textDiv)
  {
    var html = "Below is the contact information for the owner/investor.<br/><br/>";
    html += name + ": " + number + "<br/><br/>";
    html += "Tell them you saw it on<br/> Flip-Start.com!"
    
    textDiv.innerHTML = html;
    infoDiv.style.display = "block";
  }
}

function showImportantInfoText(name, number)
{
  var infoDiv = document.getElementById("importantInfo");
  var textDiv = document.getElementById("importantInfoText");
  
  if (infoDiv && textDiv)
  {
    var html = "Below is the contact information for the bank's attorney.<br/><br/>";
    html += name + ": " + number;
    
    textDiv.innerHTML = html;
    infoDiv.style.display = "block";
  }
}

function showHelp(page)
{
  Dialog.alert({url: page, options: {method: 'get'}}, {className:'alphacube', height:500, width:600, okLabel: 'OK', onOk:function(win){Dialog.closeInfo(); return false;}});
}

function showNotice(title, message, width, height)
{
  closeFetchDataDialog();
  Dialog.alert(getErrorHtml(title, message), {className:'alphacube', height:height, width:width, okLabel: 'OK', onOk:function(win){Dialog.closeInfo(); return false;}});
}

function updateImage(numImages, id, url)
{
  // update image source
  // had to do innerHTML because FF and IE will not update
  // imgObj.src if it was built with ajax
  document.getElementById("propertyImageSpan").innerHTML = "<img src='" + url + "' id='propertyImage' style='width:100%'/>";
  
  for (var i = 1; i <= numImages; i++)
  {
    var imgId = "imageNum" + i;
    var imgElement = document.getElementById(imgId);
    
    if (imgElement != null)
    {
      if (i != id)
      {
        // set everyone text decoration
        imgElement.style.textDecoration = "";
      }
      else
      {
        // set my text decoration
        imgElement.style.textDecoration = "none";
      }
    }
  }
}

function getErrorHtml(title, message)
{
  var html = "<div id=\"notice\">";
  html += "<table width=\"92%\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\" class=\"right_txt1\">";
  html += "<tr>";
  html += "<td colspan=\"3\" align=\"right\"><span class=\"persinfo_txt float_left\">" + title + "</span></td>";
  html += "</tr>";
  html += "<tr class=\"seperator_horz\">";
  html += "<td></td>";
  html += "<td></td>";
  html += "<td></td>";
  html += "</tr>";
  html += "<tr>";
  html += "<td><br/>" + message + "</td>";
  html += "<td>&nbsp;</td>";
  html += "<td>&nbsp;</td>";
  html += "</tr>";
  html += "</table>";
  html += "</div>";
  
  return html;
}

function validateEmail(elementId, errorMessage)
{
  if (!validateExists(elementId, errorMessage))
  {
    return false;
  }
  
  var val = document.getElementById(elementId).value;
  document.getElementById(elementId).value = val.replace(/ /g, "");

  if (document.getElementById(elementId).value.indexOf("@") == -1)
  {
    Dialog.alert(getErrorHtml(defaultTitle, errorMessage), {className:'alphacube', width:300, okLabel: 'OK', onOk:function(win){Dialog.closeInfo(); return false;}});
    return false;
  }
  
  if (document.getElementById(elementId).value.indexOf(".") == -1)
  {
    Dialog.alert(getErrorHtml(defaultTitle, errorMessage), {className:'alphacube', width:300, okLabel: 'OK', onOk:function(win){Dialog.closeInfo(); return false;}});
    return false;
  }
    
  return true;
}

function validateExists(elementId, errorMessage)
{
  var isValid = true;
  var element = document.getElementById(elementId);
  
  if ((element == null) || (element.value == null) || (element.value.replace(" ", "").length == 0))
  {
    Dialog.alert(getErrorHtml(defaultTitle, errorMessage), {className:'alphacube', width:300, okLabel: 'OK', onOk:function(win){Dialog.closeInfo(); return false;}});
    isValid = false;
  }
  
  return isValid;
}

function validateIsDigit(elementId, errorMessage)
{
  var isValid = true;
  var element = document.getElementById(elementId);
  
  if ((element == null) || (element.value == null) || (isNaN(element.value)))
  {
    Dialog.alert(getErrorHtml(defaultTitle, errorMessage), {className:'alphacube', width:300, okLabel: 'OK', onOk:function(win){Dialog.closeInfo(); return false;}});
    isValid = false;
  }  
  
  return isValid;
}

function validateLength(elementId, reqLength, errorMessage)
{
  var isValid = true;
  var element = document.getElementById(elementId);
  
  if ((element == null) || (element.value == null) || (element.value.replace(" ", "").length != reqLength))
  {
    Dialog.alert(getErrorHtml(defaultTitle, errorMessage), {className:'alphacube', width:300, okLabel: 'OK', onOk:function(win){Dialog.closeInfo(); return false;}});
    isValid = false;
  }  
  
  return isValid;
}

function validateMatch(elementOne, elementTwo, errorMessage)
{
  var isValid = true;
  var first = document.getElementById(elementOne);
  var second = document.getElementById(elementTwo);  
  
  if ((first == null) || (first.value == null) || (first.value.replace(" ", "").length == 0) || (second == null) || (second.value == null) || (second.value.replace(" ", "").length == 0) || (first.value != second.value))
  {
    Dialog.alert(getErrorHtml(defaultTitle, errorMessage), {className:'alphacube', width:300, okLabel: 'OK', onOk:function(win){Dialog.closeInfo(); return false;}});
    isValid = false;
  }
  
  return isValid;
}

function isPositiveDigit(num, message)
{
  if ((isNaN(num)) || (num < 0))
  {
    alert(message);
    return false;
  }
            
  return true;            
}

function isPositivePercent(num, message)
{
  if ((!isPositiveDigit(num, message)) || (num > 100))
  {
    alert(message);
    return false;
  }
  
  return true;
}

function getHeaderDate()
{
  var retVal = "";
  var today = new Date();
  var dayOfWeek = today.getDay();
  var month = today.getMonth();
  var dayOfMonth = today.getDate();
  var year = today.getFullYear();
  
  switch (dayOfWeek)
  {
    case 0:
      retVal += "Sunday";
      break;
    case 1:
      retVal += "Monday";
      break;
    case 2:
      retVal += "Tuesday";
      break;
    case 3:
      retVal += "Wednesday";
      break;
    case 4:
      retVal += "Thursday";
      break;
    case 5:
      retVal += "Friday";
      break;                              
    case 6:
      retVal += "Saturday";
      break;      
  }
  
  switch (month)
  {
    case 0:
      retVal += ", January ";
      break;
    case 1:
      retVal += ", February ";
      break;
    case 2:
      retVal += ", March ";
      break;
    case 3:
      retVal += ", April ";
      break;
    case 4:
      retVal += ", May ";
      break;
    case 5:
      retVal += ", June ";
      break;                              
    case 6:
      retVal += ", July ";
      break;
    case 7:
      retVal += ", August ";
      break;
    case 8:
      retVal += ", September ";
      break;
    case 9:
      retVal += ", October ";
      break;
    case 10:
      retVal += ", November ";
      break;                              
    case 11:
      retVal += ", December ";
      break;            
  }
  
  retVal += dayOfMonth + ", " + year;
    
  return retVal;
}