﻿/// <reference name="MicrosoftAjax.js"/>

function RemoveCompany(button) {
    var thisCompanyId = button.attr("companyid");
    RemoveCompanyById(thisCompanyId)
}

function RemoveCompanyById(thisCompanyId) {

    $(".company_" + thisCompanyId).remove();
    var togglecompany = $(".togglecompany_" + thisCompanyId);
    togglecompany.attr("src", "/Images/Buttons/RequestInfo.gif");
    togglecompany.attr("alt", "Request Info");

    var togglecompanyprofile = $(".togglecompanyprofile_" + thisCompanyId);
    togglecompanyprofile.attr("src", "/Images/Buttons/RequestInfoProfile.png");
    togglecompanyprofile.attr("alt", "Request Info");
    
    SetCompaniesCookie(thisCompanyId, null);
}


function RemoveCompanyByIdGroup(thisCompanyId, GroupId) 
{
    $(".company_" + thisCompanyId).remove();
    var togglecompany = $(".togglecompany_" + thisCompanyId);
    togglecompany.attr("src", "/Images/Buttons/RequestInfo.gif");
    togglecompany.attr("alt", "Request Info");

    var togglecompanyprofile = $(".togglecompanyprofile_" + thisCompanyId);
    togglecompanyprofile.attr("src", "/Images/Buttons/RequestInfoProfile.png");
    togglecompanyprofile.attr("alt", "Request Info");

    SetCompaniesCookie(thisCompanyId, null);
    var countCompanies = 0;
    countCompanies = $("." + GroupId).length - 1;
    if (countCompanies == 0)
     $("." + GroupId).hide(); 
        
 
}

function FillCart() {
    var companies = GetCompaniesFromCookie();
    var count = 0;
    if (companies != null) {

        for (var c in companies) {
            if (!CompanyIdExistsInHistoryCookie(companies[c].CompanyId)) {
                AddCompanyToList(c, companies[c]);
                count++;
            }
        }

        //if (document.location.pathname == '/Home/RequestSubmission') {
        if (document.location.pathname == '/RequestSubmission') {
            $(".submitrequests").hide();
            $(".submitrequestsmessage").show();
        }
        
        if (count > 0) 
            $(".requestscart").show();
    }
    
}

function GetCompaniesFromCookieName(COOKIE_NAME) {
    if ($.cookie(COOKIE_NAME) == null)
    { return null; }
    else
    { return JSON.parse($.cookie(COOKIE_NAME)); }
    
    //return JSON.parse($.cookie(COOKIE_NAME));
}

function GetCompaniesFromCookie() {
    var COOKIE_NAME = 'companies_cookie';
    return GetCompaniesFromCookieName(COOKIE_NAME);

}

function GetCompaniesFromHistoryCookie() {
    var COOKIE_NAME = 'companies_cookie_history';
    return GetCompaniesFromCookieName(COOKIE_NAME);

}

function CompanyIdExistsInHistoryCookie(companyid) {
    //alert('checking if exists..');
    //var intCompanyId = parseInt(companyid)
    try {
        var companies = GetCompaniesFromHistoryCookie();
        for (var c in companies) {
            //alert(c + ':' + companies[c] + ':' + intCompanyId + ':' + companyid);
            if (companies[c] == companyid) {
                //alert('match');
                return true;

            }
        }
        return false;
    }
    catch (ex) {
        
        return false;
    }
}



function SetCompaniesCookie(thisCompanyId, thisCompany) {
    var COOKIE_NAME = 'companies_cookie';
    var companies = GetCompaniesFromCookieName(COOKIE_NAME);
    SetCompaniesCookieByName(COOKIE_NAME, thisCompanyId, thisCompany, companies);
}

function SetCompaniesHistoryCookie(thisCompanyId, thisCompany) {
    var COOKIE_NAME = 'companies_cookie_history';
    var companies = GetCompaniesFromCookieName(COOKIE_NAME);
    SetCompaniesCookieByName(COOKIE_NAME, thisCompanyId, thisCompany, companies);
    
}


function SetCompaniesCookieByName(COOKIE_NAME, thisCompanyId, thisCompany, companies) {
    try {
        //alert('here 1 ' + thisCompanyId);
        // get cookie

        var date = new Date();
        date.setTime(date.getTime() + (60 * 60 * 1000));

        //alert(date);
        
        var options = { path: '/', expires: date };

        var strcompanies = $.cookie(COOKIE_NAME);

        if (strcompanies != null)
        { companies = JSON.parse(strcompanies); }
        else
        { companies == null }
        //companies = JSON.parse(strcompanies);

        //alert('here 2 ' + thisCompanyId);
    
        if (companies == null)
            companies = new Object();
            
        if (thisCompany != null)
            companies[thisCompanyId] = thisCompany;
        else
            delete companies[thisCompanyId];

        var companiescount = 0;
        //alert('here 3 ' + thisCompanyId);
        for (c in companies) {
            companiescount++;
        }
        if (companiescount == 0)
            $(".requestscart").hide();

        //alert('here 4 ' + thisCompanyId);
        //AlertCompanies(companies);
        // set cookie
        var strcompanies_save = JSON.stringify(companies);
        //var companies2 = GetCompaniesFromString(strcompanies_save);

        //alert('saving these companies');
        // alert(strcompanies_save);

        $.cookie(COOKIE_NAME, strcompanies_save, options);

        //alert($.cookie(COOKIE_NAME));
        
        //alert("set " + COOKIE_NAME + " : " + strcompanies_save);
    }
    catch (ex) {
        $.facebox('error saving cookie : ' + ex.description);
    }
}


function AddCompanyToList(thisCompanyId, thisCompany) {
    var selector = $(".company_" + thisCompanyId);
    if (selector.length == 0) {
        var companyname = ''
        if (thisCompany.CompanyName.indexOf(' ', 30) > 0)
            companyname = thisCompany.CompanyName.substring(0, thisCompany.CompanyName.indexOf(' ', 30));
        else
            companyname = thisCompany.CompanyName;

//        alert(JSON.stringify(thisCompany));
        //todo: maybe use a jtemplate
        $(".requests_container").append(
                    '<div class="RequestTab company_' + thisCompanyId + '">' +
                        '<div class="RequestTabInner"> ' +
                            '<div style="float: left">' + companyname + '</div> ' +
                            '<div style="float: right; padding-top: 1px"> ' +
                                '<img class="remove_' + thisCompanyId + ' submitrequests"' +
                                    'src="/Images/Buttons/Remove.png" ' +
                                    'width="14" height="14" ' +
                                    'style="cursor:pointer" alt="Remove" ' +
                                    "company='" + JSON.stringify(thisCompany) + "'>" + // use single quotes to wrap company element because JSON.stringify uses double quotes
                                '</img>' +
                                '<br />' +
                            '</div>' +                         
                        '</div>' +
                    '</div>'
                );

        $(".requests_container_main").append(
                    '<div class="RequestTabMain company_' + thisCompanyId + '">' +
                        '<div class="RequestTabInner"> ' +
                            '<div style="float: left">' + thisCompany.CompanyName + '</div> ' +
                            '<div style="float: right; padding-top: 1px"> ' +
                                '<img class="remove_' + thisCompanyId + ' submitrequests"' +
                                    'src="/Images/Buttons/Remove.png" ' +
                                    'width="14" height="14" ' +
                                    'style="cursor:pointer" alt="Remove" ' +
                                    "company='" + JSON.stringify(thisCompany) + "'>" + // use single quotes to wrap company element because JSON.stringify uses double quotes
                                '</img>' +
                                '<br />' +
                            '</div>' +
                        '</div>' +
                    '</div>'
                );
        
        $(".requests_container_medium").append(
                    '<div class="RequestTabMedium company_' + thisCompanyId + '">' +
                        '<div class="RequestTabInner"> ' +
                            '<div style="float: left">' + thisCompany.CompanyName + '</div> ' +
                            '<div style="float: right; padding-top: 1px"> ' +
                                '<img class="remove_' + thisCompanyId + ' submitrequests"' +
                                    'src="/Images/Buttons/Remove.png" ' +
                                    'width="14" height="14" ' +
                                    'style="cursor:pointer" alt="Remove" ' +
                                    "company='" + JSON.stringify(thisCompany) + "'>" + // use single quotes to wrap company element because JSON.stringify uses double quotes
                                '</img>' +
                                '<br />' +
                            '</div>' +
                        '</div>' +
                    '</div>'
                );

        $('.remove_' + thisCompanyId).click(function() {
                // remove when this company does not exist on this page.
//                alert('here');
                var strcompany = $(this).attr('company');
                //alert(strcompany);
                var company = JSON.parse(strcompany);                
                //alert(company.CompanyId);
                RemoveCompanyById(company.CompanyId);
        });

        var togglecompany = $(".togglecompany_" + thisCompanyId);
        togglecompany.attr("src", "/Images/Buttons/CompanyAdded.gif");
        togglecompany.attr("alt", "Added");

        var togglecompanyprofile = $(".togglecompanyprofile_" + thisCompanyId);
        togglecompanyprofile.attr("src", "/Images/Buttons/CompanyAddedProfile.png");
        togglecompanyprofile.attr("alt", "Added");
    }
    else {
        //$.facebox('<div class="BodyLarger">You have already added this company.</div>');
    }
}

function AddCompany(button) {
    var thisCompany = new Object();    
    thisCompany.CompanyId = button.attr("companyid");
    thisCompany.CompanyName = button.attr("companyname");
    thisCompany.NetworkId = button.attr("networkid");
    thisCompany.CompanyListingId = button.attr("CompanyListingId");
    thisCompany.SourceRequest = (document.location.pathname == "/" ? "Index" : document.location.pathname);
    //alert(thisCompanyName + ':' + thisCompanyId);
    
    AddCompanyToList(thisCompany.CompanyId, thisCompany);
    $(".requestscart").show();
    SetCompaniesCookie(thisCompany.CompanyId, thisCompany);
};

function BindCompanyButtons(togglecompanylist) {
    //alert(togglecompanylist.length);
    if (togglecompanylist.length > 0) {
        //alert(togglecompanylist.length);
        togglecompanylist.click(function() {
            try {

                var companyid = $("img", this).attr("companyid");

                // do nothing if company already exists in history
                if (CompanyIdExistsInHistoryCookie(companyid))
                    return;

                var selector = $(".company_" + companyid);
                var COOKIE_NAME = 'LeadID';
                var existsCookieLead = $.cookie(COOKIE_NAME);
                //var doAsync = (existsCookieLead != null) && (document.location.pathname.indexOf('MassLead') >= 0);
                var doAsync = (existsCookieLead != null);
                //alert(existsCookieLead);
                //alert(doAsync);
                // take action

                if (doAsync) {
                    RequestSubmissionLead($("img", this));
                }
                else {

                    if (selector.length == 0)
                        AddCompany($("img", this));
                    else
                        RemoveCompany($("img", this));
                }
            }
            catch (ex) {
                alert(ex.message);
            }
        });
    }
}


$(document).ready(function() {
    var togglecompanylist = $('.togglecompany');
    BindCompanyButtons(togglecompanylist);
});


