function clearDefault(el) {
  if (el.defaultValue==el.value) el.value = ""
}

function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else var expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

function eraseCookie(name) {
  createCookie(name,"",-1);
}

/**
 * Helper function to help display additional information (within a hidden element) when a user clicks on an element
 * Example: jQuery('.open-handler').click(openHandler);
 */
var addOpenClickHandler = function (element)
{
  var handle = jQuery(element);


  handle.show();
  handle.live('click', function ()
  {
    var contentBlock = jQuery('#' + jQuery(this).attr('id') + '-content');

    contentBlock.slideToggle();
    return false;
  });
};

jQuery().ready(function()
{
  jQuery('.tooltip[title]').tooltip({showURL: false, fade: 50});

  jQuery.ajaxSetup({
    error: function(XMLHttpRequest, textStatus, errorThrown)
    {
      switch (XMLHttpRequest.status)
      {
        case 301:
        case 302:
          if (XMLHttpRequest.getResponseHeader('redirected-location'))
          {
            jQuery(location).attr('href', XMLHttpRequest.getResponseHeader('redirected-location'));
          }
          else
          {
            alert('An error happended. Page redirect with no redirect URL');
          }

          break;

        case 401:
          alert('Your session has expired, you will be redirected to the login page');

          jQuery(location).attr('href', jQuery(location).attr('href'));
          break;

        case 404:
        case 500:
          alert('An error happened, please try again later');
          break;

        default:
          alert('Unknown error happened');
          break;
      }
    }
  });

  jQuery.fn.clickDelegate = function (eventDelegate)
  {
    jQuery(this).click(function (event)
    {
      currentElement = jQuery(event.target);
      thisElement = jQuery(this);

      do
      {
        if (currentElement.attr('id') == thisElement.attr('id'))
        {
          return;
        }

        if (eventDelegate(currentElement))
        {
          break;
        }
      }
      while (currentElement = currentElement.parent());

      event.preventDefault();
      event.stopPropagation();
    });
  }


  jQuery.fn.ajaxDialog = function ()
  {
    jQuery(this).click(function(event)
    {
      var url = this.href;
      var title = this.title;
      var dialogDiv = jQuery('<div style="display:hidden;"><p style="padding-top: 20%">Loading.... Please wait</p></div>').appendTo('body');
      dialogDiv.dialog(
      {
        modal: true,
        width: 660,
        resizable: false,
        title: title,
        dialogClass: 'alert',
        draggable: false,
        position: ['center','top'],
        minHeight: 400,
        zIndex: 20000,
        open: function(event, ui)
        {
          jQuery.ajax(
          {
            type: "GET",
            url: url,
            success: function(msg)
            {
              dialogDiv.html(msg);
            }
          });
        },
        close: function() {
          dialogDiv.remove();
        }
      });
      event.preventDefault();
    });
  }

});
