/**
* @version		$Id: validate.js 7401 2007-05-14 04:12:55Z eddieajau $
* @package		Joomla
* @copyright	Copyright (C) 2005 - 2008 Open Source Matters. All rights reserved.
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL, see LICENSE.php
* Joomla! is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.php for copyright notices and details.
*/

/**
 * Unobtrusive Form Validation library
 *
 * Inspired by: Chris Campbell <www.particletree.com>
 *
 * @author		Louis Landry <louis.landry@joomla.org>
 * @package		Joomla.Framework
 * @subpackage	Forms
 * @since		1.5
 */
var JFormValidator = new Class({
	initialize: function()
	{
		// Initialize variables
		this.handlers	= Object();
		this.custom		= Object();

		// Default handlers
		this.setHandler('username',
			function (value) {
				regex = new RegExp("[\<|\>|\"|\'|\%|\;|\(|\)|\&]", "i");
				return !regex.test(value);
			}
		);

		this.setHandler('password',
			function (value) {
				regex=/^\S[\S ]{2,98}\S$/;
				return regex.test(value);
			}
		);

		this.setHandler('numeric',
			function (value) {
				regex=/^(\d|-)?(\d|,)*\.?\d*$/;
				return regex.test(value);
			}
		);

		this.setHandler('email',
			function (value) {
				regex=/^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;
				return regex.test(value);
			}
		);

		// Attach to forms with class 'form-validate'
		var forms = $$('form.form-validate');
		forms.each(function(form){ this.attachToForm(form); }, this);
	},

	setHandler: function(name, fn, en)
	{
		en = (en == '') ? true : en;
		this.handlers[name] = { enabled: en, exec: fn };
	},

	attachToForm: function(form)
	{
		// Iterate through the form object and attach the validate method to all input fields.
		$A(form.elements).each(function(el){
			el = $(el);
			if ((el.getTag() == 'input' || el.getTag() == 'button') && el.getProperty('type') == 'submit') {
				if (el.hasClass('validate')) {
					el.onclick = function(){return document.formvalidator.isValid(this.form);};
				}
			} else {
				el.addEvent('blur', function(){return document.formvalidator.validate(this);});
			}
		});
	},

	validate: function(el)
	{
		// If the field is required make sure it has a value
		if ($(el).hasClass('required')) {
			if (!($(el).getValue())) {
				this.handleResponse(false, el);
				return false;
			}
		}
		
		// mailinator
		if ($(el).hasClass('validate-email')) {
			var checkemail = $(el).getValue().toLowerCase();
			if (checkemail.match('mailinator|sogetthis|mailin8r|spamherelots|thisisnotmyrealemail|dodgit|bugmenot|fake.com|yopmail|discardmail|@2600.ws|@3v1l0.com|@a.very.long.mail.addy.possibly.one.you.can.use.to.sendmeshit.com|@aberdc.co.uk|@aberdc.info|@behnam.no-ip.biz|@bin.project76.net|@bodhi.lawlita.com|@bonaire.name|@bonaire.net.cn|@bonaire.tk|@breezesoftware.co.uk|@bumpymail.com|@casema.org|@centermail.com|@centermail.net|@clitoburger.com|@definatelynotaspamtrap.com|@discardmail.com|@discardmail.de|@dodgeit.com|@dontsendmespam.de|@dumpmail.de|@e4ward.com|@eintagsmail.de|@emaildienst.de|@emailias.com|@evilhaxx0r.biz|@fastacura.com|@fastchevy.com|@fastchrysler.com|@fastkawasaki.com@fastmazda.com|@fastmitsubishi.com|@fastnissan.com|@fastsubaru.com|@fastsuzuki.com|@fasttoyota.com|@fastyamaha.com|@foo.eqmx.net|@freenet6.de|@front14.org|@ghosttexter.de|@gimme.wa.rez.se|@golfilla.info|@gomail.ws|@guerrillamail.com|@guerrillamail.de|@hingstbella.game-host.org|@jetable.net|@jetable.org|@jetix.info|@junkyard.cc|@kasmail.com|@linux.co.in|@lolinternets.com|@mail.juz.se|@mail.misterpinball.de|@mail.svenz.eu|@mailexpire.com|@mailin8r.com|@mailinator.com|@mailinator.net|@mailinator2.com|@malta.bz|@messagebeamer.de|@mflrevision.000webhost.info|@mx0.wwwneu.eu|@mytrashmail.com|@naked.lolicatgirl.com|@nervmich.net|@nervtmich.net|@netzidiot.de|@nospamfor.us|@nospammail.net|@not.so.l33t.in|@nowthatsfuckedup4.com|@nowthatsscrewedup.com|@nurfuerspam.de|@nuttenstecher.ath.cx|@omicron.token.ro|@openqmaild.org|@overyohead.se|@pookmail.com|@privacy.net|@psilly.com|@punkass.com|@put2.net|@pyramidspel.com|@random.ae-servu.net|@randomcrapofdoom.net|@randomthings.info|@realcambio.com|@sb.sorcix.com|@sendmeshit.com|@senseless-entertainment.com|@slask.boras.org|@slaskpost.se|@slopbox.is.g3nius.net|@slopsbox.com|@slopsbox.net|@slopsbox.org|@slopsbox.osocial.nu|@slopsbox.se|@slopsbox.uni.cc|@sneakemail.com|@sofort-mail.de|@sogetthis.com|@spailbox.com|@spailbox.net|@spailbox.org|@spam.dontassrape.us|@spam.h0lger.de|@spam.j005u.net|@spam.la|@spam.w00ttech.com|@spambob.com|@spambog.com|@spambog.de|@spamex.com|@spamgourmet.com|@spamherelots.com|@spamhole.com|@spaminator.de|@spammotel.com|@spamtrail.com|@spamtrap.saymonz.net|@stfu.plz.nu|@temporarily.de|@temporaryinbox.com|@thegaybay.com|@thisisnotmyrealemail.com|@too.much.b33r.net|@trashdevil.com|@trashdevil.de|@trash-mail.com|@trash-mail.de|@trashmail.net|@trashymail.com|@ul.imu.li|@una.nu|@wegwerfadresse.de|@xenomatrix.dyndns.org|@xenomatrix.nl|@your.gay.cat|@zipzaps.de|@zynd.com|@antichef.net|@antispam24.de|@bank4fun.it|@dodgit.com|@klassmaster.com|@liberto.it|@mail.htl22.at|@oneclickmail.net|@spambox.info|@spamoff.de|@tempinbox.com|@throwaway.de|@trashmail.com|@trashmail.de|@trashmail.org|@twinmail.de|@willhackforfood.biz|@yahoo.it|@guerrillamail.info|@mx0.wwwnew.eu|@trash-mail.de|@mailinator.com|@sofort-mail.de|@spambox.us|@spambog.com|@spambog.de|@discardmail.com|@discardmail.de|@dontsendmespam.de|@mx0.wwwnew.eu|@temporaryinbox.com|@golfilla.info|@mx0.wwwnew.eu|@bodhi.lawlita.com|@mail.misterpinball.de|@mail.svenz.eu|@mail.htl22.at|@qv7.info|@spamgourmet.com|@spammotel.com|@nervmich.net|@nervtmich.net|@wegwerfadresse.de|@sneakemail.com|@netzidiot.de|@fastacura.com|@fastchevy.com|@fastchrysler.com|@fastkawasaki.com|@fastmazda.com|@fastmitsubishi.com|@fastnissan.com|@fastsubaru.com|@fastsuzuki.com|@fasttoyota.com|@fastyamaha.com|@emailias.com|@jetable.net|@mailinator2.com|@sogetthis.com|@mailin8r.com|@spamherelots.com|@thisisnotmyrealemail.com|@trashymail.com|@trash-mail.de|@trashmail.net|@pookmail.com|@@nurfuerspam.de|@gmx-nospam.de|@Spamhole.com|@spam.la|@spambob.com') != null) 
			{
				this.handleResponse(false, el);
				return false;
			}
		}

		// Only validate the field if the validate class is set
		var handler = (el.className && el.className.search(/validate-([a-zA-Z0-9\_\-]+)/) != -1) ? el.className.match(/validate-([a-zA-Z0-9\_\-]+)/)[1] : "";
		if (handler == '') {
			this.handleResponse(true, el);
			return true;
		}

		// Check the additional validation types
		if ((handler) && (handler != 'none') && (this.handlers[handler]) && $(el).getValue()) {
			// Execute the validation handler and return result
			if (this.handlers[handler].exec($(el).getValue()) != true) {
				this.handleResponse(false, el);
				return false;
			}
		}

		// Return validation state
		this.handleResponse(true, el);
		return true;
	},

	isValid: function(form)
	{
		var valid = true;

		// Validate form fields
		for (var i=0;i < form.elements.length; i++) {
			if (this.validate(form.elements[i]) == false) {
				valid = false;
			}
		}

		// Run custom form validators if present
		$A(this.custom).each(function(validator){
			if (validator.exec() != true) {
				valid = false;
			}
		});

		return valid;
	},

	handleResponse: function(state, el)
	{
		// Find the label object for the given field if it exists
		if (!(el.labelref)) {
			var labels = $$('label');
			labels.each(function(label){
				if (label.getProperty('for') == el.getProperty('id')) {
					el.labelref = label;
				}
			});
		}

		// Set the element and its label (if exists) invalid state
		if (state == false) {
			el.addClass('invalid');
			if (el.labelref) {
				$(el.labelref).addClass('invalid');
			}
		} else {
			el.removeClass('invalid');
			if (el.labelref) {
				$(el.labelref).removeClass('invalid');
			}
		}
	}
});




document.formvalidator = null;
Window.onDomReady(function(){
	document.formvalidator = new JFormValidator();
});