$(document).ready(function(){
    $("#search_state").change( function() {
        //$("#result").html('Retrieving ...');  
       if($("#search_state").val() > 0)
      {
        $("#search_location").html('<option>Loading...</option>');
        setCookie("rentalcomp_search_state",$(this).val(),31);
        $.ajax({
            type: "POST",
            data: "data="+$(this).val() ,
            url: base_url+"main/get_city",
            success: function(msg){    
                if (msg != ''){
                    var resp = msg.split("|");   
                    $("#search_location").html(resp[0]);
                    $("#rc_logo").attr("src",resp[1]);
                    $("#rc_tagline").html(resp[2]);
                }
                else
                {
                    $("#search_location").html('<option>No Available Location</option>');
                }
            }
        });
      }
      else
      {
         $("#search_location").html('<option>Select a State</option>');
      }
    });
    $("#search_location").change( function(){
        //$("#result").html('Retrieving ...');  
        var city = $("#search_location").val();
        var url = base_url+"main/set_city/"+city;
        setCookie("rentalcomp_search_location",city,31);   
        $.ajax({
            type: "POST",
            url: url,
            success: function(msg){
                if (msg != ''){
                    var resp = msg.split("|");
                    $("#rc_logo").attr("src",resp[0]);
                    $("#rc_tagline").html(resp[1]);
                }
            } 
        });
        
        //alert('location='+getCookie("rentalcomp_search_location")+',state='+getCookie("rentalcomp_search_state")+',countrycode='+getCookie("rentalcomp_countrycode"));
    });
    
    $('#search-button').click( function(event){
      if( $("#search_state").val() == 0 || $("#search_state").val() == "")
      {
         event.preventDefault();
      }
      if( $("#search_location").val() == 0 || $("#search_location").val() == "")
      {
         event.preventDefault();
      }
      if(($("#keyword").val()).length < 3 || $("#keyword").val() == 'Enter Search Keywords Here')
      {
         event.preventDefault();
         alert("Keyword is minimum of three characters. ");
      }
    });
    
    $('li.flags').click(function(e){
       e.preventDefault();
       var language = ($(this).attr("id")).split("-");
       $.ajax({
            type: "POST",
            url: base_url+"main/set_language/"+language[1],
            success: function(msg){
               window.location.reload(); 
            }
        });
    });
    
    if($("#search_location").val() == null)
    {
       $('#search_state').trigger('change');
    }
    
    $("a.target_blank").click(function(event){
      window.open($(this).attr("href"));
		event.preventDefault(); 
    });
    
    $("a.boldchat-button").click(function(event){
      window.open((window.pageViewer && pageViewer.link || function(link){return link;})(this.href + (this.href.indexOf('?')>=0 ? '&amp;' : '?') + 'url=' + escape(document.location.href)), 'Chat4375169711430905296', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=640,height=480');
      event.preventDefault(); 
    });
    
    $("a.screenshot").click(function(){
      popup(this, '','width=900,height=800,scrollbars=yes');
    });
    
    $("a.popup_900x800").click(function(){
      popup(this, '','width=900,height=800,scrollbars=yes');
    });
    
    $("a.popup_1024x800").click(function(){
      popup(this, '','width=1024,height=800,scrollbars=yes');
    });
    
    $("a.popupFavorite").click(function(){
      var id = ($(this).attr("id")).split("_");
      popupFavorite(id[1]);
    });
    $("a.popupLogin").click(function(){
      var id = ($(this).attr("id")).split("_");
      popupLogin(id[1]);
    });                         
    
    
    
   
    
         
});

function setCookie(c_name,value,exdays)
{
   var exdate=new Date();
   exdate.setDate(exdate.getDate() + exdays);
   var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
   document.cookie=c_name + "=" + c_value;
}

function getCookie(c_name)
{
var i,x,y,ARRcookies=document.cookie.split(";");
for (i=0;i<ARRcookies.length;i++)
{
  x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
  y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
  x=x.replace(/^\s+|\s+$/g,"");
  if (x==c_name)
    {
    return unescape(y);
    }
  }
}

