Prototype.Browser.IE6 = Prototype.Browser.IE && parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE") + 5)) == 6;
Prototype.Browser.IE7 = Prototype.Browser.IE && parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE") + 5)) == 7;
Prototype.Browser.IE8 = Prototype.Browser.IE && !Prototype.Browser.IE6 && !Prototype.Browser.IE7;

Ajax.CustomAutocompleter = Class.create(Ajax.Autocompleter, {
  initialize: function($super, element, update, url, options) {
    $super(element, update, url, options)
    this.update.hasFocus = false;

    Event.observe(this.update, 'mouseover', this.onDivFocus.bindAsEventListener(this));

    Event.observe(this.update, 'mouseout', this.onDivBlur.bindAsEventListener(this));
  },

  onHover: function(event) {
    return false;
  },

  onBlur: function(event) {
    if (this.update.hasFocus == false) {
      setTimeout(this.hide.bind(this), 250);
      this.hasFocus = false;
      this.active = false;
    }
  },

  onDivFocus: function(event) {
    this.update.hasFocus = true;
  },

  onDivBlur: function(event) {
    this.update.hasFocus = false;
  },

  onKeyPress: function(event) {
    if (this.active)
      switch (event.keyCode) {
        case Event.KEY_TAB:
          this.selectEntry();
          Event.stop(event);
        case Event.KEY_ESC:
          this.hide();
          this.active = false;
          Event.stop(event);
          return;
        case Event.KEY_LEFT:
        case Event.KEY_RIGHT:
          return;
        case Event.KEY_UP:
          this.markPrevious();
          this.render();
          Event.stop(event);
          return;
        case Event.KEY_DOWN:
          this.markNext();
          this.render();
          Event.stop(event);
          return;
      }
    else
      if (event.keyCode == Event.KEY_TAB || event.keyCode == Event.KEY_RETURN ||
          (Prototype.Browser.WebKit > 0 && event.keyCode == 0)) return;

    this.changed = true;
    this.hasFocus = true;

    if (this.observer) clearTimeout(this.observer);
    this.observer =
    setTimeout(this.onObserverEvent.bind(this), this.options.frequency * 1000);
  },

  show: function() {
    if (Element.getStyle(this.update, 'display') == 'none') {
      this.update.style.display = 'block';
    }
  }

})

function handleEnter(event) {
  var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
  if (keyCode == 13)
    return false;
  else
    return true;
}

function toggleResults(linkId, contentId, textShow, textHide) {
  if ($(contentId).style.display == 'none') {
    showResults(linkId, textHide, contentId);
  }
  else {
    hideResults(linkId, textShow, contentId);
  }
}

function showResults(linkId, linkText, contentId) {
  $(linkId).innerHTML = linkText;
  $(linkId).addClassName('active');
  $(contentId).style.display = '';
  $(contentId).addClassName('result-content');
}

function hideResults(linkId, linkText, contentId) {
  $(linkId).innerHTML = linkText;
  $(linkId).removeClassName('active');
  $(contentId).style.display = 'none';
  $(contentId).removeClassName('result-content');
}

function cleanText(elementId, originalText) {
  $(elementId).addClassName('write');
  if ($(elementId).value == originalText) {
    $(elementId).value = '';
  }
}

function charsLeft(maxLength, inputElementId, charsLeftText, msgId, tooLongText) {
  document.observe('dom:loaded',
  function() {
    $(inputElementId).observe('keyup',
    function() {
      remaining = maxLength - $(inputElementId).value.length;
      if (remaining < 0) {
        $(msgId).update(tooLongText);
        $(msgId).setStyle('color:red;');
        $(inputElementId).setStyle('color:red;');
      } else {
        $(msgId).update('' + remaining + charsLeftText);
        $(msgId).setStyle('color:#668E0E;');
        $(inputElementId).setStyle('color:#666666;');
      }
    });
  });
}

function validateEmail(email) 
{ 
  var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/ 
  return email.match(re);
}

function validateAndIdentifyEmail(email, userType, partial, path, formAuthenticityToken)
{
  if (validateEmail(email) != null) {
    new Ajax.Updater(partial, path, {asynchronous:true, evalScripts:true, parameters:'email=' + email + '&user_type=' + userType + '&authenticity_token=' + encodeURIComponent(formAuthenticityToken)});
  }
}
