﻿
//-----------------------------------------------------------------------------
// <copyright file="getQuote.js" company="GenuineInteractive">
//     Copyright (c) SSI. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------------
//  Date:       12/16/2010
// Author:      J.He
//-----------------------------------------------------------------------------


var getQuoteSubmit = function () {
    // object to store all the question and answers that user has passed
    var getAQuoteInfo = {};

    function populateGetAQuoteInfo() {
        getAQuoteInfo["FirstName"] = $('#FieldFirstName').val();
        getAQuoteInfo["LastName"] = $('#FieldLastName').val();
        getAQuoteInfo["Email"] = $('#FieldEmail').val();
        getAQuoteInfo["CompanyName"] = $('#FieldCompany').val();
        getAQuoteInfo["JobTitle"] = $('#FieldJobTitle').val();
        getAQuoteInfo["Address1"] = $('#FieldAddress1').val();
        getAQuoteInfo["City"] = $('#FieldCity').val();
        getAQuoteInfo["State"] = $('#FieldState').val();
        getAQuoteInfo["Zip"] = $('#FieldZip').val();
        getAQuoteInfo["Country"] = $('#FieldCountrySelect').val();
        getAQuoteInfo["SampleType"] = $('#FieldSampleType').val();
        getAQuoteInfo["ProjectDescription"] = $('#FieldProjectDescription').val();
        getAQuoteInfo["EstimateMonth"] = $('#FieldMonthSelect').val();
        getAQuoteInfo["EstimateDay"] = $('#FieldDaySelect').val();
        getAQuoteInfo["EstimateYear"] = $('#FieldYearSelect').val();
    }

    //function declarations
    var GetAQuote = function () {

        populateGetAQuoteInfo();
        var json = JSON.stringify(getAQuoteInfo);
        $.ajaxDotNet('/app_services/corporate/public/FormServices.asmx/GetAQuote', {
            verb: 'POST',
            data: {
                'json': json,
                'guid': currentItem
            },
            success: function (obj) {
                var returnedobj = JSON.parse(obj.d);
                if (returnedobj.Success == true) {
                    alert(returnedobj.Json);
                    $('#GetQuoteDiv').append('<p>Email Sent</p>');
                }
                else {
                    alert(returnedobj.ErrorMessage);
                }
            },
            error: function (er) {

            }
        });

    };




    /* Validation Function*/
    var Validation = function () {
        var returnvalue = true;
        $('#GetQuoteDiv input, select').each(function () {

            //cache of our current input element and its label
            var that = $(this);
            var thatLabel = $(that).parent().prev('label');

            email = {
                pattern: /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/,
                address: $('#FieldEmail').val(),
                id: $('#FieldEmail'),
                label: $('#EmailLabel')
            };
            // test email
            if (email.pattern.test(email.address) == false) {
                $(email.id).addClass('error');
                $(email.label).addClass('error');
                returnvalue = false;
            }
            // if value is blank, and required alert us and don't do anymore work
            if ($(that).val() == '' && $(that).hasClass('required')) {
                $(that).addClass('error');
                // treatment for select boxes
                if ($(that).is('select')) {
                    $(that).prev('label').addClass('error');
                }
                // treatment for regular input boxes
                $(thatLabel).addClass('error');
                // return value
                returnvalue = false;
            }

        }); //end each

        // return true or false to continue submit
        return returnvalue;
    };

    //bindings
    $(function () {
        $('#corp-submit-getaquote').click(function (e) {
            e.preventDefault();
            if (Validation() == true) {
                GetAQuote();
            } else if (Validation() == false) {
                $('input, select').focusin(function () {
                    $(this).removeClass('error');
                    $(this).parent().prev('label').removeClass('error');
                    if ($(this).is('select')) {
                        $(this).prev('label').removeClass('error');
                    }
                });
            }
        });
    });
} ();
