/**
 * That function creates 'wrapper' over ValidationTextField fo handle remote validations
 * All validator functions are passed via standard 'validation' function for 'remote' ValidationDescriptor
 * 
 * Please note! Params are not 'validator' params but 'DataSet' params 
 * That are "method", "url", "async", "username", "password", "postData", "successCallback", "errorCallback", "headers", "userData", "xhRequest"
 * Please do not use any other paramethers -- it is not checked by me now (leaved it to the Spry.Framework).
 * 
 * Remote params includes paramName, validValue (1st is name to put in request, 2nd is result which indicates valid value)
 * 
 */
Spry.Widget.ValidationRemote = function (element, params) {
	// Make inner scope link to 'this' object -- will be used further in 'validation' function (closures)
	var thisObject = this; 
	// Cache for validation requests.
	this.validCache = {};
	
	
	// make a copy of params
	this.params = params;
	this.url = params.url;
	this.paramName = params.paramName;
	this.validValue = params.validValue;
	
	// Set to true if request is in process.
	// So, user can type anything in the field while request is made, and this will not produce multiple requests.
	this.isRequest = false; 
	this.checkingValue = '';
	this.gotValue = null;
	
	// restart function; makes validation re-validate itself.
	this.restart = function(){};
	
	// Creating validator itself.
	this.validationField = new Spry.Widget.ValidationTextField(element);

	this.dataSource = new Spry.Data.JSONDataSet(this.url, params);
	
	var obj = {

			onPostLoad: function(notifier, data)
			{
				thisObject.gotValue = thisObject.dataSource.getData();
				thisObject.gotValue = thisObject.gotValue[0].column0;
				thisObject.validCache[thisObject.checkingValue] = (thisObject.gotValue == thisObject.validValue);
				thisObject.isRequest = false;
				// Re-start validation, to check again.
				setTimeout(thisObject.restart, 100);
				
				
			}
	}
	this.dataSource.addObserver(obj);
	// !!! Note! Here used closure -- i have to set some function to validator and pass current 'this' to it (to access
	// other Remote functions), but cannot use 'this' in the text as it already used by caller object. 
	// So, i used closure here and thisObject as reference to current 'this'
	
	this.validationField.validation = function remoteValidate(value, options) {
		// 1. Check is that value already in cache
		if (typeof thisObject.validCache[value] != 'undefined') {
			return thisObject.validCache[value];
		}
		// 2. if already in request -- return false;
		if (thisObject.isRequest) {
			return false;
		}

		// 3. In any other case -- get data. 
		thisObject.checkingValue = value;
		thisObject.isRequest = true;
		thisObject.dataSource.setURL(thisObject.url+'?'+thisObject.paramName + '=' + value);
		
		// check what function was called.
		// As now, we are checking for 'onsubmit';
		var start = arguments.callee.caller;
		var name = '';
		while (start) {
			name = start.name;
			if (name == 'onsubmit') {
				thisObject.restart = start;
				break;
			}
			// move to the upper function
			start = start.caller;
		} 
		if (!name) {
			// If no any other calls -- revalidate field itself.
			thisObject.restart = function() {thisObject.validationField.validate(); };
		}
		
		thisObject.dataSource.loadData();
		return false;
	}

}

