// check which browser is running
var userAgent    = navigator.userAgent.toLowerCase();
var appVersion   = navigator.appVersion.toLowerCase();
var appName      = navigator.appName.toLowerCase();

// operating system and browser information
var isWin        = (appVersion.indexOf('windows') != -1);
var isOpera      = (userAgent.indexOf('opera') != -1);
var isIE         = (appName.indexOf('internet explorer') != -1) && !isOpera;
var isSafari     = (userAgent.indexOf('applewebkit') != -1);
var isMozilla    = (appName.indexOf('netscape') != -1) && !isSafari;

function getElementsByTagNameAttributeValue(ancestorEl, tagName, attrName, attrValue)
 {
  if (typeof(attrValue) == "undefined")
    attrValue = null;

  var els = [];
  var a = ancestorEl.getElementsByTagName(tagName);
  for (var i=0; i<a.length; i++)
  {
    var el = a[i];
    switch (attrName)
    {
      case "className":
      case "class":
        //if (el.className.indexOf(attrValue) != -1)
        if (Spif.ClassNameAbstraction.contains(el,attrValue))
          els.push(el);
        break;
      default:
        var val = el.getAttribute(attrName);
        if ((val != null) && ((attrValue == null) || (val == attrValue)))
          els.push(el);
    }
  }
  return els;
};

var asyncRequest = function() {
  function handleReadyState(o, callback) {
    var poll = window.setInterval(function() {
      if(o && o.readyState == 4) {
        window.clearInterval(poll);
        if ( callback ){
          callback(o);
        }
      }
    },
    50);
  }
  var http;
  try {
    http = new XMLHttpRequest();
  }
  catch(e) {
    var msxml = [
      'MSXML2.XMLHTTP.3.0', 
      'MSXML2.XMLHTTP', 
      'Microsoft.XMLHTTP'
    ];
    for ( var i=0, len = msxml.length; i < len; ++i ) {
      try {
        http = new ActiveXObject(msxml[i]);
        break;
      }
      catch(e) {}
    }
  }
  return function(method, uri, callback, postData) {
    http.open(method, uri, false);
    handleReadyState(http, callback);
    http.send(postData || null);
    return http;
  };
}();


function initXML(xmlString)
{
  if (window.ActiveXObject)
  {
    var doc=new ActiveXObject("Microsoft.XMLDOM");
    doc.async="false";
    doc.loadXML(xmlString);
    return doc;
  }
  else
  {
    var parser=new DOMParser();
    var doc=parser.parseFromString(xmlString,"text/xml");
    return doc;
  }
};


// check for XPath implementation 
if( document.implementation.hasFeature("XPath", "3.0") ) 
{ 
  // prototying the XMLDocument 
  XMLDocument.prototype.selectSingleNode = function(cXPathString, xNode) 
  { 
    if( !xNode ) { xNode = this; }
    var xItems = this.selectNodes(cXPathString, xNode);
    
    if( xItems.length > 0 )  
      return xItems[0];
    else 
      return null;
  } 
  
  // prototying the Element 
  Element.prototype.selectSingleNode = function(cXPathString) 
  {
    if(this.ownerDocument.selectSingleNode) 
      return this.ownerDocument.selectSingleNode(cXPathString, this); 
    else
      throw "For XML Elements Only";
  } 
} 

if( document.implementation.hasFeature("XPath", "3.0") ) 
{ 
  // prototying the XMLDocument 
  XMLDocument.prototype.selectNodes = function(cXPathString, xNode) 
  { 
    if( !xNode ) { xNode = this; }
    var oNSResolver = this.createNSResolver(this.documentElement) 
    var aItems = this.evaluate(cXPathString, xNode, oNSResolver,
                 XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null) 
    var aResult = [];
    for( var i = 0; i < aItems.snapshotLength; i++) 
    { 
      aResult[i] = aItems.snapshotItem(i);
    } 
    return aResult;
  } 

  // prototying the Element 
  Element.prototype.selectNodes = function(cXPathString) 
  { 
    if(this.ownerDocument.selectNodes) 
    { 
      return this.ownerDocument.selectNodes(cXPathString, this);
    } 
    else{throw "For XML Elements Only";} 
  } 
} 

function getRandomInt(min, max)
{
  return Math.floor(Math.random() * (max - min + 1)) + min;
}

function getRandomDec(min, max)
{
  return (Math.random() * (max - min + 1)) + min;
}

function newModifier ()
{
  var modifier = new Spif.Modifiers();
  modifier.duration = getRandomInt(2000, 3500);
  modifier.repeat = true;
  modifier.profile = modifier.RETURN;
  return modifier;
}

function quekInit ()
{
  if (!flashEnabled())
  {
    alert('Je moet Flash versie 9 of hoger geinstalleerd hebben om gebruik te kunnen maken van Quek.');
    window.location.href = "/overdekindertelefoon/quekhelp";
  }
  else if (QuekController.isSignedOn()) {
    QuekController.signOff();
  } 
  else 
  {
    Spif.show(document.getElementById('quekSettings'));
  }
  return false;
}

function flashEnabled()
{
  try
  {
    if (!FlashDetect.installed)
      return false
    else if (FlashDetect.major < 9)
      return false
    else
      return true;
  }
  catch(err)
  {
    return true;
  }
}
