function parseTemplateVars(tpl, vars) {
	var glo_regex	= /@@(\w+)@@/gi;
	while(result = glo_regex.exec(tpl)) {
		var regex;
		if(typeof(regExp) != 'undefined')
			regex	 = new regExp('@@'+result+'@@');
		else {
			regex	 = false;
		}

		if(typeof(vars[result]) != 'undefined') {
			if(regex == false)
				tpl	 = tpl.replace(glo_regex, '');
			else
				tpl	 = tpl.replace(regex, vars[result]);
		} else {
			tpl	 = tpl.replace(regex, '');
		}
	}

	return tpl;
}

function getTemplateSection(tpl, section) {
	var glo_regex	= /<!-- @@(\w+)@@ -->/g;
	while(result = glo_regex.exec(tpl)) {
		var regex	 = new regExp('<!-- @@'+result+'@@ -->');

		if(typeof(section[result]) != 'undefined') {
			tpl	 = tpl.split(regex, 2);
			if(typeOf(section[result]) == 'Object') {
				return getTemplateSection(tpl[1], section[result]);
			} else {
				return tpl[1].replace(glo_regex, '');
			}
		}
	}
}

function getTpl(name) {
	var template	 = '';
	var sections	 = new Object();

	switch(name) {
	case 'stats':
		sections['STATS']				 = new Object();
		sections['STATS']['DETAILS']	 = true;

		//template	 = getTemplateSection(ajaxGetTpl('meine_daten', 'return'), sections);
		template = '\
			<div class="stats_section">\
				<div class="right" style="position:relative; top:5px; right:5px;"><a href="auktionsdetails.php?@@PHPSESSION@@&pid=@@PID@@"><img src="/templates/gfx/auc_info_mini_even.png" alt="i" /></a></div>\
				<h3>@@AUKTION@@</h3>\
				<img src="/templates/gfx/bidstats_@@COLOR@@.png" alt="@@COLOR@@" class="left" style="margin-left:5px; margin-top:10px;" />\
				<p style="padding-left:62px;">\
					@@STATUS0@@<br />\
					@@STATUS1@@<br />\
					@@STATUS2@@<br />\
					@@STATUS3@@<br />\
				</p>\
			</div>';
		break;
	case 'faq':
		sections['QA']	 = true;

		template	 = getTemplateSection(ajaxGetTpl('faq', 'return'), sections);
		break;
	default:
		;
	}

	return template;
}

// Initialisation
function init(){
	// Sidebar
	getSidebar();
}

function loadPagepart(xml, id) {
	var body	 = false;
	if(xml.split('<body').length == 2 && xml.split('</body').length == 2) {
		xml	 = '<body' + xml.split('<body')[1];

		splitted	 = xml.split('>');
		splitted.shift();

		xml	 = splitted.join('>');
		xml	 = xml.split('</body>')[0];
		body	 = true;
	}

	var vars			 = new Object();
	vars["PHPSESSION"]	 = window.phpsession;

	xml	 = parseTemplateVars(xml, vars)

	if(body == true) {
		document.getElementsByTagName('body')[0].innerHTML	 = xml;
		var innerScript	 = document.getElementsByTagName('body')[0].getElementsByTagName('script');
		for(var i=0;i<innerScript.length;i++){
			eval(innerScript[i].text);
		}
	} else {
		document.getElementById(id).innerHTML	 = xml;
	}
}

// Login
function getLoginBack() {
	document.getElementById("loginfrmreplace2").style.display	= "none";
	document.getElementById("loginfrmtag").style.display		= "block";
}

function checkLoginInput(user_id, pass_id) {

	user_id	 = (typeof(user_id) == 'undefined')?'user':user_id;
	pass_id	 = (typeof(pass_id) == 'undefined')?'passwd':pass_id;



	var object1 = document.getElementById(user_id).value;

	if(strstr(object1, "...", true)) {
		document.getElementById(user_id).value	 = "";
		document.getElementById(pass_id).value	 = "";
	}
}

// Content
function getContent(type) {
	var name		 = '';
	var	staticTpl	 = true;

	switch(type) {
	case 'about':
		name	 = 'about';
		break;
	case 'faq':
		name	 = 'faq';
		break;
	case 'howto':
		name	 = 'howto';
		break;
	default:
		name		 = 'index';
		staticTpl	 = false;
	}

	if(staticTpl == true)
		ajaxGetTpl(name, loadContent);
	else
		ajaxGetPage(name, loadContent);
}

function loadContent(xml) {
	loadPagepart(xml, 'content');
}

// Sidebar
function getSidebar() {
	ajaxGetTpl('sidebar', loadSidebar);
}

function loadSidebar(xml) {
	loadPagepart(xml, 'sidebar');
}

// Header
function getHeader() {
	ajaxGetPage('index', loadHeader);
}

function loadHeader(xml) {
	loadPagepart(xml, 'header_wrap');
	getSidebar();
}

// Message boxes
function openMsgBox(caller, status, content) {
	var id = "messagebox"+caller;
	try { closeMsgBox(id); } catch(e) {};
	/*	create box	*/
	var box_id			 = document.createAttribute('id');
	box_id.nodeValue	 = id;

	var box_class		 = document.createAttribute('class');
	box_class.nodeValue	 = 'msgbox';

	var node			 = document.createElement('div');
	node.setAttributeNode(box_id);
	node.setAttributeNode(box_class);

	/*	creatre inner	*/
	var inner_class		 = document.createAttribute('class');
	inner_class.nodeValue	 = 'inner';

	var inner_node			 = document.createElement('div');
	inner_node.setAttributeNode(inner_class);
	inner_node.innerHTML	 = content;
	if(status == 'loading') {
		inner_node.style.backgroundImage	 = 'url(/templates/gfx/msgbox_ico_status_loading.gif)';
	} else {
		inner_node.style.backgroundImage	 = 'url(/templates/gfx/msgbox_ico_status_'+status+'.png)';
	}
	
	/*	create clearer	*/
	var clear_class			 = document.createAttribute('class');
	clear_class.nodeValue	 = 'clear';

	var clear_node			 = document.createElement('div');
	clear_node.setAttributeNode(clear_class);
	clear_node.innerHTML	 = '&nbsp;';

	/*	fuse everything	*/
	node.innerHTML			 = '<a href="#" class="right close_btn" onclick="closeMsgBox(\'' + id + '\'); return false;"><span>x</span></a>';
	node.appendChild(inner_node);
	node.appendChild(clear_node);

	/*	add to helper	*/
	document.getElementById('helpers').appendChild(node);

	var offset	 = getOffset(caller);
	document.getElementById(id).style.top	 = offset[0]+'px';
	document.getElementById(id).style.left	 = offset[3]+'px';
}

function closeMsgBox(id) {
	document.getElementById('helpers').removeChild(document.getElementById(id));
}

// Windows
function openWindow(caller, title, content, style) {
	style		 = (typeof(top)=="undefined")?'':style;
	var styles	 = new Array();
	
	var get_params = style.split('&');
	for(i = 0; i < get_params.length; i++) {
		var key_value = get_params[i].split('=');
		if(key_value.length == 2) {
			styles[key_value[0]] = key_value[1];
		}
	}
	
	styles['top']	 = (typeof(styles['top'])=="undefined")?0:styles['top'];
	styles['left']	 = (typeof(styles['left'])=="undefined")?0:styles['left'];
	
	var tpl		 = false;
	var id		 = "window"+caller;
	try { closeWindow(id); } catch(e) {};
	
	if(content == 'tpl') {
		content	 = '';
		tpl		 = caller;
	}
	
	/*	create window	*/
	var window_id		 = document.createAttribute('id');
	window_id.nodeValue	 = id;

	var window_class		 = document.createAttribute('class');
	window_class.nodeValue	 = 'window';

	var node	 = document.createElement('div');
	node.setAttributeNode(window_id);
	node.setAttributeNode(window_class);

	/*	create closebtn	*/
	var close_class			 = document.createAttribute('class');
	close_class.nodeValue	 = 'right';

	var close_node			 = document.createElement('span');
	close_node.setAttributeNode(close_class);
	close_node.innerHTML	 = '<a href="#" onclick="closeWindow(\'' + id + '\'); return false;"><span>x</span></a>';

	/*	create title	*/
	var title_node			 = document.createElement('h2');
	title_node.innerHTML	 = title;
	
	title_node.appendChild(close_node);

	/*	create inner	*/
	var inner_class			 = document.createAttribute('class');
	inner_class.nodeValue	 = 'inner';

	var inner_id			 = document.createAttribute('id');
	inner_id.nodeValue	 = 'inner'+caller;

	var inner_node			 = document.createElement('div');
	inner_node.setAttributeNode(inner_class);
	inner_node.setAttributeNode(inner_id);
	inner_node.innerHTML	 = content;

	/*	create content box	*/
	var box_class		 = document.createAttribute('class');
	box_class.nodeValue	 = 'box';

	var box_node		 = document.createElement('div');
	box_node.setAttributeNode(box_class);
	box_node.appendChild(inner_node)
	
	/*	create clearer	*/
	var clear_class			 = document.createAttribute('class');
	clear_class.nodeValue	 = 'clear';

	var clear_node			 = document.createElement('div');
	clear_node.setAttributeNode(clear_class);
	clear_node.innerHTML	 = '&nbsp;';

	/*	fuse everything	*/
	node.appendChild(title_node);
	node.appendChild(box_node);
	node.appendChild(clear_node);

	/*	add to helper	*/
	document.getElementById('helpers').appendChild(node);

	var offset	 = getOffset(caller);
	offset[0]	 = offset[0]+styles['top'];
	offset[3]	 = offset[3]+styles['left'];
	
	document.getElementById(id).style.top	 = offset[0]+'px';
	document.getElementById(id).style.left	 = offset[3]+'px';
	document.getElementById(id).style.display	 = 'block';
	
	if(typeof(styles['width'])!="undefined")
	{
		document.getElementById(id).style.width		 = styles['width'];
	}
	if(typeof(styles['height'])!="undefined")
	{
		document.getElementById(id).style.height	 = styles['height'];
	}
	
	if(tpl != false) { 
		var template	 = ajaxGetTpl(tpl, 'return');
		document.getElementById('inner'+caller).innerHTML	 = template;
		
		var innerScript	 = document.getElementById('inner'+caller).getElementsByTagName('script');
		for(var i=0;i<innerScript.length;i++){
			eval(innerScript[i].text);
		}
	}
}

function closeWindow(id) {
	document.getElementById('helpers').removeChild(document.getElementById(id));
}