﻿function CreatePostRadio(gRadio, cRadio, scRadio)
{
    if (gRadio != null)
        $get(gRadio).checked = true;
        
    if (cRadio != null)
        $get(cRadio).checked = true;
    else
    {
        for (var i = 0; i < document.forms[0].catRadio.length; i++)
        {
            document.forms[0].catRadio[i].checked = false;
        }
    }
    
    if (scRadio != null)
        $get(scRadio).checked = true;
    else
    {
        for (var i = 0; i < document.forms[0].subCatRadio.length; i++)
        {
            document.forms[0].subCatRadio[i].checked = false;
        }
    }
}


function UpdateSolutionRank(responseId, typeId)
{
    Kingpin.Services.Agora.Agora.UpdateSolutionType(responseId, typeId, UpdateSucceeded);
}

function DivMouseOver(div)
{
    div.style.backgroundColor = "#eff9da";
}

function DivMouseOut(div)
{
    div.style.backgroundColor = "#b2d281";
}

function BtnMouseOver(type, div)
{
    var path = "/App_Themes/TechIT/Images/";
    switch (type)
    {
        case "add":
            div.src = path + "btnAddContactOn.png";
            break;
        case "accepted":
            div.src = path + "btnAcceptSolutionOn.png";
            break;
        case "assisted":
            div.src = path + "btnAcceptAssistOn.png";
            break;
        case "acceptContact":
            div.src = path + "btnAcceptOn.png";
            break;
        case "rejectContact":
            div.src = path + "btnRejectOn.png";
            break;
        case "treeExpand":
            div.src = path + "treeCollapseOvr.png";
            break;
        case "treeCollapse":
            div.src = path + "treeFolderOvr.png";
            break;
        case "treeGo":
            div.src = path + "treeDocOvr.png";
            break;
        default:
            break;
    }
}

function BtnMouseOut(type, div)
{
    var path = "/App_Themes/TechIT/Images/";
    switch (type)
    {
        case "add":
            div.src = path + "btnAddContactOff.png";
            break;
        case "accepted":
            div.src = path + "btnAcceptSolutionOff.png";
            break;
        case "assisted":
            div.src = path + "btnAcceptAssistOff.png";
            break;
        case "acceptContact":
            div.src = path + "btnAcceptOff.png";
            break;
        case "rejectContact":
            div.src = path + "btnRejectOff.png";
            break;
        case "treeExpand":
            div.src = path + "treeCollapseOn.png";
            break;
        case "treeCollapse":
            div.src = path + "treeFolderOn.png";
            break;
        case "treeGo":
            div.src = path + "treeDocOn.png";
            break;
        default:
            break;
    }
}


function UpdateSucceeded(result, eventArgs)
{
    var info = result;
    var divName = "acceptSolution" + info[0];
    var resDivName = "topicRes" + info[0];
    
    if (info[1] == 1)
    {
        $get(divName).innerHTML = "Accepted Solution";
        $get(resDivName).style.backgroundColor = "#99FFAA";
    }
    else if (info[1] == 2)
    {
        $get(divName).innerHTML = "Assisted Solution";
        $get(resDivName).style.backgroundColor = "#4DC3FF";
    }
}


function SubscribeDropDown(subscriptionId, subscribe, bIsThread)
{
    var subDiv = $get("subscribe" + subscriptionId);
    
    if (subscribe == 1)
    {
        subDiv.innerHTML = "<select id=\"subDDDiv" + subscriptionId + "\" name=\"subDDDiv" + subscriptionId + "\"><option selected value=\"Yes\">Yes</option><option value=\"No\">No</option></select>";
    }
    else if (subscribe == 0)
    {
        subDiv.innerHTML = "<select id=\"subDDDiv" + subscriptionId + "\" name=\"subDDDiv" + subscriptionId + "\"><option value=\"Yes\">Yes</option><option selected value=\"No\">No</option></select>";
    }
    var subDD = $get("subDDDiv" + subscriptionId);
    var ddVal = subDD.value;
    
    if(document.addEventListener)
    {
        document.addEventListener('click', function(e) { ChangeSubscription(subscriptionId, ddVal, bIsThread); document.removeEventListener('click', arguments.callee, false); }, false);
        subDiv.addEventListener('click', CloseDropDown, false);
    }
    else if(document.attachEvent)
    {
        document.attachEvent('onclick', function(e) { ChangeSubscription(subscriptionId, ddVal, bIsThread); document.detachEvent('onclick', arguments.callee); });
        subDiv.attachEvent('onclick', CloseDropDown);
    }
}

function ChangeSubscription(subscriptionId, ddVal, bIsThread)
{    
    var subDiv = $get("subscribe" + subscriptionId);
    var subDD = $get("subDDDiv" + subscriptionId);
    var subDDVal = subDD.value;
    
    Kingpin.Services.Agora.Agora.ChangeSubscription(subscriptionId, ddVal, subDDVal, bIsThread);

    if (document.removeEventListener)
    {
        subDiv.removeEventListener('click', CloseDropDown, false);
    }
    else if (document.detachEvent)
    {
        
        subDiv.detachEvent('onclick', CloseDropDown);
    }
    if (subDDVal == "Yes")
        subDiv.innerHTML = "<a href=\"javascript:;\" onclick=\"SubscribeDropDown(" + subscriptionId + ", 1, '" + bIsThread + "');\">" + subDDVal + "</a>";
    else if (subDDVal == "No")
        subDiv.innerHTML = "<a href=\"javascript:;\" onclick=\"SubscribeDropDown(" + subscriptionId + ", 0, '" + bIsThread + "');\">" + subDDVal + "</a>";
}


function CloseDropDown(event)
{
    if (event.stopPropagation)
        event.stopPropagation();
    else if (window.event)
        window.event.cancelBubble = true;
}

function ToggleForum(discType, path, fId, bMainPage, show, pageTierLevel)
{
    var fadeOutDivName = "";
    
    switch (pageTierLevel)
    {
        case 1:
            fadeOutDivName = GetGroupId(discType, fId);
            break;
        case 2:
            fadeOutDivName = GetCategoryId(discType, fId);
            break;
        case 3:
            fadeOutDivName = GetSubCategoryId(discType, fId);
            break;
        case 4:
            fadeOutDivName = GetFourthTierId(discType, fId);
            break;
        default:
            break;
    }
        
    
    var fadeOutDiv = $get(fadeOutDivName);
    
    if (show == 1)
    {
        if (!CheckIfOpened(discType, fId, bMainPage))
        {
            if (bMainPage == "True")
            {

                var animation = new AjaxControlToolkit.Animation.FadeOutAnimation(fadeOutDiv, .1, 10, 0.4, 0.8, true);
                animation.play(fadeOutDiv);
                
                var loadImageAction = new AjaxControlToolkit.Animation.ScriptAction(fadeOutDiv, .1, 10, CreateLoadingImage(fadeOutDiv));
                
                var loadFadeSeq = new AjaxControlToolkit.Animation.SequenceAnimation(fadeOutDiv, .1, 10, [animation, loadImageAction], 1);
                loadFadeSeq.play();
            }

            var a = new Array();
            
            a[0] = path;
            a[1] = fId;
            a[2] = bMainPage;
            a[3] = pageTierLevel;
        
            switch (discType)
            {
                case 1:
                    Kingpin.Services.Agora.Agora.GetTierTwoForMenu(path, fId, bMainPage, pageTierLevel, SucceededExpandForumCategories, FailedExpandForumCategories, a);
                    break;
                case 2:
                    Kingpin.Services.Agora.Agora.GetTierThreeForMenu(path, fId, bMainPage, pageTierLevel, SucceededExpandForumCategories, FailedExpandForumCategories, a);
                    break;
                case 3:
                    Kingpin.Services.Agora.Agora.GetTierFourForMenu(path, fId, bMainPage, pageTierLevel, SucceededExpandForumCategories, FailedExpandForumCategories, a);
                    break;
                case 4:
                    Kingpin.Services.Agora.Agora.GetTierFiveForMenu(path, fId, bMainPage, pageTierLevel, SucceededExpandForumCategories, FailedExpandForumCategories, a);
                    break;
                default:
                    break;
            }
        }
        else
        {
            if (discType == 1)
            {
                divName = "group" + fId + "_cats";
                imgIcon = "group" + fId + "_icon";
                link = "group" + fId + "_link";
            }
            else if (discType == 2)
            {
                divName = "cat" + fId + "_subCats";
                imgIcon = "cat" + fId + "_icon";
                link = "cat" + fId + "_link";
            }
            else if (discType == 3)
            {
                divName = "subCat" + fId + "_tier4";
                imgIcon = "subCat" + fId + "_icon";
                link = "subCat" + fId + "_link";
            }
            else if (discType == 4)
            {
                divName = "tier4" + fId + "_tier5";
                imgIcon = "tier4" + fId + "_icon";
                link = "tier4" + fId + "_link";
            }
        
            var divObj = $get(divName);
            var imgObj = $get(imgIcon);
            var linkObj = $get(link);


            divObj.style.display = "block";
            imgObj.src = "/App_Themes/TechIT/Images/treeFolderOn.png";
            linkObj.onclick = function() { ToggleForum(discType, path, fId, bMainPage, 0, pageTierLevel); };
            imgObj.onmouseover = function() { BtnMouseOver("treeCollapse", imgObj); };
            imgObj.onmouseout = function() { BtnMouseOut("treeCollapse", imgObj); };
            
        }
    }
    else if (show == 0)
    {
        var divName;
        var imgIcon;
        var link;
        
        if (discType == 1)
        {
            divName = "group" + fId + "_cats";
            imgIcon = "group" + fId + "_icon";
            link = "group" + fId + "_link";
        }
        else if (discType == 2)
        {
            divName = "cat" + fId + "_subCats";
            imgIcon = "cat" + fId + "_icon";
            link = "cat" + fId + "_link";
        }
        else if (discType == 3)
        {
            divName = "subCat" + fId + "_tier4";
            imgIcon = "subCat" + fId + "_icon";
            link = "subCat" + fId + "_link";
        }
        else if (discType == 4)
        {
            divName = "tier4" + fId + "_tier5";
            imgIcon = "tier4" + fId + "_icon";
            link = "tier4" + fId + "_link";
        }
    
        var divObj = $get(divName);
        var imgObj = $get(imgIcon);
        var linkObj = $get(link);


        divObj.style.display = "none";
        imgObj.src = "/App_Themes/TechIT/Images/treeCollapseOn.png";
        linkObj.onclick = function() { ToggleForum(discType, path, fId, bMainPage, 1, pageTierLevel); };
        imgObj.onmouseover = function() { BtnMouseOver("treeExpand", imgObj); };
        imgObj.onmouseout = function() { BtnMouseOut("treeExpand", imgObj); };
    }
}

function removeOpacity(divName) {
	var e = $get(divName);
	
	if (e.style.removeAttribute) {
		e.style.removeAttribute('filter');
	}
	else {
		e.style.removeProperty('filter');
	}
	
	var popupObj = $get('loadingImg');
	
	if (popupObj)
	{
	    if (popupObj.parentElement)
            popupObj.parentElement.removeChild(popupObj);
        else if (popupObj.parentNode)
            popupObj.parentNode.removeChild(popupObj);
    }
}

function CheckIfOpened(discTypeId, fId, bMainPage)
{
    if (bMainPage == "True")
    {
        var divName = "";
        
        switch (discTypeId)
        {
            case 1:
                divName = "group" + fId + "_cats";
                break;
            case 2:
                divName = "cat" + fId + "_subCats";
                break;
            case 3:
                divName = "subCat" + fId + "_tier4";
                break;
            case 4:
                divName = "tier4" + fId + "_tier5";
                break;
            default:
                break;
        }
        if (GetFirstChild($get(divName)))
            return true;
        return false;
    }
    return false;
}

function GetGroupId(discTypeId, fId)
{
    var divName = "";
    switch (discTypeId)
    {
        case 1:
            divName = "group" + fId;
            break;
        case 2:
            divName = "cat" + fId;
            break;
        case 3:
            if ($get("subCat" + fId).parentElement)
                divName = $get("subCat" + fId).parentElement.id;
            else if ($get("subCat" + fId).parentNode)
                divName = $get("subCat" + fId).parentNode.id;
            break;
        case 4:
            if ($get("tier4" + fId).parentElement)
                divName = $get("tier4" + fId).parentElement.parentElement.id;
            else if ($get("tier4" + fId).parentNode)
                divName = $get("tier4" + fId).parentNode.parentNode.id;
            break;
        default:
            break;
    }
    
    return divName;
}

function GetCategoryId(discTypeId, fId)
{
    var divName = "";
    switch (discTypeId)
    {
        case 1:
            divName = "group" + fId;
            break;
        case 2:
            divName = "cat" + fId;
            break;
        case 3:
            divName = "subCat" + fId;
            break;
        case 4:
            if ($get("tier4" + fId).parentElement)
                divName = $get("tier4" + fId).parentElement.parentElement.id;
            else if ($get("tier4" + fId).parentNode)
                divName = $get("tier4" + fId).parentNode.parentNode.id;
            break;
        default:
            break;
    }
    
    return divName;
}

function GetSubCategoryId(discTypeId, fId)
{
   var divName = "";
    switch (discTypeId)
    {
        case 1:
            divName = "group" + fId;
            break;
        case 2:
            divName = "cat" + fId;
            break;
        case 3:
            divName = "subCat" + fId;
            break;
        case 4:
            divName = "tier4" + fId;
            break;
        default:
            break;
    }
    
    return divName;
}


function CreateLoadingImage(div)
{
    var bounds = Sys.UI.DomElement.getBounds(div);
    
    var image = document.createElement("img");
    image.style.border = "0";
    image.style.position = "absolute";
    image.style.zIndex = "5001";
    image.setAttribute("id", "loadingImg");
    image.src = "/App_Themes/TechIT/Images/agoraLoading.gif";
    
    Sys.UI.DomElement.setLocation(image, Math.floor((bounds.x + ((bounds.width / 2) - (image.width / 2)))), Math.floor((bounds.y + ((bounds.height / 2) - (image.height / 2)))));
    
    document.body.appendChild(image);
}

function FailedExpandForumCategories(error)
{
}