function createElement(tagName, id, className)
{
    var element = document.createElement(tagName); 
    element.id = id;
    element.className = className;
    return element;
}

function showLoading()
{
    document.getElementById("updateProgress").style.display = "inline";
}

function hideLoading()
{
    document.getElementById("updateProgress").style.display = "none";
}

function loadXML($xmlData)
{
    var doc = null;
    // code for IE
    if (window.ActiveXObject)
    {
        doc = new ActiveXObject('MSXML.DOMDocument');
        doc.async = false;
        doc.loadXML($xmlData);
    }
    // code for Mozilla, Firefox, Opera, etc.
    else
    {
        var parser = new DOMParser();
        doc = parser.parseFromString($xmlData, "text/xml");
    }

    return doc;
}

function deleteChildNodes($element)
{
    if (!$element || !$element.childNodes)
        return;

    while ($element.childNodes.length >= 1)
    {
        $element.removeChild($element.firstChild);
    }
}

function deleteChildNode(parentElement, childElement)
{
    if (!parentElement || !parentElement.childNodes)
        return;

    parentElement.removeChild(childElement);
}

function showModalDialog(id) {
	el = document.getElementById(id);
	el.style.display = "block";
}

function hideModalDialog(id) {
	el = document.getElementById(id);
	el.style.display = "none";
}



sfHover = function() 
{
  var rootMenus = document.getElementsByTagName("UL"); 
  for (i = 0; i < rootMenus.length; i++) 
  { 
    var rootMenu = rootMenus[i];
    if (rootMenu.className == "level1") 
    { 
      var subMenus = rootMenu.getElementsByTagName("LI");
      for (var j = 0; j < subMenus.length; j++) 
      {
        subMenus[j].onmouseover = function() 
        {
	      this.className += " sfhover";
		}
		subMenus[j].onmouseout = function() 
		{
		  this.className = this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	  }
	}
  }
}

if (window.attachEvent) window.attachEvent("onload", sfHover);


// Show/hide drop down menu lists
function SetSelectVisibility(isVisible)
{        
  if (document.getElementsByTagName)
  {
    var visibility = (isVisible) ? "visible" : "hidden";
    var selectList = document.getElementsByTagName("select");
  
    if (selectList)
    {
      for(var i = 0; i < selectList.length; i++)
      {
        selectList[i].style.visibility = visibility;
      }
    }
  }
}

function encloseHandler(func, thisArg, parameters) 
{ 
    return function() { return func.apply(thisArg, parameters); }; 
}

function getOptionHtml(text, value, currentValue)
{
    var html = "<option";
    if (value == currentValue)
        html += " selected";
    html += " value='" + value + "'>" + text + "</option>";
    return html;
}

// array functions

/*Array.prototype.remove = function(from, to)
{
    var rest = this.slice((to || from) + 1 || this.length);
    this.length = from < 0 ? this.length + from : from;
    return this.push.apply(this, rest);
};*/
/*
Array.prototype.indexOf = function(value)
{
    for (var i = 0; i < this.length; i++)
        if (this[i] == value)
            return i;
    return -1;
}*/

function loadJsCssFile(filename, filetype)
{
    if (filetype == "js")
    { 
        //if filename is a external JavaScript file
        var fileref = document.createElement('script');
        fileref.setAttribute("type","text/javascript");
        fileref.setAttribute("src", filename);
    }
    else if (filetype == "css")
    { 
        //if filename is an external CSS file
        var fileref = document.createElement("link");
        fileref.setAttribute("rel", "stylesheet");
        fileref.setAttribute("type", "text/css");
        fileref.setAttribute("href", filename);
    }
    if (typeof fileref != "undefined")
        document.getElementsByTagName("head")[0].appendChild(fileref);
}

var filesadded = ""; //list of files already added

function checkLoadJsCssFile(filename, filetype)
{
    if (filesadded.indexOf("["+filename+"]") == -1)
    {
        loadjscssfile(filename, filetype)
        filesadded += "["+filename+"]"; // List of files added in the form "[filename1],[filename2],etc"
    }
}

function loadFileIntoElement(url, target) {
  document.getElementById(target).innerHTML = 'Fetching data...';
  if (window.XMLHttpRequest) {
    req = new XMLHttpRequest();
  } else if (window.ActiveXObject) {
    req = new ActiveXObject("Microsoft.XMLHTTP");
  }
  if (req != undefined) {
    req.onreadystatechange = function() {loadFileDone(url, target);};
    req.open("GET", url, true);
    req.send("");
  }
}  

function loadFileDone(url, target) {
  if (req.readyState == 4) { // only if req is "loaded"
    if (req.status == 200) { // only if "OK"
      document.getElementById(target).innerHTML = req.responseText;
    } else {
      document.getElementById(target).innerHTML=" Load file error:\n"+ req.status + "\n" +req.statusText;
    }
  }
}


// functions to make inheritance of classes

function extend(Child, Parent)
{
    var F = function() { };
    F.prototype = Parent.prototype;
    Child.prototype = new F();
    Child.prototype.constructor = Child;
    Child.base = Parent.prototype;
}

// copies all properties from scr to dst,
function mixin(dst, src)
{
    // tobj - object to filter properties of Object class
    var tobj = {}
    for (var x in src)
    {
        // copy into dst all properties from scr, except those, which are derived from Object
        if ((typeof tobj[x] == "undefined") || (tobj[x] != src[x]))
        {
            dst[x] = src[x];
        }
    }
    // In IE toString is missing in for..in
    if (document.all && !document.isOpera)
    {
        var p = src.toString;
        if (typeof p == "function" && p != dst.toString && p != tobj.toString &&
		 p != "\nfunction toString() {\n    [native code]\n}\n")
        {
            dst.toString = src.toString;
        }
    }
}

