function show_login()
{
	document.getElementById('bloc_login').style.visibility = "visible";
}

function changePage(page)
{
	 document.getElementById('page').value = page;
	 document.forms.form.submit();
}

function showPub()
{
	document.getElementById("pub").style.display = "block";
	document.getElementById("pub2").style.display = "block";
}

function Display()
{
	document.getElementById('content').style.left = (screen.width-960) / 2 + "px";
	document.getElementById('content').style.display = "block";
}

function check(ext, update)
{
var filePath = document.getElementById('torrent').value;
	if (filePath.indexOf(ext) == -1)
	{
		alert("The file extension should be .torrent");
		filePath = '';
	}
	else if (update)
	{
		var extOff = filePath.replace('.torrent','');
		var getPath = extOff.lastIndexOf('\\');
		var updatedName = extOff.substring((getPath + 1), extOff.length);
		document.getElementById('name').value = updatedName;
	}
}

function checkpicture()
{
	var imgPath = document.getElementById('picture');
	if ((imgPath.value.indexOf('.jpg') == -1) && (imgPath.value.indexOf('.gif') == -1) && (imgPath.value.indexOf('.png') == -1) && (imgPath.value.indexOf('.bmp') == -1) && (imgPath.value.indexOf('.ico') == -1) && (imgPath.value.indexOf('.jpeg') == -1) && (imgPath.value.indexOf('.JPG') == -1) && (imgPath.value.indexOf('.GIF') == -1) && (imgPath.value.indexOf('.PNG') == -1) && (imgPath.value.indexOf('.BMP') == -1) && (imgPath.value.indexOf('.ICO') == -1) && (imgPath.value.indexOf('.JPEG') == -1))
	{
		alert("The file should be a image!");
		return false;
	}
}


function checkFormOut()
{
	var filePath = document.getElementById('torrent');
	var name = document.getElementById('name');
	var desc = document.getElementById('description');
	var mainCat = document.getElementById('mainCatSend');
	var subCat = document.getElementById('subCatSend');
	
	if (filePath.value.length == 0)
	{
		alert("Enter a torrent file please.");
		return false;
	}
	if (filePath.value.indexOf('.torrent') == -1)
	{
		alert("The file extension should be .torrent.");
		return false;
	}
	if (name.value.length == 0)
	{
		alert("Enter a name for your torrent please.");
		return false;
	}
	if (desc.value.length == 0)
	{
		alert("Enter a description for your torrent please.");
		return false;
	}
	if (mainCat.value.length == 0 || subCat.value.length == 0)
	{
		alert("You got to select a main category and a sub-category.");
		return false;
	}
		
	document.forms.upload_form.submit();
}


function getSelected(id)
{
	selectedCat = document.getElementById(id).value;
	searchString = '<option value="' + selectedCat + '">';
	html = document.getElementById(id).innerHTML;
	getIndex = html.indexOf(searchString);
	htmlBis = html.substr(getIndex,html.length);
	getIndexBis = htmlBis.indexOf('</option>');
	value = htmlBis.substr(searchString.length,getIndexBis - searchString.length);
	idSend = id + "Send";
	document.getElementById(idSend).value = value;
}

function expand(torrentId)
{
	obj = document.getElementById("torrent"+torrentId);
	
	if (obj.style.display == "none")
	{
		obj.style.display = "";
		document.getElementById('img'+torrentId).src = '/images/cross_m.gif' ;
		sndReqDetails('additional', torrentId, torrentId);
	}	
	else 
	{	
		obj.style.display = "none";
		document.getElementById('img'+torrentId).src = '/images/cross_p.gif' ;
	}	
}

function catexpand(catId)
{
	obj = document.getElementById("cat"+catId);
	
	if (obj.style.display == "none")
	{
		obj.style.display = "";
		document.getElementById('img'+catId).src = '/images/cross_m.gif' ;
		sndReqDetails('categories', catId, catId);
	}	
	else 
	{	
		obj.style.display = "none";
		document.getElementById('img'+catId).src = '/images/cross_p.gif' ;
	}	
}


function show_previous_box()
{
	obj = document.getElementById('previous_box');
	
	if (obj.style.display == "none")
	{
		obj.style.display = "inline"
		document.getElementById('img_previous').src = '/images/cross_m.gif' ;
	}	
	else 
	{	
		obj.style.display = "none";
		document.getElementById('img_previous').src = '/images/cross_p.gif' ;
	}
}

function vote(note, id)
{
	location.replace("/details.php?id=" + id + "&note=" + note)
}

function ShowStarFilled(id, opt)
{
	for(i=1;i<11;i++)
	document.getElementById("star" + i).src = "/img/star_empty.gif";
	
	if (opt == 0)
	{
		for (i=1;i<=id;i++)
		document.getElementById("star" + i).src = "/img/star_fulled.gif";
	}
	else
	{
		for (i=1;i<=id;i++)
		document.getElementById("star" + i).src = "/img/star_full.gif";
	}
}

function echo(show, hide, hide2)
{
	//alert(str);
	document.getElementById(show).style.display = '';	
	document.getElementById(hide).style.display = 'none';
	document.getElementById(hide2).style.display = 'none';
}

// A partir dici faut copier
// Function for check the contact form
function emailCheck (emailStr) 
{

var emailPat=/^(.+)@(.+)$/

var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"

var validChars="\[^\\s" + specialChars + "\]"

var quotedUser="(\"[^\"]*\")"

var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/

var atom=validChars + '+'

var word="(" + atom + "|" + quotedUser + ")"
// The following pattern describes the structure of the user
var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
/* The following pattern describes the structure of a normal symbolic
   domain, as opposed to ipDomainPat, shown above. */
var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")


/* Finally, let's start trying to figure out if the supplied address is
   valid. */

/* Begin with the coarse pattern to simply break up user@domain into
   different pieces that are easy to analyze. */
var matchArray=emailStr.match(emailPat)
if (matchArray==null) 
{
  /* Too many/few @'s or something; basically, this address doesn't
     even fit the general mould of a valid e-mail address. */
	alert("Email address seems incorrect (check @ and .'s)")
	return false
}
var user=matchArray[1]
var domain=matchArray[2]

// See if "user" is valid 
if (user.match(userPat)==null) 
{
    // user is not valid
    alert("The username doesn't seem to be valid.")
    return false
}

/* if the e-mail address is at an IP address (as opposed to a symbolic
   host name) make sure the IP address is valid. */
var IPArray=domain.match(ipDomainPat)
if (IPArray!=null) {
    // this is an IP address
	  for (var i=1;i<=4;i++) {
	    if (IPArray[i]>255) {
	        alert("Destination IP address is invalid!")
		return false
	    }
    }
    return true
}

// Domain is symbolic name
var domainArray=domain.match(domainPat)
if (domainArray==null) {
	alert("The domain name doesn't seem to be valid.")
    return false
}

var atomPat=new RegExp(atom,"g")
var domArr=domain.match(atomPat)
var len=domArr.length
if (domArr[domArr.length-1].length<2 || 
    domArr[domArr.length-1].length>3) {
   // the address must end in a two letter or three letter word.
   alert("The address must end in a three-letter domain, or two letter country.")
   return false
}

// Make sure there's a host name preceding the domain.
if (len<2) {
   var errStr="This address is missing a hostname!"
   alert(errStr)
   return false
}

// If we've gotten this far, everything's valid!
return true;
}

function checkFormContact()
{
	var name = document.forms["cform"].elements["name"];
	var mail = document.forms["cform"].elements["mail"];
	var website = document.forms["cform"].elements["website"];
	var subject = document.forms["cform"].elements["subject"];
	var message = document.forms["cform"].elements["message"];
	
	if (name.value.length == 0)
	{
		alert("Enter your name please.");
		name.focus();
		name.select();
		return false;
	}
	if (mail.value.length == 0)
	{
		alert("Enter your email please.");
		mail.focus();
		mail.select();
		return false;
	}
	if (message.value.length == 0)
	{
		alert("Write your message please.");
		message.focus();
		message.select();
		return false;
	}
	if (!emailCheck(mail.value))
		return false;
	
	document.cform.submit();
}

function checkFormUser()
{
	var login = document.forms["uform"].elements["login"];
	var mail = document.forms["uform"].elements["mail"];
	var password = document.forms["uform"].elements["password"];
	var password2 = document.forms["uform"].elements["password2"];
	
	if (login.value.length == 0)
	{
		alert("Enter your name please.");
		login.focus();
		login.select();
		return false;
	}
	else if (login.value.length < 5 || login.value.length > 255) 
	{
		alert("Your Login must comprise between 5 and 255 characters");
		login.focus();
		login.select();
		return false;
	} 
	if (mail.value.length == 0)
	{
		alert("Enter your email please.");
		mail.focus();
		mail.select();
		return false;
	}
	if (password.value.length == 0)
	{
		alert("Enter your password please.");
		password.focus();
		password.select();
		return false;
	}
	if (password2.value.length == 0)
	{
		alert("Confirm your password please.");
		password2.focus();
		password2.select();
		return false;
	}
	if (password.value != password2.value)
	{
		alert("Please verify your password!");
		password.focus();
		password.select();
		return false;
	}
	if (password.value.length < 6)
	{
		alert("Password must be more than 6 characters!");
		password.focus();
		password.select();
		return false;
	}
	if (!emailCheck(mail.value))
		return false;
	
	document.uform.submit();
}

function modifFormUser()
{
	var login = document.forms["modifForm"].elements["new_login"];
	var mail = document.forms["modifForm"].elements["new_mail"];
	var password = document.forms["modifForm"].elements["new_password"];
	var password2 = document.forms["modifForm"].elements["new_password2"];
	var client = document.forms["modifForm"].elements["new_bt_client"];
	
	if (login.value.length == 0 && password.value.length == 0 && password2.value.length == 0 && mail.value.length == 0 && client.value.length == 0)
	{//Si ya rien de modifier
		alert("Nothing needs to be change");
		return false;
	}
	
	if (password2.value.length != 0 && password.value.length == 0)
	{
		alert("Please enter your new password before confirm this!");
		password.focus();
		password.select();
		return false;
	}
	
	if (login.value.length != 0 ) //Si un new login est saisi
	{
		if(login.value.length < 5 || login.value.length > 255)
		{
			alert("Your New Login must comprise between 5 and 255 characters");
			login.focus();
			login.select();
			return false;
		}
	} 
	
	if (password.value.length != 0) //Si un new password est saisi
	{
		if (password2.value.length == 0)
		{
			alert("Confirm your password please.");
			password2.focus();
			password2.select();
			return false;
		}
		if (password.value != password2.value)
		{
			alert("Please verify your password!");
			password.focus();
			password.select();
			return false;
		}
		if (password.value.length < 6)
		{
			alert("Password must be more than 6 characters!");
			password.focus();
			password.select();
			return false;
		}
	}
	
	if (mail.value.length != 0) //Si un new email est saisi
	{
		if (!emailCheck(mail.value))
			return false;
	}
	
	document.modifForm.submit();
}

function customFormUser()
{
	var per_page = document.forms["customForm"].elements["per_page"];
	
	if (per_page.value.length != 0 && per_page.value < 1) //Si un nombre est saisi
	{
		alert("Number per page must be '1' at least!");
		return false;
	}
		
	document.customForm.submit();
}

function verifAlert()
{
	var new_alert = document.forms["alertsForm"].elements["newAlert"];
	
	if (new_alert.value.length == 0 || new_alert.value == " ")
	{
		alert("Please enter at least one alert!");
		return false;
	}
	
	document.alertsForm.submit();
}

function verifCreatePwd()
{
	var password = document.forms["createPassForm"].elements["new_pass"];
	var password2 = document.forms["createPassForm"].elements["new_pass2"];
	
	if (password.value.length == 0)
	{
		alert("Enter your password please.");
		password.focus();
		password.select();
		return false;
	}
	if (password2.value.length == 0)
	{
		alert("Confirm your password please.");
		password2.focus();
		password2.select();
		return false;
	}
	if (password.value != password2.value)
	{
		alert("Please verify your password!");
		password.focus();
		password.select();
		return false;
	}
	if (password.value.length < 6)
	{
		alert("Password must be more than 6 characters!");
		password.focus();
		password.select();
		return false;
	}
		
	document.createPassForm.submit();
}


		var timeoutID=0;

		function createRequestObject() {
		    var ro;
		    var browser = navigator.appName;
		    if(browser == "Microsoft Internet Explorer"){
		        ro = new ActiveXObject("Microsoft.XMLHTTP");
		    }else{
		        ro = new XMLHttpRequest();
		    }
		    return ro;
		}

		var http = createRequestObject();
		// hack to make the script xhtml compliant

		function sndReqDetails(req,search, divname) 
		{
			if (search != '')
			{
				http.open('get', '/ajax.php?request='+req+'&search='+search+'&divname='+divname);
				var obj = document.getElementById('i_'+divname);
				obj.style.display = "block";
				http.onreadystatechange = handleResponse;
				http.send(null);
			}
		}




		function handleResponse() {
			if(http.readyState == 4){
				var response = http.responseText;
				var update = new Array();
				
				if(response.indexOf('|' != -1)) {
					update = response.split('|');
					var obj = document.getElementById(update[0]);
						
					if (obj != null)
						obj.innerHTML = update[1];			

				}
			}
		}

		function cache(text) {
			var obj =  document.getElementById('i_suggest');
			
			if (text == "")
			{
				obj.style.display = "none" ;   
				obj.innerHTML = '<p><br /><img src="/images/load.gif" alt="..." />&nbsp;Please type your search and wait for suggestions to appear ...<br /><br /></p>';
			}
		}	

function emptyList(box)
{
	while(box.options.length)box.options[0]=null;
}

function fillList(box,arr)
{
	for(i=0;i<arr[0].length;i++)
	{
		option=new Option(arr[0][i],arr[1][i]);box.options[box.length]=option;
	}
	box.selectedIndex=0;
}

function changeList(box)
{
	list=lists[box.options[box.selectedIndex].value];
	emptyList(box.form.lngSubCat);
	fillList(box.form.lngSubCat,list);
}



function file(fichier)
{
	if(window.XMLHttpRequest) // FIREFOX
		xhr_object = new XMLHttpRequest();
	else if(window.ActiveXObject) // IE
		xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
	else
		return(false);
	xhr_object.open("GET", fichier, false);
	xhr_object.send(null);
	if(xhr_object.readyState == 4) 
		return(xhr_object.responseText);
	else 
		return(false);
}

function writediv(texte) //permet de specifier les renseignements sur le login saisi(AJAX)
{
	document.getElementById('pseudobox').innerHTML = texte;
}

function writedivpass(texte) //permet de specifier les renseignements sur le password saisi(AJAX)
{
	document.getElementById('passwdbox').innerHTML = texte;
}

function writeExist(texte) //permet de dire si le pseudo existe (AJAX)
{
	document.getElementById('existbox').innerHTML = texte;
}

function writeCorresp(texte) //permet de dire si l'email correspond au pseudo (AJAX)
{
	document.getElementById('mailcorbox').innerHTML = texte;
}

var texte = 0; //on initialise la variable texte en variable globale pour pouvoir l'utilser ds tt le script

function existPseudo(pseudo)
{
	if(pseudo != '')
	{
		if(texte = file('ajax.php?pseudo='+escape(pseudo)))
		{
			if(texte == 1)
				writeExist('<span style="color:#1A7917;font-size:12px"><b>"'+pseudo+'" :</b> User exists</span>');
			else if(texte == 2)
				writeExist('<span style="color:#cc0000;font-size:12px"><b>"'+pseudo+'" :</b> User doesn\'t exist !!</span>');
			else
				writeExist(texte);
		}
	}
}

function mailConcord(email, login)
{
	if(email != '' && login != '')
	{
		if(texte = file('ajax.php?email='+escape(email)+'&login='+escape(login)))
		{
			if(texte == 1)
				writeCorresp('<span style="color:#cc0000;font-size:11px"><b>"'+email+'" :</b> Wrong email address for the login <b>"'+login+'"</b> !!</span>');
			else if(texte == 2)
				writeCorresp('<span style="color:#1A7917;font-size:11px"><b>"'+email+'" :</b> Email address corresponds to the login <b>"'+login+'"</b></span>');
			else
				writeCorresp(texte);
		}
	}
	else if(email.length >= 1 && (login == '' || login.lenght == 0))
		alert ("Please enter your login before !");
}

function verifPseudo(pseudo)
{
	if(pseudo != '')
	{
		if(pseudo.length<5)
			writediv('<span style="color:#cc0000"><b>"'+pseudo+'" :</b> Login too short</span>');
		else if(pseudo.length>255)
			writediv('<span style="color:#cc0000"><b>"'+pseudo+'" :</b> Login too long</span>');
		else if(texte = file('ajax.php?pseudo='+escape(pseudo)))
		{
			if(texte == 1)
				writediv('<span style="color:#cc0000;font-size:14px"><b>"'+pseudo+'" :</b> Login already used !!</span>');
			else if(texte == 2)
				writediv('<span style="color:#1A7917"><b>"'+pseudo+'" :</b> Login free</span>');
			else
				writediv(texte);
		}
	}
}

function verifPass(password)
{
	var texte = 1;//juste pour pouvoir afficher le msg
	if(password != '')
	{
		if(password.length<6)
			writedivpass('<span style="color:#cc0000">Password too short</span>');
		else if(password.length>50)
			writedivpass('<span style="color:#cc0000">Password too long</span>');
		else if(texte == 1)
			writedivpass('<span style="color:#1A7917">Right size</span>');
	}
}


function getSelectedCat(id)
{
	selectedCat = document.getElementById(id).value;
	searchString = '<option value="' + selectedCat + '">';
	html = document.getElementById(id).innerHTML;
	getIndex = html.indexOf(searchString);
	htmlBis = html.substr(getIndex,html.length);
	getIndexBis = htmlBis.indexOf('</option>');
	value = htmlBis.substr(searchString.length,getIndexBis - searchString.length);
	idSend = id + "Send";
	document.getElementById(idSend).value = value;
}
/*
function writedivpics(image) //permet d'afficher les images(AJAX)
{
	document.getElementById('picsBox').innerHTML = image;
}

function show_pics(catId) //Pour afficher les images des categories
{
	var image = file('/ajax.php?cat_id='+catId);
	writedivpics(image);
}
				
function onLoadCatId() //chargement de la page
{	
	var defaultCat = file('/ajax.php?verifDefaultCat=1'); //cat_id a afficher par defaut
	show_pics(defaultCat);
}*/

function OnTextChange(html_obj) //Changement du lien dans le href pour le comment preview
{			
	document.getElementById('comment').value = html_obj.value;
	
	var i = 0;
	var href = '';
	var form = document.getElementById('sendcomment');
	var all_a = form.getElementsByTagName('a');
	for (; i < all_a.length; ++i)
		if (all_a.item(i).className == 'lbOn')
		{
			href = all_a.item(i).href.toString();
			break;
		}
		
	if (href.length)
	{
		if (href.indexOf('?') != -1)
			href += '&';
		else
			href += '?';
		
		var idx = href.indexOf('comment');
		if (idx != -1)
			href = href.substr(0, idx);
							
		href += 'comment=' + escape(html_obj.value);
		all_a.item(i).href = href;
	}
}

function highlight(field) {        field.focus();        field.select();}