﻿(function ($) {
    $.business = {
        sendBusinessInquiry: function (businessName, contactName, emailAddress, contactPhone, timeFrame, questions, callback) {
            $.ajax({
                type: "POST",
                cache: false,
                url: "/Business/SendBusinessInquiry",
                data: { businessName: businessName, contactName: contactName, emailAddress: emailAddress, contactPhone: contactPhone, timeFrame: timeFrame, questions: questions },
                success: function (obj) {
                    if ($.isFunction(callback)) {
                        callback(obj);
                    }
                },
                error: function (obj) {
                    if ($.isFunction(callback)) {
                        callback(obj);
                    }
                }
            });
        },
        completePearlDealOrder: function (dealId, billingFirstName, billingLastName, billingCardNumber, billingAddress, billingCity, billingCode, billingState, billingPostalCode, billingExpDateMonth, billingExpDateYear, quantity, callback) {
            $.ajax({
                type: "POST",
                cache: false,
                url: "/Business/OrderPearlDeal",
                data: { dealId: dealId, billingFirstName: billingFirstName, billingLastName: billingLastName, billingCardNumber: billingCardNumber, billingAddress: billingAddress,
                    billingCity: billingCity, billingCode: billingCode, billingState: billingState, billingPostalCode: billingPostalCode, billingExpDateMonth: billingExpDateMonth,
                    billingExpDateYear: billingExpDateYear, quantity: quantity
                },
                success: function (obj) {
                    if ($.isFunction(callback)) {
                        callback(obj);
                    }
                },
                error: function (obj) {
                    if ($.isFunction(callback)) {
                        callback(obj);
                    }
                }
            });
        },
        registerForPearlDeal: function (username, email, firstName, lastName, password) {
            var success = "False";
            $.ajax({
                async: false,
                type: "POST",
                cache: false,
                url: "/Account/RegisterAjax",
                data: { username: username, email: email, firstName: firstName, lastName: lastName, password: password },
                success: function (obj) {
                    success = obj;
                },
                error: function (obj) {
                    success = obj;
                }
            });
            return success;
        },
        savePearlDealListEmail: function (email, callback) {
            $.ajax({
                type: "POST",
                cache: false,
                url: "/Business/SaveEmailtoPearlDealsList",
                data: { email: email },
                success: function () {
                    if ($.isFunction(callback)) {
                        callback();
                    }
                },
                error: function () {
                    if ($.isFunction(callback)) {
                        callback();
                    }
                }
            });
        },
        sendPearlDealEmail: function (id, to, from, msg, callback) {
            $.ajax({
                type: "POST",
                cache: false,
                url: "/Business/SendPearlDealEmail",
                data: { id: id, to: to, from: from, msg: msg },
                success: function () {
                    if ($.isFunction(callback)) {
                        callback();
                    }
                },
                error: function () {
                    if ($.isFunction(callback)) {
                        callback();
                    }
                }
            });
        },
        getBusinessAverageRating: function (businessId, callback) {
            $.ajax({
                type: "POST",
                cache: false,
                url: "/Business/GetBusinessAverageRating",
                data: { businessId: businessId },
                success: function (obj) {
                    if ($.isFunction(callback)) {
                        callback(obj);
                    }
                },
                error: function () {
                    if ($.isFunction(callback)) {
                        callback(0);
                    }
                }
            });
        },
        validatePearlDealOrder: function (form, loggedIn, jLoggedIn, callback) {
            var error = "";
            if (!loggedIn) {
                if ($(form).find("#txtSignUpFirstName").val().length == 0) {
                    error += "Please enter your First Name.<br />";
                }
                if ($(form).find("#txtSignUpLastName").val().length == 0) {
                    error += "Please enter your Last Name.<br />";
                }
                if (!$.common.validateEmail($(form).find("#txtSignUpEmail").val())) {
                    error += "Please enter a valid email address.<br />";
                }
                else {
                    if (!jLoggedIn) {
                        $.common.checkIfEmailExists($(form).find("#txtSignUpEmail").val(), function (exists) {
                            if (exists == "True") {
                                error += "Email address is already in use.<br />";
                            }
                        });
                    }
                }
                if ($(form).find("#txtSignUpPassword").val().length < 6 || $(form).find("#txtSignUpPasswordConfirm").val().length < 6) {
                    error += "Please enter and confirm a password. Password must be at least 6 characters long.<br />";
                }
                else {
                    if ($(form).find("#txtSignUpPassword").val() != $(form).find("#txtSignUpPasswordConfirm").val())
                        error += "Passwords do not match.<br />";
                }
            }
            if ($(form).find("#txtBillingFirstName").val().length == 0) {
                error += "Please enter a Billing First Name.<br />";
            }
            if ($(form).find("#txtBillingLastName").val().length == 0) {
                error += "Please enter a Billing Last Name.<br />";
            }
            if ($(form).find("#txtBillingAddress").val().length == 0) {
                error += "Please enter a Billing Address.<br />";
            }
            if ($(form).find("#txtBillingCity").val().length == 0) {
                error += "Please enter a City.<br />";
            }
            if (!$.common.validateCCNumber($(form).find("#txtBillingCardNumber").val(), $(form).find("#txtBillingSecurityCode").val())) {
                error += "Credit Card Number or Security Code is invalid.<br />";
            }
            if ($(form).find("#txtBillingPostalCode").val().length == 0) {
                error += "Please enter a Postal Code.<br />";
            }
            if ($(form).find("#chkTermsAndUse:checked").length == 0) {
                error += "You must agree to the Terms and User Conditions.";
            }
            if ($.isFunction(callback)) {
                callback(error);
            }
        },
        validateReview: function (headline, body, rating, callback) {
            var error = "";
            if (headline.length == 0) {
                error += "Please enter a headline for your review.";
            }
            if (body.length == 0) {
                error += (error.length > 0 ? "<br />" : "") + "Please write your review.";
            }
            if (rating == null) {
                error += (error.length > 0 ? "<br />" : "") + "Please select a Overall Rating. (Top)";
            }
            if ($.isFunction(callback)) {
                callback(error);
            }
        },
        getPrePopulatedReviews: function (type, callback) {
            $.ajax({
                type: "POST",
                cache: false,
                url: "/Review/GetPrePopulatedReviews",
                data: { type: type },
                success: function (obj) {
                    if ($.isFunction(callback)) {
                        callback(obj);
                    }
                },
                error: function () {
                    if ($.isFunction(callback)) {
                        callback(null);
                    }
                }
            });
        },
        getReviewForTicker: function (excludeIds, type, callback) {
            $.ajax({
                type: "POST",
                cache: false,
                url: "/Review/GetRecentReview",
                data: { exclude: excludeIds, type: type },
                success: function (obj) {
                    if ($.isFunction(callback)) {
                        callback(obj);
                    }
                },
                error: function () {
                    if ($.isFunction(callback)) {
                        callback(null);
                    }
                }
            });
        },
        createReviewDialog: function (html, businessId, callback) {
            $.fancybox({
                "Modal": true,
                "autoDimensions": true,
                "autoScale": true,
                "scrolling": "no",
                "padding": 10,
                "showCloseButton": false,
                "content": html,
                "onComplete": function () {
                    $(".btnSubmitReview").unbind().click(function () {
                        var headline = $("#reviewHeadline").val();
                        var body = $("#reviewBody").val();

                        $("#reviewErrors").html("");
                        $("#reviewErrors").hide();
                        if (headline.length == 0) {
                            $("#reviewErrors").append("Headline field is required.<br />").show();
                        }
                        if (body.length == 0) {
                            $("#reviewErrors").append("Body field is required.<br />").show();
                        }
                        if (body.length == 0 || headline.length == 0) {
                            return;
                        }

                        var ratings = "";
                        $(".reviewCriteraRating").each(function () {
                            ratings += $(this).attr("Name") + ":" + $(this).val() + ";";
                        });

                        $.common.modalDialog("Saving review...");

                        $.ajax({
                            type: "POST",
                            cache: false,
                            url: "/Review/AddReview",
                            data: { businessId: $("#reviewBusinessId").val(), rating: $("#reviewRating").val(), headline: $("#reviewHeadline").val(), body: $("#reviewBody").val(), ratings: ratings },
                            success: function () {
                                if ($.isFunction(callback)) {
                                    callback();
                                }
                                $.fancybox.close();
                            },
                            error: function () {
                                $("#reviewErrors").html("Error creating review.").show();
                            }
                        });
                    });

                    $(".btnCancelReview").unbind().click(function () {
                        $.fancybox.close();
                    });
                }
            });
        },
        searchWriteAReviewSearchBusinesses: function (search, callback) {
            $.ajax({
                type: "POST",
                cache: false,
                url: "/Business/WriteAReviewSearchBusinesses",
                data: { search: search },
                success: function (obj) {
                    if ($.isFunction(callback)) {
                        callback(obj);
                    }
                },
                error: function () {
                    $.common.errorDialog("Error searching businesses.", true);
                }
            });
        },
        searchBusinesses: function (search, callback) {
            $.ajax({
                type: "POST",
                cache: false,
                url: "/Business/SearchBusinesses",
                data: { search: search },
                success: function (obj) {
                    if ($.isFunction(callback)) {
                        callback(obj);
                    }
                },
                error: function () {
                    $.common.errorDialog("Error searching businesses.", true);
                }
            });
        },
        createCommentDialog: function (reviewId, businessName, reviewBody) {
            var html = '<div style="width: 400px; height: auto; margin-bottom: 10px;"><div style="margin-bottom: 5px;">';
            html += '<b>Write a comment for:</b></div><div style="margin-bottom: 5px; margin-left: 10px;">' + businessName + '</div><div>';
            html += '<b>Review you\'re commenting on:</b><div style="margin-top: 5px; margin-bottom: 5px; margin-left: 10px;"><p>' + reviewBody + '</p></div></div>';
            html += '<div> <div style="display: none; color: red;" id="commentRequired">&nbsp;</div>';
            html += '<textarea id="commentbody" style="width: 310px; height: 100px;"></textarea></div>';
            html += '<div style="text-align: right; margin-top: 20px; width: 290px;">';
            html += '<a class="ui-button-text-only ui-state-default ui-corner-all" href="#"><span class="ui-button-text" id="submitComment">Submit</span></a>&nbsp;';
            html += '<a class="ui-button-text-only ui-state-default ui-corner-all" href="javascript:$.fancybox.close();"><span class="ui-button-text">Cancel</span></a>';
            html += '</div></div>';

            $.fancybox({
                "Modal": true,
                "autoDimensions": true,
                "autoScale": true,
                "scrolling": "no",
                "padding": 10,
                "showCloseButton": false,
                "content": html,
                "onComplete": function () {
                    $("#submitComment").unbind().click(function () {
                        var comment = $("#commentbody").val();

                        if (comment.length == 0) {
                            $("#commentRequired").html("*Comment field is required.").show();
                            return;
                        }

                        $.ajax({
                            type: "POST",
                            cache: false,
                            url: "/Review/AddCommentToReview",
                            data: { reviewId: reviewId, body: comment },
                            success: function () {
                                reloadComments(reviewId);
                                $.fancybox.close();
                            },
                            error: function () {
                                $("#commentRequired").html("Error adding comment").show();
                            }
                        });
                    });
                }
            });
        }
    }
})(jQuery);
