function whiteSpace(text) {
  return text.replace(/\//g, ' / ').replace(/-/g, ' - ');
}

function escapeHTML(text) {
  return text.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
}

function escapeJS(text) {
  return escapeHTML(text).replace(/"/g,"\\x22").replace(/'/g,"\\x27");
}

//call this function from the onsubmit for any forms with elements with titles
//can not be automatically because sometimes the onsubmit returns false, in which case
//this should not run, but there is no way to check that reliably
function inputTitlesForms(form) {
  $(":input[title][title!='']", form).each(function () {
    if(this.empty) $(this).val('');
  });
}

function inputTitles() {
  //add onsubmit to parent form that clears any empties
  $(":input[title][title!='']").each(function () {
    $(this)
      .bind("focus.hover", function() {
        $(this)
          .css("cursor", "")
          .removeClass("input_hover")
        ;
      })
      .bind("blur.hover", function() {
        $(this)
          .css("cursor", "pointer")
          .addClass("input_hover")
        ;
      })
      .trigger("blur")
    ;
    $(this).filter()
  });
  $(":input[title][title!=''][value='']").each(function () {
    var id = Math.random().toString().substr(2);
    $(this)
      .bind("focus.title", function () {
        $(this)
          .css("color", "")
          .css("text-align", "")
          .val("")
        ;
      })
      .bind("blur.title", function () {
        $(this)
          .css("color", "gray")
          .css("text-align", "center")
          .val(this.title)
        ;
      })
      .bind("keydown.title", function () {
        $(this)
          .unbind("keydown.title")
          .unbind("blur.title")
          .unbind("focus.title")
          .get(0).empty = false
        ;
        $(window).unbind("unload."+id)
      })
      .trigger("blur")
      .get(0).empty = true
    ;
    $(window).bind("unload."+id, this, function(el) {
        $(el.data).val('');
    });
  });
}

function bigTarget() {
  doBigTarget('#buttons td', 'a', 'td', 'target_hover');
  doBigTarget('#buttons td', 'input', 'td', 'target_hover');
  doBigTarget('.cbox_large_blue, .cbox_large_green, .cbox_med_blue, .cbox_med_green, .cbox_smed_blue, .cbox_smed_green, .cbox_small_blue, .cbox_small_green', 'a', 'td', 'cbox_hover');
  doBigTarget('.cbox_large_blue, .cbox_large_green, .cbox_med_blue, .cbox_med_green, .cbox_smed_blue, .cbox_smed_green, .cbox_small_blue, .cbox_small_green', 'input', 'td', 'cbox_hover');
}

function doBigTarget(scope, tag, parent, addClass) {
  var tagName = tag;
  $(scope).find(tag).each(function () {
    var el = this;
    $(this).parent(parent).css('cursor', 'pointer').addClass(addClass).click(function (e) {
      if(e.target.tagName.toLowerCase() == tagName.toLowerCase()) {
        return;
      } else if(tagName.toLowerCase() == 'a') {
        if(!el.onclick || el.onclick()) window.location = el.href;
      } else { //probably an input element
        if(el.click) el.click();
      }
    });
  });
}

var titleLang = {};
var oldLang;
function changeLang(lang) {
  if(oldLang == lang) return;
  var date = new Date();
  date.setTime(date.getTime()+30*24*60*60*1000);
  $('body').removeClass('disp_lang_' + oldLang).addClass('disp_lang_' + lang); document.cookie='lang=' + lang + ';expires=' + date.toGMTString();
  document.title = titleLang[lang] ? titleLang[lang] : titleLang['en'];
  $('.lang_' +  lang + ':input, .lang_' +  lang + ' :input').removeAttr("disabled");
  $('.lang_' +  oldLang + ':input, .lang_' +  oldLang + ' :input').attr("disabled", "disabled");
  oldLang = lang;
}

var overbox;
function showOverlay(el, inner) {
  $('#overlay').show();
  if(!overbox) {
    overbox = el;
    $('#overlay').height($('#tail').offset().top - $('#overlay').offset().top);
    $(overbox).css('top', $('#overlay').offset().top + 'px');
    if(inner) $(inner).height(Math.max($('#overlay').outerHeight(true)-$(overbox).outerHeight(true)-4, 200));
    $(document.body).css('background-color', '#4c4c4c');
  }
  $(el).show();
  $(document).bind("keyup.overlay", function (e) {
    if(e.keyCode==27) {
      $('#body :text:first').focus();
      hideOverlay();
    }
  });
}

function hideOverlay() {
  $('#overlay').hide();
  $(overbox).hide();
  overbox = null;
  $(document).unbind("keyup.overlay");
  $(document.body).css('background-color', '');
}

function blink(id) {
  var toggle = false;
  $(id).data("blink", setInterval(function() {
    if(toggle) {
      $(id).animate({opacity: 0}, "slow");
      toggle = false;
    } else {
      $(id).animate({opacity: 1}, "slow");
      toggle = true;
    }
  }, 1000));
}

function blink_stop(id) {
  clearInterval($(id).data("blink"));
  $(id).animate({opacity: 1}, "slow");
}

function setSelection(el) {
  if (window.getSelection) {
    var selection = window.getSelection();
    if (selection.setBaseAndExtent) { /* for Safari */
      selection.setBaseAndExtent(el, 0, el, 1);
    } else { /* for FF, Opera */
      var range = document.createRange();
      range.selectNodeContents(el);
      selection.removeAllRanges();
      selection.addRange(range);
    }
  } else if(document.body.createTextRange) { /* for IE */
    var range = document.body.createTextRange();
    range.moveToElementText(el);
    range.select();
  }
}

