var rtxt = new Array('Select country','Select region','Select area','Select location','Cancel selection');
var src = '';
var engine = 'bcm';
$(document).ready(init);

function init()
   {
    var x = document.getElementById("r0");
    if (x != null)
       {
        if (x.value == 0)
           {
            x.options[0].text = rtxt[0];
            target_off("select#r1");
            target_off("select#r2");
            target_off("select#r3");
           }
        else
           {
            x.options[0].text = rtxt[4];
            var x = document.getElementById("r1");
            if (x.value == 0)
               {
                x.options[0].text = rtxt[1];
                target_off("select#r2");
                target_off("select#r3");
               }
            else
               {
                x.options[0].text = rtxt[4];
                var x = document.getElementById("r2");
                if (x.value == 0)
                   {
                    x.options[0].text = rtxt[2];
                    target_off("select#r3");
                   }
                else
                   {
                    x.options[0].text = rtxt[4];
                    var x = document.getElementById("r3");
                    if (x.value == 0) x.options[0].text = rtxt[3];
                    else x.options[0].text = rtxt[4];
                   }
               }
           }
       }
   }

function addBookmark(url, title)
 {
  if (!url) url = location.href;
  if (!title) title = document.title;
  //Gecko
  if ((typeof window.sidebar == "object") && (typeof window.sidebar.addPanel == "function")) window.sidebar.addPanel (title, url, "");
  //IE4+
  else if (typeof window.external == "object") window.external.AddFavorite(url, title);
  //Opera7+
  else if (window.opera && document.createElement)
   {
    var a = document.createElement('A');
    if (!a) return false; //IF Opera 6
    a.setAttribute('rel','sidebar');
    a.setAttribute('href',url);
    a.setAttribute('title',title);
    a.click();
   }
  else alert('Ваш браузер не поддерживает автоматическое добавление закладок.');
  return true;
 }
function target_off(target)
 {
  $(target).empty();
  $(target).attr("disabled","disabled");
 }
function target_on(target, options)
 {
  $(target).html(options);
  $(target).removeAttr("disabled");
 }
$(function(){$("select#r0").change(function()
 {
  if($(this).val()==0)
   {
    target_off("select#r1");
    target_off("select#r2");
    target_off("select#r3");
   }
  else
   {
    $.getJSON('get_region.php',{q: $(this).val(), lang: "en"}, function(j)
     {
      var options = '';
      for(var i = 0; i < j.length; i++) options += '<option value="' + j[i][0] + '">' + j[i][1] + '</option>';
      target_on("select#r1", options);
      target_off("select#r2");
      target_off("select#r3");
      var x = document.getElementById("r1");
      x.options[0].text = rtxt[1];
     })
   }
  var x = document.getElementById("r0");
  if(x.selectedIndex == 0) x.options[0].text = rtxt[0];
  else x.options[0].text = rtxt[4];
 })});
$(function(){$("select#r1").change(function()
 {
  if($(this).val()==0)
   {
    target_off("select#r2");
    target_off("select#r3");
   }
  else
   {
    $.getJSON('get_region.php',{q: $(this).val(), lang: "en"}, function(j)
     {
      var options = '';
      for(var i = 0; i < j.length; i++) options += '<option value="' + j[i][0] + '">' + j[i][1] + '</option>';
      target_on("select#r2", options);
      target_off("select#r3");
      var x = document.getElementById("r2");
      x.options[0].text = rtxt[2];
     })
   }
  var x = document.getElementById("r1");
  if(x.selectedIndex == 0) x.options[0].text = rtxt[1];
  else x.options[0].text = rtxt[4];
 })});
$(function(){$("select#r2").change(function()
 {
  if($(this).val()==0) target_off("select#r3");
  else
   {
    $.getJSON('get_region.php',{q: $(this).val(), lang: "en"}, function(j)
     {
      var options = '';
      for(var i = 0; i < j.length; i++) options += '<option value="' + j[i][0] + '">' + j[i][1] + '</option>';
      target_on("select#r3", options);
      var x = document.getElementById("r3");
      x.options[0].text = rtxt[3];
     })
   }
  var x = document.getElementById("r2");
  if(x.selectedIndex == 0) x.options[0].text = rtxt[2];
  else x.options[0].text = rtxt[4];
 })});
$(function(){$("select#r3").change(function()
 {
  var x = document.getElementById("r3");
  if(x.selectedIndex == 0) x.options[0].text = rtxt[3];
  else x.options[0].text = rtxt[4];
 })});

function bcm()
 {
  $("#bcm").addClass("active");
  $("#goo").removeClass("active");
  $("#ya").removeClass("active");
  $("#bing").removeClass("active");
  engine = 'bcm';
 }
function ya()
 {
  $("#ya").addClass("active");
  $("#bcm").removeClass("active");
  $("#goo").removeClass("active");
  $("#bing").removeClass("active");
  engine = 'ya';
 }
function goo()
 {
  $("#goo").addClass("active");
  $("#bcm").removeClass("active");
  $("#ya").removeClass("active");
  $("#bing").removeClass("active");
  engine = 'goo';
 }
function bing()
 {
  $("#bing").addClass("active");
  $("#bcm").removeClass("active");
  $("#goo").removeClass("active");
  $("#ya").removeClass("active");
  engine = 'bing';
 }
function gosrch()
  {
   src = '';
   if ($("#q").val())
      {
       if      (engine == 'ya')    src = 'http://www.yandex.ru/yandsearch?text=' + $("#q").val();
       else if (engine == 'goo')   src = 'http://www.google.ru/search?hl=ru&q=' + $("#q").val();
       else if (engine == 'bing') src = 'http://www.bing.com/search?q=' + encodeURIComponent($("#q").val());
       if (src) window.open(src);
       else if (engine == 'bcm') document.srchform.submit();
      }
  }

