﻿//var FormBuilder = function () {
var NEWSLETTER_COOKIE_NAME = 'newsLetterEmail';
var NEWSLETTER_COOKIE_OPTIONS = {
    path: '/',
    expires: 365
}
    var contactInfo = {};

    var Submit = function () {

        BuildFormList();
        var json = JSON.stringify(contactInfo);
        var folderID = $('#form-folder-id').val();
        $.ajaxDotNet('/App_Webservices/Vpriv/Public.asmx/CreateUser', {
            verb: 'POST',
            data: {
                'folderId': folderID,
                'json': json
            },
            success: function (obj) {
                var returnedObj = JSON.parse(obj.d);
                if (returnedObj.Success == true) {
                    // Redirect user to contact-us-confirmation page
                    window.location.replace("/general/contact-us-confirmation");
                }
                else {
                    alert(returnedObj.ErrorMessage);
                }
            },
            error: function (err) {
                $('.error-container').show();
            }
        });

    };

    //loop through all input fields
    var BuildFormList = function () {
        contactInfo["ddl-suffix"] = $('#FieldSuffix').val();
        contactInfo["tb-firstname"] = $('#FieldFirstName').val();
        contactInfo["tb-lastname"] = $('#FieldLastName').val();

        contactInfo["rbl-iam"] = $('input:radio[name=FieldIAmA]:checked').val();
        // loop through the "interested in" checkboxes to get all the checked values
        var interestedIn = "";
        var isFirst = true;
        $("input:checkbox[name=FieldInterested]:checked").each(function () {
            // add $(this).val() to the string
            if (!isFirst)
                interestedIn += ",";

            isFirst = false;
            interestedIn += $(this).val();
        });
        contactInfo["cbl-interestedin"] = interestedIn;

        contactInfo["tb-email"] = $('#FieldEmail').val();
        contactInfo["tb-address1"] = $('#FieldAddress1').val();
        contactInfo["tb-address2"] = $('#FieldAddress2').val();
        contactInfo["tb-city"] = $('#FieldCity').val();
        contactInfo["ddl-state"] = $('#FieldState').val();
        contactInfo["tb-zip"] = $('#FieldZip').val();
        contactInfo["tb-phone"] = $('#FieldPhone').val();
        contactInfo["ta-questions"] = $('#FieldQuestions').val();
        contactInfo["rbl-contactmethod"] = $('input:radio[name=FieldContactMethod]:checked').val();

        return;
    };

    var Validation = function () {
        var returnvalue = true;

        var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;

        if ($('#FieldSuffix').val() == '' && $('#FieldSuffix').attr('required')) {
            $('#FieldSuffix-error').show();
            if (returnvalue) {
                // bring focus to this field if it's the first error
                $('#FieldSuffix').focus();
            }
            returnvalue = false;
        }
        if ($('#FieldFirstName').val() == '' && $('#FieldFirstName').attr('required')) {
            $('#FieldFirstName-error').show();
            if (returnvalue) {
                // bring focus to this field if it's the first error
                $('#FieldFirstName').focus();
            }
            returnvalue = false;
        }
        if ($('#FieldLastName').val() == '' && $('#FieldLastName').attr('required')) {
            $('#FieldLastName-error').show();
            if (returnvalue) {
                // bring focus to this field if it's the first error
                $('#FieldLastName').focus();
            }
            returnvalue = false;
        }
        if ($('#FieldAm').attr('required')) {
            if ($('input:radio[name=FieldIAmA]:checked').val() == null
            || $('input:radio[name=FieldIAmA]:checked').val() == '') {
                $('#FieldIAmA-error').show();
                if (returnvalue) {
                    // bring focus to this field if it's the first error
                    $('input[name=FieldIAmA]').focus();
                }
                returnvalue = false;
            }
        }
        if ($('#FieldInterestedIn').attr('required')) {
            var testSelect = false;
            $("input:checkbox[name=FieldInterested]:checked").each(function () {
                testSelect = true;
            });
            if (!testSelect) {
                $('#FieldInterested-error').show();
                if (returnvalue) {
                    // bring focus to this field if it's the first error
                    $('input[name=FieldInterested]').focus();
                }
                returnvalue = false;
            }
        }

        if ($('#FieldEmail').attr('required') && $('#FieldEmail').val() == '') {
            $('#FieldEmail-error').show();
            if (returnvalue) {
                // bring focus to this field if it's the first error
                $('#FieldEmail').focus();
            }
            returnvalue = false;
        }
        else if ($('#FieldEmail').val() != '') {
            var emailVal = $('#FieldEmail').val();
            var success = emailVal.match(/^(\w[-._\w]*@\w[-._\w]*\w\.\w{2,6})$/);
            if (success == null) {
                $('#FieldEmail-error').show();
                if (returnvalue) {
                    // bring focus to this field if it's the first error
                    $('#FieldEmail').focus();
                }
                returnvalue = false;
            }
        }

        if ($('#FieldAddress1').attr('required') && $('#FieldAddress1').val() == '') {
            $('#FieldAddress1-error').show();
            if (returnvalue) {
                // bring focus to this field if it's the first error
                $('#FieldAddress1').focus();
            }
            returnvalue = false;
        }

        if ($('#FieldAddress2').attr('required') == true && $('#FieldAddress2').val() == '') {
            $('#FieldAddress2-error').show();
            if (returnvalue) {
                // bring focus to this field if it's the first error
                $('#FieldAddress2').focus();
            }
            returnvalue = false;
        }

        if ($('#FieldCity').attr('required') && $('#FieldCity').val() == '') {
            $('#FieldCity-error').show();
            if (returnvalue) {
                // bring focus to this field if it's the first error
                $('#FieldCity').focus();
            }
            returnvalue = false;
        }

        if ($('#FieldState').attr('required') && $('#FieldState').val() == '') {
            $('#FieldState-error').show();
            if (returnvalue) {
                // bring focus to this field if it's the first error
                $('#FieldState').focus();
            }
            returnvalue = false;
        }

        if ($('#FieldZip').attr('required') && $('#FieldZip').val() == '') {
            $('#FieldZip-error').show();
            if (returnvalue) {
                // bring focus to this field if it's the first error
                $('#FieldZip').focus();
            }
            returnvalue = false;
        }

        if ($('#FieldPhone').attr('required') && $('#FieldPhone').val() == '') {
            $('#FieldPhone-error').show();
            if (returnvalue) {
                // bring focus to this field if it's the first error
                $('#FieldPhone').focus();
            }
            returnvalue = false;
        }

        if ($('#FieldContactMethod').attr('required')) {
            if ($('input:radio[name=FieldContactMethod]:checked').val() == null
            || $('input:radio[name=FieldContactMethod]:checked').val() == '') {
                $('#FieldContactMethod-error').show();
                if (returnvalue) {
                    // bring focus to this field if it's the first error
                    $('input[name=FieldContactMethod]').focus();
                }
                returnvalue = false;
            }
        }
        
        if ($('#FieldAgree').attr('required')) {
            var testSelect = false;
            $("input:checkbox[name=FieldAgree]:checked").each(function () {
                testSelect = true;
            });
            if (!testSelect) {
                $('#FieldAgree-error').show();
                if (returnvalue) {
                    // bring focus to this field if it's the first error
                    $('input[name=FieldAgree]').focus();
                }
                returnvalue = false;
            }
        }

        return returnvalue;
    };

    //create button
    //bindings
    $(document).ready(function () {
        $('.error').hide();
        $('.error-container').hide();
        $('#contact-us-submit').click(function (e) {
            e.preventDefault();
            $('.error').hide();
            if (Validation() == true) {
                Submit();
            }
            else {
                return;
            }
        });

        if ($.cookie(NEWSLETTER_COOKIE_NAME) != null) {
            $("#FieldEmail").val($.cookie(NEWSLETTER_COOKIE_NAME));
        }

        $('#search-btn').click(function (e) {
            e.preventDefault();
            // Redirect user to contact-us page
            var togoLocation = $('#search-btn').attr('href');
            if (togoLocation != "") {
                var emailText = $('#searchText').val();
                if (emailText != "") {
                    // Set email in the cookie
                    $.cookie(NEWSLETTER_COOKIE_NAME, emailText, NEWSLETTER_COOKIE_OPTIONS);
                }
            }
            $('#GoSearch').submit();
        });
    });

//}
