function isLeapYear(year)
{
  return ( ((year)>0) && !((year)%4) && ( ((year)%100) || !((year)%400) ) );
}
  
function monthDays(month,year)
{
  month = parseInt(month);
  year = parseInt(year);
  switch(month)
  {
    case 1: return 31;
    case 2: 
      if(year == 0 || isLeapYear(year)) 
        return 29; 
      else 
        return 28;
      break;
    case 3: return 31;
    case 4: return 30;
    case 5: return 31;
    case 6: return 30;
    case 7: return 31;
    case 8: return 31;
    case 9: return 30;
    case 10: return 31;
    case 11: return 30;
    case 12: return 31;
    default: return 31;
  }
}

function submitenter(myfield,e)
{
  var keycode;
  if (window.event) keycode = window.event.keyCode;
  else if (e) keycode = e.which;
  else return true;
  
  if (keycode == 13)
  {
   myfield.form.submit();
   return false;
  }
  else return true;
}

function isValidIdentifier(ident, minlength)
{
  var reg = new RegExp("^[A-Za-z0-9_]{" + minlength.toString() + ",}$");
  return reg.test(ident);
}

function getViewPortHeight()
{
  if (typeof window.innerWidth != 'undefined') // Mozilla
  {
    return window.innerHeight;
  }
  else if (typeof document.documentElement != 'undefined'
      && typeof document.documentElement.clientWidth !=
      'undefined' && document.documentElement.clientWidth != 0) // IE Standards compliant
  {
    return document.documentElement.clientHeight;
  }
  else // Old IE
  {
    return document.getElementsByTagName('body')[0].clientHeight;
  }  
}

function shade()
{
  var wrapperHeight = $('wrapper').offsetHeight;
  var viewportHeight = getViewPortHeight();
  var height = viewportHeight > wrapperHeight ? viewportHeight : wrapperHeight;
  $('shader').style.height = height.toString() + "px";
  $('shader').setStyle({ opacity: 0.30 });
  $('shader').show();
}

function unshade()
{
  Effect.Fade($('shader'), { duration: 0.4 } );
  $('shader').hide();
}

function centerWindow(id)
{
  var wnd = $(id);
  var real_xy = Position.realOffset(wnd);
  var diff = getViewPortHeight() - wnd.offsetHeight;
  var newtop = diff / 2 + real_xy[1];
  var docheight = $('wrapper').offsetHeight;
  if( newtop + wnd.offsetHeight > docheight ) newtop = docheight - wnd.offsetHeight;
  wnd.style.top = newtop.toString() + "px";
}

function openWindow(id)
{
  shade();
  var window = $(id);
  window = window.remove();
  $('adminbody').appendChild(window);
  window.show();
  centerWindow(id);
}

function closeWindow(id)
{
  Effect.Fade($(id), { duration: 0.4 } );
  unshade();
}

function showHint(elem)
{
  elem = $(elem);
  var container = elem.up();
  var hintLeft = (elem.offsetLeft + elem.offsetWidth + 5).toString() + "px";
  var hintTop = (elem.offsetTop).toString() + "px";
  
  var div = Builder.node("div", { className: 'hint-top', style: "left: " + hintLeft + "; top: " + hintTop + ";" }, [
    Builder.node("div", { className: 'hint-bottom' }, [
      Builder.node("div", { className: 'hint-content' }, elem.alt )
    ])
  ]);
  
  if( elem.next() == null )
    container.appendChild(div);  
  else
    container.insertBefore(div, elem.next());  
}

function hideHint(elem)
{
  elem = $(elem);
  var hint = elem.next();
  if( hint != null ) hint.remove();
}

function showSuccess(txt)
{
  var adminSuccess = $('admin-success');
	adminSuccess.innerHTML = txt;
	adminSuccess.show();
	new PeriodicalExecuter(function(pe) {
    pe.stop();
    Effect.Fade(adminSuccess);
	}, 8);
}

function track(rel, lookup, recursive, referer)
{
  var params = "&w=" + screen.width;
  params = params + "&h=" + screen.height;
  params = params + "&c=" + screen.colorDepth;
  params = params + "&l=" + lookup;
  params = params + "&r=" + recursive;
  params = params + "&ref=" + referer;
  var ajax = new Ajax.Request('ajax.php',
    {method: 'post', evalScripts: true, parameters: 's=' + rel + '/callback/track.php' + params, onComplete: track_complete }
  );
}

function track_complete(xhr)
{
}

/*
 *  Global responders for Ajax events.
 *  OnCreate   - an Ajax request was initiated. Show the throbbing loader.
 *  OnComplete - an Ajax request was completed. Hide the throbbing loader 
 *               (but only when there are no more active requests).
 *
 */
var globalResponders = 
{
  onCreate: function()
  {
    Element.show('loader');
  },
  onComplete: function() 
  {
    if(Ajax.activeRequestCount == 0)
    {
      Element.hide('loader');
    }
  }
};
Ajax.Responders.register(globalResponders);



/*
 *  Add automatic hint display for form fields.
 */
Event.observe(window, 'load', function() {
  var inputs = $$('textarea[alt]', 'input[alt]');
  inputs.each(function(input) {
    input.onfocus = function() { showHint(input); return false; };
    input.onblur = function() { hideHint(input); return false; };
  });
});