Event.observe(window, "load", function(){
  
  for (var i = 0; i < document.forms.length; i++) {
    if (document.forms[i].className.match("validate")) {
      Event.observe(document.forms[i], "submit", validateForm);
    }
  }
  
  var county = document.getElementsByName("megye")[0];
  if (county) {
    new asyncRequest("/ajax/locations", function(xhr){
      var locations = JSON.parse(xhr.responseText);
      if (locations.counties && locations.cities) {
        var value = "";
        if (county.value) value = county.value;
        fillSelect(county, locations.counties);
        county.value = value;
        var cache = document.createElement("input");
        cache.type = "hidden";
        cache.name = county.form.name + "DependencyCache";
        cache.value = encodeURIComponent(JSON.stringify(locations.cities));
        county.form.appendChild(cache);
        DependencyManager.init(county.form);
      }
    });
  }
  
  
  for (var i = 0; i < document.links.length; i++) {
    unmaskEmail(document.links[i]);
    if (document.links[i].className.match("popup")) {
      Event.observe(document.links[i], "click", function(_href){return function(e){
        var el = Event.delegate(e);
        var href = _href;
        Event.cancel(e);
        var popup = new Popup();
        popup.open({
          "uri":href
          ,"name":"rl"+parseInt(Math.random()*1000)
          ,"top":0
          ,"left":Math.round((window.screen.width - 960) / 2)
          ,"width":960
          ,"height":window.screen.height
          ,"features":"resizable=yes,scrollbars=yes,menubar=yes,toolbar=yes,status=yes,directories=yes,location=yes"
        });
      }}(document.links[i].href));
    }
    if (document.links[i].rel == "tree") {
      var href = document.links[i].href;
      var id = document.links[i].id;
      var div = document.createElement("div");
      document.links[i].id = "";
      div.id = id;
      document.links[i].parentNode.insertBefore(div, document.links[i]);
      new clsTreeBox(id, href);
    }
  }
  
});
