// JavaScript Document
if (
		(typeof Prototype=='undefined') ||
		(typeof Element == 'undefined') ||
		(typeof Element.Methods=='undefined') ||
		parseFloat(Prototype.Version.split(".")[0] + "." + Prototype.Version.split(".")[1]) < 1.5
		)
	throw("FenetreGC exige le framework Prototype version >= 1.5.0");
	
dataRetriever = Class.create();
dataRetriever.prototype = {
  initialize: function(url) {
		this.authorise 	= true;
		this.method 		= 'get';
		this.async 			= false;
		this.evalJS 		= false;
		this.defautHttp	= 'http';
		if (window.location.protocol == 'http' || window.location.protocol == 'https')
			this.defautHttp = window.location.protocol;
		else if (window.location.protocol != '') {
			this.defautHttp = window.location.protocol.replace(/[^A-Za-z]/, '');	
		}
    
    //alert(this.defautHttp);
    
    this.error 			= '';
    var urlOrig 		= new urlCreate(url);
		this.url 				= urlOrig.toString();
		this.protocol 	= urlOrig.getProtocol();
		this.user 			= (urlOrig.getUserName() != undefined) ? urlOrig.getUserName() : '';
		this.password 	= (urlOrig.getPassword() != undefined) ? urlOrig.getPassword() : '';
		this.address 		= urlOrig.getHost();
		this.port 			= (parseInt(urlOrig.getPort()) < 1) ? '' : parseInt(urlOrig.getPort());
		this.path 			= urlOrig.getPath();
		this.file 			= urlOrig.getFile();
		this.ressource 	= urlOrig.getPath();
		this.params 		= urlOrig.getQuery();
		if (this.params != '') this.params += '&';
		else this.params += '?';
		this.params 		+= '_rand=' + Math.random();
		this.anchor 		= urlOrig.getReference();
		this.paramsStr	= urlOrig.getArgumentValues();
		//alert('argumentvalue:'+url.getArgumentValue('arg'));
		//alert('all arguments:'+url.getArgumentValues());
		
		if (urlOrig.getArgumentValue('__callMethod') == 'get' || urlOrig.getArgumentValue('__callMethod') == 'post') {
			this.setMethod(urlOrig.getArgumentValue('__callMethod'));
		}

    if (this.url == '') {
      this.setErrorMsg('La création du contenu pour une fenêtre exige les paramètres de l\'URL.');
      return false;
		}
/*a REMETRE !!! probleme dans la gestion erreur
		if (this.address != window.location.hostname) {
      this.authorise = false;
      this.setErrorMsg('Pour des raisons de sécurité il n\'est pas possible de récupérer des informations ne venant pas de notre site.');
      return false;
		}*/
		return true;
  },

	setErrorMsg: function(string) {
		if (string != '') {
			this.error = string;
			return true;
		}
		else {
		  this.error = 'La valeur saisie est vide !';
		  return false;
		}
	},

	getErrorMsg: function() {
		return this.error;
	},

	setMethod: function(string) {
	  string = string.toLowerCase();
		if (string != 'post' && string != 'get') {
		  this.setErrorMsg('La valeur saisie est vide !');
		  return false;
		}
		else {
			this.method = string;
			return true;
		}
	},

	getMethod: function() {
		return this.method;
	},

	setAsync: function(bool) {
		if (bool != true && bool != false) {
		  this.setErrorMsg('La valeur saisie est invalide !');
		  return false;
		}
		else {
			this.async = bool;
			return true;
		}
	},

	getAsync: function() {
		return this.async;
	},

	setEvalJS: function(bool) {
		if (bool != true && bool != false) {
		  this.setErrorMsg('La valeur saisie est invalide !');
		  return false;
		}
		else {
			this.evalJS = bool;
			return true;
		}
	},

	getEvalJS: function() {
		return this.evalJS;
	},
	
	createUrl: function() {
		return 	this.protocol + '://' +
						((this.user != '') ?
							this.user +
								((this.password != '') ?
							    ':' + this.password
							    :
							    ''
								)
							+ '@'
							:
							'') +
						this.address +
						(
							(this.port == '') ? '' : ':' + this.port
						) +
						this.ressource ;
	},
  
  /**
    @rtn type de données à retourner : 'data' si données javascript; 'html'; 'div' si objet DOM etc ...
  */
  retrieve: function(rtn) {
    if (!this.authorise) {
      this.setErrorMsg('Non autorisé !');
      return false;
		}
		switch (this.protocol) {
		  case 'menu':
		    return this.menuRetriever(rtn);
		    break;
			case 'newslist':
			  return this.newsListRetriever(rtn);
			  break;
			case 'news':
			  return this.newsRetriever(rtn);
			  break;
			case 'openlist':
			  return this.openingRetriever(rtn);
			  break;
			case 'http':
			case 'https':
			default:
			  return this.defaultRetrieve(rtn);
			  break;
		}
	},

	defaultRetrieve: function(rtn) {
	  var result = '';
		data = new Ajax.Request(
			this.createUrl(),
			{
				method 				: this.getMethod(),
				parameters 		: this.params,
				asynchronous 	: this.getAsync(),
				evalScripts   : ((rtn=='html')?this.getEvalJS():true),
				onComplete		: function(or) {
													result = or.responseText;
												}
			}
		);
		return result;
	},

	menuRetriever: function(rtn) {
	  var result = '';
	  var menu = null;
	  this.protocol = this.defautHttp;
		data = new Ajax.Request(
			this.createUrl(),
			{
				method 				: this.getMethod(),
				parameters 		: this.params,
				asynchronous 	: this.getAsync(),
				evalScripts   : true,
				onComplete		: function(or, mnu) {
				                  result = or.responseText;
				                  menu = mnu;
												}
			}
		);

		//alert(data);

		return ( ((typeof menu) == 'object') ? menu : false );
	},

	openingRetriever: function(rtn) {
	  var result = '';
	  var list = null;
	  this.protocol = this.defautHttp;
		data = new Ajax.Request(
			this.createUrl(),
			{
				method 				: this.getMethod(),
				parameters 		: this.params,
				asynchronous 	: this.getAsync(),
				evalScripts   : true,
				onComplete		: function(or, l) {
				                  result = or.responseText;
				                  list = l;
												}
			}
		);

		return ( ((typeof list) == 'object') ? list : false );
	},

	newsListRetriever: function(rtn) {
	  var result = '';
	  var menu = null;
	  this.protocol = this.defautHttp;
		data = new Ajax.Request(
			this.createUrl(),
			{
				method 				: this.getMethod(),
				parameters 		: this.params,
				asynchronous 	: false,
				evalScripts   : true,
				onComplete		: function(or) {
				                  result = or.responseText;
												}
			}
		);
		//return result.replace(/\&/g, '&amp;');
		return result;
	},
	
	newsRetriever: function(rtn) {
	  var result = '';
	  var menu = null;
	  this.protocol = this.defautHttp;
		data = new Ajax.Request(
			this.createUrl(),
			{
				method 				: this.getMethod(),
				parameters 		: this.params,
				asynchronous 	: false,
				evalScripts   : true,
				onComplete		: function(or) {
				                  result = or.responseText;
												}
			}
		);
		//return result.replace(/\&/g, '&amp;');
		return result;
	}
}

