//Precaching menu images

var onDiv = false;
var onButton = false;

var menu_o = new Image();
menu_o.src = "/layout/menu_over.gif";

var menu_d = new Image();
menu_d.src = "/layout/menu_down.gif";

var search_b = new Image();
menu_d.src = "/layout/sb_over.gif";

function bookmarkit(title,url){
	if (window.sidebar)
		window.sidebar.addPanel(title, url, "");
	else if(window.opera && window.print){
		var elem = document.createElement('a');
		elem.setAttribute('href',url);
		elem.setAttribute('title',title);
		elem.setAttribute('rel','sidebar');
		elem.click();
	} 
	else if(document.all)
		window.external.AddFavorite(url, title);
}


function goMenu(){
	onButton = true;
	theMenu = document.getElementById('menu');
	theMenu.style.visibility = "visible";
	theMenu.style.zIndex = 10;
	setTimeout("pollMenu();",500);
}

function pollMenu(){
	if (onDiv == false && onButton == false){
		theMenu = document.getElementById('menu');
		theMenu.style.visibility = "hidden";
	}else{
		setTimeout("pollMenu();",500);
	}
}


var http_request = false;


function doReport(hashcode){
	if (document.getElementById('r_prot').checked == true){
		getR(false, hashcode, 'prot', document.getElementById('r_pass').value);
	}
	if (document.getElementById('r_corr').checked == true){
		getR(false, hashcode, 'corr', 'Corrupted Data');
	}
	if (document.getElementById('r_priv').checked == true){
		getR(false, hashcode, 'priv', 'Private Tracker');
	}
	if (document.getElementById('r_info').checked == true){
		getR(false, hashcode, 'info', document.getElementById('r_more').value);
	}
	if (document.getElementById('r_matu').checked == true){
		getR(false, hashcode, 'matu', 'Mature Content');
	}
}


function sendFeedback() {
	  var textz = document.getElementById('textz').value;
	  var emailz = document.getElementById('emailz').value;
      var poststr = 'textz=' + encodeURI(textz) + '&emailz=' + encodeURI(emailz);
      var gourl = 'http://www.torrenthound.com/send_msg.php';
      makeMessageRequest(gourl, poststr);
}

   
function getR(obj, hashcode, rtype, rcontent) {
      var poststr = 'hash=' + hashcode + '&reporttype=' + rtype + '&reportcontent=' + encodeURI(rcontent);
      var gourl = 'http://www.torrenthound.com/report.php';
      makeSyncRequest(gourl, poststr);
}

function getC(obj, hashcode) {
      var poststr = 'hash=' + hashcode + '&commentbody=' + encodeURI( document.getElementById('commentbody').value );
      var gourl = 'http://www.torrenthound.com/comment.php';
      makeSyncRequest(gourl, poststr);
}


function makeMessageRequest(doiturl, poststr){
      http_request = false;

      if (window.XMLHttpRequest) { // Mozilla, Safari,...
            http_request = new XMLHttpRequest();
            if (http_request.overrideMimeType) {
                  http_request.overrideMimeType('text/xml');
                  // See note below about this line
            }
      } else if (window.ActiveXObject) { // IE
            try {
                  http_request = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                  try {
                        http_request = new ActiveXObject("Microsoft.XMLHTTP");
                  } catch (e) {}
            }
      }
      if (!http_request) {
            alert('Giving up :( Cannot create an XMLHTTP instance');
            return false;
      }
	  
	  if (poststr != null){
		    http_request.onreadystatechange = function() { alertMessage(http_request); };
		  	http_request.open('POST', doiturl, true);
			http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			http_request.setRequestHeader("Content-length", poststr.length);
			http_request.setRequestHeader("Connection", "close");
			http_request.send(poststr);	
	  }
}


function makeSyncRequest(doiturl, poststr){
      http_request = false;

      if (window.XMLHttpRequest) { // Mozilla, Safari,...
            http_request = new XMLHttpRequest();
            if (http_request.overrideMimeType) {
                  http_request.overrideMimeType('text/xml');
                  // See note below about this line
            }
      } else if (window.ActiveXObject) { // IE
            try {
                  http_request = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                  try {
                        http_request = new ActiveXObject("Microsoft.XMLHTTP");
                  } catch (e) {}
            }
      }
      if (!http_request) {
            alert('Giving up :( Cannot create an XMLHTTP instance');
            return false;
      }
	  
	  if (poststr == null){
			http_request.open('GET', doiturl, false);
      		http_request.send(null);
			
			if(http_request.status == 200) {
				var thoundreturn  = http_request.responseText;
				if (thoundreturn.indexOf(":error:") != -1){
					var thoundnotify = thoundreturn.split(":error:");
					alert(thoundnotify[1]);
				}else{
					alert('returned vals: ' + thoundreturn);	
				}
		  	}
	  }else{
		    http_request.onreadystatechange = function() { alertContents(http_request); };
		  	http_request.open('POST', doiturl, true);
			http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			http_request.setRequestHeader("Content-length", poststr.length);
			http_request.setRequestHeader("Connection", "close");
			http_request.send(poststr);	
	  }
}

function alertContents(hrequest){
	if (hrequest.readyState == 4) {
		if (hrequest.status == 200) {
			if (hrequest.responseText.indexOf(":error:") != -1){
				var thounderror = hrequest.responseText.split(":error:");
				alert(thounderror[1]);
			}else{
				var thoundnotify = hrequest.responseText.split(":notify:");
				document.getElementById('notifyitem').innerHTML = thoundnotify[1];
				document.getElementById('notifyitem').style.display = 'block';
				if (isNull(document.getElementById('commentbody')) == false){
					document.getElementById('commentbody').value = '';
				}
				if (isNull(document.getElementById('r_pass')) == false){
					document.getElementById('r_pass').value = '';
					document.getElementById('r_more').value = '';
					document.getElementById('r_prot').checked = false;
					document.getElementById('r_matu').checked = false;
					document.getElementById('r_corr').checked = false;
					document.getElementById('r_priv').checked = false;
				}
			}
		} else {
			alert('There was a problem with the request.');
		}
	}
}

function alertMessage(hrequest){
	if (hrequest.readyState == 4) {
		if (hrequest.status == 200) {
			if (hrequest.responseText.indexOf("error:") != -1){
				var thounderror = hrequest.responseText.split("error:");
				alert(thounderror[1]);
			}else{
				alert('Your message was successfully sent!\n\nThank you for your time.');
				/* document.getElementById('emailz').value = ''; */
				document.getElementById('textz').value = '';
			}
		} else {
			alert('There was a problem with the request.');
		}
	}
}

function hideNotify(){
	document.getElementById('notifyitem').style.display = 'none';
}

function isNull(val){return(val==null);}
