<?php 
include_once("_dirinfo.php");
include_once(PATH_TO_ROOT."init.php");

//20150528 Tanis nekateri klienti imajo še staro verzijo codemirrorja, ki ima drugačne nastavitve kot verzija na našem strežniku 
// 1 - old verison
// 2 - new version 
//20150625 Tanis added config for codemirror version selection
$codeMirrorVersion = configLoader::loadConfig('webcati');
$codeMirrorVersion = $codeMirrorVersion['codemirror_version'];
//20150625 Tanis END
//$codeMirorVersion = 1;

if($codeMirrorVersion == 1)
    $versionDependetCode = "parser: 'rmmix',"; 
    //$codeMirorVersion == 1?"parser: 'mix":"path: '_plugins/CodeMirror-0.63/js',parser: 'rmmix" . "
else
    $versionDependentCode = "path: '_plugins/CodeMirror-0.63/js',parser:'mixed',";



$projectId = $_POST['id_project'];
$db->ChangeDatabase(DB_WARPIT_WEBCATI_BASE);
$tabId = $_POST['uniqueTabId'];
$tableName = $_POST['projName'];
$qId = intval($_POST['idQuestion']);
$q_name = mysql_real_escape_string($_POST['q_name']);

$reqUrl = "webcati/_ajax/buildQuestionare/randomDataFile/ajax.customFillScript.php";

//let's get the value of the script
$db->ChangeDatabase(DB_WARPIT_WEBCATI);
$script = $db->get($tableName.'_script','id_question = ' . $qId . ' AND id_event = 4','script');
$script = json_encode( $script[0] );



echo "  var tmpForm = Ext.getCmp('" . $tabId . "_tab').add({
                                id:'" . $tabId . "_" . $qId . "_editor' ,
                               xtype: 'uxCodeMirrorPanel',
                             " . $versionDependentCode . "
                                title: 'Script - $q_name',
                                closable: true,
                                listeners: {
                                    render: function(){
                                        this.doLayout();
                                    }
                                },
                                sourceCode: $script,
                                //layout: 'fit',
                                //parser: 'rmmix',
                                onSave: function() 
                                {
                                    new RMajax().request({
                                        url: '$reqUrl',
                                        params: {
                                            action:'saveScript',
                                            database: '".DB_WARPIT_WEBCATI."',
                                            RMtable : '$tableName',
                                            id_question: '$qId',
                                            value: tmpForm.codeMirrorEditor.getCode(),
                                            idEvent: 4
                                        },
                                        success: function(){
                                              var tree = Ext.getCmp('" . $_POST['treeName'] . "');  
                                              tree.getLoader().load(tree.getRootNode());
                                              tree.getRootNode().expand();
                                        }
                                    });
                                },
                                idEvent:1,
                                codeMirror: {
                                    height: '100%',
                                    width: '100%'
                                },
                                listeners:{
                                    
                                    afterrender:function(p){
                                       
                                    }
                                }
                        });
                        tmpForm.show();";
            

?>