function FormSubmit(formid, options) {
	
	this.initialize = function(formid, options) {
		
		this.callback = null;
		
		if(typeof options !== 'unfefined') {
			this.callback = options.callback;
			if(typeof options.sendform === 'undefined') {
				this.sendform = options.onsubmit;
			} else {
				this.sendform = false;
			}
		}
		
		//set properties
		this.formid = formid;
		this.required_fields = new Array();
		this.type_int = new Array();
		this.errors = new Array();
		this.status = false;
		
		$(this.formid).observe('submit', this.send.bind(this));
	}
	
	this.required = function(required) {
		this.required_fields = Array.from(required);
	}
	
	this.is_email = function(email) {
		this.type_email = email;
	}
	
	this.is_int = function(fields) {
		this.type_int = Array.from(fields);
	}
	
	this.setPasswordRules = function(rules) {
		if(typeof rules != 'object') {
			return false;
		}
	}
	
	this.check_int = function() {
		if(this.type_int.length === 'undefined' || typeof this.type_int === 'undefined') {
			return false;
		}
		
		$A(this.type_int).each(function(i) {
			
			var field = i;
			var int = $F(field) * 1;

			if(int === 'NaN' || int === 0) {
				
				this.status = false;
				this.errors[i] = field+' is not an int';
				return false;
			} else {
				this.status = true;
			}
			
		}.bind(this));
		
		
		
		if(!this.status) {
			return false;
		}
		return true;
	}
	
	this.check_email = function() {
		if($('email')) {
			var email = $F(this.type_email);
			var pattern = /^[^@]+@[^@]+.[a-z]{2,}$/i;
			var test 	= email.search(pattern);

			if(test == -1) {
				alert(test+email)
				this.errors['email'] = 'Your email is invalid';
				this.status = false;
				return false;
			}
		}

		this.status = true;
		return true;
		
	}
	
	
	this.check_required = function() {
		$A(this.required_fields).each(function(i) {
			
			if(!Field.present(i)) {
				this.status = false;
				return false;
			}
			
		}.bind(this));
		
		if(!this.status) {
			return false;
		}
		
		return true;
	}
	
	this.write_errors = function() {
		var allinput = Form.getElements(this.formid);
		
		for(var p in allinput) {
			if(allinput.hasOwnProperty(p)) {
				//alert(allinput[p].name)
			}
		}
		
		if($(this.formid+'_errors')) {
			$(this.formid+'_errors').remove();
		}
		
		$(this.formid).insert({
			
			top: '<div id="'+this.formid+'_errors" class="errors"><ul id="error_list"></ul></div>'
			
		});
		
		for(var k in this.errors) {
			if (this.errors.hasOwnProperty(k)) {
			        $('error_list').insert({
			            bottom: new Element('li').update(k + ' :'+this.errors[k])
			        })
			    }
		}
		
	}
	
	this.send = function() {

		if(this.type_int.length > 0) {
			if(!this.check_int()) {
				this.status = false;
			}
		}
		
		if(this.required_fields.length > 0) {
			if(!this.check_required()) {
				
				this.status = false;
				
			} 
		}
		
		if(typeof this.type_email != 'undefined') {
			if(!this.check_email()) {
				this.status = false;
			}
		}
		
		if(!this.status) {
			
			this.write_errors();
						
		} else {
			
			var cb = this.callback;
			
			if(this.sendform) {
				window[cb].apply();
				$(this.formid).submit();
			} else {
				window[cb].apply();
			}
			
		}
	}
	
	this.initialize(formid, options);

}

function addMailAddress() {
	var url = '/ajax/add_email';
	var params = {'email':$('email').value};

	new Ajax.Request(url, {
		method: 'post',
		parameters: params,
		onSuccess: function(r) {
			if(r.responseText != '1') {
				alert(r.responseText)
			} else {
				$('email').value = 'thank you';
			}
		}
	})
	
}




var mailing = new FormSubmit('mail_list', {callback: 'addMailAddress'});
mailing.is_email('email');
$('email').observe('click', function() {
	if($('email').value == 'JOIN OUR MAILING LIST') {
		$('email').value = '';
	}
})



