var languages = {
	de: 'Deutsch',
	en: 'English',
	es: 'Espa&ntilde;ol',
	fr: 'Fran&ccedil;ais',
	nl: 'Nederlands',
	no: 'Norsk',
	sq: 'Shqip'
}

function createCookie(name,value,days){
        if (days)
        {
                var date = new Date();
                date.setTime(date.getTime()+(days*24*60*60*1000));
                var expires = "; expires="+date.toGMTString();
        }
        else var expires = "";
        document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name){
        var nameEQ = name + "=";
        var ca = document.cookie.split(';');
        for(var i=0;i < ca.length;i++)
        {
                var c = ca[i];
                while (c.charAt(0)==' ') c = c.substring(1,c.length);
                if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
        }
        return null;
}

function selectLanguage() {
	language = document.getElementById('language').value;
	createCookie("edt_language", language, 300);
 	window.location.reload();
}


function parseLog(txt) {
	var ok = 1;
	var fcl= '';
	var head = '';

	head += '<div id="filter">';
	head += '<select id="language" onchange="selectLanguage()" style="float:right">';
	for (i in languages) {
		head += '<option value="' + i + '"';
		if (i == language) {
			head+= ' selected="true"';
		}
		head += '>' + languages[i] + '</option>';
	}
	head += '</select>';
	head += '<h2>' + msg['filter'] + '</h2>';
	for (i in filter) {
		if (i > cf.length-1) {
			cf+=1;
		}
		head += '<div class="l' + i + '" style="display:block" >';
		head += '<input type="checkbox" id="l' + i + '" onclick="toggle(' + i + ', this.checked)"';
		if (cf.charAt(i) == '1') {
		 	head += 'checked="checked" ';
			changecss('.l' + i,'display','block'); 
		} else {
			changecss('.l' + i,'display','none'); 
		}
		head += ' />';
		head += '<label for="l' + i + '">' + filter[i] + '</label></div>';
	}
	head += '<br />';
	head += '</div>';
	head += '<div id="log"><img src="../../admin/logo.png" /><h1>' + msg['titel'] + '</h1><h2>' + msg['logfile'] + '</h2>';
	head += '<table>';
	var num = 1;
	var ret = '';
	txt = txt.replace(/\n\r/g, '\n');
	txt = txt.replace(/\|/g, '<br>');
	txt = txt.replace(/\r/g, '');
	var a = txt.split('\n');
	//alert(a.length);
	for (i=0; i<a.length; i++) {
		try {	
		var b = a[i].split(';');
		if (b.length > 0) {
			fcl = 'l' + fclass[b[0]];
			switch (b[0]) {
				case '':
				case 'language':
					continue;
				case 'storing':
					for (j=0; j < b[1].length; j++) {
						foo = b[1].charCodeAt(j);
						if (foo < 128) {
							ok += foo*7;
						}
					}
					storing = parseInt(b[1]);
					if (storingen[b[1]]) {
						head += '<tr><th>' + msg[b[0]] + ':</th><td>' + storingen[b[1]].name + '</td></tr>';
					} else {
						head += '<tr><th>' + msg[b[0]] + ':</th><td>' + b[1] + '</td></tr>';
					}
					break;
				case 'niveau':
					head += '<tr><th>' + msg[b[0]] + ':</th><td>' + b[1] + '</td></tr>';
					break;
				case 'cursist':
					for (j =0; j < b[1].length; j++) {
						foo = b[1].charCodeAt(j);
						if (foo < 128) {
							ok += foo;
						}
					}
					head += '<tr><th>' + msg[b[0]] + ':</th><td>' + b[1] + '</td></tr>';
					break;
				case 'opgelost':
				case 'nietopgelost':
					head += '<tr><th>' + msg['actie'] + ':</th><td>' + msg[b[0]] + '</td></tr>';
					break;
				case 'begin':
				case 'einde':
					head += '<tr><th>' + msg[b[0]] + ':</th><td>' + b[1] + '</td></tr>';
					break;
				case 'vervangen':
					head += '<tr><th>' + msg['actie'] + ':</th><td>' +  msg[b[0]] + vervangen[b[1]] + '</td></tr>';
					break;
				case 'diagnose':
					head += '<tr><th>' + msg[b[0]] + ':</th><td>';
					for (j = 1; j < b.length; j++) {
						b[j] = b[j].replace(/(>)/g, '&gt;');
						b[j] = b[j].replace(/(<)/g, '&lt;');
						b[j] = b[j].replace(/(&lt;br&gt;)/g, '<br />');
						b[j] = b[j].replace(/(&lt;BR&gt;)/g, '<br />');
						head += b[j];
					}
					head += '</td></tr>';
					break;
				case 'beoordeling':
					head += '<tr><th>' + msg[b[0]] + ':</th><td>';
					if (parseInt(b[1]) == ok) {
						head += '<b>' + msg['goed'] + '</b><br />';
					} else {
						head += '<b>' + msg['fout'] + '</b><br />';
					}

					for(j = 2; j< b.length; j++) {
						var c =  b[j].split('_');
						if (c.length > 1) {
							head += logArgument(c[0]).replace(/(%2)/, c[1]) + '<br />';
						} else {
							head += logArgument(b[j]) + '<br />';
						}
					}
					head += '</td></tr>';
					break;
				case 'duur':
					head += '<tr><th>' + msg[b[0]] + ':</th><td>' + b[1] + ' ' + msg['minuten'] + '</td></tr>';
					break;
				default:
					ret += '<div class="' + fcl + '">' + num + '. ';
					if (b.length > 1) {
						if (msg[b[1]] != undefined) {
							b[1] = msg[b[1]];
							for (j = 2; j < b.length; j++) {
								b[1] = b[1].replace(/(%s)/, b[j]);
							}
						}
						b[0] = msg[b[0]];
							for (j = 1; j < b.length; j++) {
								b[0] = b[0].replace(/(%s)/, logArgument(b[j]));
							}
					} else {
						if (msg[b[0]] != undefined) {
							b[0] = msg[b[0]];
						} else {
							b[0] = '<b>' + b[0] + '</b> (undefined)';
						}
					}
//					b[0] = b[0].replace(/(°)/g, '&deg;');
					b[0] = b[0].replace(/(%1)/g, '<br />');
					ret += b[0];
					ret += '</div>\n';
					num++;
/*					
					switch (b.length) {
						case 4:
							ret += msg[b[0]] + ' -- ' + logArgument(b[1]) + '-- ' + logArgument(b[2]) + ' -- ' + logArgument(b[3]) + '</div>\n';
							break;
						case 3:
							ret += msg[b[0]] + ' -- ' + logArgument(b[1]) + ' -- ' + logArgument(b[2]) + '</div>\n';
							break;
						case 2:
							ret += msg[b[0]] + ' -- ' + logArgument(b[1]) + '</div>\n';
							break;
						case 1:
							ret += msg[b[0]] + '</div>\n';
							break;
					}
*/
			}
		}
		} catch (e) {
//			alert (e + '\n' + a);
		}

	} 
	head += '</table>';
	ret += '</div>';
	
	return head + ret;
}

function logArgument(a) {
	if (obj[a] == undefined) {
		if (msg[a] == undefined) {
			//a = escape(a);
			return a;
		} else {
			return msg[a];
		}
	} else {
		return obj[a];
	}
}

function toggle (n, val) {
	if (val) {
		cf = cf.substr(0,n) + '1' + cf.substr(n+1);
		changecss('.l' + n,'display','block'); 
	} else {
		cf = cf.substr(0,n) + '0' + cf.substr(n+1);
		changecss('.l' + n,'display','none');
	}
	createCookie('edt_filter', cf, 100);
}

function changecss(theclass,element,value) { 
	var cssRules; 
	if (document.all) { 
		cssRules = 'rules'; 
	} else if (document.getElementById) { 
		cssRules = 'cssRules'; 
	} 
	for (var S = 0; S < document.styleSheets.length; S++){ 
		for (var R = 0; R < document.styleSheets[S][cssRules].length; R++) { 
			if (document.styleSheets[S][cssRules][R].selectorText == theclass) { 
				document.styleSheets[S][cssRules][R].style[element] = value; 
			} 
		} 
	} 
}


function jsload() {
	var div = document.createElement('div');
	div.innerHTML = parseLog(document.getElementById('log').innerHTML);
	document.getElementsByTagName('body')[0].appendChild(div);
	document.title = "EDT - Electude " + msg['titel'];
}

function init() {
	var script = document.createElement('script');
	script.type = 'text/javascript';
	if (language == null) {
		m = /language;([A-Z]{2})/g.exec(document.getElementById('log').innerHTML);
		if (m) {
			language = m[1];
		}
	}
	if (languages[language] == undefined) {
		language = 'en';
	}
	script.src = '../../admin/' + language + '.js';
	document.getElementsByTagName('head')[0].appendChild(script);
	window.setTimeout('jsload()', 40);
}

var fclass = {	
	wizard: 14,
	
	// gevolgschade
	backfire: 0,
	overstroom: 0,
	overspanning: 0, 
	ecudefect: 0, 
	
	// handmatige acties
	los: 1,
	vast: 1,
	filt_overbrug: 1,
	filt_aangesloten: 1,
	bekijk: 1,
	bots_reset: 1,
	
	// menu
	demonteer: 6,
	monteer: 6,
	knip: 7,
	tik: 8,
	luister: 9,
	reinig: 10,
	startpilot: 10,
	duw: 11,
	bijvullen: 12,
	vervang: 13,
	
	// gereedschappen
	mul_st: 4,
	mul_bus: 4,
	mul_on: 4,
	mul_off: 4,
	mul_DC: 4,
	mul_AC: 4,
	obd:4,
	dyndraai: 1,
	slot: 3,
	gasklep: 3,
	
	// onderdelen en overige gereedschappen
	voegtoe: 5,
	voegtoe2: 5,
	verwijder: 5,
	
	// draden verbinden en verbreken
	verbreek: 2,
	verbind: 2
}


var storing;
var cf = readCookie('edt_filter');
if (cf == null) {
	cf = '1';
}

var language = readCookie('edt_language');

with (document) {
	open();
 	//write('<script type="text/javascri'+'pt" src="../../admin/lang.js"></script>');
 	write('<link rel="stylesheet" type="text/css" href="../../admin/style.css" />');
 	write('<!--[if IE]><link rel="stylesheet" type="text/css" href="../../admin/ie.css" /><![endif]-->');
 	close();
}
window.onload=init;
