var contactBox;

contactWidgetInit = function () {
    $("#submitRFP").click(function () {

        submitContactForm();
    });
};

function showPrintView() {
    
    var url = window.location.href + '?printView=1';
    //window.location = url;
    window.open(url);
    return false;
}


function submitContactForm() {
    contactBox = $("body").jBox({ 'showClose': false, 'clickToClose': false }).showWindow('<div class="overlayMessage"><h3>Contact Us Submission</h3>processing...</div>');

    try {
        //capture the click of the contact us widget
        var type = cleanse($("#c_type").val());
        var name = cleanse($("#c_name").val());
        var store = cleanse($("#c_store").val());
        var email = cleanse($("#c_email").val());
        var phone = cleanse($("#c_phone").val());
        var distributor = cleanse($("#c_distributor").val());
        var city = cleanse($("#c_city").val());
        var state = cleanse($("#c_state").val());
        var comments = cleanse($("#c_comments").val());

        var postData = { 'action': 'submitRFP', 'type': type, 'name': name, 'store': store, 'email': email, 'phone': phone, 'distributor': distributor, 'city': city, 'state': state, 'comments': comments, 'url': window.location.href };

        if (postData.name == '') {
            showErrorMessage('Please enter your name.');
            return;
        }

        if (postData.email == '') {
            showErrorMessage('Please enter your email.');
            return;
        }

        if (postData.phone == '') {
            showErrorMessage('Please enter your phone number.');
            return;
        }

        if (postData.city == '') {
            showErrorMessage('Please enter your city.');
            return;
        }

        if (postData.state == '') {
            showErrorMessage('Please enter your state.');
            return;
        }

        $.ajax({
            type: "POST",
            url: "/services/contactHandler.ashx",
            data: postData,
            success: function (boolVal) {
                if (boolVal == '1') {
                    showErrorMessage('<h3>Submission Successful</h3>Thank you for your submission.<img height="1" width="1" style="border-style:none;" alt="" src="https://www.googleadservices.com/pagead/conversion/1027672046/?label=YlOsCNzWsAEQ7o-E6gM&amp;guid=ON&amp;script=0"/>');
                    
                    clearContactForm();
                }
                else {
                    showErrorMessage("<h3>Submission Failed!</h3>Please refresh the page and try again.");
                }
            },
            error: function (e) {
                showErrorMessage("<h3>Submission Failed!</h3>Please refresh the page and try again.");
            }
        });

    } catch (e) {
        showErrorMessage("<h3>Submission Failed!</h3>Please refresh the page and try again.");
    }
}

function clearContactForm() {    
    $("#c_name").val('');
    $("#c_store").val('');
    $("#c_email").val('');
    $("#c_phone").val('');
    $("#c_distributor").val('');
    $("#c_city").val('');
    $("#c_state").val('');
    $("#c_comments").val('');
}

function showErrorMessage(msg) {    
    contactBox = $("body").jBox().showWindow('<div class="overlayMessage">' + msg + '</div>');
}

function cleanse(t) {
    return t.replace('/(Name|Store|E\-Mail|Phone|Distributor|City|State|Comments|Questions)/', '');    
}








//$(document).ready(contactWidgetInit);

function contactUsWidgetResponse(message, success) {    
    if (success == 'True') {        
        showErrorMessage('<h3>Submission Successful</h3>Thank you for your submission.<img height="1" width="1" style="border-style:none;" alt="" src="https://www.googleadservices.com/pagead/conversion/1027672046/?label=YlOsCNzWsAEQ7o-E6gM&amp;guid=ON&amp;script=0"/>');
    }
    else {        
        showErrorMessage("<h3>Submission Failed!</h3>Reason for failure: " + message);
    }
}

function jumpToProductCustomization() {    
    scrollToObject('.productCustomization', function () { $('.productCustomization').trigger('click'); });
}

function scrollToObject(obj, callBckFcn) {
    var y = $(obj).offset().top;
    $('html, body').animate({ scrollTop: y }, function () {
        if (typeof callBckFcn == 'function') callBckFcn.call(this);
    });    
}
