Ajax.UvisorRequest = Class.create(Ajax.Request, {
	initialize: function($super, url, options) 
	{
		options = Object.clone(options);
		/**
		 * NOTE: the code further down will safely handle these possibilities, but
		 * because they almost certainly indicate programmer error we'll still
		 * throw errors up here when they occur - "DONALD!"
		 */
		if(options.onFailure)
			throw('Error: Ajax.UvisorRequest does not support onFailure functions');
		if((options.onError && options.errorText) || (options.onParameterError && options.parameterErrorText))
			throw('Error: Unacceptable options supplied to Ajax.UvisorRequest -- text for an error handling function and *that* error handling function should not be simultaneously supplied.');

		var error_func = options.onError || function(resp){
			var alert_text = this.options.errorText||'An error has occurred and we were unable to complete your request. If the problem persists, contact Uvisor support.';
			alert(alert_text);
		}.bind(this);
		options.on400 = options.on400 || error_func;
		options.on500 = options.on500 || error_func;
		options.on550 = options.on550 || error_func;
		options.onFailure = options.onFailure || error_func;
	
		var validation_func = options.onParameterError || function(resp){
			var result = resp.responseText.replace(/^\s+|\s+$/g,"");
			if(result != null && result != undefined && result != '')
			{
				alert(result);
			}
			else
			{
				var alert_text = this.options.parameterErrorText||'Invalid information supplied. Check the information you provided and try again. If the problem persists, contact Uvisor support.';
				alert(alert_text);
			}
		}.bind(this);
		options.on460 = options.on460 || validation_func;
		
		
		//create a fully qualified URL even if the original URL was relative 
		//(necessary for the protocol-setting code below to work)
		var baseURL = location.href;
		baseURL = baseURL.substring(0, baseURL.indexOf('/', 14));
		
		var proto = 'https';
		if(baseURL.indexOf('http://') == 0)
		{
			proto = 'http';
		}
		
		if(url.indexOf('/') == 0)
		{
			url = baseURL + url;
		}
		
		//Now, we explicitly set a protocol based on developer input
		options.proto = options.proto || proto;
		if(options.proto == 'https')
		{
			url = url.replace('http://','https://');
		}
		else if(options.proto == 'http')
		{
			url = url.replace('https://','http://');
		}
		
    	$super(url,options);
    	
  	},
}
);
Ajax.UvisorUpdater = Class.create(Ajax.Updater, {
	initialize: function($super, container, url, options) 
	{
		options = Object.clone(options);
		/**
		 * NOTE: the code further down will safely handle these possibilities, but
		 * because they almost certainly indicate programmer error we'll still
		 * throw errors up here when they occur - "DONALD!"
		 */
		if(options.onFailure)
			throw('Error: Ajax.UvisorUpdater does not support onFailure functions');
		if((options.onError && options.errorText) || (options.onParameterError && options.parameterErrorText))
			throw('Error: Unacceptable options supplied to Ajax.UvisorUpdater -- text for an error handling function and *that* error handling function should not be simultaneously supplied.');

		var error_func = options.onError || function(resp){
			var alert_text = this.options.errorText||'An error has occurred and we were unable to complete your request. If the problem persists, contact Uvisor support.';
			alert(alert_text);
		}.bind(this);
		options.on400 = options.on400 || error_func;
		options.on500 = options.on500 || error_func;
		options.on550 = options.on550 || error_func;
		options.onFailure = options.onFailure || error_func;
	
		var validation_func = options.onParameterError || function(resp){
			var result = resp.responseText.replace(/^\s+|\s+$/g,"");
			if(result != null && result != undefined && result != '')
			{
				alert(result);
			}
			else
			{
				var alert_text = this.options.parameterErrorText||'Invalid information supplied. Check the information you provided and try again. If the problem persists, contact Uvisor support.';
				alert(alert_text);
			}
		}.bind(this);
		options.on460 = options.on460 || validation_func;
		
    	$super(container,url,options);
  	},
}
);

Ajax.UvisorFormSubmitter = Class.create(Ajax.UvisorRequest, {
	initialize: function($super, form, url, options) 
	{
		options = Object.clone(options);
		this.options = options;
		this.url = url;
		this.form = form;
		this.mySuper = $super;
  	},
  	submit: function()
  	{
  		this.options.postBody = this.form.serialize();
  		mySuper(this.url,this.options);
  	}
}
);
