﻿var SearchCategoryKey = null;
var SearchSubCategoryKey = null;
var SearchLocationKey = null;
var SearchInvestmentLevelIDKey = null;

function AddFranchiseToCart(franchiseID, leadSourceID, bRet) {

    jQuery.get("/handlers/CartEngine.ashx?action=add&franchise_id=" + franchiseID + "&lsid=" + leadSourceID + "&rctp=" + bRet + "&ord=" + Math.floor(Math.random() * 10e12),
        function (data) {
            if (bRet) {
                ShowCart(data);
            }
        });
}

function RemoveFranchiseFromCart(franchiseID, bRet) {


    jQuery.get("/handlers/CartEngine.ashx?action=remove&franchise_id=" + franchiseID + "&rctp=" + bRet + "&ord=" + Math.floor(Math.random() * 10e12),
        function (data) {
            if (bRet) {
                ShowCart(data);
            }
        });
}
function AddToRemoveFromCart(franchiseID, leadSourceID, bRet) {

    jQuery.get("/handlers/CartEngine.ashx?action=addremove&franchise_id=" + franchiseID + "&lsid=" + leadSourceID + "&rctp=" + bRet + "&ord=" + Math.floor(Math.random() * 10e12),
        function (data) {
            if (bRet) {
                ShowCart(data);
            }
        });
}

function GetContactCart() {
    jQuery.get("/handlers/CartEngine.ashx?action=getcart" + "&ord=" + Math.floor(Math.random() * 10e12),
        function (data) {
            ShowCart(data);
        });
}

function EmptyContactCart() {
    jQuery.get("/handlers/CartEngine.ashx?action=empty" + "&ord=" + Math.floor(Math.random() * 10e12),
        function (data) {
            ShowCart(data);
        });
}

function ShowCart(sData) {
    if (sData.length > 10) {
        $('#MCSelectList').html(sData);
        $('#divModalFreeInfoText').hide();
        $('.selectFranchise').show();
        $('.selectFranchise').html("Selected Franchises: <span>" + $('#MCSelectList input').length + "</span>");
        $('#MCSelectList').show();
    } else {
        $('#divModalFreeInfoText').show();
        $('.selectFranchise').html("Selected Franchises: <span>" + $('#MCSelectList input').length + "</span>");
        $('.selectFranchise').hide();
        $('#MCSelectList').html('');
        $('#MCSelectList').hide();
    }
    //Bug #28796 To show the mininum liquid capital.
    CalculateMinimumCapital();
    ResetCheckedImages();

}

function ResetCheckedImages() {
    var formElements = $('#MCSelectList input:checked');
    var images = $('img[id^= imgAddFranchiseToCart]');

    // Reset all image buttons to default unchecked (green) state
    $('img[id^= imgAddFranchiseToCart]').attr('src', '/images/buttons/btn-add-to-free-info-list.png');
    $('img[id^= imgAddFranchiseToCart1]').attr('src', '/images/buttons/btn-add-to-free-info-list.png');

    //loop session cart items and see of there are checked input boxes then set checked (ungrayed) state
    if (formElements.length > 0) {
        for (var i = 0; i < formElements.length; i++) {
            var aParts = formElements[i].name.split("_");
            ChangeAddToFreeInfoListButtonImage('imgAddFranchiseToCart_' + aParts[1]);
            ChangeAddToFreeInfoListButtonImage('imgAddFranchiseToCart1_' + aParts[1]);
        }
    }
}

function CalculateMinimumCapital() {
    jQuery.get("/handlers/CartEngine.ashx?action=capital&ord=" + Math.floor(Math.random() * 10e12),
        function (data) {
            var min_max = 0;
            var min_max_str = "";
            var min_cap = 0;
            if (!isNaN(parseInt(data))) {
                if (parseInt(data) < parseInt(9999)) {
                    $('#divMLC').css("display", "none");
                }
                else {
                    min_max_str = FormatCurrency(data);
                    $('#divMLC').html("Min. Liquid Capital " + min_max_str);
                    $('#divMLC').css("display", "block");
                }
            } else {
                $('#divMLC').css("display", "none");
            }
        });
}

function CheckYESAdditionalConsultationImage(objId) {
    try {

        objId = ("#" + objId.id);

        var isChecked = ($(objId).attr('src') == "/images/buttons/checked.png") ? true : false;
        $(objId).attr('src', isChecked ? '/images/buttons/unchecked.png' : '/images/buttons/checked.png');

        //this will be hidden and so get checkbox id and set checked/unchecked
        var chkBoxId = (objId.split('_').reverse())[0];
        chkBoxId = objId.replace('imgAdditionalSelected_', "SSControl_" + chkBoxId + '_chkAdditionalSelected_');

        if (!isChecked) {
            $(chkBoxId).attr('checked', 'checked');
            $("#ctl00_plcBody_additionalfranchisesCheckbox").attr('checked', 'checked');
        }
        else {
            $(chkBoxId).removeAttr('checked');
            $("#ctl00_plcBody_additionalfranchisesCheckbox").removeAttr('checked');
        }

    }
    catch (e) { }
}

function FormatCurrency(num) {
    num = num.toString().replace(/\$|\,/g, '');

    if (isNaN(num))
        num = "0";

    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num * 100 + 0.50000000001);
    cents = num % 100;
    num = Math.floor(num / 100).toString();
    if (cents < 10)
        cents = "0" + cents;

    for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)
        num = num.substring(0, num.length - (4 * i + 3)) + ',' + num.substring(num.length - (4 * i + 3));
    return (((sign) ? '' : '-') + '$' + num);  // + '.' + cents
}

function ChangeAddToFreeInfoListButtonImage(objID) {
    try {
        objID = '#' + objID;
        var isChecked = ($(objID).attr('src') == "/images/buttons/btn-on-free-info-list.png") ? true : false;
        $(objID).attr('src', isChecked ? '/images/buttons/btn-add-to-free-info-list.png' : '/images/buttons/btn-on-free-info-list.png');
    }
    catch (e) { }
}

function IsCheckedAddToFreeInfoListButtonImage(objID) {

    return ($('#' + objID).attr('src') == "/images/buttons/btn-on-free-info-list.png");

}

function PerformSearch(bModalWin) {
    var qs = '';
    var ddlCat = $('#ctl00_plcBody_Finder_FranchiseCategoryDropDownList').val();
    var ddlSubCat = $('#ctl00_plcBody_Finder_FranchiseSubCategoryDropDownList').val();
    var ddlST = $('#ctl00_plcBody_Finder_FranchiseRegionDropDownList1').val();
    var ddlIL = $('#ctl00_plcBody_Finder_FranchiseInvestmentLevelDropDownList1').val();

    var path = '/handlers/searchredirector.ashx';

    if (ddlCat != '') {
        if (qs != '')
            qs += '&' + SearchCategoryKey + '=' + ddlCat;
        else
            qs = SearchCategoryKey + '=' + ddlCat;
    }
    if (ddlSubCat != '') {
        if (qs != '')
            qs += '&' + SearchSubCategoryKey + '=' + ddlSubCat;
        else
            qs = SearchSubCategoryKey + '=' + ddlSubCat;
    }
    if (ddlST != '') {
        if (qs != '')
            qs += '&' + SearchLocationKey + '=' + ddlST;
        else
            qs = SearchLocationKey + '=' + ddlST;
    }
    if (ddlIL != '') {
        if (qs != '')
            qs += '&' + SearchInvestmentLevelIDKey + '=' + ddlIL;
        else
            qs = SearchInvestmentLevelIDKey + '=' + ddlIL;
    }
    if (bModalWin && bModalWin != '') {
        if (qs != '')
            qs += '&bModalWin=1';
        else
            qs = 'bModalWin=1';
    }

    window.location = qs != '' ? (path + '?' + qs) : path;
}
function InitValues(catKey, subcatKey, locationKey, invlevelKey) {
    SearchCategoryKey = catKey;
    SearchSubCategoryKey = subcatKey;
    SearchLocationKey = locationKey;
    SearchInvestmentLevelIDKey = invlevelKey;
}

function GetContentModal(franchiseID, sTitle, hideAddToLink) {

    var sAddToLink = "<h1><a href=\"javascript:void(0);\" onclick=\"AddToRemoveFromCart(" + franchiseID + ",5110, true);$.fn.colorbox.close();\">Add to Free Info List &raquo;&raquo;</a></h1>";

    var cb = $.colorbox({ width: '800px', height: '600px', overlayClose: false, opacity: 0.75, speed: 500 });

    jQuery.get("/handlers/CartEngine.ashx?action=modal&franchise_id=" + franchiseID + "&ord=" + Math.floor(Math.random() * 10e12),
        function (stringResult) {

            var sHTML = "";
            if (typeof (hideAddToLink) != 'undefined' && hideAddToLink != null && hideAddToLink == "true") {
                sHTML = stringResult;
            }
            else {
                sHTML = sAddToLink + stringResult + sAddToLink;
            }

            /*
            onComplete - disables body scrolling
            onClosed - enables body scrolling
            */
            $.colorbox({ title: ((!sTitle) ? '' : sTitle),
                html: sHTML,
                width: '800px',
                height: '600px',
                overlayClose: false,
                opacity: 0.75,
                speed: 500,
                onComplete: function () {
                    if ($.browser.msie && document.documentMode != 7)
                        $("body").css("overflow", "hidden");

                    if (!$.browser.msie)
                        $("body").css("overflow", "hidden");
                    $("body").attr("scroll", "no");
                },
                onClosed: function () {
                    if ($.browser.msie && document.documentMode != 7)
                        $("body").css("overflow", "auto");

                    if (!$.browser.msie)
                        $("body").css("overflow", "auto");

                    $("body").attr("scroll", "yes");
                }
            });
        });

    return false;

}
