/**
 *  Document   : dtmoving
 *  Created on : 19-Mar-2010, 16:01:21
 *  Author     : Michal Wrzesniewski <mw@faktumpro.net>
 */

Control.DatePicker.Locale['en_GB'] = {
    dateTimeFormat: 'dd-MM-yyyy HH:mm',
    dateFormat: 'dd-MM-yyyy',
    firstWeekDay: 1,
    weekend: [0,6],
    timeFormat: 'HH:mm',
    language: 'en'
};
function createPickers() {
    $(document.body).select('input.datepicker').each( function(e) {
        new Control.DatePicker(e, {
            locale : 'en_GB',
            'icon': '/images/calendar.png'
        });
    } );
}
Event.observe(window, 'load', createPickers);

var DTmoving ={

    requiredFieldsAlertMsg:'Please fill all required fields',

    ValidateEmail:function(email){
        return (/^([\w]+)(.[\w]+)*@([\w-]+\.){1,5}([A-Za-z]){2,4}$/i.test(email));
    },

    ValidateForm:function(){
        var isFormValid=true;
        var requiredFields = $$('.required');
        requiredFields.each(function(field){
            if(field.getValue()==""){
                if(!$(field.id+"-invalid")){
                    
                }
                $(field.id+"-invalid").setStyle({
                    display:'inline'
                });
                isFormValid=false
            }else{
                $(field.id+"-invalid").setStyle({
                    display:'none'
                });
            }
            if(field.hasClassName('validate-email')){
                if(!this.ValidateEmail(field.getValue())){
                    $(field.id+"-invalid").setStyle({
                        display:'inline'
                    });
                    isFormValid=false
                }else{
                    $(field.id+"-invalid").setStyle({
                        display:'none'
                    });
                }
            }
        }.bind(this))
        return isFormValid;
    },
    
    SubmitRequestSurveyForm:function(){


        if(!this.ValidateForm()){
            this.onFormInvalid();
            return;
        }
         $$('#wideCol h1').invoke("fade");
         $$('#wideCol p').invoke("fade");
       
        Effect.ScrollTo('wrapper');
        var cont = $('requestsurvey_div');
        var form = $('requestsurvey_form');
        var loading = new Element('DIV',{
            id:'loading',
            style:'position:relative;top:50px;'
        });
        var loading_html='Sending request<br/><img src="../images/loadinfo.gif" />';
        loading.update(loading_html)

        new Effect.Opacity(form, {
            from: 1,
            to: 0,
            afterFinish:function(){
                cont.insert({
                    top:loading
                });
                form.request({
                    onComplete:function(response){
                        var r =response.responseText.evalJSON();
                        this.restoreForm(form);
                    }.bind(this)
                })
            }.bind(this)
        });
        
    },
    SubmitCustomerSatisfactionForm:function(){

        if(!this.ValidateForm()){
            this.onFormInvalid();
            return;
        }
         $$('#wideCol h1').invoke("fade");
         $$('#wideCol p').invoke("fade");

        Effect.ScrollTo('wrapper');
        var cont = $('customer_satisfaction_div');
        var form = $('customer_satisfaction_form');
        var loading = new Element('DIV',{
            id:'loading',
            style:'position:relative;top:50px;'
        });
        var loading_html='Sending request<br/><img src="../images/loadinfo.gif" />';
        loading.update(loading_html)

        new Effect.Opacity(form, {
            from: 1,
            to: 0,
            afterFinish:function(){
                cont.insert({
                    top:loading
                });
                form.request({
                    onComplete:function(response){
                        var r =response.responseText.evalJSON();

                        this.restoreForm(form);
                    }.bind(this)
                })
            }.bind(this)
        });

    },
    SubmitQuoteForm:function(){
        if(!this.ValidateForm()){
            this.onFormInvalid();
            return;
        }
        $$('#wideCol h1').invoke("fade");
        $$('#wideCol p').invoke("fade");
        Effect.ScrollTo('wrapper');
        
        var cont = $('quote_div');
        var form = $('quote_form');
        var loading = new Element('DIV',{
            id:'loading',
            style:'position:relative;top:50px;'
        });
        var loading_html='Sending request<br/><img src="../images/loadinfo.gif" />';
        loading.update(loading_html)

        new Effect.Opacity(form, {
            from: 1,
            to: 0,
            afterFinish:function(){
                cont.insert({
                    top:loading
                });
                form.request({
                    onComplete:function(response){
                        var r =response.responseText.evalJSON();
                        this.restoreForm(form);
                    }.bind(this)
                })
            }.bind(this)
        });
    },
    SubmitRFPForm:function(){
        if(!this.ValidateForm()){
            this.onFormInvalid();
            return;
        }
        $$('#wideCol h1').invoke("fade");
        $$('#wideCol p').invoke("fade");
        Effect.ScrollTo('wrapper');

        var cont = $('rfp_div');
        var form = $('rfp_form');
        var loading = new Element('DIV',{
            id:'loading',
            style:'position:relative;top:50px;'
        });
        var loading_html='Sending request<br/><img src="../images/loadinfo.gif" />';
        loading.update(loading_html)
        new Effect.Opacity(form, {
            from: 1,
            to: 0,
            afterFinish:function(){
                cont.insert({
                    top:loading
                });
                form.request({
                    onComplete:function(response){
                        var r =response.responseText.evalJSON();
                        this.restoreForm(form);
                    }.bind(this)
                })
            }.bind(this)
        });
    },
    SubmitEnquiryForm:function(){

        if(!this.ValidateForm()){
            this.onFormInvalid();
            return;
        }
        $$('#wideCol p').invoke("fade");
        $$('#wideCol h1').invoke("fade");
        Effect.ScrollTo('wrapper');

        var cont = $('corporateenquiry_div');
        var form = $('corporateenquiry_form');
        var loading = new Element('DIV',{
            id:'loading',
            style:'position:relative;top:50px;'
        });
        var loading_html='Sending enquiry <br/><img src="../images/loadinfo.gif" />';
        loading.update(loading_html)

        new Effect.Opacity(form, {
            from: 1,
            to: 0,
            afterFinish:function(){
                cont.insert({
                    top:loading
                });
                form.request({
                    onComplete:function(response){
                        var r =response.responseText.evalJSON();
                        this.restoreForm(form);
                    }.bind(this)
                })
            }.bind(this)
        });
    },


    onFormInvalid:function(){
       

        var validationFields = $$('.validation')

        var firstInvalidField;
        validationFields.each(function(field){
            
            if(field.visible()){
                firstInvalidField = field;
                throw $break;            
            }
        });

        var inputField = (firstInvalidField.id).replace('-invalid','');

        Effect.ScrollTo(inputField,{
            offset:-20,
            axis:'x'
        });
        $(inputField).focus();
        alert(this.requiredFieldsAlertMsg)
    },

    SubmitMobilityManagementForm:function(){

        var cont = $('mobilitymanagement_div');
        var form = $('mobilitymanagement_form');

        if(!this.ValidateForm()){
            this.onFormInvalid();
            return;
        }
        $$('#wideCol h1').invoke("fade");
        $$('#wideCol p').invoke("fade");

        Effect.ScrollTo('wrapper', 1000, {
            axis:'x'
        });

        var loading = new Element('DIV',{
            id:'loading',
            style:'position:relative;top:50px;'
        });
        var loading_html='Sending request<br/><img src="../images/loadinfo.gif" />';
        loading.update(loading_html)

        new Effect.Opacity(form, {
            from: 1,
            to: 0,
            afterFinish:function(){
                cont.insert({
                    top:loading
                });
                form.request({
                    onComplete:function(response){
                        var r =response.responseText.evalJSON();
                        this.restoreForm(form);
                    }.bind(this)
                })
            }.bind(this)
        });
    },

    restoreForm:function(form){
//        $$('validation').each(function(el){
//            el.setStyle({
//                display:'none'
//            })
//        });
        var confHTML = "";

        if(form.id == 'customer_satisfaction_form') {
           var confHTML ="<h1>Thank You</h1><p>Thank you for your feedback. Your comments are critical to ensure we continually achieve the very best customer service.";
        } else {
            confHTML ="<h1>Thank You</h1><p>Thank you for your request. We will be in touch very soon with further information.</p>Kind regards,<br/>The DT Moving Team";
        }

        var confirmSentCont = '<div id="'+form.id+'_confirmation">'+confHTML+'</div>';
        form.up().insert({
            top:confirmSentCont
        });

        if($('loading')){
            $('loading').remove();
        }
    //        new Effect.Opacity(form, {
    //            from: 0,
    //            to: 1
    //        });
    },
    removeEmptyText:function(selectElement){
        var options = selectElement.options
        var emptyTextIndex=null;

        for (var i = 0; i < options.length; i++) {
            if(options[i].text=="Please select"){
                selectElement.remove(i);
                selectElement.addEventListener('blur', this.addEmptyText,[selectElement])
            }
        }
    },
    addEmptyText:function(selectElement){
        if(!selectElement.getValue()){
            var emptyOption = new Option("Please select",null)

        }
    },
    showHideQuoteDate:function() {
        if($("quote_survey").value=="Yes") {
            $("quote_survey_date").addClassName("required");
            $($("quote_survey_date").up("tr")).show();
            $("quote_survey_date").up("tr").next().show();
        }
        if(($("quote_survey").value=="Not right now") || ($("quote_survey").value=="")) {
            $("quote_survey_date").removeClassName("required");
            $("quote_survey_date").up("tr").hide();
            $("quote_survey_date").up("tr").next().hide();
        }
    }
}


