function print_page()
{
  window.print();
}

var version4 = (navigator.appVersion.charAt(0) == "4");
function popup(evnt, url)
{
  var width = 500;
  var height = 400;
  var position = 1;
  var properties = "scrollbars = 1, toolbar = 0, location = 0, height = " + height;
  properties = properties + ", width=" + width;
  var leftprop, topprop, screenX, screenY, cursorX, cursorY, padAmt;
  if(navigator.appName == "Microsoft Internet Explorer")
  {
    screenY = document.body.offsetHeight;
    screenX = window.screen.availWidth;
  }
  else
  {
    screenY = window.outerHeight
    screenX = window.outerWidth
  }
  if (position == 1)
  { // if POPUP not CENTER
    cursorX = evnt.screenX;
    cursorY = evnt.screenY;
    padAmtX = 10;
    padAmtY = 10;
    if((cursorY + height + padAmtY) > screenY)
    {
      // make sizes a negative number to move left/up
      padAmtY = (-30) + (height * -1);
      // if up or to left, make 30 as padding amount
    }
    if ((cursorX + width + padAmtX) > screenX)
    {
      padAmtX = (-30) + (width * -1);
      // if up or to left, make 30 as padding amount
    }
    if(navigator.appName == "Microsoft Internet Explorer")
    {
      leftprop = cursorX + padAmtX;
      topprop = cursorY + padAmtY;
    }
    else
    {
      leftprop = (cursorX - pageXOffset + padAmtX);
      topprop = (cursorY - pageYOffset + padAmtY);
    }
  }
  else
  {
    leftvar = (screenX - width) / 2;
    rightvar = (screenY - height) / 2;
    if(navigator.appName == "Microsoft Internet Explorer")
    {
      leftprop = leftvar;
      topprop = rightvar;
    }
    else
    {
      leftprop = (leftvar - pageXOffset);
      topprop = (rightvar - pageYOffset);
    }
  }
  if(evnt != null)
  {
    properties = properties + ", left = " + leftprop;
    properties = properties + ", top = " + topprop;
  }
  closePopup();
  popupHandle = open(url, "", properties);
}

var agent = navigator.userAgent.toLowerCase();
var browser_ie;
if (agent.indexOf("msie") != -1)
  browser_ie = true;
else
  browser_ie = false;

//aux functions
function createMethodRef(object, methodName, arg2)
{
  return function()
  {
    return object[methodName].apply(object, [arg2, arguments]);
  }
}

function attEvt_ie(obj, event, func)
{
  try{
    obj['evt_on' + event] = func;
    obj.attachEvent('on' + event, obj['evt_on' + event]);
  }catch(e)
  {
    alert(event);
  }
}

function attEvt_moz(obj, event, func)
{
  obj['evt_on' + event] = func;
  obj.addEventListener(event, obj['evt_on' + event], false);
}

function detEvt_ie(obj, event, func)
{
  obj.detachEvent("on" + event, obj['evt_on' + event]);
}

function detEvt_moz(obj, event, func)
{
  obj.removeEventListener(event, obj['evt_on' + event], false);
}

if (browser_ie)
{
  attEvt = attEvt_ie;
  detEvt = detEvt_ie;
}
else
{
  attEvt = attEvt_moz;
  detEvt = detEvt_moz;
}


function getOffsetLeft(elm)
{
  var mOffsetLeft = elm.offsetLeft;
  var mOffsetParent = elm.offsetParent;
  while(mOffsetParent)
  {
    if ((browser_ie) && (mOffsetParent.style.borderLeftWidth))
      mOffsetLeft += parseInt(mOffsetParent.style.borderLeftWidth.replace('px', ''));
    mOffsetLeft += mOffsetParent.offsetLeft;
    mOffsetParent = mOffsetParent.offsetParent;
  }
  return mOffsetLeft;
}

function getOffsetTop(elm)
{
  var mOffsetTop = elm.offsetTop;
  var mOffsetParent = elm.offsetParent;
  while(mOffsetParent)
  {
    if ((mOffsetParent.style.borderTopWidth))
      mOffsetTop += parseInt(mOffsetParent.style.borderTopWidth.replace('px', ''));
    mOffsetTop += mOffsetParent.offsetTop;
    mOffsetParent = mOffsetParent.offsetParent;
  }
  return mOffsetTop;
}

function getZIndex(obj)
{
  var par = obj.parentNode;
  var zIndex = 0;
  while (par)
  {
    if (browser_ie)
    {
      if (par.currentStyle)
        zIndex = Math.max(zIndex, par.currentStyle.zIndex);
    }
    else
      if (par.style)
        zIndex = Math.max(zIndex, par.style.zIndex);
    par = par.parentNode;
  }
  return zIndex;
}

var popupHandle;
function closePopup()
{
  if (popupHandle != null && !popupHandle.closed) popupHandle.close();
}

function displayPopup(position, url, name, height, width, evnt)
{
  // position=1 POPUP: makes screen display up and/or left, down and/or right
  // depending on where cursor falls and size of window to open
  // position=2 CENTER: makes screen fall in center
  var properties = "toolbar = 0, location = 0, height = " + height;
  properties = properties + ", width=" + width;
  var leftprop, topprop, screenX, screenY, cursorX, cursorY, padAmt;
  if(navigator.appName == "Microsoft Internet Explorer")
  {
    screenY = document.body.offsetHeight;
    screenX = window.screen.availWidth;
  }
  else
  {
    screenY = window.outerHeight
    screenX = window.outerWidth
  }
  if(position == 1)
  { // if POPUP not CENTER
    cursorX = evnt.screenX;
    cursorY = evnt.screenY;
    padAmtX = 10;
    padAmtY = 10;
    if((cursorY + height + padAmtY) > screenY)
    {
      // make sizes a negative number to move left/up
      padAmtY = (-30) + (height * -1);
      // if up or to left, make 30 as padding amount
    }
    if((cursorX + width + padAmtX) > screenX)
    {
      padAmtX = (-30) + (width * -1);
      // if up or to left, make 30 as padding amount
    }
    if(navigator.appName == "Microsoft Internet Explorer")
    {
      leftprop = cursorX + padAmtX;
      topprop = cursorY + padAmtY;
    }
    else
    {
      leftprop = (cursorX - pageXOffset + padAmtX);
      topprop = (cursorY - pageYOffset + padAmtY);
    }
  }
  else
  {
    leftvar = (screenX - width) / 2;
    rightvar = (screenY - height) / 2;
    if(navigator.appName == "Microsoft Internet Explorer")
    {
      leftprop = leftvar;
      topprop = rightvar;
    }
    else
    {
      leftprop = (leftvar - pageXOffset);
      topprop = (rightvar - pageYOffset);
    }
  }
  if(evnt != null)
  {
    properties = properties + ", left = " + leftprop;
    properties = properties + ", top = " + topprop;
  }
  closePopup();
  popupHandle = open(url,name,properties);
}

function in_array(needle, haystack)
{
  var found = false;
  var i = 0;
  while (i < haystack.length)
  {
    if (needle == haystack[i])
      found = true;
    i++;
  }
  return found;
}

function getObj(id)
{
  return document.getElementById(id);
}

function ValidateStandartForm(form)
{
  var inputs = form.getElementsByTagName('input');
  var c = inputs.length;
  var valid = true;
  for (var i = 0; i < c; i++)
  {
    var inp = inputs[i];
    if (inp.getAttribute('mandatory') && (inp.getAttribute('mandatory').toLowerCase() == 'true'))
    {
      if (!inp.getAttribute('handlersSet'))
      {
        attEvt(inp, 'change', createMethodRef(window, 'ClearError', inp));
        attEvt(inp, 'keydown', createMethodRef(window, 'ClearError', inp));
        attEvt(inp, 'focus', createMethodRef(window, 'ClearError', inp));
        inp.setAttribute('handlersSet', 'true')
      }
      if (inp.value == "")
      {
        try{
          var obj = getObj(inp.getAttribute('errortarget'));
        }catch(e){}
        if (obj)
          obj.style.display = '';
        valid = false;
      }
    }
  }
  return valid;
}

function ClearError(obj)
{
  try{
    getObj(obj.getAttribute('errortarget')).style.display = 'none';
  }catch(e){}
}

function ErrorToInfo(obj)
{
  try{
    getObj(obj.getAttribute('errortarget')).className = 'info';
  }catch(e){}
}
