var formHandler= Class.create({
	form: null,
	identifier: null,
	updater: null,
	submitButton: null,
	submitText: null,
	redirect: null,
	
	initialize: function(name,redirect) {
		this.form=$(name);
		this.redirect=redirect;
		
		var el, i = 0;
		while (el = this.form.elements[i++]){
			if( el.name=='submit' && el.type!='submit' ){
				alert('error: form has field called submit which is not a submit button');
				continue;
			}else if( el.name=='action' ){
				alert('error: form has field called action');
				continue;
			}else if( el.name=='target' ){
				alert('error: form has field called target');
				continue;
			}else if( el.name=='reset' ){
				alert('error: form has field called reset');
				continue;
			}
			
			if( el.type != 'hidden' && el.type!='submit' ){
				//el.observe('change', reset_field.bindAsEventListener(el)); //doesn't work in ie!
				new Form.Element.EventObserver(el, this.reset_field.bindAsEventListener(el));
			}else if( el.type=='submit' ){
				this.submitButton=el;
			}
		}
		
		this.form.observe('submit', this.onSubmit.bindAsEventListener(this));

		if (this.form.select('input[name="UPLOAD_IDENTIFIER"]')[0]) {
			this.identifier = this.form.select('input[name="UPLOAD_IDENTIFIER"]')[0].value;
		
			var iframe = document.createElement('iframe');
			iframe.setAttribute('id', 'upload');
			iframe.setAttribute('name', 'upload');
			iframe.id='upload'; //IE
			iframe.name='upload'; //IE
			iframe.setAttribute('style', 'display: none; width: 0; height: 0;');
			iframe.setAttribute('src', 'about:blank');
			iframe.style.display='none'; //IE
			iframe.style.width='0'; //IE
			iframe.style.height='0'; //IE
			
			this.form.appendChild(iframe);
			
			/*
			alert($('upload'));
			alert($('upload').name);
			
			alert(this.form.targer);
			*/
			
			if(self.frames['upload'].name != 'upload') {
				self.frames['upload'].name = 'upload';
			}
			
			this.form.target='upload';
		}
	},
	
	reset_field: function(e)
	{
		this.style.backgroundColor='#FFFFFF';
	},
	
	onSubmit: function(evt)
	{
		Event.stop(evt);
	
		if( this.submitButton ){
			submitText=this.submitButton.innerHTML;
		
			this.submitButton.disabled=true;
			this.submitButton.innerHTML='Saving...';
		}
	
		var url = location.href;
		
		var pars = Form.serialize(this.form);
			pars=pars+'&validate=1';
		
		var myAjax = new Ajax.Request(
				url, 
				{
					method: 'post', 
					parameters: pars, 
					onComplete: this.submitResponse.bindAsEventListener(this)
				});
				
		return false;	
	},
	
	submitResponse: function(originalRequest)
	{
		result = originalRequest.responseText;
	
		//alert(result);
	
		returned=eval(result);
	
		if( this.submitButton ){
			this.submitButton.disabled=false;
			this.submitButton.innerHTML=submitText;
		}
	
		if( parseInt(returned)!=returned-0 ){
			if( returned.length>0 ){
				var errors='\n';
				for( i=0;i<returned.length;i++ ){
					if( returned[i]=='email in use' ){
						$('email').className='error';
						errors+='email in use\n';
					}else if( $(returned[i]) ){
						$(returned[i]).style.backgroundColor='#FFBFBF';
						errors+=returned[i]+'\n';
					}else{
						errors+=returned[i]+'\n';
					}
				}
				alert('Please check the following:'+errors);
				$(returned[0]).focus();
			}
		}else{
			if( this.identifier ){
				this.onCreate();
			}
			
			this.form.submit();
		}
	},

	onCreate: function() {
        $('upload').observe('load', this.onComplete.bindAsEventListener(this));
		
		Ext.MessageBox.show({
			title: 'Please wait - Uploading',
			msg: 'Uploading..',
			progressText: 'Initializing...',
			width:400,
			progress:true,
			closable:false
		});
		updater = new PeriodicalExecuter(this.__tick.bind(this), 1);
	},

	// Timer function
	__tick: function(identifier) {
		new Ajax.Request('/_lib/ajax/upload_progress.php', { 
			method: 'post', 
			parameters: { 'uniq': this.identifier }, 
			onComplete: function(result){
				var data=eval('('+result.responseText+')');
				if( data.perc ){
					Ext.MessageBox.updateProgress((data.perc/100), data.perc+'% completed');
					Ext.MessageBox.updateText(data.status);
				}
			}
		});
	},

	onComplete: function() {
		updater.stop();
		
		if( this.redirect ){
			window.location.href=this.redirect;
		}else{
			window.location.href=window.location.href
		}
	}
});
