﻿/// <reference path="../jquery-1.4.1-vsdoc.js" />
/// <reference path="../GeneralFunctions.js" />
/// <reference path="jquery.selectboxes.min.js" />
/// <reference path="jquery.scrollTo-min.js" />
var DefaultSearchSetup = false;
var NewMapSearchSetup = false;
var FreeTextSearchSetup = false;
var AdvSearchSetup = false;
var _defaultSearchType = 4;


//sets up the search page
function SetupSearchPage() {
    zipCodesList = ZipCodeLoadAll();
    regionsList = RegionsLoadAll();
    kommunerList = KommuneLoadAll();
    retskredseList = RetskredseLoadAll();
    categoryList = CategoryLoadAll();
    $(document).ready(function() {
        auctionproxy.invoke("LoadSearchCriterias", {}, function(result) {
            var searchCriterias = result;
/*
            if (searchCriterias != null) {
                ShowSearch(result.SearchType);
            }
            else {
                ShowSearch(0);
            }*/
            ShowSearch(0);
        });
    });
}
//Shows the relevant search
function ShowSearch(type) {
    //Set all visible
    $('#LiAvanceret').show();
    //Hide all divs
    $('#divDefaultSearch').hide();
    $('#divNewMapSearch').hide();
    $('#divFreeTextSearch').hide();
    $('#divAdvancedSearch').hide();

    if (0 == type) {
        type = $.cookie("MapSearchType");
        if (null == type) {
            type = _defaultSearchType;
        }
    }

    // If not logged in and last was Advanced => New Map
    if (!IsLoggedIn()) {
        if (3 == type) {
            type = _defaultSearchType;
        }
    }

    // If logged in and is not paying and last was Advanced => New Map
    if (IsLoggedIn() && !(IsLawyer() || IsRekvirent() || IsPayingUser())) {
        if (3 == type) {
            type = _defaultSearchType;
        }
    }

    if (type == 1) {
        $.cookie("MapSearchType", "1", { expires: 90, path: '/' });
        // Setup default search if it has not been loaded
        if (!DefaultSearchSetup) {
            SetupDefaultSearch();
            DefaultSearchSetup = true;
        }
        $('#divDefaultSearch').show();
    }
    else if (4 == type) {
    $.cookie("MapSearchType", "4", { expires: 90, path: '/' });
        // Setup new map search if it has not been loaded
        if (!NewMapSearchSetup) {
            SetupNewMapSearch();
            NewMapSearchSetup = true;
        }
        $('#divNewMapSearch').show();

    }
    else if (type == 2) {
    $.cookie("MapSearchType", "2", { expires: 90, path: '/' });
        // Setup freetext search if it has not been loaded
        if (!FreeTextSearchSetup) {
            SetupFreeTextSearch();
            FreeTextSearchSetup = true;
        }
        $('#divFreeTextSearch').show();

    }
    else if (type = 3) {
    $.cookie("MapSearchType", "3", { expires: 90, path: '/' });
        // Setup advanced search if it has not been loaded
        if (!AdvSearchSetup) {
            SetupAdvancedSearch();
            AdvSearchSetup = true;
        }
        $('#divAdvancedSearch').show();
    }

    // If not logged in hide advanced button
    if (!IsLoggedIn()) {
        $('#divAdvancedSearch').hide();
        $('#divSpacer').show();
        $('#LiAvanceret').hide();
    }

    // If logged in and not paying hide advanced button
    if (IsLoggedIn() && !(IsLawyer() || IsRekvirent() || IsPayingUser())) {
        $('#divAdvancedSearch').hide();
        $('#divSpacer').show();
        $('#LiAvanceret').hide();
    }
}


  //*****************************************************************************Free text search************************************************************************************/
 

  //*****************************************************************************Various************************************************************************************/
 
    //Resets the form
  function ResetForm() {
      document.forms[0].reset();
      $('#selAdvSelectedKommuner').removeOption(/./);
      var inputElements = document.getElementsByTagName("input");
      for (var i = 0; i < inputElements.length; i++) {
          if (inputElements[i].type == 'text') {
              inputElements[i].value = '';
          }
      }


  }
  function PerformSearch(url) {
      auctionproxy.invoke("Search", {}, function(result) {
         Goto(url);
      });
  }
  function SearchResultCount() {
      var records = 0;
      //perform the actual search
      auctionproxySync.invoke("SearchResultCount", {},
      function(result) {
          records = result;
      }
      );
      return records;
  }
  function ShowSearchResultCountOnButton(btn) {
      //perform the actual search
      auctionproxy.invoke("SearchResultCount", {},
      function(result) {
      $(btn).val("SØG [" + result + "]");
      }
      );
  }


  /*************************************************************************Search result***************************************************************************************/
  
 //AddsRetskredse
