﻿var FormPopUp = function () {
    //function declarations
    // object to store all the question and answers that user has passed
    var popupFormInfo = {};
    var targetLocation = "";
    var callerAssetUrl = "";

    function populatePopupFormInfo() {
        popupFormInfo["AssetUrl"] = callerAssetUrl;
        popupFormInfo["FirstName"] = $('#FieldFirstName').val();
        popupFormInfo["LastName"] = $('#FieldLastName').val();
        popupFormInfo["JobTitle"] = $('#FieldJobTitle').val();
        popupFormInfo["CompanyName"] = $('#FieldCompany').val();
        popupFormInfo["Email"] = $('#FieldEmail').val();

        popupFormInfo["Address1"] = $('#FieldAddress1').val();
        popupFormInfo["City"] = $('#FieldCity').val();
        popupFormInfo["State"] = $('#FieldState').val();
        popupFormInfo["Zip"] = $('#FieldZip').val();

        popupFormInfo["Country"] = $('#FieldCountrySelect').val();
        popupFormInfo["Phone"] = $('#FieldPhone').val();
    }

    var SendPopupData = function () {
        populatePopupFormInfo();
        var json = JSON.stringify(popupFormInfo);
        $.ajaxDotNet('/app_services/corporate/public/FormServices.asmx/SendPopUpInfo', {
            verb: 'POST',
            data: {
                'json': json,
                'guid': currentItem
            },
            success: function (obj) {
                var returnedobj = JSON.parse(obj.d);
                if (returnedobj.Success == true) {
                    // Set the cookie so the user doesn't have to take it again
                    CookieSet();
                    alert(returnedobj.Json);
                    // Close the popup
                    popupFormInfo = {};
                    $(".simplemodal-close").click();
                    window.location = targetLocation;
                }
                else {
                    alert(returnedobj.ErrorMessage);
                }
            },
            error: function (er) {

            }
        });

    };

    var GetPopUp = function () {
        $.ajaxDotNet('/app_services/corporate/public/PublicServices.asmx/GetFormPopUP', {
            verb: 'POST',
            data: { 'uri': window.location.protocol + "//" + window.location.host
            },
            success: function (obj) {
                var returnedobj = JSON.parse(obj.d);
                if (returnedobj.Success == true) {
                    $('#PopUpDivHolder').empty().append(returnedobj.Json);
                    $('#PopUpDivHolder').modal({
                        // This shouldn't be nescessary. Why isn't it being hidden already?
                        onClose: function () {
                            $.modal.close();
                            $('#PopUpDivHolder').hide();
                        }

                    });
                    //$('#simplemodal-wrap').append();
                    $('#corp-submit-getaquote').click(function (e) {
                        e.preventDefault();
                        if (Validation() == true) {
                            SendPopupData();
                        } else if (Validation() == false) {
                            $('input, select').focusin(function () {
                                $(this).removeClass('error');
                                $(this).parent().children('label').removeClass('error');
                                if ($(this).is('select')) {
                                    $(this).prev('label').removeClass('error');
                                }
                            });
                        }
                    });
                }
                else {
                    alert(returnedobj.ErrorMessage);
                }
            },
            error: function (er) {

            }
        });

    };

    /* Validation Function*/
    var Validation = function () {
        var returnvalue = true;
        $('#PopUpDivHolder input, select').each(function () {

            //cache of our current input element and its label
            var that = $(this);
            var thatLabel = $(that).parent().children('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;
    };


    var CookieCheck = function () {
        var needpopup = true;

        try {
            var test = $.cookie('corpFormCookie');
            if (test == 'filled') {
                needpopup = false;
            }
        }
        catch (er) {
        }
        return needpopup;
    }

    var CookieSet = function () {
        var options = { path: '/', expires: 10 };
        $.cookie('corpFormCookie', 'filled', options);
        return false;
    }


    $(function () {
        $('a.needpopup').live("contextmenu", function (e) {
            if (CookieCheck()) {
                targetLocation = e.target;
                GetPopUp();
                return false;
            }
            else {
                return true;
            }

        });

        $('a.needpopup').live('click', function (e) {
            e.preventDefault();
            callerAssetUrl = jQuery(this).attr('href');
            if (CookieCheck()) {
                targetLocation = e.target;
                GetPopUp();
            }
            else {
                window.location = e.target;
            }

        });
    });
} ();


