var XLCommon = {};

XLCommon.CommonForm = document.createElement("form"); XLCommon.CommonForm.setAttribute("method", "post"); 


XLCommon.WindowScrollTop = function ()
{
  var st = (document.documentElement && document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
  var parts = 10;
  if(st > 0)
  {
    for(var i = st * (parts - 1) / parts; i >= 0; i -= st / parts)
    {
      window.scroll(0, i);
    }
    window.scroll(0, 0);
  }
}

XLCommon.Sleep = function (msec)
{
  var start = new Date().getTime();
  while (new Date().getTime() - start < msec);
}



XLCommon.CommonFormClear = function (Action)
{
  if(!XLCommon.IsEmpty(XLCommon.CommonForm))
  {
    XLCommon.CommonForm.innerHTML = "<input name = 'action' value = '" + Action + "'>";
    return true;
  }
  return false;
}



XLCommon.IsEmpty = function (O)
{
  if(O == undefined) return true;
  else if(!O) return true;
  else if(0 + O == 0) return true;
  return false;
}

XLCommon.CreateXmlHttpObject = function ()
{
  var XO = false;
  if(window.XMLHttpRequest)
  {
    XO = new XMLHttpRequest();
  }
  else if(window.ActiveXObject)
  {
    XO = new ActiveXObject("Microsoft.XMLHTTP");
  }
  else
  {
    alert('Your browser have not support XmlHttpRequest');
  }
  return XO;
}

XLCommon.CreateXmlDocument = function (Text)
{
  if(window.ActiveXObject)
  {
    var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
    xmlDoc.async = false;
    if(!XLCommon.IsEmpty(Text))
    {
      if(!xmlDoc.loadXML(Text))
      {
        return false;
      }
    }
    return xmlDoc;
  }
  else if(document.implementation && document.implementation.createDocument)
  {
    var xmlDoc = false;
    if(XLCommon.IsEmpty(Text))
    {
      xmlDoc = document.implementation.createDocument("", "dummy-root", null);
      xmlDoc.async = false;
    }
    else
    {
      var p = new DOMParser();
      xmlDoc = p.parseFromString(Text, "text/xml");
    }
    return xmlDoc;
  }
  else
  {
    alert('Your browser have not support XmlHttpRequest');
  }
  return false;
}

XLCommon.GetXmlObject = function (XmlElem, Path)
{
  if(this.IsEmpty(XmlElem) || this.IsEmpty(Path)) return false;
  var p = Path.Trim("/ ").split("/");
  var e = XmlElem;
  for(var i = 0; i < p.length && !this.IsEmpty(e); i++)
  {
    var ex = false;
    
    for(var j = 0; j < e.childNodes.length; j++)
    {
      
      if(e.childNodes.item(j).tagName == p[i])
      {
        ex = true;
        e = e.childNodes.item(j);
        break;
      }
    }
    if(!ex)
    {
      return false;
    }
  }
  return e;
}

XLCommon.GetXmlObjectValue = function (XmlElem, Path)
{
  var o = XLCommon.GetXmlObject(XmlElem, Path);
  if(this.IsEmpty(o)) return false;
  if(!this.IsEmpty(o.childNodes) && o.childNodes.length > 0) return o.childNodes.item(0).nodeValue;
  return o.nodeValue;
}

XLCommon.GetElement = function (Identifier)
{
  if(Identifier != null && Identifier != "")
  {
    return document.getElementById(Identifier);
  }
  return false;
}

XLCommon.CreateBlankTable = function (NoBorderCollapse)
{
  var e = document.createElement("table");
  e.setAttribute("cellspacing", 0);
  e.setAttribute("cellpadding", 0);
  e.setAttribute("boder", 0);
  if(XLCommon.IsEmpty(NoBorderCollapse))
  {
    e.style.borderCollapse = "collapse";
  }
  return e;
}

XLCommon.CreateBlankImage = function ()
{
  var e = document.createElement("img");
  e.style.verticalAlign = "middle";
  e.style.border = "0px";
  e.setAttribute("boder", 0);
  e.style.boder = "0px";
  return e;
}

XLCommon.CreateBlankFieldSet = function (Legend)
{
  var e = document.createElement("fieldset");
  if(!this.IsEmpty(Legend))
  {
    var l = document.createElement("legend");
    l.innerHTML = Legend;
    e.appendChild(l);
  }
  return e;
}

XLCommon.FormSetNameValue = function (Form, Name, Value)
{
  if(XLCommon.IsEmpty(Form) || XLCommon.IsEmpty(Name)) return false;
  var i = document.createElement("input");
  i.setAttribute("type", "hidden");
  i.setAttribute("name", Name);
  i.setAttribute("value", Value);
  Form.appendChild(i);
  return true;
}

XLCommon.GetElementPosition = function (E)
{
  var x = y = 0;
  while(E)
  {
    x += E.offsetLeft;
    y += E.offsetTop;
    E = E.offsetParent;
  }
  return {x:x, y:y};
}





XLCommon.Random = function (ValueStart, ValueEnd)
{
  if(ValueEnd > ValueStart && ValueStart >= 0)
  {
    return Math.round(Math.random() * (ValueEnd - ValueStart));
  }
  return Math.random();
}


XLCommon.GetMousePosition = function (e)
{
  var x = 0, y = 0;
  if (!e) e = window.event;
  if (e.pageX || e.pageY)
  {
    x = e.pageX;
    y = e.pageY;
  }
  else if (e.clientX || e.clientY)
  {
    x = e.clientX +
      (document.documentElement.scrollLeft || document.body.scrollLeft) -
      document.documentElement.clientLeft;
    y = e.clientY +
      (document.documentElement.scrollTop || document.body.scrollTop) -
      document.documentElement.clientTop;
  }
  return {"x":x, "y":y};
}



XLCommon.GetMiddleWindowTop = function (h)
{
  return Math.round((XLConst.ScreenHeight - h) / 2);
}



XLCommon.GetMiddleWindowLeft = function (w)
{
  return Math.round((XLConst.ScreenWidth - w) / 2);
}





