// This file contains support functionality for AJAX calls and feed-backs.

function execJS(node)
{
  var bSaf = (navigator.userAgent.indexOf('Safari') != -1);
  var bOpera = (navigator.userAgent.indexOf('Opera') != -1);
  var bMoz = (navigator.appName == 'Netscape');

  if (!node) return;

  /* IE heeft liever hoofdletters */
  var st = node.getElementsByTagName('SCRIPT');
  var strExec;

  for(var i=0;i<st.length; i++)
  {
    if (bSaf) {
      strExec = st[i].innerHTML;
      st[i].innerHTML = "";
    } else if (bOpera) {
      strExec = st[i].text;
      st[i].text = "";
    } else if (bMoz) {
      strExec = st[i].textContent;
      st[i].textContent = "";
    } else {
      strExec = st[i].text;
      st[i].text = "";
    }

    try {
      var x = document.createElement("script");
      x.type = "text/javascript";

      /* Voor IE gebruiken we .text! */
      if ((bSaf) || (bOpera) || (bMoz))
        x.innerHTML = strExec;
      else x.text = strExec;

      document.getElementsByTagName("head")[0].appendChild(x);
    } catch(e) {
      alert(e);
    }
  }
}; 

function InsertElementOnPage(targetElement, rtn, cleanUp)
{
  if (targetElement != null) {
    if (cleanUp == 1) {
      targetElement.innerHTML = "&nbsp;";
      if (rtn != '') {
        targetElement.innerHTML = "";
      }
    }
    if (navigator.appName.indexOf("Microsoft") != -1) {
      targetElement.insertAdjacentHTML("beforeEnd", "<body>" + rtn + "</body>"); // We need to add this for IE to parse any javascript.
    }
    else { // Firefox, Opera, Safari etc.
      var r = this.document.createRange();
      r.selectNodeContents(targetElement);
      r.collapse(false);
      df = r.createContextualFragment(rtn);
      targetElement.appendChild(df);
    }
  }
}

function GetXmlHttpObject()
{
  var xmlHttp=null;
  try
  {
  // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
  }
  catch (e)
  {
  // Internet Explorer
    try
    {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e)
    {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
  return xmlHttp;
}

function LockForXAction() {
	document.getElementById("styled_popup").style.display = "";
	document.getElementById("TB_overlay").style.display = "";

}

function UnlockAfterXAction() {
	document.getElementById("styled_popup").style.display = "none";
	document.getElementById("TB_overlay").style.display = "none";
	var o2 = document.getElementById("oLoader");
	if (o2 != null) {
		o2.style.display = "none";
	}
}

function LockForXActionAnimated(evt) {
	LockForXAction();
	var o = fPosition(evt);
	var o2 = document.getElementById("oLoader");
	o2.style.display = "";
	o2.style.top = o.y + "px";
	o2.style.left = o.x + "px";
}

function fPosition(e) {
	e = e || window.event;
	var cursor = {x:0, y:0};
	if (e.pageX || e.pageY) {
		cursor.x = e.pageX;
		cursor.y = e.pageY;
	} else {
		var de = document.documentElement;
		var b = document.body;
		cursor.x = e.clientX + (de.scrollLeft || b.scrollLeft) - (de.clientLeft || 0);
		cursor.y = e.clientY + (de.scrollTop || b.scrollTop) - (de.clientTop || 0);   
	}
	return cursor;
}

function findPos(obj) {
	if (obj != null) {
		var curleft = curtop = 0;
		if (obj.offsetParent) {
			curleft = obj.offsetLeft
			curtop = obj.offsetTop
			while (obj = obj.offsetParent) {
				curleft += obj.offsetLeft
				curtop += obj.offsetTop
			}
		}
		return [curleft, curtop];
	}
	else {
		return [0, 0];
	}
}

var TopDivName;

function fGetDivAbsPos(obj, curs) {
	var cursor = {x:0, y:0};
	if (obj != null) {
		if (curs == null) {
			curs = {x:0, y:0};
		}
		if (curs.x == NaN) {curs.x = 0;}
		if (curs.y == NaN) {curs.y = 0;}
		x1 = parseInt(obj.style.left);
		y1 = parseInt(obj.style.top);
		if (x1 == NaN) {x1 = 0;}
		if (y1 == NaN) {y1 = 0;}
		cursor.x = curs.x + x1;
		cursor.y = curs.y + y1;
		var parent = obj.parentNode;
		
		if ((parent != null) && (parent.id != '') && (parent.id != TopDivName)) {
		
			var parCurs = fGetDivAbsPos(parent, cursor);
			if (parCurs != null) {
				cursor = parCurs;
			}
		}
	}
	return cursor;

}

var faderDirection = 0;
var fadeValue = 0;

var faderDirections = new Array();
var fadeValues = new Array();

function fnSetOpacity(obj, value) {
	if (obj != null) {
		if (value < 0.001 ) {value = 0;}
		if (value > 0.999 ) {value = 1;}
		obj.style.filter = 'alpha(opacity=' + 100*value + ')';
		obj.style.MozOpacity = value;
		obj.style.opacity = value;
		if(value == 1)
		{
			obj.style.filter = null;
		}
	}
}

function fSmoothShow(divName, delay, min, max, cur, dx) {
	if (faderDirections[divName] * dx <= 0) {
		return;
	}
	var obj = document.getElementById(divName);
	if (obj != null) {

		if (cur > 0) {
			if ((obj.style.display == 'none') || (obj.style.visibility == 'hidden')) {
				obj.style.display = '';
				obj.style.visibility = 'visible';
			}
		}
		else if ((dx <= 0) && (cur <= 0 - dx)) {
			obj.style.display = 'none';
			fadeValues[divName] = 1;

			fnSetOpacity(obj, cur);
//			fadeValues[divName] = fadeValues[divName];
			return;
		}
	
		if (((dx > 0) && (cur < max)) || ((dx < 0) && (cur > max))) {
			cur += dx;
			fnSetOpacity(obj, cur);
			fadeValues[divName] = cur;
			if (faderDirections[divName] * dx > 0) {
				setTimeout("fSmoothShow('" + divName + "', " + delay + ', ' + min +', ' + max + ', ' + cur + ', ' + dx + ')', delay);
	        }
		}
	}
}


function launchJavascript(responseText) {
	// RegExp from prototype.sonio.net
	var ScriptFragment = '(?:<script.*?>)((\n|.)*?)(?:</script>)';

	var match    = new RegExp(ScriptFragment, 'img');
	var scripts  = responseText.match(match);
	if(scripts) {
		var js = '';
		for(var s = 0; s < scripts.length; s++) {
			var match = new RegExp(ScriptFragment, 'im');
			js += scripts[s].match(match)[1];
		}
		eval(js);
	}
}
