﻿var ValidCow = new Class({
	options: {
		prefix: 'val_',
		styles: {
			valid: 'valid',
			error: 'error'
		},
		msgShow: true,
		msgColor: '#ff0000',
		msgValues: {
			textFailFilter: 'Please enter a valid',
			textFailRequired: 'This is a required field',
			textFailMinMax: 'Please enter a character length between ',
			radioFailedRequired: 'You must select an option',
			checkFailRequired:'You must select this option'
		},
		onInitialize: Class.empty,
		onFormValid: Class.empty,
		onFormInvalid: Class.empty
	},
	initialize: function(myForm, options){
		
		this.fireInitialize();
		if(!myForm) { return (false); }
		this.setOptions(options);
		this.formElements = $(myForm).getElements('*[class^='+this.options.prefix+']');
		this.theForm = myForm;
		this.success = true;
		
		$(myForm).addEvent('submit', function(e) {
			if(!this.validate())
			{
				new Event(e).stop();
			}
		}.bind(this));
		
		$(myForm).addEvent('reset', function(e) {
			this.resetForm();
		}.bind(this));
		
		this.formElements.each(function(el) {
		
			el.myType = (el.get('tag') == 'input') ? el.getProperty('type') : el.get('tag');
		
			el.msg = new Element('span');
			if (el.myType=='checkbox')
			el.msg.inject(el, 'after');
			el.msg.setStyles({'color':this.options.msgColor,float:'left',margin:'0 5px 0 5px'});
		}.bind(this));
		
	},
	setMessage: function(el, message, showName, val1, val2) {
		this.success = false;
		var num1 = (val1 == null) ? false : val1;
		var num2 = (val2 == null) ? false : val2;
		var showname = (showName == false) ? '' : showName;
		if(this.options.msgShow == true) {
			if(num1 != false && num2 != false) {
				el.msg.set('text', ' '+message+val1+' and '+val2);
			} else {
				el.msg.set('text', ' '+message+showname);
			}
		}
	},
	validate: function() {
		this.success=true;
		this.formElements.each(function(el) 
{
			this.clearClasses(el);
			el.myParams = el.getProperty('class').replace(this.options.prefix,"").split("-");
			el.myFilter = el.myParams[1];
			el.myType = (el.get('tag') == 'input') ? el.getProperty('type') : el.get('tag');
			switch (el.myType) {
				case "text":
					this.validate_text(el);
					break;
				case "password":
					this.validate_text(el);
					break;
				case "textarea":
					this.validate_text(el);
					break;
				case "radio":
					this.validate_radio(el);
					break;
				case "checkbox":
					this.validate_checkbox(el);
					break;
				case "select":
					this.validate_select(el);
					break;
			}
		}.bind(this));
		
		if(this.success == true) { 
			this.fireValid();
			return(true);
		} else { 
			this.fireInvalid();
			return(false); 
		};
	},
	validate_min_max: function(el) {
		if(el.myParams[2] == null || [el.myParams[2], el.myParams[3]].contains('false')) { 
			return (true);
		} else {
			if(el.value.length >= el.myParams[2] && el.value.length <= el.myParams[3]) { return (true); } else { return (false); }
		}
	},
	validate_text: function(el) {
	
		var valid = true;
		if(el.myParams[0] == 'true') {
			if(el.value.trim() == "") {
				valid = false;
				this.setMessage(el, this.options.msgValues.textFailRequired, false);
			}
			if(this.validate_min_max(el) == false && valid == true) {
				valid = false;
				this.setMessage(el, this.options.msgValues.textFailMinMax, false, el.myParams[2], el.myParams[3]);
			}
			if(this.validate_by_filter(el.myParams[1], el.value) == false && valid == true) {
				valid = false;
				switch(el.myFilter) {
					case "email":
						this.setMessage(el, this.options.msgValues.textFailFilter, ' Email Address');
						break;
					default :
						this.setMessage(el, this.options.msgValues.textFailFilter, ' value');
						break;
				}
			}
		}
		if(valid == true) {
			el.addClass(this.options.styles.valid);
		} else {
			el.addClass(this.options.styles.error);
		}
	},
	validate_radio: function(el) {
		var valid = true;
		if(el.myParams[0] == 'true') {
			var boxValid = false;
			var radioBoxes = $(this.theForm).getElements('input[name^='+el.name+']');
			radioBoxes.each(function(radio) {
				if(radio.getProperty('checked')) {
					boxValid = true;
				}
			}.bind(this));
			if(boxValid == false) {
				valid = false;
			}
		}
		if(valid == true) {
			el.addClass(this.options.styles.valid);
		} else {
			el.addClass(this.options.styles.error);
			this.setMessage(el, this.options.msgValues.radioFailedRequired, false);
		}
	},
	validate_checkbox: function(el) {
		var valid = true;
		if(el.myParams[0] == 'true') {
			if(!el.getProperty('checked')) {
				valid = false;
				this.setMessage(el, this.options.msgValues.checkFailRequired, false);
			}
		}
		if(valid == true) {
			el.addClass(this.options.styles.valid);
		} else {
			el.addClass(this.options.styles.error);
		}
	},
	validate_select: function(el) {
		var valid = true;
		if(el.myParams[0] == 'true') {
			if(!el.getProperty('selected')) {
				valid = false;
				this.setMessage(el, this.options.msgValues.textFailRequired, false);
			}
		}
		if(valid == true) {
			el.addClass(this.options.styles.valid);
		} else {
			el.addClass(this.options.styles.error);
		}
	},
	validate_ssn:function(input_data)
	{
		if ( !input_data.match( /^[0-9]{3}-[0-9]{2}-[0-9]{2}-[0-9]{3}$/ )
		&& !input_data.match( /^[0-9]{3}-[0-9]{3}-[0-9]{2}-[0-9]{2}$/ )
		&& !input_data.match( /^[0-9]{10}$/ ) )
		return false;
		var my_nums = input_data.replace(/-/g,'');
		var valid_nums = "657234567";
		var sum=0;
		for (var temp=8;temp>=0;temp--)
		sum += (parseInt(valid_nums.charAt(temp)) * parseInt(my_nums.charAt(temp)));
		if ( (sum % 11) == 10 ? (0 == parseInt(my_nums.charAt(9))) : ((sum % 11) == parseInt(my_nums.charAt(9))) )
		return true;
		else
		return false;
	},
	validate_regon:function(input_data)
	{
		if (!input_data.match( /^[0-9]{9}$/ ))
		return false;
		var my_nums = input_data.replace(/-/g,'');
		var valid_nums = "89234567";
		var sum=0;
		for (var temp=7;temp>=0;temp--)
		sum += (parseInt(valid_nums.charAt(temp)) * parseInt(my_nums.charAt(temp)));
		
		if ( (sum % 11) == 10 | ((sum % 11) == parseInt(my_nums.charAt(8))) )
		return true;
		else
		return false;
	},
	validate_by_filter: function(type, value) 
	{
		switch (type) 
		{
			case "ssn":
				return this.validate_ssn(value);
				break;
			case "regon":
				return this.validate_regon(value);
				break;
			default:
				return this.validators[type].test(value);
		}
	},
	validators: {
		general:		/^[ąęśćóżźł\.\:\,\;\*\(\!\?\'\)\/\\\$\%\&\+\-\_\#\'\@\w\d\s]+$/i, /* ' */
		alpha: 			/^[a-z\s-_ąęśćóżźł]+$/i, 
		email:			/^([a-z0-9_-]+)(\.[a-z0-9_-]+)*@([a-z0-9_-]+)(\.[a-z0-9_-]+)*[\.]([a-z0-9_-]+)$/i,
		captcha: 		/^[\.\:\,\;\*\(\!\?\'\)\/\\\$\%\&\+\-\_\#\'\@\w\d\s]+$/i, /* ' */
		numeric:		/^\d+$/,
		alphanumeric:	/^[a-z\d]+$/i,

		postalcode:		/^([a-z]\d[a-z])[\s|-]?(\d[a-z]\d)$/i,
		zipcode:		/^\d{2}(-\d{3})?$/,
		
		http:			/^((http|https|ftp):\/\/)?([a-z0-9_-]+)(\.[a-z0-9_-]+)+(\/\w+)*(\.[a-z0-9_-]+)*$/i,
		ipaddress:		/^(\d{1,3})(\.\d{1,3}){3}$/,
		
		lzfulldate: 	/^(0[1-9]|1[012])\/(0[1-9]|[12][0-9]|3[01])\/(?:18|19|20|21)\d\d$/,
		lzmonth:		/^(0[1-9]|1[012])$/,
		lzday:			/^(0[1-9]|[12][0-9]|3[01])$/,
		fulldate:		/^([1-9]|1[012])\/([1-9]|[12][0-9]|3[01])\/(?:18|19|20|21)\d\d$/,
		month:			/^([1-9]|1[012])$/,
		day:			/^([1-9]|[12][0-9]|3[01])$/,
		year:			/^\d{2}$/,
		fullYear:		/^(?:18|19|20|21)\d\d$/,
	
		integer:		/^-?\d{1,3}(,?\d{3})*(\.00)?$/,
		float:			/^([0-9]\d*\.|0\.)[0-9]*\d+$/,
		currency:		/^(\$|\-|\$\-)?\d{1,3}([,]?\d{3})*(\.\d{2})?$/
	},
	resetForm: function() {
		this.formElements.each(function(el) {
			el.myParams = el.getProperty('class').replace(this.options.prefix,"").split("-");
			el.myType = (el.get('tag') == 'input') ? el.getProperty('type') : el.get('tag');
			switch (el.myType) {
				case "text":
					el.value = "";
					break;
				case "password":
					el.value = "";
					break;
				case "textarea":
					el.value = "";
					break;
				case "radio":
					el.checked = "unchecked";
					break;
				case "checkbox":
					el.checked = "unchecked";
					break;
				case "select":
					el.value = "";
					break;
			}
			this.clearClasses(el);
		}.bind(this));
	},
	clearClasses: function(el) {
		el.removeClass(this.options.styles.valid).removeClass(this.options.styles.error);
		el.msg.empty();
	},
	fireInitialize: function() {
		this.fireEvent("onInitialize");
		return(true);
	},
	fireValid: function() {
		this.fireEvent("onFormValid");
		return(true);
	},
	fireInvalid: function() {
		this.fireEvent("onFormInvalid");
		return(false);
	}
});
ValidCow.implement(new Options, new Events);