﻿var GenericForm = function () {
    var genericFormInfo = {};

    function populateFormInfo() {

        var holder = "";
        //$('#GetQuoteDiv input[type=checkBox][checked]').each(function () {
        $('input[type=checkBox]').each(function () {

            if ($(this).attr('checked')) {
                holder = holder + $(this).val() + "|";
            }
        });
        genericFormInfo["CheckBoxes"] = holder;
        genericFormInfo["FirstName"] = $('#FieldFirstName').val();
        genericFormInfo["LastName"] = $('#FieldLastName').val();
        genericFormInfo["Title"] = $('#FieldTitle').val();
        genericFormInfo["CompanyName"] = $('#FieldCompany').val();
        genericFormInfo["Address"] = $('#FieldAddress').val();
        genericFormInfo["City"] = $('#FieldCity').val();
        genericFormInfo["State"] = $('#FieldStateSelect').val();
        genericFormInfo["Zip"] = $('#FieldZip').val();
        genericFormInfo["Country"] = $('#FieldCountrySelect').val();
        genericFormInfo["Phone"] = $('#FieldPhone').val();
        genericFormInfo["Fax"] = $('#FieldFax').val();
        genericFormInfo["Email"] = $('#FieldEmail').val();

    }

    var Submit = function () {



        populateFormInfo();
        var json = JSON.stringify(genericFormInfo);
        $.ajaxDotNet('/app_services/corporate/public/PublicServices.asmx/GetGenericFormSubmit', {
            verb: 'POST',
            data: {
                'json': json,
                'guid': currentItem
            },
            success: function (obj) {
                var returnedobj = JSON.parse(obj.d);
                if (returnedobj.Success == true) {
                    window.location = returnedobj.JavaScript;
                }
                else {
                    alert(returnedobj.ErrorMessage);
                }
            },
            error: function (er) {

            }
        });


    };


    var Reset = function () {
        $('#GetQuoteDiv input[type=text]').each(function () {

            $(this).removeClass('error');

        });

    };



    /* 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;
    };

    $(function () {



        $('#corp-submit-form').click(function (e) {
            e.preventDefault();
            Reset();
            if (Validation() == true) {
                Submit();
            } else {
                $('input, select').focusin(function () {
                    $(this).removeClass('error');
                    $(this).parent().prev('label').removeClass('error');
                    if ($(this).is('select')) {
                        $(this).prev('label').removeClass('error');
                    }
                });
            };
        });




    });





} ();
