﻿// Send Lead Company.

function NoRequest(companyId, networkId, CompanyListingId, buttonId) {


    var togglecompany = $(".togglecompany_" + companyId);

    var companyInHistory = CompanyIdExistsInHistoryCookie(companyId);

    if (!companyInHistory) {
        togglecompany.attr("src", "/Images/Buttons/RequestingInfo.gif");
        togglecompany.attr("alt", "Requesting Info...");

        var togglecompanyprofile = $(".togglecompanyprofile_" + companyId);
        togglecompanyprofile.attr("src", "/Images/Buttons/RequestingInfoProfile.png");
        togglecompanyprofile.attr("alt", "Requesting Info...");

        var sourceRequest = (document.location.pathname == "/" ? "Index" : document.location.pathname);
        var requestQuery = "/ViewAll/RequestSubmissionViewAll/?companyId=" + companyId + "&networkId=" + networkId + "&sourceRequest=" + sourceRequest + "&CompanyListingId=" + CompanyListingId + "&button=" + buttonId;

        $.ajax({
            type: "POST",
            url: requestQuery,
            dataType: "json",
            data: {},
            success: function(response) {

                var togglecompany = $(".togglecompany_" + companyId);
              
                togglecompany.attr("src", "/Images/Buttons/RequestInfo.gif");
                togglecompany.attr("alt", "Request Info");

                var togglecompanyprofile = $(".togglecompanyprofile_" + companyId);
                togglecompanyprofile.attr("src", "/Images/Buttons/RequestInfoProfile.png");
                togglecompanyprofile.attr("alt", "Request Info");


            },
            error: function(xhr, ajaxOptions, thrownError) {

                var togglecompany = $(".togglecompany_" + companyId);
                togglecompany.attr("src", "/Images/Buttons/RequestInfo.gif");
                togglecompany.attr("alt", "Request Info");

                var togglecompanyprofile = $(".togglecompanyprofile_" + companyId);
                togglecompanyprofile.attr("src", "/Images/Buttons/RequestInfoProfile.png");
                togglecompanyprofile.attr("alt", "Request Info");

                $.facebox('<div><h1>Error Processing</h1>' + thrownError + '</div>');

            }
        });
    } else {

    }

}

function YesRequest(companyId, networkId, CompanyListingId, buttonId) {
  
    var togglecompany = $(".togglecompany_" + companyId);

    var companyInHistory = CompanyIdExistsInHistoryCookie(companyId);

    if (!companyInHistory) {
        togglecompany.attr("src", "/Images/Buttons/RequestingInfo.gif");
        togglecompany.attr("alt", "Requesting Info...");

        var togglecompanyprofile = $(".togglecompanyprofile_" + companyId);
        togglecompanyprofile.attr("src", "/Images/Buttons/RequestingInfoProfile.png");
        togglecompanyprofile.attr("alt", "Requesting Info...");
        
        var sourceRequest = (document.location.pathname == "/" ? "Index" : document.location.pathname);
        var requestQuery = "/ViewAll/RequestSubmissionViewAll/?companyId=" + companyId + "&networkId=" + networkId + "&sourceRequest=" + sourceRequest + "&CompanyListingId=" + CompanyListingId + "&button=" + buttonId;
      
        $.ajax({
            type: "POST",
            url: requestQuery,
            dataType: "json",
            data: {},
            success: function(response) {
                
                var togglecompany = $(".togglecompany_" + companyId);
                SetCompaniesHistoryCookie(companyId, companyId);
                togglecompany.attr("src", "/Images/Buttons/InfoRequested.gif");
                togglecompany.attr("alt", "Info Requested");

                var togglecompanyprofile = $(".togglecompanyprofile_" + companyId);
                togglecompanyprofile.attr("src", "/Images/Buttons/InfoRequestedProfile.png");
                togglecompanyprofile.attr("alt", "Info Requested");
            },
            error: function(xhr, ajaxOptions, thrownError) {
              
                var togglecompany = $(".togglecompany_" + companyId);
                togglecompany.attr("src", "/Images/Buttons/RequestInfo.gif");
                togglecompany.attr("alt", "Request Info");

                var togglecompanyprofile = $(".togglecompanyprofile_" + companyId);
                togglecompanyprofile.attr("src", "/Images/Buttons/RequestInfoProfile.png");
                togglecompanyprofile.attr("alt", "Request Info");
                
                $.facebox('<div><h1>Error Processing</h1>' + thrownError + '</div>');

            }
          });
      } else {
         
      }
}

function RequestSubmissionLead(button) {

    var companyId = button.attr("companyid");
    var networkId = button.attr("networkid");
    var CompanyListingId = button.attr("CompanyListingId");
    
    var togglecompany = $(".togglecompany_" + companyId);

    var companyInHistory = CompanyIdExistsInHistoryCookie(companyId);

    if (!companyInHistory) {
        togglecompany.attr("src", "/Images/Buttons/RequestingInfo.gif");
        togglecompany.attr("alt", "Requesting Info...");

        var togglecompanyprofile = $(".togglecompanyprofile_" + companyId);
        togglecompanyprofile.attr("src", "/Images/Buttons/RequestingInfoProfile.png");
        togglecompanyprofile.attr("alt", "Requesting Info...");
        
        var sourceRequest = (document.location.pathname == "/" ? "Index" : document.location.pathname);
        //        var requestQuery = "/ViewAll/RequestSubmissionViewAll/?companyId=" + companyId + "&networkId=" + networkId + "&sourceRequest=" + sourceRequest + "&CompanyListingId=" + CompanyListingId;
        var requestQuery = "/ViewAll/VerifyMatchCapitalInvest/?companyId=" + companyId + "&networkId=" + networkId + "&sourceRequest=" + sourceRequest + "&CompanyListingId=" + CompanyListingId;
       
        
//        $.ajax({
//            type: "POST",
//            url: requestQuery,
//            dataType: "json",
//            data: {},
//            success: function(response) {
//                var companyId = button.attr("companyid");
//                //alert(companyId);
//                var togglecompany = $(".togglecompany_" + companyId);
//                SetCompaniesHistoryCookie(companyId, companyId);
//                togglecompany.attr("src", "/Images/Buttons/InfoRequested.gif");
//                togglecompany.attr("alt", "Info Requested");

//                var togglecompanyprofile = $(".togglecompanyprofile_" + companyId);
//                togglecompanyprofile.attr("src", "/Images/Buttons/InfoRequestedProfile.png");
//                togglecompanyprofile.attr("alt", "Info Requested");
//            },
//            error: function(xhr, ajaxOptions, thrownError) {
//                var companyId = button.attr("companyid");
//                var togglecompany = $(".togglecompany_" + companyId);
//                togglecompany.attr("src", "/Images/Buttons/RequestInfo.gif");
//                togglecompany.attr("alt", "Request Info");

//                var togglecompanyprofile = $(".togglecompanyprofile_" + companyId);
//                togglecompanyprofile.attr("src", "/Images/Buttons/RequestInfoProfile.png");
//                togglecompanyprofile.attr("alt", "Request Info");
//                
//                $.facebox('<div><h1>Error Processing</h1>' + thrownError + '</div>');

//            }
        //        });

        $.ajax({
            type: "POST",
            url: requestQuery,
            dataType: "json",
            data: {},
            success: function(response) {
                var companyId = button.attr("companyid");
                var networkId = button.attr("networkid");
                var CompanyListingId = button.attr("CompanyListingId");
                var yes = "1";
                var no = "0";
                
                if (response.Success) {

                    var togglecompany = $(".togglecompany_" + companyId);
                    SetCompaniesHistoryCookie(companyId, companyId);
                    togglecompany.attr("src", "/Images/Buttons/InfoRequested.gif");
                    togglecompany.attr("alt", "Info Requested");

                    var togglecompanyprofile = $(".togglecompanyprofile_" + companyId);
                    togglecompanyprofile.attr("src", "/Images/Buttons/InfoRequestedProfile.png");
                    togglecompanyprofile.attr("alt", "Info Requested");
                }
                else {

                    var togglecompanyprofile = $(".togglecompanyprofile_" + companyId);
                    togglecompanyprofile.attr("src", "/Images/Buttons/RequestingInfoProfile.png");
                    togglecompanyprofile.attr("alt", "Requesting Info...");

                    $.facebox('<div><table width="100%" border="0"><tr><td colspan="2"><span class="DetailsTitle">Would you like to change investment level to match:</span><br /></td></tr><tr align="center" ><td colspan="2"><span class="CapitalInvestText">' + response.Message + '</span><br /><br /></td></tr><tr align="center"> <td ><img src="/Images/Buttons/Yes.png" class="RequestButton" style="cursor:pointer"  onclick="javascript:{ $.facebox.close(); YesRequest(' + companyId + ',' + networkId + ',' + CompanyListingId + ',' + yes + ');}"/> </td> <td ><img src="/Images/Buttons/NO.png" class="RequestButton" style="cursor:pointer"  onclick="javascript:{ $.facebox.close(); NoRequest(' + companyId + ',' + networkId + ',' + CompanyListingId + ',' + no + ');}"/></td></tr></table></div>');
                    $.facebox.HiddenClose();
                }

            },
            error: function(xhr, ajaxOptions, thrownError) {
                var companyId = button.attr("companyid");
                var togglecompany = $(".togglecompany_" + companyId);
                togglecompany.attr("src", "/Images/Buttons/RequestInfo.gif");
                togglecompany.attr("alt", "Request Info");

                var togglecompanyprofile = $(".togglecompanyprofile_" + companyId);
                togglecompanyprofile.attr("src", "/Images/Buttons/RequestInfoProfile.png");
                togglecompanyprofile.attr("alt", "Request Info");

            }
        });
    } else {
        //alert('company was in history');
    }

}

function LoadCompanyListing() {
    
    var _html = '<br/><center><img src="/facebox/loading.gif" /></center><br/><h1> Processing ...  </h1>';

    $('#ResultRequest').html(_html);

    $('#IndustryML').attr('disabled', 'disabled');
    $('#InvestmentML').attr('disabled', 'disabled')
    $('#LocationML').attr('disabled', 'disabled');

    var industry = $('#IndustryML').val();
    var investment = $('#InvestmentML').val();
    var location = $('#LocationML').val();
    var requestQuery = '/ViewAll/ViewAllCompanyListing/?Industry=' + industry + '&Investment=' + investment + '&Location=' + location;
    $.ajax({
        type: "GET",
        url: requestQuery,
        dataType: "html",
        data: {},
        success: function(response) {
            $('#ResultRequest').html(response);
            $('#IndustryML').attr('disabled', '');
            $('#InvestmentML').attr('disabled', '');
            $('#LocationML').attr('disabled', '');

            var togglecompanylist = $('#ResultRequest .togglecompany');
            BindCompanyButtons(togglecompanylist);
        },
        error: function(xhr, textStatus, thrownError) {
            $('#ResultRequest').html('');
            $.facebox('<div><h1>Error Processing</h1><a href="' + requestQuery + '">more info</a></div>');
            $('#IndustryML').attr('disabled', '');
            $('#InvestmentML').attr('disabled', '');
            $('#LocationML').attr('disabled', '');
            $('#ResultRequest').html('');
        }


    });
}

$(function() {
var path1 = document.location.pathname;
var path2 = path1.toLowerCase();

//if (document.location.pathname.indexOf('MassLead') >= 0 || document.location.pathname.indexOf('masslead') >= 0) {
if (path2.indexOf('viewall') >= 0) {
        $('#IndustryML').change(function() {
            LoadCompanyListing();
        });

        $('#InvestmentML').change(function() {
            LoadCompanyListing();
        });

        $('#LocationML').change(function() {
            LoadCompanyListing();
        });
        
     //   if (window.location.hostname != 'localhost')
        LoadCompanyListing();
    }

});
