function getListContentTable() {
	listcontent=document.createElement("table");
	listcontent.id="listcontent";
	listcontent.width="100%";
	listcontent.border=0;
	listcontent.cellPadding=0;
	listcontent.cellSpacing=2;
	return listcontent;
}

function getListContentHeader(isFirst, width, url, text, sorting) {
	var header=document.createElement("th");
	if(isFirst==true) {
		header.className="tableheader firstheader";
	} else {
		header.className="tableheader";
	}
	header.scope="col";
	header.width=width;
	var headerlink=document.createElement("a");
	headerlink.href=url;
	var headerlinkdiv=document.createElement("div");
	headerlinkdiv.appendChild(document.createTextNode(text));
	if(sorting==0) headerlinkdiv.className="sortasc";
	if(sorting>0) headerlinkdiv.className="sortdesc";
	headerlink.appendChild(headerlinkdiv);
	header.appendChild(headerlink);
	return header;
}

function appendMultipleChilds(parent, childlist) {
	var i;
	for(i=0;i<childlist.length;i++) {
		if(childlist[i]!=null) parent.appendChild(childlist[i]);	
	}
}

function removeAllChilds(parent) {
	while(parent.firstChild) {
		parent.removeChild(parent.firstChild);	
	}
}

function removeFromParent(child) {
	child.parentNode.removeChild(child);	
}

function selectValueInList(list, value) {
	for(i=0;i<list.options.length;i++) {
		var option=list.options[i];
		if(option.value==value) {
			//option.defaultSelected=true;
			option.selected=true;	
		} else {
			//option.defaultSelected=false;
			option.selected=false;
		}
	}	
}

function getListTableElement(alignment, child, width, bgcolor, colspan, rowspan) {
	var newElement=document.createElement("td");
	newElement.align=alignment;
	newElement.valign="middle";
	if(child!=null) newElement.appendChild(child);
	if(width!=null) newElement.width=width;
	if(bgcolor!=null) newElement.style.background=bgcolor;
	if(colspan!=null) newElement.colSpan=colspan;
	if(rowspan!=null) newElement.rowSpan=rowspan;
	return newElement;
}

function getLinkElement(text, target, addto, linktarget) {
	if(text==null || text=="") return null;
	var linkelement=document.createElement("a");
	linkelement.href=target;
	if(typeof(text)=="string") {
		linkelement.appendChild(document.createTextNode(text));
	} else {
		linkelement.appendChild(text);
	}
	if(linktarget) linkelement.target=linktarget;
	if(addto!=null) {
		addto.appendChild(linkelement);
	}
	return linkelement;
}

function getTextInputField(id, characters) {
	var fieldelement=document.createElement("input");
	fieldelement.id=id;
	fieldelement.name=id;
	fieldelement.type="text";
	fieldelement.size=characters;
	return fieldelement;
}

function getButton(text, onclick) {
	var button=document.createElement("input");
	button.type="button";
	button.value=text;
	if(onclick!=null) button.onclick=onclick;
	return button;
}

function getTable(width, border, cellSpacing, cellPadding) {
	var table=document.createElement("table");
	table.width=width;
	table.border=border;
	table.cellSpacing=cellSpacing;
	table.cellPadding=cellPadding;
	return table;
}

function getSpan(className, id) {
	var span=document.createElement("span");
	span.id=id;
	span.className=className;
	return span;
}

function getCheckbox(id, value, defaultChecked, checked, onclick) {
	var checkbox=document.createElement("input");
	checkbox.type="checkbox";
	checkbox.id=id;
	checkbox.name=id;
	checkbox.value=value;
	checkbox.defaultChecked=defaultChecked;
	checkbox.checked=checked;
	if(onclick) checkbox.onclick=onclick;
	return checkbox;
}	

function getDropsite(type) {
	switch(type) {
		case 1:
			return "item";
		case 2:
			return "spell";
		case 3:
			return "itemset";
		case 4:
			return "npc";
		case 5:
			return "quest";
		case 6:
			return "object";
		case 7:
			return "zone";
		case 8:
			return "faction";
		case 9:
			return "achievement";
	}
}

function displayLoadingScreen() {
	var listbody=document.getElementById("listbody");
	var listcontent=document.getElementById("listcontent");
	if(listcontent!=null) listbody.removeChild(listcontent);
	var listheader=document.getElementById("listheader");
	var listfooter=document.getElementById("listfooter");
	if(listheader!=null) removeAllChilds(listheader.firstChild.firstChild);
	if(listfooter!=null) removeAllChilds(listfooter.firstChild.firstChild);
	
	listcontent=getListContentTable();
	var listcontent_tr=document.createElement("tr");
	var listcontent_td=document.createElement("td");
	var loadingscreen_container=document.createElement("div");
	var loadingscreen_img=document.createElement("img");
	loadingscreen_container.className="loading";
	loadingscreen_container.align="center";
	loadingscreen_img.src="images/loadingindicator.gif";
	loadingscreen_container.appendChild(loadingscreen_img);
	listcontent_td.appendChild(loadingscreen_container);
	listcontent_tr.appendChild(listcontent_td);
	listcontent.appendChild(listcontent_tr);
	listcontent.id="listcontent";
	listbody.appendChild(listcontent);
}

function trimString(str) {
	if(typeof(str)=="string") return str.replace(/^\s+|\s+$/g,"");
	else return str;
}

function decodeText(text) {
	text=text.replace(/&lt;/g, "<");
	text=text.replace(/&gt;/g, ">");
	text=text.replace(/&quot;/g, "\"");
	text=text.replace(/&amp;/g, "&");
	text=text.replace(/&124;/g, "|");
	text=text.replace(/&35;/g, "#");
	return text;
}

function decodeListPart(text) {
	text=text.replace(/&38;/g, "&");
	text=text.replace(/&35;/g, "#");
	return text;
}

function formatDate(date) {
	var datestring=date.getDate()+"."+(date.getMonth()+1)+"."+date.getFullYear()+" ";
	if(date.getHours()<10) datestring=datestring+"0";	
	datestring=datestring+date.getHours()+":";
	if(date.getMinutes()<10) datestring=datestring+"0";
	datestring=datestring+date.getMinutes()+":";
	if(date.getSeconds()<10) datestring=datestring+"0";
	datestring=datestring+date.getSeconds();	
	return datestring;
}

function getItemIcon(icon, url, tooltip, mincount, maxcount, size, nopadding, nsoffsetX, nsoffsetY, offsetX, offsetY, edge, tag) {
	if(url!=null) {
		var icon_a=document.createElement("a");
		icon_a.href=url;
	}
	var icon_div=document.createElement("div");
	if(!icon.match(/\//)) icon_div.style.backgroundImage="url(\"images/wowicons/"+size+"/"+icon+".jpg\")";
	else icon_div.style.backgroundImage="url(\""+icon+"\")";
	icon_div.style.position="relative";
	icon_div.style.left="0px";
	icon_div.style.top="0px";
	if(size=="medium") {
		if(nopadding!=true)	icon_div.className="itemicon iconpadding";
		else icon_div.className="itemicon";		
	}
	if(size=="small") {
		if(nopadding!=true)	icon_div.className="smallitemicon smalliconpadding";
		else icon_div.className="smallitemicon";		
	}
	if(size=="tiny") {
		if(nopadding!=true)	icon_div.className="tinyitemicon tinyiconpadding";
		else icon_div.className="tinyitemicon";		
	}
	var icon_div_border=document.createElement("div");
	icon_div_border.className="border";
	var icon_div_glow=document.createElement("div");
	icon_div_glow.className="noglow";
	if(tooltip!=null) {
		var func;
		if(typeof tooltip == "string") {
			func="this.className='glow'; displayTooltip(event, '"+tooltip.replace(/'/g,'\\\'')+"'";
		} else {
			func="this.className='glow'; displayTooltip(event, "+recreateArray(tooltip);
		}
		if(nsoffsetX!=null) func+=", "+nsoffsetX; else func+=", null";
		if(nsoffsetY!=null) func+=", "+nsoffsetY; else func+=", null";
		if(offsetX!=null) func+=", "+offsetX; else func+=", null";
		if(offsetY!=null) func+=", "+offsetY; else func+=", null";
		if(edge!=null) func+=", "+edge; else func+=", null";
		func+=");";
		icon_div_glow.onmouseover=new Function("event",func);
		icon_div_glow.onmousemove=new Function("event","repositionTooltip(event);");
		icon_div_glow.onmouseout=new Function("this.className='noglow'; hideTooltip();");
	} else {
		icon_div_glow.onmouseover=new Function("event","this.className='glow';");
		icon_div_glow.onmouseout=new Function("this.className='noglow';");
	}
	icon_div_border.appendChild(icon_div_glow);
	icon_div.appendChild(icon_div_border);
	if((mincount!=maxcount || mincount>1) && size!="tiny") {
		var countText=mincount;
		if(mincount!=maxcount) countText=mincount+"-"+maxcount;
		var icon_div_count=document.createElement("div");
		icon_div_count.className="count";
		
		var icon_div_count_text=document.createElement("div");
		icon_div_count_text.appendChild(document.createTextNode(countText));
		icon_div_count_text.className="text";
		icon_div_count.appendChild(icon_div_count_text);
		
		var i;
		for(i=1;i<=8;i++) {
			var icon_div_count_border=document.createElement("div");
			icon_div_count_border.className="textborder"+i;
			icon_div_count_border.appendChild(document.createTextNode(countText));
			icon_div_count.appendChild(icon_div_count_border);
		}
		
		icon_div_glow.appendChild(icon_div_count);
	}
	if(tag!=null) {
		var icon_tag=document.createElement("img");
		icon_tag.src="images/"+tag+"_"+size+".png";
		icon_tag.width=36;
		icon_tag.height=13;
		icon_tag.style.position="absolute";
		icon_tag.style.right="4px";
		icon_tag.style.bottom="-2px";
		icon_div.appendChild(icon_tag);
	}
	if(url!=null) {
		icon_a.appendChild(icon_div);
		return icon_a;
	} else {
		return icon_div;
	}
}

function addItemIcon(targetelement, icon, url, tooltip, mincount, maxcount, size) {
	var target=targetelement;
	if(typeof(target)=="string") {
		target=document.getElementById(targetelement);
	}
	if(target!=null) target.appendChild(getItemIcon(icon, url, tooltip, mincount, maxcount, size));
}

function addTooltip(element, tooltip, nsoffsetX, nsoffsetY, offsetX, offsetY, edge) {
	var func;
	if(typeof tooltip == "string") {
		func="displayTooltip(event, '"+tooltip.replace(/'/g,'\\\'')+"'";
	} else {
		func="displayTooltip(event, "+recreateArray(tooltip);
	}
	if(nsoffsetX!=null) func+=", "+nsoffsetX; else func+=", null";
	if(nsoffsetY!=null) func+=", "+nsoffsetY; else func+=", null";
	if(offsetX!=null) func+=", "+offsetX; else func+=", null";
	if(offsetY!=null) func+=", "+offsetY; else func+=", null";
	if(edge!=null) func+=", "+edge; else func+=", null";
	func+=");";
	element.onmouseover=new Function("event",func);
	element.onmousemove=new Function("event","repositionTooltip(event);");
	element.onmouseout=new Function("hideTooltip();");
}

function addMoney(targetelement, amount) {
	var money_copper=Math.floor(amount%100);
	var money_silver=Math.floor((amount/100)%100);
	var money_gold=Math.floor(amount/10000);
	if(money_gold>0) {
		var listcontent_tr_img=document.createElement("img");
		listcontent_tr_img.src="images/coins/gold.png";
		listcontent_tr_img.align="absmiddle";
		targetelement.appendChild(document.createTextNode(money_gold));
		targetelement.appendChild(listcontent_tr_img);
	}
	if(money_silver>0 || money_gold>0) {
		var listcontent_tr_img=document.createElement("img");
		listcontent_tr_img.src="images/coins/silver.png";
		listcontent_tr_img.align="absmiddle";
		targetelement.appendChild(document.createTextNode(" "+money_silver));
		targetelement.appendChild(listcontent_tr_img);
	}
	var listcontent_tr_img=document.createElement("img");
	listcontent_tr_img.src="images/coins/copper.png";
	listcontent_tr_img.align="absmiddle";
	targetelement.appendChild(document.createTextNode(" "+money_copper));
	targetelement.appendChild(listcontent_tr_img);
}

function addAltCurrency(targetelement, icon, url, tooltip, amount) {
	var lnk;
	if(url!=null) {
		lnk=document.createElement("a");
		lnk.href=url;
	}
	var img=document.createElement("img");
	img.src=icon;
	img.width=16;
	img.height=16;
	img.border=0;
	img.align="absmiddle";
	if(tooltip!=null) {
		var func;
		if(typeof tooltip == "string") {
			func="displayTooltip(event, '"+tooltip.replace(/'/g,'\\\'')+"'"+");";
		} else {
			func="displayTooltip(event, "+recreateArray(tooltip)+");";
		}
		img.onmouseover=new Function("event",func);
		img.onmousemove=new Function("event","repositionTooltip(event);");
		img.onmouseout=new Function("hideTooltip();");
	}
	targetelement.appendChild(document.createTextNode(amount+" "));
	if(url!=null) {
		lnk.appendChild(img);
		targetelement.appendChild(lnk);
	} else {
		targetelement.appendChild(img);
	}
}

function addPrice(targetelement, pricedata) {
	if(pricedata.length>0) {
		for(k=0;k<pricedata.length;k++) {
			var money_span=document.createElement("span");
			var currencyicon=pricedata[k][0];
			var currencyid=pricedata[k][1];
			var currencytooltip=pricedata[k][2];
			var value=pricedata[k][3];
			if(currencyicon=="money") {
				addMoney(money_span, value);
			} else {
				if(currencyicon=="honor") {
					addAltCurrency(money_span, "images/coins/honorpoints.png", null, "<span class=\"ttwhite\">Ehrenpunkte</span>", value);
				} else {
					if(currencyicon=="arena") {
						addAltCurrency(money_span, "images/coins/arenapoints.png", null, "<span class=\"ttwhite\">Arenapunkte</span>", value);
					} else {
						addAltCurrency(money_span, "images/wowicons/small/"+currencyicon+".jpg", "item?id="+currencyid, currencytooltip, value);
					}
				}
			}
			targetelement.appendChild(money_span);
		}
	}
}

function getElementPosition(element) {
	var x=0;
	var y=0;
	if(element.pageX) {
		x=element.pageX;
		y=element.pageY;
	} else {
		if(element.offsetLeft) {
			x=absLeft(element);
			y=absTop(element);
		}
	}
	return {posx: x, posy: y};
}

function absLeft(el) {
	return (el.offsetParent)?el.offsetLeft+absLeft(el.offsetParent):el.offsetLeft;
}

function absTop(el) {
	return (el.offsetParent)?el.offsetTop+absTop(el.offsetParent):el.offsetTop;
}

function getAntiCacheNumber() {
	var now=new Date();
	return now.getTime();
}

function scrollToBottom() {
	window.scrollTo(0,getWindowHeight());	
}

function setPermanentCookie(name, value) {
	var expire=new Date();
	expire.setTime(expire.getTime()+(1000*60*60*24*365));
	document.cookie=name+"="+escape(value)+"; expires="+expire.toGMTString()+";";
}

function getCookie(name) {
	var cookie=document.cookie;
  	var posName=document.cookie.indexOf("; "+name+"=");
  	if(posName==-1) {
		if(document.cookie.indexOf(name+"=")==0) posName=0;
      	else return null;
   	}

   	var start=cookie.indexOf("=",posName)+1;
   	var end=cookie.indexOf(";",posName+1);
   	if(end==-1) end=cookie.length;

   	var value=cookie.substring(start,end);
   	return unescape(value);
}

function deleteCookie(name) {
	document.cookie=name+"=; expires=Thu, 01-Jan-70 00:00:01 GMT;";
}

function createGenericButton(text, url) {
	var a=document.createElement("a");
	a.className="genericbutton";
	a.href=url;
	var div=document.createElement("div");
	var span=document.createElement("span");
	span.appendChild(document.createTextNode(text));
	div.appendChild(span);
	a.appendChild(div);
	return a;
}

function evaluateArray(arraystring) {
	if(arraystring==null) return null;
	var arr=eval("("+arraystring+")");
	return arr;
}

function recreateArray(array) {
	var str="[";
	var i;
	for(i=0;i<array.length;i++) {
		var data=array[i];
		if(data == null) {
			str+="null";
		} else if(typeof data == "string") {
			str+="'"+data.replace(/'/g,'\\\'')+"'";
		} else if(typeof data == "number") {
			str+=data;
		} else if(typeof data == "object") {
			str+=recreateArray(data);	
		} else {
			str+=data;
		}
		if(i+1<array.length) str+=",";
	}
	str+="]";
	return str;
}

function addTextBorder(element, size) {
	var div=document.createElement("div");
	div.style.position="relative";
	var text=element.firstChild.nodeValue;
	
	var x,y;
	for(x=-size; x<=size; x+=size) {
		for(y=-size; y<=size; y+=size) {
			if(x!=0 || y!=0) {
				var border=document.createElement("span");
				border.className=element.className+" "+element.className+"border";
				border.style.position="absolute";
				border.style.left=x+"px";
				border.style.top=y+"px";
				border.style.zIndex=1;
				
				if(typeof border.style.MozUserSelect!="undefined") border.style.MozUserSelect="none"
				else if(typeof border.style.webkitUserSelect!="undefined") border.style.webkitUserSelect="none"
				else border.onmousedown=function(){return false};
			    border.style.cursor = "default";

				border.appendChild(document.createTextNode(text));
				div.appendChild(border);
			}
		}
	}
	
	element.style.position="relative";
	element.style.zIndex=2;
	var parent=element.parentNode;
	parent.removeChild(element);
	div.insertBefore(element,div.firstChild);
	parent.appendChild(div);
}
