// JavaScript Document
var querycount;
var queryerror;

function sure()
{
	return confirm("Wirklich ausführen?");	
}

function remember(ID, target)
{
	//Fügt ein Medium zur Merkliste hinzu.
	target.click = 'return forget('+ID+', this)';
	target.innerHTML = '<img src="grafiken/remember_rem.gif" />';
	target.blur();
	return false;
}

function forget(ID, target)
{
	//Entfernt ein Medium aus der Merkliste.
	target.click = 'return remember('+ID+', this)';
	target.innerHTML = '<img src="grafiken/remember_add.gif" />';
	target.blur();
	return false;
}

function showNotMail()
{
	$("not_payrange").style.display = "none";
	$("thebound").style.display = "block";
}

function showNotRSS()
{
	$("not_payrange").style.display = "inline";
	$("thebound").style.display = "none";
}

function showPreview()
{
	$("cov_preview").src = $("new_coverurl").value;
}

function hideLoader()
{
	$("lastfm_loader").style.display = "none";
	$("formkeeper").style.display = "block";
}

function showFilter()
{
	var box = $("filterdetails");
	if(box.style.display == "none")
	{
		box.style.display = "block";
	}
	else
	{
		box.style.display = "none";
	}
}
function checkForm_Musikalben()
{
	var okay = true;
	if($("new_interpret").value == "") okay = false;
	if($("new_titel").value == "") okay = false;
	if($("new_genre").value == 0) okay = false;
	if($("new_datum").value == "") okay = false;
	if($("new_cover").value == "" && $("new_coverurl").value == "") okay = false;
	if($("new_tracks").value == "") okay = false;
	if(okay == false)
	{
		alert("Bitte alle Felder mit * ausfüllen!");
		return false;
	}
}

function checkForm_Filme()
{
	var okay = true;
	if($("new_titel").value == "") okay = false;
	if($("new_genre").value == "") okay = false;
	if($("new_datum").value == 0) okay = false;
	if($("new_inhalt").value == "") okay = false;
	if($("new_cover").value == "" && $("new_coverurl").value == "") okay = false;
	if(okay == false)
	{
		alert("Bitte alle Felder mit * ausfüllen!");
		return false;
	}
}

function checkForm_Serien()
{
	var okay = true;
	if($("new_titel").value == "") okay = false;
	if($("new_staffel").value == "") okay = false;
	if($("new_genre").value == "") okay = false;
	if($("new_datum").value == 0) okay = false;
	if($("new_inhalt").value == "") okay = false;
	if($("new_episoden").value == "") okay = false;
	if($("new_cover").value == "" && $("new_coverurl").value == "") okay = false;
	if(okay == false)
	{
		alert("Bitte alle Felder mit * ausfüllen!");
		return false;
	}
}

function arrivingMusicbrainz(interpret, titel)
{
		if(xmlhttp)
		{
			if(xmlhttp.readyState == 4)
			{
				if(xmlhttp.status == 200)
				{
					var empfang = xmlhttp.responseText;
					xmlhttp = null;
					if(empfang != "nothing")
					{
						data = empfang.split("~#~");
						$("new_amazon").value = data[0];
						$("new_lastfmb").value = "http://www.lastfm.de/music/"+interpret;

						bakeAPreview();
					}
					else queryerror++;
					stopQuery("mbrainz");
				}
			}
		}
}

function bakeAPreview()
{
	$("alink").href = "http://www.amazon.de/exec/obidos/ASIN/"+$("new_amazon").value;
	$("alink").style.display = "inline";
}
function bakeAPreview2()
{
	$("cflink").href = "http://www.cinefacts.de"+$("new_cflink").value;
	$("cflink").style.display = "inline";
}

function bakeAPreview3()
{
	$("wlink").href = "http://www.wunschliste.de"+$("new_wlink").value;
	$("wlink").style.display = "inline";
}

function arrivingLastfm(interpret, titel)
{
		if(xmlhttpx)
		{
			if(xmlhttpx.readyState == 4)
			{
				if(xmlhttpx.status == 200)
				{
					var empfang = xmlhttpx.responseText;
					xmlhttpx = null;
					if(empfang != "nothing")
					{
						data = empfang.split("~#~");
						var lastfmurl = data[0];
						var release = data[1];
						var cover = data[2];
						var tracks = data[3];
						if(release == "01.01.1970") release = "";
						$("new_tracks").value = tracks;
						$("new_lastfma").value = lastfmurl;
						$("new_datum").value = release;
						$("new_coverurl").value = cover;
					}
					else queryerror++;
					stopQuery("lastfm");
				}
			}
		}
}

function arrivingCinefacts()
{
		if(xmlhttpx)
		{
			if(xmlhttpx.readyState == 4)
			{
				if(xmlhttpx.status == 200)
				{
					var empfang = xmlhttpx.responseText;
					xmlhttpx = null;
					if(empfang != "nothing")
					{
						data = empfang.split("~#~");
						var titel = data[0];
						var cflink = data[2];
						var inhalt = data[3];
						var release = data[4];
						var dauer = data[5];
						var fsk = data[6];
						var www = data[7];
						$("new_titel").value = titel;
						$("new_inhalt").value = inhalt;
						$("new_datum").value = release;
						$("new_fsk").value = fsk;
						$("new_dauer").value = dauer;
						$("new_www").value = www;
						$("new_cflink").value = cflink;
						bakeAPreview2();
					}
					else alert("CineFacts kennt diesen Film nicht. Bitte überprüfe deine Eingaben nochmal, vielleicht hast du dich vertippt!");
		hideLoader();
		showPreview();
				}
			}
		}
}

function arrivingWunschliste()
{
		if(xmlhttpx)
		{
			if(xmlhttpx.readyState == 4)
			{
				if(xmlhttpx.status == 200)
				{
					var empfang = xmlhttpx.responseText;
					xmlhttpx = null;
					if(empfang != "nothing")
					{
						data = empfang.split("~#~");
						var titel = data[0];
						var wlink = data[2];
						var inhalt = data[3];
						var eps = data[4];
						$("new_titel").value = titel;
						$("new_wlink").value = wlink;
						$("new_inhalt").value = inhalt;
						$("new_episoden").value = eps;
						bakeAPreview();
						bakeAPreview3();
					}
					else alert("CineFacts kennt diesen Film nicht. Bitte überprüfe deine Eingaben nochmal, vielleicht hast du dich vertippt!");
		hideLoader();
		showPreview();
				}
			}
		}
}

function arrivingFernsehserien(serie, staffel)
{
		if(xmlhttp)
		{
			if(xmlhttp.readyState == 4)
			{
				if(xmlhttp.status == 200)
				{
					var empfang = xmlhttp.responseText;
					xmlhttp = null;
					if(empfang != "nothing")
					{
						data = empfang.split("~#~");
						$("new_titel").value = data[0];
						$("new_staffel").value = staffel;
						$("new_datum").value = data[1];
						$("new_inhalt").value = data[3];
						$("new_episoden").value = data[4];
						hideLoader();
					}
				}
			}
		}
}

function queryWunschliste(titel, staffel)
{
	var url = "gear/ajax_snippets/wunschliste.php?titel="+titel+"&staffel="+staffel;
	var params = null;
	var callback = new Function("","if (arrivingWunschliste) {arrivingWunschliste();}");
	xmlhttpx = null;
	xmlhttpx = send_httprequest("POST",url,true,params,callback);
	return false;
}

function queryCinefacts(titel)
{
	var url = "gear/ajax_snippets/cinefacts.php?titel="+titel;
	var params = null;
	var callback = new Function("","if (arrivingCinefacts) {arrivingCinefacts();}");
	xmlhttpx = null;
	xmlhttpx = send_httprequest("POST",url,true,params,callback);
	return false;
}

function queryLastfm(interpret, titel)
{
	var url = "gear/ajax_snippets/lastfm.php?interpret="+interpret+"&titel="+titel;
	var params = null;
	var callback = new Function("","if (arrivingLastfm) {arrivingLastfm('"+interpret+"', '"+titel+"');}");
	xmlhttpx = null;
	xmlhttpx = send_httprequest("POST",url,true,params,callback);
	return false;
}

function queryMusicbrainz(interpret, titel)
{
	var url = "gear/ajax_snippets/musicbrainz.php?interpret="+interpret+"&titel="+titel;
	var params = null;
	var callback = new Function("","if (arrivingMusicbrainz) {arrivingMusicbrainz('"+interpret+"', '"+titel+"');}");
	xmlhttp = null;
	xmlhttp = send_httprequest("POST",url,true,params,callback);
	return false;
}

function queryFernsehserien(serie, staffel)
{
	var url = "gear/ajax_snippets/fernsehserien.php?titel="+serie+"&staffel="+staffel;
	var params = null;
	var callback = new Function("","if (arrivingFernsehserien) {arrivingFernsehserien('"+serie+"', '"+staffel+"');}");
	xmlhttp = null;
	xmlhttp = send_httprequest("POST",url,true,params,callback);
	return false;
}

function stopQuery(query)
{
	if(query == "lastfm")
	{
		querycount--;
		$("icon_lastfm").style.display = "none";
	}

	if(query == "mbrainz")
	{
		querycount--;
		$("icon_mbrainz").style.display = "none";
	}

	if(querycount == 0)
	{
		if(queryerror == 2)
		{
			alert("LastFM und Musicbrainz kennen dieses Album nicht. Bitte überprüfe deine Eingaben nochmal, vielleicht hast du dich vertippt!");
		}
		hideLoader();
		showPreview();
	}
}

function startQueries(interpret, titel)
{
	titel.replace(" ", "+");
	interpret.replace(" ", "+");
	querycount = 2;
	queryerror = 0;
	queryLastfm(interpret, titel);
	queryMusicbrainz(interpret, titel);
}

function changeID(ID)
{
	$("itemID").value = ID;
	$("buddySwitch").submit();
}

function verifyState(objekt)
{
	switch(objekt.value)
	{
		case '0':
		case '1':
			$('con_info').style.display="none";
			break;
		case '2':
			$('con_info').style.display="block";
			$('vl_text').style.display="none";
			$('vl_txb').style.display="none";
			break;
		case '3':
			$('con_info').style.display="block";
			$('vl_text').style.display="inline";
			$('vl_txb').style.display="block";
			break;
		
	}
}

function verifyRating(objekt)
{
	if(objekt.value != '5')
	{
		$('great').disabled = true;
		$('great').checked = false;
	}
	else
	{
		$('great').disabled = false;
	}
}

function arrivingGenres(xTarget)
{
		if(xmlhttp)
		{
			if(xmlhttp.readyState == 4)
			{
				if(xmlhttp.status == 200)
				{
					var empfang = xmlhttp.responseText;
					xmlhttp = null;
					$(xTarget).innerHTML = empfang;
				}
			}
		}
}

function updateCmbEditGenre(typ, key, xTarget)
{
	$("genrecontainer").innerHTML = "&nbsp;Einen Moment, empfange Genres...";
	var url = "gear/ajax_snippets/cmb_editgenre.php?type="+typ+"&key="+key;
	var params = null;
	var callback = new Function("","if (arrivingGenres) {arrivingGenres('"+xTarget+"');}");
	xmlhttp = null;
	xmlhttp = send_httprequest("POST",url,true,params,callback);
	return false;
}

function editNotification(id, typ, genre)
{
	$('editForm').style.display = "inline";
	$('not_mType2').value = typ;
}

function arrivingSalt()
{
	
		if(xmlhttp)
		{
			if(xmlhttp.readyState == 4)
			{
				if(xmlhttp.status == 200)
				{
					var empfang = xmlhttp.responseText;
					xmlhttp = null;
					if(empfang.length < 2)
					{
						$("loginInfo").innerHTML = "&nbsp;Beim Login ist ein Fehler aufgetreten! :(";
					}
					else
					{
						var crypted = Javacrypt.prepareForm(empfang);
						$("login").submit();
					}
				}
			}
		}
}

function doLogin()
{
	
	$("login").style.display = "none";
	$("loginInfo").innerHTML = "&nbsp;Einen Moment, Login läuft...";
	var url = "gear/ajax_snippets/salzstreuer.php";
	var params = "nick="+$("username").value;
	var callback = new Function("","if (arrivingSalt) {arrivingSalt();}");
	xmlhttp = null;
	xmlhttp = send_httprequest("POST",url,true,params,callback);
	return false;
}

function clearText(textObjekt)
{
	textObjekt.value = "";
}

function $(objektID)
{
	return document.getElementById(objektID);	
}


// AJAX
//--------------------------------------------------------------------------

// XML-HTTP-Request
//--------------------------------------------------------------------------
function get_new_xmlhttprequest()
{
	var xmlhttp = null;
	
	if (window.XMLHttpRequest)
	{
		// code for Mozilla, etc.
		//--------------------------------------------------------------------------
		xmlhttp = new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{
		// code for IE
		//--------------------------------------------------------------------------
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}

	return xmlhttp;
}
//--------------------------------------------------------------------------
function send_httprequest(inMethod,inURL,inAsync,inParams,inCallback)
{
	var xmlhttp = get_new_xmlhttprequest();

	// Callback-Function setzen
	//--------------------------------------------------------------------------
	if (inCallback != null)
	{
		xmlhttp.onreadystatechange = inCallback;
	}

	// Verbindung öffnen
	//--------------------------------------------------------------------------
	xmlhttp.open(inMethod, inURL, inAsync);

	// Parameter
	//--------------------------------------------------------------------------
	if(inParams != null)
	{
		xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
	}
	
	// Anfrage senden
	//--------------------------------------------------------------------------
	xmlhttp.send(inParams);

	return xmlhttp;
}

