// JavaScript Document

	function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_nbGroup(event, grpName) { //v6.0
  var i,img,nbArr,args=MM_nbGroup.arguments;
  if (event == "init" && args.length > 2) {
    if ((img = MM_findObj(args[2])) != null && !img.MM_init) {
      img.MM_init = true; img.MM_up = args[3]; img.MM_dn = img.src;
      if ((nbArr = document[grpName]) == null) nbArr = document[grpName] = new Array();
      nbArr[nbArr.length] = img;
      for (i=4; i < args.length-1; i+=2) if ((img = MM_findObj(args[i])) != null) {
        if (!img.MM_up) img.MM_up = img.src;
        img.src = img.MM_dn = args[i+1];
        nbArr[nbArr.length] = img;
    } }
  } else if (event == "over") {
    document.MM_nbOver = nbArr = new Array();
    for (i=1; i < args.length-1; i+=3) if ((img = MM_findObj(args[i])) != null) {
      if (!img.MM_up) img.MM_up = img.src;
      img.src = (img.MM_dn && args[i+2]) ? args[i+2] : ((args[i+1])? args[i+1] : img.MM_up);
      nbArr[nbArr.length] = img;
    }
  } else if (event == "out" ) {
    for (i=0; i < document.MM_nbOver.length; i++) {
      img = document.MM_nbOver[i]; img.src = (img.MM_dn) ? img.MM_dn : img.MM_up; }
  } else if (event == "down") {
    nbArr = document[grpName];
    if (nbArr)
      for (i=0; i < nbArr.length; i++) { img=nbArr[i]; img.src = img.MM_up; img.MM_dn = 0; }
    document[grpName] = nbArr = new Array();
    for (i=2; i < args.length-1; i+=2) if ((img = MM_findObj(args[i])) != null) {
      if (!img.MM_up) img.MM_up = img.src;
      img.src = img.MM_dn = (args[i+1])? args[i+1] : img.MM_up;
      nbArr[nbArr.length] = img;
  } }
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) 
		return c.substring(nameEQ.length,c.length);
	}
	return 0;
}

function friend()
{
var passwd = readCookie('pass');
var username = readCookie('username');
var user_id = readCookie('userid');

if(!passwd == 0 && !username == 0)
{
document.getElementById('login').innerHTML = 'Hello '+ username +', <a href="/bookmarks.php?id=' + user_id + '">My Bookmarks</a>,&nbsp;' 
                                             +
                                             '<a href="/user.php?id=' + user_id + '">My Torrents</a>,&nbsp;<a href="/login/logout.php">Logout</a>';
}
else
{
document.getElementById('login').innerHTML = '<a href="/login/login.php">Login</a>&nbsp;or&nbsp;<a href="/login/register_form.php">Register</a>';
}
}


//Declaration of global variables

var _ad;          //Scrolling banner element
var _adTop;       //Original vertical position of div#ad-right
var _maxScroll;   //Space between the top of the document and the place where the ad should stop scrolling (the top of the div#footer)
var _lastPos = 0; //Last value returned by function pos()


function init() {

 if(document.getElementById('adv_right')) 
 {
  //Define the variables
  _ad = document.getElementById('adv_right');
  _adTop = _ad.offsetTop;
  _maxScroll = document.getElementById('main').offsetHeight + _adTop - _ad.offsetHeight;

  //Checks if the page is long enough for a scrolling ad
  if(_maxScroll > _adTop)
   adScroll();

 }
}


function pos() { //Returns current vertical position on page (for scrolling ad)

 if(window.innerHeight)
  return window.pageYOffset;

 if(document.documentElement && document.documentElement.scrollTop)
  return document.documentElement.scrollTop;

 if(document.body)
  return document.body.scrollTop;

 return 0;

}


function adScroll(){ //Recursive function, changes the position of the scrolling ad

 if(_lastPos != pos()) {

  _lastPos = pos();

  if(pos() < _maxScroll && pos() > _adTop)
   _ad.style.top = pos() + 'px';

  else if(pos() < _maxScroll)
   _ad.style.top = _adTop + 'px';

  else _ad.style.top = _maxScroll + 'px';

 }

 setTimeout('adScroll()', 10);

}


window.onresize = function() { //Relocate the ad when the browser window resizes

 if(_ad) {

  _adTop = document.getElementById('main').offsetTop;
  _maxScroll = document.getElementById('main').offsetHeight + _adTop - _ad.offsetHeight;

 }

}


//XHTMLHTTPREQUEST--------------------------------------------------------------------------------------------------------------------------------------------------------
		  
function Ajaxify(target, method, url, param, slideIt, loadImage, shoWait) {

if(shoWait != 0)
{
document.getElementById(target).innerHTML = shoWait;	
}


if(loadImage != 0)
{
document.getElementById(loadImage).innerHTML = '<img src="/images/loading.gif"/>';
}

/*
	Find Correct XMLHTTP Connection	*/ 
if(!window.XMLHttpRequest) 
{
 window.XMLHttpRequest = function() 
 {
  var types = [
   'Microsoft.XMLHTTP',
   'MSXML2.XMLHTTP.5.0',
   'MSXML2.XMLHTTP.4.0',
   'MSXML2.XMLHTTP.3.0',
   'MSXML2.XMLHTTP'
  ];
  for(var i = 0; i < types.length; i++) {
   try {
    return new ActiveXObject(types[i]);
   } catch(e) {}
  }
  return false;
 };
}
  
/*
	Create Socket	*/
NewRequest = new XMLHttpRequest();
NewRequest.open(method, url, true);
NewRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
NewRequest.send(param); 


NewRequest.onreadystatechange = function() 
{

if (NewRequest.readyState == 4) 
{
if (NewRequest.status == 200) 
{

if(loadImage != 0)
{
document.getElementById(loadImage).innerHTML = '';
}

if(slideIt != 0)
{
var comment = new Fx.Slide(target, {duration: slideIt}).hide();
}
document.getElementById(target).innerHTML = unescape(NewRequest.responseText);
if(slideIt != 0)
{
comment.toggle();
}
}
	
else
{
document.getElementById(target).innerHTML = 'Ajax Error';
}
}
   
};
}

//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

//SEARCH FORM VALIDATION--------------------------------------------------------------------------------------------------------------------------------------------------------------------

function checkSearchForm()
{
 if(document.gosearch.term.value == "")
 {
  alert("Fill out the entire form. Please try again");
  document.gosearch.term.focus();
 } 
 else
 { 
  document.gosearch.submit();
 }
}

//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

//UPLOAD FORM VALIDATION--------------------------------------------------------------------------------------------------------------------------------------------------------------------

function checkUploadForm(form)
{
	if(form.torrent.value == "")
	{
	  alert("Field 'torrent file' empty. Please try again");
	  return false;
	}
	else 
	{
		var startChar = form.torrent.value.lastIndexOf(".");
		
		if(startChar == -1)
		{
			alert("This file is not a torrent file.Please try again.");
			return false;
		}
		else
		{
			var fileType = form.torrent.value.substring(startChar);
			
			if(fileType != ".torrent")
			{
				alert("This file is not a torrent file.Please try again.");
				return false;
			}
		}
	}
	
	if(form.filename.value == "")
	{
		alert("Field 'torrent name' empty. Please try again.");
		return false;
	}
	
	if(form.type.options[form.type.selectedIndex].value == "")
	{
	  alert("Choose a category. Please try again.");
	  return false;
	}
	
	if(!form.reg[0].checked && !form.reg[1].checked)
	{
	  alert("Informed the database about the tracker. Please try again.");
	  return false;
	}
}

//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

//DROP DOWN MENOU--------------------------------------------------------------------------------------------------------------------------------------------------------------------------
/*
var id_timmer_menou_close;
var id_timmer_menou_open;
var li_height = 24;
var step = 120;
var transparency = 0;
var step_transparency = 20;
var browser = navigator.userAgent;	

function opacity_plus()
{
	var elem_shadow = document.getElementById("shadow");
	
	elem_shadow.style.display = "block";
	
	if(browser.indexOf("MSIE") >= 0)//IE
	{
	 elem_shadow.filters["alpha"].opacity += step_transparency;
	}
	else
	{
	  transparency += step_transparency / 100;
	  elem_shadow.style.opacity = transparency;
	}
	//alert(browser);
}

function opacity_minus()
{
	var elem_shadow = document.getElementById("shadow");
	
	if(browser.indexOf("MSIE") >= 0)//IE
	{
	 elem_shadow.filters["alpha"].opacity -= step_transparency;
	}
	else
	{
	  transparency -= step_transparency / 100;
	  elem_shadow.style.opacity = transparency;
	}
	//alert(browser);
}


function max_height()
{
	var li_elements = document.getElementsByTagName("li");
	var total_height = 0;
	var count = 0;
	
    for(var i = 0; i < li_elements.length; i++)
	{
		var drop_li = li_elements[i].innerHTML.indexOf("subcat");
		var sort_li = li_elements[i].innerHTML.indexOf("sort");
		
		if(drop_li >= 0 && sort_li == -1)
		{
			count++;
		}
	}
	
	var m = li_height * count;
	return m;
}

function drop_down_menou_open(max_h)
{
	var el = document.getElementById("sub_ul");
	var el_height = 0;
	
	el_height = parseInt(el.clientHeight);

	if(el_height < max_h)
	{
		el.style.height = (el_height + step) + "px";
		opacity_plus();
	}
	else
	{
		clearInterval(id_timmer_menou_open);
	}
	
}

function drop_down_menou_close()
{
	var el = document.getElementById("sub_ul");
	var elem_shadow = document.getElementById("shadow");
	var el_height;
	
	el_height = parseInt(el.clientHeight);
	//alert(el_height);
	if(el_height > 0)
	{
		el.style.height = (el_height - step) + "px";
		opacity_minus();
	}
	else
	{
		elem_shadow.style.display = "none"; 
		clearInterval(id_timmer_menou_close);
	}
}

function drop_down_menu(evt)
{
	var elem = document.getElementById("sub_ul");
	var e_type;
	var e_src;
	var maxHeight;
	
	maxHeight = max_height();
	
	if(window.event)
	{
		e_type = event.type;
	}
	else
	{
		e_type = evt.type;
	}
	
	if(e_type == "click")
	{
	 if(parseInt(elem.clientHeight) == 0)
	 {
	   if(window.location.href.indexOf("subcat") == -1 && window.location.href.indexOf("search") == -1 && window.location.href.indexOf("browse") == -1)
	   {
	     document.getElementById("flash").style.visibility = "hidden";
	   }
	   
	   id_timmer_menou_open = setInterval("drop_down_menou_open(" + maxHeight + ")", 10);
	 }
	 else if(parseInt(elem.clientHeight) == maxHeight)
	 {
	   if(window.location.href.indexOf("subcat") == -1 && window.location.href.indexOf("search") == -1 && window.location.href.indexOf("browse") == -1)
	   {
	     document.getElementById("flash").style.visibility = "visible";
	   }
	   
	   id_timmer_menou_close = setInterval("drop_down_menou_close()", 10);
	 }
	}
	else if(e_type == "mouseover" && parseInt(elem.clientHeight) == maxHeight)
	{
	   if(window.location.href.indexOf("subcat") == -1 && window.location.href.indexOf("search") == -1 && window.location.href.indexOf("browse") == -1)
	   {
	     document.getElementById("flash").style.visibility = "visible";
	   }
	   
	   id_timmer_menou_close = setInterval("drop_down_menou_close()", 10);
	}
}

//----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

//CHANGE CONTENT WITHIN SHADOW ELEMENTS ---------------------------------------------------------------------------------------------------------------------------------------------------

function transport_cat(evt)
{
  if(window.event)
	  e_target = event.srcElement;
  else
	  e_target = evt.target;
	  
  var text = e_target.innerHTML;
  
  document.getElementById("inner_cat").innerHTML = text;
}
*/	
//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

//DISPLAY CATEGORIES --------------------------------------------------------------------------------------------------------------------------------------------------------------------------
/*
var displayStep = 10;
var transparencyLevel = 0;
var id_time_adv_search;

function adv_search()
{
	var elem_categ = document.getElementById("adv_search");
	 
	elem_categ.style.display = "block";
	
	if(browser.indexOf("MSIE") >= 0)//IE
	{
	  transparencyLevel += displayStep;
	  elem_categ.filters["alpha"].opacity = transparencyLevel;
	  if(transparencyLevel >= 80)
	  {
	    clearInterval(id_time_adv_search);
		displayStep = 10;
		transparencyLevel = 0;
	  }
	}
	else
	{
	  transparencyLevel += displayStep / 100;
	  elem_categ.style.opacity = transparencyLevel;
	  
	  if(transparencyLevel >= 0.8)
	  {
	    clearInterval(id_time_adv_search);
		displayStep = 10;
		transparencyLevel = 0;
	  }
	}
	//alert(opacityLevel);
}

function call_adv_search()
{
	document.getElementById("tod_yest").innerHTML = "<a href=\"javascript:void(0)\" onclick=\"close_adv_search();\"><b>Close advanced search</b></a>";
	id_time_adv_search = setInterval("adv_search()", 50);
}

function close_adv_search()
{
 if(transparencyLevel > 0)
 {
	 //do nothing
 }
 else
 {
  document.getElementById("adv_search").style.display = "none";
  document.getElementById("tod_yest").innerHTML = "<a href=\"javascript:void(0);\" onclick=\"javascript:call_adv_search();\"><b>Advanced search</b></a>";
 }
}
*/
// ADVANCED SEARCH --------------------------------------------------------------------------------------------------------------------------------------------------------
function call_adv_search()
{
	document.getElementById("tod_yest").innerHTML = "<a href=\"javascript:void(0)\" onclick=\"close_adv_search();\"><b>Close advanced search</b></a>";
	document.getElementById("adv_search").style.display = "block";
}

function close_adv_search()
{
  document.getElementById("adv_search").style.display = "none";
  document.getElementById("tod_yest").innerHTML = "<a href=\"javascript:void(0);\" onclick=\"javascript:call_adv_search();\"><b>Advanced search</b></a>";
}
//Warning (fake, virus ect...)------------------------------------------------------------------------
function warning(z)
{
  if(z=='fake')
  {
    window.alert("Warning this torrent has been voted as fake");
  }
  else if(z=='password')
  {
	window.alert("Warning this torrent has been voted as password protected");
  }
  else if(z=='virus')
  {
	window.alert("Warning this torrent has been voted as virus");
  }
  else
  {
	//do nothing  
  }
}

//------

function comfirmAction(z)
{
  var z;
  var is_sure = window.confirm("Are you sure that you want to do it?");
  
  if(is_sure == true)
  {
    window.location = z;
  }
}

//---
function addOpenSearch(name, pid, meth)
{
  if ((typeof window.external == "object") && ((typeof window.external.AddSearchProvider == "unknown") || (typeof window.external.AddSearchProvider == "function"))) 
  {
    // See bugs 430058/430067/430070 for Camino
    if (((typeof window.external.AddSearchProvider == "unknown") || (window.navigator.vendor == 'Camino'))&& meth == "p") {
      alert("This plugin uses POST which is not currently supported by your browser's implementation of OpenSearch.");
    } else {
      window.external.AddSearchProvider(
        "http://mycroft.mozdev.org/installos.php/"+pid+"/"+name+".xml");
    }
  } else {
    alert("You will need a browser which supports OpenSearch to install this plugin.");
  }
}

//---


//---

function show_info(z, x)
{
  var objSpan = document.getElementById(z);
  
  if(objSpan.style.display == 'block')
  {
	 objSpan.style.display = 'none';
  
     var objImage = document.getElementById(x);
     objImage.style.backgroundImage = "url(/images/expand.gif)";
     objImage.style.backgroundRepeat = "no-repeat";
  }
  else
  {
     objSpan.style.display = 'block';
  
     var objImage = document.getElementById(x);
     objImage.style.backgroundImage = "url(/images/shrink.gif)";
     objImage.style.backgroundRepeat = "no-repeat";
  }
}
