var Validate = Class.create();
Validate.prototype = {

	initialize: function()
	{
		this.count = 0;
		this.form = $$('.validate');
		if(this.form != ""){
			this.fields = $$('.validate .valid');
			this.sbt = $$('.validate input[type="submit"]')[0];
			try{
				this.sbt.type = "button"; /* IE does not allow to change type of input element */
			}catch(e){
				var n_sbt = new Element('input', {
					type		: 'button', 
					className	: this.sbt.className,
					name		: this.sbt.name,
					value		: this.sbt.value,
					id			: this.sbt.id
				});
				this.sbt.parentNode.appendChild(n_sbt);
				this.sbt.parentNode.removeChild(this.sbt);
				this.sbt = n_sbt;
			}
			
		}

	},


	do_valid: function ()
	{
		this.count = 0;
		this.fields.each(function (e){
			if(e.value == ""){
				alert(required_fields);
				e.focus();
				this.count = 1;
				throw $break;
			}
			if(e.hasClassName('email')){
				if(!this.is_mail(e)){
					alert(not_valid_email);
					e.focus();
					this.count = 1;
					throw $break;
				}
			}
			if(e.id == "pass" && $('conf')){
				if(!this.is_same()){
					alert(passes_not_match);
					this.count = 1;
				}
			}
		}.bind(this));
//		if(this.count == 0) {
//			this.form[0].submit();
//		}
	},

	is_mail: function(element)
	{
			if($F(element).match(/^\s*$/))
				return; // should be handled by isempty
			var regex = /^([\w]+)(\.[\w]+)*@([\w]+\.)+([\w]{2,4})$/;
			var mail = $F(element);
			if(mail.match(regex))
				return true;
			else
				return false;
	},

	is_same: function (){
		if($('pass').value !== $('conf').value){
			return false;
		}
		return true;
	}

};


