//_SARISSA_DOM_PROGID = "Microsoft.XMLDOM";	// Deze regel weghalen zodra we niet meer van de draft-specificatie gebruik maken
xmlErrorGlobal = 0;


function ErrorSet( obj, code, error )
{
	if( typeof( obj.parseError ) == "object" )
	{
		if( typeof( Sarissa) != "undefined" )
		{
			Sarissa.customErrorCode = code;
			if( typeof( error ) == "object" )
				Sarissa.customErrorMsg = error.message;
			else
				Sarissa.customErrorMsg = error;
		}
		else
			xmlErrorGlobal = code;
	}
	else
	{
		obj.parseError = code;

		if( typeof( error ) == "object" )
			obj.errorMsg = error.message;
		else
			obj.errorMsg = error;
	}
}

function ErrorCode( obj )
{
	var code = 0;
	
	if( typeof( obj.parseError ) == "object" )
	{
		code = obj.parseError.errorCode;
		
		if( !code )
		{
			if( typeof( Sarissa) != "undefined" )
				code = Sarissa.customErrorCode;
			else
				code = xmlErrorGlobal;
		}
	}
	else
		code = obj.parseError;
	
	
	return( code );
}

function ErrorMsg( obj )
{
	var msg = "";
	var tmpmsg = "";
	
	msg += "<table border=1>";

	if( typeof( obj.parseError ) != "object" )
		msg += "<tr>" + "<td>Error code</td><td>:</td><td>" + obj.parseError + "</td></tr>";
	else if( obj.parseError.errorCode )
		msg += "<tr>" + "<td>Error code</td><td>:</td><td>" + obj.parseError.errorCode + "</td></tr>";

	if( typeof( Sarissa) != "undefined" )
	if( typeof( Sarissa.getParseErrorText( obj ) ) != "undefined" )
		msg += "<tr>" + "<td>Reason</td><td>:</td><td>" + Sarissa.getParseErrorText( obj ) + "</td></tr>";

	if( typeof( Sarissa) != "undefined" )
	if( typeof( Sarissa.customErrorMsg ) != "undefined" )
		msg += "<tr>" + "<td>Reason</td><td>:</td><td>" + Sarissa.customErrorMsg + "</td></tr>";

	if( typeof( obj.errorMsg ) != "undefined" )
		msg += "<tr>" + "<td>Reason</td><td>:</td><td>" + obj.errorMsg + "</td></tr>";
		
	if( obj.parseError.reason )
		msg += "<tr>" + "<td>Reason</td><td>:</td><td>" + obj.parseError.reason + "</td></tr>";
		
	if( obj.parseError.url )
		msg += "<tr>" + "<td>Url</td><td>:</td><td>" + obj.parseError.url + "</td></tr>";
		
	if( obj.parseError.line )
		msg += "<tr>" + "<td>Linenr</td><td>:</td><td>" + obj.parseError.line + "</td></tr>";
		
	if( obj.parseError.srcText )
		msg += "<tr>" + "<td>Line</td><td>:</td><td><pre>" + escape( obj.parseError.srcText ) + "</pre></td></tr>";
	
	msg += "</table>";
	return( msg );
}

function XMLObjFromFile( fname )
{
	var xmlobj = null;

	if( typeof( Sarissa) != "undefined" )
		xmlobj = Sarissa.getDomDocument();
	else
		xmlobj = new ActiveXObject( "Microsoft.XMLDOM" );

	xmlobj.async = false;
//document.write( "<br>" + new Date() + ": " + fname );
	xmlobj.load( fname );
//document.write( "<br>" + new Date() + ": " + fname );
	return( xmlobj );
}

function XMLObjFromString( source )
{
	var xmlobj = null;
	if( typeof( Sarissa) != "undefined" )
		xmlobj = Sarissa.getDomDocument();
	else
		xmlobj = new ActiveXObject( "Microsoft.XMLDOM" );
		
	xmlobj.async = false;
	xmlobj.loadXML( source );
	return( xmlobj );
}

function XSLTTransform( XMLobj, XSLobj )
{
	var source = "";
	
	if( XMLobj )
	if( XSLobj )
	if( !ErrorCode( XMLobj ) )
	if( !ErrorCode( XSLobj ) )
	{
		try
		{
			if (_SARISSA_IS_MOZ)
			{
				var processor = new XSLTProcessor();
				processor.importStylesheet( XSLobj );
				source += new XMLSerializer().serializeToString( processor.transformToDocument( XMLobj ) );
			}
			else
			{
				source += XMLobj.transformNode( XSLobj );
			}

			
		}
		catch( error )
		{
			ErrorSet( XMLobj, -1, error );
			ErrorSet( XSLobj, -1, error );
		}
		
		if( !ErrorCode( XMLobj ) )
		if( ErrorCode( XSLobj ) )
			ErrorSet( XMLobj, ErrorCode( XSLobj ), ErrorMsg( XSLobj ) );
			
		if( ErrorCode( XMLobj ) )
		if( !ErrorCode( XSLobj ) )
			ErrorSet( XSLobj, ErrorCode( XMLobj ), ErrorMsg( XMLobj ) );
	}

	if( ErrorCode( XMLobj ) )
		source += ErrorMsg( XMLobj );
	
	return( source );
}

function XMLFileAdd( XMLroot, XMLfile_name )
{
	if( XMLroot )
	if( !ErrorCode( XMLroot ) )
	{
		var XMLsub = XMLObjFromFile( XMLfile_name );

		if( !ErrorCode( XMLsub ) )
		{
			var root = XMLroot.documentElement;
			root.insertBefore( XMLsub.documentElement, null ); 
		}
		else
			ErrorSet( XMLroot, ErrorCode( XMLsub ), ErrorMsg( XMLsub ) );
	}

	return( !ErrorCode( XMLroot ) );
}

function XMLStringAdd( XMLroot, XMLstring )
{
	if( XMLroot )
	if( !ErrorCode( XMLroot ) )
	{
		var XMLsub = XMLObjFromString( XMLstring );

		if( !ErrorCode( XMLsub ) )
		{
			var root = XMLroot.documentElement;
			root.insertBefore( XMLsub.documentElement, null ); 
		}
		else
			ErrorSet( XMLroot, ErrorCode( XMLsub ), ErrorMsg( XMLsub ) );
	}

	return( !ErrorCode( XMLroot ) );
}

function XMLFilesCombine( XMLfile_names )
{
	var XMLroot = null;
	var objs = XMLfile_names.split( "," );
	XMLroot = XMLObjFromFile( "root.xml" );
	
	if( XMLroot )
	if( !ErrorCode( XMLroot ) )
	for( var i = 0; i < objs.length; i++ )
	{
		var XMLfile_name = objs[ i ];

		if( XMLfile_name.length )
		if( !XMLFileAdd( XMLroot, XMLfile_name ) )
			break;
	}

	return( XMLroot );
}

function XMLTagsStrip( xml, tagname )
{
	var tmpxml = "";
	
	if( xml )
	if( xml.length > 0 )
	{
		tmpxml = xml;
		tmpxml = tmpxml.replace( "<" + tagname + ">", "" );
		tmpxml = tmpxml.replace( "</" + tagname + ">", "" );
	}
	
	return( tmpxml );
}

function XMLobjXSLFileCombine( XMLobj, XSLfile_name, errorObj )
{
	var source = "";
	
	if( XMLobj )
	if( !ErrorCode( XMLobj ) )
	{

		var XSLfile_name_tag = XSLfile_name;
		var XSLfile_query = "";
		var qrypos = XSLfile_name.indexOf( "?" );
		
		if( qrypos >= 0 )
		{
			XSLfile_name_tag = XSLfile_name.substring( 0, qrypos );
			XSLfile_query = XSLfile_name.substring( qrypos );
		}
	
		var XSLfile_newname = "";
		var xmlobjs = null;
		
		if( XSLfile_name_tag.length > 0 )
			xmlobjs = XMLobj.getElementsByTagName( XSLfile_name_tag.toLowerCase() );

		if( xmlobjs )
		if( xmlobjs[ 0 ] )
			XSLfile_newname = XMLTagsStrip( xmlobjs[ 0 ].xml, XSLfile_name_tag.toLowerCase() );

		if( XSLfile_newname.length > 0 )
			XSLfile_name = XSLfile_newname + XSLfile_query;
	
		var XSLobj = XMLObjFromFile( XSLfile_name );
		
	
		
		if( ErrorCode( XSLobj ) )
		{
		
			ErrorSet( XMLobj, ErrorCode( XSLobj ), ErrorMsg( XSLobj ) );
		}
	
		if( !ErrorCode( XMLobj ) )
		{
		
			source += XSLTTransform( XMLobj, XSLobj );
		
		}
	}
	

	
	if( ErrorCode( XMLobj ) )
	if( XMLobj.documentElement )
		source += ErrorMsg( XMLobj );
	
	
	if( errorObj )
	if( typeof( errorObj ) != "undefined" )
		errorObj.error = ErrorCode( XMLobj );

	
	return( source );
}

function XSLTCombine( XMLfile_names, XSLfile_name, errorObj )
{
	var source = "";
	xmlErrorGlobal = 0;
	var XMLroot = XMLFilesCombine( XMLfile_names );
	
	source = XMLobjXSLFileCombine( XMLroot, XSLfile_name, errorObj );
	
	return( source );
}

function XSLTCombine2( XMLfile_names, XMLstring, XSLfile_name, errorObj )
{
	var source = "";
	var XMLroot = XMLFilesCombine( XMLfile_names );
	XMLStringAdd( XMLroot, XMLstring );

	if( XMLroot )
	if( !ErrorCode( XMLroot ) )
	{
		var XSLobj = XMLObjFromFile( XSLfile_name );
		
		if( ErrorCode( XSLobj ) )
			ErrorSet( XMLroot, ErrorCode( XSLobj ), ErrorMsg( XSLobj ) );

		if( !ErrorCode( XMLroot ) )
			source += XSLTTransform( XMLroot, XSLobj );
	}

	if( ErrorCode( XMLroot ) )
		source += ErrorMsg( XMLroot );

	if( errorObj )
	if( typeof( errorObj ) != "undefined" )
		errorObj.error = ErrorCode( XMLroot );

	return( source );
}

function XMLGetNodeValue( root, nodePath )
{
	var result = "";
	var uniqueNode = nodePath + "[0]";
	
	var selectedElem = root.selectNodes( uniqueNode );
	
	if( selectedElem.item(0) )
		result = selectedElem.item(0).nodeTypedValue;
	
	return( result );
}
	
function GetXMLValueFromFile( filename, itemname )
{
	var itemvalue = "";
	var XMLClientConfig = XMLObjFromFile( filename );
	
	if( !ErrorCode( XMLClientConfig ) )
	{
		if( XMLClientConfig.documentElement )
		{
			itemvalue= XMLGetNodeValue( XMLClientConfig.documentElement, itemname )
		}
	}
	return itemvalue;
}



