Dvintage.Form = Class.create({
    initialize: function(){
  		this.errorNodes = $$('.error');
		if (this.errorNodes.length == 0)
			return;
		this.inputNodes = [];
		this.enteredTexts = [];
		this.hasbeenclicked = [];
		this._findInputNodes();
		this._insertElements();
		this.errorNodes[0].up('form').observe('submit', this._handleSubmit.bind(this));
    },
	_findInputNodes:function(){
		this.errorNodes.each(function(elem){
			if (elem.innerHTML=='')
				return;
			if (elem.previous('input'))
				var inputNode = elem.previous('input');
			else if (elem.previous('select'))
				var inputNode = elem.previous('select');
			else if (elem.previous('textarea'))
				var inputNode = elem.previous('textarea');
			this.inputNodes.push(inputNode);
		}.bind(this))
	},
	_insertElements:function(){
		this.errorNodes.each(function(elem, i){
			if (elem.innerHTML=='')
				return;			
			var inputNode = this.inputNodes[i];
			if (inputNode.tagName == 'INPUT' || inputNode.tagName == 'TEXTAREA') {
				this.enteredTexts[i] = inputNode.value;
				if (inputNode.readAttribute('type') == 'password') {
					var tempinput = new Element('input', {temppassword:'true'}).setStyle({color:'#B4005E', cursor:'pointer'});
					tempinput.value = elem.innerHTML;
					inputNode.insert({before:tempinput});
					tempinput.observe('click', function(){
						this._handleClick(i);
					}.bind(this));
					inputNode.hide();
				}
				inputNode.value = elem.innerHTML;
			}
			inputNode.setStyle({color:'#B4005E', cursor:'pointer'})
			inputNode.observe('click', function(){
				this._handleClick(i);
			}.bind(this));
			inputNode.observe('focus', function(){
				this._handleClick(i)
			}.bind(this));
		}.bind(this))		
	},
	_handleSubmit:function(){
		this.inputNodes.each(function(elem, i){
			if (this.errorNodes[i].innerHTML=='')
				return;
			if (!this.hasbeenclicked[i])
				this._handleClick(i);
			}.bind(this)
		)
	},	
	_handleClick:function(i){
		this.hasbeenclicked[i] = true;
		this.inputNodes[i].setStyle({color:'#000', cursor:'default'});
		if (this.inputNodes[i].tagName == 'INPUT' || this.inputNodes[i].tagName == 'TEXTAREA') {
			if (this.inputNodes[i].previous()) {
				this.inputNodes[i].previous().remove();
				this.inputNodes[i].show();
			}
			this.inputNodes[i].value = this.enteredTexts[i];
		}
	}
	
})

document.observe('dom:loaded', function () {
	new Dvintage.Form();
})






Dvintage.DisplayFormErrors = Class.create({
	initialize: function(parent){
		this.inputNodes = parent.select('input, textarea');
		this.parentNode = parent;
		this.messages = {
			'not.blank':'Please fill in',
			email:'This is not a correct email address'
		}
	},
	_hideError: function(evt){
		var errornode = evt.element();
		var field = errornode.previous('input, textarea');
		errornode.remove();
		field.show();
		field.focus();
	},
	showError: function(fieldname, error){
		var field = this.parentNode.down('input[name='+fieldname+'], textarea[name='+fieldname+']');
		if (field.tagName == 'INPUT') {
			var errornode = new Element('input', {id:field.id}).addClassName('invalidfield');
			if (field.readAttribute('type')=='file') {
				errornode.setStyle({position:'absolute'})
			}
			errornode.value = this.messages[error];
		} else {
			var errornode = new Element('textarea').addClassName('invalidfield');
			errornode.update(this.messages[error]);
		}
		field.hide();
		field.insert({after:errornode});
		errornode.observe('focus', this._hideError.bind(this));
	},
	removeAllErrors: function(){
		this.parentNode.select('.invalidfield').invoke('remove');
		this.inputNodes.invoke('show');
	}
})





