/*
 * Create a document global IFRAME that catches submitted form results.
 * This gets pasted to a selectable element
 */
var iframe = document.getElementById('AjaxUploadFrame');
if (!iframe) {
	iframe = new IFrame({

		id: 'AjaxUploadFrame',
		name: 'AjaxUploadFrame',
				
		styles: {
			display: 'none'
		},

		events: {
			load: function() {
				// extract response from IFRAME body
				var json = 0, responseText = null, responseXML = null;
				try {
					responseText = this.contentWindow.document.body
							?this.contentWindow.document.body.innerHTML
							:null;
					responseXML = this.contentWindow.document.XMLDocument
							?this.contentWindow.document.XMLDocument
							:this.contentWindow.document;
				} catch(e) {
					json = {success:0,message:'lost AJAX response'};
				}

				// construct JSON object
				if (!json) {
					try {
						// perform a html entity decode
						responseText = responseText
								.replace(/&lt;/g, '<')
								.replace(/&gt;/g, '>')
								.replace(/&amp;/g, '&');

						json = JSON.decode(responseText);
						if (!json)
							json = {success:0,message:'empty AJAX response'};
					} catch(e) {
						window.alert(responseText);
						json = {success:0,message:'invalid AJAX response'};
					}
				}
				json.responseText = responseText;
				json.responseXML = responseXML;
				
				// fire events
				if (json.success) {
					this.fireEvent('onSuccess', json );
				} else {
					this.fireEvent('onFailure', json );
				}
			}
		}
	 
	});
	document.body.appendChild(iframe);
}

/*
 * Extend element to support uploading of form files with fancy in-progress bar
 */
MyAjaxForm = window.MyAjaxForm || {};
MyAjaxForm = new Class(
	{
		
		getOptions: function(){
			return {
			};
		},
		initialize: function( el, options ){
			// basic setup
			this.element = $(el);
			this.setOptions( this.getOptions(), options );

			// submit to the intermediate IFRAME
			this.element.target = 'AjaxUploadFrame';
			
			// invoke this objects submit when submitting
			this.element.addEvent('submit', this.submit.bindWithEvent(this) );
		},

		showProgress: function() {
			this.element.set('class', this.element.get('class').replace('engineidle', 'enginebusy'));
		},
		hideProgress: function() {
			this.element.set('class', this.element.get('class').replace('enginebusy', 'engineidle'));
		},

		submit: function(evt){
			evt.preventDefault();
			
			// prepare the intermediate IFRAME before submitting to it
			var iframe = $('AjaxUploadFrame');
			if (iframe) {
				var me = this; // use function closure to access proper object
				me.success = function(json) {
					// remove handlers
					iframe.removeEvent('onSuccess', me.success);
					iframe.removeEvent('onFailure', me.failure);
					// hide progress bar
					me.hideProgress();
					// fire event
					me.fireEvent('onSuccess', json );
				};
				me.failure = function(json) {
					// remove handlers
					iframe.removeEvent('onSuccess', me.success);
					iframe.removeEvent('onFailure', me.failure);
					// hide progress bar
					me.hideProgress();
					// fire event
					me.fireEvent('onFailure', json );
				};
				iframe.addEvent('onSuccess', me.success);
				iframe.addEvent('onFailure', me.failure);
					
				// show in-progress bar
				me.showProgress();
				// submit form
				me.element.submit();
			}
			return false;
		}
	}
);
MyAjaxForm.implement( new Options );
MyAjaxForm.implement( new Events );
