﻿// (c) visarc 2007

// browser detection

var nn4 = false;	
var ie4 = false;
var ie5 = false;
var ie6 = false;
var moz = false;	
var dom1 = false;	
var dom2 = false;	
var old = false;	

if(window.document.implementation)
{
  dom1 = window.document.implementation.hasFeature("HTML","1.0");
  dom2 = window.document.implementation.hasFeature("HTML","2.0") &&	window.document.implementation.hasFeature("Events","2.0") && 	window.document.implementation.hasFeature("Core","2.0") && window.document.implementation.hasFeature("CSS2","2.0");
}
moz = window.navigator ? (window.navigator.userAgent.indexOf("ecko") != -1)	: false;
nn4 = window.document.layers && !moz;
ie6 = window.document.all && dom1;
ie5 = window.document.all && window.document.getElementsByTagName && !ie6;
ie4 = window.document.all && !ie6 && !ie5;
old = (!ie4 && !ie5 && !ie6 && !dom1 && !nn4 && !moz);

// misc helpers

function getElement(id)
{
  return document.getElementById ? document.getElementById(id) : document.all[id];
}

function getLeft(e)
{
  if(e.offsetParent) return e.offsetLeft + getLeft(e.offsetParent);
  return e.offsetLeft;
}

function getTop(e)
{
  if(e.offsetParent) return e.offsetTop + getTop(e.offsetParent);
  return e.offsetTop;
}

function getWidth(e)
{
  return parseInt(e.offsetWidth, 10);
}

function getHeight(e)
{
  return parseInt(e.offsetHeight, 10);
}

function getWindowWidth()
{
  if(document.layers || (document.getElementById && !document.all))
  {
    return window.outerWidth;
  }
  return document.body.clientWidth;
}

function getWindowHeight()
{
  if(document.layers || (document.getElementById && !document.all))
  {
    return  window.outerHeight;
  }
  return document.body.clientHeight;
}

function getScrollHeight()
{
  var h = window.pageYOffset || document.body.scrollTop || document.documentElement.scrollTop;
  return h ? h : 0;
}

function getScrollWidth()
{
  var w = window.pageXOffset || document.body.scrollLeft || document.documentElement.scrollLeft;
  return w ? w : 0;
}

function setOpacity(e, pc)
{
  var u = (pc * 1.0) / 100.0;
  e.style.opacity = u;
  e.style.MozOpacity = u;
  e.style.KhtmlOpacity = u;
  e.style.filter = "alpha(opacity=" + pc + ")";
}

function setEvent(e, tag, callback)
{
  if(e.addEventListener)
  {
    e.addEventListener(tag, callback, true);
    return true;
  }
  else if(e.attachEvent)
  {
    return e.attachEvent("on" + tag, callback);
  }
}

function detachEvent(e, tag, callback)
{
  if(e.removeEventListener)
  {
    e.removeEventListener(tag, callback, true);
    return true;
  }
  else if(e.detachEvent)
  {
    return e.detachEvent("on" + tag, callback);
  }
}

function cancelEvent(e)
{
  if(moz || dom2)
  {
    e.stopPropagation();
    e.preventDefault();
  }
  if(ie4 || ie5 || ie6)
  {
	  window.event.cancelBubble = true;
	  window.event.returnValue = false;
  }
}

function checkFlash()
{
  var hasFlash = false;
  if(navigator.plugins && navigator.plugins.length)
  {
    var flash = navigator.plugins["Shockwave Flash"];
    return flash != null;
  }
  else if (navigator.mimeTypes && navigator.mimeTypes.length)
  {
    var flash = navigator.mimeTypes["application/x-shockwave-flash"];
    return flash && flash.enabledPlugin;
  }
  else
  {
    // the ie way...
    try
    {
      var flash = new ActiveXObject("Shockwave.Flash.ShockwaveFlash.ShockWaveFlash");
      return flash != null;
    }
    catch(e) { };
  }
}

function centreDiv(e)
{
  var left = (getWindowWidth() - getWidth(e)) / 2;
  var top = ((getWindowHeight() - getHeight(e)) / 2) + getScrollHeight() - 50;
  e.style.left = left + "px";
  e.style.top = top + "px";
}

function setCursorTo(e, pos)
{
  if(e.createTextRange)
  {
    var range = e.createTextRange();
    range.move("character", pos);
    range.select();
  }
  else if(e.selectionStart)
  {
    e.focus();
    e.setSelectionRange(pos, pos);
  }
}

function showPopup(url, width, height)
{
  var left = (screen.width - width) / 2;
  var top = (screen.height - height) / 2;
  window.open(url, "", "width=" + width + ",height=" + height + ",screenX=" + left + ",left=" + left + ",screenY=" + top + ",top=" + top + ",menubar=no,toolbar=no,status=no,scrollbars=no,location=no,directories=no");
}

function swapImage(id, src)
{
  var e = getElement(id);
  if(e)
  {
    e.src = src;
  }
}

function isValidDate(str)
{
  var monthLength = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
  var a = str.split(/[-/.]/gi);
  if(a.length == 3)
  {
    var day = parseInt(a[0], 10);
    var month = parseInt(a[1], 10);
    var year = parseInt(a[2], 10);
    if(isNaN(day) || isNaN(month) || isNaN(year) || day < 1 || month < 1 || month > 12 || year < 0) return false;
    if(year % 4 == 0) monthLength[1] = 29;
    if(day > monthLength[month - 1]) return false;
    return true;
  }
  return false;
}