var xmlHttpReq = new Array();

var addURL = "";
var loadajax = new Image(); loadajax.src = "./img/ajaxload.gif";
var loadstar0 = new Image(); loadstar0.src = "./img/star0.png";
var loadstar1 = new Image(); loadstar1.src = "./img/star0.png";

var SearchBox = 0;

function loadSong(id) {
	if (id.innerHTML.indexOf("ajaxload.gif") == -1) id.innerHTML += ' <img src="./img/ajaxload.gif" style="vertical-align:middle;cursor:help;" title="Some songs are still not filled with all info and can take up to a minute to load..." />';
}

function ajaxload(strURL, strID, strQ, old) {
    if (document.getElementById(strID).innerHTML.indexOf("ajaxload.gif") == -1 && strID != "comment")
		document.getElementById(strID).innerHTML += ' <img src="./img/ajaxload.gif">';

    var i = xmlHttpReq.length;

    xmlHttpReq[i] = false;
    var self = this;
    if (window.XMLHttpRequest) {
        self.xmlHttpReq[i] = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        self.xmlHttpReq[i] = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq[i].open('POST', strURL, true);
    self.xmlHttpReq[i].setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq[i].onreadystatechange = function() {
        if (self.xmlHttpReq[i].readyState == 4) {
		
			if (strID == "addbox") {
				if (self.xmlHttpReq[i].responseText.indexOf("Error:") == -1) {
					document.getElementById("submit_vote").style.visibility = "hidden";
					document.getElementById(strID).innerHTML = self.xmlHttpReq[i].responseText;
				} else {
					alert(self.xmlHttpReq[i].responseText);
					document.getElementById(strID).innerHTML = "";
				}
			} else if (strID == "comment") {
				alert(self.xmlHttpReq[i].responseText);
				if (self.xmlHttpReq[i].responseText.indexOf("Error:") == -1) addComment(1);
			} else {
				alert(self.xmlHttpReq[i].responseText);
				if (self.xmlHttpReq[i].responseText.indexOf("error") == -1) {
					document.getElementById(strID).innerHTML = '';
					if (old != undefined) showStar(old,old);
					else changeGY();
				}
			}
			
        }
    }
    self.xmlHttpReq[i].send(strQ);
}

function submitStar(num, old) {
	ajaxload("./inc/vote.php","submit_vote","id="+document.getElementsByName("id")[0].value+"&user_code="+document.getElementsByName("user_code_s")[0].value+"&star="+num,old)
}

function showStar(num, old) {
	var str = "";
	for (var i=0; i<5; i++) {
		if ((i+1) <= num) str += '<img src="./img/star1.png" onmouseover="showStar('+(i+1)+','+old+')" onmouseout="showStar('+old+','+old+')" onmousedown="showStar('+(i+1)+','+(i+1)+');rateStar('+(i+1)+','+old+')" />';
		else str += '<img src="./img/star0.png" onmouseover="showStar('+(i+1)+','+old+')" onmouseout="showStar('+old+','+old+')" onmousedown="showStar('+(i+1)+','+(i+1)+');rateStar('+(i+1)+','+old+')" />';
	}
	document.getElementById("stars").innerHTML = str;
}

function rateStar(num, old) {
	var now = new Date();
	var str = "";
	if (num > 0) {
		str += 'Please enter the check code to verify your vote:<br />';
		str += '<img src="./inc/code.php?'+now.getTime()+'" id="checkimg1" align="top" title="Click here to try another one" onclick="imgReload(\'checkimg1\')" />';
		str += '<input type="text" name="user_code_s" size="5" /> ';
		str += '<input type="button" value="Submit!" onclick="submitStar('+num+','+old+')" /> <input type="button" value="Cancel" onclick="rateStar(0);showStar('+old+','+old+')"/>';
	}
	document.getElementById("submit_vote").innerHTML = str;
}

function submitGY(s1, s2, s3, s4, s5) {
	if (document.getElementById("user_year")) nYear = document.getElementById("user_year").options[document.getElementById("user_year").selectedIndex].text;
	if (document.getElementById("user_genre")) nGenre = document.getElementById("user_genre").options[document.getElementById("user_genre").selectedIndex].text;
	var url = "id="+document.getElementsByName("id")[0].value+"&user_code="+document.getElementsByName("user_code_gy")[0].value;
	if (nYear != Year) url += "&user_year="+nYear; 
	if (nGenre != Genre) url += "&user_genre="+nGenre.toLowerCase();
	if (nYear != Year || nGenre != Genre) ajaxload("./inc/gy.php","submit_gy", url);
}

function changeGY(id) {
	var now = new Date();
	var str = '';
	if (id == "genre" || id == "year") {
		if (id == "genre") {
			document.getElementById('genre').innerHTML = '<select id="user_genre">' +
			'<option>'+Genres[0]+'</option><option>'+Genres[1]+'</option><option>'+Genres[2]+'</option><option>'+Genres[3]+'</option><option>'+Genres[4]+'</option>' +
			'</select> <input type="button" value="Cancel" onclick="changeGY()" />';
		} else if (id == "year") {
			document.getElementById('year').innerHTML = '<select id="user_year">' +
			'<option>'+Years[0]+'</option><option>'+Years[1]+'</option><option>'+Years[2]+'</option><option>'+Years[3]+'</option><option>'+Years[4]+'</option>' +
			'</select> <input type="button" value="Cancel" onclick="changeGY()" />';
		}
		str += 'Please enter the check code to avoid abuse. Make sure that you don\'t enter something incorrect. Thanks!<br />';
		str += '<img src="./inc/code.php?'+now.getTime()+'" id="checkimg2" align="top" title="Click here to try another one" onclick="imgReload(\'checkimg2\')" />';
		str += '<input type="text" name="user_code_gy" size="5" /> ';
		str += '<input type="button" value="Submit!" onclick="submitGY()" />';
	} else {
		document.getElementById('genre').innerHTML = '<span onclick="changeGY(\'genre\')">'+nGenre+'</span>';
		document.getElementById('year').innerHTML = '<span onclick="changeGY(\'year\')">'+nYear+'</span>';
	}
	document.getElementById("submit_gy").innerHTML = str;
}

function addSong(val) {
	if (val != undefined) {
		addURL = val+"&val=1";
	} else {
		addURL = "song="+encodeURIComponent(document.getElementsByName("a_song")[0].value)+"&user_code="+document.getElementsByName("user_code")[0].value;
		if (document.getElementsByName("a_artist")[0]) addURL += "&artist="+encodeURIComponent(document.getElementsByName("a_artist")[0].options[document.getElementsByName("a_artist")[0].selectedIndex].text);
	}
	ajaxload("./inc/add_check.php", "addbox", addURL);
}

function addComment(i) {
	if (i == 1) {
		document.getElementById("comment").innerHTML =
			'<a>You</a>:<br />' + document.getElementsByName("c_comment")[0].value+'<br /><br />' + document.getElementById("comment").innerHTML;

			document.getElementsByName("c_comment")[0].value = "";
			document.getElementsByName("c_user_code")[0].value = "";
	} else {
		var addURL = "id="+document.getElementsByName("id")[0].value+"&user_code="+document.getElementsByName("c_user_code")[0].value+"&comment="+encodeURIComponent(document.getElementsByName("c_comment")[0].value);
		ajaxload("./inc/comment.php", "comment", addURL);
	}
}
function imgReload(id) {
    var now = new Date();
	document.getElementById(id).src = "./inc/code.php?" + now.getTime();
}
function reportSong(id) {
	var w = 310;
	var h = 220;
	var screenw = screen.availWidth;
	var screenh = screen.availHeight;
	var posx = (screenw / 2) - (w / 2) - 20;
	var posy = (screenh / 2) - (h / 2) - 20;
	var mywin = window.open("./inc/report.php?id="+id,"mywin","top="+posy+",left="+posx+",width="+w+",height="+h+",menubar=no,locationbar=no,statusbar=no,resizable=no,toolbar=no,dependent=yes,scrollbars=no");
	mywin.focus();
}
function recommendSong(id) {
	var w = 330;
	var h = 330;
	var screenw = screen.availWidth;
	var screenh = screen.availHeight;
	var posx = (screenw / 2) - (w / 2) - 20;
	var posy = (screenh / 2) - (h / 2) - 20;
	var mywin2 = window.open("./inc/recommend.php?id="+id,"mywin2","top="+posy+",left="+posx+",width="+w+",height="+h+",menubar=no,locationbar=no,statusbar=no,resizable=no,toolbar=no,dependent=yes,scrollbars=no");
	mywin.focus();
}
function toggleSearch() {
	if (SearchBox == 0) {
		document.getElementById('top_switch').innerHTML = '<a href="#" onclick="toggleSearch()"><img src="./img/search2.png" /> Close Toolbar</a>&nbsp;';
		// <a href="'+document.URL.substr(document.URL.lastIndexOf('/')+1)+'&bgcolor='+document.getElementsByTagName('body')[0].style.backgroundColor+'" title="Set this background color as default"><img src="./img/color_wheel.png" /></a><img src="./img/vertical.png" />
		// <a href="#" onclick="userLogin(2)" title="Login/Register"><img src="./img/user.png" /></a><img src="./img/vertical.png" />
		document.getElementById('top_search').innerHTML = '<div id="top_searchfield"><script type="text/javascript">addthis_pub  = "kallculator";</script><a href="http://www.addthis.com/bookmark.php" onmouseover=\'return addthis_open(this, "", "[URL]", "[TITLE]")\' onmouseout="addthis_close()" onclick="return addthis_sendto()"><img src="http://s9.addthis.com/button1-bm.gif" width="125" height="16" border="0" alt="" /></a><script type="text/javascript" src="http://s7.addthis.com/js/152/addthis_widget.js"></script><img src="./img/vertical.png" /><a href="./forum" title="Forums"><img src="./img/group.png" /></a><img src="./img/vertical.png" /><a href="index.php?rank=0" title="Charts"><img src="./img/charts_line.png" /></a><img src="./img/vertical.png" /><a href="add.php" title="Add a new song"><img src="./img/add.png" /></a><img src="./img/vertical.png" /><a href="random.php" title="Open a random song"><img src="./img/random1.png" onmouseover="this.src=\'./img/random2.png\'" onmouseout="this.src=\'./img/random1.png\'" /></a> &nbsp; <form action="/" method="GET" name="topsearch"><input type="hidden" name="pa" value="1" /><input id="top_searchbox" type="text" name="q" value="Search for song/artist" onfocus="this.select()" /> <a href="#" onclick="topsearch.submit()" title="Search for this song"><img src="./img/search.png" /></a></form></div>';
		SearchBox = 1;
	} else {
		document.getElementById('top_switch').innerHTML = '<a href="#" onclick="toggleSearch()"><img src="./img/search1.png" /> Toolbar</a>&nbsp;';
		document.getElementById('top_search').innerHTML = '';
		SearchBox = 0;
	}
}
function userLogin(Switch) {
	var now = new Date();
	if (Switch == 1) {
		document.getElementById('login').style.visibility = 'visible';
		document.getElementById('login').innerHTML = '<form method="POST"><table>' +
			'<tr><td colspan="2"><b>Register New User</b></td></tr>' +
			'<tr><td>Username:</td><td><input type="text" name="user" /></td></tr>' +
			'<tr><td>Password:</td><td><input type="text" name="pwd" /></td></tr>' +
			'<tr><td>eMail:</td><td><input type="text" name="email" /></td></tr>' +
			'<tr><td>Real Name:</td><td><input type="text" name="realname" /></td></tr>' +
			'<tr><td><img src="./inc/code.php?'+now.getTime()+'" id="checkimg1" align="top" title="Click here to try another one" onclick="imgReload(\'checkimg1\')" /></td>' +
			'<td style="text-align:left"><input type="text" name="reg_user_code" size="5" /></td></tr>' +
			'<tr><td></td><td><input type="submit" value="Register" /> <input type="button" value="Cancel" onclick="userLogin()" /></td></tr>' +
			'</table><p align="center" style="font-size:12px">As a member you can set up your own toplist. Other people can then judge your list. And you never have to enter an anti-spam code again.</p></form>';
	} else if (Switch == 2)  {
		document.getElementById('login').style.visibility = 'visible';
		document.getElementById('login').innerHTML = '<form method="POST"><table>' +
			'<tr><td colspan="2"><b>Login</b></td></tr>' +
			'<tr><td>Username:</td><td><input type="text" name="user" /></td></tr>' +
			'<tr><td>Password:</td><td><input type="text" name="pwd" /></td></tr>' +
			'<tr><td></td><td><input type="submit" value="Login" /> <input type="button" value="New User" onclick="userLogin(1)" /> <input type="button" value="Cancel" onclick="userLogin()" /></td></tr>' +
			'</table><p align="center" style="font-size:12px">If you aren\'t already a member, click on the [New User] button.</p></form>';
	} else {
		document.getElementById('login').style.visibility = 'hidden';
		document.getElementById('login').innerHTML = '';
	}
}
function CheckVideo() {
	var Movie = window.document.youtube;
	if (Movie.getPlayerState() == 0) window.setTimeout("CheckVideo()", 5000);
	else self.location.href = "random.php";
}