<!--//

/***** AJAX *****/

function getCityZips(id, type, city, state){
  jQuery('span#' + id).load('/ajax/getCityZips.php?type=' + type + '&city=' + city + '&state=' + state);
}

function getNearbyCities(id, type, city, state){
  jQuery('span#' + id).load('/ajax/getNearbyCities.php?type=' + type + '&city=' + city + '&state=' + state);
}

function getMapTooltip(id, listing){
  jQuery('div#' + id).load('/ajax/getMapTooltip.php?id=' + listing);
}

function getMapListing(id, listing){
  jQuery('div#' + id).load('/ajax/getMapListing.php?id=' + listing);
}

function buildSaveListingLink(id, listing, icon){
  jQuery('span#' + id).load('/ajax/buildSaveListingLink.php?id=' + listing + '&icon=' + icon);
}

function createAlert(id, object){
  jQuery('span#' + id).load('/ajax/createAlert.php?' + $(object).serialize());
}

function updatePictureDescription(filename, description){
  jQuery.post('/ajax/secure/updatePictureDescription.php', {'filename': filename, 'description': description}, function(data){ });
}

function saveListing(id){
  jQuery.post('/ajax/saveListing.php', {'id': id}, function(data){
    $('#save_' + id).html('<a href="/member/favorites/" id="icon_favorite"><strong>View Favorites</strong></a>');
  });
}

/***** Colorbox *****/

function closeColorbox(){
  //$().bind('cbox_closed', function(){ alert("closed"); });
  $.fn.colorbox.close();
}

function closeColorboxReloadPage(){
  $.fn.colorbox.close();
  location.reload(true);
}

/***** Google Maps *****/

var gmarkers = [];
var htmls = [];
function myclick(i){
  //gmarkers[i].openInfoWindowHtml(htmls[i]);
  gmarkers[i].openInfoWindowHtml('<div id="marker_' + i + '"></div>');
  getMapListing("marker_" + i, i);
}

/***** Search Alert *****/

function createAlertOpen(){
  document.getElementById('alertCreation').style.display = 'block';
  document.getElementById('alertToggle').innerHTML='<a href="javascript:createAlertClose();" style="color: #B84715; text-decoration: none;" id="icon_alert_email"><strong>Be the first to know when we receive rentals matching this search</strong></a>';
}

function createAlertClose() {
  document.getElementById('alertCreation').style.display = 'none';
  document.getElementById('alertToggle').innerHTML='<a href="javascript:createAlertOpen();" style="color: #B84715; text-decoration: none;" id="icon_alert_email"><strong>Be the first to know when we receive rentals matching this search</strong></a>';
}

/***** Forms *****/

function verifyQuery(form){
  if(form.query.value == ''){
    alert('Please enter a City, State or Zipcode.\nEx: Beverly Hills, CA or 90210');
    form.query.value='';
    form.query.focus();
    return false;
  } else {
    form.submit();
    return true;
  }
}

function displayLoginPrompt(){
  var agree = confirm("You must be logged in to save properties.\nClick OK to sign up for an account - it only takes a minute!");
  if(agree){
    window.location = '/register/';
  } else {
    return false;
  }
}

function copyAddress(form){  
  if(form.same.checked == true){
    form.billFirstName.value = form.fname.value;
    form.billLastName.value = form.lname.value;
    //form.billAdr.value = form.adr.value;
    //form.billCity.value = form.city.value;
    //form.billState.value = form.state.value;
    form.billZip.value = form.zip.value;
  } else {
    form.billFirstName.value = '';
    form.billLastName.value = '';
    //form.billAdr.value = '';
    //form.billCity.value = '';
    //form.billState.value = 'null';
    form.billZip.value = '';
  }
}

function disableButton(theButton){
 theButton.disabled = true;
 theButton.form.submit();
}

/***** Text Counter *****/

function getkey(e){
  if (window.event)
    return window.event.keyCode;
  else if (e)
    return e.which;
  else
    return null;
}

function goodchars(e, goods){
  var key, keychar;
  key = getkey(e);
  if (key == null) return true;
  keychar = String.fromCharCode(key);
  keychar = keychar.toLowerCase();
  goods = goods.toLowerCase();
  if (goods.indexOf(keychar) != -1)  return true;
  if ( key==null || key==0 || key==8 || key==9 || key==13 || key==27 )  return true;
  return false;
}

var isNN = (navigator.appName.indexOf("Netscape")!=-1);
function autoTab(input,len, e) {
  var keyCode = (isNN) ? e.which : e.keyCode;
  var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];
  if(input.value.length >= len && !containsElement(filter,keyCode)) {
    input.value = input.value.slice(0, len);
    input.form[(getIndex(input)+1) % input.form.length].focus();
  }
  function containsElement(arr, ele) {
    var found = false, index = 0;
    while(!found && index < arr.length)
      if(arr[index] == ele)
        found = true;
      else
        index++;
      return found;
  }  
  function getIndex(input) {
    var index = -1, i = 0, found = false;
    while (i < input.form.length && index == -1)
      if (input.form[i] == input)index = i;
      else i++;
     return index;
  }
  return true;
}

function textCounter(field, maxlimit) {
  if(field.value.length > maxlimit) {
    field.value = field.value.substring(0, maxlimit);
  }
}

function textCounterUpdate(fieldName, maxlimit) {
  field = document.getElementById(fieldName+'_txt');
  txt_field = document.getElementById(fieldName+'_char_count');
  txt_field.innerText = field.value.length;
  txt_field.innerHTML = field.value.length;
  textCounter(field, maxlimit);
}

function descWordCount(fieldName, maxlimit, init) {
  if(!init) {
    textCounterUpdate(fieldName, maxlimit);
  } else {
    document.write("<strong id=\""+fieldName+"_char_count\">0</strong>");
  }
}

/***** Flash *****/

function write_object(embedded_object){
  document.write(embedded_object);
  document.write('\n');
}

/***** Listing Slideshow *****/

function slideSetting(){
  slide = document.getElementById('pic');
  slide.src = slideImages[0];
  slide.setAttribute("width", width);
  slide.setAttribute("height", height);
}

function slideStart(){
  slide.src = slideImages[0];
  picture = 0;
  slide.setAttribute("width", slideImagesWidth[picture]);
  slide.setAttribute("height", slideImagesHeight[picture]);
  document.getElementById("picNum").innerHTML = picture + 1;
}

function slidePrev(){
  if(picture > 0 ){
    picture = picture - 1;
    slide.src = slideImages[picture];
    slide.setAttribute("width", slideImagesWidth[picture]);
    slide.setAttribute("height", slideImagesHeight[picture]);
    document.getElementById("picNum").innerHTML = picture+1;
  } else {
    picture = slideImages.length - 1;
    slide.src = slideImages[picture];
    slide.setAttribute("width", slideImagesWidth[picture]);
    slide.setAttribute("height", slideImagesHeight[picture]);
    document.getElementById("picNum").innerHTML = picture+1;
  }
}

function slideNext(){
  if(picture < slideImages.length-1){
    picture = picture + 1;
    slide.src = slideImages[picture];
    slide.setAttribute("width", slideImagesWidth[picture]);
    slide.setAttribute("height", slideImagesHeight[picture]);
    document.getElementById("picNum").innerHTML = picture+1;
  } else {
    picture=0;
    slide.src = slideImages[picture];
    slide.setAttribute("width", slideImagesWidth[picture]);
    slide.setAttribute("height", slideImagesHeight[picture]);
    document.getElementById("picNum").innerHTML = picture+1;
  }
}

function slideEnd(){
  slide.src = slideImages[slideImages.length-1];
  picture = slideImages.length - 1;
  slide.setAttribute("width", slideImagesWidth[picture]);
  slide.setAttribute("height", slideImagesHeight[picture]);
  document.getElementById("picNum").innerHTML = picture + 1;
}

//-->