﻿
var serviceUrl = "/Ajax/Itemend/ItemendService.svc/";
var proxy = new ServiceProxy(serviceUrl);

function sendEmailFriend() {

    proxy.invoke("sendEmailFriend",
    {
        sendername: $('#sendername').val(),
        emails: $('#emails').val(),
        message: $('#message').val(),
        accountid: $('#hdnAccountId').val(),
        itemno: $('#hdnItemNo').val()
    },
    function(res, status) {
        if (res != null) {
            if (res == "") {
                $('.email-wrapper').html("<div><center><h1>Thanks for sending!</h1></center></div>");
            }
            else {
                $('.divError').html(res);
                $('.divError').show();
            }
            }
    },
    onPageError, false);
}


function showCompleteTransactionModal() {
    $('#cartdialog').dialog('open');
}


function submitRobbinsAppointmentForm() {


    //validate form before doing anythign else
    var bIsValid = true;
    var sContactBy = $('[name="contact_by_input"]:radio:checked').val();
    var sSalutation = $('[name="salutation_input"]:radio:checked').val();

    $("#appointmentForm span.error").remove();
    $('#appointmentForm .required').each(function (index, domEle) {

        

        if (
                ($(domEle).val() == "" && domEle.id != "email") || 
                (domEle.id=="email" && $(domEle).val() == "" && sContactBy == "E-mail")
            ) 
            {
                //not doing this, but this is the box highlighting $(domEle).addClass("errorReq");
                $("<span class=error> * required</span>").insertAfter(domEle);
                bIsValid = false;
            }
        });

        //check phone separately
        if (sContactBy == "Phone" && ($('#phone_1').val() == "" || $('#phone_2').val() == "" || $('#area_code').val() == "")) {
            $("<span class=error> * required</span>").insertAfter($('#phone_2'));
        }


    if (bIsValid) {

        

        if (sContactBy == undefined)
            sContactBy = "";
        if (sSalutation == undefined)
            sSalutation = "";

        var params = {
            Salutation: sSalutation,
            FirstName: $('#first_name').val(),
            LastName: $('#last_name').val(),
            Email: $('#email').val(),
            AreaCode: $('#area_code').val(),
            Phone1: $('#phone_1').val(),
            Phone2: $('#phone_2').val(),
            ContactBy: sContactBy,
            StoreId: $('#location option:selected').val(),
            AppointmentDate: $('#datepicker').val(),
            AppointmentTime: $('#time option:selected').val(),
            ReferringUrl: window.location.href,
            Source: "ProdDetail"
        };










        $('#btnSubmitAppointment').hide();
        //$('#btnSubmitAndContinueProcessing').show();

        $.ajax({
            type: "POST",
            dataType: 'json',
            url: "/Ajax/RobbinsUtilities.svc/submitAppointmentRequest",
            data: "form=" + JSON.stringify(params),
            success: function (response) {
                if (!response.d.IsError) {
                    // TODO: confirmation message?
                    $('#divAppointmentFeedback').html('Your appointment has been submitted. We will be in touch within 24 hrs to help you find the ring of your dreams.');
                    $('#divAppointmentTracking').html(function () {
                        var trackingHtml = "<!-- Google Code for &lt;aw&gt; New Appointment Lead Conversion Page -->" +
                        "<script type=\"text/javascript\">" +
                        "<!--" +
                         "var google_conversion_id = 1070792923;" +
                         "var google_conversion_language = \"en\";" +
                         "var google_conversion_format = \"3\";" +
                         "var google_conversion_color = \"ffffff\";" +
                         "var google_conversion_label = \"TZncCM2nuwEQ24HM_gM\";" +
                         "var google_conversion_value = 0;" +
                         "//-->" +
                         "</script>" +
                         "<script type=\"text/javascript\" src=\"http://www.googleadservices.com/pagead/conversion.js\">" +
                         "</script>" +
                         "<noscript>" +
                         "<div style=\"display:inline;\">" +
                         "<img height=\"1\" width=\"1\" style=\"border-style:none;\" alt=\"\" src=\"http://www.googleadservices.com/pagead/conversion/1070792923/?label=TZncCM2nuwEQ24HM_gM&amp;guid=ON&amp;script=0\"/>" +
                         "</div>" +
                         "</noscript>" +
                        "<!-- MSN -->" +
                        "<script type=\"text/javascript\">" +
                        "    if (!window.mstag) mstag = { loadTag: function () { }, time: (new Date()).getTime() };" +
                        "</script> " +
                        "<script id=\"mstag_tops\" type=\"text/javascript\" src=\"http://flex.atdmt.com/mstag/site/7e819790-c173-4a8f-b6bb-85499aa9804c/mstag.js\"></script>" +
                        "<script type=\"text/javascript\">mstag.loadTag(\"conversion\", { cp: \"5050\", dedup: \"1\" })</script> " +
                        "<noscript>" +
                        "<iframe src=\"http://flex.atdmt.com/mstag/tag/7e819790-c173-4a8f-b6bb-85499aa9804c/conversion.html?cp=5050&dedup=1\" frameborder=\"0\" scrolling=\"no\" width=\"1\" height=\"1\" style=\"visibility:hidden; display:none\"></iframe> " +
                        "</noscript>" +

                        "<!-- Begin SearchForce tracking code for conversion page (4.1) -->" +
                        "<div id=\"sf_fl_content\"></div>" +
                        "<script type=\"text/javascript\" language=\"Javascript\" src=\"http://sftrack.searchforce.net/SFConversionTracking/CTCommon.js\"></script>" +
                        "<script type=\"text/javascript\" language=\"Javascript\">SFCookie.include(); var jconversion_type = \"Appointment\"; var jValue = \"\"; var jOrderID = \"\"; ImageJSConversionProcess(); </script>" +
                        "<!-- End SearchForce tracking code -->";

                        return trackingHtml;
                    });


                }
                else {
                    $('#divAppointmentFeedback').html("'" + response.d.ErrorMessage + "'");
                    $('#btnSubmitAppointment').show();
                }
            },
            error: function (xhr, status) {
                alert('An error occurred while trying to send your request.  The technical team has been notified. ');
            }
        });

    }
   

}


function submitConsultantForm() {

    var sContactBy = $('input:radio[@name=contact_by_input]:checked').val();
    var params = {
        FirstName: $('#fname').val(),
        LastName: $('#lname').val(),
        Email: $('#email').val(),
        Phone: $('#phone').val(),
        Comments: $('#comments').val(),
        ItemNo: $('#hdnItemNo').val(),
        HostId: "100015",
        ContactBy: sContactBy,
        BestTimeToReach: $('#time option:selected').val()
    };

    jQuery.validator.messages.required = " * required";
    $("#consultantForm").validate();

    var bIsValid = $("#consultantForm").valid();

    if (bIsValid) {

        $('#btnSubmitAndContinue').hide();
        $('#btnSubmitAndContinueProcessing').show();

        $.ajax({
            type: "POST",
            dataType: 'json',
            url: "/Ajax/Itemend/ItemendService.svc/sendConsultantEmail",
            data: "form=" + JSON.stringify(params),
            success: function (response) {
                if (!response.d.IsError) {
                    // confirmation
                    addCartPostback();
                }
            },
            error: function (xhr, status) {
                alert('An error occurred while trying to send your request.  The technical team has been notified. ');
            }
        });
    }
}



    

