/**
* getMove get move button clicked
**/
function getMove()
{
    #    $clang = Yii::app()->lang;
    $aAcceptedMove = array('default', 'movenext', 'movesubmit', 'moveprev', 'saveall', 'loadall', 'clearall', 'changelang');
    // We can control is save and load are OK : todo fix according to survey settings
    // Maybe allow $aAcceptedMove in Plugin
    $move = Yii::app()->request->getParam('move');
    foreach ($aAcceptedMove as $sAccepteMove) {
        if (Yii::app()->request->getParam($sAccepteMove)) {
            $move = $sAccepteMove;
        }
    }
    if ($move == 'clearall' && App()->request->getPost('confirm-clearall') != 'confirm') {
        $move = "clearcancel";
    }
    if ($move == 'default') {
        $surveyid = Yii::app()->getConfig('surveyID');
        $thissurvey = getsurveyinfo($surveyid);
        $iSessionStep = isset($_SESSION['survey_' . $surveyid]['step']) ? $_SESSION['survey_' . $surveyid]['step'] : false;
        $iSessionTotalSteps = isset($_SESSION['survey_' . $surveyid]['totalsteps']) ? $_SESSION['survey_' . $surveyid]['totalsteps'] : false;
        if ($iSessionStep && $iSessionStep == $iSessionTotalSteps || $thissurvey['format'] == 'A') {
            $move = "movesubmit";
        } else {
            $move = "movenext";
        }
    }
    return $move;
}
/**
* doHtmlSaveAll return HTML part of saveall button in survey
* @param string $move :
* @return string
**/
function doHtmlSaveAll($move = "")
{
    static $aSaveAllButtons = array();
    if (isset($aSaveAllButtons[$move])) {
        return $aSaveAllButtons[$move];
    }
    $surveyid = Yii::app()->getConfig('surveyID');
    $thissurvey = getsurveyinfo($surveyid);
    $aHtmlOptionsLoadall = array('type' => 'submit', 'id' => 'loadallbtn', 'value' => 'loadall', 'name' => 'loadall', 'class' => "saveall btn btn-default col-xs-12 col-sm-4 submit button hidden");
    $aHtmlOptionsSaveall = array('type' => 'submit', 'id' => 'saveallbtn', 'value' => 'saveall', 'name' => 'saveall', 'class' => "saveall btn btn-default col-xs-12 col-sm-4 submit button hidden");
    if ($thissurvey['allowsave'] == "Y") {
        $sLoadButton = CHtml::htmlButton(gT("Load unfinished survey"), $aHtmlOptionsLoadall);
        $sSaveButton = CHtml::htmlButton(gT("Resume later"), $aHtmlOptionsSaveall);
    } else {
        $sLoadButton = '';
        $sSaveButton = '';
    }
    // Fill some test here, more clear ....
    $bTokenanswerspersistence = $thissurvey['tokenanswerspersistence'] == 'Y' && tableExists('tokens_' . $surveyid);
    $bAlreadySaved = isset($_SESSION['survey_' . $surveyid]['scid']);
    $iSessionStep = isset($_SESSION['survey_' . $surveyid]['step']) ? $_SESSION['survey_' . $surveyid]['step'] : false;
    $iSessionMaxStep = isset($_SESSION['survey_' . $surveyid]['maxstep']) ? $_SESSION['survey_' . $surveyid]['maxstep'] : false;
    $sSaveAllButtons = "";
    // Find out if the user has any saved data
    if ($thissurvey['format'] == 'A') {
        if (!$bTokenanswerspersistence && !$bAlreadySaved) {
            $sSaveAllButtons .= $sLoadButton;
        }
        $sSaveAllButtons .= CHtml::htmlButton(gT("Resume later"), $aHtmlOptionsSaveall);
    } elseif (!$iSessionStep) {
        if (!$bTokenanswerspersistence && !$bAlreadySaved) {
            $sSaveAllButtons .= $sLoadButton;
        }
        if ($thissurvey['showwelcome'] == "N") {
            $sSaveAllButtons .= $sSaveButton;
        }
    } elseif ($iSessionMaxStep == 1 && $thissurvey['showwelcome'] == "N") {
        if (!$bTokenanswerspersistence && !$bAlreadySaved) {
            $sSaveAllButtons .= $sLoadButton;
        }
        $sSaveAllButtons .= $sSaveButton;
    } elseif ($move != "movelast") {
        $sSaveAllButtons .= $sSaveButton;
    }
    $aSaveAllButtons[$move] = $sSaveAllButtons;
    return $aSaveAllButtons[$move];
}
/**
 * doHtmlSaveAll return HTML part of saveall button in survey
 * */
function doHtmlSaveAll($move = "")
{
    $surveyid = Yii::app()->getConfig('surveyID');
    $thissurvey = getsurveyinfo($surveyid);
    $clang = Yii::app()->lang;
    $aHtmlOptionsLoadall = array('type' => 'submit', 'id' => 'loadallbtn', 'value' => 'loadall', 'name' => 'loadall', 'class' => "saveall submit button");
    $aHtmlOptionsSaveall = array('type' => 'submit', 'id' => 'saveallbtn', 'value' => 'saveall', 'name' => 'saveall', 'class' => "saveall submit button");
    if ($thissurvey['active'] != "Y") {
        $aHtmlOptionsLoadall['disabled'] = 'disabled';
        $aHtmlOptionsSaveall['disabled'] = 'disabled';
    }
    $_saveall = "";
    // Find out if the user has any saved data
    if ($thissurvey['format'] == 'A') {
        if ($thissurvey['tokenanswerspersistence'] != 'Y' || !isset($surveyid) || !tableExists('tokens_' . $surveyid)) {
            $_saveall .= CHtml::htmlButton($clang->gT("Load unfinished survey"), $aHtmlOptionsLoadall);
        }
        $_saveall .= CHtml::htmlButton($clang->gT("Resume later"), $aHtmlOptionsSaveall);
    } elseif ($surveyid && (!isset($_SESSION['survey_' . $surveyid]['step']) || !$_SESSION['survey_' . $surveyid]['step'])) {
        //First page, show LOAD (but not save)
        if ($thissurvey['tokenanswerspersistence'] != 'Y' || !isset($surveyid) || !tableExists('tokens_' . $surveyid)) {
            $_saveall .= CHtml::htmlButton($clang->gT("Load unfinished survey"), $aHtmlOptionsLoadall);
        }
    } elseif ($surveyid && (isset($_SESSION['survey_' . $surveyid]['maxstep']) && $_SESSION['survey_' . $surveyid]['maxstep'] == 1) && $thissurvey['showwelcome'] == "N") {
        //First page, show LOAD and SAVE  //First page, show LOAD
        if ($thissurvey['tokenanswerspersistence'] != 'Y' || !isset($surveyid) || !tableExists('tokens_' . $surveyid)) {
            $_saveall .= CHtml::htmlButton($clang->gT("Load unfinished survey"), $aHtmlOptionsLoadall);
        }
        $_saveall .= CHtml::htmlButton($clang->gT("Resume later"), $aHtmlOptionsSaveall);
    } elseif (!isset($_SESSION['survey_' . $surveyid]['scid']) || $move == "movelast") {
        // Not on last page or submited survey
        $_saveall .= CHtml::htmlButton($clang->gT("Resume later"), $aHtmlOptionsSaveall);
    }
    return $_saveall;
}