/*****************************************************************************************
    login.js
	
	by: Martin Mohring for Webmakers.de
	date: 13/08/2008
	
	based on:
		- Script to import XML data files and make them available to JavaScript 
			v2.0.6 written by Mark Wilton-Jones, 13/04/2004
		
					 
******************************************************************************************/


//section: import XML

var MWJ_ldD = [];

function importXML( oURL, oFunct, oNoRand, oDelay ) {
	
	//note: in XML importing event handlers, 'this' refers to window
	if( !oNoRand ) { oURL += ( ( oURL.indexOf('?') + 1 ) ? '&' : '?' ) + ( new Date() ).getTime(); } //prevent cache
	if( window.XMLHttpRequest ) {
		//alternate XMLHTTP request - Gecko, Safari 1.2+ and Opera 7.6+
		MWJ_ldD[MWJ_ldD.length] = new XMLHttpRequest();
		MWJ_ldD[MWJ_ldD.length-1].onreadystatechange = new Function( 'if( MWJ_ldD['+(MWJ_ldD.length-1)+'].readyState == 4 && MWJ_ldD['+(MWJ_ldD.length-1)+'].status < 300 ) { '+oFunct+'(MWJ_ldD['+(MWJ_ldD.length-1)+'].responseXML); }' );
		MWJ_ldD[MWJ_ldD.length-1].open("GET", oURL, true);
		MWJ_ldD[MWJ_ldD.length-1].send(null);
		return true;
	}
	if( !navigator.__ice_version && window.ActiveXObject ) {
		//the Microsoft way - IE 5+/Win (ICE produces errors and fails to use try-catch correctly)
		var activexlist = ['Microsoft.XMLHTTP','Microsoft.XMLDOM'], tho; //add extra progids if you need specifics
		for( var i = 0; !tho && i < activexlist.length; i++ ) {
			try { tho = new ActiveXObject( activexlist[i] ); } catch(e) {}
		}
		if( tho ) {
			MWJ_ldD[MWJ_ldD.length] = tho;
			MWJ_ldD[MWJ_ldD.length-1].onreadystatechange = new Function( 'if( MWJ_ldD['+(MWJ_ldD.length-1)+'].readyState == 4 ) { '+oFunct+'(MWJ_ldD['+(MWJ_ldD.length-1)+'].load?MWJ_ldD['+(MWJ_ldD.length-1)+']:MWJ_ldD['+(MWJ_ldD.length-1)+'].responseXML); }' );
			if( MWJ_ldD[MWJ_ldD.length-1].load ) {
				MWJ_ldD[MWJ_ldD.length-1].load(oURL);
			} else {
				MWJ_ldD[MWJ_ldD.length-1].open('GET', oURL, true);
				MWJ_ldD[MWJ_ldD.length-1].send(null);
			}
			return true;
		}
	}
	if( document.createElement && document.childNodes ) {
		//load the XML in an iframe
		var ifr = document.createElement('DIV');
		ifr.style.visibility = 'hidden'; ifr.style.position = 'absolute'; ifr.style.top = '0px'; ifr.style.left = '0px';
		//onload only fires in Opera so I use a timer for all
		if( !window.MWJ_XML_timer ) { window.MWJ_XML_timer = window.setInterval('MWJ_checkXMLLoad();',100); }
		ifr.innerHTML = '<iframe src="'+oURL+'" name="MWJ_XML_loader_'+MWJ_ldD.length+'" height="0" width="0"><\/iframe>';
		MWJ_ldD[MWJ_ldD.length] = oFunct+'MWJ_SPLIT'+(oDelay?oDelay:1)+'';
		document.body.appendChild(ifr);
		return true;
	}
	return false;
}

function MWJ_checkXMLLoad() {
	//check if each imported file is available (huge files may not have loaded completely - nothing I can do - use the delay to help)
	for( var x = 0; x < MWJ_ldD.length; x++ ) { if( MWJ_ldD[x] && window.frames['MWJ_XML_loader_'+x] ) {
		setTimeout( MWJ_ldD[x].split('MWJ_SPLIT')[0] + '(window.frames.MWJ_XML_loader_'+x+'.window.document);', parseInt(MWJ_ldD[x].split('MWJ_SPLIT')[1]) );
		MWJ_ldD[x] = false;
	} }
}


// section login

function checkCustomer(xmlDoc){
	var inputData = document.getElementById('Kunde');
	
	if (inputData.value !=''){
		var x = xmlDoc.getElementsByTagName('kunde');					
		var found=false;
	
		for (i=0;i<x.length;i++) {			
			if (found==true) break;
			for (j=0;j<x[i].childNodes.length;j++) {				
				if (x[i].childNodes[j].nodeType != 1) continue;								
				var theData = x[i].childNodes[j].firstChild.nodeValue;					
				if (inputData.value==theData){
					found=true;					
					document.getElementById('Kundenbereich').innerHTML = '<iframe src="kunde/'+theData+'" frameborder=0 name="Kundenbereich" height="600" width="600"><\/iframe>';						
					document.getElementById('Kundenbereich').style.visibility='visible';
									
					break;
				}					
			}						
		}
		if (!found){
			alert(unescape("Kundennummer ung%FCltig."));
		}
	}
	else{
		alert("Bitte geben Sie Ihre Kundennummer ein.");
	}
}

// section filelisting
var fileArray = [];
function loadFiles(xmlDoc){		
	var x = xmlDoc.getElementsByTagName('datei');
	for (i=0;i<x.length;i++) {	
		var tmp = x[i].firstChild.nodeValue;		
		fileArray[i] = 	tmp;
	}
	fileArray.sort(mySortingA);
	createTable();
}

function getCaption(tmp){
	var tmpCaption="";
	var index = tmp.lastIndexOf("/");
	if (index != -1) {
		tmpCaption = tmp.substr(0,index);
	}
	return tmpCaption;
}

function getFilename(tmp){
	var filename;
	var i = tmp.lastIndexOf("/");
	var ii = tmp.lastIndexOf(".");
	if (i != -1) {
		filename = tmp.substr(i+1, ii-i-1 );
	} else{
		filename = tmp.substr(0, ii-i-1 );
	}
	return filename;
}

function getFiletyp(tmp){
	var filetyp;
	var i = tmp.lastIndexOf(".");
	return tmp.substr(tmp.lastIndexOf(".")+1);
}

function createTable(){	
	var src = "<p>Die angezeigten Dateien lassen sich durch Klicken auf den jeweiligen Tabellenkopf (Name oder Typ) sortieren.</p><div id='accordion'><dl class='accordion' id='slider'>";
	
	var tmp;
	var count = 0;
	while(tmp = fileArray.shift()){
		var tmpCaption = getCaption(tmp);		
		var tableArray = [];
		tableArray.push(tmp);
		for (i=0; i<fileArray.length; i++)
		{
			if (getCaption(fileArray[i]) == tmpCaption){
				tableArray.push(fileArray[i]);
				fileArray.splice(i,1);
				i--;
			}
		}		
		tableArray.sort();
		src += createTableHeader(tmpCaption);
		
		var idCount=0;
		while (file=tableArray.shift()){
			idCount++;
			src += createTableRow(idCount,file);			
		}
		tmpCaption = "";
		src += createTableFooter();
		count++;
		
	}	
	
	document.body.innerHTML = src + document.body.innerHTML + "</dl></div>";
	SortTable.init();
	slider1=new accordion.slider("slider1");
	slider1.init("slider",0,"open"); 
}

var slider1;

function createTableHeader(caption){
	var src;
	if (caption==""){caption="Hauptverzeichnis";}
	src = "<dt>" + caption + "</dt><dd>"
		+ "<table class='sortable'>" 		
		+ "<thead>"	
		+	"<tr>"
		+		"<th class='no_sort'>ID</th>"
		+		"<th>Name</th>"
		+		"<th>Typ</th>"
		+		"<th class='no_sort'>Download</th>"
		+	"</tr>"
		+"</thead>"
		+"<tbody>";
	return src;
}

function createTableRow(id,file){
	var src;
	
	src = "<tr>"
		+		"<td>" + id + "</td>"
		+		"<td>" + getFilename(file) + "</td>"
		+		"<td>" + getFiletyp(file).toUpperCase() + "</td>"
		+		"<td><a href='"+ file +"' target='_blanc'>Download</a></td>"
		+	"</tr>";
	return src;
}

function createTableFooter(){
	var src;
	
	src =  "</tbody>"
		+ "</table></dd>"
	return src;
}

// sort-compare-functions

function byIndex0(x,y)
{
	if(x[0]<y[0]){res=-1} else if(y[0]<x[0]){res=1} else{res=0}
	return(res);
}
function byIndex1(x,y)
{
	if(x[1]<y[1]){res=-1} else if(y[1]<x[1]){res=1} else{res=0}
	return(res);
}

function mySortingA(a,b) {
a = a[0]+a[1]+a[2]+a[3];
b = b[0]+b[1]+b[2]+b[3];
return a == b ? 0 : (a < b ? 1 : -1)
}


var accordion=function(){
	var tm=sp=10;
	function slider(n){this.nm=n; this.arr=[]}
	slider.prototype.init=function(t,c,k){
		var a,h,s,l,i; a=document.getElementById(t); this.sl=k?k:'';
		h=a.getElementsByTagName('dt'); s=a.getElementsByTagName('dd'); this.l=h.length;
		for(i=0;i<this.l;i++){var d=h[i]; this.arr[i]=d; d.onclick=new Function(this.nm+'.pro(this)'); if(c==i){d.className=this.sl}}
		l=s.length;
		for(i=0;i<l;i++){var d=s[i]; d.mh=d.offsetHeight; if(c!=i){d.style.height=0; d.style.display='none'}}
	}
	slider.prototype.pro=function(d){
		for(var i=0;i<this.l;i++){
			var h=this.arr[i], s=h.nextSibling; s=s.nodeType!=1?s.nextSibling:s; clearInterval(s.tm);
			if(h==d&&s.style.display=='none'){s.style.display=''; su(s,1); h.className=this.sl}
			else if(s.style.display==''){su(s,-1); h.className=''}
		}
	}
	function su(c,f){c.tm=setInterval(function(){sl(c,f)},tm)}
	function sl(c,f){
		var h=c.offsetHeight, m=c.mh, d=f==1?m-h:h; c.style.height=h+(Math.ceil(d/sp)*f)+'px';
		c.style.opacity=h/m; c.style.filter='alpha(opacity='+h*100/m+')';
		if(f==1&&h>=m){clearInterval(c.tm)}else if(f!=1&&h==1){c.style.display='none'; clearInterval(c.tm)}
	}
	return{slider:slider}
}();