﻿var rbActionState = new Array();
var divQuestionRemarkStatus = 'none';

function showRecurringTypes(showType)
{
    $(".recurringTypes")[0].style.display = showType;
}

function removeItem(item)
{
    return confirm('Weet u zeker dat u het dit item wil verwijderen?');
}

function disableRadioButtons()
{
    var clientId = $('.txtClientIdsForRiskActions')[0].value;
    
    arrClientId = clientId.split(";");
    
    for(var i=0; i<arrClientId.length; i++)
    {
        $("#"+arrClientId[i])[0].disabled = "disabled";
    }    
}

function ChangeItemStatus(itemId, currentStatus)
{
    var url = "../../Templates/RelatePlus/Pages/AgendaItem.aspx/ChangeItemStatus";
    
    AjaxPostCall(url, '{"agendaItemId": '+itemId+', "isOpen": "' + currentStatus + '"}', handleTheChangedContent); 
}

function handleTheChangedContent(content)
{
    $("#agendaLine").replaceWith(content['d']);
}

function handleTheErrors()
{
    alert('error');
}

function ShowProtocolLine(ClientId, DisplayType)
{
    $('#'+ClientId)[0].style.display = DisplayType;
}

function toggleMenuOpen(objectCountActions)
{
//    var test = $('.txtStatus');
//    
//    test[objectCountActions].value = "Closed";

    var bolOpen = false;

    $('fieldset').children().each(function(index, obj) {
        var $item = $(obj);        
        var input = $item.find("input");

        for(var i=0;i<input.length; i++)
        {
            if(input[i].type=='text')
            {
                if(input[i].value == 'Open')
                {
                    input[i].value = 'Closed';
                }
            }
        }
    });
}

function toggleCustomMenuOpen(objectCountActions) 
{	
	var test = $('.txtCustomStatus');

	test[objectCountActions].value = "Open";
}

function OpenSlideActions()
{
    var bolOpen = false;

    $('fieldset').children().each(function(index, obj) {
        var $item = $(obj);        
        var input = $item.find("input");

        for(var i=0;i<input.length; i++)
        {
            if(input[i].type=='text')
            {
                if(input[i].value == 'Open')
                {
                    bolOpen = true;
                }
            }
        }

        if(bolOpen)
        {
            $item.find('.action_detail').show();
            bolOpen = false;
        }
    });
}

function CheckShowQuestionRemark()
{
	var object = '';
    
    $('fieldset input:checked').each(function(index, obj) {
        try{
            this.onclick();
        }
        catch(e) {}
    });   
}

function ShowQuestionRemark(ClientId)
{
    var divQuestionRemark = $('#' + ClientId)[0];    
    var content = $(".txtCheckboxClientId")[0].value;
    
    var arrControls = content.split(':');
    
    $(".txtCheckboxClientId")[0].value = 'none:'+arrControls[1];
    divQuestionRemark.style.display = 'none';
    
    if($('#' + arrControls[1])[0].checked)
    {
        $(".txtCheckboxClientId")[0].value = 'block:'+arrControls[1];
        divQuestionRemark.style.display = 'block';
        return;
    }
    return;
}

function ShowCustomActionComment(ClientId) {
	var divCustomActionComment = $('#' + ClientId)[0];

	if (divCustomActionComment.style.display == 'none') {
		divCustomActionComment.style.display = 'block';
		return;
	}
}

function array_length(arr) {
    var length = 0;
    for(val in arr) {
        length++;
    }
    return length;
}

function stripId(rbActionId)
{
    var actionID = rbActionId.split("_");
    var i = actionID.length;
    
    return actionID[i-2];
}

function resetWhenChecked(rbActionId, rbResetId, blAnswer) 
{
    if(blAnswer == "False")
    {
        var ctrlNumber = stripId(rbActionId);
        var rbAction = $('#' + rbActionId)[0];
        var rbReset = $('#' + rbResetId)[0];
        
        if(rbActionState[rbActionId] == undefined)
        {
            rbActionState[rbActionId] = rbAction.checked;
            rbReset.checked = false;
            return;
        }
        
        if(array_length(rbActionState) > 1)
        {
            var previousObject;
            
            for (i in rbActionState)
            {
                if (i.indexOf(ctrlNumber)!=-1)
                {
                    if (i != rbActionId)
                    {
                        previousObject = i;
                    }
                }
            }
        }      

        if(rbReset.checked && rbAction.checked)
        {
            rbActionState[rbActionId] = true;
            rbReset.checked = false;
            return; 
        }
        
        if(rbAction.checked && !rbActionState[rbActionId])
        {
            rbActionState[rbActionId] = true;
            rbActionState[previousObject] = false;
            rbReset.checked = false;
            return;
        }
        
        if(rbActionState[previousObject] != undefined && rbActionState[previousObject] && rbAction.checked)
        {
            rbActionState[rbActionId] = true;
            rbActionState[previousObject] = false;
            rbReset.checked = false;
            return;
        }
        
        if(rbAction.checked && !rbActionState[rbActionId] && rbReset.checked)
        {
            rbActionState[rbActionId] = true;
            rbReset.checked = false;
            return;        
        }
        
        if(rbActionState[previousObject] != undefined && rbAction.checked && rbActionState[rbActionId] && rbReset.checked && !rbActionState[previousObject])
        {
            rbActionState[rbActionId] = true;
            rbReset.checked = false;
            return;        
        }
        
        if(rbAction.checked && rbActionState[rbActionId])
        {
            rbActionState[rbActionId] = false;
            rbReset.checked = true;
            return;        
        }
        
        if(rbAction.checked && !rbReset.checked && rbActionState[rbActionId])
        {
            rbActionState[rbActionId] = rbAction.checked;
            rbReset.checked = false;
            return;
        }
    }
}

function toggleModuleDiv(objectCount, toggleHide)
{
    var test = $('#table_create_rie .td_rie_detail');

    for(var i=0; i<test.length; i++)
    {
        if(i==objectCount)
        {
            if(!toggleHide)
            {
                test[i].style.display = 'none';   
                return;
            }
            try{
                test[i].style.display = 'table-cell';
            }
            catch (e){
                test[i].style.display = 'block';
            }
            
            return;            
        }
    }
}

function OpenDivModuleRie(objectCount)
{
    var count=0;
    
    $('#table_create_rie .onclick').children().each(function() {
        var thisItem = $(this)[0];
        
        if(thisItem.type == 'checkbox')
        {
            if(count==objectCount)
            {            
                toggleModuleDiv(objectCount, thisItem.checked);
            }
            count++;
        }        
    });
}

function ShowHide(displayType, rbAction, rbReset, blAnswer) 
{
    resetWhenChecked(rbAction, rbReset, blAnswer);
    var strRieType = $('.txtCurrentRieType')[0].value;
    
	if(strRieType == 1)
	{
	    if(WalkTroughTheQuestions() && CheckRisks())
        {
            if($('.HideOrShow') != null)
            {
                $('.HideOrShow')[0].style.display = 'block';
            }
        }
        else
        {
            if($('.HideOrShow') != null)
            {
                $('.HideOrShow')[0].style.display = 'none';
                unCheckCheckBoxNoExtraMaatregelen();
            }
        }
	}
}

function unCheckCheckBoxNoExtraMaatregelen()
{
     $('.HideOrShow').each(function(index, obj){
        var $item = $(obj);
        
        var checkbox = $item.find("input");
        
        for(var i=0;i<checkbox.length; i++)
        {
            if(checkbox[i].type=='checkbox')
            {
                checkbox[i].checked = false;
            }
        }
     });
}


function ShowHideMonitoringQuestions(className, objectCount, displayType, custom, blAnswer, rbAction, rbReset, blMonitoring)
{
    resetWhenChecked(rbAction, rbReset, blAnswer);
    if(!custom)
    {        
    	var test = $('.list_measures .list_measures_details');
        
        for(var i=0; i<test.length; i++)
        {
            if(i == objectCount)
            {
                test[i].style.display = displayType;
            }
        }
    }
    
    if(blMonitoring)
    {
       ShowProtocolLine(rbAction, displayType);
    }
    
    var strRieType = $('.txtCurrentRieType')[0].value;
    
//	if (strRieType == 1)
//	{
//	    if(WalkTroughTheQuestions() && CheckRisks())
//        {
//            $('.HideOrShow')[0].style.display = 'block';
//        }
//        else
//        {
//            $('.HideOrShow')[0].style.display = 'none';
//            // uncheck the checkbox!;
//            unCheckCheckBoxNoExtraMaatregelen();
//            
//        }
//    }
    
    if(strRieType == 3)
    {
        if(WalkTroughTheQuestions())
        {
            $('div.disable').each(function(index, obj){
         
                var $item = $(obj);
                
                var input = $item.find("input");
                for(var i=0; i<input.length; i++)
                {
                    if(input[i].type == 'checkbox')
                    {
                        input[i].disabled = 'true';
                    }
                }
                
                $item.addClass("quiet");
                
            });
            FindRiskControls('rbSubQuestionOneAnswerFalse');
            $(".txtRiskSet")[0].value = "false";
        }
        
        else
        {
            FindRiskControls('rbSubQuestionOneAnswerTrue'); 
            $(".txtRiskSet")[0].value = "true";
            $('div.quiet').each(function(index, obj){
         
                var $item = $(obj);
                
                var input = $item.find("input");
                for(var i=0; i<input.length; i++)
                {
                    if(input[i].type == 'checkbox')
                    {
                        input[i].disabled = false;
                    }
                }
                
                $item.removeClass("quiet");
            
            });
        }
    }
}

function FindRiskControls(setStatus)
{
    var isTrue = false;
     $('fieldset.fsSubQuestionRisk').each(function(index, obj){
     
        var $item = $(obj);
        if($item.find("fieldset :div.field"))
        {
            var test = $item.find("div");
                
            if(test.find("input"))
            {
                var radio = test.find("input");
                
                for(var i=0;i<radio.length;i++)
                {
                    if(radio[i].type == 'radio')
                    {
                        
                        switch(radio[i].value)
                        {
                            case setStatus:
                                radio[i].checked = true;                    
                            break;
                        }                        
                    }
                }
            }                        
        }
    });
    return isTrue;
}

function CheckRisks()
{
    var isTrue = false;
     $('fieldset.fsSubQuestionRisk').each(function(index, obj){
     
        var $item = $(obj);
        if($item.find("fieldset :div.field"))
        {
            var test = $item.find("div");
                
            if(test.find("input"))
            {
                var radio = test.find("input");
                
                for(var i=0;i<radio.length;i++)
                {
                    if(radio[i].type == 'radio')
                    {
                        switch(radio[i].value)
                        {
                            case 'rbSubQuestionOneAnswerTrue':
                                if(!radio[i].checked)
                                {
                                    isTrue = true;
                                }
                            break;
                        }                        
                    }
                }
            }                        
        }
    });
    return isTrue;
}

function WalkTroughTheQuestions()
{
    var isTrue = true;

    $('fieldset.actionsList').each(function(index, obj){
        var $item = $(obj);
        if($item.find("fieldset.actionsList :div.field"))
        {
            var test = $item.find("div");
                
            if(test.find("input"))
            {
                var radio = test.find("input");
                
                for(var i=0;i<radio.length;i++)
                {
                    if(radio[i].type == 'radio')
                    {
                        switch(radio[i].value)
                        {
                            case 'rbActionAnswerTrue':
                                if(!radio[i].checked)
                                {
                                    isTrue = false;
                                }
                            break;
                            case 'rbMonitoringActionAnswerTrue':
                                if(!radio[i].checked)
                                {
                                    isTrue = false;
                                }
                            break;
                        }                        
                    }
                }
            }                        
        }
    });
    return isTrue;
}

function whenPageRenders()
{
    if(WalkTroughTheQuestions() && CheckRisks())
    {
        
        if($('.HideOrShow').length != 0)
        {        
            $('.HideOrShow')[0].style.display = 'block';
            var strRieType = $('.txtCurrentRieType')[0].value;
        
	        FindRiskControls('rbSubQuestionOneAnswerFalse');
	    }
    }
    else
    {
        if($('.HideOrShow').length != 0)
        { 
            $('.HideOrShow')[0].style.display = 'none';
//            FindRiskControls('rbSubQuestionOneAnswerTrue');
        }
    }
}
