/*
    validatecontact.js
    validation classes for various contact forms on the site
    @author Andrew Bredow <bredow@gmail.com>
*/

/*
    This base class just needs to be extended for each form. only the _validateFields
    method should need to be implemented in the child class
*/
var ValidateBase = Class.create({
    
    hasErrors : false,
    
    initialize : function(formElement) {
        formElement.observe('submit', this.validateForm.bindAsEventListener(this));
    },
    
    validateForm : function(e){
        $$('.form_error_message').invoke('remove');
        this.hasErrors = false;
        
        this._validateFields();
        
        if (this.hasErrors === true) {
            Event.stop(e);
        }
        
    },
    
    _validateFields : function(){}, 
    
    isPhoneNumber : function(phoneNumber) {
        var regex = /^[\D]{0,1}[\d]{3}[\D]{0,2}[\d]{3}[\D]{0,1}[\d]{2}[\D]{0,1}[\d]{2}$/;
        return phoneNumber.match(regex);
    },
    
    isEmailAddress : function(emailAddress) {
        var regex = /^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/;
        return emailAddress.match(regex);
    },
    
    isGoodAddress : function(emailAddress) {
        var regex = /@mailinator|@mytrashmail|@mailexpire|@temporaryinbox|@maileater|@jetable|@spambox|@guerillamail|@spamhole|@fificorp|@bsnow|@tempomail@pookmail|@spamfree24|@greensloth|@dodgeit|@bugmenot|@dontreg|@tempemail.net|@ipsite.org|@mytempemail.com|temporary|spam|junk|unknown/;
        return ! emailAddress.match(regex);
    },
    
    isZipCode : function(zipCode) {
        var regex = /^[0-9]{5}$/;
        return zipCode.match(regex);
    },
    
    isEmpty : function(testValue) {
        return testValue.empty();
    },
    
    addError : function(el, errorMessage) {
        this.hasErrors = true;
        var errorEl = new Element('div').update(errorMessage);
        errorEl.addClassName('form_error_message');
        el.insert({after : errorEl});
    }
    
});

var ValidateHelixThree = Class.create(ValidateBase, {
	
	_validateFields : function() {
		
		if (this.isEmpty($F('first_name'))) {
            this.addError($('first_name'), 'First name is required');
        }
        
        if (this.isEmpty($F('last_name'))) {
            this.addError($('last_name'), 'Last name is required');
        }
        
        if (this.isEmpty($F('phone_work'))) {
            this.addError($('phone_work'), 'Phone number is required');
        }
        else if (!this.isPhoneNumber($F('phone_work'))) {
            this.addError($('phone_work'), 'Invalid phone number');
        }
        
        if (this.isEmpty($F('webtolead_email1'))) {
            this.addError($('webtolead_email1'), 'Email address is required');
        }
        else if (!this.isEmailAddress($F('webtolead_email1'))) {
            this.addError($('webtolead_email1'), 'Invalid email address');
        }
        else if (!this.isGoodAddress($F('webtolead_email1'))) {
            this.addError($('webtolead_email1'), 'Invalid email address');
        }
		
	}
});

var ValidateContact = Class.create(ValidateBase, {
    
    _validateFields : function() {
        
        if (this.isEmpty($F('first_name'))) {
            this.addError($('first_name'), 'First name is required');
        }
        
        if (this.isEmpty($F('last_name'))) {
            this.addError($('last_name'), 'Last name is required');
        }
        
        if (this.isEmpty($F('phone_work'))) {
            this.addError($('phone_work'), 'Phone number is required');
        }
        else if (!this.isPhoneNumber($F('phone_work'))) {
            this.addError($('phone_work'), 'Invalid phone number');
        }
        
        if (this.isEmpty($F('webtolead_email1'))) {
            this.addError($('webtolead_email1'), 'Email address is required');
        }
        else if (!this.isEmailAddress($F('webtolead_email1'))) {
            this.addError($('webtolead_email1'), 'Invalid email address');
        }
        else if (!this.isGoodAddress($F('webtolead_email1'))) {
            this.addError($('webtolead_email1'), 'Invalid email address');
        }
        
        if (this.isEmpty($F('product_c'))) {
            this.addError($('product_c'), 'Product of interest required');
        }
        
        if (!this.isEmpty($F('primary_address_postalcode'))) {
            if (!isZipCode($F('primary_address_postalcode'))) {
                this.addError($('primary_address_postalcode'), 'Invalid Zip Code');
            }
        }
        
    }
});

Event.observe(document, 'dom:loaded', function() {
    if ($('WebToLeadForm')) {
        new ValidateContact($('WebToLeadForm'));
    }
    if ($('Helix3DownloadForm')) {
        new ValidateHelixThree($('Helix3DownloadForm'));
    }
});

