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 = "modules/smart-enquiry/img/exclamation.gif";
                    statusImg.alt = "This field is required";
                } else if (status == "valid") {
                    statusImg.src = "modules/smart-enquiry/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');
                }   
            }
        });
    });    

    var statusbox = $('statusbox');
    
    statusbox = new Fx.Tween(statusbox, {
        transition: 'cubic:out',
        onComplete : function() {
           $('message2').set('html', message)
           $('message2').setStyle('opacity','0').morph({'opacity': '1'});  
           
        }
    });
    
    $('smart-enquiry').addEvent('submit', function(e) {
        new Event(e).stop();
        new ajaxRequest({   
            onRequest  :  function() {
                $('submit-smart-enquiry').setStyle('display','none');
                $('ajax-load').setStyle('display','block');
                $('message2').setStyle('opacity','1').morph({'opacity': '0'});  
            },
            onSuccess  :  function(responseHTML) {    
                $('ajax-load').setStyle('display','none');
                $('submit-smart-enquiry').setStyle('display','block');
                statusbox.set('overflow','hidden');
                statusbox.set('display','block');
                statusbox.start('height','100px');
                message = responseHTML;
                
            }                
        }).send(this); 
    });

    // ------------------------------------------ Extends request class fer ajax
    ajaxRequest = new Class({
        Extends: Request,
        options: { 
            method: 'post',
            url: 'modules/smart-enquiry/ajax.php'
        }
    });    
    
});

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;
    }   
}
