window.addEvent("domready", function(){
    var status = "";

    $$('#smart-enquiry input[type=text]', '#smart-enquiry textarea', '#smart-enquiry select').each(function(el) {
        el.addEvents({                              
            'blur'   :  function() {
                this.setStyle('background-color','');
                        
                if (this.value.clean() == "" && this.hasClass('required')) {   
                    // Required field - not filled.
                    this.highlight('#ee8282');
                    var status = "invalid";
                } else if ((this.value.clean() != "" && this.hasClass('required')) ||
                           (this.value.clean() != "" && !this.hasClass('required'))) {
                    // Check for valid email
                    if (this.hasClass('email') && isEmail(this.value.clean())) {
                        this.highlight('#87ee82');
                        var status = "valid";
                    } else if (!this.hasClass('email')){
                        this.highlight('#87ee82');
                        var status = "valid";
                    } else {
                        this.highlight('#ee8282');
                        var status = "invalid";
                    }
                }
                
                // label that will be adopting
                var adopter = this.getParent();
                
                // New status symbol element
                statusImg = new Element('img', {
                    'class': 'status ' + status,
                    'width': '16',
                    'height': '16'});
                
                // Change image source
                if (status == "invalid") {
                    statusImg.src = "img/exclamation.gif";
                    statusImg.alt = "This field is required";
                } else if (status == "valid") {
                    statusImg.src = "img/tick.gif";
                    statusImg.alt = "This field is filled";
                }
    
                // Check for existing status image
                if (adopter.getFirst($('img')).hasClass('status')) {
                    var toReplace = adopter.getFirst($('img'));
                    // Destroy the status image if this is not a required field
                    if (this.value.clean() == "" && !this.hasClass('required')) {
                        toReplace.destroy();
                    } else {
                        statusImg.replaces(toReplace);
                    }   
                } else if (this.value.clean() != "" || this.hasClass('required')) { 
                    statusImg.inject(adopter, 'top');
                }   
            }
        });
    });    
});

function isEmail(address) {
    var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
    if(reg.test(address) == false) {
        return false;
    } else {
        return true;
    }   
}