/*
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; version 2 of the License.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** Loïc DIAS DA SILVA <mglcel@mglcel.fr>
** FOAFDrive - another FOAF wizzard
**
** $Id: compat.js,v 1.12 2008-08-02 00:24:41 mglcel Exp $
*/

// Compatibility framework ----------------------------------------------------

function compat_getElementById(id, doc)
{
  if (doc == null)
    doc = document;
  if (doc.getElementById) 
    return doc.getElementById(id);
  if (doc.all && !doc.getElementById) 
    return doc.all[id];
  if (doc.layers) 
    return doc.layers[id];
}

// ============================================================================

function compat_getStyleById(id)
{
  return (document.layers ? compat_getElementById(id) : compat_getElementById(id).style);
}

// ============================================================================

function compat_getXMLHttpRequest () {
  var xmlhttp = false;

  if(window.XMLHttpRequest)
    xmlhttp = new XMLHttpRequest();
  else if(window.ActiveXObject) {
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
        xmlhttp = false;
      }
    }
  }

  if (!xmlhttp && window.createRequest) {
    try {
      xmlhttp = window.createRequest();
    } catch (e) {
      xmlhttp = false;
    }
  }

  return xmlhttp;
}

// ============================================================================

function compat_getIFrameDocument (iframe) {
  if (iframe.contentDocument != null)
    return iframe.contentDocument;
  if (iframe.contentWindow != null)
    return iframe.contentWindow.document;
  if (iframe.document != null) {
    if (iframe.document.documentElement)
      return iframe.document.documentElement;
    return iframe.document;
  }
}

// ============================================================================

function compat_getNewDocument() {
  var xmlDoc = null;

  var ARR_ACTIVEX = Array("MSXML4.DOMDocument", 
                     "MSXML3.DOMDocument",
                     "MSXML2.DOMDocument", 
                     "MSXML.DOMDocument",
                     "Microsoft.XmlDom");

	if (document.implementation && document.implementation.createDocument) {
		xmlDoc = document.implementation.createDocument("", "", null);
	}
	else if (window.ActiveXObject) {
    var found = false;
    for (var i=0; i < ARR_ACTIVEX.length && !found; i++) {
      try {
         var xmlDoc = new ActiveXObject(ARR_ACTIVEX[i]);
         STR_ACTIVEX = ARR_ACTIVEX[i];
         found = true;
      } catch (e) { }
    }
 	} 
	else
	{
		alert('Your browser can\'t handle XML Document creation');
		return false;
	}

  return xmlDoc;
}

// ----------------------------------------------------------------------------

function compat_importXML(xmlFile)
{
  xmlDoc = compat_getNewDocument();

  if (xmlDoc == null)
    return false;

  xmlDoc.async=false;
  xmlDoc.validateOnParse=false;
  //xmlDoc.resolveExternal=false;
	xmlDoc.load(xmlFile);

  return xmlDoc;
}

// ============================================================================

function compat_getElementsByTagNameNS(node, prefix, ns, tagname) {
  try {
    return node.getElementsByTagNameNS(ns, tagname);
  } catch (e) {
    return node.getElementsByTagName(prefix+':'+tagname);
  }
}

// ============================================================================

function compat_getAttributeNS(node, prefix, ns, tagname) {
  try {
    return node.getAttributeNS(ns, tagname);
  } catch (e) {
    return node.getAttribute(prefix+':'+tagname);
  }
}

// ============================================================================

function compat_setOnChange(id, eventString) {
  try {
      id.attachEvent('onchange', eventString);
      id.setAttribute('onchange', eventString); 
  } catch (e) {
      id.onchange = function () { eval(eventString); };
      id.onkeypress = function () { 
        if (window.event && window.event.keyCode == 13)
          eval(eventString); 
      };
  }
}

// ============================================================================

function compat_setOnClick(id, eventString) {
  try {
      id.attachEvent('onclick', eventString);
      id.setAttribute('onclick', eventString); 
  } catch (e) {
      id.onclick = function () { eval(eventString); };
  }
}

// ============================================================================

function compat_setOnMouseOver(id, eventString) {
  try {
      id.attachEvent('onmouseover', eventString);
      id.setAttribute('onmouseover', eventString); 
  } catch (e) {
      id.onmouseover = function () { eval(eventString); };
  }
}

// ============================================================================

function compat_setOnMouseOut(id, eventString) {
  try {
      id.attachEvent('onmouseout', eventString);
      id.setAttribute('onmouseout', eventString); 
  } catch (e) {
      id.onmouseout = function () { eval(eventString); };
  }
}

// ============================================================================

function compat_setOnLoad(id, eventString) {
  try {
      id.attachEvent('onload', eventString);
      id.setAttribute('onload', eventString); 
  } catch (e) {
      id.onload = function () { eval(eventString); };
  }
}

// ============================================================================

function compat_deleteElementById(id) {
  if (id == "" || typeof(id) == 'undefined')
    return;

  var myID = compat_getElementById(id);

  if (!myID)
    return false;

  if (!myID.parentNode)
    return false;

  // Removes
  myID.parentNode.removeChild(myID);
  return true;
}

// ============================================================================

function compat_windowResize(width, height) {
  if (window.outerWidth) {
    window.outerWidth = width;
    window.outerHeight = height;
  }
  else if (window.resizeTo) {
    window.resizeTo(width,height);
  }
}

// ============================================================================

function compat_iframeResize(obj) {
  var aID = obj.id;

  if (obj.contentDocument){
    obj.height = obj.contentDocument.images[0].height;
    obj.width = obj.contentDocument.images[0].width;
  } else {
    obj.style.height = document.frames[aID].document.images[0].height;
    obj.style.width = document.frames[aID].document.images[0].width;
  }

  obj.style.display = 'block';
}

// ============================================================================

function compat_createXMLDocument (rootElementName, namespaceURI, documentType) {
  var xmlDocument = null;
  namespaceURI = namespaceURI || '';
  documentType = documentType || null;
  if (document.implementation && document.implementation.createDocument) {
    xmlDocument = document.implementation.createDocument(
      namespaceURI,
      rootElementName,
      documentType
    );
  }
  else if (typeof ActiveXObject != 'undefined') {
    /*@cc_on @*/
    /*@if (@_jscript_version >= 5)
    try {
      xmlDocument = new ActiveXObject('Microsoft.XMLDOM');
      var rootElement = xmlDocument.createNode(1, rootElementName,
namespaceURI);
      xmlDocument.appendChild(rootElement);
    }
    catch (e) { }
    @end @*/
  }
  return xmlDocument;
}

// ----------------------------------------------------------------------------

function compat_createElementNS (namespaceURI, elementName, ownerDocument) {
  var element = null;
  if (typeof ownerDocument.createElementNS != 'undefined') {
    element = ownerDocument.createElementNS(namespaceURI, elementName);
  }
  else if (typeof ownerDocument.createNode != 'undefined') {
    element = ownerDocument.createNode(1, elementName, namespaceURI);
  }
  return element;
}

// ----------------------------------------------------------------------------

function compat_serializeNode (node) {
  if (typeof XMLSerializer != 'undefined') {
    return new XMLSerializer().serializeToString(node);
  }
  else if (typeof node.xml != 'undefined') {
    return node.xml;
  }
  else {
    return '';
  }
}

// ============================================================================

function compat_pageYOffset() {
  var scrOfY = 0;

  if( typeof( window.pageYOffset ) == 'number' ) {
    scrOfY = window.pageYOffset;
  } else if( document.body && document.body.scrollTop ) {
    scrOfY = document.body.scrollTop;
  } else if( document.documentElement && document.documentElement.scrollTop ) {
    scrOfY = document.documentElement.scrollTop;
  }

  return scrOfY;
}

// ____________________________________________________________________________

function compat_pageXOffset() {
  var scrOfX = 0;

  if( typeof( window.pageXOffset ) == 'number' ) {
    scrOfX = window.pageXOffset;
  } else if( document.body && document.body.scrollLeft ) {
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && document.documentElement.scrollLeft ) {
    scrOfX = document.documentElement.scrollLeft;
  }

  return [ scrOfX, scrOfY ];
}
