//$c4 SKN 07/03/14 Prj 19654 changes in showPage() to show correct display mode of model for mesh failure diagnostics.
//$c3 SKN 02/10/14 prj 19654 changes in loadContentStrings() and Added ShowFailedBodies(), ShowMeshForAllBodies().
//$c2 MGG 05/28/08 Moved code to write strings to cwcommon.js
//$c1 MGG 04/21/08 Moved colorOver and colorOut to cwcommon.js
//
//
//========================================================================//
//              Copyright 2007 (Unpublished Material)                     //
//                  SolidWorks Corp.                                      //
//========================================================================//
//
//
//     Application: Cosmosworks
//
//
//========================================================================//
// cwpart.js : Script for: Simulation Advisor -- Connections 

var showingFullTip = false;
var tipText = "";
var mouseOverExpando = false;		// Used to help set color of up/down arrow
var interferenceDetected = false;

var g_RunSectionCWID = 3;
var modelBeingSolved = false;
var g_activeDivID = "";
var g_preDivID = "";

function initPage()
{
    var loadButtonState = GetSavedState(g_RunNavBtnID);
    if(loadButtonState == 2)
    {
        ActivateNavBtn(g_GetStartedNavBtnID);
        return;
    }
    loadButtonState = GetSavedState(g_ResultsNavBtnID);
    if(loadButtonState == 4)
    {
        UpdateBtnState(g_RunNavBtnID);	        
        SetSectionState(4, 'Div_Result_First', 3);	        	    
        UpdateBtnState(g_ResultsNavBtnID);
        ActivateNavBtn(g_ResultsNavBtnID)   
        return;
    }
    loadContentStrings();
   commonInitPage(g_RunSectionCWID);
   setActivateNavBtnState(g_RunNavBtnID, true);
   if (g_activeDivID == "Div_Mesh_Fail_Diagnostic") {
       return;
   }
   var meshStatus = GetSimWizValue(21);
   CheckInterference();
   if(interferenceDetected)
   { // interference detected
       showPage("Div_Mesh");
   }
   //else if( meshStatus != "1" )
   //{
   //     showPage( "Div_Mesh_Failure" );
   //}
   else{
        showPage( "Div_Ready_ToSolve" );   
   }
}


function onUnLoad()
{
    setActivateNavBtnState(g_RunNavBtnID, false);
}

function showPage(whichDiv)
{
    var loadButtonState = GetSavedState(g_RunNavBtnID);
    if(loadButtonState == 0)
        {loadButtonState = 1;}
        	
	hideShowAllOf( whichDiv, false );
	resetInfoPane();
	resizeInfoPane();
	if (whichDiv == "Div_Mesh_Fail_Diagnostic")
	{
	    GetSimWizValue(32);
	}
    SetSectionState(g_RunSectionCWID, whichDiv, loadButtonState);	    
    UpdateBtnState(g_RunNavBtnID);    
    
    g_preDivID = g_activeDivID;
	g_activeDivID = whichDiv;   
}

function loadContentStrings() 
{
	var pageStrings = "";
	pageStrings = GetPageText(55556, 55592) + GetPageText(55207, 55211) + GetPageText(56023, 56027) + GetPageText(56031, 56056) + GetPageText(55558, 55558) + GetPageText(55282, 55282) + GetPageText(55980, 55980) + GetPageText(56065, 56065) + GetPageText(56066, 56066);
//		alert(pageStrings);

	if(pageStrings == "Error")					// If SW strings not found, then get default English string values.
		return;
    
    writeStrings(pageStrings);
    return;
}

function Back( div )
{
    var backDiv = g_preDivID;
    if(backDiv.length > 0)
    {
        if(backDiv == "Div_Mesh" && interferenceDetected == false)
        {
            ActivateNavBtn(g_LoadFixNavBtnID);
        }
        else
        {
            showPage(backDiv);
        }
        g_preDivID = "";
        return;
    }
    if (g_activeDivID == "Div_Mesh") {
        ActivateNavBtn(g_LoadFixNavBtnID);
    }
    else if (g_activeDivID == "Div_Mesh_Setting" && interferenceDetected) {
        backDiv = "Div_Mesh";
    }
    else if (g_activeDivID == "Div_Mesh_Setting" && interferenceDetected == false) {
        backDiv = "Div_Ready_ToSolve";
    }
    else if (g_activeDivID == "Div_Ready_ToSolve") {
        if (interferenceDetected) {
            backDiv = "Div_Mesh";
        }
        else {
            ActivateNavBtn(g_LoadFixNavBtnID);
        }
    }
    else if (g_activeDivID == "Div_Mesh_Failure") {
        backDiv = "Div_Mesh_Setting";
    }
    else if (g_activeDivID == "Div_Mesh_Setting3") {
        backDiv = "Div_Mesh_Setting";
    }
    else if (g_activeDivID == "Div_Check_Geometry") {
        backDiv = "Div_Mesh_Setting";
    }
    else if (g_activeDivID == "Div_Mesh_Fail_Diagnostic") {
        backDiv = "Div_Mesh_Failure";
    }
    if(backDiv.length > 0)
    {
        showPage(backDiv);
        g_preDivID = "";
    }
}

function RunAnalysis()
{
    if( checkPMActive() || !checkActiveDoc() ) return;
    if( GetSimWizValue(13) != "0" ) return;
    doCommand2(3, 32, 0, 0 );
//    SetSectionState(g_RunSectionCWID, 'Div_Mesh', 1);	        	    
//    UpdateBtnState(g_RunNavBtnID);	        
//    UpdateBtnState(g_ResultsNavBtnID);
//    ActivateNavBtn(g_ResultsNavBtnID)   
}

function ChangeMeshSetting()
{
    if( checkPMActive() || !checkActiveDoc() ) return;
    if( GetSimWizValue(13) != "0" ) return;
    doCommand(3, 10, 0);
}

function ShowMeshPMWithInCompatible() {
    if (checkPMActive() || !checkActiveDoc()) return;
    if (GetSimWizValue(13) != "0") return;
    doCommand(3, 10, 1);
}

function CreateMeshControl(createMesh)
{
    if( checkPMActive() || !checkActiveDoc() ) return;
    if( GetSimWizValue(13) != "0" ) return;
    doCommand(3, 45, createMesh);
}

function ShowFailedBodies()
{
    if (checkPMActive() || !checkActiveDoc()) return;
    if (GetSimWizValue(13) != "0") return;
    doCommand(5, 0, 0);
}

function ShowMeshForAllBodies()
{
    if (checkPMActive() || !checkActiveDoc()) return;
    if (GetSimWizValue(13) != "0") return;
    doCommand(6, 0, 0);
}
function CreateMeshDiagnose()
{
    if( checkPMActive() || !checkActiveDoc() ) return;
    if( GetSimWizValue(13) != "0" ) return;
    doCommand(3, 42916, 0);
}

function GotoNextStep()
{
    if( checkPMActive() || !checkActiveDoc() ) return;
    if( GetSimWizValue(13) != "0" ) return;
    ChangeMeshSetting();
//    SetSectionState(g_RunSectionCWID, 'Div_Mesh', 1);	        	    
//    UpdateBtnState(g_RunNavBtnID);	        
//    UpdateBtnState(g_ResultsNavBtnID);
//    ActivateNavBtn(g_ResultsNavBtnID)   
}

function Next(id)
{
    if( GetSimWizValue(13) != "0" ) return;
    var analysisDone = GetSimWizValue(2);    
    if( id == "Div_Mesh" )
    {
        showPage( "Div_Mesh_Setting" );
        showInfoPane('Div_Mesh_Density');
    }
    else if( id == "Div_Mesh_Setting" )
        showPage( "Div_Mesh_Setting3" );
    else
    {
        var meshStatus = GetSimWizValue(21);
        if( meshStatus != "1" )
           showPage( "Div_Mesh_Failure" );
       else if (analysisDone != "1" && meshStatus != "1")
            GotoNextStep();
        else
            showPage( "Div_Mesh_Setting3" );
    }
}

function AddIncompatibleMeshOption() {
    var applicable = GetSimWizValue(30);
    if (applicable != "0") {
        addScriptButton('ShowMeshPMWithInCompatible()', 'IDS_MSG_INCOMPATIBLE_MESH');
    }
}

function AddInterferenceInfo() {
    var analysisType = GetSimWizValue(25);
    if (analysisType == "0" || analysisType == "5") // static and non-linear
    {
        document.write("<p id=\"IDS_MSG_CREATE_SHRINKFIT_INFO\"></p>");
    }
    else {
        document.write("<p id=\"IDS_MSG_ACCI_INTERFERENCE\"></p>");
    }
}

function AddCreateShrinkFitBtn() {
    var analysisType = GetSimWizValue(25);
    if (analysisType == "0" || analysisType == "5") // static and non-linear
    {
        addScriptButton('CreateShrinkFit()', 'IDS_CREATE_SHRINK_BTN');
    }
}

function AddReadyToSolveDivHeader() {
    var meshStatus = GetSimWizValue(21);
    if (meshStatus != "1") { // mesh has failed
        document.write("<p style=\"color:Red\">Mesh failed.</p>");
        addScriptButton('showPage(\'Div_Mesh_Fail_Diagnostic\')', 'IDS_MSG_MESH_FAIL_DIAGNOSTIC');        
    }
    else {// mesh is successful
        document.write("<p class=\"fontregular\" ><span id=\"IDS_READY_TO_SOLVE\"></span></p>");
		document.write("<p style=\"color:Green\"><span id=\"IDS_NO_INTRF_DETECTED\"></span></p>");
    }
}

function ShowCheckInterference()
{
    if( checkPMActive() || !checkActiveDoc() ) return;
    if(GetSimWizValue(23) != "0")
    {// for assembly
        //CheckInterference();
        doCommand(3, 36, 1);
        /*
        if(interferenceDetected == false)
        {
        // goto mesh/run page
            showPage("Div_Ready_ToSolve");
        }
        else
        {
            //doCommand(3, 36, 1);            
        }*/
        
    }
    else { // for parts
        doCommand(3, 36, 1);
        /*
        CheckInterference();
        if(interferenceDetected == false)
        {
        // goto mesh/run page
            showPage("Div_Ready_ToSolve");
        }*/
    }
}

function ShowCavityTool()
{
    if(GetSimWizValue(23) != "0")
    {// only for assemblies
        //alert("show info tip");
        showInfoTip(-1, -1, 'cavityTool.gif');
    }
    else if((GetSimWizValue(23) != "1") && (CheckModelAssembly()))
    { // for multi body parts
            if( checkPMActive() || !checkActiveDoc() ) return;

            doCommand(3, 40, 1);    
    }    
}

function AddShowCheckInteference()
{
    if(GetSimWizValue(23) != "0")
    {// only for assemblies
        document.write("</br>");
        addScriptButton('ShowCheckInterference()', 'IDS_SHOWCHECK_INTER');   
    }
    else // parts
    {
        document.write("</br>");
        addScriptButton('ShowCheckInterference()', 'IDS_SHOWCHECK_INTER');
    }
}

function AddCavityBtn()
{
    if( GetSimWizValue(23) != "0")
    {// only for assemblies and multi-body parts
        document.write("</br>");
        //addScriptButtonText('ShowCavityTool()','Use SolidWorks Cavity tool to remove interferences between two bodies');
        document.write("<span id=\"IDS_USE_CAVITY_TOOL\"></span>");
        document.write("<span id=\"IDS_EXAMPLES\" class=\"BlueUnderline\" onmouseover=\"javascript:ShowCavityTool()\" onmouseout=\"javascript:hideInfoTip()\">See example</span>");
        document.write("</br>");        
    }
    else if((GetSimWizValue(23) != "1") && CheckModelAssembly())
    {// for multi body parts
        document.write("</br>");
        addScriptButton('ShowCavityTool()', 'IDS_USE_COMBINE_TOOL');    
    }
}

function AddMatesBtn()
{
    if(GetSimWizValue(23) != "0")
    {// only for assemblies
        document.write("</br>");
        addScriptButton('ShowMatesPM()', 'IDS_USE_MATES');    
    }
}

function ShowMatesPM()
{
    if( checkPMActive() || !checkActiveDoc() ) return;

    doCommand(3, 38, 1);    
}

function AddMoveBodiesBtn()
{
    if(GetSimWizValue(23) != "1")
    {// only for parts
        document.write("</br>");
        addScriptButton('ShowMoveBodiesPM()', 'IDS_USE_MOVE_BODIES')    
    }
}

function ShowMoveBodiesPM()
{
    if( checkPMActive() || !checkActiveDoc() ) return;
    
    doCommand(3, 39, 1);    
}
function CreateShrinkFit()
{
    if( checkPMActive() || !checkActiveDoc() ) return;
    doCommand(3, 37, 1);
}

function CheckInterference()
{
    interferenceDetected = false;
    if(GetSimWizValue(22) != "0")
    {// detected
        interferenceDetected = true;
    }
}

function AddMeshQualityBtn()
{
    addScriptButtonText('MeshQuality()','Improve the quality of mesh')
}

function MeshQuality()
{
    alert("show mesh quality page");
}

function ShowSimplify() {
    doCommand(3, 41, 0);
}

function ShowCheckEntity() {
    doCommand(3, 43, 0);
}

function ShowThicknessAnalysis() {
    doCommand(3, 42, 0);
}
