var MONTH_NAMES_SHORT = ['JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC'];

function DisableSelection(AID)
{
  var LElement = document.getElementById(AID);
  LElement.onselectstart = function()
  {
    return false;
  };

  LElement.unselectable = "on";
  LElement.style.MozUserSelect = "none";
  LElement.style.cursor = "default";
}

function FormatMoney(amount, currency_sign_before,
  currency_sign_after, thousands_separator, decimal_point,
  significant_after_decimal_pt, display_after_decimal_pt)
{
    // Only display a minus if the final displayed value is going to be <= -0.01 (or equivalent)
    var str_minus = (amount * significant_multiplier <= -0.5 ? "-" : "");

    // Sanity check on the incoming amount value
    amount = parseFloat(amount);
    if( isNaN(amount) || Math.LOG10E * Math.log(Math.abs(amount)) +
            Math.max(display_after_decimal_pt, significant_after_decimal_pt) >= 21 )
    {
        return str_minus + currency_sign_before +
            (isNaN(amount)? "#" : "####################".substring(0, Math.LOG10E * Math.log(Math.abs(amount)))) +
            (display_after_decimal_pt >= 1?
                (decimal_point + "##################".substring(0, display_after_decimal_pt)) : "") +
            currency_sign_after;
    }

    var significant_multiplier = Math.pow(10, significant_after_decimal_pt);

    // Make +ve and ensure we round up/down properly later by adding half a penny now.
    amount = Math.abs(amount) + (0.5 / significant_multiplier);

    amount *= significant_multiplier;

    var str_display = parseInt(
        parseInt(amount) * Math.pow(10, display_after_decimal_pt - significant_after_decimal_pt) ).toString();

    // Prefix as many zeroes as is necessary and strip the leading 1
    if( str_display.length <= display_after_decimal_pt )
        str_display = (Math.pow(10, display_after_decimal_pt - str_display.length + 1).toString() +
            str_display).substring(1);

    var comma_sep_pounds = str_display.substring(0, str_display.length - display_after_decimal_pt);
    var str_pence = str_display.substring(str_display.length - display_after_decimal_pt);

    if( thousands_separator.length > 0 && comma_sep_pounds.length > 3 )
    {
        comma_sep_pounds += ",";

        // We need to do this twice because the first time only inserts half the commas.  The reason is
        // the part of the lookahead ([0-9]{3})+ also consumes characters; embedding one lookahead (?=...)
        // within another doesn't seem to work, so (?=[0-9](?=[0-9]{3})+,)(.)(...) fails to matchanything.
        if( comma_sep_pounds.length > 7 )
            comma_sep_pounds = comma_sep_pounds.replace(/(?=[0-9]([0-9]{3})+,)(.)(...)/g, "$2,$3");

        comma_sep_pounds = comma_sep_pounds.replace(/(?=[0-9]([0-9]{3})+,)(.)(...)/g, "$2,$3");

        // Remove the fake separator at the end, then replace all commas with the actual separator
        comma_sep_pounds = comma_sep_pounds.substring(0, comma_sep_pounds.length - 1).replace(/,/g, thousands_separator);
    }

    return str_minus + currency_sign_before +
        comma_sep_pounds + (display_after_decimal_pt >= 1? (decimal_point + str_pence) : "") +
        currency_sign_after;
}

function MM_findObj(n, d) //v4.0
{
  var p,i,x;
  if(!d)
    d = document;
  if((p = n.indexOf("?")) > 0 && parent.frames.length)
  {
    d = parent.frames[n.substring(p+1)].document;
    n = n.substring(0,p);
  }
  if(!(x = d[n]) && d.all)
    x = d.all[n];
  for (i = 0; !x && i<d.forms.length; i++)
    x = d.forms[i][n];
  for (i = 0; !x && d.layers && i < d.layers.length; i++)
    x = MM_findObj(n, d.layers[i].document);
  if (!x && document.getElementById)
    x = document.getElementById(n);
  return x;
}

function MM_preloadImages() //v3.0
{
  var d = document;
  if (d.images)
  {
    if(!d.MM_p)
      d.MM_p = new Array();
    var i,j = d.MM_p.length,
    a = MM_preloadImages.arguments;
    for (i = 0; i < a.length; i++)
      if (a[i].indexOf("#") != 0)
      {
        d.MM_p[j] = new Image;
        d.MM_p[j++].src = a[i];
      }
  }
}

function MM_swapImage() //v3.0
{
  var i,j = 0, x, a = MM_swapImage.arguments;
  document.MM_sr = new Array;
  for (i = 0; i < (a.length-2); i += 3)
    if ((x = MM_findObj(a[i])) != null)
    {
      document.MM_sr[j++] = x;
      if (!x.oSrc)
        x.oSrc = x.src;
      x.src = a[i+2];
    }
}

function MM_swapImgRestore() //v3.0
{
  var i,x,a = document.MM_sr;
  for (i = 0; a && i < a.length && (x = a[i]) && x.oSrc; i++)
    x.src=x.oSrc;
}

function OpacitySet(AElement, ALevel)
{
  if (!AElement)
    return;
  AElement.style.opacity = ALevel;
  AElement.style.MozOpacity = ALevel;
  AElement.style.KhtmlOpacity = ALevel;
  AElement.style.filter = "alpha(opacity=" + (ALevel * 100) + ");";
}

function elementByClassNameGet(AParent, AClassName, ATagName)
{
  var Result = [], LTagName = ATagName ? ATagName : '*';
  if(document.getElementsByClassName)
    Result = AParent.getElementsByClassName(AClassName);
  else
  {
    var LElem,
      LChildren = AParent.getElementsByTagName(LTagName),
      LLen = LChildren.length,
      LRE = new RegExp('\\b' + AClassName + '\\b','ig');
    while(LLen)
    {
      LElem = LChildren[--LLen];
      if (LRE.test(LElem.className))
        Result.push(LElem);
    }
  }
  return (Result && Result.length > 0 ? Result[0] : null);
}

function cookieDelete(AName, APath, ADomain)
{
  if (cookieGetCheck(AName))
    document.cookie = AName + "=" + (APath ? ";path=" + APath : "") + (ADomain ? ";domain=" + ADomain : "" ) + ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

function cookieGetCheck(AName)
{
  var nameEQ = AName + "=", 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 cookiesPresent()
{
  return ((window.FCookies || (window.FCookies = (cookieGetCheck('SessionID') ? 2 : 1))) == 2);
}

function cookieSet(AName, AValue, AExpDays)
{
  var LExpDate=new Date();
  if (AExpDays)
    LExpDate.setDate(LExpDate.getDate() + AExpDays);
  document.cookie = AName + '=' + (escape(AValue) + (AExpDays ?'; expires=' + LExpDate.toUTCString() : '') + '; path=/');
}

function formCreateAndSubmit(AURL, AParams)
{
  function formCreate(AActionURL)
  {
    var result = document.createElement('FORM');
    document.body.appendChild(result);
    result.method = 'POST';
    result.action = 'http://' + AActionURL;
    return result;
  };

  function formInputCreate(AForm, AName, AValue)
  {
    var result = document.createElement('INPUT');
    result.type = 'hidden';
    result.name = AName;
    result.value = AValue;
    AForm.appendChild(result);
    return result;
  };

  var LForm = formCreate(AURL);
  for (var i = 0; i < AParams.length / 2; i++)
    formInputCreate(LForm, AParams[2 * i], AParams[2 * i + 1]);
  LForm.submit();
}

function objToQS(AObj)
{
  var LRes = [], i = 0;
  for(var LProp in AObj) {
    LRes[i++] = escape(LProp) + '=' + escape(AObj[LProp])};
  if (LRes.length == 0)
    LRes[0] = 'dummy=1';
  return LRes.join('&');
}

function xmlNodeTextGet(ANode)
{
  return ANode.childNodes.item(ANode.childNodes.length == 3 ? 1 : 0).nodeValue;
}

function xmlNodeValueGetCheck(AXMLDocument, AName)
{
  var LXMLList = AXMLDocument.getElementsByTagName(AName);
  if (LXMLList.length == 0)
    return null;
  else
    return xmlNodeTextGet(LXMLList.item(0));
}

function windowDimension(AWindow)
{
  if( typeof( AWindow.innerWidth ) == 'number' )
    return { //Non-IE
      W: AWindow.innerWidth,
      H: AWindow.innerHeight};
  else
  if (AWindow.document.documentElement &&
    (AWindow.document.documentElement.clientWidth || AWindow.document.documentElement.clientHeight)
  )
    return {//IE 6+ in 'standards compliant mode'
      W: AWindow.document.documentElement.clientWidth,
      H: AWindow.document.documentElement.clientHeight};
  else
  if (AWindow.document.body &&
    (AWindow.document.body.clientWidth || AWindow.document.body.clientHeight)
  )
    return {//IE 4 compatible
      W: AWindow.document.body.clientWidth,
      H: AWindow.document.body.clientHeight}
}

(function($){
  $.fn.extend({
    center: function () {
      return this.each(function() {
        var top = ($(window).height() - $(this).outerHeight()) / 2;
        var left = ($(window).width() - $(this).outerWidth()) / 2;
        $(this).css({position:'absolute', margin:0, top: (top > 0 ? top : 0)+'px', left: (left > 0 ? left : 0)+'px'});
      });
    }
  });
})(jQuery);
