Ext.MessageBox.buttonText.cancel = "Fortryd"

function hide(element_name) {
  Ext.fly(element_name).setVisibilityMode(Ext.Element.DISPLAY);
  Ext.fly(element_name).hide();
}

function show(element_name) {
  Ext.fly(element_name).removeClass('hidden');
  Ext.fly(element_name).show();
}

function submit_form(direction) {
  Ext.get('direction').set({value:direction});
  this.document.forms[0].submit();
}

function update_select_box(name, json) {
  var options = Ext.decode(json);
  html = '<select id="house_' + name + '" name="house[' + name + ']">';
  for (var i = 0; i < options.length; i++) {
    html += '<option value="' + options[i][1] + '">' + options[i][0] + '</option>';
  }
  html += '</select>';
  var field = Ext.get('house_' + name);
  field.remove();
  var field_outer = Ext.get(name + '-wrapper');
  field_outer.insertHtml('beforeEnd', html);
}

function update_field_label(field, label) {
  Ext.fly(Ext.query("label[for='house_" + field + "']")[0]).update(label);
}

function ajax_request(url, success, params) {
  Ext.Ajax.request({
    url: url,
    success: success,
    method: 'GET',
    params: params
  });
}

function add_help(field, title, text, width) {
  var width = width || 250;

  var target = "house_" + field;

  var div = Ext.get(target).insertSibling({
    tag: "div",
    cls: "help-wrapper"
  }, "after");

  div.insertFirst({
    tag: "img",
    src: "/images/question.png",
    cls: "help",
    id: "help_" + field
  }, "after");

  new Ext.ToolTip({
    target: "help_" + field,
    title: title,
    id: "help_tooltip_" + field,
    width:width,
    dismissDelay: 0,
    html: text.replace(/\n/g, "<br>"),
    trackMouse:true,
    autoHide: true
  });
}

Ext.onReady(function() {
  var err = Ext.get('errorExplanation');
  if (err) {
    var err_window;
    Ext.get('errorExplanation').on("click", function() {
      err_window.hide()
    })
    if (err) {
      err.show();
      err_window = new Ext.Window({
        autoHeight: true,
        width:450,
        contentEl:'errorExplanation',
        plain: true,
        border:false,
        bodyBorder:false,
        resizable:false,
        footer:false,
        //draggable:false,
        header: false,
        //modal:true,
        closable:false
      }).show();
    }
  }
});

function heating_source_name(source) {
  var types = [
    ["braendefyr","brændefyr"],
    ["elvarme","elektricitet"],
    ["fjernvarme", "fjernvarme"],
    ["halm", "halm"],
    ["jordvarme", "jordvarme"],
    ["naturgas", "naturgas"],
    ["olie", "olie"],
    ["traepillefyr", "træpillefyr"],
    ["varmepumpe_jord_luft","varmepumpe luft-vand"],
    ["andet", "andet"]
  ];

  for (var i = 0; i < types.length; i++) {
    if (types[i][0] == source) {
      return types[i][1];
    }
  }
}

function valid_email(email) {
  var regExpr = /^[-a-z0-9~!$%^&*_=+}{\'?]+(\.[-a-z0-9~!$%^&*_=+}{\'?]+)*@([a-z0-9_][-a-z0-9_]*(\.[-a-z0-9_]+)*\.(aero|arpa|biz|com|coop|edu|gov|info|int|mil|museum|name|net|org|pro|travel|mobi|[a-z][a-z])|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(:[0-9]{1,5})?$/i
  return regExpr.test(email);  
}

// 
if (!window.console) {
    window.console = {
        log: function () {},
        group: function () {},
        error: function () {},
        warn: function () {},
        groupEnd: function () {}
    };
}

