﻿addEvent = function(source, eventType, callBack)
{
    if (window.addEventListener)
        source.addEventListener(eventType, callBack, false);
    else if (window.attachEvent)
        source.attachEvent('on' + eventType, callBack);
}

getXmlHttp = function()
{
    var result;

    try
    {
        result = new XMLHttpRequest();
    }
    catch (e)
    {
        result = new ActiveXObject('Msxml2.XMLHTTP');
    }
    return result;
}

getParams = function(source)
{ 
  var result = new Array();
  var pos = source.lastIndexOf('?');

  if (pos >= 0 && pos < (source.length - 1))
  {
    var rx = /([^=]+)=([^(&|$)]*)(&|$)/g;
    var m;
    source = source.slice(pos + 1);

    while (m = rx.exec(source))
      result[m[1]] = m[2];
  }

  return result;
}

getFirstChild = function(elm)
{
    var result = elm.firstChild;
    
    if (result == null)
        return null;
    
    if (result.nodeType == 3)
        return result.nextSibling;
        
    return result;
}

getPreviousSibling = function(elm)
{
    var result = elm.previousSibling;
    
    if (result == null)
        return null;
        
    if (result.nodeType == 3)
        return result.previousSibling;
    
    return result;
}

getNextSibling = function(elm)
{
    var result = elm.nextSibling;
    
    if (result == null)
        return null;
    
    if (result.nodeType == 3)
        return result.nextSibling;
        
    return result;
}

setOpacity = function(elm, opacityPercentage)
{ 
  var reScaled = opacityPercentage / 100;
  with (elm.style)
  { 
    opacity = reScaled; 
    MozOpacity = reScaled; 
    KhtmlOpacity = reScaled;
    filter = 'alpha(opacity=' + opacityPercentage + ')'; 
  }
}

getScrollPosition = function()
{
  var resultX, resultY;
  if (window.pageYOffset)
  {
    resultX = window.pageXOffset;
    resultY = window.pageYOffset;
  }
  else
  {
    resultX = document.body.scrollLeft | document.documentElement.scrollLeft;
    resultY = document.body.scrollTop | document.documentElement.scrollTop;
  }
  return { x : resultX, y : resultY };
}

getAbsolutePosition = function(elm)
{
  var resultX = 0;
  var resultY = 0;
  
  do
  {
    resultX += elm.offsetLeft;
    resultY += elm.offsetTop;
  }
  while (elm = elm.offsetParent);
  
  return { x : resultX, y : resultY };
}

getModelFromXml = function(rootElm)
{
  var result = null;
  var attrs = rootElm.attributes;
  var attrsLen = attrs.length;
  var isCollection = false;
  for (var i = 0; i < attrsLen; i++)
  {
    var attr = attrs[i];
    if (attr.name == 'count')
    {
      result = new Array();
      isCollection = true;
      break;
    }
  }
  
  for (var childElm = getFirstChild(rootElm); childElm; childElm = getNextSibling(childElm))
  {
    if (isCollection)
      result.push(getModelFromXml(childElm));
    else
    {
      var subModel = getModelFromXml(childElm);
      var nn = childElm.nodeName;

      if (result == null)
        result = new Array();
        
      if (subModel != null)
        result[nn] = subModel;
      else
      {
        var fc = childElm.firstChild;
        if (fc != null)
          result[nn] = fc.nodeValue;
        else
          result[nn] = '';
      }
    }
  }
  
  return result;
}
