/*
 * Utility method that eval a json feed into a variable
 * the json feed and the variableName are required, if one of the parameters is missing : an exception is thrown
 * returns the object corresponding to the variable name 
 */
JSONToVar = function(jsonFeed,variableName){
	if (typeof jsonFeed == 'undefined'){
		throw "The JSON feed is a required parameter";
	}
	if (typeof variableName == 'undefined'){
		throw "The varName feed is a required parameter"; 
	}
	eval(variableName+"="+jsonFeed);
	return eval(variableName);
}

/* Default callback function that put the json feed in a variable 'fullTextQueryResults'*/
defaultCallback= function(jsonFeed){
 	JSONToVar(jsonFeed,'queryResults');
}

/*
 * Check parameters from a form
 * formName the name of the formulaire to check
 * return true if parameters are corrects
 */
 checkParameters = function(formName){
 	if (typeof $(formName)['q'] != 'undefined'){
	 	if (($(formName)['q'].value == '')){
			alert('The search term is required');
			$(formName)['q'].focus();
			return false;
		}
		if ($(formName)['q'].value.length > GisgraphyQuery.FULLTEXTQUERY_MAXLENGTH ){
			alert('The search term must have less than '+GisgraphyQuery.FULLTEXTQUERY_MAXLENGTH +' characters');
			$(formName)['q'].focus();
			return false;
		}
	}
	if (typeof $(formName)['lat'] != 'undefined'){
		if ($(formName)['lat'].value == ''){
			alert('The latitude is mandatory');
			$(formName)['lat'].focus();
			return false;
		}
		if ($(formName)['lat'].value > 90 || $(formName)['lat'].value < -90){
			alert('The latitude must be > -90 and < 90');
			$(formName)['lat'].focus();
			return false;
		}
	}
	if (typeof $(formName)["lng"] != 'undefined'){
		 if ($(formName)["lng"].value == ''){
			alert('The longitude is mandatory');
			$(formName)["lng"].focus();
			return false;
		}
		 if ($(formName)["lng"].value > 180 || $(formName)["lng"].value < -180){
			alert('The longitude must be > -180 and < 180');
			$(formName)["lng"].focus();
			return false;
		}
	}
	return true;
 }

/*
 * Default Constructor 
 */
GisgraphyQuery = function(formName, callbackParam){

	GisgraphyQuery.prototype.formName = formName;
	GisgraphyQuery.prototype.form = $(formName);
	GisgraphyQuery.prototype.URL= this.form.action
	GisgraphyQuery.prototype.callback = callbackParam || defaultCallback ;
	if (this.form == 'undefined'){
	GisgraphyQuery.prototype.parameters = {} ;
	}
	else {
	GisgraphyQuery.prototype.parameters = this.form.serialize(true) ;
	}

/*
 * Set a parameter for the Ajax query"
 * The parameter value and the parameter name are both required.
 * The parameters are to be a string
 */
	GisgraphyQuery.prototype.setParameter = function(parameterName,parameterValue){
		if ((typeof parameterName == 'undefined') ||(typeof parameterValue == 'undefined')) {
			throw "parameterName and parameterValue are both required parameters";
		}
		eval("this.parameters."+parameterName+"='"+parameterValue+"'");
	};

/*
 * Set the URL for the Ajax query"
 * The parameter value and the parameter name are both required.
 * The parameter value is to be a string
 */
	GisgraphyQuery.prototype.setURL = function(URLValue){
		if (typeof URLValue == 'undefined') {
			throw "URLValue is mandatory";
		}
		this.URL = URLValue;
	};

}

//Constants
GisgraphyQuery.JSON_FORMAT="JSON";
GisgraphyQuery.FULLTEXTQUERY_MAXLENGTH = 200;


/*
 * Execute a FulltextQuery
 * If no callback function 
 */
GisgraphyQuery.prototype.execute = function(){
	request= this;
	//overide format
	this.parameters.format=GisgraphyQuery.JSON_FORMAT;
	alert(this.formName);
	checkParameters(this.formName);
	new Ajax.Request(this.URL, {
	  method: 'get',
	  evalJSON : true,
	  onSuccess: function(transport) {
	    var notice = $('notice');
	    if (transport.responseText){
	     request.callback(transport.responseText)
	    } else {
	      alert("no response from server");
	      }
	  },
	  onFailure : function(transport){
	  	alert("an error has occured");
	  }, 
	  encoding : "UTF-8",
	  parameters : this.parameters
	});
}
