function trim(a)
{
  return a.replace(/(^\s*)|(\s*$)/g, "")
}
function StartSearches(locale)
{
  if($('searches_remember_search').checked && trim($('searches_name').value) == '')
  {
    alert('Please enter a name for save.');
    return false;
  }
  var aa = $('#searches_remember_search:checked').val();
  var name = $('#searches_name').val();

  var rem = '0';
  if(aa != undefined)
  {
    rem  = '1';
  }
  if(name == undefined)
  {
    name = '';
  }
  document.cookie = "remember_search=" + rem + ";";
  document.cookie = "searches_name=" + name + ";";
  window.location.href = createSearchURL(locale);
  return true;
}
function createSearchURL(locale)
{
  var purpose = getPurpose();
  var district = $('#searches_district').val();
  //jacky add 2011.3.15
  var other_destinations = $('#searches_other_destinations').val();
  if($('#divotherdestinations')[0].style.display=='none'){
     other_destinations = '';	  
  }
  //
  var property_type = $('#searches_property_type').val();
  var bedrooms = getRoomNum();
  var sale_from_price = $('#searches_sale_from_price').val();
  var sale_to_price = $('#searches_sale_to_price').val();
  var rent_from_price = $('#searches_rent_from_price').val();
  var rent_to_price = $('#searches_rent_to_price').val();
  var square_from = $('#searches_square_from').val();
  var square_to = $('#searches_square_to').val();
  var keywords = $('#searches_keywords').val();
  var field = $('#field').val();
  var order = $('#order').val();
  //jacky add 2011.10.7
  if(sale_to_price<sale_from_price) sale_to_price=sale_from_price;
  if(rent_to_price<rent_from_price) rent_to_price=rent_from_price;
  if(square_to<square_from) square_to=square_from;
  //
  if(keywords == '樓盤名稱 或 關鍵字' || keywords == '楼盘名称 或 关键字' || keywords == 'Property name Or Keyword' || keywords == '關鍵字' || keywords == '关键字' || keywords == 'keyword')
  {
	keywords='';  
  }	
  var url = "/"+ locale + "/property-search/";
  url += district + "/";
  url += property_type + "/";
  url += purpose + "/?";
  if(trim(keywords) != '')
  {
    url += 'key=' + keywords + "&";
  }
  url += 'salefp=' + sale_from_price + "&";
  url += 'saletp=' + sale_to_price + "&";
  url += 'rentfp=' + rent_from_price + "&";
  url += 'renttp=' + rent_to_price + "&";
  url += 'squaref=' + square_from + "&";
  url += 'squaret=' + square_to + "&";
  url += 'other_destinations=' + other_destinations + "&";//jacky add 2011.3.21
  if(bedrooms != '0')
  {
    url += 'room=' + bedrooms + "&";
  }
  url += 'field=' + field + "&";
  url += 'order=' + order + "&";
  url += 'page=1';

  return url.toLowerCase();

}
function selectPurpose(purpose)
{
  switch (purpose)
  {
    case 'RENT-SALE':
      //$('#searches_purpose_RENT-SALE').attr('checked',true);
      $('#divsaleprice,#divleaseprice').show();
      break;
    case 'RENT':
      //$('#searches_purpose_RENT').attr('checked',true);
      $('#divsaleprice').hide();
      $('#divleaseprice').show();
      break;
    case 'SALE':
      //$('#searches_purpose_SALE').attr('checked',true);
      $('#divsaleprice').show();
      $('#divleaseprice').hide();
      break;
  }
}
function propertyTypeChanged(propertyType)
{
  if(propertyType.toLowerCase() == "condo" || propertyType.toLowerCase() == "villa")
  {
    $('#divbedrooms').show();
  }
  else
  {
    $('#divbedrooms').hide();
  };
}
//jacky add 2011.3.15
function districtChanged(district)
{
  if(district.toLowerCase() == "other-destinations")
  {
    $('#divotherdestinations').show();
  }
  else
  {
    $('#divotherdestinations').hide();
  };
}
//
function getPurpose()
{
  var purpose = $('#searches_purpose').val();
  /*var searches_purpose_ALL =  $('#searches_purpose_RENT-SALE:checked').val();
  var searches_purpose_RENT = $('#searches_purpose_RENT:checked').val();
  var searches_purpose_SALE = $('#searches_purpose_SALE:checked').val();

  if(searches_purpose_RENT)
  {
    purpose = searches_purpose_RENT;
  }
  else if(searches_purpose_SALE)
  {
    purpose = searches_purpose_SALE;
  }
  else
  {
    purpose = searches_purpose_ALL;
  }*/
  return purpose.toLowerCase();
}
function getRoomNum()
{
  var roomnum = "";
  for(i=1;i<=4;i++)
  {
    if ($('#searches_bedrooms_'+i).attr('checked'))
    {
      roomnum += i;
    }
  }
  return roomnum;
}
function expandListItem(obj,id)
{
  if(obj.className == 'expandicon')
  {
    obj.className = 'collapseicon';
    $('#' +id).hide();
  }
  else
  {
    obj.className = 'expandicon';
    $('#' +id).show();
  }
}

var currentselecttab = 'tablistview';
var currentselectcon = 'divlistview';
function selectTab(id,conid,url,tab)
{
  if(id == currentselecttab){
    return;
  }
  $('#' + currentselecttab).attr('className','tab_new_off');
  $('#' + currentselectcon).hide();
  currentselecttab = id;
  currentselectcon = conid;
  $('#' + id).attr('className','tab_new_on');
  $('#' + conid).show();
  if(url != undefined && trim(url) != '')
  {
    getCompareView(conid,url,tab);
  }
}

var currentIDS = '';
var currentMapIDS = '';
function getCompareView(updateId,url,tab)
{
  var ids = getIds(0);
  if(tab == 'map'){
    ids = getIds(1);
    if(currentMapIDS == ids){
      return;
    }else{
      currentMapIDS = ids;
    }
    }
  if(tab == 'compare'){
    if(ids == '')
	  {
		alert('Please selected properties for compare first.');
		selectTab('tablistview','divlistview','');
		return;
	  }
    if(currentIDS == ids){
      return;
    }else{
      currentIDS = ids;
    }
    }

  var html = "<img src='/images/ui/loading.gif'>";
  $("#" + updateId).html(html);

  url += '?ids=' + ids;
  $.ajax({
    url: url,
    cache: false,
    success: function(data) {
      $("#" + updateId).html(data);
    }
  });
}
function getIds(n)
{
  var ids = '';
  if(n==1){
  $('#divlistview > div').find("input[@type='checkbox']").each(function () {
    ids += this.value + '-';
  });
  }else{
    $('#divlistview > div').find('input[checked]').each(function () {
    ids += this.value + '-';
  });
  }
  return ids.substr(0, ids.length - 1);
}
