console.log(dojo.version);
// Menu for the conference
dojo.require("myWidgets.MyWidgetMenu");
dojo.require("myWidgets.TranslationNls");
dojo.registerModulePath("modulesDojo","../../modulesDojo");
dojo.require("modulesDojo.SelectLanguage");

var translation = {};
var user = {};
var menuDef = {};

var conference = {
  urlWebSite : ''
 ,conferenceName : ''	
 ,programBulletin : false
 ,themeName : ''
 ,logoName : ''	
 ,externalRegistrationCheck : false
 ,externalRegistrationUrl : ""
 ,functionList : 0

}	

dojo.addOnLoad(initIndex);

function initIndex() {
console.log(dojo.version);
	if (location.search) {
		initParams = utils.parseQueryString(location.search);	
		if (initParams.dialog == "true") {
			initParams.dialog = true;
		} else {
			initParams.dialog = false;
		}
		if (initParams.message == "completeYourData") {
			initParams.displayMenu = false;
		} else {
			initParams.displayMenu = true;
		}
	} else {
		
		initParams = {
			dialog			: false
		 ,displayMenu : true	
		};
	}
	  // -------- init session -------------------
		
		var _self = this;
		var oDate = new Date();
		
	  var dfd = dojo.xhrGet( {
	            url: "../php/initSession.php?locale="+(GetCookie('locale') ? GetCookie('locale') : "")+"&"+oDate, 
	            handleAs: "json",
							sync : true,
	            load: function(response, ioArgs) {							
	              if (response.response) {

								  afficheEntete();

	                dojo.myLocale = (response.locale ? response.locale : GetCookie('locale') );
									SetCookie('locale',dojo.myLocale);
									
	                dojo.registerModulePath("root", "../../..");
	                dojo.requireLocalization("root", "translation", dojo.myLocale);
	                translation = dojo.i18n.getLocalization("root", "translation", dojo.myLocale);
	                this.returnCode = true;
					
									
									if (initParams.dialog == false) {
								  	displayMenu();
								  } else {
										dojo.style(document.body, "background", "none");	
										dojo.addClass(dojo.byId("top"), "hide");
										dojo.addClass(dojo.byId("menu"), "hide");
									}		
									//----------------------------------------------
	              } else {
	                document.location = "../html/erreur404.html";
	              }

	            },
	            error: function(response, ioArgs) { 
	              console.log("HTTP status code: " + ioArgs);
	              dojo.byId("message").innerHTML = "HTTP status code: " + ioArgs.xhr.status;
	            }
	  });
	
}

//---------------------------------------------------------------------
// affiche logo 
//---------------------------------------------------------------------
function afficheEntete() {  

	// ---------- Get conference data -----
	
  dojo.xhrGet( {
  
    url: '../php/conference.php', 
    content : { 
                phase : 'selectxml'
              },
    handleAs: 'xml',
    sync : true,
    load: function (response, ioArgs) {
		  data = response.getElementsByTagName('data')[0];
			if (data.getElementsByTagName('urlWebSite')[0].firstChild) {
			  conference.urlWebSite = data.getElementsByTagName('urlWebSite')[0].firstChild.nodeValue;
			}
			if (data.getElementsByTagName('conferenceName')[0].firstChild) {
			  conference.conferenceName = data.getElementsByTagName('conferenceName')[0].firstChild.nodeValue;
			}
			if (data.getElementsByTagName('programBulletin')[0].firstChild) {
			  conference.programBulletin = (data.getElementsByTagName('programBulletin')[0].firstChild.nodeValue == '1' ? true : false);
			}
			if (data.getElementsByTagName('theme')[0].firstChild) {
			  conference.theme = (data.getElementsByTagName('theme')[0].firstChild.nodeValue);
			}
			if (data.getElementsByTagName('logoName')[0].firstChild) {
			  conference.logoName = (data.getElementsByTagName('logoName')[0].firstChild.nodeValue);
			}
			if (data.getElementsByTagName('locale')[0].firstChild) {
			  conference.locale = (data.getElementsByTagName('locale')[0].firstChild.nodeValue);
			}
			if (data.getElementsByTagName('localeSet')[0].firstChild) {
			  conference.localeSet = data.getElementsByTagName('localeSet')[0].firstChild.nodeValue.split(",");
			}
			if (data.getElementsByTagName('externalRegistrationCheck')[0].firstChild) {
			  conference.externalRegistrationCheck = data.getElementsByTagName('externalRegistrationCheck')[0].firstChild.nodeValue.split(",");
			} else {
				conference.externalRegistrationCheck = 0;
			}
			if (data.getElementsByTagName('externalRegistrationUrl')[0].firstChild) {
			  conference.externalRegistrationUrl = data.getElementsByTagName('externalRegistrationUrl')[0].firstChild.nodeValue.split(",");
			} else {
				conference.externalRegistrationUrl = "";
			}
			if (data.getElementsByTagName('functionList')[0].firstChild) {
			  conference.functionList = data.getElementsByTagName('functionList')[0].firstChild.nodeValue;
			} else {
				conference.functionList = 0;
			}
			if (initParams.dialog == false) {
			 	/*
				 * Traitement du multilinguisme
				 */
				var aDiv = document.createElement('div');
				dojo.addClass(aDiv, "selectLanguage")
				aTop = utils.getElementsByClassName("top", document)[0];
				aTop.appendChild(aDiv);
				var aDiv2 = document.createElement('div');
				aDiv.appendChild(aDiv2);
								
				var languageChoice = new modulesDojo.SelectLanguage({
						 languages : conference.localeSet
						,currentLanguage : dojo.myLocale
						,onClick : function(language) {
							dojo.myLocale = language;
							SetCookie('locale', language);
						  dojo.xhrGet( {
						  
						    url: '../php/setSessionVariable.php', 
						    content : { 
						                variable : 'myLocale'
						               ,value : dojo.myLocale 
						              },
						    load: function (response, ioArgs) {
									document.location = document.location;
							  },
						    error: function(response, ioArgs) { 
						      console.error('HTTP status code: ', ioArgs.xhr.status); 
						      return response; 
						    }
						  });
						} 
				},aDiv2);					
			 	/*
				 * Traitement du logo et du lien avec le site vitrine de la conférence
				 */
				if (conference.logoName != '') {
					aLogo = dojo.byId("logoConference");
					aLogo.src = "../php/getLogoConference.php";
				}
				else {
					aLogo = dojo.byId("logoConference");
					aLogo.src = "../images/transparent.gif";
				}
				
				dojo.byId("topConferenceName").innerHTML = conference.conferenceName;
				
				if (conference.urlWebSite != '') {
				
					utils.removeAllChildren(dojo.byId("logo"));
					
					aElem = document.createElement("a");
					aElem.href = "http://" + conference.urlWebSite;
					aElem.appendChild(aLogo);
					
					dojo.byId("logo").appendChild(aElem);
					
				}
				
			}		
	  },

    error: function(response, ioArgs) { 
      console.error('HTTP status code: ', ioArgs.xhr.status); 
      return response; 
    }
  });
	
    

}
function getProgramBulletin() {
	return conference.programBulletin;
}
function displayMenu() {
	
								//----------------------------------------------
								  // ---------- Create menu -------------
								  var menuDef = {items : [
								  
								       { label: "Super admin", users : ["SUPERADMIN"] 
								        ,subMenu : { items : [
								             {label: translation.newConference,  users : ["SUPERADMIN"], href: "javascript:affiche( { url : '../html/conference.html?newConference=true', type : 'html' } )"}
								            ,{label: translation.conferenceList, users : ["SUPERADMIN"], href: "javascript:affiche( { url : '../html/conferenceList.html', type : 'html' } )"}
								            ,{label: translation.personalData,   users : ["SUPERADMIN"], href: "javascript:affiche( { url : '../html/admin.html', type : 'html' } )"}
								            ,{label: translation.newSuperAdmin,  users : ["SUPERADMIN"], href: "javascript:affiche( { url : '../html/admin.html?newAdmin=true', type : 'html' } )"}
								            ,{label: translation.adminList,      users : ["SUPERADMIN"], href: "javascript:affiche( { url : '../html/adminList.html', type : 'html' } )"}
								            ,{label: translation.forcolWiki,     users : ["SUPERADMIN"], href: "javascript:affiche( { url : '../mediawiki-1.15.0/', type : 'html' } )"}
								          ]}
								       }
								      ,{ label: translation.myAccount,   users : ["SUPERADMIN","ADMIN", "PART", "COMPTA"]
								        ,subMenu : { items : [
								             {label: translation.personalData,   users : ["ADMIN","PART"], href: "javascript:affiche({url : '../html/personalData.html', type : 'html' } )"}
								            ,{label: translation.changePassword, users : ["SUPERADMIN","ADMIN","PART","COMPTA"], href: "javascript:affiche({url : '../html/changePassword.html', type : 'html' } )"}
								         ]} 
								       }
								      ,{ label: translation.payment,   users : ["PART"],         href: "javascript:affiche( { url : '../html/bulletin.html', type : 'html' } )"
								       }
								      ,{ label: translation.conference, users : ["SUPERADMIN","ADMIN","COMPTA"], href: "javascript:affiche( { url : '../html/conference.html', type : 'html' } )" 
								        ,subMenu : { items : [
								             {label: translation.conferenceData,  users : ["SUPERADMIN","ADMIN","COMPTA"], href: "javascript:affiche( { url : '../html/conference.html', type : 'html' } )"}
								            ,{label: translation.createBulletin,  users : ["SUPERADMIN","ADMIN"], href: "javascript:affiche( { url : '../html/createBulletin.html', type : 'html' } )"}
								            ,{label: translation.previewBulletin, users : ["SUPERADMIN","ADMIN"], href: "javascript:affiche( { url : '../html/bulletin.html?preview=true', type : 'html' } )"}
								            ,{label: translation.createProgramme, condition : conference.programBulletin, users : ["SUPERADMIN","ADMIN"], href: "javascript:affiche( { url : '../html/createProgram.html', type : 'html' } )"}
								            ,{label: translation.previewProgramme, condition : conference.programBulletin, users : ["SUPERADMIN","ADMIN"], href: "javascript:affiche( { url : '../html/program.html?preview=true', type : 'html' } )"}
								            ,{label: translation.productList,     users : ["SUPERADMIN","ADMIN","COMPTA"], href: "javascript:affiche( { url : '../html/productList.html', type : 'html' } )"}
								          ]}
								       }
								      ,{ label: translation.registrations, users : ["SUPERADMIN","ADMIN"]  
								        ,subMenu : { items : [
								             {label: translation.registrationByAdmin,  users : ["SUPERADMIN","ADMIN"], href: "javascript:affiche( { url : '../html/registration.html?newRegistration=true', type : 'html' } )"}
								            ,{label: translation.registrationList,     users : ["SUPERADMIN","ADMIN"], href: "javascript:affiche( { url : '../html/registrationList.html', type : 'html' } )"}
								            ,{separator : true}
								            ,{label: translation.confirmationList,     users : ["SUPERADMIN","ADMIN"], href: "javascript:affiche( { url : '../html/list.html?liste=registrationTemp', type : 'html' } )"}
								          ]}
								       }
								      ,{ label: translation.comptabilite, users : ["SUPERADMIN","COMPTA"]  
								        ,subMenu : { items : [
								             {label: translation.invoiceList,     users : ["SUPERADMIN","COMPTA"], href: "javascript:affiche( { url : '../html/invoiceList.html', type : 'html' } )"}
								            ,{label: translation.paymentList,     users : ["SUPERADMIN","COMPTA"], href: "javascript:affiche( { url : '../html/paymentList.html', type : 'html' } )"}
								            ,{label: translation.transactionList, users : ["SUPERADMIN","COMPTA"], href: "javascript:affiche( { url : '../html/transactionList.html', type : 'html' } )"}
								            ,{label: translation.invoiceList+"Grid",     users : ["SUPERADMIN","COMPTA"], href: "javascript:affiche( { url : '../html/invoiceListGrid.html', type : 'html' } )"}
								            ,{label: translation.paymentList+"Grid",     users : ["SUPERADMIN","COMPTA"], href: "javascript:affiche( { url : '../html/paymentListGrid.html', type : 'html' } )"}
								            ,{label: translation.transactionList+"Grid", users : ["SUPERADMIN","COMPTA"], href: "javascript:affiche( { url : '../html/transactionListGrid.html', type : 'html' } )"}
								          ]}
								       }
								      ,{ label: translation.statistics, users : ["SUPERADMIN"]  
								        ,subMenu : { items : [
								             {label: translation.registrationPerCountry, users : ["SUPERADMIN"], href: "javascript:affiche( { url : '../html/list.html?liste=registrationPerCountry', type : 'html' } )"}
								            ,{label: translation.registrationPerProduct, users : ["SUPERADMIN"], href: "javascript:affiche( { url : '../html/list.html?liste=registrationPerProduct', type : 'html' } )"}
								            ,{label: translation.registrationPerPrice  , users : ["SUPERADMIN"], href: "javascript:affiche( { url : '../html/list.html?liste=registrationPerPrice', type : 'html' } )"}
								            ,{label: translation.packOccupation        , users : ["SUPERADMIN"], href: "javascript:affiche( { url : '../html/list.html?liste=packOccupation', type : 'html' } )"}
								            ,{separator : true}
								            ,{label: translation.chiffreAffaire  , users : ["SUPERADMIN"], href: "javascript:affiche( { url : '../html/chiffreAffaire.html', type : 'html' } )"}
								          ]}
								       }
								      ,{ label: translation.connect,      users : ["VISIT"], href: "javascript:affiche( { url : '../html/connect.html', type : 'html' } )" 
								       }
								      ,{ label: translation.registration, users : ["VISIT"], href: "javascript:affiche( { url : '../html/registration.html', type : 'html'} )" 
								       }
								      ,{ label: translation.program, users : ["PART"], condition : conference.programBulletin, href: "javascript:affiche( { url : '../html/program.html', type : 'html'} )" 
								       }
								      ,{ label: translation.disconnect,   users : ["SUPERADMIN","ADMIN", "PART","COMPTA"], href: "javascript:utils.disconnect()" 
								       }
								  
								  ]};                
							  
								  try {
								      params = new Object;
								      params.menuDef = menuDef;
								      user = utils.userConnected();
								
								      if (user.userLevel) {
								        params.userLevel = user.userLevel; 
								      } 
								      menu = new myWidgets.MyWidgetMenu(
								              params 
								            , dojo.byId("menuDiv"));
								
											if (initParams.displayMenu == false) {
												dojo.addClass(dijit.byId("menuDiv").myWidgetMenuContent, "hide");
											}	
											if (user.fullName) {
											 dijit.byId("menuDiv").myWidgetMenuUser.innerHTML = user.fullName;          		
											} else {
											 dijit.byId("menuDiv").myWidgetMenuUser.innerHTML = "";          		
											}
								      
								  } catch (e) {
								    console.log(e);
								  }
								
}

