var current_request = '';
var current_action = '';
var current_property_id = 0;
var do_search = false;
var do_reload = false;



function updateToolbarShortlistStatus()
{
   var obj = document.getElementById('toolbar_shortlist_status_div');
   var this_button = current_shortlist_name + ' (' + current_shortlist_count + ')';
   var html = '<a href="/shortlist/" class="toolbar_button">'+this_button+'</a>';
   obj.innerHTML = html;
}


function hideShortlistOption()
{
   document.getElementById('shortlist_option_div').style.display = 'none';   
}

 
function setShortlistOption(action)
{
   var html = '';
   switch(action)
   {
      case 'save':
      html = '<table border="0" cellspacing="0" cellpadding="0" width="400" class="main"><tr><td width="150">Shortlist Name:</td><td width="150"><input type="text" maxlength="25" name="so_shortlist_name" id="so_shortlist_name" class="form_input"></td><td width="100" align="right"><a style="float:right;" class="rrg_button" href="#" onclick="this.blur(); saveShortlist(); return false;"><span>Save</span></a></td></tr></table>';
      break;
      
      case 'add_specific':
      html = '<table border="0" cellspacing="0" cellpadding="0" width="400" class="main"><tr><td width="150">Property Ref No.:</td><td width="150"><input type="text" name="so_property_ref_no" id="so_property_ref_no" class="form_input"></td><td width="100" align="right"><a style="float:right;" class="rrg_button" href="#" onclick="this.blur(); propertyRefShorlistAdd(); return false;"><span>Go</span></a></td></tr></table>';
      break;
      
      case 'open':
      html = open_html;
      break;
   } 
   html = html + '<br>';
   document.getElementById('shortlist_option_div').style.display = '';
   document.getElementById('shortlist_option_div').innerHTML = html; 
}


function openShortlist()
{
   var to_open = document.getElementById('so_property_shortlist_id').value;
   window.location.href="/open_shortlist.php?property_shortlist_id="+to_open;  
}


function saveShortlist()
{
   var shortlist_name = document.getElementById('so_shortlist_name').value;
   if(shortlist_name == '')
   {
      alert("Please enter a name for this shortlist");
   }  
   else
   {
      current_action = 'save';
      current_shortlist_name = shortlist_name;
      var url = '/ajax/shortlist_action.ajax.php?action=save&shortlist_name='+shortlist_name+'&shortlist_session='+server_shortlist_session+'&uid='+server_uid+'&login_type='+server_login_type;
      ajaxRequest(url, true, 'shortlistActionResponse');    
   }
   hideShortlistOption();
}


var shortlist_action_complete = 0;
function shortlistAction(action,property_id)
{
   shortlist_action_complete = 0;
   if(server_do_refresh_on_shortlist_action == '1')
   {
   do_search = true;
   }

   if(action == 'add_specific' || action == 'save' || action == 'open')
   {
      setShortlistOption(action); 
      return;
   }
   else if(action == 'delete')
   {
      do_reload = true; 
      do_search = false;  
   }
   current_action = action;
   current_property_id = property_id;
   var url = '/ajax/shortlist_action.ajax.php?action='+action+'&property_id='+property_id+'&shortlist_session='+server_shortlist_session;
   ajaxRequest(url, true, 'shortlistActionResponse');
}


function shortlistActionResponse()
{
   if (xmlHttp.readyState==4)
   {
      rc = xmlHttp.responseText;
      
      
      // ONLY ERROR HERE IS NAME ALREADY EXISTS ON SAVE
      if(current_action == 'save' && rc == '0')
      {
         alert("You already have a shortlist by this name. Please enter a different name for this shortlist.");
         return;
      }
      
      
      // USE RESPONSE CODE TO TRACK SHORTLIST COUNT 
      if(current_action == 'add' && rc == '1') current_shortlist_count++;
      else if(current_action == 'remove' && rc == '1') current_shortlist_count--; 
      else if(current_action == 'clear' && rc == '1') current_shortlist_count = 0;
      
      

      // IF ADD, REMOVE OR SAVE, UPDATE THE SHORTLIST STATUS IN THE TOOLBAR
      if(current_action == 'add' ||
         current_action == 'remove' ||
         current_action == 'save' ||
         current_action == 'clear')
      {
         updateToolbarShortlistStatus();
         // IF ACTION IS ONE OF ADD OR REMOVE
         // AND WHATEVER WAS ATTEMPTED WAS SUCCESSFUL, 
         // ATTEMPT TO UPDATE THE SHORTLIST UTILITY IMAGE
         if((current_action == 'add' || current_action == 'remove') && rc == '1')
         {
            var shortlist_img = document.getElementById('shortlist_img_'+current_property_id);
            var shortlist_href = document.getElementById('shortlist_href_'+current_property_id);
            if(shortlist_img && shortlist_href)
            {
               shortlist_img.src = (current_action == 'add') ? '/images/shortlist_remove_s.png' : '/images/shortlist_add_s.png';
               shortlist_img.title = (current_action == 'add') ? 'Remove this property from shortlist' : 'Add this property to shortlist';
               shortlist_href.href = (current_action == 'add') ? 'javascript:shortlistAction(\'remove\','+current_property_id+');' : 'javascript:shortlistAction(\'add\','+current_property_id+');';
            }  
         } 
         
         if(current_action == 'save')
         {
            // Refresh the page because of the page title
            window.location.href = window.location.href;
         } 
      }
      
      // IF DO SEARCH IS SET TO TRUE, REFRESH THE SHORTLIST
      if(do_search == true)
      {
         doSearch();
         do_search = false;     
      }
      
      // DELETING A SHORTLIST FORCES A RELOAD TO THE EMPTY SHORTLIST
      if(do_reload == true)
      {
         window.location.href = '/shortlist/-1/';  
      }
      
      shortlist_action_complete = 1;
   }
}



function propertyRefShorlistAdd()
{
   var property_id = document.getElementById("so_property_ref_no").value;
   current_request = property_id;
   do_search = true;
   var url = '/ajax/property_ref_search.ajax.php?toolbar_ref_no='+property_id;
   ajaxRequest(url, true, 'propertyRefShorlistAddResponse');
   hideShortlistOption();  
}


function propertyRefShorlistAddResponse()
{
   if (xmlHttp.readyState==4)
   {
      rc = xmlHttp.responseText;
      if(rc == "1")
      {
         alert("Please enter a property reference number");  
      }
      else if(rc == "2")
      {
         alert("Property reference number must be numeric");  
      }
      else if(rc == "3")
      {
         alert("Couldn't find a property with reference number " + current_request + ". Please enter a different reference number or click 'Holiday Rentals' to see all properties.");  
      }
      else if(rc == "4")
      {
         alert("We found a property with this reference number but it cannot be viewed because it is currently not 'Active'. Please check back in a couple of hours."); 
      } 
      else
      {
         shortlistAction('add',current_request);
      }
   }
}



function toolbarPropertyRefNoSearch(obj)
{
   var toolbar_ref_no = obj.value;
   current_request = toolbar_ref_no;
   var url = '/ajax/property_ref_search.ajax.php?toolbar_ref_no='+toolbar_ref_no;
   ajaxRequest(url, true, 'propertyRefNoSearchResponse');
}


function propertyRefNoSearchResponse()
{
   if (xmlHttp.readyState==4)
   {
      rc = xmlHttp.responseText;
      if(rc == "1")
      {
         alert("Please enter a property reference number");  
      }
      else if(rc == "2")
      {
         alert("Property reference number must be numeric");  
      }
      else if(rc == "3")
      {
         alert("Couldn't find a property with reference number " + current_request + ". Please enter a different reference number or click 'Holiday Rentals' to see all properties.");  
      }
      else if(rc == "4")
      {
         alert("We found a property with this reference number but it cannot be viewed because it is currently not 'Active'. Please check back in a couple of hours."); 
      } 
      else
      {
         window.location.href = rc;
         _gaq.push(['_trackPageview','/search/propertyReferenceNumber/'+current_request]); 
      }
   }
}


function inputStatusFocus(obj, val, type, default_value)
{
   if(obj.value == default_value)
   {
      obj.type = type;
      obj.value = val;   
   }
}

function inputStatusBlur(obj, val, type)
{
   if(obj.value == '')
   {
      obj.type = type;
      obj.value = val;   
   }
}

function inputStatusFocusPassword(text_obj, pass_obj)
{
   var pass_div = document.getElementById("pass_div");
   pass_div.innerHTML = "<input type=\"password\" style=\"border:1px #BDBDBD solid;font-size:10px;color:#CCCCCC;height:18px;width:100px;\" name=\"toolbar_password\" id=\"toolbar_password\" class=\"form_input\" value=\"\" onBlur=\"inputStatusBlurPassword('toolbar_passtext','toolbar_password');\">";
   setTimeout("document.getElementById('"+pass_obj+"').focus();",100);
}

function inputStatusBlurPassword(text_obj, pass_obj)
{
   if(document.getElementById(pass_obj).value == "")
   {
      var pass_div = document.getElementById('pass_div');
      pass_div.innerHTML = "<input type=\"text\" style=\"border:1px #BDBDBD solid;font-size:10px;color:#CCCCCC;height:18px;width:100px;\" name=\"toolbar_passtext\" id=\"toolbar_passtext\" class=\"form_input\" value=\"Password\" onFocus=\"inputStatusFocusPassword('toolbar_passtext','toolbar_password');\">";
   }
}
