function toggleButton(elementObj, idRegex) {
	var arraySpans = document.body.getElementsByTagName("span");
	
	for(var i = 0; i < arraySpans.length; i++)
	{
		if(arraySpans[i].id.match(idRegex))
		{
			arraySpans[i].className = 'raised';
		}
	}
	var arrayTds = document.body.getElementsByTagName("td");
	
	for(var i = 0; i < arrayTds.length; i++)
	{
		if(arrayTds[i].id.match(idRegex))
		{
			arrayTds[i].className = 'raised';
		}
	}
	elementObj.className = 'depressed';
}

function disableEnterKey(e)
{
	var key;
	if(window.event)
		key = window.event.keyCode;     //IE
	else
		key = e.which;     //firefox
	if(key == 13)
		return false;
	else
		return true;
}

function searchItems() {
	var searchObj = document.searchbar.searchitem.value;
	var address = document.getElementById("layer_content").src;
	var radioObj = document.forms['searchbar'].elements['stype'];
	var radioLength = radioObj.length;
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			var searchType = radioObj[i].value;
		}
	}
	if (address.match(/&name=(.*)$/) == null) {
		document.getElementById("layer_content").src = address + "&name=" + searchObj + "&searchtype=" + searchType;
	} else {
		document.getElementById("layer_content").src = address.replace(/&name=(.*)$/, "&name=" + searchObj + "&searchtype=" + searchType);
	}
}

function update_items() {
	frame = parent.document.getElementById("wardrobe_content").src;
	tmp = new Date();
	tmp = "&"+tmp.getTime();
	address = parent.document.the_avatar.src;
	if (address.match(/&\d/)) {
		parent.document.the_avatar.src = address.replace(/&\d{1,50}/, tmp);
	} else {
		parent.document.the_avatar.src = address + tmp;
	}
	if (frame.match(/\&\d/)) {
		parent.document.getElementById("wardrobe_content").src = frame.replace(/\&\d{1,50}/, tmp);
	} else {
		parent.document.getElementById("wardrobe_content").src = frame+tmp;
	}
}

function clear_clothes() {
	tmp = new Date();
	tmp = "&"+tmp.getTime();
	address = "ha.php?";
	address2 = "wardrobe_content.php?";
	document.the_avatar.src = address + tmp + "&end";
	document.getElementById("wardrobe_content").src = address2 + tmp;
	ch_clothes(0);
}

function ch_layer(layerid) {
	document.getElementById("layer_content").src = "layer.php?id=" + layerid + "&name=&searchtype=local";
}

function reorder_clothes() {
	var neworder = Sortable.serialize('list',{name:'what'});
	tmp = new Date();
	tmp = "&"+tmp.getTime();
	address = "ha.php?";
	parent.document.the_avatar.src = address + tmp + "&" + neworder + "&end";
}

function ch_clothes(itemid) {
	var address = parent.document.the_avatar.src;
	regexp = "="+itemid + "&";
	regexp2 = "&what\[\]=" + itemid + "&";
	if (address.match(regexp) == null) {
		address = address.replace(/&end/, "");
		address = address + "&what[]=" + itemid + "&end";
	} else {
		address = address.replace(regexp2, "&");
	}
	parent.document.the_avatar.src = address;
	update_items();
}

function ch_clothes2(itemid) {
	var address = parent.document.the_avatar.src;
	var regexp1 = "="+itemid;
	if (address.match(regexp1) == null) {
		if (address.match(/what\[\]=/) == null) {
			address = address + "&what[]=" + itemid;
			parent.document.the_avatar.src = address;
		} else {
			address = address + "&what[]=" + itemid;
			parent.document.the_avatar.src = address;
		}
	} else {
		regexp = "&what\[\]="+itemid;
		address = address.replace(regexp, "");
		parent.document.the_avatar.src = address;
	}
	update_items();
}

function ch_gender(gender) {
	var address = document.the_avatar.src;
	if (address.match(/=male/)) {
		document.the_avatar.src = address.replace(/male/, gender);
	} else { 
		if (address.match(/=female/)) {
			document.the_avatar.src = address.replace(/female/, gender);
		}
	}
	refresh();
}

function ch_base(base) {
	var address = document.the_avatar.src;
	if (address.match(/base=\d{1,28}/)) {
		document.the_avatar.src = address.replace(/base=\d{1,28}/, "base="+base);
	}
	refresh();
}

function ch_hair(hair) {
	var address = document.the_avatar.src;
	if (address.match(/hair=(.*)&h/)) {
		document.the_avatar.src = address.replace(/hair=(.*)&h/, "hair="+hair+"&h");
	}
	refresh();
}

function ch_haircolor(haircolor) {
	var address = document.the_avatar.src;
	if (address.match(/haircolor=(.*)&eyes/)) {
		document.the_avatar.src = address.replace(/haircolor=(.*)&eyes/, "haircolor="+haircolor+"&eyes");
	}
	refresh();
}

function ch_eyes(eyes) {
	var address = document.the_avatar.src;
	if (address.match(/eyes=(.*)&e/)) {
		document.the_avatar.src = address.replace(/eyes=(.*)&e/, "eyes="+eyes+"&e");
	}
	refresh();
}

function ch_eyecolor(eyecolor) {
	var address = document.the_avatar.src;
	if (address.match(/eyecolor=(.*)&m/)) {
		document.the_avatar.src = address.replace(/eyecolor=(.*)&m/, "eyecolor="+eyecolor+"&m");
	}
	refresh();
}

function ch_mouth(mouth) {
	var address = document.the_avatar.src;
	if (address.match(/mouth=(.*)&n/)) {
		document.the_avatar.src = address.replace(/mouth=(.*)&n/, "mouth="+mouth+"&n");
	}
	refresh();
}

function ch_nose(nose) {
	var address = document.the_avatar.src;
	if (address.match(/nose=(.*)&\d/)) {
		document.the_avatar.src = address.replace(/nose=(.*)&(\d)/, "nose="+nose+"&$1");
	}
	refresh();
}

function refresh() {
	var t = 120;
	image = document.the_avatar.src;
	tmp = new Date();
	tmp = "&"+tmp.getTime();
	if (image.match(/nose=\d&\d/)) {
		document.the_avatar.src = image.replace(/nose=(\d{1,})&\d{1,50}/, "nose=$1"+tmp);
	} else {
		document.images["the_avatar"].src = image+tmp;
	}
}

//Other functions

function getMouseCoords(e) {
	var coords = {};
	if (!e) var e = window.event;
	if (e.pageX || e.pageY) 	{
		coords.x = e.pageX;
		coords.y = e.pageY;
	}
	else if (e.clientX || e.clientY) 	{
		coords.x = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
		coords.y = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
	}
	return coords;
}
				
function med_mouseMoveHandler(e, desc_string){
	var coords = getMouseCoords(e);
	med_showDescription(coords, desc_string);
}
				
function med_closeDescription(){
	var layer = document.getElementById("description");
	layer.style.visibility = "hidden";
}
				
function init_local(){
	med_init();
}
				
function med_init(){
	layer = document.createElement("div");
	//layer.style.width = 200 + "px";
	layer.style.position = "absolute";
	layer.style.zIndex = 999;
	layer.style.visibility = "hidden";
	layer.style.backgroundColor = "#FFF";
	layer.style.border = "1px solid #BBB";
	layer.style.padding = "2px 5px";
	layer.id = "description";
	document.body.appendChild(layer);
}
				
function med_showDescription(coords, desc_string){
	var layer = document.getElementById("description");
	layer.style.top = (coords.y + 25)+ "px";
	layer.style.left = (coords.x - 20) + "px";
	layer.style.visibility = "visible";
	layer.innerHTML = desc_string;
}
