﻿var ok = false;
function postClick() {
    var message = "";
    if ($("body").attr("class") == "seller") {
        message = "<h3>Create a match</h3><p>By submitting your Sell Project, you will only match with a relevant Buyer.<p>Go ahead and create match and agree to our <a class='external tc-popup' href='/Home/Terms' rel='external'>Terms &amp; Conditions</a> and <a class='external privacy-popup' href='/Home/Privacy' rel='external'>Privacy Policy</a>?</p>";
    }
    else {
        message = "<h3>Create a match</h3><p>By submitting your Buy Project, you will only match with a relevant Seller.<p>Go ahead and create match and agree to our <a class='external tc-popup' href='/Home/Terms' rel='external'>Terms &amp; Conditions</a> and <a class='external privacy-popup' href='/Home/Privacy' rel='external'>Privacy Policy</a>?</p>";
    }
    //if (!ok) {
    $.prompt(message, {
        buttons: { "Yes": true, "No": false },
        overlayspeed: "fast",
        top: "30%",
        callback: function(v, m, f) {
            if (v) {
                //ok = true;
                //Do BM Match Relevence if we clicked Match directly
                if ($("body").attr("class") == "buyer" && ($("div.buyer-Edit-page").length > 0 || $("div.buyer-New-page").length > 0)) {
                    try {
                        BuyMandateMatchRelevence();
                    } catch (err) {
                        var txt = "There was an error on this page.\n\n";
                        txt += "Error description: " + err.description + "\n\n";
                        txt += "Click OK to continue.\n\n";
                        alert(txt);
                    }
                }
                $("form")[0].submit();
                //$("p.buttons input#post").click();
            }
        }
    });
    insertExtraData(addContactDetails);
    return false;
//    }
//    else {
//        ok = false;
//        return true;
//    }
}

function postToNetworkClick() {
    var customerGroupName = $("input#hidGroupName").val();
    $.prompt("<h3>Create a " + customerGroupName + " Network Match</h3><p>You are about to submit to " + customerGroupName + " Network; the matching will be restricted to members of the " + customerGroupName + " network. Please make sure you abide by our <a class='external tc-popup' href='/Home/Terms' rel='external'>Terms &amp; Conditions</a> and <a class='external privacy-popup' href='/Home/Privacy' rel='external'>Privacy Policy.</a></p> <p>Go ahead and create network match?</p>", {
        buttons: { "Yes": true, "No": false },
        overlayspeed: "fast",
        top: "30%",
        callback: function(v, m, f) {
            if (v) {
                $("form")[0].submit();
            }
        }
    });
    insertExtraData(addContactDetails);
    return false;
}

// Add extra data to dynamic places
function insertExtraData(func) {
    $("div.jqi .jqibuttons").after(func());
    $(".siccode-lookup #sendemail-container").after(func());
    $("#businesseslike-container").append(func());
    $(".nda-lookup .popup-buttons").after(func());
}

// Add contact numbers to popups using static HTML file
var midContactDetails = null;
function addContactDetails() {
    if (midContactDetails === null) {
        $.get("/Content/Static/partialhtml/contacts.htm", function(data) {
            midContactDetails = data;
        });
    }
    return midContactDetails;
}


$(document).ready(function() {
    $("#sic-suggestion-container").hide();
    // Pre-load contact details
    addContactDetails();

    $("p.buttons input.button").click(function() {
        $("#clicked-button-id").val($(this).attr("id"));
        return true;
    });

    // Hides helper text in forms, straight off the bat
    $("p.helper").hide();

    // Show the helper, if there is one, when input is focused
    $("textarea, select, fieldset[class!='buyer-geography'] input[type=text]").focus(function() {
        $(this).parent().prev("p.helper").fadeIn('fast');
    });

    // Hide the helper when input is blurred
    $("textarea, select, fieldset input[type=text]").blur(function() {
        var container = $(this).parent().parent();
        $(container).find("p.helper").fadeOut('fast');
    });

    // Hide / Show helper when near / away from 'must have' checkbox
    $("div.must-have, div.exclusion").hover(function() {
        $(this).prev("p.helper").fadeIn('fast');
    }, function() {
        $(this).prev("p.helper").fadeOut('fast');
    }
    );

    // Add focus styles
    $("textarea, input[type=text], input[type=password]").focus(function() {
        $(this).addClass("focus");
    });

    // Remove focus styles
    $("textarea, input[type=text], input[type=password]").blur(function() {
        $(this).removeClass("focus");
    });


    $("a[rel='external']").attr({
        title: "Opens in a new window"
    });
    $("a[rel='external']").addClass("external");
    $("a[rel='external']").live("click", function() {
        window.open(this.href);
        return false;
    });

    $("p.buttons input#post").click(postClick);
    $("#posttonetwork").click(postToNetworkClick);
});

function checkForError() {
    if ($("#operation-error")) {
        $.prompt($("#operation-error").html(), {
            buttons: { "Yes": true, "No": false },
            overlayspeed: "fast",
            top: "30%",
            submit: function(v, m, f) {
                return true;
            }
        });
    }
}
