var xmlhttp;

/*@cc_on @*/
/*@if (@_jscript_version >= 5)
    try {
        xmlhttp=new ActiveXObject("Msxml2.XMLHTTP")
    }
    catch (e) {
        try {
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
        }
        catch (E) {
            xmlhttp=false
        }
    }
@else
 xmlhttp=false
@end @*/

if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    try {
        xmlhttp = new XMLHttpRequest();
    } catch (e) {
        xmlhttp=false;
    }
}

function check_search(query) {
	if (query.length > 1) {
		xmlhttp.open("GET", "xmlrequest.php?action=lookup&q=" + escape(query), true);
		xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState == 4) {
				if (xmlhttp.responseText == '') {
					document.getElementById('resultbox').style.display = 'none';
				} else {
					document.getElementById('resultbox').innerHTML = xmlhttp.responseText;
					document.getElementById('resultbox').style.display = 'block';
				}
			}
		}
		xmlhttp.send(null);
	} else {
		document.getElementById('resultbox').style.display = 'none';
	}
}

function changedata(element,action,category,data_id,type) {
	var newtext;
	if (action == "add") {
		xmlhttp.open("GET", "xmlrequest.php?action=adduserdata&category="+category+"&data_id="+data_id+"&type="+escape(type), true);
		xmlhttp.send(null);
		if (type == 'seen') { newtext = 'Læst' }
		if (type == 'gmed') { newtext = 'Kørt' }
		if (type == 'played') { newtext = 'Spillet' }
		if (type == 'visited') { newtext = 'Besøgt' }
		document.getElementById(element).innerHTML = "- "+newtext+" <a href=\"javascript:changedata('"+element+"','remove','"+category+"','"+data_id+"','"+escape(type)+"')\">(skift)</a>";
	}
	if (action == "remove") {
		xmlhttp.open("GET", "xmlrequest.php?action=removeuserdata&category="+category+"&data_id="+data_id+"&type="+escape(type), true);
		xmlhttp.send(null);
		if (type == 'seen') { newtext = 'Ikke læst' }
		if (type == 'gmed') { newtext = 'Ikke kørt' }
		if (type == 'played') { newtext = 'Ikke spillet' }
		if (type == 'visited') { newtext = 'Ikke besøgt' }
		document.getElementById(element).innerHTML = "- "+newtext+" <a href=\"javascript:changedata('"+element+"','add','"+category+"','"+data_id+"','"+escape(type)+"')\">(skift)</a>";
	}
}

function switchicon(element,action,category,data_id,type) {
	var newtext;
	if (type == 'seen') { newtext = 'Læst' }
	if (type == 'gmed') { newtext = 'Kørt' }
	if (type == 'played') { newtext = 'Spillet' }
	if (type == 'visited') { newtext = 'Besøgt' }
	if (action == "add") {
		xmlhttp.open("GET", "xmlrequest.php?action=adduserdata&category="+category+"&data_id="+data_id+"&type="+escape(type), true);
		xmlhttp.send(null);
		document.getElementById(element).innerHTML = "<a href=\"javascript:switchicon('"+element+"','remove','"+category+"','"+data_id+"','"+escape(type)+"')\"><img src=\"gfx/"+type+"_active.jpg\" alt=\""+newtext+" active\" title=\""+newtext+"\" border=\"0\" /></a>";
	}
	if (action == "remove") {
		xmlhttp.open("GET", "xmlrequest.php?action=removeuserdata&category="+category+"&data_id="+data_id+"&type="+escape(type), true);
		xmlhttp.send(null);
		document.getElementById(element).innerHTML = "<a href=\"javascript:switchicon('"+element+"','add','"+category+"','"+data_id+"','"+escape(type)+"')\"><img src=\"gfx/"+type+"_passive.jpg\" alt=\""+newtext+" passive\" title=\""+newtext+"\" border=\"0\" /></a>";
	}
}
