/*
* 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();

Validation.prototype = {
    initialize : function(form, options){
        this.options = Object.extend({
            onSubmit : true,
            stopOnFirst : false,
            immediate : false,
            focusOnError : true,
            useTitles : false,
            killObservers : false,
            onFormValidate : function(result, form) {},
            onElementValidate : function(result, elm) {}
        }, options || {});
        this.form = $(form);

        var obj = {
          fx: function(ev) {
              if(Event.element(ev) && Event.element(ev).observe)
              Validation.validate(Event.element(ev),{useTitle : useTitles, onElementValidate : callback});
          }
        };

		if(this.options.onSubmit) {
            submitBind = this.onSubmit.bind(this);
		    Event.observe(this.form,'submit',submitBind,false);
		}

		if(this.options.killObservers){
		    var useTitles = this.options.useTitles;
			var callback = this.options.onElementValidate;

			Form.getElements(this.form).each(function(input) {
			    element = $(input);
			    element.observe = false;
			});
		    return;
		}

        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}); });
            });
        }
    },
    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) {
            try{
                Form.getElements(this.form).findAll(function(elm){return $(elm).hasClassName('validation-failed')}).first().focus()
            }
            catch(e){

            }
        }
        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 || {});
        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;
        });
    },
    insertAdvice : function(elm, advice){
        var container = elm.up('.field-row');
        if(container){
            new Insertion.After(container, advice);
        }
        else {
            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);
            }
        }
    },
    showAdvice : function(elm, advice, adviceName){
        if(!elm.advices){
            elm.advices = new Hash();
        }
        else{
            elm.advices.each(function(pair){
                this.hideAdvice(elm, pair.value);
            }.bind(this));
        }
        elm.advices[adviceName] = advice;
        if(typeof Effect == 'undefined') {
            advice.style.display = 'block';
        } else {
            new Effect.Appear(advice, {duration : 1 });
        }
    },
    hideAdvice : function(elm, advice){
        if(advice != null) advice.hide();
    },
    test : function(name, elm, useTitle) {
        var container = elm.up('.payment-methods-box-hidden');
        if(container){
            return true;
        }

        var v = Validation.get(name);
        var prop = '__advice'+name.camelize();
        try {
        if(Validation.isVisible(elm) && !v.test($F(elm), elm)) {
            //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 = '<div class="validation-advice" id="advice-' + name + '-' + Validation.getElmID(elm) +'" style="display:none">' + errorMsg + '</div>'
                    this.insertAdvice(elm, advice);
                    advice = Validation.getAdvice(name, elm);
                }
                this.showAdvice(elm, advice, name);
            //}
            elm[prop] = true;
            elm.removeClassName('validation-passed');
            elm.addClassName('validation-failed');
            return false;
        } else {
            var advice = Validation.getAdvice(name, elm);
            this.hideAdvice(elm, advice);
            elm[prop] = '';
            elm.removeClassName('validation-failed');
            elm.addClassName('validation-passed');
            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));
    },
    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_','',{})
    }
});

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

Validation.addAllThese([
    ['required-entry', 'This is a required field.', function(v, elm) {
                if(elm.getAttribute('length') && elm.getAttribute('error')){
                    var pattern = /\s+/g;
                    var realstring = String();
                    realstring = v.replace(pattern,'');
                    reallength = realstring.length;
                    if( reallength < elm.getAttribute('length') ){
                        this.error = elm.getAttribute('error');
                        return false;
                    }
                }
                this.error = 'This is a required field.';
                return (!Validation.get('IsEmpty').test(v));
            }],
    ['must-smaller-than', 'This value must be a smaller than you specified.', function(v, elm) {
                if(v > elm.title) {
                    this.error = 'You cannot return more items than you ordered. Please correct the quantity of items to be returned.';
                    return false;
                }
                return true;
            }],
    ['must-bigger-than', 'This value must be a bigger than you specified.', function(v, elm) {
                if(v < elm.title) {
                    this.error = 'This value must be not smaller than '+elm.title;
                    return false;
                }
                return true;
            }],
    ['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. 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-street', 'Please use only letters (a-z) or numbers (0-9) or spaces and # only in this field.', function(v) {
				return Validation.get('IsEmpty').test(v) ||  /^[ \w]{1,}([A-Za-z]\.)?([ \w]*\#\d+)?(\r\n| )[ \w]{1,}/.test(v)
                //return Validation.get('IsEmpty').test(v) ||  /^[a-zA-Z0-9]{3,}?([ \w]*\#\d+)$/.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. For example fred@domain.com .', function (v) {
                return Validation.get('IsEmpty').test(v) || /\w{1,}[@][\w\-]{1,}([.]([\w\-]{1,})){1,3}$/.test(v)
            }],
    ['validate-password', 'Please enter 6 or more characters.', function(v) {
                return !(v.length < 6);
            }],
    ['validate-cpassword', 'Please make sure your passwords match.', function(v) {
                var pass = ((document.getElementById("password")!=undefined)?document.getElementById("password"):document.getElementById("password_new"));
                return (pass.value == 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-clean-url', 'Please enter a valid URL. For example http://www.example.com or www.example.com', function (v) {
                return Validation.get('IsEmpty').test(v) || /^(http|https|ftp):\/\/(([A-Z0-9][A-Z0-9_-]*)(\.[A-Z0-9][A-Z0-9_-]*)+.(com|org|net|dk|at|us|tv|info|uk|co.uk|biz|se)$)(:(\d+))?\/?/i.test(v) || /^(www)((\.[A-Z0-9][A-Z0-9_-]*)+.(com|org|net|dk|at|us|tv|info|uk|co.uk|biz|se)$)(:(\d+))?\/?/i.test(v)
            }],
    ['validate-ssn', 'Please enter a valid social security number. For example 123-45-6789.', function(v) {
            return Validation.get('IsEmpty').test(v) || /^\d{3}-?\d{2}-?\d{4}$/.test(v);
            }],
    ['validate-zip', 'Please enter a valid zip code. For example 90602 or 90602-1234.', function(v) {
            return Validation.get('IsEmpty').test(v) || /(^\d{5}$)|(^\d{5}-\d{4}$)/.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-expDate', 'Please select a valid expiration date. For example July 2008.', function(v) {
                //check date for future date
                today = new Date();
                expDate = new Date();
                expDate.setFullYear('20'+$('payflowpro_cc_expires_year').value, $('payflowpro_cc_expires_month').value, expDate.getDate());
                if (expDate <= today)
                {
                    $('payflowpro_cc_expires_month').addClassName('validation-failed');
                    $('payflowpro_cc_expires_year').addClassName('validation-failed');
                    return false;
                }
                else
                {
                    $('payflowpro_cc_expires_month').removeClassName('validation-failed');
                    $('payflowpro_cc_expires_year').removeClassName('validation-failed');
                    return true;
                }

            }],
    ['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-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);
                });
    }],
    ['validate-phoneStrict', 'Please enter a valid phone number. For example (123) 456-7890.', function(v, elm) {
            //return true; /// !!!!!!!!!!!!!!!! BYPASS FOR TEMA
            var elm = $(elm);
            if ( elm.hasClassName('validate-skip') ) return true;
            var empty = Validation.get('IsEmpty').test(v);
            var right = /^(\()?\d{3}(\))?(-|\s)?\d{3}(-|\s)\d{4}(-|\s)?(\w{0,4})?$/.test(v);
            /*
            var chunk1 = $('telephone_p1');
            var chunk2 = $('telephone_p2');
            var chunk3 = $('telephone_p3');

            if( chunk1.value.match(/^(\d{3})$/) ) chunk1.removeClassName('validation-failed');
            if( chunk2.value.match(/^(\d{3})$/) ) chunk2.removeClassName('validation-failed');
            if( chunk3.value.match(/^(\d{4})$/) ) chunk3.removeClassName('validation-failed');
            */
            if (!right)
            {
                /*
                if( !$('telephone_p1').value || !$('telephone_p1').value.match(/^(\d{3})$/) )$('telephone_p1').addClassName('validation-failed');
                if( !$('telephone_p2').value || !$('telephone_p2').value.match(/^(\d{3})$/) )$('telephone_p2').addClassName('validation-failed');
                if( !$('telephone_p3').value || !$('telephone_p3').value.match(/^(\d{4})$/) )$('telephone_p3').addClassName('validation-failed');
                */
                return false;
            }
            return true;
    }],
    ['validate-faxStrict', 'Please enter a valid fax number. For example (123) 456-7890.', function(v, elm) {
            //return true; /// !!!!!!!!!!!!!!!! BYPASS FOR TEMA
            var elm = $(elm);
            if ( elm.hasClassName('validate-skip') ) return true;
            if (Validation.get('IsEmpty').test(v)) return true;
            var right = /^(\()?\d{3}(\))?(-|\s)?\d{3}(-|\s)\d{4}(-|\s)?(\w{0,4})?$/.test(v);
            /*
            var chunk1 = $('fax_p1');
            var chunk2 = $('fax_p2');
            var chunk3 = $('fax_p3');

            if( chunk1.value.match(/^(\d{3})$/) ) chunk1.removeClassName('validation-failed');
            if( chunk2.value.match(/^(\d{3})$/) ) chunk2.removeClassName('validation-failed');
            if( chunk3.value.match(/^(\d{4})$/) ) chunk3.removeClassName('validation-failed');
            */
            if (!right)
            {
                /*
                if( !$('fax_p1').value || !$('fax_p1').value.match(/^(\d{3})$/) )$('fax_p1').addClassName('validation-failed');
                if( !$('fax_p2').value || !$('fax_p2').value.match(/^(\d{3})$/) )$('fax_p2').addClassName('validation-failed');
                if( !$('fax_p3').value || !$('fax_p3').value.match(/^(\d{4})$/) )$('fax_p3').addClassName('validation-failed');
                */
                return false;
            }
            return true;
    }]
]);