/*
Common Javascript Function
Version : 1.00
Update  : 24.04.2007
Author  : Daniel Mueller

Functions:
 -$(id)
 - newTag(tag, attributes, <childs>)
 - newText(text)
 - getRequestObject()

Prototyped Functions:
 - String.startsWith
 - String.endsWith
*/


String.prototype.startsWith = function(s) { return this.indexOf(s)==0; }
String.prototype.endsWith   = function(s) { return (s == this.substring(this.length - s.length)); }


// remove/remane this function if the prototype.js-Framework is included too
var $ = function(id)
{
  return document.getElementById(id);
}


/*
  Function creates an Ajax request
*/
var getRequestObject = function()
{
  var request;
  if (window.ActiveXObject)
  {
     request = new ActiveXObject("Microsoft.XMLHTTP");
     //alert('ActiveX');
  }
  else if (window.XMLHttpRequest)
  {
     request = new XMLHttpRequest();
     //alert('XMLHTTP');
  }
  return request;
}

/*
  Function creates a new HTML Element with the given attributes and childs
*/
var newTag = function(tag, attributes, childs)
{
  var newElement = document.createElement(tag);
  for ( var n = 0; attributes && n < attributes.length; n++ )
  {
      //newElement.setAttribute(attributes[n][0],attributes[n][1]);
      if ( attributes[n][0].startsWith('on') )
      {
        var evt = attributes[n][0].substring(2, attributes[n][0].length);
        var functionName = attributes[n][1];
        newElement["on" + evt] = function()
        {
           eval(functionName);
        }

      }
      else
      {
        newElement.setAttribute(attributes[n][0],attributes[n][1], 1);
      }
  }
  for ( var n = 0; childs && n < childs.length; n++ )
  {
      newElement.appendChild(childs[n]);
  }
  return newElement;
}


/*
  Function creates a new TextNode: document.createTextNode(text)
  ... just a shortcut
*/
var newText = function(text)
{
  return document.createTextNode(text);
}




function addEvent( obj, type, fn )
{
   if (obj.addEventListener) {
      obj.addEventListener( type, fn, false );
   } else if (obj.attachEvent) {
      obj["e"+type+fn] = fn;
      obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
      obj.attachEvent( "on"+type, obj[type+fn] );
   }
}

function removeEvent( obj, type, fn )
{
   if (obj.removeEventListener) {
      obj.removeEventListener( type, fn, false );
   } else if (obj.detachEvent) {
      obj.detachEvent( "on"+type, obj[type+fn] );
      obj[type+fn] = null;
      obj["e"+type+fn] = null;
   }
}
