// JavaScript Document
dojo.require("dojox.layout.ContentPane");
dojo.require("dijit.Dialog");
dojo.require("dijit.form.Button");
dojo.require("modulesDojo.TranslatableButton");

function affiche(params) {

	myRand=parseInt(Math.random()*99999999);  // cache buster

  if (!params.type) {
    params.type = "ajax";
  }
  switch (params.type) {
  case 'ajax':
    dojo.byId("message").innerHTML = "";
    form = dijit.byId("centerBlock");
    form.setHref(params.url);
    break;
  case 'html':
    document.location = params.url;
    break;
  }
  
}
function checkSecurity(level) {

		var returnCode = false;
		if (level && level != user.userLevel) {
      document.location = "/";
		} else {
			var dfd = dojo.xhrGet( {
			          url: "../php/checkSecurity.php", 
			          handleAs: "json",
			          sync : true,
			          load: function(response, ioArgs) {
			              if (response.response) {
			                returnCode = true;  
			              } else {
			                returnCode = false;
			                document.location = response.location;
			              } 
			          },
			          error: function(response, ioArgs) { 
			          }
			});
		}
		
		return returnCode;
}
function exportGESCOM() {

      var dfd = dojo.xhrGet( {
                url: "../php/exportGESCOM.php", 
                handleAs: "json",
								content : {phase : 'generate'},
                sync : true,
                load: function(response, ioArgs) {
                    dojo.addClass(dojo.byId('message'), 'red');
										if (response) {
											switch (response.returnCode) {
												case 1: // Fichier g�n�r� OK
													dojo.byId('message').innerHTML = response.message;
													document.location = "../php/exportGESCOM.php?phase=download";
													break;
												case 2: // Fichier d�j� pr�sent
													dojo.byId('message').innerHTML = response.message;
													document.location = "../php/exportGESCOM.php?phase=download";
													break;
												case 3: // Erreur
													dojo.byId('message').innerHTML = response.message;
													break;
											}
                    }
                },
                error: function(response, ioArgs) { 
                }
       });

}
function activateRegistration(activation, conferenceId) {
	
	  dojo.registerModulePath("root", "../../..");
	  dojo.requireLocalization("root", "dataList",dojo.myLocale);
	  var dataListNls = dojo.i18n.getLocalization("root", "dataList",dojo.myLocale);

    if (dijit.byId("confirmWidget")) {
      dijit.byId("confirmWidget").destroy();
    }
    if (dijit.byId("buttonOk")) {
      dijit.byId("buttonOk").destroy();
    }
    if (dijit.byId("buttonCancel")) {
      dijit.byId("buttonCancel").destroy();
    }
  	var confirm = dojo.byId('confirm');
  	
		var divElem = document.createElement("div");
  	confirm.appendChild(divElem);
    divElem.style.width = "500px";
    var pElem = document.createElement("p");
    pElem.innerHTML = dataListNls.confirmActivation;
    divElem.appendChild(pElem);

  	var centerElem = document.createElement("center");

    buttonOk = new dijit.form.Button({
                                       id : "buttonOk"
                                      ,label  : dataListNls.confirmButtonOK 
                                      ,onClick : function () {
                                                   confirmActivation(activation);
                                                 }  
    });
    
    centerElem.appendChild(buttonOk.domNode);
    
    buttonCancel = new dijit.form.Button({
                                       id : "buttonCancel"
                                      ,label  : dataListNls.confirmButtonCancel 
                                      ,onClick : function () {
                                                   cancelActivation();
                                                 }  
    });
  
    centerElem.appendChild(buttonCancel.domNode);
    divElem.appendChild(centerElem);
          	
  	confirmWidget = new dijit.Dialog({
	                                         id : "confirmWidget"       
		                                      ,title: dataListNls.confirmTitle
                                          },divElem);
    
		confirmWidget.show();

}
function confirmActivation(activation, conferenceId) {
	
  hideConfirmDialog();
	
  var dfd = dojo.xhrGet( {
            url: "../php/registration.php", 
						content : { phase : 'activation'
										   ,activation : activation
											 ,conferenceId : conferenceId
											 ,registrationByAdmin : true 
											},
	          handleAs: "json",
	          sync : true,
            load: function(response, ioArgs) {
								if (response.response) {
								  dijit.byId("dataList").deleteRow([response.registrationId]);
                }
            },
            error: function(response, ioArgs) { 
            }
   });

}
function cancelActivation() {
	
  hideConfirmDialog();

}
function cancelRegistration(activation, conferenceId) {
	
	  dojo.registerModulePath("root", "../../..");
	  dojo.requireLocalization("root", "dataList",dojo.myLocale);
	  var dataListNls = dojo.i18n.getLocalization("root", "dataList",dojo.myLocale);

    if (dijit.byId("confirmWidget")) {
      dijit.byId("confirmWidget").destroy();
    }
    if (dijit.byId("buttonOk")) {
      dijit.byId("buttonOk").destroy();
    }
    if (dijit.byId("buttonCancel")) {
      dijit.byId("buttonCancel").destroy();
    }
  	var confirm = dojo.byId('confirm');
  	
		var divElem = document.createElement("div");
  	confirm.appendChild(divElem);
    divElem.style.width = "500px";
    var pElem = document.createElement("p");
    pElem.innerHTML = dataListNls.confirmCancel;
    divElem.appendChild(pElem);

  	var centerElem = document.createElement("center");

    buttonOk = new dijit.form.Button({
                                       id : "buttonOk"
                                      ,label  : dataListNls.confirmButtonOK 
                                      ,onClick : function () {
                                                   confirmCancel(activation);
                                                 }  
    });
    
    centerElem.appendChild(buttonOk.domNode);
    
    buttonCancel = new dijit.form.Button({
                                       id : "buttonCancel"
                                      ,label  : dataListNls.confirmButtonCancel 
                                      ,onClick : function () {
                                                   cancelActivation();
                                                 }  
    });
  
    centerElem.appendChild(buttonCancel.domNode);
    divElem.appendChild(centerElem);
          	
  	confirmWidget = new dijit.Dialog({
	                                         id : "confirmWidget"       
		                                      ,title: dataListNls.confirmTitle
                                          },divElem);
    
		confirmWidget.show();

}
function confirmCancel (activation, conferenceId) {
	
  hideConfirmDialog();
	
  var dfd = dojo.xhrGet( {
            url: "../php/registration.php", 
						content : { phase : 'cancel'
										   ,activation : activation
											 ,conferenceId : conferenceId
											 ,registrationByAdmin : true 
											},
	          handleAs: "json",
	          sync : true,
            load: function(response, ioArgs) {
								if (response.response) {
								  dijit.byId("dataList").deleteRow([response.registrationId]);
                }
            },
            error: function(response, ioArgs) { 
            }
   });

}
function cancelCancel() {
	
  hideConfirmDialog();

}
function hideConfirmDialog(dialog) {
  if (dijit.byId("confirmWidget")) {
    dijit.byId("confirmWidget").hide();
  }
}
