﻿
quoteData = {
    name: $('input[id$=txtName]'),
    phone: $('input[id$=txtPhone]'),
    email: $('input[id$=txtEmail]'),
    company: $('input[id$=txtCompany]'),
    position: $('input[id$=txtPosition]'),
    bussiness: $('select[id$=ddlBusiness]'),
    calltime: $('select[id$=ddlCallTime]'),
    comments: $('textarea[id$=txtComments]'),
    agreement: embedded ? $('input[id$=chkAgreementEmbeded]') : $('input[id$=chkAgreement]'),
    onInit: function() {
        // window.frames["QuoteRequestIFrame"].location = "about:blank"; // for feature re-sender...
        this.displayRequestForm();
    },
    sendRequest: function(emb) {
        if (!this.checkValidFields(this.company, this.name, this.phone, this.email, this.agreement)) {
            StraussEpiSLN.AFH.QuoteRequest.SendQuoteRequest(this.name.val(), this.phone.val(), this.email.val(),
                                                                this.company.val(), this.position.val(), this.bussiness.val(),
                                                                this.calltime.val(), this.comments.val(),
                                                                true, this.onRequestComplete, this.onRequestError);
            //after the request recived..
            var x = "";

            if (emb == null) {
                x = "/OfficeCofee/Lead/QuoteSendSuccess.htm";
            }
            else {
                var x = "/OfficeCofee/Lead/QuoteSendSuccessLarge.htm";
            }
            window.frames["QuoteRequestIFrame"].location = x;
            this.displayMessageComplete();
        }
    },
    onRequestComplete: function() { },
    onRequestError: function() { },
    displayMessageComplete: function() {
        $('#headerPopup').hide();
        $('#request').hide();
        $('#message').show();
        $('#wrpAgreement').hide();
        $('#sender').hide();
        $('#closer').show();

        if (embedded) {
            $('#closer').hide();
            //$('#imageTop').hide();
            $('#senderEmbeded').hide();
            $('#wrpAgreementEmbeded').hide();
        }
    },
    checkValidFields: function() {
        var err = false;

        this.clearValidationForm();
        // move on all fields
        for (var i = 0; i < arguments.length; i++) {
            // read field
            field = arguments[i];

            //check for requieds:
            if (field.val() == "") {
                field.css("background-color", "#fbf4cc");
                $('#' + field.attr('id').substring(field.attr('id').lastIndexOf('_') + 1) + '_error').show();
                err = true;
            }

            //check for extra validation:
            if (field == this.email) {
                var emailPattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);

                if (!emailPattern.test(field.val())) {
                    field.css("background-color", "#fbf4cc");
                    $('#' + field.attr('id').substring(field.attr('id').lastIndexOf('_') + 1) + '_error').show();

                    err = true;
                }
            }

            if (field == this.agreement) {
                if (field.attr('checked') == false) {
                    $('#' + field.attr('id').substring(field.attr('id').lastIndexOf('_') + 1) + '_error').show();
                    err = true;
                }
            }
        }

        return err;
    },
    displayRequestForm: function() {
        this.clearValidationForm();

        $('#message').hide();
        $('#headerPopup').show();
        $('#contentPopup').show();
        $('#request').show();

        $('#wrpAgreement').show();
        $('#sender').show();
        $('#closer').hide();
        $('#floatFooter').show();
        $('#embededFooter').hide();
        $('#mainCloser').show();

        if (embedded) {
            $('#mainCloser').hide();
            $('#floatFooter').hide();
            $('#embededFooter').show();

            $('#headerPopup').hide();
            $('#rowCallTime').hide();
            $('#rowComments').hide();
            $('#rowLicense').hide();
        }
    },
    clearValidationForm: function() {
        $('.error').hide();

        this.name.css("background-color", "#fff");
        this.phone.css("background-color", "#fff");
        this.email.css("background-color", "#fff");
    }
};
