/*
* Really easy field validation with Prototype
* http://tetlaw.id.au/view/javascript/really-easy-field-validation
* Andrew Tetlaw
* Version 1.5.4.1 (2007-01-05)
*
* Copyright (c) 2007 Andrew Tetlaw
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/
var Validator = Class.create();

Validator.prototype = {
	initialize : function(className, error, test, options) {
		if(typeof test == 'function'){
			this.options = $H(options);
			this._test = test;
		} else {
			this.options = $H(test);
			this._test = function(){return true};
		}
		this.error = error || 'Validation failed.';
		this.className = className;
	},
	test : function(v, elm) {
		return (this._test(v,elm) && this.options.all(function(p){
			return Validator.methods[p.key] ? Validator.methods[p.key](v,elm,p.value) : true;
		}));
	}
}
Validator.methods = {
	pattern : function(v,elm,opt) {return Validation.get('IsEmpty').test(v) || opt.test(v)},
	minLength : function(v,elm,opt) {return v.length >= opt},
	maxLength : function(v,elm,opt) {return v.length <= opt},
	min : function(v,elm,opt) {return v >= parseFloat(opt)},
	max : function(v,elm,opt) {return v <= parseFloat(opt)},
	notOneOf : function(v,elm,opt) {return $A(opt).all(function(value) {
		return v != value;
	})},
	oneOf : function(v,elm,opt) {return $A(opt).any(function(value) {
		return v == value;
	})},
	is : function(v,elm,opt) {return v == opt},
	isNot : function(v,elm,opt) {return v != opt},
	equalToField : function(v,elm,opt) {return v == $F(opt)},
	notEqualToField : function(v,elm,opt) {return v != $F(opt)},
	include : function(v,elm,opt) {return $A(opt).all(function(value) {
		return Validation.get(value).test(v,elm);
	})}
}

var Validation = Class.create();
var disableValidation = false;

Validation.prototype = {
	initialize : function(form, options){
		this.options = Object.extend({
			onSubmit : true,
			advice : true,
			stopOnFirst : false,
			immediate : false,
			focusOnError : false,
			useTitles : false,
			onFormValidate : function(result, form) {},
			onElementValidate : function(result, elm) {}
		}, options || {});
		this.form = $(form);
		if(this.options.onSubmit) Event.observe(this.form,'submit',this.onSubmit.bind(this),false);

		if(this.options.immediate) {
			// Validate straight away
			this.validate({advice:false});

			var useTitles = this.options.useTitles;
			var callback = this.options.onElementValidate;
			Form.getElements(this.form).each(function(input) { // Thanks Mike!
				Event.observe(input, 'blur', function(ev) { Validation.validate(Event.element(ev),{useTitle : useTitles, onElementValidate : callback}); });
				// Live validation
				Event.observe(input, 'keyup', function(ev) { Validation.validate(Event.element(ev),{useTitle : useTitles, onElementValidate : callback, advice: false}); });
			});
		}
	},
	onSubmit :  function(ev){
		if(disableValidation != true && !this.validate()) Event.stop(ev);
	},
	validate : function(options) {
		var result = false;
		var useTitles = this.options.useTitles;
		var callback = this.options.onElementValidate;

		options = Object.extend({
			advice : true,
			useTitle : useTitles,
			onElementValidate : callback
		}, options || {});

		if(this.options.stopOnFirst) {
			result = Form.getElements(this.form).all(function(elm) { return Validation.validate(elm,{useTitle : useTitles, onElementValidate : callback}); });
		} else {
			result = Form.getElements(this.form).collect(function(elm) { return Validation.validate(elm, options); }).all();
		}

		if(!result && this.options.focusOnError) {
			Form.getElements(this.form).findAll(function(elm){return $(elm).hasClassName('validation-failed')}).first().focus()
		}

		if (!result && options.advice){
			show_alert('One or more problems occured when trying to submit the form. The problems are indicated by the red fields. Please fix them before continuing.');
		}

		this.options.onFormValidate(result, this.form);
		return result;
	},
	reset : function() {
		Form.getElements(this.form).each(Validation.reset);
	}
}

Object.extend(Validation, {
	validate : function(elm, options){
		options = Object.extend({
			useTitle : false,
			onElementValidate : function(result, elm) {}
		}, options || {});

		if (typeof(this.binds) == 'undefined'){
			this.binds = new Array();
		}

		elm = $(elm);
		var cn = elm.classNames();

		if (elm.type && (elm.type == 'button' || elm.type == 'submit')){
			return true;
		}

		return result = cn.all(function(value) {
			var test = Validation.test(value,elm,options.useTitle, options.advice);
			options.onElementValidate(test, elm);
			return test;
		});
	},
	test : function(name, elm, useTitle, showAdvice, result) {
		var v = Validation.get(name);
		var prop = '__advice'+name.camelize();
		try {

		if (result == null){
			var result = v.test($F(elm), elm);
		}

		if(Validation.isVisible(elm) && !result){

			if(!elm[prop]){
				var advice = Validation.getAdvice(name, elm);

				if(advice == null){
					var errorMsg = useTitle ? ((elm && elm.title) ? elm.title : v.error) : v.error;
					//advice = '<span class="validation-advice" id="advice-' + name + '-' + Validation.getElmID(elm) +'" style="display:none">' + errorMsg + '</span>'

					//advice = wrap_message(errorMsg, elm.name);

//					new Insertion.After(elm, advice);

					switch (elm.type.toLowerCase()) {
						case 'checkbox':
						case 'radio':
							/*var p = elm.parentNode;
							if(p) {
								new Insertion.Bottom(p, advice);
							} else {
								new Insertion.After(elm, advice);
							}*/
							break;
						case 'text':
						case 'password':

							elm[prop] = true;
							elm.removeClassName('validation-passed');
							elm.addClassName('validation-failed');

							// Create a tooltip
							Tip.create(elm, errorMsg, 'focus', 'blur', 'mouseover');
				    		break;
						default:

							elm[prop] = true;
							elm.removeClassName('validation-passed');
							elm.addClassName('validation-failed');

							// Add the little icon thing
							new Insertion.After(elm, '<img src="images/required_field.gif" class="tooltipimg" onmouseover="Tip.show(this, \'' + errorMsg.replace('"', '\"').replace("'", "\'") + '\');" id="tooltipimg_' + this.getElmID(elm) + '" />');
					}
					//	advice = Validation.getAdvice(name, elm);
				}
				else {

					switch (elm.type.toLowerCase()) {
						case 'checkbox':
						case 'radio':
							break;
						case 'text':
						case 'password':

							elm[prop] = true;
							elm.removeClassName('validation-passed');
							elm.addClassName('validation-failed');

							this.binds[elm.name] = show_tooltip_event.bindAsEventListener(false, elm.name);
							this.binds[elm.name + '_hide'] = hide_tooltips_event.bindAsEventListener(false, false);

							if ($('tooltip_' + elm.name)){
								Event.observe($('tooltip_' + elm.name), 'mouseover', this.binds[elm.name + '_hide']);
							}

							Event.observe(elm, 'focus', this.binds[elm.name]);
							//Event.observe(elm, 'mouseover', this.binds[elm.name]);
							Event.observe(elm, 'blur', this.binds[elm.name + '_hide']);
				    		break;
						default:

							elm[prop] = true;
							elm.removeClassName('validation-passed');
							elm.addClassName('validation-failed');

							this.binds[elm.name] = show_tooltip_event.bindAsEventListener(false, elm.name);
							this.binds[elm.name + '_hide'] = hide_tooltips_event.bindAsEventListener(false, false);

							if ($('tooltipimg_' + elm.name)){
								$('tooltipimg_' + elm.name).show();
							}

							if ($('tooltip_' + elm.name)){
								Event.observe($('tooltip_' + elm.name), 'mouseover', this.binds[elm.name + '_hide']);
							}

							//Event.observe(elm, 'mouseover', this.binds[elm.name]);
							Event.observe($('tooltipimg_' + elm.name), 'mouseover', this.binds[elm.name]);
					}

				}
			//	if(typeof Effect == 'undefined') {
			//		advice.style.display = 'block';
			//	} else {
			//		new Effect.Appear(advice, {duration : 1 });
			//	}
			}

			return false;
		} else {
			//var advice = Validation.getAdvice(name, elm);
			//alert(advice);
			//if(advice != null) advice.hide();

			switch (elm.type.toLowerCase()) {
				case 'checkbox':
				case 'radio':
					break;
				case 'text':
				case 'password':
					elm[prop] = '';
					elm.removeClassName('validation-failed');
					elm.addClassName('validation-passed');

					// Remove the tooltip
				//	Tip.destroy(elm, 'focus');
		    		break;
				default:

					elm[prop] = '';
					elm.removeClassName('validation-failed');
					elm.addClassName('validation-passed');

					// Remove the warning icon
					if ($('tooltipimg_' + this.getElmID(elm))){

						// Remove the tooltip
						Tip.destroy($('tooltipimg_' + this.getElmID(elm)));

						$('tooltipimg_' + this.getElmID(elm)).remove();
					}
			}

			return true;
		}
		} catch(e) {
			throw(e)
		}
	},
	isVisible : function(elm) {
		while(elm.tagName != 'BODY') {
			if(!$(elm).visible()) return false;
			elm = elm.parentNode;
		}
		return true;
	},
	getAdvice : function(name, elm) {
		//return $('advice-' + name + '-' + Validation.getElmID(elm)) || $('advice-' + Validation.getElmID(elm));
		return $('tooltip_' + name) || $('tooltip_' + Validation.getElmID(elm));
	},
	getElmID : function(elm) {
		return elm.id ? elm.id : elm.name;
	},
	reset : function(elm) {
		elm = $(elm);
		var cn = elm.classNames();
		cn.each(function(value) {
			var prop = '__advice'+value.camelize();
			if(elm[prop]) {
				var advice = Validation.getAdvice(value, elm);
				advice.hide();
				elm[prop] = '';
			}
			elm.removeClassName('validation-failed');
			elm.removeClassName('validation-passed');
		});
	},
	add : function(className, error, test, options) {
		var nv = {};


		nv[className] = new Validator(className, error, test, options);
		Object.extend(Validation.methods, nv);
	},
	addAllThese : function(validators) {
		var nv = {};
		$A(validators).each(function(value) {
				nv[value[0]] = new Validator(value[0], value[1], value[2], (value.length > 3 ? value[3] : {}));
			});
		Object.extend(Validation.methods, nv);
	},
	get : function(name) {
		return  Validation.methods[name] ? Validation.methods[name] : Validation.methods['_LikeNoIDIEverSaw_'];
	},
	methods : {
		'_LikeNoIDIEverSaw_' : new Validator('_LikeNoIDIEverSaw_','',{})
	},

	// Functions below hopefully won't be needed with prototype 1.6
	viewportWidth: function(){
		if (Prototype.Browser.Opera) return document.body.clientWidth;
		return document.documentElement.clientWidth;
	},

	viewportHeight: function(){
		if (Prototype.Browser.Opera) return document.body.clientHeight;
		if (Prototype.Browser.WebKit) return this.innerHeight;
		return document.documentElement.clientHeight;
	},

	viewportSize : function(){
		return {'height': this.viewportHeight(), 'width': this.viewportWidth()};
	},

	getScrollLeft: function(){
		return this.pageXOffset || document.documentElement.scrollLeft;
	},

	getScrollTop: function(){
		return this.pageYOffset || document.documentElement.scrollTop;
	},

	getScrollOffsets: function(){
		return {'left': this.getScrollLeft(), 'top': this.getScrollTop()}
	}

});

Validation.add('IsEmpty', '', function(v) {
				return  ((v == null) || (v.length == 0)); // || /^\s+$/.test(v));
			});

Validation.addAllThese([
	['required', 'This is a required field.', function(v) {
				return !Validation.get('IsEmpty').test(v);
			}],
	['validate-number', 'Please enter a valid number in this field.', function(v) {
				return Validation.get('IsEmpty').test(v) || (!isNaN(v) && !/^\s+$/.test(v));
			}],
	['validate-digits', 'Please use numbers only in this field.', function(v) {
				return Validation.get('IsEmpty').test(v) ||  !/[^\d]/.test(v);
			}],
	['validate-alpha', 'Please use letters only (a-z) in this field.', function (v) {
				return Validation.get('IsEmpty').test(v) ||  /^[a-zA-Z]+$/.test(v)
			}],
	['validate-alphanum', 'Please use only letters (a-z) or numbers (0-9) only in this field.', function(v) {
				return Validation.get('IsEmpty').test(v) ||  !/\W/.test(v)
			}],
	['validate-date', 'Please enter a valid date.', function(v) {
				var test = new Date(v);
				return Validation.get('IsEmpty').test(v) || !isNaN(test);
			}],
	['validate-email', 'Please enter a valid email address.', function (v) {
				return Validation.get('IsEmpty').test(v) || /\w{1,}[@][\w\-]{1,}([.]([\w\-]{1,})){1,3}$/.test(v)
			}],
	['validate-url', 'Please enter a valid URL.', function (v) {
				return Validation.get('IsEmpty').test(v) || /^(http|https|ftp):\/\/(([A-Z0-9][A-Z0-9_-]*)(\.[A-Z0-9][A-Z0-9_-]*)+)(:(\d+))?\/?/i.test(v)
			}],
	['validate-date-au', 'Please use this date format: dd/mm/yyyy.', function(v) {
				if(Validation.get('IsEmpty').test(v)) return true;
				var regex = /^(\d{2})\/(\d{2})\/(\d{4})$/;
				if(!regex.test(v)) return false;
				var d = new Date(v.replace(regex, '$2/$1/$3'));
				return ( parseInt(RegExp.$2, 10) == (1+d.getMonth()) ) &&
							(parseInt(RegExp.$1, 10) == d.getDate()) &&
							(parseInt(RegExp.$3, 10) == d.getFullYear() );
			}],
	['validate-currency-dollar', 'Please enter a valid $ amount.', function(v) {
				// [$]1[##][,###]+[.##]
				// [$]1###+[.##]
				// [$]0.##
				// [$].##
				return Validation.get('IsEmpty').test(v) ||  /^\$?\-?([1-9]{1}[0-9]{0,2}(\,[0-9]{3})*(\.[0-9]{0,2})?|[1-9]{1}\d*(\.[0-9]{0,2})?|0(\.[0-9]{0,2})?|(\.[0-9]{1,2})?)$/.test(v)
			}],
	['validate-selection', 'Please make a selection', function(v,elm){
				return elm.options ? elm.selectedIndex > 0 : !Validation.get('IsEmpty').test(v);
			}],
	['validate-one-required', 'Please select one of the above options.', function (v,elm) {
				var p = elm.parentNode;
				var options = p.getElementsByTagName('INPUT');
				return $A(options).any(function(elm) {
					return $F(elm);
				});
			}],
	['address-not-empty', 'Please fill in either PO Box, Flat number, Building name, or Building number', function (v,elm) {
				var classes = elm.form.getElementsByClassName('address-not-empty');
				// Check if any of them are filled
				var valid = classes.any(function(elm){
					// Does it have a value?
					return $F(elm);
				});

				// Validate them all
				if (valid){

					classes.each(function(elm){
						Validation.test('address-not-empty', elm, false, true, true);
					});

					return true;
				}

				classes.each(function(elm){
					Validation.test('address-not-empty', elm, false, true, false);
				});

				return false;
			}],
	['dic-selected', 'Please make a selection', function (v,elm) {
				val = elm.options[elm.selectedIndex].value;

				if (val === '1'){
					$('yield').disabled = true;
					$('yield').addClassName('disabled');
				}
				else {
					$('yield').disabled = false;
					$('yield').removeClassName('disabled');
				}

				if (val === '2'){
					$('flatrate').disabled = true;
					$('flatrate').addClassName('disabled');
				}
				else {
					$('flatrate').disabled = false;
					$('flatrate').removeClassName('disabled');
				}

				return elm.options ? elm.selectedIndex > 0 : !Validation.get('IsEmpty').test(v);
			}]
]);