var utils = {

  serialize: function(object) {

    for (var i = 0; i < object.length; i++) {
    }

    return // queryComponents.join('&');
  }
 ,locale : null 
	//
  // Parses a querystring into a object consisting of the name=value pairs
  //
  // Splits the querystring up and returns an object with properties that
  // correspond to the names of the variables in the querystring.
  //
  // @author  Dan Delaney     http://fluidmind.org/
  // @param   queryString     Obviously, the query string to parse
  //
 ,parseQueryString : function (queryString) {

      if(queryString == 'undefined' || queryString == '') {
          return false;
      } else {
          // Get rid of a leading '?' so that you can pass 'location.search' to this function
          if(queryString.substr(0, 1) == '?') { queryString = queryString.substr(1); }
  
          // Split up the querystring
          var components = queryString.split('&');
  
          // Assign each variable of the querystring to a new property of the final object
          var finalObject = new Object();
          var parts;
          for (var i = 0; i < components.length; i++) {
              parts = components[i].split('=');
              finalObject[parts[0]] = decodeURI(parts[1]);
          }
  
          return finalObject;
      }
  }
  // 
  // Check if a user is connected
  // 
  // Call the service testConnected.php
  // 
  // Returns boolean
  // - true  : connected
  // - false : not connected
  //                        
  // @author  Roger FREBAULT  roger.frebault@free.fr
  // @param   no
  // 
  //      
 ,userConnected : function () {
 
        var returnCode;
          
        dojo.xhrGet( {
        
          url: "../php/userConnected.php", 
          handleAs: "json",
          sync : true,  

          load: function(response, ioArgs) { 
            if (response.response == true) {
              returnCode = response;
            } else {
              returnCode = response;
            }
          },
  
          error: function(response, ioArgs) { 
            console.error("HTTP status code: ", ioArgs.xhr.status); 
            returnCode.response = false; 
          }
        });
        
        return returnCode;
 } 
  // 
  // Disconnect a user
  // 
  // Call the service disconnect.php
  // 
  // Returns nothing
  //                        
  // @author  Roger FREBAULT  roger.frebault@free.fr
  // @param   no
  // 
  //      
 ,disconnect : function() {

      var dfd = dojo.xhrPost( {
          
                url: "../php/disconnect.php", 
                sync : true,  
                handleAs: "json",
                
                load: function(response, ioArgs) { 

                  if (response.response) {
		                dojo.registerModulePath("root", "../../..");
		                dojo.requireLocalization("root", "translation", dojo.myLocale);
		                translation = dojo.i18n.getLocalization("root", "translation", dojo.myLocale);
                    dojo.byId("message").innerHTML = translation.Disconnecting ;
                    document.location = "..";
                  } else {
                    dojo.byId("message").innerHTML = response.message ;
                  }
                },
        
                error: function(response, ioArgs) { 
                  dojo.byId("message").innerHTML = "HTTP status code: "+ ioArgs.xhr.status; 
                }
       });
 }
 ,removeNode : function (node) {
    utils.removeAllChildren(node); 
    node.parentNode.removeChild(node);
 } 
 ,removeAllChildren : function (node) {
    while (node.firstChild) { 
      node.removeChild(node.firstChild);
    };
 } 
 ,euroFormatted : function (amount)
  {
		amount = amount.toString();

		if (amount.indexOf(".") == -1) {
			amount = amount + ".00";
		}
  var delimiter = " "; // replace comma if desired
  var a = amount.split('.',2)
  var d = a[1];
  var i = parseInt(a[0]);
  if(isNaN(i)) { return ''; }
  var minus = '';
  if(i < 0) { minus = '-'; }
  i = Math.abs(i);
  var n = new String(i);
  var a = [];
  while(n.length > 3)
  {
  	var nn = n.substr(n.length-3);
  	a.unshift(nn);
  	n = n.substr(0,n.length-3);
  }
  if(n.length > 0) { a.unshift(n); }
  n = a.join(delimiter);
  if(d.length < 1) { amount = n; }
  else { amount = n + ',' + d + ' €'; }
  amount = minus + amount;
  return amount;
  }
  // end of function euroFormatted

 ,replaceAll : function (motif, replaceText, text) {

	var _text = text;
	while (_text.indexOf(motif,0) > 0) {

		_text = _text.replace(motif, replaceText);

	}
	return _text
 }
 ,getTranslationValues : function (_id) {
	
		var values = new Array();
		if (_id != 0) {
			var dfd = dojo.xhrGet( {
	              
	              url: '../php/translation.php', 
	              content : { id : _id 	
													}, 
	              handleAs: 'xml',
								sync : true,
	              load: function(data, ioArgs) {
	
							    data = data.getElementsByTagName('reponse')[0];
							
									values["en"] = data.getElementsByTagName('en')[0].firstChild.nodeValue;
							    values["fr"] = data.getElementsByTagName('fr')[0].firstChild.nodeValue;
	
								},
	              error: function(response, ioArgs) { 
	                console.error('HTTP status code: ', ioArgs.xhr.status); 
	                return response; 
	              }
	
	    });
		} else {
			values["en"] = '';
	    values["fr"] = '';
		}     
		return values;								
		 	
 }
 ,getTranslationValue : function (_id, language) {

		var value = "";
		if (_id != 0) {
	    var dfd = dojo.xhrGet( {
	              
	              url: '../php/translation.php', 
	              content : { id : _id
													 ,language : language	 	
													}, 
	              handleAs: 'xml',
								sync : true,
	              load: function(data, ioArgs) {
	
							    data = data.getElementsByTagName('reponse')[0];
									value = data.getElementsByTagName(language)[0].firstChild.nodeValue;
	
								},
	              error: function(response, ioArgs) { 
	                console.error('HTTP status code: ', ioArgs.xhr.status); 
	                return response; 
	              }
	
	    });
		} 
		return value;								
		 	
 }
 ,getElementsByClassName : function(nomClasse, element) {
	  var resulat = new Array();
	  if(nomClasse!="" || typeof element == 'object'){
	    var mesFils = element.getElementsByTagName('*');
	    var exp_reg = new RegExp("(^|\\s)" + nomClasse + "(\\s|$)");
	    for (var i = 0; i < mesFils.length; i++) {
	      var laClasse = (mesFils[i].className)? mesFils[i].className : "";
	      if(laClasse != "" && (laClasse == nomClasse || laClasse.match(exp_reg))){
	        resulat.push(mesFils[i])
	      }
	    }
	  }
	  return resulat;
 }
 ,destroyDijit : function(id) {
 		if (dijit.byId(id)) {
			dijit.byId(id).destroy();
		}
 }	
}
