
/*

//TODO: Implement automatic event attaching with prevention for duplicate
//		Something like this \/

var XelynActivated = false;
AttachXelynInit();

function AttachXelynInit()
{
	alert( 'XelynActivated = ' + XelynActivated );
	
	if( !XelynActivated )
	{
		window.attachEvent( "onload", XelynInit );
		XelynActivated = true
	}
}
*/


// Allow for firefox logging statements, even in browsers that don't support it!
if (!("console" in window) || !("firebug" in console))
{
    var names = ["log", "debug", "info", "warn", "error", "assert", "dir", "dirxml",
    "group", "groupEnd", "time", "timeEnd", "count", "trace", "profile", "profileEnd"];

    window.console = {};
    for (var i = 0; i < names.length; ++i)
        window.console[names[i]] = function() {}
}


function XelynInit( e )
{
	if( typeof listInit != 'undefined' )
		listInit();

	if( typeof logboekInit != 'undefined' )
		logboekInit();

	if( typeof pageInit != 'undefined' )
		pageInit();

	if( typeof initInterface != 'undefined' )
		initInterface( 'content' );

	if( typeof parent.initInterface != 'undefined' )
		parent.initInterface( 'content' );

	// scriptInit( e );


	// Catch all exceptions generated from init script to
	// ensure correct rendering of the page
	try
	{
		if( typeof scriptInit != 'undefined' )
		{
			scriptInit( e );
		}
	}
	catch( ex )
	{
		alert( 'Error in scriptInit: ' + ex );
	}


	// if( true )
	if( typeof correctPNG != 'undefined' )
		correctPNG();
};


var showHideTimer = null;

// Displays or hides an element
function showHide( objName, bShow )
{
  var fObj = getElement( objName );

	if( fObj == null )
		return false;

	if( bShow == true )
	{
		fObj.style.display="";
		fObj.style.visibility="visible";
	}
	else
	{
		fObj.style.display="none";
		fObj.style.visibility="hidden";
	}

	return true;
};

function isVisible( target )
{
	var fObj = getElement( target );

	if( fObj == null )
		return false;

	if( fObj.style.visibility != "hidden" )
		return true;
	else
		return false;
};

function doAction( action )
{
	switch( action )
	{
		case "Save":
			if( typeof Save != 'undefined' ) Save();
			break;
		case "Print":
			if( typeof Print != 'undefined' ) Print();
			break;
		default:
			alert( "Action: '" + action + "' does not exist." );
	}
};

function checkAction( action )
{
	switch( action )
	{
		case "Save":
			if( typeof Save != 'undefined' ) return true;
		case "Print":
			if( typeof Print != 'undefined' ) return true;
		default:
			return false;
	}
	return false;
};

function validateEMailAddress( email )
{
	return email.match( "^([0-9a-zA-Z]+[-._+&])*[0-9a-zA-Z]+@([-0-9a-zA-Z]+[.])+[a-zA-Z]{2,6}$" );
};

// Displays or hides an element
function showHideLater( objName, bShow, waitTime )
{
	setTimeout( "showHide( '" + objName + "', " + bShow + " )", waitTime );

	return true;
};

// Sets the active CSS class for object
function setClass( target, newClass )
{
	var qObj = getElement( target );

	if( qObj != null )
	{
		qObj.className = newClass;
		return true;
	}
	else
	{
		return false;
	}
};

// Displays an invisible object and hides a visible one
function toggleDisplay( objName )
{
  var fObj = getElement( objName );

	if( fObj == null )
		return false;

	if( fObj.style.visibility != "hidden" )
	{
		fObj.style.display="none";
		fObj.style.visibility="hidden";
	}
	else
	{
		fObj.style.display="";
		fObj.style.visibility="visible";
	}
};

// Returns one element in a browser-independent way
function getElement( objName )
{
  return (typeof document.getElementById != "undefined") ? document.getElementById(objName) : document.all[objName];
};

function blinkObject( objName )
{
	var fObj = getElement( objName );

	if( fObj == null || fObj.className == "" )
		return;

	var curClass = "" + fObj.className;

	if( curClass.substr( curClass.length - 5 ) == "Blink" )
	{
		fObj.className = curClass.substr( 0, fObj.className.length - 5 );
		return true;
	}
	else
	{
		fObj.className = curClass + "Blink";
		return false;
	}
};

/*** Add & Remove event handlers, browser safe ***
 *
 ** useCapture
 *
 * As to the true or false that is the last argument
 * of addEventListener, it is meant to state whether
 * the event handler should be executed in the
 * capturing or in the bubbling phase. If you’re
 * not certain whether you want capturing or bubbling,
 * use false (bubbling).
 */
function addEvent(obj, evType, fn, useCapture)
{
  if (obj.addEventListener){
    obj.addEventListener(evType, fn, useCapture);
    return true;
  } else if (obj.attachEvent){
    var r = obj.attachEvent("on"+evType, fn);
    return r;
  } else {
    alert("Handler could not be attached");
  }
};

function removeEvent(obj, evType, fn, useCapture)
{
  if (obj.removeEventListener){
    obj.removeEventListener(evType, fn, useCapture);
    return true;
  } else if (obj.detachEvent){
    var r = obj.detachEvent("on"+evType, fn);
    return r;
  } else {
    alert("Handler could not be removed");
  }
};




var blinkTimerObj;

function unblinkObject( objName )
{
	var fObj = getElement( objName );

	if( fObj == null )
		return false;

	if( fObj.className.substr( fObj.className.length - 5 ) == "Blink" )
		fObj.className = fObj.className.substr( 0, fObj.className.length - 5 );

	if( blinkTimerObj != null )
		clearTimeout( blinkTimerObj );

	return true;
};

function blinkTimer( objName, count, max )
{
	var fObj = getElement( objName );

	if( fObj == null )
		return;

	if( blinkObject( objName ) )
		count++;

	if( count < max )
		blinkTimerObj = setTimeout( "blinkTimer( '" + objName + "', " + count + ", " + max + "  )", 300);
	else
		unblinkObject( objName );
};

function showPopUp( url )
{
	newwindow=window.open(url,'Xelyn','height=600,width=800,scrollbars=1,resizable=1');
	if (window.focus) {newwindow.focus()}
	return false;
};

var uploadwindow = null;
function showUploadWindow( uid )
{
	if( uploadwindow != null && uploadwindow.focus )
		uploadwindow.focus();
	else
		uploadwindow = null;	// Window is closed or just gone

	if( uploadwindow == null )
		uploadwindow = window.open('XelynWebFiles/Templates/Upload.aspx?uid=' + uid,'Xelyn','height=600,width=800,scrollbars=1,resizable=1');
	
	return false;
};

function showUploadWindowModal( uid )
{
	if( typeof xShowModalDialog != 'undefined' )
	{
		var uploadurl = 'XelynWebFiles/Templates/Upload.aspx?uid=' + uid;
	
		xShowModalDialog(uploadurl,window,"dialogWidth:800px; dialogHeight:600px;help:0;status:0;resizeable:1;scrollbars=0;", showUploadWindowModalClose );
	}
	else
	{
		alert( 'Using window.open :S' );
		showUploadWindow( uid );
	}
};

function showUploadWindowModalClose( value )
{
	var obj = getElement( 'refreshButton' );

	if( obj && obj.click )
		obj.click();
	else if( obj && obj.onclick )
		obj.onclick();
	else
		alert( 'Could not refresh list, select the node again to refresh manually' );
};

// Mozilla compatible function to replace a selection
/*
function replaceSelection_Gecko(o, s)
{
	var s2 = o.value;
	o.value = s2.substring(0, o.selectionStart)
	+ s + s2.substr(o.selectionEnd + 1);
};
*/

function getWindowHeight( )
{
	return getWindowSize( 'y' );
};

function getWindowWidth( )
{
	return getWindowSize( 'x' );
};

function getWindowSize( axis )
{
	var myWidth = 0, myHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}
	
	/*
	alert( 'Width = ' + myWidth );
	alert( 'Height = ' + myHeight );
	*/


	if( axis == 'y' )
		return myHeight;
	else if( axis == 'x' )
		return myWidth;
	else
		return null;
	
  
};


var op5 = false;	// Sorry Opera users

function getElementHeight(Elem) {
	var obj = getElement( Elem );

	if (op5) { 
		xPos = obj.style.pixelHeight;
	} else {
		xPos = obj.offsetHeight;
	}
	return xPos;
};

function getElementWidth(Elem) {
	var obj = getElement( Elem );

	if (op5) {
		xPos = obj.style.pixelWidth;
	} else {
		xPos = obj.offsetWidth;
	}
	return xPos;
};

function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft;
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;

	return curleft;
};

function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop;
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
};

function getQueryVariable(variable)
{
  var query = window.location.search.substring(1);
  var vars = query.split("&");

  for ( var i=0; i<vars.length; i++ )
  {
    var pair = vars[i].split( "=" );

    if (pair[0] == variable)
      return pair[1];
  } 
  // alert('Query Variable ' + variable + ' not found');
};

function goto( target )
{
	if( target.length > 0 )
		window.location = target;
};

function gotoOnNotChanged( target )
{
	if( typeof this.edit != 'undefined' )
		if( this.edit ) return false;

	if( typeof parent != 'undefined' )
	{ 
		if( typeof parent.edit != 'undefined' )
			if( parent.parent.edit ) return false;

		if( typeof parent.parent != 'undefined' && typeof parent.parent.edit != 'undefined' )
			if( parent.parent.edit ) return false;
	}
	
	document.location = target;
};

function clickButton(e, buttonid)
{
	var bt = getElement(buttonid);

	if ( e.keyCode == 13 && bt != null )
	{ 
		if( bt.click )
			bt.click();
		else if( bt.onclick )
			bt.onclick();

		return false; 
	}
	
	/*
      if (typeof bt == 'object')
      {
            // if(navigator.appName.indexOf("Netscape")>(-1)){ 
            if( isIE )
            {
                  if (e.keyCode == 13){ 
                        bt.click(); 
                        return false; 
                  } 
            } 
            // if (navigator.appName.indexOf("Microsoft Internet Explorer")>(-1)){ 
            else 
            {
                  if (event.keyCode == 13){ 
                        bt.click(); 
                        return false; 
                  } 
            } 
      }
	*/
};

function GetXmlHttpObject(handler)
{
	var objXmlHttp = null;

	// This check is for IE7, Mozilla, Safari, Opera,...
	if( window.XMLHttpRequest )
	{
		try
		{
			objXmlHttp = new XMLHttpRequest();
			objXmlHttp.onload = handler;
			objXmlHttp.onerror = handler;
			objXmlHttp.onreadystatechange = handler;
		   
		   // alert( 'Using native version!' );
			/*
			 * Since we're not using XML, this is not needed (and will break our script!)
			 *
			if (objXmlHttp.overrideMimeType)
			{
				objXmlHttp.overrideMimeType('text/xml');
			}
			*/
		}
		catch (e)
		{
			objXmlHttp = false;
		}
	}
	else if ( window.ActiveXObject )	// For Internet Explorer
	{
		var types = [
			'Microsoft.XMLHTTP',
			'MSXML2.XMLHTTP.5.0',
			'MSXML2.XMLHTTP.4.0',
			'MSXML2.XMLHTTP.3.0',
			'MSXML2.XMLHTTP'
		];
	
		for (var i = 0; i < types.length; i++)
		{
			try {
				objXmlHttp = new ActiveXObject(types[i]);
				objXmlHttp.onreadystatechange = handler;
				break;
			} catch(e) {}
		}
	}

	if( typeof objXmlHttp == 'boolean' || objXmlHttp == null )
	{
		// alert( 'Error initialising XML RPC system, please update to a newer browser version' );
		return null;
	}

	return objXmlHttp;
};

function GetXmlHttpObject2(handler)
{
	var objXmlHttp = null;

	/*
	if( navigator.userAgent.indexOf("Opera") >= 0 )
	{
		alert("This feature doesn't work in Opera");
		return null;
	}
	*/
	if ( navigator.userAgent.indexOf("MSIE") >= 0 )
	{
		try
		{ 
			objXmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			objXmlHttp.onreadystatechange = handler;
			return objXmlHttp;
		}
		catch(e)
		{ 
			alert("Error. Scripting for ActiveX might be disabled: " + e );
			return null;
		}
	}
	else // if (navigator.userAgent.indexOf("Mozilla")>=0)
	{
		objXmlHttp = new XMLHttpRequest();
		objXmlHttp.onload = handler;
		objXmlHttp.onerror = handler;
		return objXmlHttp;
	}
};

function getBrowserLanguage()
{
	var lang = 'en';
	
	if (navigator.userLanguage) // Explorer
		lang = navigator.userLanguage;
	else if (navigator.language) // FF
		lang = navigator.language;

	return lang;
}

// Function to correct the disability of IE to show semi-transparent PNG's
function correctPNG()
{
	return;

/*
	if (window.navigator.appVersion.indexOf("MSIE") == -1)
		return;

	for(var i=0; i<document.images.length; i++)
	{
		var img = document.images[i]
		var imgName = img.src.toUpperCase()

		if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
		{
			var imgID = (img.id) ? "id='" + img.id + "' " : "";
			var imgClass = (img.className) ? "class='" + img.className + "' " : "";
			var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' ";
			var imgStyle = "display:inline-block;" + img.style.cssText;
			var imgOnClick = (img.onclick) ? "onClick=\"" + img.onclick + "\" " : "";

			// Stupid way, but it works: auto-generated 'function anonymous()' removed:
			imgOnClick = imgOnClick.replace(/\n/gi,'');
			imgOnClick = imgOnClick.replace( /\}/gi ,'');
			imgOnClick = imgOnClick.replace(/function anonymous\(\)\{/gi,'javascript:');

			//alert( 'OnClick=' + imgOnClick );

			if (img.align == "left") imgStyle = "float:left;" + imgStyle;
			if (img.align == "right") imgStyle = "float:right;" + imgStyle;
			if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle;
			
			var strNewHTML = "<span " + imgID + imgClass + imgTitle + imgOnClick
			+ " width=\"" + img.width + "\" height=\"" + img.height + "\" "
			+ " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + "; "
			+ "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
//			+ "( src=\'" + img.src + "\', sizingMethod='scale');\"></span>";
			+ "( src=\'" + img.src.replace( /XelynWebFiles\/Images/gi, "XelynWebFiles/Images/png" ) + "\', sizingMethod='scale');\"></span>";

			img.outerHTML = strNewHTML;
			
			// alert( 'Test: ' + strNewHTML );

			i = i-1;
		}
	}
*/
};

var isIE4 = (document.all && !document.getElementById) ? true : false;
var isIE5 = (document.all && document.getElementById) ? true : false;

var isNS4 = (document.layers) ? true : false;
var isNS6 = (!document.all && document.getElementById) ? true : false;

var isIE = ( isIE4 || isIE5 );
var isNS = ( isNS4 || isNS6 );