// JavaScript Document

function chartChanged() 
{ 
if (xmlHttp.readyState==4)
{ 
document.getElementById("charttype").innerHTML=xmlHttp.responseText;
}
}

function loadchart(charttype, linkname) {
	document.getElementById("charttype").innerHTML = "";
	document.getElementById("charttype").innerHTML = "&nbsp;&nbsp;&nbsp;<img src=\"flash/loader1.gif\" width=\"30px\" height=\"20px\" />";
	
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)  {
		  alert ("Your browser does not support AJAX!");
		  return;
	} 
	var url="";
	if (charttype=="downloads") {
		url = "charts/downloadchart.php";
	}
	if (charttype=="plays") {
		url = "charts/playschart.php";
	}
	if (charttype=="rating") {
		url = "charts/ratingchart.php";
	}
	
	xmlHttp.onreadystatechange=chartChanged;
	url=url;
	url=url+"?sid="+Math.random();
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	
	var linknames = Array('dltab','pltab','ratab')
	var dl = document.getElementById(linknames[0])
	var pl = document.getElementById(linknames[1])
	var ra = document.getElementById(linknames[2])
	
	dl.onmouseover = function() {dl.style.color="red";showCursor(linknames[0])}
	pl.onmouseover = function() {pl.style.color="red";showCursor(linknames[1])}
	ra.onmouseover = function() {ra.style.color="red";showCursor(linknames[2])}
	dl.onmouseout = function() {dl.style.color="#cccccc";restoreCursor(linknames[0])}
	pl.onmouseout = function() {pl.style.color="#cccccc";restoreCursor(linknames[1])}
	ra.onmouseout = function() {ra.style.color="#cccccc";restoreCursor(linknames[2])}
	
	for (n=0; n<3; n++) {
		if (linkname == linknames[n]) {
			var lnkon = document.getElementById(linkname);
			lnkon.style.color = "#e77878";
			var x = document.getElementById(linknames[n]);
			var y = linknames[n]
		} else {
			var lnk = document.getElementById(linknames[n])
			lnk.style.color = "#cccccc";
		}
	}
	x.onmouseover = function() {x.style.color="red";showCursor(y)}
	x.onmouseout = function() {x.style.color="#e77878";restoreCursor(y)}/**/
}

function showCursor (elementid) {
	document.getElementById(elementid).style.cursor = "pointer";
	
}
function restoreCurson (elementid) {
	document.getElementById(elementid).style.cursor = "default";
}

function redDloadLink() {
	document.getElementById('dltab').style.color = "#e77878";
}

function helpOn () {
	var x = document.getElementById("helpicon");
	x.src = "images/icons/help_on.png";

}
function helpOff() {
	var x = document.getElementById("helpicon");
	x.src = "images/icons/help_off.png";
}

function openHelp() {
	
	var br = version();
	if (br =="Netscape") {
		var y = window.outerHeight;
		var x = window.outerWidth;
		var top = (y/2)-50;
		var left= (x/2)-220;	
	}
	if (br == "Microsoft Internet Explorer") {
		var x = window.screen.width;
		var y = window.screen.height;
		
		var top = (y/2)-50;
		var left= (x/2)-220;
		
		
	}
		
	url = "charthelp.php";
	var downloadWindow = window.open(url, "downloadWindow", "dependent=yes,width=350, height=275, resizable=no, menubar=no, status=no, scrollbars=no, toolbar=no, directories=no, titlebar=no, location=no, top="+top+", left="+left+"\"");	
	
}

//for the send to friend box
function stretchBox() {
	var x = document.getElementById("email");
	var wstr = x.style.width;
	var w = parseFloat(wstr)
	y = w + 20;
	if (y >= 200) {
		x.value = "Enter email address.";
	}
	if (y >=220) {
		clearInterval(tp);
	}
	x.style.width = y+"px";
	
	
}

function clearText(words) {
	var z = document.getElementById("email")
	z.innerHTML = "";
	var x =z.value;
	if (x==words) {
		z.value = "";
	}
}
function deleteContent(el) {
	document.getElementById(el).innerHTML = "";
	var e = document.getElementById("tellfriend");
	e.style.background = "";
	e.style.color = "";
}
	

function openTextarea () {
	//insert the textinputs and buttons
	var e = document.getElementById("tellfriend");
	e.style.background = "url(images/backgroundbars/tellfriendBG.png) repeat-y";
	e.style.color = "#000000";
	document.getElementById("mailshareHolder").innerHTML = "<textarea id=\"email\" /></textarea><div id=\"buttons\"><button id=\"emailSend\" onclick=\"sendShare()\" >send</button><button id=\"cancel\" class=\"mailshare\" onclick=\"deleteContent('mailshareHolder')\" >close</button></div>";
	//var y = setInterval(stretchBox(), 500);
	var x = document.getElementById("email");
	x.onclick=function() {clearText('Enter email address.')};
	x.style.width = "1px";
	tp = setInterval ('stretchBox(tp)',50);
	
}

function sendShare() {
	showLoaderPic("tellfriendlink");
	var hint = document.getElementById("tellfriendlink");
	htp = GetXmlHttpObject();
	var to = document.getElementById("email").value;
	var params = "to="+to;
	url = "scripts/sharesendmail.php"
	htp.open("POST", url, true);
	//Send the proper header information along with the request
	htp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	htp.setRequestHeader("Content-length", params.length);
	htp.setRequestHeader("Connection", "close");
	htp.onreadystatechange = function() {
		if(htp.readyState == 4 ) {
			if (htp.status == 200)  {
				var msg = htp.responseText;
				var regexp = /success/;
				//alert(msg);
				var count = msg.match(regexp)
				if (count != null) { //success
				//alert(msg)
					clearLoaderPic("tellfriendlink");
					document.getElementById("tellfriendlink").innerHTML = "reloading (<span id=\"reloadcount\">3</span>)";
					document.getElementById("email").value = "Message has been sent.";
					var tt = setInterval("reloadText()", 1000);
					
				}
				if (count == null) {
					clearLoaderPic("tellfriendlink");
					document.getElementById("tellfriendlink").innerHTML = msg;
				}
			}
		}
	}
	htp.send(params);
}

function reloadText () {
	
	var s = document.getElementById("reloadcount").innerHTML;
	if (s > 0) {
		s = s-1;
		document.getElementById("reloadcount").innerHTML=s;
	}
	if (s == 0) {
		var e = document.getElementById("tellfriend");
		e.style.background = "";
		e.style.color = "";
		document.getElementById("tellfriendlink").innerHTML = "Tell a friend about Overground Music.";
		document.getElementById("mailshareHolder").innerHTML = "";
		clearInterval(tt);
	}
	
}