﻿
var _updateType;
var _userType;
var _NAICSAction;
var _NAICSPosition = 1;
var _NAICSNodeCount = 1;
var _PRCAction;
var _PRCPosition = 2;
var _PRCNodeCount = 1;
var _NAICSShow = 0;

/*--------------------------------------------------------------- */
function rdNodeTreeInit(treeID) {

    if ($(treeID + '_start_node')) {
        rdGetTreeNodes($(treeID + '_start_node').value);
    }
}

/*--------------------------------------------------------------- */
function rdGetTreeNodes(nodeid) {
    // extract the node id so we can pass that to the retrieval operation
    var arr = nodeid.split('_');

    // expecting 4 parts to the node id
    var treeid = arr[0];
    var type = arr[1];
    var id = arr[3];
    var sid = GetSearchID();
    var cid = GetCollectionID();
    var cccid = GetCallCampaignCompanyID();
    var comid = GetCompanyID();
    var isUpdate = GetUpdateStatus();

    // show the loader
    rdShowLoader(treeid);

    // create prototype request
    new Ajax.Request(_appPath + 'callbacks/Trees/GetTreeNodes.aspx',
	{
	    method: 'post',
	    parameters: { tree_id: treeid, node_id: id, collection_id: cid, search_id: sid, call_campaign_company_id: cccid, company_id: comid, type_id: type, is_update: isUpdate },
	    onSuccess: function(transport) {
	        // rdSearchSelectionSuccess(tree_id, node_id, collection_id, type_id);
	        rdDisplayTreeNodes(nodeid, transport.responseText);
	    },
	    onFailure: function(transport) {
	        rdHideLoader();
	        alert('Error retrieving tree nodes');
	    }
	});
}

/*--------------------------------------------------------------- */
function rdDisplayTreeNodes(nodeid, retVal) {

    var arr = nodeid.split('_');
    var treeid = arr[0];
    var type = arr[1];
    rdHideLoader(treeid);

    if ($(nodeid)) {
        $(nodeid).innerHTML = retVal;
        rdAssignTreeClickActions(treeid, type, nodeid);
    }

    if ($('naicsupdate')) {

        if (treeid == 'naics' && _NAICSAction == 0) {
            rdShowSelectedNAICSNodesforUpdate(treeid, type);
        }
        else if (treeid == 'prc' && _PRCAction == 0) {
            rdShowSelectedPRCNodesforUpdate(treeid, type);
        }
    }

    /*if (_NAICSAction == 1 && _NAICSShow == 1) {        
        rdDoAction('naics_3_s_0', treeid, type, 0)
    }*/
}

/*--------------------------------------------------------------- */
function rdAssignTreeClickActions(treeid, type, nodeid) {

    var arr = $$('#' + treeid + '_TreeContainer' + ' li');

    //clear all previous events
    //Event.unloadCache();

    for (var x = 0; x < arr.length; x++) {
        // need to grab the first child and check it's class. should be the div
        var obj = arr[x].firstDescendant();

        // assign the Tree Node Click event to the icon div
        if ("p" == obj.className || "m" == obj.className || "f" == obj.className) {
            obj.onclick = rdTreeNodeClick;
        }

        var objSplit = obj.id.split('_');

        if (objSplit.length == 4) {
            // assign the Select / Unselect action to the check boxes
            if ($(treeid + '_' + type + '_chk_' + objSplit[3])) {
                $(treeid + '_' + type + '_chk_' + objSplit[3]).onclick = rdIWasClicked;
            }
        }
    }

    if (!$('naicsupdate')) {
        //automatically select all children and current parent
        if ((nodeid.startsWith('prc')) && nodeid.indexOf('_root') == -1) {
            $(nodeid.replace("_n_", "_chk_")).checked = true;

            var arr = $$('#' + nodeid + ' li');
            var isSelected = false;

            //clear all previous events
            //Event.unloadCache();

            //check if any one is selected
            for (var x = 0; x < arr.length && !isSelected; x++) {
                // assign the Select / Unselect action to the check boxes
                var chkboxes = $$("#" + arr[x].id + " input");

                for (var k = 0; k < chkboxes.length; k++) {
                    if (chkboxes[k].checked) {
                        isSelected = true;
                        break;
                    }
                }
            }

            //if no child is checked, check all
            if (!isSelected) {
                for (var x = 0; x < arr.length; x++) {
                    // assign the Select / Unselect action to the check boxes
                    var chkboxes = $$("#" + arr[x].id + " input");

                    if (chkboxes.length > 0) {
                        $(chkboxes[0]).click();
                    }
                }
            }

        }
    }
    else if (_NAICSAction == 1 && _PRCAction == 1) {
        rdAssignTreeClickActionsforUpdate(nodeid);
    }
}

/*--------------------------------------------------------------- */
function rdIWasClicked(osrc, evElement) {

    var obj = '';

    if (!osrc) {
        osrc = window.event.srcElement;
        window.event.cancelBubble = true; 	// cancel the bubble in IE
    }
    else {
        osrc.cancelBubble = true; // cancel the bubble in NN
    }

    // now we need to detect the element that was clicked, it's tag name and id. Have to
    // cater for Netscape and IE cos they deal with the detection differently!
    if (!evElement) {
        // Netscape
        evElement = this;
        obj = evElement;
    }
    else {
        // IE
        obj = osrc;
    }

    var arr = obj.id.split('_');
    var treeID = arr[0];
    var type = arr[1];
    var value = arr[3];


    if ($('naicsupdate')) {
        /* if (treeID == "prc" && $('naics_3_s_0').className == "m") {
            $('naics_3_s_0').className = "f";
            $('naics_3_ul_0').className = "hide";
        }*/
        
        rdChildNodeSelectionsforUpdate(obj.parentNode.previousSibling.id);
    }
    else {
        if (obj.checked) {
            rdSelectNode(obj.id, 1);
            if ($(treeID + '_' + type + '_nt_' + value)) {
                $(treeID + '_' + type + '_nt_' + value).className = "nts";
            }


            if (obj.parentNode.previousSibling.className == "f") {
                var divParent = obj.parentNode.previousSibling;

                if (divParent.dispatchEvent) {
                    //onclick to work in mozilla
                    //ref:http://forums.asp.net/p/987493/1643371.aspx

                    var e = document.createEvent("MouseEvents");

                    e.initEvent("click", true, true);
                    divParent.dispatchEvent(e);
                }
                else {
                    //in IE
                    divParent.click();
                }
            }
        }
        else {
            rdSelectNode(obj.id, 2);
            if ($(treeID + '_' + type + '_nt_' + value)) {
                $(treeID + '_' + type + '_nt_' + value).className = "nt";
            }
        }
    }
}

/*---------------------------------------------------------------
name        : fst_CategoryClick()
description : 
----------------------------------------------------------------- */
function rdTreeNodeClick(osrc, evElement) {
    var obj = '';

    if (!osrc) {
        osrc = window.event.srcElement;
        window.event.cancelBubble = true; 	// cancel the bubble in IE
    }
    else {
        osrc.cancelBubble = true; // cancel the bubble in NN
    }

    // now we need to detect the element that was clicked, it's tag name and id. Have to
    // cater for Netscape and IE cos they deal with the detection differently!
    if (!evElement) {
        // Netscape
        evElement = this;
        obj = evElement;
    }
    else {
        // IE
        obj = osrc;
    }

    var clickedItemID = obj.id;
    var arr = obj.id.split('_');
    var treeID = arr[0];
    var type = arr[1];
    var value = arr[3];

    if ("DIV" == obj.tagName) {
        switch (obj.className) {
            case "f":
                rdGetTreeNodes(treeID + '_' + type + '_n_' + value);
                break;

            case "m":
                if ($(treeID + '_' + type + '_ul_' + value)) {
                    $(treeID + '_' + type + '_ul_' + value).className = "hide";
                }

                obj.className = "p";

                break;

            case "p":
                if ($(treeID + '_' + type + '_ul_' + value)) {
                    $(treeID + '_' + type + '_ul_' + value).className = "show";
                }

                obj.className = "m";

                break;
        }
    }
}

// ---------------------------------------------------------------------------------------
function rdShowHideTreeforUpdate(obj) {

    var arr = obj.split('_');
    var treeID = arr[0];
    var type = arr[1];
    var value = arr[3];

    if (treeID == "naics" && $(obj).className == "f") {
        _NAICSAction = 0;
        _NAICSNodeCount = 1;
        _NAICSShow = 1;

        rdGetSelectedNodes(type);        
        rdNodeTreeInit('naics');
    }
    else {
        rdDoAction(obj, treeID, type, value);
    }
}

// ---------------------------------------------------------------------------------------
function rdDoAction(obj, treeID, type, value) {

    if (treeID == "naics" && $(obj).className == "f") {
        _NAICSShow = 0;
    }
    
    switch ($(obj).className) {

        case "f":
            if ($(treeID + '_' + type + '_ul_' + value)) {
                $(treeID + '_' + type + '_ul_' + value).className = "show";
            }

            $(obj).className = "m";
            break;

        case "m":
            if ($(treeID + '_' + type + '_ul_' + value)) {
                $(treeID + '_' + type + '_ul_' + value).className = "hide";
            }

            $(obj).className = "f";
            break;
    }
}

// ---------------------------------------------------------------------------------------
function rdShowPRCCodesPopup(callCampaignCompanyID, companyID, type) 
{
    if ($('naicscode').value == 'success') 
    {
        url = _appPath + "MappingCodes/UpdatePRCCodes.aspx?cccid=" + callCampaignCompanyID + "&comid=" + companyID + "&type=" + type;
        popup(url, 560, 930, 'popupwindow');
    }
    else 
    {
        alert("Please select NAICS Information.");
    }
}

// ---------------------------------------------------------------------------------------
function rdShowAdminPRCCodesPopup(companyID, type) 
{
    if ($('naicscode').value == 'success') 
    {
        url = _appPath + "MappingCodes/Admin/UpdatePRCCodes.aspx?comid=" + companyID;
        popup(url, 560, 930, 'popupwindow');
    }
    else 
    {
        alert("Please select NAICS Information.");
    }
}

// ---------------------------------------------------------------------------------------
function rdShowSelectedNAICSNodesforUpdate(treeid, type) {
    var selectedNodes = $('selectedNAICSNodes').value;
    var nodeArr = selectedNodes.split('|');

    if (nodeArr.length == 1) {
        _NAICSAction = 1;
        return;
    }

    if (_NAICSNodeCount == (nodeArr.length - 1)) {
        _NAICSAction = 1;
        return;
    }

    var childArr = nodeArr[_NAICSNodeCount].split('/');

    if (_NAICSPosition < childArr.length) {
        var ctrl = $(treeid + '_' + type + '_s_' + childArr[_NAICSPosition]);

        if (ctrl != null) {
            var id = childArr[_NAICSPosition];
            var obj = $(treeid + '_' + type + '_s_' + id);

            _NAICSPosition++;

            switch (obj.className) {
                case "f":
                    if (obj.dispatchEvent) {
                        //onclick to work in mozilla
                        //ref:http://forums.asp.net/p/987493/1643371.aspx

                        var e = document.createEvent("MouseEvents");

                        e.initEvent("click", true, true);
                        obj.dispatchEvent(e);
                    }
                    else {
                        //in IE
                        obj.click();
                    }
                    break;

                default:
                    rdShowSelectedNAICSNodesforUpdate(treeid, type);
                    break;
            }
        }
        else {
            _NAICSPosition = 1;
            _NAICSNodeCount++;
            rdShowSelectedNAICSNodesforUpdate(treeid, type);
        }
    }
}

// ---------------------------------------------------------------------------------------
function rdShowSelectedPRCNodesforUpdate(treeid, type) {
    var selectedNodes = $('selectedPRCNodes').value;
    var nodeArr = selectedNodes.split('|');

    if (nodeArr.length == 1) {
        _PRCAction = 1;
        return;
    } 
    
    if (_PRCNodeCount == (nodeArr.length - 1)) {
        _PRCAction = 1;
        return;
    }    
    
    var childArr = nodeArr[_PRCNodeCount].split('/');

    if (_PRCPosition < childArr.length) {
        var ctrl = $(treeid + '_' + type + '_s_' + childArr[_PRCPosition]);

        if (ctrl != null) {
            var id = childArr[_PRCPosition];
            var obj = $(treeid + '_' + type + '_s_' + id);

            _PRCPosition++;

            switch (obj.className) {
                case "f":
                    if (obj.dispatchEvent) {
                        //onclick to work in mozilla
                        //ref:http://forums.asp.net/p/987493/1643371.aspx

                        var e = document.createEvent("MouseEvents");

                        e.initEvent("click", true, true);
                        obj.dispatchEvent(e);
                    }
                    else {
                        //in IE
                        obj.click();
                    }
                    break;

                default:
                    rdShowSelectedPRCNodesforUpdate(treeid, type);
                    break;
            }
        }
        else {
            _PRCPosition = 2;
            _PRCNodeCount++;
            rdShowSelectedPRCNodesforUpdate(treeid, type);
        }
    }
}

// ---------------------------------------------------------------------------------------
function rdChildNodeSelectionsforUpdate(id) {

    var arr = $$('#' + id.replace("_s_", "_n_") + ' input');
    var isChecked = $(id.replace("_s_", "_chk_")).checked;

    for (var i = 0; i < arr.length; i++) {

        var chk = $(arr[i]).id.split('_');
        var treeID = chk[0];
        var type = chk[1];
        var value = chk[3];

        var divParent = $(arr[i]).parentNode.previousSibling;
        var ulParent = $(arr[i]).parentNode.previousSibling.id.replace("_s_", "_ul_");
        
        if (isChecked) {
            rdSelectNode($(arr[i]).id, 1);
            $(arr[i]).checked = true;

            if ($(treeID + '_' + type + '_nt_' + value)) {
                $(treeID + '_' + type + '_nt_' + value).className = "nts";
            }

            if ($(arr[i]).parentNode.previousSibling.className == "f") {              
                if (divParent.dispatchEvent) {
                    //onclick to work in mozilla
                    //ref:http://forums.asp.net/p/987493/1643371.aspx

                    var e = document.createEvent("MouseEvents");

                    e.initEvent("click", true, true);
                    divParent.dispatchEvent(e);
                }
                else {
                    //in IE
                    divParent.click();
                }
            }
            else if($(arr[i]).parentNode.previousSibling.className == "p") {
                $(divParent).className = "m";
                $(ulParent).className = "show";
            }
        }
        else {
            rdSelectNode($(arr[i]).id, 2);
            $(arr[i]).checked = false;

            if ($(treeID + '_' + type + '_nt_' + value)) {
                $(treeID + '_' + type + '_nt_' + value).className = "nt";
            }

            if ($(arr[i]).parentNode.previousSibling.className == "p") {
                $(divParent).className = "m";
                $(ulParent).className = "show";
            }
        }
    }
}

// ---------------------------------------------------------------------------------------
function rdAssignTreeClickActionsforUpdate(nodeid) {
    if ((nodeid.startsWith('prc') || nodeid.startsWith('naics') || nodeid.startsWith('sic')) && nodeid.indexOf('_root') == -1) {

        //automatically select all children when current parent is checked
        if ($(nodeid.replace("_n_", "_chk_")).checked == true) {

            var arr = $$('#' + nodeid + ' li');
            var isSelected = false;

            //clear all previous events
            //Event.unloadCache();

            //check if any one is selected
            for (var x = 0; x < arr.length && !isSelected; x++) {
                // assign the Select / Unselect action to the check boxes
                var chkboxes = $$("#" + arr[x].id + " input");

                for (var k = 0; k < chkboxes.length; k++) {
                    if (chkboxes[k].checked) {
                        isSelected = true;
                        break;
                    }
                }
            }

            //if no child is checked, check all
            if (!isSelected) {
                for (var x = 0; x < arr.length; x++) {
                    // assign the Select / Unselect action to the check boxes
                    var chkboxes = $$("#" + arr[x].id + " input");

                    if (chkboxes.length > 0) {
                        $(chkboxes[0]).click();
                    }
                }
            }
        }
        else {
            var arr = $$('#' + nodeid + ' li');

            //if no child is collapsed, expand all
            for (var x = 0; x < arr.length; x++) {

                var cltrs = $$("#" + arr[x].id + " div");

                if (cltrs.length > 0) {
                    if ($(cltrs[0]).dispatchEvent) {
                        //onclick to work in mozilla
                        //ref:http://forums.asp.net/p/987493/1643371.aspx

                        var e = document.createEvent("MouseEvents");

                        e.initEvent("click", true, true);
                        $(cltrs[0]).dispatchEvent(e);
                    }
                    else {
                        //in IE
                        $(cltrs[0]).click();
                    }
                }
            }
        }
    }
}

// ---------------------------------------------------------------------------------------
function rdGetSelectedNodes(type) {
    var id = (_userType) == "Admin" ? GetCompanyID() : GetCallCampaignCompanyID();
    var actionUrl = _appPath + "Callbacks/Trees/GetSelectedTreeNodes.aspx";

    new Ajax.Request(actionUrl,
    {
        method: 'post',
        parameters: { id: id, type: type, userType:_userType },
        onSuccess: function(transport) {
        rdGetSelectedNodesSuccess(transport.responseText);
        },
        onFailure: function(transport) {
            alert('Error selecting tree node212');
        }
    });
}

// ---------------------------------------------------------------------------------------
function rdGetSelectedNodesSuccess(retVal) {
    if (retVal.substring(0, 7) == "success") {
        $('selectedNAICSNodes').value = retVal.substring(8);
    }
}

/*---------------------------------------------------------------
name        : rdSelectNode()
description : 
----------------------------------------------------------------- 
function rdSelectNode(objectID, action) {
var actionUrl = _appPath;
var arr = objectID.split('_');
var treeID = arr[0];
var type = arr[1];
var value = arr[3];
var sid = GetSearchID();

actionUrl += "Callbacks/Search/UpdateSearchCriteria.aspx";

new Ajax.Request(actionUrl,
{
method: 'post',
parameters: { search_id: sid, type_id: type, action: action, value: value },
onSuccess: function(transport) {
rdSearchSelectionSuccess(transport.responseText);
},
onFailure: function(transport) {
alert('Error selecting tree node');
}
});
}
*/