function loadSaveConfig()
{
    var _tp_configFilePath = __projectDirectory__ + '/desite.modules/Absturzsicherung.module/save_config.json';
    if(desiteAPI.checkIfFileExists( _tp_configFilePath))
    {
        var configSaveJSONStr = desiteAPI.readTextFileAsString(_tp_configFilePath, "UTF-8");
        configSave = JSON.parse(configSaveJSONStr).save;
        __currentLangIndex__ = configSave.langIndex;
        __opDet1__ = configSave.det1;
        __opDet2__ = configSave.det2;    
    }
    else
    {
        __currentLangIndex__ = 0;    
        saveConfig();
    }

}

function saveConfig()
{
    var _tp_configFilePath = __projectDirectory__ + '/desite.modules/Absturzsicherung.module/save_config.json';
    var save = '{"save":{"langIndex": ' + __currentLangIndex__ + ',"det1": '+__opDet1__+',"det2": '+__opDet2__+'}}';
    desiteAPI.writeFile(_tp_configFilePath,save,"UTF-8");
}

function openCloseDetails()
{
    for(var i = 0; i < 8; i++)
    {
        var obj = document.getElementById('details'+i);
        obj.open = true;
    }
    for(var i = 7; i >-1 ; i--)
    {
        var obj = document.getElementById('details'+i);
        obj.open = false;
    }
}

function createMaterial(ModelID)
{
    var id = desiteAPI.getMaterialIDByName('ModelColor1');
    if(id == '')
    {
        var matXml ='<material ID="ModelColor1" name="ModelColor1"> <diff r="237" b="116" g="133" a="1"/> <amb r="237" b="116" g="133" a="1"/> <spec r="255" b="255" g="255" a="1"/> <trans v="0"/> <shin v="0.5"/>  </material>';
        desiteAPI.createMaterial(matXml,ModelID);
    }
    id = desiteAPI.getMaterialIDByName('ModelColor2');
    if(id == '')
    {
        var matXml ='<material ID="ModelColor2" name="ModelColor2"> <diff r="166" b="58" g="58" a="1"/> <amb r="166" b="58" g="58" a="1"/> <spec r="255" b="255" g="255" a="1"/> <trans v="0"/> <shin v="0.5"/>  </material>';
        desiteAPI.createMaterial(matXml,ModelID);
    }
    id = desiteAPI.getMaterialIDByName('ModelColor3');
    if(id == '')
    {
        var matXml ='<material ID="ModelColor3" name="ModelColor3"> <diff r="179" b="102" g="141" a="1"/> <amb r="179" b="102" g="141" a="1"/> <spec r="255" b="255" g="255" a="1"/> <trans v="0"/> <shin v="0.5"/>  </material>';
        desiteAPI.createMaterial(matXml,ModelID);
    }
    id = desiteAPI.getMaterialIDByName('ModelColor4');
    if(id == '')
    {
        var matXml ='<material ID="ModelColor4" name="ModelColor4"> <diff r="255" b="158" g="156" a="1"/> <amb r="255" b="158" g="156" a="1"/> <spec r="255" b="255" g="255" a="1"/> <trans v="0"/> <shin v="0.5"/>  </material>';
        desiteAPI.createMaterial(matXml,ModelID);
    }
    id = desiteAPI.getMaterialIDByName('ModelColor5');
    if(id == '')
    {
        var matXml ='<material ID="ModelColor5" name="ModelColor5"> <diff r="255" b="64" g="255" a="1"/> <amb r="255" b="64" g="255" a="1"/> <spec r="255" b="255" g="255" a="1"/> <trans v="0"/> <shin v="0.5"/>  </material>';
        desiteAPI.createMaterial(matXml,ModelID);
    }
    id = desiteAPI.getMaterialIDByName('ModelColor6');
    if(id == '')
    {
        var matXml ='<material ID="ModelColor6" name="ModelColor6"> <diff r="255" b="64" g="217" a="1"/> <amb r="255" b="64" g="217" a="1"/> <spec r="255" b="255" g="255" a="1"/> <trans v="0"/> <shin v="0.5"/>  </material>';
        desiteAPI.createMaterial(matXml,ModelID);
    }
    id = desiteAPI.getMaterialIDByName('ModelColor7');
    if(id == '')
    {
        var matXml ='<material ID="ModelColor7" name="ModelColor7"> <diff r="255" b="64" g="140" a="1"/> <amb r="255" b="64" g="140" a="1"/> <spec r="255" b="255" g="255" a="1"/> <trans v="0"/> <shin v="0.5"/>  </material>';
        desiteAPI.createMaterial(matXml,ModelID);
    }
    id = desiteAPI.getMaterialIDByName('ModelColor8');
    if(id == '')
    {
        var matXml ='<material ID="ModelColor8" name="ModelColor8"> <diff r="62" b="176" g="62" a="1"/> <amb r="62" b="176" g="62" a="1"/> <spec r="255" b="255" g="255" a="1"/> <trans v="0"/> <shin v="0.5"/>  </material>';
        desiteAPI.createMaterial(matXml,ModelID);
    }
    id = desiteAPI.getMaterialIDByName('ModelColor9');
    if(id == '')
    {
        var matXml ='<material ID="ModelColor9" name="ModelColor9"> <diff r="64" b="255" g="179" a="1"/> <amb r="64" b="255" g="179" a="1"/> <spec r="255" b="255" g="255" a="1"/> <trans v="0"/> <shin v="0.5"/>  </material>';
        desiteAPI.createMaterial(matXml,ModelID);
    }
    id = desiteAPI.getMaterialIDByName('ModelColor10');
    if(id == '')
    {
        var matXml ='<material ID="ModelColor10" name="ModelColor10"> <diff r="63" b="178" g="63" a="1"/> <amb r="63" b="178" g="63" a="1"/> <spec r="255" b="255" g="255" a="1"/> <trans v="0"/> <shin v="0.5"/>  </material>';
        desiteAPI.createMaterial(matXml,ModelID);
    }
}
function createDocuments()
{
    var t = desiteAPI.getModelListByName(__internal__[13].txt,'documents');
    if(t == '')
    {
        desiteAPI.createModel(__internal__[13].txt,false,'documents');
        var RT = desiteAPI.getModelListByName(__internal__[13].txt,'documents');
        var str = '<container ID="'+__internal__[13].txt+'" name="'+__internal__[13].txt+'" />';
        var id = desiteAPI.createObjectFromXml(RT+'-oh',str);
        var _tp_configFilePath = __projectDirectory__ + '/desite.modules/Absturzsicherung.module/documents/';
        __dokuList__.push(desiteAPI.createObjectFromXml(id,'<document path="' + _tp_configFilePath + 'Typen-Absturzsicherung-2022-2021-11-12 DE-1.pdf" />'));
        __dokuList__.push(desiteAPI.createObjectFromXml(id,'<document path="' + _tp_configFilePath + 'Typen-Absturzsicherung-2022-2021-11-12 DE-2.pdf" />'));
        __dokuList__.push(desiteAPI.createObjectFromXml(id,'<document path="' + _tp_configFilePath + 'Typen-Absturzsicherung-2022-2021-11-12 DE-3.pdf" />'));
        __dokuList__.push(desiteAPI.createObjectFromXml(id,'<document path="' + _tp_configFilePath + 'Typen-Absturzsicherung-2022-2021-11-12 DE-4.pdf" />'));
        __dokuList__.push(desiteAPI.createObjectFromXml(id,'<document path="' + _tp_configFilePath + 'Typen-Absturzsicherung-2022-2021-11-12 DE-5.pdf" />'));
        __dokuList__.push(desiteAPI.createObjectFromXml(id,'<document path="' + _tp_configFilePath + 'Typen-Absturzsicherung-2022-2021-11-12 DE-6.pdf" />'));
        __dokuList__.push(desiteAPI.createObjectFromXml(id,'<document path="' + _tp_configFilePath + 'Typen-Absturzsicherung-2022-2021-11-12 DE-7.pdf" />'));
        __dokuList__.push(desiteAPI.createObjectFromXml(id,'<document path="' + _tp_configFilePath + 'Typen-Absturzsicherung-2022-2021-11-12 DE-8.pdf" />'));
        __dokuList__.push(desiteAPI.createObjectFromXml(id,'<document path="' + _tp_configFilePath + 'Typen-Absturzsicherung-2022-2021-11-12 DE-9.pdf" />'));
        __dokuList__.push(desiteAPI.createObjectFromXml(id,'<document path="' + _tp_configFilePath + 'Typen-Absturzsicherung-2022-2021-11-12 DE-10.pdf" />'));
        __dokuList__.push(desiteAPI.createObjectFromXml(id,'<document path="' + _tp_configFilePath + 'Typen-Absturzsicherung-2022-2021-11-12 DE-11.pdf" />'));
        __dokuList__.push(desiteAPI.createObjectFromXml(id,'<document path="' + _tp_configFilePath + 'Typen-Absturzsicherung-2022-2021-11-12 DE-12.pdf" />'));
        __dokuList__.push(desiteAPI.createObjectFromXml(id,'<document path="' + _tp_configFilePath + 'Typen-Absturzsicherung-2022-2021-11-12 DE-13.pdf" />'));
        __dokuList__.push(desiteAPI.createObjectFromXml(id,'<document path="' + _tp_configFilePath + 'Typen-Absturzsicherung-2022-2021-11-12 DE-14.pdf" />'));
        __dokuList__.push(desiteAPI.createObjectFromXml(id,'<document path="' + _tp_configFilePath + 'Typen-Absturzsicherung-2022-2021-11-12 DE-15.pdf" />'));
        __dokuList__.push(desiteAPI.createObjectFromXml(id,'<document path="' + _tp_configFilePath + 'Typen-Absturzsicherung-2022-2021-11-12 DE-16.pdf" />'));
        __dokuList__.push(desiteAPI.createObjectFromXml(id,'<document path="' + _tp_configFilePath + 'Typen-Absturzsicherung-2022-2021-11-12 DE-17.pdf" />'));
        __dokuList__.push(desiteAPI.createObjectFromXml(id,'<document path="' + _tp_configFilePath + 'Typen-Absturzsicherung-2022-2021-11-12 DE-18.pdf" />'));
        __dokuList__.push(desiteAPI.createObjectFromXml(id,'<document path="' + _tp_configFilePath + 'Typen-Absturzsicherung-2022-2021-11-12 DE-19.pdf" />'));
        __dokuList__.push(desiteAPI.createObjectFromXml(id,'<document path="' + _tp_configFilePath + 'Typen-Absturzsicherung-2022-2021-11-12 DE-20.pdf" />'));
    }
    else{
        var l = desiteAPI.getContainedElements(t+'-oh',0);
        for(var i = 1; i < l.length; i++)
        {
            __dokuList__.push(l[i]);
        }
    }
}
function startCheckWith4D()
{
    var RT = desiteAPI.getRootContainerList(globalModelName);
    if(RT != '')
    {
        alert(__internal__[17].txt);
        return;
    }
    else
    {
        globalModelID = '';
        globalDTMCounter = 0;
        globalRoofCounter = 0;
        globalCeilingCounter = 0;
    }
    var tList = desiteAPI.getVisibleElements('activities');
    var _progressObject = {
        text: false,
        bar: false,
        label: true,
        closeButton: false,
        title: __internal__[4].txt
    }      
    desiteMD.showProgress(_progressObject);
    document.getElementById('output').innerHTML +='<br>' + __internal__[5].txt;
    desiteMD.showProgressMessageLabel(__internal__[5].txt);
    getGeometryDataAll();
    desiteMD.showProgressMessageLabel(__internal__[6].txt);
    document.getElementById('output').innerHTML +='<br>' + __internal__[6].txt;
    calculateDTM();
    checkFolderCeiling();
    checkFolderRoof();
    desiteMD.showProgressMessageLabel(__internal__[7].txt);
    document.getElementById('output').innerHTML +='<br>' + __internal__[7].txt;
    var startDate = new Date(desiteAPI.get4dProjectStart());
    var endDate = new Date(desiteAPI.get4dProjectFinish());
    var currentDate = startDate; 
    var finishedTaskList = [];
    var counter = 0;
    while(currentDate < endDate)
    {
        desiteMD.showProgressMessageLabel(__internal__[9].txt + currentDate.toUTCString());
        document.getElementById('output').innerHTML +='<br>' + __internal__[9].txt + currentDate.toUTCString();
        document.getElementById('output').scrollBottom;
        var taskList = desiteAPI.get4dActiveTaskList( currentDate, true);
        for(var i = 0; i < taskList.length; i++)
        {
            if(finishedTaskList.indexOf(taskList[i]) > -1)
            {
                continue;
            }
            else
            {
                if(tList.indexOf(taskList[i]) > -1)
                {
                    var objList = desiteAPI.getObjectsByTaskList(taskList[i]);
                    getCurrentGeometry(objList);
                    counter += __currentRoofData__.length;
                    checkCeiling4D(taskList[i]);
                    checkLiftOpening4D(taskList[i]);
                    checkRoof4D(taskList[i])
                    updateCurrentGeometry();
                    finishedTaskList.push(taskList[i]);
                }
            }
        }
        var prevDate = currentDate.getDate();
        currentDate.setDate( currentDate.getDate() + 1)
        desiteAPI.set4dDateTime(currentDate);
        if(prevDate == currentDate.getDate())
        {
            currentDate.setDate(currentDate.getDate() + 2);
        }               
    }
    releaseCurrentGeometry();
    desiteAPI.showAll(true);
    desiteMD.showProgressMessageLabel(__internal__[8].txt);
    document.getElementById('output').innerHTML +='<br>' + __internal__[8].txt;
    releaseGeometryData();
    desiteMD.hideProgress();
    numFloor = 0;
    document.getElementById('idAvaRoof').value = '(0)';
}
function releaseCurrentGeometry()
{
    __currentSlabData__ = [];
    __legacySlabData__ = [];
    __legacyRoofData__ = [];
    __legacyGeometryData__ = [];
}
function releaseGeometryData()
{
    __frameworkList__ = [];
    __globalGeometryData__ = [];
    __globalSlabData__ = [];
    __globalLiftOpeningData__ = [];
    __globalLiftData__ = [];
    __globalRoofData__ = [];
    __globalDTMData__ = [];
    __currentSlabData__ = [];
    __legacySlabData__ = [];
    __legacyRoofData__ = [];
    __legacyGeometryData__ = [];
    roofID = [];
    frameworkID = [];
    previousIDs = [];
    currentIDs = [];
    FWList = [];
    __createdGeo__ = [];
}

function clearData()
{
    releaseCurrentGeometry();
    releaseGeometryData();
    numFloor = 0;
    document.getElementById('idAvaRoof').value = '(0)';
    document.getElementById('FrameworkDistance').value = 0.1;
    document.getElementById('cb1').checked = false;
    document.getElementById('output').innerHTML = '';
    var name = desiteAPI.getModelListByName(globalModelName);
    desiteAPI.deleteModel(name[0]);

}

function closeOptions(i)
{
    if(i == 0)
    {
        document.getElementById('txt-15').style.backgroundColor = "white";
        document.getElementById('txt-20').style.backgroundColor = "white"; 
        if(document.getElementById('details2').open == false)
        {
            document.getElementById('details0').open = false;
            document.getElementById('details1').open = false;
            document.getElementById('details4').open = true;
            document.getElementById('details5').open = true;
            document.getElementById('details6').open = true;
            document.getElementById('details7').open = true;
            document.getElementById('details4').open = false;
            document.getElementById('details5').open = false;
            document.getElementById('details6').open = false;
            document.getElementById('details7').open = false; 
            document.getElementById('txt-8').style.backgroundColor = "#85b400";
        }
        else
        {
            document.getElementById('txt-8').style.backgroundColor = "white";
            document.getElementById('details4').open = true;
            document.getElementById('details5').open = true;
            document.getElementById('details6').open = true;
            document.getElementById('details7').open = true;
            document.getElementById('details4').open = false;
            document.getElementById('details5').open = false;
            document.getElementById('details6').open = false;
            document.getElementById('details7').open = false; 
        } 
    }
    else if(i==1)
    {
        document.getElementById('txt-8').style.backgroundColor = "white";
        document.getElementById('txt-20').style.backgroundColor = "white";
        if(document.getElementById('details4').open == false)
        {
            document.getElementById('details0').open = false;
            document.getElementById('details1').open = false;
            document.getElementById('details2').open = true;
            document.getElementById('details3').open = true;
            document.getElementById('details6').open = true;
            document.getElementById('details7').open = true; 
            document.getElementById('details2').open = false;
            document.getElementById('details3').open = false;
            document.getElementById('details6').open = false;
            document.getElementById('details7').open = false; 
            document.getElementById('txt-15').style.backgroundColor = "#85b400";
        }
        else
        {
            document.getElementById('details2').open = true;
            document.getElementById('details3').open = true;
            document.getElementById('details6').open = true;
            document.getElementById('details7').open = true;
            document.getElementById('details2').open = false;
            document.getElementById('details3').open = false;
            document.getElementById('details6').open = false;
            document.getElementById('details7').open = false;
            document.getElementById('txt-15').style.backgroundColor = "white";
        }
    }
    else
    {
        document.getElementById('txt-8').style.backgroundColor = "white";
        document.getElementById('txt-15').style.backgroundColor = "white";
        if(document.getElementById('details6').open == false)
        {
            document.getElementById('details0').open = false;
            document.getElementById('details1').open = false;
            document.getElementById('details2').open = true;
            document.getElementById('details3').open = true;
            document.getElementById('details4').open = true;
            document.getElementById('details5').open = true; 
            document.getElementById('details2').open = false;
            document.getElementById('details3').open = false;
            document.getElementById('details4').open = false;
            document.getElementById('details5').open = false; 
            document.getElementById('txt-20').style.backgroundColor = "#85b400";
        }
        else
        {
            document.getElementById('details2').open = true;
            document.getElementById('details3').open = true;
            document.getElementById('details4').open = true;
            document.getElementById('details5').open = true;
            document.getElementById('details2').open = false;
            document.getElementById('details3').open = false;
            document.getElementById('details4').open = false;
            document.getElementById('details5').open = false; 
            document.getElementById('txt-20').style.backgroundColor = "white";
        }
    }
}

function saveOpt(i)
{
    if(i == 0)
    {
        if(document.getElementById('details0').open == false)
        {
            __opDet1__ = true;
        }
        else
        {
            __opDet1__ = false;
        }
    }
    else
    {
        if(document.getElementById('details1').open == false)
        {
            __opDet2__ = true;
        }
        else
        {
            __opDet2__ = false;
        }
    }
    saveConfig();
}
function executeOpen()
{
    document.getElementById('details0').open = __opDet1__;
    document.getElementById('details1').open = __opDet2__;
}