/////////////////////////////////////////////////////////////////////////
//  zxValidateForm v1.0 - 9:20 AM 30/10/2007 | Upd 03/06/2008          //
//                                                                     //
// @author: azim.zakhidov@cyberplex.com                                //
// @Form Validation for Hook                                           //
/////////////////////////////////////////////////////////////////////////

var ValidateForm;
window.addEvent('load', function(){
    ValidateForm = new zxValidateForm();
});

var zxValidateForm = new Class({
    options: {
        hookId: 'your-hook-email',
        hookPhone: 'your-hook-phone',
        buttonId: 'hook_submit',
        errorDiv: 'hook_error',
        rows:'.table-style1 tr',
        formId: 'start-hooking',
        emailClass: '.email-input',
        nameEmpty: 'You must enter First or Last Name of the Person to proceed',
        emailsEmpty: 'You must enter at least one email address to proceed',
        emailsError: 'Invalid Email Addresses',
        emailError: 'Invalid Email Address',
        emailOnePerSend: 'You must enter an email address for each person',
        yemailError: 'Your Email Address is invalid',
        yemailEmpty: 'Your Email Address is mandatory',
        yphoneError: 'Your hook&trade; Phone Number is invalid',
        yphoneEmpty: 'Your hook&trade; Phone Number is mandatory'
    },

    setOptions:function(options){
        this.options = Object.extend(this.options, options || {} );
    },

    initialize:function(options){
        this.setOptions(options);
        if ( !$(this.options.buttonId) || !$(this.options.hookId) || !$(this.options.hookPhone)) { return ''; }
        this.bind();
    },

    bind: function(){
        $(this.options.formId).addEvent('submit', function(ev){
            var valid = true;
            var e_empty = true;
            var n_empty = true;
            var bfoundEmptyEmailWhenNamePresent = false;
            this.showError('');

            $$(this.options.rows).each(function(el,i){
                if(i>3){
			  var inp = $ES('input',el);
			  var eInp = inp.getLast();
			  eInp.value=eInp.value.trim();
			  if ( (inp[0].value.trim()!='' || inp[1].value.trim()!='') && eInp.value==='')
			  {
                        bfoundEmptyEmailWhenNamePresent = true;
                    }
                    if (!this.validateEmail(eInp)){ valid = false;}
                    if (eInp.value != ''){
                        e_empty = false;
                        if ( inp.length==3 ){
                            n_empty = (n_empty && inp[0].value=='' && inp[1].value=='');
                        }
                    }
                }
            }.bind(this));

            if (!valid || e_empty || n_empty || bfoundEmptyEmailWhenNamePresent){
                var ev = new Event(ev).stop();
		    if (bfoundEmptyEmailWhenNamePresent){
		        this.showError(this.options.emailOnePerSend);
		    }else if (e_empty){
                    this.showError(this.options.emailsEmpty);
                }else if(n_empty){
                    this.showError(this.options.nameEmpty);
                }else{
                    this.showError(this.options.emailsError);
                }
            }

            if ( $(this.options.hookId) && valid){
                $(this.options.hookId).value = $(this.options.hookId).value.trim();
                if ( $(this.options.hookId).value=='' ){
                    valid=false;
                    var ev = new Event(ev).stop();
                    this.showError(this.options.yemailEmpty);
                }else if ( !this.validateEmail($(this.options.hookId)) ){
                    valid=false;
                    var ev = new Event(ev).stop();
                    this.showError(this.options.yemailError);
                }
            }

            if ( $(this.options.hookPhone) && valid){
                $(this.options.hookPhone).value = $(this.options.hookPhone).value.trim();
                if ( $(this.options.hookPhone).value=='' ){
                    valid=false;
                    var ev = new Event(ev).stop();
                    this.showError(this.options.yphoneEmpty);
                }else if ( !$(this.options.hookPhone).value.test(/^[0-9]{10}$/i) ){
                    valid=false;
                    var ev = new Event(ev).stop();
                    this.showError(this.options.yphoneError);
                }
            }
        }.bind(this));

        $$(this.options.emailClass).each(function(el){
            this.bindEmail(el);
        }.bind(this));
    },

    bindEmail: function(el){
        el.addEvent('blur',function(){
            if ( !this.validateEmail(el) ){
                this.showError(this.options.emailError);
            }else{
                this.showError('');
            }
        }.bind(this));
    },

    showError:function(error){
        $(this.options.errorDiv).setHTML(error).setStyle('display',(error=='')?'none':'block');
    },

    validateEmail:function(el){
        var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
        if (!el.value.test(reg) && el.value != '') {
            el.addClass('notValid');
            return false;
        }else{
            el.removeClass('notValid');
            return true;
        }
    }
});