/*
 * Really easy field validation with Prototype
 * http://tetlaw.id.au/view/blog/really-easy-field-validation-with-prototype
 * Andrew Tetlaw
 * Version 1.5.3 (2006-07-15)
 * 
 * Copyright (c) 2006 Andrew Tetlaw
 * http://www.opensource.org/licenses/mit-license.php
 */
Validator = Class.create();

Validator.prototype = {
	initialize : function(className, error, test, options) {
		this.options = Object.extend({}, options || {});
		this._test = test ? test : function(v,elm){ return true };
		this.error = error ? error : 'Validation failed.';
		this.className = className;
	},
	test : function(v, elm) {
		return this._test(v,elm);
	}
}

var Validation = Class.create();

Validation.prototype = {
	initialize : function(form, options){
		this.options = Object.extend({
			onSubmit : true,
			stopOnFirst : false,
			immediate : false,
			focusOnError : true,
			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) {
			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}); });
					//add on click checking for radio and checkboxes because they are grouped items and onblur isn't good enough 
					if(input.className=='required-checkbox'){
						Event.observe(input, 'click', function(ev) { Validation.validate(Event.element(ev),{useTitle : useTitles, onElementValidate : callback}); });
					}
			});
		}
	},
	onSubmit :  function(ev){
		if(!this.validate()) Event.stop(ev);
	},
	validate : function() {
		var result = false;
		var useTitles = this.options.useTitles;
		var callback = this.options.onElementValidate;
		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,{useTitle : useTitles, onElementValidate : callback}); }).all();
		}
		if(!result && this.options.focusOnError) {
			Form.getElements(this.form).findAll(function(elm){return $(elm).hasClassName('validation-failed')}).first().focus()
		}
		this.options.onFormValidate(result, this.form);
		return result;
	},
	reset : function() {
		Form.getElements(this.form).each(Validation.reset);
	}
}

Object.extend(Validation, {
	getLastFocus : function(){
	alert("funct "+this.lastFocus);
	return this.lastFocus;
	},
	validate : function(elm, options){
		options = Object.extend({
			useTitle : false,
			onElementValidate : function(result, elm) {}
		}, options || {});
		elm = $(elm);
		var cn = elm.classNames();
		return result = cn.all(function(value) {
			var test = Validation.test(value,elm,options.useTitle);
			options.onElementValidate(test, elm);
			return test;
		});
	},
	test : function(name, elm, useTitle) {
		var v = Validation.get(name);
		var prop = '__advice'+name.camelize();
		if(Validation.isVisible(elm) && !v.test($F(elm), elm)) {
			if(!elm[prop]) {
				var advice = Validation.getAdvice(name, elm);
				if(typeof advice == 'undefined') {
					var errorMsg = useTitle ? ((elm && elm.title) ? elm.title : v.error) : v.error;
					advice = '<div class="validation-advice" id="advice-' + name + '-' + Validation.getElmID(elm) +'" style="display:none">' + errorMsg + '</div>'
					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;
						default:
							new Insertion.After(elm, advice);
				    }
					advice = $('advice-' + name + '-' + Validation.getElmID(elm));
				}
				if(typeof Effect == 'undefined') {
					advice.style.display = 'block';
				} else {
					new Effect.Appear(advice, {duration : 1 });
				}
			}
			elm[prop] = true;
			elm.removeClassName('validation-passed');
			elm.addClassName('validation-failed');
			return false;
		} else {
			var advice = Validation.getAdvice(name, elm);
			if(typeof advice != 'undefined') advice.hide();
			elm[prop] = '';
			elm.removeClassName('validation-failed');
			elm.addClassName('validation-passed');
			return true;
		}
	},
	isVisible : function(elm) {
		while(elm.tagName != 'BODY') {
			if(!$(elm).visible()) return false;
			elm = elm.parentNode;
		}
		return true;
	},
	getAdvice : function(name, elm) {
		return Try.these(
			function(){ return $('advice-' + name + '-' + Validation.getElmID(elm)) },
			function(){ return $('advice-' + 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] : new Validator();
	},
	methods : {}
});

Validation.add('IsEmpty', '', function(v) {
				return  ((v == null) || (v.length == 0)); // || /^\s+$/.test(v));
			});

Validation.addAllThese([
	['required', 'Required', function(v) {
				return !Validation.get('IsEmpty').test(v);
			}],
	['validate-number', 'Invalid', function(v) {
				return Validation.get('IsEmpty').test(v) || (!isNaN(v) && !/^\s+$/.test(v));
			}],
	['validate-digits', 'Please use numbers only in this field. please avoid spaces or other characters such as dots or commas.', 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. No spaces or other characters are allowed.', function(v) {
				return Validation.get('IsEmpty').test(v) ||  !/\W/.test(v)
			}],
	['validate-date', 'Invalid', function(v) {
				//too generic
				var test = new Date(v);
				return Validation.get('IsEmpty').test(v) || !isNaN(test);
			}],
	['validate-email', 'Invalid Email Address Format. ', 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. For example 17/03/2006 for the 17th of March, 2006.', 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. For example $100.00 .', 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-one-required', 'Choose one', function (v,elm) {
				var p = elm.parentNode;
				var options = p.getElementsByTagName('INPUT');
				return $A(options).any(function(elm) {
					return $F(elm);
				});
			}]
]);

//customized validation Nathan Morris 9/12/2006
Validation.addAllThese([
	['required-radio', 'Choose One', function (v,elm) { 
	  var p = elm.parentNode;
	  var elmId = Validation.getElmID(p);  
      var fields = p.getElementsByTagName('INPUT');
	  //one of these could be another type of input, but it doesn't really matter
      for (i=0;i< fields.length;i++) { 
        if (fields[i].checked) { 
		 	//change parent class
			//p.className = 'required-radio-'+elmId+'-passed';
			p.className = 'validation-passed';
			// call the validation for the other field if there is one
			var otherField = document.getElementById(elmId+'_other');
			if(otherField != null && otherField != 'undefined'){
				Validation.validate(otherField);
			}
            return(true) 
        } 
      } 
	  //change parent class
	  //p.className = 'required-radio-'+elmId+'-error';
	  p.className = 'validation-failed';
      return false; 
		}],
	['required-other', 'Required', function (v,elm) { 
	  var p = elm.parentNode;
	  var elmId = Validation.getElmID(p);  
      var fields = p.getElementsByTagName('INPUT');
      for (i=0;i< fields.length;i++) { 
	  	//there MUST be a checked input field with the value of other for this validation to work
        if (fields[i].checked && fields[i].value == "other" ) { 
			//is this field empty then
			 if(Validation.get('IsEmpty').test(v)){
			 	return(false);
			 }
            return(true); 
        } 
      } 
      return(true); 
		}],
	['required-checkbox', 'Choose', function (v,elm) { 
	//event check if previously focused element? was not this element ID then do not check yet
	  var p = elm.parentNode;
	  var elmId = Validation.getElmID(p);  
      var fields = p.getElementsByTagName('INPUT');
	  var requiredStatement = document.getElementById(elmId+'-required').value;
	  var checkedCount = 0;
	  //one of these could be another type of input, but it doesn't really matter
      for (i=0;i< fields.length;i++) { 
		var currElm = fields[i]
        if (fields[i].checked) { 
			checkedCount++;
        } 
		//console.debug(elmId+":"+currElm.value+":"+currElm.checked);
      } 
		// call the validation for the "other" field if there is one
		//there MUST be a checked input field with the value of other for this validation to work
		var otherField = document.getElementById(elmId+'_other');
		if(otherField != null && otherField != 'undefined'){
			Validation.validate(otherField);
		}
	  if(eval("checkedCount "+requiredStatement) ){
	  	p.className = 'validation-passed';
	  	return(true);
	  }
	  p.className = 'validation-failed';
      return false; 
		}],
	['required-select', 'Required', function(v,elm) {
		var success =  !( (elm.selectedIndex == "0") || (elm.selectedIndex == null) || (elm.selectedIndex == "")   );
		var p = elm.parentNode;
		if(success){
			p.className = 'validation-passed';
		}else{
			p.className = 'validation-failed';
		}
		return success;
		}],
	['required-multiselect', 'Required', function(v,elm) {
		//a separate multiselect is required because the validation logic is in direct conflict with that of the regular select.
		var success =  !Validation.get('IsEmpty').test(v);
		var p = elm.parentNode;
		if(success){
			p.className = 'validation-passed';
		}else{
			p.className = 'validation-failed';
		}
		return success;
		}],
	['validate-phone', 'Please use this phone format: 111-111-1111', function(v) {
		if(Validation.get('IsEmpty').test(v)) return(true);
		var stripped = v.replace(/[\(\)\.\-\ ]/g, '');
		if (isNaN(parseInt(stripped))) {
   			return(false);
		}
		if (!(stripped.length == 10)) {
			return(false);
		}
		return(true);
		}],
	['validate-dependent', 'Required', function(v,elm) {
		var elmId = Validation.getElmID(elm); 
		var dependsOnFieldId = document.getElementById(elmId+'-dependent').value;
		var dependsOnFieldValue = null;
		if(!(Validation.get('IsEmpty').test(dependsOnFieldId)) ){
			dependsOnFieldValue = document.getElementById(dependsOnFieldId).value;
		}
		if(  Validation.get('IsEmpty').test(v) &&  !Validation.get('IsEmpty').test(dependsOnFieldValue) ){
			return(false);
		}
		return(true);
		}]
]);
