var cur_table = 0;
var cur_rch = 1;
var cur_ads = 0;
var arr_rch_kit = new Array();
var arr_rch_cat = new Array();
var arr_rch_typ = new Array();
var arr_rch_gen = new Array();
var arr_rch_color = new Array();
var arr_action_id =  new Array();
var nbs_rch = 0;
var is_adult = 0;
var typ_adult = 0;
var aff_adult = -1;
var arr_img = new Array(null,null,null,null,null,null,null,null,null,null);
var pg_ref = 0;
var pg_form_head = 0;
var cur_select_color = new Array();
var arr_fait_aff = new Array();
var arr_ls_aff = new Array();
var timeout = false;
var freq=8000;

function OpenRef(ref)
{
	window.open(arr_open_part[ref][0],'fiche','');
	PutStatElt(arr_open_part[ref][1],arr_open_part[ref][2]);
}
function ActionAdultOpen()
{
	AffRes(action_cur_rch,0,1);
}
function ActionAdultIn()
{
	aff_adult = 1;
	AffRes(action_cur_rch,0,1);
	document.getElementById('fen_add_div').style.visibility="hidden";
	FenCloseBlack();
	ChangeTable(1);
	ChangeRch(typ_adult);
}
function ActionAdultOut()
{
	aff_adult = 0;
	AffRes(action_cur_rch,0,0);
	FenCloseBlack();
	document.getElementById('fen_add_div').style.visibility="hidden";
	ChangeTable(1);
	ChangeRch(typ_adult);
}
function OpenFirst(nb)
{
	window.open(arr_open_first[nb],'fiche','');
	if(arr_code_first[nb]!=null)
		PutStatElt(arr_code_first[nb][0],arr_code_first[nb][1]);
}
function OpenFiche(nb)
{
	window.open(arr_open_fich[nb],'fiche','');
	if(arr_code_fich[nb]!=null)
		PutStatElt(arr_code_fich[nb][0],arr_code_fich[nb][1]);
}
function OpenAnim(nb)
{
	window.open(arr_open_anim[nb],'anim','');
}
function PutAff(ids)
{

	if(arr_fait_aff[ids] == undefined)
	{
		if(arr_open_aff[ids] == undefined)
			arr_fait_aff[ids] = 2;
		else
		{
			arr_fait_aff[ids] = 1;
			arr_ls_aff.push(ids);
			if(!timeout)
			{
				ActionAff();
				timeout =  setInterval("TimerAff()", freq);
			}
		}
	}	
}
function TimerAff()
{
	if(arr_ls_aff.length == 0)
	{
		clearInterval(timeout);
		timeout = false;
	}
	else
	{
		 ActionAff();
	}
}
function ActionAff()
{
	if(arr_ls_aff.length > 0)
	{
		document.getElementById('main').src=arr_open_aff[arr_ls_aff[0]];		
		window.status ="";
		arr_ls_aff.shift();
	}
}
function ModLeftColor(ids,nb)
{
	var next = 0;
	if(ids == 0)
		next = 1;
	document.getElementById('check_color_'+next+'_'+nb).checked = document.getElementById('check_color_'+ids+'_'+nb).checked;
}
function ModClickColor(ids,nb)
{
	if(document.getElementById('check_color_'+ids+'_'+nb).checked)
	{
		document.getElementById('check_color_0_'+nb).checked = false;
		document.getElementById('check_color_1_'+nb).checked = false;
	}
	else
	{
		document.getElementById('check_color_0_'+nb).checked = true;	
		document.getElementById('check_color_1_'+nb).checked = true;	
	}
}
function OpenURL(url,ids)
{
	window.open(url,'anim','');
	if(ids!=0)
	{
		arr_fait_aff[ids] = 2;
		PutStatElt(ids,"URL");
	}
}
function PutStatElt(ids,code)
{
	var params = new Array(ids,code);
	Ajax('Main','PutStatElt',params,false,new Array());
}
function LanceRch(nbs)
{
	var rbs = 0;
	if(nbs ==0)
		rbs =1;
	var cat=document.getElementById('form_cat_'+nbs).value;
	var typ=document.getElementById('form_typ_'+nbs).value;
	var isrc=document.getElementById('form_src_'+nbs).value;
	var cls=document.getElementById('form_class_'+nbs).value;
	var pxt=document.getElementById('form_pstand_'+nbs).value;
	var pxe=document.getElementById('form_pexclu_'+nbs).value;
	var lib=document.getElementById('form_lib_'+nbs).value;
	
	document.getElementById('form_cat_'+rbs).selectedIndex = document.getElementById('form_cat_'+nbs).selectedIndex;
	document.getElementById('form_typ_'+rbs).selectedIndex = document.getElementById('form_typ_'+nbs).selectedIndex;
	document.getElementById('form_src_'+rbs).selectedIndex = document.getElementById('form_src_'+nbs).selectedIndex;
	document.getElementById('form_class_'+rbs).selectedIndex = document.getElementById('form_class_'+nbs).selectedIndex;
	document.getElementById('form_pstand_'+rbs).selectedIndex = document.getElementById('form_pstand_'+nbs).selectedIndex;
	document.getElementById('form_pexclu_'+rbs).selectedIndex = document.getElementById('form_pexclu_'+nbs).selectedIndex;
	document.getElementById('form_lib_'+rbs).value=lib;
	var color="";
	for(var n=0;n<cur_select_color.length;n++)
	{
		if(cur_select_color[n]==1)
		{
			if(color!="")
				color+=':';
			color+=n;
		}
	}
	ActionRch(cat,typ,isrc,cls,pxt,pxe,lib,0,color);
}
function OpenGen(ids)
{
	action_cur_rch = arr_rch_gen[ids];
	AffRes(action_cur_rch,0);
	ChangeTable(1);
	ChangeRch(0);
}
function OpenTyp(typ)
{
	if(arr_rch_typ[typ]==undefined)
	{
		ActionRch(-1,typ,-1,-1,-1,-1,"",2,"");
		arr_rch_typ[typ]="action";
	}
	else if(arr_rch_typ[typ]!="action")
	{
		FinalRchTyp(typ);
	}
}
function OpenColor(color)
{
	if(arr_rch_color[color]==undefined)
	{
		if(color == "ALL")
			ActionRch(41,-1,-1,-1,-1,-1,"",3,"");
		else
			ActionRch(41,-1,-1,-1,-1,-1,"",3,color);
		arr_rch_color[color]="action";
	}
	else if(arr_rch_color[color]!="action")
	{
		FinalRchColor(color);
	}
}
function OpenCat(cat)
{
	if(arr_rch_cat[cat]==undefined)
	{
		arr_rch_cat[cat]="action";
		ActionRch(cat,-1,-1,-1,-1,-1,"",1,"");
	}
	else if(arr_rch_cat[cat]!="action")
	{
		FinalRchCat(cat);
	}
	
}
function ValidColor(ids)
{
	var arr = new Array();
	for(n=0;n<10;n++)
	{
		if(document.getElementById('check_color_'+ids+'_'+n).checked)
			arr[n] = 1;
		else
			arr[n] = 0;
	}
	cur_select_color = arr;
	AffFormColor();
	ModFormHead(ids,0);
}
function SupFormColor()
{
	for(n=0;n<10;n++)
	{
		document.getElementById('check_color_0_'+n).checked = false;
		document.getElementById('check_color_1_'+n).checked = false;
		cur_select_color[n] = 0;
	}
	AffFormColor();
}
function AffFormColor()
{
	var bl = false;
	for(n=0;n<cur_select_color.length;n++)
	{
		if(cur_select_color[n] == 1)
		{
			bl = true;
			document.getElementById('img_in_form_0_'+n).style.display = "inline";	
			document.getElementById('img_in_form_1_'+n).style.display = "inline";	
		}
		else
			{
			document.getElementById('img_in_form_0_'+n).style.display = "none";	
			document.getElementById('img_in_form_1_'+n).style.display = "none";	
		}
	}
	if(bl)
	{
		document.getElementById('return_in_form_0').style.display = "";	
		document.getElementById('click_lib_color_0').style.display = "none";	
		document.getElementById('return_in_form_1').style.display = "";	
		document.getElementById('click_lib_color_1').style.display = "none";	
	}
	else
	{
		document.getElementById('click_lib_color_0').style.display = "";	
		document.getElementById('return_in_form_0').style.display = "none";	
		document.getElementById('click_lib_color_1').style.display = "";	
		document.getElementById('return_in_form_1').style.display = "none";	
	}
}

function ModFormHead(ids,nb)
{
	if(nb != pg_form_head)
	{
		document.getElementById('rech_form_head_0_'+nb).style.display = "";	
		document.getElementById('rech_form_head_0_'+pg_form_head).style.display = "none";	
		document.getElementById('rech_form_head_1_'+nb).style.display = "";	
		document.getElementById('rech_form_head_1_'+pg_form_head).style.display = "none";	
		pg_form_head = nb;
	}
}

function ModImgForm(form,val,ids)
{
	if(val == 0)
		next = 1;
	else
		next = 0;
	document.getElementById('img_form_'+form+'_'+next+'_'+ids).style.display = "";	
	document.getElementById('img_form_'+form+'_'+val+'_'+ids).style.display = "none";	
}
function ActionRch(cat,typ,src,cla,prx_st,prx_ex,rch_lib,fin,color)
{
	var params = new Array(cat,typ,src,cla,prx_st,prx_ex,rch_lib,bs_lang,fin,color);
	var arr = new Array();
	if(bs_lang == 1)
		arr = new Array("Chargement","de la recherche");
	else 				
		arr = new Array("Loading","Search"); 
	Ajax('Main','SubmiRch',params,true,arr);
}
function FinalRchGen(ids)
{	
	typ_adult = 0;
	action_cur_rch = arr_rch_gen[ids];
	if(ids<25)
	{
		
		document.getElementById('rch_elt_'+ids).style.display = "";
		document.getElementById('ids'+ids).innerHTML = action_cur_rch.length;
	}
	document.getElementById('no_ls_rech').style.display = "none";	
	if(TestAdult())
	{
		AffRes(action_cur_rch,0,aff_adult);
		ChangeTable(1);
		ChangeRch(0);
	}
}
function ChangeLG(id)
{
	window.location='?LG='+id;
}
function AdminNoValid(id)
{
	var params = new Array();
	params.push(arr_action_id[id]);
	Ajax('Main','AdminNoValid',params,true,new Array("Suppression","de la validation"));
}
function AffFenAdult()
{	
	var fen=document.getElementById('fen_add_div');
	if(fen !=undefined)
	{
		FenOpenBlack();
		InitPosFenCh(fen);
		fen.style.visibility="visible";
		open_add = true;
	}
}
function TestAdult()
{
	if(is_adult == 1 && aff_adult == -1)
	{
		AffFenAdult();
		return false;
	}
	else return true;
}
function FinalRchTyp(ids)
{
	action_cur_rch = arr_rch_typ[ids];
	typ_adult = 2;
	if(TestAdult())
	{
		AffRes(action_cur_rch,0,aff_adult);
		ChangeTable(1);
		ChangeRch(2);
	}
}function FinalRchColor(ids)
{
	action_cur_rch = arr_rch_color[ids];
	typ_adult = 2;
	if(TestAdult())
	{
		AffRes(action_cur_rch,0,aff_adult);
		ChangeTable(1);
		ChangeRch(3);
	}
}
function FinalRchCat(ids)
{
	action_cur_rch = arr_rch_cat[ids];
	typ_adult = 1;
	if(TestAdult())
	{
		AffRes(action_cur_rch,0,aff_adult);
		ChangeTable(1);
		ChangeRch(1);
	}
}
function AffRchPg(nb)
{
		AffRes(action_cur_rch,nb,aff_adult);
}
function Mail(ilang)
{
	var alert_1 = "";
	var alert_2 = "";
	var mess_1 = "";
	var mess_2 = "";
	if(bs_lang == 1)
	{
		alert_1 = "Votre courriel semble invalide.";
		alert_2 = "Votre message semble vide.";
	  mess_1 = "Envoi du";
	  mess_2 = "message";
	}
	else
	{		
		alert_1 = "Your email seems invalid.";
		alert_2 = "Your message seems empty.";
	  mess_1 = "Sending of";
	  mess_2 = "message";
	}
	var mail = document.getElementById('mail_email').value;
	var lib = document.getElementById('mail_lib').value;
	var txt = document.getElementById('mail_txt').value;
	if(!ValidEmail(mail))
		alert(alert_1);
	else if(txt == "")
		alert(alert_2);
	else
	{
		var params = new Array(mail,lib,txt,ilang);
		Ajax('Main','SubmiMail',params,true,new Array(mess_1,mess_2));
	}
}
function EndMail(lang)
{
	
	var mess_1 = "";
	if(bs_lang == 1)
		mess_1 = "Votre message a bien "+Chr(130)+"t"+Chr(130)+" envoy"+Chr(130)+".";
	else 
		mess_1 = "Your message has been sent.";
	alert(mess_1);
	document.getElementById('mail_lib').value="";
	document.getElementById('mail_txt').value="";
}
function AffRes(arr,pg,adult)
{
	var bas = 0;
	var max = 1;
	bas = pg * 10;
	max = pg * 10 + 10;
	var mod_adult = 0;
	var cont_adult = false;
	if(adult == 0)
	{
		var adult = new Array();
		for(n=0;n<arr.length;n++)
		{
			if(arr[n][3] != 37)
			{
				adult.push(arr[n]);
			}
			else
			{
				mod_adult++;
				cont_adult=true;
			}
		}
		arr = adult;
	}
	else
	{
		for(n=0;n<arr.length;n++)
		{
			if(arr[n][3] == 37)
			{
				cont_adult=true;
				break;
			}
		}
	}
	if(max > 	arr.length)
		max = arr.length;
	var nb = 0;
	for(n=bas;n<max;n++)
	{
		var in_name = "IMG_KITS_"+arr[n][1]+"_"+arr[n][9]+".png";
		var next_name = document.getElementById('aff_img_'+nb).src;
		var tbs = next_name.split('/');
		next_name = tbs[tbs.length-1];
	  if(in_name != next_name)
			document.getElementById('aff_img_'+nb).src = "./Images/charge.gif";
		nb++;
	}
	nb = 0;
	for(n=bas;n<max;n++)
	{
		var sec = "";
		var ids = arr[n][1]; 
		var typ = arr[n][2]; 
		var cat = arr[n][3]; 
		PutAff(ids);
		arr_open_fich[nb] = arr[n][7]; 
		arr_action_id[nb] = arr[n][0]; 
		arr_code_fich[nb]=new Array(ids,arr[n][9]);
		if(arr_lib_typ[typ] != undefined) 
		{
			if(sec != "")
				sec += " - ";
			sec += '<span class="blue">'+arr_lib_typ[typ]+'</span>'	;				
		}
		if(arr_lib_cat[cat] != undefined) 
		{
			if(sec != "")
				sec += " - ";
			sec += '<span class="blue">'+arr_lib_cat[cat]+'</span>'	;				
		}
		var name = "./Caps/IMG_KITS_"+ids+"_"+arr[n][9]+".png";
		if(name != document.getElementById('aff_img_'+nb).src)
		{
			arr_img[nb]= new Image();
			arr_img[nb].src = name;	
			eval('arr_img['+nb+'].onload=function(e){document.getElementById("aff_img_'+nb+'").src = "'+name+'";}');
		}
			
		document.getElementById('aff_cont_rch_'+nb).style.display = "";
		
		var ng = "";
		if(bs_lang == 0 && arr_lang_src[ids] == 1)
		 	ng='&nbsp;&nbsp;<img src="./Images/fr_mini.png">';
		document.getElementById('aff_lib_rch_'+nb).innerHTML = arr[n][6]+ng;
		
		var nis ="";
		if(bs_lang == 1)
			nis = "Ouvrir sur ";
		else
			nis = "Open on ";
		
		document.getElementById('but_rch_fic_'+nb).alt = nis+ arr_lib_src[ids];	
		document.getElementById('but_rch_fic_'+nb).title = nis+ arr_lib_src[ids];
		document.getElementById('aff_mini_rch_'+nb).innerHTML = sec;			
		if(arr[n][4] == "Abonnement" || arr[n][4] == "Subscription" )
			document.getElementById('aff_val_stand_'+nb).innerHTML = '<span style="font-size:10px;" class="rouge">'+arr[n][4]+'</span>';
		else if(arr[n][4] == "Gratuit" || arr[n][4] == "Free")
			document.getElementById('aff_val_stand_'+nb).innerHTML = '<span style="font-size:10px;" class="green">'+arr[n][4]+'</span>';
		else
			document.getElementById('aff_val_stand_'+nb).innerHTML = arr[n][4];
		var  sec= 22;
		if(arr[n][5] == "")
		{
			sec+=30;
			document.getElementById('aff_prx_exclu_'+nb).style.display = "none";
		}
		else
		{
			document.getElementById('aff_prx_exclu_'+nb).style.display = "";
			document.getElementById('aff_val_exclu_'+nb).innerHTML = arr[n][5];
		}
		
		if(arr[n][8] == "")
		{
			sec+=55;
			document.getElementById('aff_anim_'+nb).style.display = "none";
		}
		else
		{
			arr_open_anim[nb] = arr[n][8]; 
			document.getElementById('aff_anim_'+nb).style.display = "";
		}
		document.getElementById('aff_line_'+nb).style.height = sec+"px";
		
		nb++;
	}	
	for(n=nb;n<10;n++)
	{
		document.getElementById('aff_cont_rch_'+nb).style.display = "none";
		nb++;
	}
	if(arr.length>10)
	{
		var nbs = Math.ceil(arr.length/10);
		var act = "";
		var sec = 20;
		var cum = Math.ceil(nbs/sec);
		for(n=0;n<cum;n++)
		{
			if(pg >= n * sec &&  pg < n * sec + sec)
			{
				var pmin = n * sec;
				var pmax = n * sec + sec;
				if(pmax >= nbs)
					pmax = nbs;
				for(p=pmin;p<pmax;p++)
				{
						if(pg == p)
							act += '<div onclick="AffRchPg('+p+');" class="count_is_ping">'+(p+1)+'</div>';
						else
							act += '<div onclick="AffRchPg('+p+');" class="count_ping">'+(p+1)+'</div>';
				}
			}
			else
			{
				act += '<div onclick="AffRchPg('+(n* sec)+');" class="count_max_ping">'+(n * sec +1)+'</div>';
			}
		}
		var le = "";
		var ri = "";
		if(pg > 0)
		 le = 'onclick="AffRchPg('+(pg-1)+');"';
		if(pg < nbs)
		 ri = 'onclick="AffRchPg('+(pg+1)+');"';
		var bas = '<div class="count_fin_ping" '+ri+'> >> </div><div class="sel_rch_form" style=""><input size="1" class="input_rch_form" style="" type="text" value="'+(pg+1)+'"> / <input style="height:20px;" onfocus="this.blur()" value="'+nbs+'" size="1" type="text"></div><div class="count_fin_ping" '+le+'> << </div>';
			
		document.getElementById('count_haut_div').style.display = "";
		document.getElementById('class_inner').style.display = "";
		document.getElementById('count_haut').innerHTML = act;
		document.getElementById('class_inner').innerHTML = bas;
	}
	else
	{	
		document.getElementById('count_haut_div').style.display = "none";
		document.getElementById('class_inner').style.display = "none";
	}
	
	if(arr.length>0)
	{
		document.getElementById('no_recherche').style.display = "none";	
	}
	else
	{
		if(bs_lang == 1)
			document.getElementById('no_res').innerHTML= "Aucun r&eacute;sultat pour cette recherche.";	
		else
				document.getElementById('no_res').innerHTML= "No results for this search.";	
		document.getElementById('no_recherche').style.display = "";	
		document.getElementById('adult_recherche').style.display = "none";
	}	
		
		if(!cont_adult)
		{
			document.getElementById('adult_recherche').innerHTML ="";
			document.getElementById('adult_recherche').style.display = "none";	
		}
		else if(adult == 0)
		{
			if(bs_lang == 1)
				document.getElementById('adult_recherche').innerHTML = mod_adult+' r&eacute;sultat(s) de la cat&eacute;gorie "Adulte" ont &eacute;t&eacute; filtr&eacute;s, pour les afficher <span class="links" onclick="ActionAdultOpen()">cliquez ici</span>. <br>Pour d&eacute;sactiver le filtre automatique  <span class="links"  onclick="ActionAdultIn()">cliquez ici</span>.';	
			else
				document.getElementById('adult_recherche').innerHTML = mod_adult+' results from the "adult" category have been screened. To display them, please click here. To remove the automatic screening, please  <span class="links" onclick="ActionAdultOpen()">click here</span>. <br>To disable the automatic screening of templates from the "adult" category, please <span class="links"  onclick="ActionAdultIn()">click here</span>.';	
			document.getElementById('adult_recherche').style.display = "";
		}
		else if(adult == 1)
		{
			if(bs_lang == 1)
				document.getElementById('adult_recherche').innerHTML = 'Pour activer le filtre automatique des kits graphiques de la cat&eacute;gorie "Adulte" <span class="links" onclick="ActionAdultOut()">cliquez ici</span>.';
			else
				document.getElementById('adult_recherche').innerHTML = 'To activate the automatic screening of templates from the "adult" category, please <span class="links" onclick="ActionAdultOut()">click here</span>.';
			document.getElementById('adult_recherche').style.display = "";
		}
}
function AffAnnPg(id)
{
	if(pg_ref!=id)
	{
		document.getElementById('pg_annu_'+id).style.display = "";	
		document.getElementById('pg_annu_'+pg_ref).style.display = "none";	
		document.getElementById('css_'+id).style.color='orange'; 
		document.getElementById('css_'+pg_ref).style.color='black'; 	
		pg_ref = id;
	}
}
function ChangeAide(id)
{
	if(id != cur_ads)
	{
		document.getElementById('ads_'+id).style.display = "";	
		document.getElementById('menu_ads_'+id).style.color = "black";	
		document.getElementById('ads_'+cur_ads).style.display = "none";	
		document.getElementById('menu_ads_'+cur_ads).style.color = "#636363";	
		cur_ads = id;
	}
}
function ChangeTable(id)
{
	if(id != cur_table)
	{
		document.getElementById('table_'+id).style.display = "";	
		document.getElementById('table_'+cur_table).style.display = "none";	
		window.scrollTo(0,0);
		cur_table = id;
	}
}
function ChangeRch(id)
{
	if(id != cur_rch)
	{
		document.getElementById('cont_page_'+id).style.display = "";	
		document.getElementById('img_meta_'+id).src= './Images/ping_bs.png';
		document.getElementById('cont_page_'+cur_rch).style.display = "none";	
		document.getElementById('img_meta_'+cur_rch).src= './Images/ping_dr.png';
		cur_rch = id;
	}
}