function searchFormHtml()
{
  var theHTML = '<div class="left">';
  theHTML += '<div class="dropdownSec">';
  theHTML += '<div class="finderDropdown">';
  theHTML += '<div class="drowdownField">';
  theHTML += '<input type="text" name="search_region" id="search_region" class="searchTxtBox" value="" />';
  theHTML += '</div>';
  theHTML += '<img src="/images/btn-dropdown-arrow.png" alt="" id="btnRegionDropDown" class="btnDropDownImg"/>';
  theHTML += '<div class="clear"></div>';
  theHTML += '</div>';
  theHTML += '<div class="finderDropdown finderTown">';
  theHTML += '<div class="drowdownField">';
  theHTML += '<input type="text" name="search_town" id="search_town" class="searchTxtBox" value="" />';
  theHTML += '</div>';
  theHTML += '<img src="/images/btn-dropdown-arrow.png" alt="" id="btnTownDropDown" class="btnDropDownImg"/>';
  theHTML += '<div class="clear"></div>';
  theHTML += '</div>';
  theHTML += '<div class="finderDropdown finderStation">';
  theHTML += '<div class="drowdownField">';
  theHTML += '<input type="text" name="search_station" id="search_station" class="searchTxtBox" value="" />';
  theHTML += '</div>';
  theHTML += '<img src="/images/btn-dropdown-arrow.png" alt="" id="btnStationDropDown" class="btnDropDownImg"/>';
  theHTML += '<div class="clear"></div>';
  theHTML += '</div>';
  theHTML += '<div class="finderDropdown finderFrequncy">';
  theHTML += '<div class="drowdownField">';
  theHTML += '<input type="text" name="search_frequency" id="search_frequency" class="searchTxtBox" value="" />';
  theHTML += '</div>';
  theHTML += '<img src="/images/btn-dropdown-arrow.png" alt="" id="btnFrequencyDropDown" class="btnDropDownImg"/>';
  theHTML += '<div class="clear"></div>';
  theHTML += '</div>';
  theHTML += '</div>';
  theHTML += '<a class="btnFinderSubmit" href="javascript:findFrequency();"><img src="/images/btn-finder-submit.png" alt=""/></a>';
  theHTML += '</div>';
  return theHTML;
}



function renderResultHtml(region, town, stations, position, actionType, search)
{
  var LinkRegion = region.replace(" ","_");
  LinkRegion = LinkRegion.replace("'","+");
  var LinkTown = town.replace(" ","_");
  LinkTown = LinkTown.replace("'","+");
  
  var printUrl = "/print/region/"+LinkRegion+"/"+LinkTown;
  var html = '<div class="resultFor">';
  var backPosition = "right";
  var backImg = "btn-back2map.png";
  var alias = "";
  var aliasLinkReq = "";
  if(position == 'left')
  {
    backPosition = 'left';
    backImg = 'btn-newsearch.png';
    printUrl = "/print/"+actionType+"/"+stations[0].townId+"/"+search;
    if($("#searchAlias").val() != '')
    {
      alias = $("#searchAlias").val();
      aliasLinkReq = "?alias="+alias.replace(' ', '_').replace("'","+");;
    }

  }
  
  html += region+", "+town;
  html += '</div>';
  html += '<div class="tableArea"><img src="/images/tableheader-frequncy.png"/>';
  if(parseInt(stations.length)> 16)
  {
    html += '<div class="tableWrapper">';
  }
  else
  {
    html += '<div class="tableWrapper noScroll">';
  }
  html += '<table cellspacing="0" align="center"><tbody>';

  for(var i=0; i<stations.length; i++)
  {
    var item = stations[i];
    var evenStyle = '';
    
    if(i%2)
    {
      evenStyle = ' class="even"';
    }

    var theStationName = item.name;
    if(alias != '')
    {
      theStationName = alias;
    }
    html += "<tr "+evenStyle+">";
    html += '<td class="frqCol1">'+theStationName+'</td>';
    html += '<td class="frqCol2">'+item.freqOld+'</td>';
    html += '<td class="frqCol3">'+item.freqNew+'</td>';
    html += '<td class="frqCol4">'+item.dateChange+'</td>';
    html += "</tr>";
  }

  html += '</tbody></table></div>';
  html += '<div class="tableButtons">';
  html += '<a href="javascript:backToMap(\''+backPosition+'\');"><img src="/images/'+backImg+'"/></a>';
  html += '<a href="'+printUrl+'.html'+aliasLinkReq+'" target="_blank"><img src="/images/btn-print.png"/></a>';
  html += '<a href="'+printUrl+'.pdf'+aliasLinkReq+'" target="_blank"><img src="/images/btn-savepdf.png"/></a></div>';
  html += '</div>';;

  return html;
}


function backToMap(position)
{
  if(position == 'left')
  {
    //$("#leftResult").html('');
    //$("#contentMainLeft").css('background', "transparent url('/images/bg-finder-form.jpg') top left no-repeat");
    //$("#searchFormDiv").css('display', 'block');
    //clearSearchForm();
    window.location = "/";
  }
  else
  {
    //clearMapForm();
    //$("#resultDiv").html('');
    //loadTheMap();
    //$("#theMap").css('display', 'block');

    window.location = "/";
  }
  
}

function showRegionList(region, town)
{
  var LinkRegion = region.replace(" ","_");
  LinkRegion = LinkRegion.replace("'","+");
  var LinkTown = town.replace(" ","_");
  LinkTown = LinkTown.replace("'","+");
  var LINK = "/station/region/"+LinkRegion+"/"+LinkTown+".xml";
  clearMapForm();
  $("#resultDiv").html("Loading...");
  $.ajax({
    url: LINK,
    dataType: "xml",
    success: function(xmlResponse) {
      var stations = $("station", xmlResponse).map(function() {
        return {
          name: $("name", this).text(),
          freqOld: $("frequencyold", this).text(),
          freqNew: $("frequencynew", this).text(),
          dateChange: $("datechange", this).text(),
          region: $("region", this).text()
        };
      }).get();
      region = stations[0].region;
      var resultHtml = renderResultHtml(region, town, stations);
      $("#resultDiv").html(resultHtml);
    }
  });
}

function clearMapForm()
{
  $("#mapOverlay").css('display', 'none');
  $("#search_map_town").autocomplete("close");
  $("#search_map_town").val('');
  $("#searchMapRegionId").val('');
  return true;
}


function clearSearchForm()
{
  $("#search_region").val('');
  $("#search_town").val('');
  $("#search_station").val('');
  $("#search_frequency").val('');
  
  $("#searchTown").val('');
  $("#searchTownId").val('');
  $("#searchRegion").val('');
  $("#searchRegionId").val('');
  $("#searchStation").val('');
  $("#searchStationId").val('');
  $("#searchFrequencyOld").val('');
  $("#searchFrequencyNew").val('');
  $("#searchDateChange").val('');
  $("#searchActionType").val('');
  $("#searchAlias").val('');

  autoCompleteRegion();
}

function closeAutoComplete(thisAutoComplete)
{
  switch(thisAutoComplete)
  {
    case "region":
      $("#search_town").autocomplete("close");
      $("#search_station").autocomplete("close");
      $("#search_frequency").autocomplete("close");
      $("#search_map_town").autocomplete("close");
      break;

    case "town":
      $("#search_region").autocomplete("close");
      $("#search_station").autocomplete("close");
      $("#search_frequency").autocomplete("close");
      $("#search_map_town").autocomplete("close");
      break;

    case "townMap":
      $("#search_region").autocomplete("close");
      $("#search_station").autocomplete("close");
      $("#search_frequency").autocomplete("close");
      $("#search_town").autocomplete("close");
      break;

    case "station":
      $("#search_town").autocomplete("close");
      $("#search_region").autocomplete("close");
      $("#search_frequency").autocomplete("close");
      $("#search_map_town").autocomplete("close");
      break;

    case "frequency":
      $("#search_town").autocomplete("close");
      $("#search_station").autocomplete("close");
      $("#search_region").autocomplete("close");
      $("#search_map_town").autocomplete("close");
      break;
  }
}

function toggleAutoComplete(field)
{
  var searchField = '';
  var autoCompleteField = '';
  var msg = '';
  var check = true;
  
  switch(field)
  {
    case "region":
      searchField = '#search_region';
      autoCompleteField = '#searchRegionAutoComplete';
      break;

    case "town":
      searchField = '#search_town';
      autoCompleteField = '#searchTownAutoComplete';
      if($("#search_region").val() =='' || $("#searchRegionId").val() =='')
      {
        msg = "Please Select your Region first.";
        check = false;
      }
      break;

    case "station":
      searchField = '#search_station';
      autoCompleteField = '#searchStationAutoComplete';
      if($("#search_region").val() =='' || $("#searchRegionId").val() =='' || $("#search_town").val() =='' || $("#searchTownId").val() =='')
      {
        msg = "Please Select your Region and Town first.";
        check = false;
      }
      break;

    case "frequency":
      searchField = '#search_frequency';
      autoCompleteField = '#searchFrequencyAutoComplete';
      if($("#search_region").val() =='' || $("#searchRegionId").val() =='' || $("#search_town").val() =='' || $("#searchTownId").val() =='')
      {
        msg = "Please Select your Region and Town first.";
        check = false;
      }
      break;

    case "townMap":
      searchField = '#search_map_town';
      autoCompleteField = '#searchMapTownAutoComplete';
      break;
  }
  
  closeAutoComplete(field);
  if(!check)
  {
    alert(msg);
    return;
  }
  if($(autoCompleteField).val() == '0')
  {
    $(searchField).autocomplete("option", "minLength", 0);
    $(searchField).autocomplete("search", "");
    $(autoCompleteField).val('1');
  }
  else
  {
    $(autoCompleteField).val('0');
    $(searchField).autocomplete("close");
  }
}

function loadTheMap()
{
  var flashvars = {};
  var params = {wmode: "transparent"};
  swfobject.embedSWF('/swf/mapApplication_v001.swf', 'theMap', '450', '490', '9.0.45','/swf/expressinstall.swf', flashvars,params,false);
}
