function login_loadAjaxSync(pageRequest, actionSelected, dataPost)
{
	showPopupWait();
	var dDate=new Date();

	postParameter = dataPost;
	getParameter = "?pageRequest="+pageRequest+"&actionSelected="+actionSelected+"&dt="+dDate.getTime();
	
	var xhr_object = null;
	xhr_object = getHTTPObject();

	xhr_object.open("POST", "index.php"+getParameter, false);

	xhr_object.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xhr_object.send(postParameter);

	hidePopupWait();
	return xhr_object.responseText;
}

function sendPwdMail(pageRequest, actionSelected, dataPost)
{
	result = login_loadAjaxSync(pageRequest, actionSelected, dataPost);
	//alert(result);
	document.getElementById('msgCont').innerHTML=result;
}

/*
function getBoPassword()
{ 
		email=document.getElementById('inputMail').value;
		
		if(email!='')
		{
			response=login_loadAjaxSync("login","login",formSerialize("formulaire_login"));
			//alert(response);
			if(response.indexOf("|||", 0)>0)
				var res=response.split("|||");
			else
				var res=Array(response);
			
			if(res[0]=='loginSimple' && res[1].length==32){
				submitBousrPop(windowsname, res[1]);
			}else if(res[0].length>0){
				// post vers page login_admin
				//set_action selected
				submitPageAdminSearch(res[0]);
			}else{
				document.getElementById('username').value='';
				document.getElementById('password').value='';
				
				setCssVisibility('popup_badpassword','visible');
			}
		}
	return false;
}
*/

function checkLogin()
{
		username=document.getElementById('username').value;
		password=document.getElementById('password').value;
		if(username!='' && password!='' && checkBrowserVersion())
		{
			response=login_loadAjaxSync("login","login",formSerialize("formulaire_login"));
			//alert(response);

			if(response.indexOf("|||", 0)>0)
				var res=response.split("|||");
			else
				var res=Array(response);

			if(res[0]=='storeClosed')
			{
				if(res[1]=='postFact')
				{
						showPopup('popup_mag_postFact');
				}else if(res[1]=='preFact'){
						showPopup('popup_mag_preFact');
				}else{
						showPopup('popup_mag_trialEnd');
				}
					
			}else if(res[0]=='loginSimple' && res[1].length==32){
					initUserLogedIn(res[1]);				
			}else if(res[0].length>0 && res[0]!='badLogin'){
				// post login_admin
				submitPageAdminSearch(res[0]);
			}else{
				document.getElementById('username').value='';
				document.getElementById('password').value='';

				showPopup('popup_badpassword');
			}
		}
	return false;
}

/**************** admin *******************/

function loginAdmin_loadAjaxSync(pageRequest, actionSelected, dataPost)
{

	showPopupWait();
	var dDate=new Date();

	postParameter = dataPost;
	
	if(postParameter.length>0)
		postParameter += "&";
	
	postParameter += "pageRequest="+pageRequest+"&actionSelected="+actionSelected;
	
	getParameter = "?boid="+getBoId()+"&dt="+dDate.getTime();
	
	var xhr_object = null;
	xhr_object = getHTTPObject();

	xhr_object.open("POST",  "index.php"+getParameter, false);

	xhr_object.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xhr_object.send(postParameter);

	hidePopupWait();
	return xhr_object.responseText;
}

function getCodeSiteList(pageRequest, actionSelected, dataPost)
{
	response=loginAdmin_loadAjaxSync(pageRequest, actionSelected, dataPost);
	
	listboxSearch = document.getElementById('listStoreSearch');
	listboxSearch.options.length = 0; 

		if(response!='')
		{
			var ligne=response.split("[EOL]");
			var listbox = listboxSearch;
			listbox.options[0] = new Option('Sélectionner', '');
			for (var j=0; j<(ligne.length-1); j++) {
				if(ligne[j]!='') {
					var champ=ligne[j].split("|||");
													
					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;
													
				}
			}
			setCssDisplay('result', 'block');
			setCssDisplay('noresult', 'none');
		}else{
			setCssDisplay('result', 'none');
			setCssDisplay('noresult', 'block');
		}
		listboxSearch.focus();
	return false; 
}

function loginStoreAdmin(pageRequest, actionSelected, dataPost)
{
		username=document.getElementById('username').value;
		password=document.getElementById('password').value;
		storeId=document.getElementById('listStoreSearch').value;
	
		if(username!='' && password!='' && storeId!='')
		{
			 
			response=loginAdmin_loadAjaxSync(pageRequest, actionSelected, dataPost);
			//alert(response);

			if(response.indexOf("|||", 0)>0)
				var res=response.split("|||");
			else
				var res=Array(response);
			
			if(res[0]=='boUserId' && res[1].length==32){
					initUserAdminLogedIn(res[1]);
			}else if(res[0]=='storeClosed')
			{
				if(res[1]=='postFact')
				{
						showPopup('popup_mag_postFact');
				}else if(res[1]=='preFact'){
						showPopup('popup_mag_preFact');
				}else{
						showPopup('popup_mag_trialEnd');
				}	
			}else{
				
			}
		}
	return false;
}



