function replaceHtml(el, html) {
var oldEl = (typeof el === "string" ? document.getElementById(el) : el);
/*@cc_on // Pure innerHTML is slightly faster in IE
oldEl.innerHTML = html;
return oldEl;
@*/
var newEl = oldEl.cloneNode(false);
newEl.innerHTML = html;
oldEl.parentNode.replaceChild(newEl, oldEl);
/* Since we just removed the old element from the DOM, return a reference
to the new element, which can be used to restore variable references. */
return newEl;
};


Event.observe(window, 'load', function() {
//  fillGlossaire();
});

function setFlashMessage(type, message)
{
  Element.show('flash_notice');
  Element.setOpacity('flash_notice', 1);
  setTimeout('Element.update($$(\'#flash_notice p\')[0], \''+addslashes(message)+'\')', 5);
  Effect.Fade($$('#flash_notice p')[0], { from: 0, to: 1 });
}

/**
 * Update flash box from json request
 */
function setFlashMessageFromJSON(json)
{
  setFlashMessage(json[0], json[1]);
}

function updateFlashBox(data)
{
  Element.update('flash_notice', '<div class="'+data[0]+'">'+data[1]+'</div>');
}

/**
 * Show loading message while running ajax request
 */
function loadingFlashMessage()
{
	$('flash_notice').show();
  Element.update('flash_notice', '<div class="top">&nbsp;</div><div class="content"><p><span class="waiting_message">Veuillez patienter...</span><span class="waiting_logo"><img src="/images/ajax.gif" alt="loading"/></span></p></div>');
}

/**
 * Show loading message while running ajax request
 */
function clearFlashMessage()
{
  Element.update('flash_notice', '<div class="top">&nbsp;</div><div class="content"><p>&nbsp;</p></div>');
	$('flash_notice').hide();
}

/**
 * Animation on flash message
 */
Event.observe(window, 'load', function() {
  // Important fields
  if ($('flash_error'))
  {
    Effect.Fade($$('#flash_error p')[0], { from: 0.9, to: 0.2, duration: .6 });
    setTimeout('Effect.Fade($$(\'#flash_error p\')[0], {from: 0.3, to: 1, duration: .5})', 650);
  }
  else if ($('flash_notice'))
  {
    Effect.Fade($$('#flash_notice p')[0], { from: 0.9, to: 0.2, duration: .6 });
    setTimeout('Effect.Fade($$(\'#flash_notice p\')[0], {from: 0.3, to: 1, duration: .5})', 650);
  }
});

/**
 * Auto focus on first field
 */
Event.observe(window, 'load', function() {

  var fields = $$('input.image');

  if (fields.length > 0)
  {
    for (var i = 0; i < fields.length; i++)
    {
      fields[i].onclick = function(e) {
        if (!this.disabled)
        {
          new Effect.Opacity(this, {duration:0.8, from:1, to:0.45});

          this.disabled = true;
          result = (this.form.onsubmit ? (this.form.onsubmit() ? this.form.submit() : false) : this.form.submit());
          if (result == false)
          {
            this.disabled = false
          }

          return result;
        }
      };
    }
  }

  // Important fields
  var fields = $$('input.focus', 'textarea.focus', 'select.focus');
  if (fields.length > 0)
  {
      Form.Element.focus(fields[0]).select();
      return;
  }

  // Important forms
  var forms = new Array('login', 'register', 'address', 'password');
  for (var i = 0; i < forms.length; i++)
  {
    var name = 'form_'+forms[i];
    if ($(name))
    {
      new Form.focusFirstElement(name);
      return;
    }
  }
});


/**
 * Clear autocomplete fields
 */
Event.observe(window, 'load', function() {
  // Important fields
  var fields = $$('input[autocomplete=off]');
  if (fields.length > 0)
  {
      for (var i = 0; i < fields.length; i++)
      {
        if (fields[i].id.indexOf('autocomplete') < 0)
        {
          fields[i].value = '';
        }
      }
  }
});

function addslashes(str)
{
  str = str.replace(/\'/g,'\\\'');
  return str;
}

function stripslashes(str)
{
  str = str.replace(/\\'/g,'\'');
  str = str.replace(/\\"/g,'"');
  str = str.replace(/\\\\/g,'\\');
  str = str.replace(/\\0/g,'\0');
  return str;
}

function getElementX(object) {return getElementC(object, true)}
function getElementY(object) {return getElementC(object, false)}

function getElementC(element, xAxis)
{
	var initialElement = element;
	var c = 0;

	while (element != null)
	{
		c += (xAxis) ? element.offsetLeft : element.offsetTop;
		if (element.style.position == "absolute")
			break;
		else
			element = element.offsetParent;
	}

	var elementWnd = document.window;
	if (!elementWnd) return c;

	if (!elementWnd.frameElement) return c;

	return c + getElementC(elementWnd.frameElement, xAxis);
}

function trim(string)
{
	return string.replace(/(^\s*)|(\s*$)/g,'');
}

