var contentType = "";
var storeUrl = "";
var currentLng = 0;
var lngBo="";
var tbText = new Array();
function fxInit(tabTxt, type, url, tbLng, curLng, media_path, full_media_path, md5_media_path, lngBo)
{
	contentType = type;
	storeUrl = url;
	currentLng = curLng;
	tbText = tabTxt;
	var listbox = document.getElementById("lngCombo");
	
	fxResetList(listbox);
	//alert(currentLng);
	for (key in tbLng)
	{
		if(key>0 && tbLng[key]!=undefined) {
			var idx = listbox.options.length;
			listbox.options[idx] = new Option(tbLng[key], key);
			if(currentLng==key)
				listbox.options[idx].selected = true;
		}
		
	}
	
	fxCreateEditor(media_path, full_media_path, md5_media_path, lngBo);
	
	document.getElementById("flexContent").style.minWidth = "0px";
	document.getElementById("flexContent").style.minHeight = "0px";
	document.getElementById("flexContent").style.width = "0px";
	document.getElementById("flexContent").style.height = "0px";
	
	document.getElementById("fck").style.display="block";
	
	return 1;
}

function fxFlashLoadedEvent()
{
	//alert("Flash chargé");
	fxHideDiv("divLoading");
	//document.getElementById("boLogin").disabled=false;
	//document.getElementById("boPassword").disabled=false;
	//document.getElementById("comboLang").disabled=false;
	document.getElementById("btnConnect").disabled=false;
	fxShowLogin();
}

function fxCancel()
{
	document.getElementById("divLogin").style.display="none";
	document.getElementById("fck").style.display="none";
	document.getElementById("flexContent").style.left = "0px";
	document.getElementById("flexContent").style.top = "0px";
	document.getElementById("flexContent").style.minWidth = "1004px";
	document.getElementById("flexContent").style.minHeight = "548px";
	if (navigator.appName.indexOf ("Microsoft") !=-1) {
		
		var w = document.documentElement.clientWidth+"px";
		var h = document.documentElement.clientHeight+"px";
		document.getElementById("flexContent").style.width = w;
		document.getElementById("flexContent").style.height = h;
	}
	else
	{
		
		document.getElementById("flexContent").style.width = "100%";
		document.getElementById("flexContent").style.height = "100%";
	}
	return 1;
}

function fxGetMovieById(id) {
	if (navigator.appName.indexOf ("Microsoft") !=-1) {
		return window[id];
	} else {
		return window.document[id];
	}
}

function fxSetHTML()
{
	var txt="";
	txt = tbText[currentLng];
	var oEditor = FCKeditorAPI.GetInstance('FCKeditor1');
	oEditor.SetHTML(txt);
}

function fxGetHTML()
{
	var oEditor = FCKeditorAPI.GetInstance('FCKeditor1') ;
	var txt = oEditor.GetXHTML();
	return txt;
}

function fxSendTxt()
{
	fxUpdateCurLng();
	var movie = fxGetMovieById("flexContent");
	fxCancel();
	if(contentType=="search")
		movie.setNewTxtSearch(tbText);
	else if(contentType=="delivery")
		movie.setNewTxtDelivery(tbText);
	else if(contentType=="tunnel")
		movie.setNewTxtTunnel(tbText);
	else if(contentType=="page")
		movie.setNewTxtPage(tbText);
	else if(contentType=="pdt")
		movie.setNewTxtPdt(tbText);
	else if(contentType=="list")
		movie.setNewTxtList(tbText);
}

function fxUpdateCurLng()
{
	tbText[currentLng] = fxGetHTML();
	return 1;
}

function fxChangeLng(newLng)
{
	fxUpdateCurLng();
	currentLng = newLng;
	fxSetHTML();
	return 1;
}
function FCKeditor_OnComplete( editorInstance )
{
	fxSetHTML();
}

function fxResetList(listbox)
{
	listbox.options.length = 0; 
	//listbox.options[listbox.options.length] = new Option("", 0);
	return 1;
}

function fxCreateEditor(media_path, full_media_path, md5_media_path, lngBO)
{
	var div = document.getElementById("myFCKeditor");
	div.innerHTML = "";
	var oFCKeditor = new FCKeditor("FCKeditor1");
	oFCKeditor.BasePath = "/inc/js/fckeditor/";
	oFCKeditor.Config["CustomConfigurationsPath"] = "/inc/js/fckeditor/fckeditor_options.js.php?store_url="+ storeUrl +"?" + ( new Date() * 1 ) +"&lngBO="+lngBO;
	oFCKeditor.Width = '100%' ;
	oFCKeditor.Height = '100%' ;
	oFCKeditor.Config["MEDIA_PATH"] = media_path;
	oFCKeditor.Config["FULL_MEDIA_PATH"] = full_media_path;
	oFCKeditor.Config["MD5_MEDIA_PATH"] = md5_media_path;

	div.innerHTML = oFCKeditor.CreateHtml();
}


function fxGetHTTPObject()
{
	if(window.XMLHttpRequest)
		return new XMLHttpRequest(); 
	else if(window.ActiveXObject) 
		return new ActiveXObject("Microsoft.XMLHTTP"); 
	else {
		return; 
	}
}

function fxLogin()
{
	
	var login =	document.getElementById('boLogin').value;
	var password =	document.getElementById('boPassword').value;
	var lng =	document.getElementById('comboLang').value;
	var xhr_object = null;
	xhr_object = fxGetHTTPObject();
	xhr_object.open("GET", "/scripts/loginService.php?action=login&login="+login+"&password="+password, true); 
	
	listboxNum = document.getElementById('listStoreNum');
	listboxName = document.getElementById('listStoreName');
	
	reinitList(listboxNum);
	reinitList(listboxName);
	
	fxHideDiv("result");

	xhr_object.onreadystatechange = function() { 
		if(xhr_object.readyState == 4) {
			var reponse = xhr_object.responseText;
			var block=reponse.split("[EOB]");

			var temp=block[0].split("[EOSB]");
			block[0]=temp[0];
			SupId=temp[1];
			SupHtml=temp[2];
			
			
			// Authentification failed
			if(block[0]==0)
			{
				fxShowDiv("divAlert");
				fxSetText("pAlert", "Erreur d'identification");
			}
			else if(block[0]==1) // Connexion admin
			{
			
				if(SupId=="1" || SupId=="root")
				{
					fxHideDiv("showAdminNoSF");
					fxSetText("showAdminSF", SupHtml)
					fxShowDiv("showAdminSF");		
				}
				
				fxHideDiv("divAlert");
				fxHideDiv("authentif");
				fxShowDiv("storeList");
				
				if(block.length == 3) 
				{
					for (var i=1; i<block.length; i++) {
						if(block[i]!='') {
							var ligne=block[i].split("[EOL]");
							
							for (var j=0; j<ligne.length; j++) {
								if(ligne[j]!='') {
									var champ=ligne[j].split("|||");
									if(i==1)
										listbox = listboxNum;
									else if(i==2)
										listbox = listboxName;

									if(i==1 || i==2) {	
										var idx = listbox.options.length;
										
										if(i==1)
											txt = champ[0]+ " (" + champ[1]+ ")";
										else
											txt = champ[1]+ " (" + champ[0]+ ")";

										if(champ[5]==1)
											txt = champ[2] + " " + txt;

										listbox.options[idx] = new Option(txt, champ[0]);
										
										//alert(ligne[j]);
										var classname="";
										
										if(champ[4]==1)
											classname = "close";
										
										if(champ[2]=="[TEST]")
										{
											if(classname!="")
												classname = classname + " ";
												
											classname = classname + "trial";
										}
										else if(champ[3]==1)
										{
											if(classname!="")
												classname = classname + " ";
												
											classname = classname + "mb";
										}
										
																			
										if(classname!="")
											listbox.options[idx].className = classname;
									}
									
								}
							}
							
						}
					}
				}
			}
			else if(block[0]==2) // Connexion user
			{
				fxHideDiv("divAlert");
				fxHideDiv("storeList");
				fxHideDiv("result");
				fxFlexConnect(login, password, "0", lng);
			}
			else
			{
				fxShowDiv("divAlert");
				fxSetText("pAlert", "Erreur de connexion");
			}

		}
	}
	
	xhr_object.send(null);
}


function fxSearchStore()
{
	
	var login =	document.getElementById('boLogin').value;
	var password =	document.getElementById('boPassword').value;
	var search =	document.getElementById('txtSearch').value;
	var xhr_object = null;
	xhr_object = fxGetHTTPObject();
	xhr_object.open("GET", "/scripts/loginService.php?action=search&login="+login+"&password="+password+"&search="+search, true); 
	
	listboxSearch = document.getElementById('listStoreSearch');
	
	reinitList(listboxSearch);
	
	xhr_object.onreadystatechange = function() { 
		if(xhr_object.readyState == 4) {
			var reponse = xhr_object.responseText;
			var block=reponse.split("[EOB]");
			if(block[0]==0)
			{
				fxShowDiv("divAlert");
				fxSetText("pAlert", "Erreur d'identification");
			}
			else if(block[0]==1) // Connexion admin
			{

				fxHideDiv("divAlert");

				if(block.length == 2) {
					var i = 1;
					if(block[i]!='') {
						var ligne=block[i].split("[EOL]");
						fxSetText("nbResult", ligne.length-1);
						for (var j=0; j<ligne.length; j++) {
							if(ligne[j]!='') {
								var champ=ligne[j].split("|||");
								var listbox = listboxSearch;
																
								var idx = listbox.options.length;
								
								
								txt = champ[0]+ " --- " + champ[6] + " --- " + champ[1]+ " --- " + champ[7];
								
								
								if(champ[5]==1)
									txt = champ[2] + " --- " + txt;

								listbox.options[idx] = new Option(txt, champ[0]);

								var classname="";
										
								if(champ[4]==1)
									classname = "close";
								
								if(champ[2]=="[TEST]")
								{
									if(classname!="")
										classname = classname + " ";
										
									classname = classname + "trial";
								}
								else if(champ[3]==1)
								{
									if(classname!="")
										classname = classname + " ";
										
									classname = classname + "mb";
								}
								
																	
								if(classname!="")
									listbox.options[idx].className = classname;
																
							}
						}
						fxShowDiv("result");
					}
				}
			}
			else
			{
				fxShowDiv("divAlert");
				fxSetText("pAlert", "Erreur de connexion");
			}

		}
	}
	
	xhr_object.send(null);
}


function fxGetAccess(storeId)
{
	
	var login =	document.getElementById('boLogin').value;
	var password =	document.getElementById('boPassword').value;
	var lng =	document.getElementById('comboLang').value;
	var xhr_object = null;
	xhr_object = fxGetHTTPObject();
	xhr_object.open("GET", "/scripts/loginService.php?action=access&login="+login+"&password="+password+"&storeId="+storeId, true); 
	
	xhr_object.onreadystatechange = function() { 
		if(xhr_object.readyState == 4) {
			var reponse = xhr_object.responseText;
			var block=reponse.split("[EOB]");
			
			if(block[0]==1)
			{
				if(block.length == 2) {
					if(block[1]==1)
						fxFlexConnect(login, password, storeId, lng);
					else
					{
						fxShowDiv("divAlert");
						fxSetText("pAlert", "Erreur d'identification");
					}
					
					/*var champ=block[1].split("|||");
					if(champ.length == 2)
						fxFlexConnect(champ[0], champ[1]);*/
				}
				else
				{
					fxShowDiv("divAlert");
					fxSetText("pAlert", "Erreur d'identification");
				}
			}
			else
			{
				fxShowDiv("divAlert");
				fxSetText("pAlert", "Erreur d'identification");
			}
		}
	}
	xhr_object.send(null);
}


function fxFlexConnect(login, password, storeId, lng)
{
	var movie = fxGetMovieById("flexContent");
	if(movie) {
		try
		{
			movie.fxLogin(login, password, storeId, lng);
			fxCancel();
		}
		catch(err)
		{
			alert("Flash en cours de chargement, réessayez dans quelques secondes");
		}
		
	}
	else
		alert("Flash en cours de chargement, réessayez dans quelques secondes");
}

function fxShowDiv(div)
{
	document.getElementById(div).style.display="block";
}

function fxHideDiv(div)
{
	document.getElementById(div).style.display="none";
}

function fxSetText(id, text)
{
	document.getElementById(id).innerHTML = text;
}

function reinitList(listbox)
{
	listbox.options.length = 0; 
	//listbox.options[listbox.options.length] = new Option("", 0);
}


function fxShowStoreList()
{	
	
	document.getElementById("flexContent").style.minWidth = "0px";
	document.getElementById("flexContent").style.minHeight = "0px";
	document.getElementById("flexContent").style.width = "0px";
	document.getElementById("flexContent").style.height = "0px";
	fxHideDiv("authentif");
	fxShowDiv("storeList");
	fxShowDiv("divLogin");
	
	return 1;
}

function fxShowLogin()
{	
	
	document.getElementById("flexContent").style.minWidth = "0px";
	document.getElementById("flexContent").style.minHeight = "0px";
	document.getElementById("flexContent").style.width = "0px";
	document.getElementById("flexContent").style.height = "0px";
	fxHideDiv("storeList");
	fxHideDiv("result");
	fxSetText("showAdminSF", ""); //supression des lien
	fxHideDiv("showAdminSF");
	
	fxShowDiv("authentif");
	fxShowDiv("divLogin");
	fxShowDiv("showAdminNoSF");
	
	return 1;
}

function fxRecoverPwd()
{
	var email = prompt('Saisissez votre email :','');

	if(email!=null && email!="")
	{
		var login =	document.getElementById('boLogin').value;
		
		var xhr_object = null;
		xhr_object = fxGetHTTPObject();
		xhr_object.open("GET", "/scripts/loginService.php?action=recover&email="+email, true); 
		
		xhr_object.onreadystatechange = function() { 
			if(xhr_object.readyState == 4) {
				var reponse = xhr_object.responseText;
				var block=reponse.split("[EOB]");
				
				if(reponse=="OK")
				{
					fxShowDiv("divAlert");
					fxSetText("pAlert", "Votre mot de passe a été envoyé");
				}
				else
				{
					fxShowDiv("divAlert");
					fxSetText("pAlert", "Compte inexistant");
				}
			}
		}
		xhr_object.send(null);

	}
}
