/*

	Wer'e gonna handle all XML parse functions in this library.
	
	Developed by: 	Marcel Jepma
					Jepma Internet Services.

*/
var xmlDoc;
var req;
var xmlContent;
var loaderVisible = '0';
var timeout = 2;
var lastXML = '';
var lastXML2 = '';
var lastXML3 = '';		// History 2
var lastXML4 = '';		// History 3
var lastXML5 = '';		// History 4


function processReqChange() 
{
    // only if req shows "complete"

    if (req.readyState == 4) {
        // only if "OK"

        if (req.status == 200) {
			xmlContent = req.responseText;			// Haal de XML op uit de request.
			parseXML(xmlContent);					// Parse de XML output.
			
        } else {
            alert("There was a problem retrieving the XML data:\n" + req.statusText);
        }
		
    }
	//loadingHider();
}

function parseXML(xmlContent){
	if(xmlContent != ''){
		if(lastXML != '') lastXML2 = lastXML;
		lastXML = xmlContent;
	}
	//lastXML2 = xmlContent;					// Ze de laatste XML output in een variabele, deze hebben we later nodig.

	// code for IE
	if (window.ActiveXObject)
	{
		xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.async=false;
		xmlDoc.loadXML(xmlContent);
	}
	
	// code for Mozilla, Firefox, Opera, etc.
	else if (document.implementation && document.implementation.createDocument)
	{
		var parser=new DOMParser();
		var xmlDoc=parser.parseFromString(xmlContent,"text/xml");
	}
	else
	{
		alert('Your browser cannot handle this script');
	}
	
	var x=xmlDoc.documentElement;
	
	count = x.getElementsByTagName('item').length -1;
	if(count != -1){
	
		for(i=0;i<=count;i++){
			var getContent = '-';
			var getUrl 	   = '-';
			var styleInput	   = '-';

			var getContent 	= xmlDoc.getElementsByTagName("content")[i].childNodes[0].nodeValue;
			var getUrl 		= xmlDoc.getElementsByTagName("url")[i].childNodes[0].nodeValue;
	
			if(getUrl != '-'){
				new Ajax.Updater(xmlDoc.getElementsByTagName("divID")[i].childNodes[0].nodeValue, getUrl, {method: 'get',evalScripts: true});
			}else{
				if(getContent != '-'){
					$(xmlDoc.getElementsByTagName("divID")[i].childNodes[0].nodeValue).innerHTML = getContent;
				}
			}
	
			// We gaan nu kijken of we eventueel moeten stylen:
			var styleInput=xmlDoc.getElementsByTagName("style")[i].childNodes[0].nodeValue;	
	
			if(styleInput != '-'){
				var styleInput_Array=styleInput.split(";");
				for(j=0;j<=styleInput_Array.length;j++){
					if(styleInput_Array[j] != '' && isdefined(styleInput_Array,j) == true){
						// We gaan hem nu splitten op type & dan waarde:
						var divObject = $(xmlDoc.getElementsByTagName("divID")[i].childNodes[0].nodeValue);
						styleStage2_Array = styleInput_Array[j].split(":");
	
						if(styleStage2_Array[0] == 'display'){divObject.style.display = styleStage2_Array[1];}
						if(styleStage2_Array[0] == 'width'){divObject.style.width = styleStage2_Array[1];}
						if(styleStage2_Array[0] == 'height'){divObject.style.height = styleStage2_Array[1];}
						if(styleStage2_Array[0] == 'border'){divObject.style.border = styleStage2_Array[1];}
						if(styleStage2_Array[0] == 'padding'){divObject.style.padding = styleStage2_Array[1];}
						if(styleStage2_Array[0] == 'background'){
							divObject.style.background = "url(data/header/"+styleStage2_Array[1]+")";
							divObject.style.backgroundRepeat = "no-repeat";
							divObject.style.backgroundPosition = "center";
							divObject.style.backgroundPosition = "top";
						}
					}
					
				}
			}
		}
	}
	if(chkObject(false,'loadOrderForm')){
		Element.show('loadOrderForm');
	}
}


var req;
function loadXMLDoc(url) 
{
    // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = processReqChange;
        req.open("GET", url, true);
        req.send(null);
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = processReqChange;
            req.open("GET", url, true);
            req.send();
        }
    }
}

function goBackScript(display){
	if(lastXML2 != ''){
		if(display != ' '){
			//$('GoBackDiv').innerHTML = '<a href="#" onClick="javascript:goBackScript(\' \');"><img src="images/goBack.gif" border="0"/></a>';
//			document.write('<a href="#" onClick="javascript:goBackScript(\'JA\');"><img src="images/goBack.gif"/></a>');
		}else{
			parseXML(lastXML2);	
			lastXML2 = '';
		}
	}
}

function parsePage(URL)
{
	// We gaan loading bar laten zien....
	//if(loading != 0){loader();}
//	alert('Loading....'+URL);
	// We gaan de XML file inladen in het geheugen: (xmlContent)
	loadXMLDoc(URL);
	
	// Loading bar weghalen.
	//if(loading != 0){loader();}
}
function loadPage(page){
	$('innerContent').innerHTML = '<div class="loadingImage" style="height:' + $('innerContent').style.height + '"><div class="loadingImage2"><img src="images/loading.gif" style="#position: relative;  #top: -50%" /></div></div>';
	
	parsePage(page);
}