/*
 *
 */
var utils = {
   getVar : function(v) {
	   var c;
	   var i = location.href.substring(location.href.indexOf("?") + 1, location.href.length) + "&";
	   if (i.indexOf("#") != -1) {
		   i = i.substring(0, i.indexOf("#")) + "&";
		   c = 0;
	   } else {
		   v = v + "=";
		   var taille = v.length;
		   if (i.indexOf(v) != -1) {
			   c = i.substring(i.indexOf(v) + taille, i.length).substring(0, i.substring(i.indexOf(v) + taille, i.length).indexOf("&"));
		   }
	   }
	   return c;
   },
   /*
    * protection des caractéres speciaux
    */
   protect : function(inString) {
	   var outString = inString;
	   outString = outString.replace(new RegExp('\&amp\;', 'gi'), 'et');
	   outString = outString.replace(new RegExp('\;', 'gi'), '');
	   outString = outString.replace(new RegExp('\=', 'gi'), '');
	   outString = outString.replace(new RegExp('\<', 'gi'), '');
	   return outString;
   },
   b64 : {
      // private property
      _keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
      // public method for encoding
      encode : function(input) {
	      var output = "";
	      var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
	      var i = 0;
	      // input = utils.b64._utf8_encode(input);
	      while (i < input.length) {
		      chr1 = input.charCodeAt(i++);
		      chr2 = input.charCodeAt(i++);
		      chr3 = input.charCodeAt(i++);
		      enc1 = chr1 >> 2;
		      enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
		      enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
		      enc4 = chr3 & 63;
		      if (isNaN(chr2)) {
			      enc3 = enc4 = 64;
		      } else if (isNaN(chr3)) {
			      enc4 = 64;
		      }
		      output = output + this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) + this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
	      }
	      return output;
      },
      // public method for decoding
      decode : function(input) {
	      var output = "";
	      var chr1, chr2, chr3;
	      var enc1, enc2, enc3, enc4;
	      var i = 0;
	      input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
	      while (i < input.length) {
		      enc1 = this._keyStr.indexOf(input.charAt(i++));
		      enc2 = this._keyStr.indexOf(input.charAt(i++));
		      enc3 = this._keyStr.indexOf(input.charAt(i++));
		      enc4 = this._keyStr.indexOf(input.charAt(i++));
		      chr1 = (enc1 << 2) | (enc2 >> 4);
		      chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
		      chr3 = ((enc3 & 3) << 6) | enc4;
		      output = output + String.fromCharCode(chr1);
		      if (enc3 != 64) {
			      output = output + String.fromCharCode(chr2);
		      }
		      if (enc4 != 64) {
			      output = output + String.fromCharCode(chr3);
		      }
	      }
	      // output = utils.b64._utf8_decode(output);
	      return output;
      },
      // private method for UTF-8 encoding
      _utf8_encode : function(string) {
	      string = string.replace(/\r\n/g, "\n");
	      var utftext = "";
	      for ( var n = 0; n < string.length; n++) {
		      var c = string.charCodeAt(n);
		      if (c < 128) {
			      utftext += String.fromCharCode(c);
		      } else if ((c > 127) && (c < 2048)) {
			      utftext += String.fromCharCode((c >> 6) | 192);
			      utftext += String.fromCharCode((c & 63) | 128);
		      } else {
			      utftext += String.fromCharCode((c >> 12) | 224);
			      utftext += String.fromCharCode(((c >> 6) & 63) | 128);
			      utftext += String.fromCharCode((c & 63) | 128);
		      }
	      }
	      return utftext;
      },
      // private method for UTF-8 decoding
      _utf8_decode : function(utftext) {
	      var string = "";
	      var i = 0;
	      var c = 0;
	      var c1 = 0;
	      var c2 = 0;
	      while (i < utftext.length) {
		      c = utftext.charCodeAt(i);
		      if (c < 128) {
			      string += String.fromCharCode(c);
			      i++;
		      } else if ((c > 191) && (c < 224)) {
			      c2 = utftext.charCodeAt(i + 1);
			      string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
			      i += 2;
		      } else {
			      c2 = utftext.charCodeAt(i + 1);
			      c3 = utftext.charCodeAt(i + 2);
			      string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
			      i += 3;
		      }
	      }
	      return string;
      }
   },
   zip : {
      // private property
      // _secret_key: secret_key,
      // _secret_prefix: secret_prefix,
      // _secret_suffix: secret_suffix,
      // Ajoute le préfixe et suffixe
      add_fix : function(string) {
	      return '---|---' + string + '---|---';
      },
      // Supprime le préfixe et suffixe
      remove_fix : function(string) {
	      prefix_length = suffix_length = ('---|---').length;
	      string_length = string.length;
	      return string.substr(prefix_length, (string.length - prefix_length - suffix_length));
      },
      // Cryptage des données
      encrypt : function(string) {
	      result = '';
	      string = this.add_fix(string);
	      var key = 'Je QOOQ donc je suis. UNOWHY @ 2009';
	      for ( var i = 0; i < string.length; i++) {
		      cur_char = string.substr(i, 1);
		      keychar = key.substr((i % (key.length - 1)), 1);
		      cur_char = String.fromCharCode(cur_char.charCodeAt(0) + keychar.charCodeAt(0));
		      result += cur_char;
	      }
	      return utils.b64.encode(result);
      },
      // Décryptage des données
      decrypt : function(string) {
	      result = '';
	      string = utils.b64.decode(string);
	      var key = 'Je QOOQ donc je suis. UNOWHY @ 2009';
	      for ( var i = 0; i < string.length; i++) {
		      cur_char = string.substr(i, 1);
		      keychar = key.substr((i % (key.length - 1)), 1);
		      cur_char = String.fromCharCode(cur_char.charCodeAt(0) - keychar.charCodeAt(0));
		      result += cur_char;
	      }
	      result = this.remove_fix(result);
	      return result;
      }
   },
   goAndTrackVideo : function(url, width, height, trackinfo) {
	   if (trackinfo == undefined) {
		   utils.goVideo(url, width, height);
		   return;
	   }
	   trackinfo = "/videoplayer/" + trackinfo;
	   // temporaire
	   // gaTrackPage(trackinfo);
	   //
	   utils.goVideo(url, width, height);
   },
   goVideo : function(url, width, height, url_visuel, elem_id) {
	   if (width == undefined) {
		   width = 480;
	   }
	   if (height == undefined) {
		   height = 385;
	   }
	   var html = '';
	   if (url_visuel) {
		   html = '<img alt="" src="' + url_visuel + '" />';
	   } else if (elem_id) {
		   html = $(elem_id).innerHTML;
	   } else {
		   html = '<object id="video" width="' + width + '" height="' + height + '"><param name="movie" value="' + url + '&autoplay=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="' + url + '&autoplay=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="' + width + '" height="' + height + '"></embed></object>';
	   }
	   var top = '<table id="videoContainer" cellpadding="0"><tr><td><img id="bPopClose" src="/img/video/bClose.png" alt="Fermer" /></td><td class="top"></td><td class="topRight"></td></tr><tr><td class="left"></td><td id="videoTd" align="center" valign="center" style="background-color:#fff;">';
	   var bot = '</td><td class="right"></td></tr><tr><td class="botLeft"></td><td class="bot"></td><td class="botRight"></td></tr></table>';
	   var divPop = new Element('table', {
	      'id' : 'pop',
	      'class' : 'pop',
	      'height' : window.outerHeight,
	      'html' : '<tr><td align="center" valign="center">' + top + html + bot + '</td></tr>'
	   });
	   divPop.inject(document.body, 'top');
	   divPop.onmousedown = $('bPopClose').onmousedown = function() {
		   trace('clic sur fond');
		   window.onscroll = window.onresize = null;
		   // this.destroy();
		   divPop.parentNode.removeChild(divPop);
		   return false;
	   };
	   $('videoContainer').onmousedown = function(event) {
		   trace('clic sur videoContainer');
		   if (typeof event == "undefined") {
			   event = window.event;
			   window.event.cancelBubble = true;
		   } else {
			   event.stopPropagation();
		   }
		   return false;
	   };
	   /*
	    * window.onscroll = window.onresize = function(){ // var mytop = $('pop').style.top; var newtop = utils.getScrollTop() + 'px'; $('pop').style.top = newtop; $('pop').style.height = $('html').offsetHeight + 'px'; }; window.onscroll();
	    */
   },
   getScrollTop : function() {
	   var scrolledY = 0;
	   if (self.pageYOffset) {
		   scrolledY = self.pageYOffset;
	   } else if (document.documentElement && document.documentElement.scrollTop) {
		   scrolledY = document.documentElement.scrollTop;
	   } else if (document.body) {
		   scrolledY = document.body.scrollTop;
	   }
	   return scrolledY;
   },
   /*
    * FONCTION DE GESTION DE CLIC SUR LES CASES A COCHER
    */
   // action de clic sur la case √† cocher
   // recuperation de la valeur : coch√©e : 1 ou pas : 0
   case_init : function(c) {
	   $(c).onclick = function() {
		   if (!qooq.action) {
			   if (this.className != 'case _on') {
				   this.className = 'case _on';
			   } else {
				   this.className = 'case _off';
			   }
		   }
	   };
   },
   case_test : function(c) {
	   return $(c).className != 'case _on' ? 0 : 1;
   },
   /*
    * CHAMPS DE FORMULAIRE
    */
   fields : {
	   initFieldClic : function(array) {
		   trace('ACCOUNT :initFieldClic pour ' + array.length + ' champs');
		   if (!array[1]) {
			   return false;
		   } else {
			   for ( var i = array.length - 1; i > -1; i--) {
				   $(array[i]).disabled = false;
				   $(array[i]).tabindex = i;
				   var clicFunc = function() {
					   if (!qooq.action) {
						   for ( var a = array.length - 1; a > -1; a--) {
							   $(array[a]).className = 'fieldOff';
						   }
						   this.className = 'fieldOn';
						   this.focus();
					   }
					   return false;
				   };
				   $(array[i]).onfocus = clicFunc;
			   }
		   }
	   }
   },
   /*
    * POPUPS
    */
   pop : {
      open : function(title, mess, bottomButtons, bClose, plusId, size, bgBlack, topWhite) {
	      trace('pop' + title);
	      qooq.action = true;
	      if ($('pop')) {
		      this.close();
	      }
	      var monHTML = '<div id="popCont' + (plusId ? plusId : '') + '" class="popCont">';
	      if (!topWhite) {
		      monHTML += '<h3 id="popTitle' + (plusId ? plusId : '') + '">' + title + '</h3>';
		      monHTML += !bClose ? '' : ('<img id="bClosePop' + (plusId ? plusId : '') + '"' + (bClose ? '' : ' style="display:none;"') + ' class="popClose" src="img/pop/bClose.png" />');
	      } else {
		      monHTML += '<h3 class="topWhite"></h3>';
	      }
	      monHTML += '<div id="popContenu' + (plusId ? plusId : '') + '" class="popContenu">' + mess + '</div>';
	      if (!bottomButtons) {
		      // monHTML += '<img src="img/pop/bot' + (size ? '_' + size : '') + '.png"/>';
	      } else {
		      monHTML += '<div id="popBottom' + (plusId ? plusId : '') + '" class="popBottom">' + bottomButtons + '</div>';
	      }
	      monHTML += '</div>';
	      var id = 'pop' + (plusId ? plusId : '');
	      var className = 'pop' + (bgBlack ? ' _black' : '') + (size ? ' ' + size : '');
	      var divPop = new Element('table', {
	         'id' : id,
	         'class' : className,
	         'html' : '<tr><td align="center" valign="center">' + monHTML + '</td></tr>'
	      });
	      divPop.inject(document.body, 'top');
	      // l'action des boutons devra etre definie dans le script declencheur
	      if (bClose) {
		      $('bClosePop' + (plusId ? plusId : '')).onclick = utils.pop.close;
	      }
	      /*
	       * window.onscroll = window.onresize = function(){ //var mytop = $('pop').style.top; var newtop = utils.getScrollTop() + 'px'; $('pop').style.top = newtop; $('pop').style.height = $('html').offsetHeight + 'px'; }; window.onresize();
	       */
	      qooq.action = false;
      },
      close : function(force) {
	      if ((!qooq.action || force) && $('pop')) {
		      $('pop').destroy();
	      }
	      // window.onscroll = window.onresize = null;
	      return false;
      }
   }
};
if (utils.getVar('debug') == '1' && typeof (console) == 'object') {
	trace = function(m) {
		console.log(m);
	};
} else {
	trace = function(m) {
	};
}
var qooqPopup = null;
var qooq = {
   token : null,
   action : true,
   start : function() {
	   // test navigateur
	   if (Browser.Engine.name == "trident" && Browser.Engine.version <= 4) {
		   document.body.innerHTML = '';
		   new Element('div', {
		      'id' : 'stopIE',
		      'style' : 'text-align:center;padding:20px;',
		      'html' : '<img src="/img/header/QOOQ.png" alt="QOOQ" /><br /><br />Votre navigateur est trop ancien pour afficher le site QOOQ.<br />Veuillez le mettre à jour ou utiliser un autre navigateur.'
		   }).inject(document.body, 'top');
		   return;
	   }
	   // test des cookies;
	   Cookie.write('cookie', 'true');
	   if (!Cookie.read('cookie')) {
		   new Element('div', {
		      'id' : 'stopIE',
		      'html' : '<img src="/img/header/QOOQ.png" alt="QOOQ" /><br /><br />Votre navigateur n\'accepte pas les cookies.<br />Veuillez activer les cookies pour pouvoir profiter de QOOQ !'
		   }).inject($('body'), 'top');
		   return;
	   } else {
		   Cookie.dispose('cookie');
	   }
	   /* utile ? */
	   if ($('noJs') != undefined) {
		   $('noJs').style.display = 'none';
		   $('contenu').style.display = 'block';
	   }
	   //
	   var a = qooqCookies.read().email;
	   var b = qooqCookies.read().password;
	   // trace(a + "/" + b);
	   if (a && b) {
		   QAPI.init("QOOQ", qooq.started, true);
	   } else {
		   trace('pas de cookie : déconnecté")');
		   qooq.login._init(false);
		   qooq.action = false;
	   }
	   $('visu').src = 'http://static.qooq.com/img/header/visu.png';
   },
   started : function(a, b) {
	   trace('started');
	   if (!a) {
		   alert("Erreur : Le service est indisponible, veuillez re-essayer plus tard. Merci.");
		   qooq.action = false;
		   return;
	   } else {
		   var a = qooqCookies.read().email;
		   var b = qooqCookies.read().password;
		   // trace(a + "/" + b);
		   if (a && b) {
			   trace('des cookies : on tente de se connecter');
			   // $('email').value = a;
			   // $('passwd').value = b;
			   qooq.login._init(true);
			   qooq.login.valid(a, b, true);
		   } else {
			   trace('pas de cookie : déconnecté")');
			   qooq.login._init(false);
			   qooq.action = false;
		   }
	   }
   },
   goQooq : function() {
	   trace('goQooq');
	   var url = 'http://' + window.location.host + '/qooq#token=' + qooq.token;
	   if (!qooq.service) {
		   Cookie.write('pageFrom', '', {
		      path : '/qooq',
		      'duration' : 365
		   });
		   Cookie.write('dataFrom', '', {
		      path : '/qooq',
		      'duration' : 365
		   });
		   qooq.service = window.open(url, "_blank");
		   trace('qooq.service : ' + qooq.service);
	   } else {
		   try {
			   qooq.service.focus();
		   } catch (err) {
			   qooq.service = window.open(url, "_blank");
		   }
	   }
	   if (simpleHome > 0)
		   qooq.go_complete();
   },
   goAccount : function() {
	   trace('goAccount');
	   var url = 'https://' + window.location.host + '/qooq/account#token=' + qooq.token;
	   if (!qooq.service) {
		   qooq.service = window.open(url, "_blank");
		   trace('qooq.service : ' + qooq.service);
	   } else {
		   try {
			   qooq.service.location = url;
		   } catch (err) {
			   qooq.service = window.open(url, "_blank");
		   }
	   }
   },
   go_complete : function() {
	   document.location.href = "/";
   },
   goMyQooq : function() {
	   trace('goMyQooq');
	   document.location.href = QAPI.goqooqurl('myqooq');
   },
   // ##########################################################################
   // ##########################################################################
   // NEWSLETTER
   // ##########################################################################
   // ##########################################################################
   news : {
	   suscribe : function() {
		   if (!qooq.login.email_chk(($('newsMail').value))) {
			   $('newsError').style.color = "#FF0000";
			   $('newsError').innerHTML = "L'email saisi n'est pas valide"; //
			   return;
		   }
		   var sender = new Request({
		      url : '/logger/subsribenl.php',
		      method : 'GET',
		      data : {
			      'mail' : $('newsMail').value
		      },
		      async : true,
		      noCache : true
		   });
		   sender.send();
		   $('newsError').innerHTML = "Vous êtes inscrit(e).";
		   $('newsError').style.color = "#243C4C";
		   $('newsMail').style.display = 'none';
		   $('newsBtn').style.display = 'none';
	   }
   },
   // ##########################################################################
   // ##########################################################################
   // ZONE DE LOGIN
   // ##########################################################################
   // ##########################################################################
   login : {
      direct : false,
      logged : false,
      _init : function(direct) {
	      trace('login _init');
	      if (!direct) {
		      $('logincontainer').className = '_off';
		      $('conBloc').className = '_off';
		      if ($('accountBloc')) {
			      $('accountBloc').style.display = 'block';
			      qooq.account._init();
			      $('pubBloc').style.display = 'none';
		      }
		      qooq.action = false;
	      }
      },
      close : function() {
	      $('logincontainer').className = '_off';
	      window.scrollTo(0, 0);
      },
      open : function() {
	      if (qooq.login.logged) {
		      qooq.login.disconnect();
		      return false;
	      }
	      qooq.login.direct = false;
	      $('logincontainer').className = '_on';
	      $('loginBloc').className = '_off';
	      $('login').removeEvents();
	      $('login').addEvent('keydown', function(evt) {
		      if (evt.keyCode == 13) {
			      $('password').focus();
			      return false;
		      }
		      return true;
	      });
	      $('login').addEvent('focus', function() {
		      if (this.value == 'adresse email') {
			      this.value = '';
		      }
		      return true;
	      });
	      $('login').addEvent('blur', function() {
		      if (this.value == '') {
			      this.value = 'adresse email';
		      }
		      return true;
	      });
	      $('password').removeEvents();
	      $('password').addEvent('focus', function() {
		      if (this.value == '******') {
			      this.value = '';
		      }
		      return true;
	      });
	      $('password').addEvent('blur', function() {
		      if (this.value == '******') {
			      this.value = '';
		      }
		      return true;
	      });
	      $('password').addEvent('keydown', function(evt) {
		      if (!qooq.action) {
			      if (evt.key == 'enter') {
				      qooq.login.valid();
				      return false;
			      }
		      }
	      });
      },
      login : function() {
	      $('loginBloc').className = '_off';
	      $('loginError').innerHTML = '';
      },
      valid : function(email, password, direct) {
	      trace('login valid  direct:' + direct + '  email:' + email + '  password:' + password);
	      if (!email && !password) {
		      email = $('login').value;
		      password = $('password').value;
	      }
	      if (password == '******') {
		      $('password').value = password = '';
	      }
	      if (!email || !password || !login.email_chk(email)) {
		      $('loginError').innerHTML = 'Adresse email ou mot de passe erroné. Veuillez vérifier votre saisie, svp.';
		      qooq.action = false;
	      } else {
		      qooq.action = true;
		      $('loginError').innerHTML = '';
		      // this.email = email;
		      qooq.login.email = email;
		      qooq.login.password = password;
		      trace('login valid  before password:' + password);
		      password = !direct ? MD5(qooq.login.password) : qooq.login.password;
		      trace('login valid  before password:' + password);
		      qooq.login.direct = direct;
		      trace('qooq.login.valid : QAPI.login');
		      if (!QAPI.qas.length) {
			      QAPI.init("QOOQ", qooq.login.valid_after_init, true);
		      } else {
			      QAPI.login_main(utils.protect(qooq.login.email), password, qooq.login.test);
		      }
	      }
      },
      valid_after_init : function(a, b) {
	      if (!a) {
		      alert("Erreur : Le service est indisponible, veuillez re-essayer plus tard. Merci.");
		      qooq.action = false;
		      return;
	      } else {
		      QAPI.login_main(utils.protect(qooq.login.email), !qooq.login.direct ? MD5(qooq.login.password) : qooq.login.password, qooq.login.test);
	      }
      },
      /*
       * send : function(){ this.myAjax = new Request.JSON({ url : '/ajaxcall/web_generals/login', method : 'POST', data : { 'email' : this.email, 'password' : this.password, 'authenticity_token' : qooq.token }, async : false, noCache : true, onFailure : function(xhr){ alert('onFailure : code erreur : ' + (xhr ? xhr.status : null)); delete qooq.login.myAjax; }, onSuccess : this.test }); this.myAjax.send(); },
       */
      test : function(ok, repJSON) {
	      trace('qooq.login.test : ' + ok);
	      // qooq.token = repJSON.token;
	      if (!ok || repJSON.errors.status || !repJSON.userlist.length) {
		      trace('login test fail');
		      // repJSON.errors.messages[0];
		      $('conBloc').className = '_off';
		      $('loginBloc').className = '_off';
		      if (!qooq.login.direct) {
			      $('loginError').innerHTML = 'Adresse email ou mot de passe erroné. Veuillez vérifier votre saisie, svp.';
			      qooq.login.direct = false;
		      } else {
			      qooq.login.direct = false;
			      if ($('accountBloc')) {
				      $('accountBloc').style.display = 'block';
				      qooq.account._init();
				      $('pubBloc').style.display = 'none';
			      }
		      }
		      qooq.login.empty_cookies();
		      qooq.action = false;
	      } else {
		      trace('login ok : email:' + login.email);
		      $('loginError').innerHTML = '';
		      qooqCookies.write(qooq.login.email, !qooq.login.direct ? MD5(qooq.login.password) : qooq.login.password);
		      qooq.login.logged = true;
		      var mess = 'Bonjour ' + repJSON.userlist[0].prenom;
		      if (!qooq.login.direct) {
			      $('loginBloc').className = '_on';
			      $('loggedBonjour').innerHTML = 'Bonjour ' + repJSON.userlist[0].prenom;
		      } else {
		      }
		      qooq.login.direct = false;
		      $('conBloc').className = '_on';
		      $('conBtn').href = "javascript:qooq.goQooq();";
		      $('conImg').alt = $('conBtn').title = "Accéder à QOOQ Online";
		      mess += '<br /><a href="javascript:qooq.login.disconnect();">Se déconnecter</a>';
		      $('conBonjour').innerHTML = mess;
		      //
		      if ($('accountBloc')) {
			      $('accountBloc').style.display = 'none';
			      $('pubBloc').style.display = 'block';
		      }
	      }
	      qooq.action = false;
	      delete qooq.login.email;
	      delete qooq.login.password;
	      delete qooq.login.myAjax;
      },
      empty_cookies : function() {
	      qooqCookies.remove();
	      Cookie.write('pageFrom', '', {
	         path : '/qooq',
	         'duration' : 0
	      });
	      Cookie.write('dataFrom', '', {
	         path : '/qooq',
	         'duration' : 0
	      });
      },
      disconnect : function() {
	      qooq.login.empty_cookies();
	      try {
		      qooq.service.close();
	      } catch (err) {
	      }
	      /*qooq.service = 0;
	      qooq.login.logged = false;
	      $('password').value = '';
	      this.login();
	      $('conBloc').className = '_off';
	      $('conBtn').onClick = function() {
	         qooq.login.open();
	      };
	      $('conImg').alt = "Se connecter";
	      $('conBonjour').innerHTML = '';
	      if ($('accountBloc')) {
	         $('accountBloc').style.display = 'block';
	         $('pubBloc').style.display = 'none';
	         qooq.account._init();
	      }*/
	      location.reload();
      },
      stay : function() {
	      if (simpleHome > 0) {
		      qooq.go_complete();
	      } else {
		      $('logincontainer').className = '_off';
		      $('loginBloc').className = '_off';
	      }
      },
      pass_open : function() {
	      trace('login pass_open');
	      $('loginBloc').className = '_pass';
	      $('login2').value = '';
	      $('login2').focus();
      },
      pass_send : function() {
	      trace('login pass_send');
	      var email = $('login2').value;
	      if (!email) {
		      $('loginError').innerHTML = 'Veuillez saisir votre adresse email';
	      } else if (!login.email_chk(email)) {
		      $('loginError').innerHTML = 'Le format de l\'email est incorrect. Veuillez vérifier votre saisie.';
	      } else {
		      qooq.login.email = email;
		      if (!QAPI.qas.length) {
			      QAPI.init("QOOQ", qooq.login.pass_after_init, true);
		      } else {
			      QAPI.lostpassword(qooq.login.email, qooq.login.pass_test);
		      }
	      }
      },
      pass_after_init : function(a, b) {
	      if (!a) {
		      alert("Erreur : Le service est indisponible, veuillez re-essayer plus tard. Merci.");
		      qooq.action = false;
		      return;
	      } else {
		      QAPI.lostpassword(qooq.login.email, qooq.login.pass_test);
	      }
      },
      pass_test : function(repJSON) {
	      trace('login pass_test');
	      if (repJSON.errors && repJSON.errors.status) {
		      $('loginError').innerHTML = 'Adresse email incorrecte';
		      $('login2').innerHTML = '';
		      $('login2').focus();
	      } else {
		      $('loginError').innerHTML = '';
		      $('loginBloc').className = '_passOk';
		      $('loginSentMail').innerHTML = login.email;
		      $('login').value = login.email;
	      }
	      delete login.email;
      },
      email_chk : function(email) {
	      var regExp = /^[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z]{2,})$/g;
	      regExp.lastIndex = null;
	      return email.test(regExp);
      }
   },
   account : {
      _init : function() {
	      // listes des champs de formulaire client
	      this.civilArray = [ 'Mademoiselle', 'Madame', 'Monsieur' ];
	      if (simpleHome < 2) {
		      this.fieldList_client = [ 'accountName', 'accountPName', 'accountMail', 'accountPass' ];
		      this.labelList_client = [ 'civilLbl', 'accountNameLbl', 'accountPNameLbl', 'accountMailLbl', 'accountMailLbl', 'accountPassLbl' ];
	      } else {
		      this.fieldList_client = [ 'accountMail', 'accountPass' ];
		      this.labelList_client = [ 'accountMailLbl', 'accountMailLbl', 'accountPassLbl' ];
	      }
	      $('accountBloc').className = '_on';
	      utils.fields.initFieldClic(this.fieldList_client);
	      utils.case_init('caseNews');
	      $('caseNews').className = 'case _off';
	      if (simpleHome == 0) {
		      utils.case_init('casePartners');
		      $('casePartners').className = 'case _off';
		      $('charte').onclick = function() {
			      if (!qooq.action) {
				      var mess = "QOOQ a reçu l'agrément de la CNIL N° 1330802. Conformément à la loi Informatique et Libertés en date du 6 janvier 1978, vous disposez d'un droit d'accès, de rectification, de modification et de suppression des données qui vous concernent. Vous pouvez exercer ce droit en contactant notre Service Clients au 09 74 50 54 04. Il vous est également possible de nous envoyer un courrier à l'adresse suivante : UNOWHY – 11 rue Tronchet - 75008 Paris - France.";
				      utils.pop.open('<img src="img/bloc_account/title_pop_charte.png" />', mess, '<img id="popValid" src="img/_buttons/bValidate.png" alt="Valider" />', true);
				      $('popValid').onclick = utils.pop.close;
			      }
		      };
	      }
	      $('bValidCreate').onclick = function() {
		      if (!qooq.action) {
			      qooq.account.clientForm_valid();
		      }
	      };
      },
      // mise en off des labels de formulaire client
      clientLabels_off : function() {
	      trace('clientLabels_off');
	      var label;
	      for ( var i = this.labelList_client.length - 1; i > -1; i--) {
		      label = this.labelList_client[i];
		      trace(i + " > " + label);
		      $(label).className = 'fldTitle_off';
	      }
	      for ( var i = this.fieldList_client.length - 1; i > -1; i--) {
		      $(this.fieldList_client[i]).className = 'fieldOff';
	      }
      },
      // popup d'erreur de saisie manquante
      fill_error : function(mess, complete) {
	      trace('fill_error');
	      $('accountError').innerHTML = !complete ? mess + '.' : 'Veuillez saisir ' + mess + '.';
      },
      validateur : function(string1, typeVal, string2, param1, param2, fieldToFocus) {
	      trace('PERSO ----------------->VALIDATEUR<-----------------' + string1);
	      // //trace('typeVal : ' + typeVal);
	      // //trace('string2 : ' + string2);
	      // //trace('param1 : ' + param1);
	      // //trace('param2 : ' + param2);
	      // //trace('*----------------------*');
	      string1 = new String(string1);
	      // /Controle des valeurs occurentes
	      var ok = false;
	      var regExp;
	      if (string1) {
		      switch (typeVal) {
			      case '=':
				      ok = string1 != string2 ? false : true;
				      break;
			      case '@':
				      // test de courriel
				      regExp = /^[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z]{2,})$/g;
				      regExp.lastIndex = null;
				      ok = (string1.test(regExp)) ? true : false;
				      break;
			      case '1':
				      regExp = /^[0-9]+$/g;
				      regExp.lastIndex = null;
				      ok = (regExp.test(string1)) ? true : false;
				      break;
			      case 'tel':
				      // test de numero de telephone
				      if (string1.length) {
					      regExp = /^(01|02|03|04|05|06|08|09)[0-9]{8}/g;
					      regExp.lastIndex = null;
					      ok = (string1.test(regExp)) ? true : false;
				      } else {
					      // car tel optionnel
					      ok = true;
				      }
				      break;
			      case 'in':
				      // test d'entier
				      ok = (string1 <= param2) && (string1 >= param1) ? true : false;
				      break;
			      case '$':
				      // test de numero de carte de credit
				      ok = this.isCreditCard(string1);
				      break;
			      case 'min':
				      // test longueur minimale
				      ok = string1.length < param1 ? false : true;
		      }
	      }
	      // /Affichage de la popup si erreur
	      if (!ok) {
		      // var bottomButtons = '<img id="popValid" src="img/_button/bValidate.png" alt="Valider" />';
		      // var titre = null;
		      var mess = null;
		      switch (typeVal) {
			      case '=':
				      // titre = 'Erreur de saisie';
				      mess = 'La valeur saisie n\'est pas identique à celle ci-dessus';
				      break;
			      case '@':
				      // titre = 'Erreur d\'adresse mail';
				      mess = 'L\'adresse mail saisie n\'est pas valide';
				      break;
			      case '1':
				      // titre = 'Erreur de saisie';
				      mess = 'La valeur saisie doit étre numérique';
				      break;
			      case 'tel':
				      // titre = 'Erreur de numéro';
				      mess = 'Le numéro saisi n\'est pas valide';
				      break;
			      case 'in':
				      // titre = 'Erreur de saisie';
				      mess = 'La valeur saisie doit étre comprise entre ' + param1 + ' et ' + param2 + '';
				      break;
			      case '$':
				      // titre = 'Erreur de saisie';
				      mess = 'Le numero de carte de crédit est invalide';
				      break;
			      case 'min':
				      // titre = 'Erreur de saisie';
				      mess = 'Pas assez de caractéres';
				      break;
		      }
		      trace('erreur : ' + mess);
		      this.fill_error(mess);
		      // !fieldToFocus ? null : fieldToFocus.focus();
	      }
	      trace('OK = ' + ok);
	      trace('---------------------------------');
	      return ok;
      },
      clientForm_valid : function() {
	      if (qooq.action)
		      return;
	      qooq.action = true;
	      trace('ACCOUNT :clientForm_valid');
	      trace('qooq.aff_val : ' + qooq.aff_val);
	      this.clientLabels_off();
	      // test boutons radios, puis champs, puis cases à cocher
	      // temporaire
	      // var civ = utils.select.test($('civil'));
	      var civil;
	      if (simpleHome < 2) {
		      var civ = parseInt($('civil').options[$('civil').selectedIndex].value, 10);
		      //
		      trace('ACCOUNT :clientForm_valid : civ : ' + civ);
		      if (civ == -1) {
			      $('civilLbl').className = 'fldTitle_on';
			      this.fill_error('votre civilité', true);
			      qooq.action = false;
			      return;
		      } else {
			      civil = this.civilArray[civ];
			      trace("civil : dimpleHome : " + civil);
		      }
	      } else {
		      // civilité par défaut si home simple
		      civil = this.civilArray[2];
	      }
	      trace("civil : " + civil);
	      // test de validité du contenu des champs
	      for ( var i = 0; i < this.fieldList_client.length; i++) {
		      var ok = false;
		      var string = $(this.fieldList_client[i]).value;
		      trace($(this.fieldList_client[i]));
		      trace('string : ' + string);
		      if (simpleHome < 2) {
			      switch (i) {
				      case 0:
				      case 1:
					      ok = this.validateur(string, 'min', null, 1, null, $(this.fieldList_client[i]));
					      break;
				      case 2:
					      ok = this.validateur(string, '@', null, null, null, $(this.fieldList_client[i]));
					      break;
				      case 3:
					      ok = this.validateur(string, 'min', null, 6, null, $(this.fieldList_client[i]));
					      break;
			      }
		      } else {
			      switch (i) {
				      case 0:
					      ok = this.validateur(string, '@', null, null, null, $(this.fieldList_client[i]));
					      break;
				      case 1:
					      ok = this.validateur(string, 'min', null, 6, null, $(this.fieldList_client[i]));
					      break;
			      }
		      }
		      if (!ok) {
			      trace('erreur au champ : ' + i);
			      $(this.labelList_client[i + 1]).className = 'fldTitle_on';
			      qooq.action = false;
			      $(this.fieldList_client[i]).focus();
			      return;
		      } else {
			      this.fill_error('');
		      }
	      }
	      trace('pas d\'erreur : on peut soumettre le formulaire');
	      utils.pop.open('Envoi au serveur...', '<p style="text-align:center;"><img alt="Chargement en cours" src="img/pop/preload.png" /></p>', '&nbsp;');
	      var email;
	      var password;
	      var firstname;
	      var lastname;
	      if (simpleHome < 2) {
		      email = $(this.fieldList_client[2]).value;
		      password = $(this.fieldList_client[3]).value;
		      firstname = utils.protect($(this.fieldList_client[1]).value);
		      lastname = utils.protect($(this.fieldList_client[0]).value);
	      } else {
		      email = $(this.fieldList_client[0]).value;
		      password = $(this.fieldList_client[1]).value;
		      // nom et prénom par défaut si home simple
		      firstname = email;
		      lastname = email;
	      }
	      // email = email;
	      // password = password;
	      // QAPI.create = function(email, pass, firstname, lastname, civility, extra, fct) {
	      var extraobj = {
	         optin_newsletter : $('caseNews').className == 'case _on' ? 1 : 0,
	         optin_partners : simpleHome == 0 ? ($('casePartners').className == 'case _on' ? 1 : 0) : 0
	      };
	      this.logInfos = {
	         email : email,
	         password : password,
	         extra : extraobj,
	         fname : firstname,
	         lname : lastname,
	         civ : civil
	      };
	      if (!QAPI.qas.length) {
		      trace("init api");
		      QAPI.init("QOOQ", qooq.account.client_after_init, true);
	      } else {
		      trace("QAPI.create : " + civil);
		      QAPI.create(email, password, firstname, lastname, civil, extraobj, qooq.account.client_add_ok);
	      }
      },
      client_after_init : function(a, b) {
	      if (!a) {
		      alert("Erreur : Le service est indisponible, veuillez re-essayer plus tard. Merci.");
		      qooq.action = false;
		      return;
	      } else {
		      QAPI.create(qooq.account.logInfos.email, qooq.account.logInfos.password, qooq.account.logInfos.fname, qooq.account.logInfos.lname, qooq.account.logInfos.civ, qooq.account.logInfos.extra, qooq.account.client_add_ok);
	      }
      },
      // reception de creation de compte client
      client_add_ok : function(ok, repJSON) {
	      trace('client_add_ok : ' + ok + ' > ' + JSON.encode(repJSON));
	      if (!ok) {
		      $('popTitle').innerHTML = 'ERREUR';
		      if (repJSON && repJSON.errors && repJSON.errors.messages) {
			      $('popContenu').innerHTML = repJSON.errors.messages.join('<br>');
		      } else {
			      $('popContenu').innerHTML = '<br>';
		      }
		      $('popBottom').innerHTML = '<img alt="Retour" src="img/_buttons/bBack.png" id="popRetour" />';
		      $('popRetour').onclick = function() {
			      utils.pop.close();
		      };
	      } else {
		      $('popTitle').innerHTML = 'Succès';
		      var mess = 'Votre inscription a bien été prise en compte, un email de confirmation avec un lien d’activation vous a été envoyé à l’adresse indiquée. Vous n’avez qu’à cliquer dessus pour valider définitivement votre inscription.';
		      $('popContenu').innerHTML = mess + '<br/>';
		      $('popBottom').innerHTML = '<img alt="Retour" src="img/_buttons/bBack.png" id="popRetour" />';
		      $('popRetour').onclick = function() {
			      if (simpleHome == 0) {
				      utils.pop.close();
				      window.scrollTo(0, 0);
				      qooq.action = false;
			      } else {
				      qooq.go_complete();
			      }
		      };
		      // // en commentaire car l'api ne renvoie pas de client_id
		      /*
		       * try { gaTrackPage("/account/accountdone"); perfTrackLead("/account/accountdone", repJSON.client_id); } catch (err) { }
		       */
		      qooqCookies.write(qooq.account.logInfos.email, MD5(qooq.account.logInfos.password));
		      //$('accountBloc').innerHTML = mess;
		      qooq.login.valid(qooq.account.logInfos.email, MD5(qooq.account.logInfos.password), true);
	      }
	      qooq.account.logInfos = 0;
	      qooq.action = false;
	      trace('client_add_ok : fin');
      }
   }
};
var login = qooq.login;
/*
 * 
 */
window.addEvent('domready', qooq.start);

