﻿/*
 *
 *	SimpleForm (Simple client-side form validation)
 *
 *	Version: 1.0
 *	Documentation: AndrewPlummer.com (http://www.andrewplummer.com/code/hottext/)s
 *	Written for: Mootools 1.2
 *	License: MIT-style License
 *	
 *	Copyright (c) 2008 Andrew Plummer
 *
 *
 */

var SimpleForm = new Class({

	Implements: Options,

	options: {
	
		required: null,
		hideSubmit: null,
		addStar: true				
	},
	
	required: new Array(),
	emails: new Array(),

	initialize: function(css, options){

		this.form = $(css);
		this.setOptions(options);
		if(!this.form) this.throwError("No form!");
		
		this.form.addEvent("submit", this.handleSubmit.bindWithEvent(this));
		
		if(options.emails) this.collectElements(options.emails, "email");
		this.collectElements(["email"], "email");
		if(options.required) this.collectElements(options.required, "required");
		if(this.options.hideSubmit){
			var submit = this.form.getElement("input[type=submit]");
			if(!submit) this.throwError("Submit <input> required.");
			this.fx = new Fx.Tween(submit, this.options.hideSubmit.options);
			this.fxProp = this.options.hideSubmit.property;
			this.fxInit = this.options.hideSubmit.amount;
			submit.setStyle(this.fxProp, this.fxInit);
		}
	},
	
	collectElements: function(option, type){
	
		var array = (type == "email") ? this.emails : this.required;
		option.each(function(name){
		
			var el = this.form.getElement("*[name="+name+"]");
			if(type == "required" && !this.emails.contains(el)) el.addEvent("blur", this.checkRequired.bindWithEvent(this));
			else if(type == "email") el.addEvent("blur", this.checkEmail.bindWithEvent(this));
			if(el) array.push(el);
		
		}, this);
		
	},
		
	handleSubmit: function(){
	
		var ok = this.checkSubmit();
		if(!ok) return false;
	},

	showSubmit: function(){
	
		if(!this.options.hideSubmit) return;
		var ok = this.checkSubmit();
		if(ok) this.fx.start(this.fxProp, 0);
		else this.fx.start(this.fxProp, this.fxInit);
	},
	
	checkSubmit: function(){
	
		var ok = true;
		this.required.each(function(el){
			var good = this.checkRequired(null, el);
			if(!good) ok = false;
		}, this);
		this.emails.each(function(el){
			var good = this.checkEmail(null, el);
			if(!good) ok = false;
		}, this);
		
		return ok;
	},
	
	checkRequired: function(event, el){
		
		if(!el) el = event.target;
		var value = el.get("value");
		var ok = value ? true : false;
		if(event){
			this.setLabel(el, ok);
			this.showSubmit();
		}
		return ok;
	},
	
	checkEmail: function(event, el){
		if(!el) el = event.target;
		var value = el.get("value");
		var ok = value.match(/\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i) ? true : false;
		if(event){
			this.setLabel(el, ok);
			this.showSubmit();
		}
		return ok;
	},
	
	setLabel: function(el, ok){
	
		var label = this.form.getElement("label[for="+el.get("id")+"]");
		if(!label) return;
		var text = label.get("text");
		if(!ok){
			label.addClass("error");
			text = text.replace(/^\**/, "*");
		} else {
			label.removeClass("error");
			text = text.replace(/^\**/, "");
		}
		if(this.options.addStar) label.set("text", text);
		
	},
	
	
	throwError: function(error){
	
		var exception = "SimpleForm Error: " + error;
		alert(exception);
		throw new Error(exception);
	}

});


