function PhraseTesting_block(numberWord, numberWordTranslate, ClWord, ClWordTranslate, animateNoun, animateNounTranslate, 
    inanimateNoun, inanimateNounTranslate, reps, production_text, task_type) {

    // myWordClass: ['Number Word', 'Class Word']
    // myWordTranslate: [['two','three'],['animate','inanimate']]
    // exposure_text: ['exposure text for Num','exposure text for Cl']
    // reps: repetitions for each of the four words (not each word class), so repetitions for each word class is 2*reps 
	var production_trials = [];

    // set buttons (buttons for two words, the Reset button and Submit button)
    var wordButtonLeftPos = [41.5,48,54.5];
    var wordButtonTopPos = [69,69,69];
    var resetButtonLeftPos = 43;
    var resetButtonTopPos = 77.5;
    var submitButtonLeftPos = 52;
    var submitButtonTopPos = 77.5;
    // button properties for choices of words
    var buttonPropertyWords  = 'font-size:1em; font-weight:100; border:#ccc solid; border-radius:10px; border-width:0.12vw; width:5vw; height: 2.8vw; z-index:1';
    // a background disabled button was set to have a boundary to contain all three choices of words
    var backgroundButtonLeftPos = wordButtonLeftPos[0]-1.5;
    var backgroundButtonTopPos = wordButtonTopPos[0]-1.5;
    var buttonPropertyBackground = 'border:#aaa solid; border-radius:10px; border-width:0.12vw; width:21vw; height:4vw; z-index:2, pointer-events:none; background-color:transparent';
    var backgroundButton = '<button class="jspsych-btn" style="position:absolute;left:'+backgroundButtonLeftPos+'%;top:'+backgroundButtonTopPos+'%;'+buttonPropertyBackground+'" disabled="true"></button>';
    // button properties for Reset and Submit
    var buttonPropertyResetSubmit  = 'font-size:0.7em; font-weight:100; border:#ccc solid; border-radius:10px; border-width:0.12vw; width:5v; height: 2vw; background-color:transparent';
    // buttons for Reset and Submit (abled + disabled)
    var reset_bt_abled = '<button class="jspsych-btn" style="position:absolute;left:'+resetButtonLeftPos+'%;top:'+resetButtonTopPos+'%;'+buttonPropertyResetSubmit+'">Reset</button>';
    var reset_bt_disabled = '<button class="jspsych-btn" style="position:absolute;left:'+resetButtonLeftPos+'%;top:'+resetButtonTopPos+'%;'+buttonPropertyResetSubmit+'" disabled="true">Reset</button>';
    var submit_bt_abled = '<button class="jspsych-btn" style="position:absolute;left:'+submitButtonLeftPos+'%;top:'+submitButtonTopPos+'%;'+buttonPropertyResetSubmit+'">Submit</button>';
    var submit_bt_disabled = '<button class="jspsych-btn" style="position:absolute;left:'+submitButtonLeftPos+'%;top:'+submitButtonTopPos+'%;'+buttonPropertyResetSubmit+'" disabled="true">Submit</button>';
    
    // compile all possible combinations (Num + Cl + Noun)
    var NumWord_allTrials = [];
    var NumWord_allTrialsTranslate = [];
    var ClWord_allTrials = [];
    var ClWord_allTrialsTranslate = [];
    var Noun_allTrials = [];
    var Noun_allTrialsTranslate = [];    
    for (var r=0; r<reps; r++){
        for (var num=0; num<numberWord.length; num++){
            // animates
            for (var an=0; an<animateNoun.length; an++){
                NumWord_allTrials = NumWord_allTrials.concat(numberWord[num]);
                NumWord_allTrialsTranslate = NumWord_allTrialsTranslate.concat(numberWordTranslate[num]);
                ClWord_allTrials = ClWord_allTrials.concat(ClWord[ClWordTranslate.indexOf('animate')]); 
                ClWord_allTrialsTranslate = ClWord_allTrialsTranslate.concat('animate');
                Noun_allTrials = Noun_allTrials.concat(animateNoun[an]);
                Noun_allTrialsTranslate = Noun_allTrialsTranslate.concat(animateNounTranslate[an]);
            }
            // inanimates
            for (var inan=0; inan<inanimateNoun.length; inan++){
                NumWord_allTrials = NumWord_allTrials.concat(numberWord[num]);
                NumWord_allTrialsTranslate = NumWord_allTrialsTranslate.concat(numberWordTranslate[num]);
                ClWord_allTrials = ClWord_allTrials.concat(ClWord[ClWordTranslate.indexOf('inanimate')]); 
                ClWord_allTrialsTranslate = ClWord_allTrialsTranslate.concat('inanimate');
                Noun_allTrials = Noun_allTrials.concat(inanimateNoun[inan]);
                Noun_allTrialsTranslate = Noun_allTrialsTranslate.concat(inanimateNounTranslate[inan]);
            }
        }
    }
    // if repetitions==1 then:
    // NumWord_allTrialsTranslate: ['one','one','one','one','one','one','one','one','two','two','two','two','two','two','two','two']
    // ClWord_allTrialsTranslate: ['animate','animate','animate','animate','inanimate','inanimate','inanimate','inanimate','animate','animate','animate','animate','inanimate','inanimate','inanimate','inanimate']
    // Noun_allTrialsTranslate: ['deer','lion','peacock','chicken','ship','coat','wheel','house','deer','lion','peacock','chicken','ship','coat','wheel','house'];

    var total_number_of_trials = NumWord_allTrials.length;
    // randomise trial sequence
    // make sure that: (1) no more than two consecutive words appear for any of the Number, Cl or Noun words; and
    // (2) no exact same phrase will appear consecutively
	var if_consecutive_items = true;
    var max_num_consecutive_items = 2;
    while (if_consecutive_items == true){
        var seq = range(0,100).slice(0,total_number_of_trials);
        seq = shuffle(seq);
        var NumWord_allTrials_reorder = [];
        var ClWord_allTrials_reorder = [];
        var Noun_allTrials_reorder = [];
        var Phrase_allTrials_reorder = [];
        for (var n=0; n<total_number_of_trials; n++){
            NumWord_allTrials_reorder[n] = NumWord_allTrials[seq[n]];
            ClWord_allTrials_reorder[n] = ClWord_allTrials[seq[n]];
            Noun_allTrials_reorder[n] = Noun_allTrials[seq[n]];
            Phrase_allTrials_reorder[n] = [NumWord_allTrials[seq[n]],ClWord_allTrials[seq[n]],Noun_allTrials[seq[n]]];
        }
        var if_consecutive_items1 = if_consecutive(NumWord_allTrials_reorder, max_num_consecutive_items+1); // for Num
        var if_consecutive_items2 = if_consecutive(ClWord_allTrials_reorder, max_num_consecutive_items+1); // for Cl
        var if_consecutive_items3 = if_consecutive(Noun_allTrials_reorder, max_num_consecutive_items+1); // for nouns
        var if_consecutive_items4 = if_consecutive(Phrase_allTrials_reorder, 2); // for phrases
        if_consecutive_items = (if_consecutive_items1)|(if_consecutive_items2)|(if_consecutive_items3)|(if_consecutive_items4); // need to be all false to end the loop
    };

    // randomise word button sequence
    var if_consecutive_items = true;
    var max_num_consecutive_items = 2;
    while (if_consecutive_items == true){
        var buttonPosSeq = [];
        for (var t=0; t<total_number_of_trials; t++){
            buttonPosSeq[t] = shuffle([0,1,2]);
        }
        if_consecutive_items = if_consecutive(buttonPosSeq, max_num_consecutive_items+1);		
        // ensure that a same button sequence does not present more than twice consecutively
    };

    // *************** construct the trials *********************
    var durAfterSubmit = 500; // time to stay after the answer is submitted; can use this time to save data
    // initialise the numbers of produced word orders
    var Noun_Cl_Num = 0;
    var Noun_Num_Cl = 0;
    var Num_Noun_Cl = 0;
    var Num_Cl_Noun = 0;
    var Cl_Noun_Num = 0;
    var Cl_Num_Noun = 0;

    console.log(NumWord_allTrials)
    console.log(seq)
    for (var t = 0; t < total_number_of_trials; t++) {
        
        var numName = NumWord_allTrials[seq[t]];
        var numNameTranslate = NumWord_allTrialsTranslate[seq[t]];
        var ClName = ClWord_allTrials[seq[t]];
        var ClNameTranslate = ClWord_allTrialsTranslate[seq[t]];
        var nounName = Noun_allTrials[seq[t]];
        var nounNameTranslate = Noun_allTrialsTranslate[seq[t]];
        var orientation = shuffle(['','']);
        orientation = orientation[0];
        var image_name = numNameTranslate + ' ' + nounNameTranslate;

        // assign images
        var image_blank = '<img src="'+img_path+"empty_image.png"+'" class="Cntr0" >';
        var image = [];
        image[0] = image_blank;
        image[1] = '<img src="'+img_path+image_name+orientation+'.jpg" class="Cntr0" >';

        // assign buttons for the words (noun and target)
        var btPosSeq = buttonPosSeq[t];
        var wordButton_abled = [];
        var wordButton_disabled = [];
        var wordContent = [nounName,ClName,numName]; // button seq set as [noun, Cl, Num]
        var wordContentTranslate = [nounNameTranslate,ClNameTranslate,numNameTranslate];
        for (var w = 0; w < wordContent.length; w++) {
            wordButton_abled[w] = '<button class="jspsych-btn" style="position:absolute;left:'+wordButtonLeftPos[btPosSeq[w]]+
               '%;top:'+wordButtonTopPos[btPosSeq[w]]+'%;'+buttonPropertyWords+'"> '+wordContent[w]+' </button>';
            wordButton_disabled[w] = '<button class="jspsych-btn" style="position:absolute;left:'+wordButtonLeftPos[btPosSeq[w]]+
               '%;top:'+wordButtonTopPos[btPosSeq[w]]+'%;'+buttonPropertyWords+'" disabled="true"> '+wordContent[w]+' </button>';
        }
        
        production_trials.push({trl_num:t+1, num_name:numName, num_trnsl:numNameTranslate, cl_name:ClName, 
            cl_trnsl:ClNameTranslate, noun:nounName, noun_trnsl:nounNameTranslate,img:image, img_blank:image_blank, ortt:orientation, 
            wrd_content:wordContent, wrd_content_trnsl:wordContentTranslate, wrd_button_pos_seq:btPosSeq, wrd_button_abled:wordButton_abled, 
            wrd_button_disabled:wordButton_disabled});
    }

	// for save csv
	var csv_header = ['','trial_overall','WordsOfChoice (Noun-Num-Cl)','WordsOfChoiceTranslate','Noun_buttonPos','ClWord_buttonPos',
        'NumberWord_buttonPos','orientation','ProducedPhrase','ProducedPhraseTranslate','Noun-Cl-Num (accmlt)','Noun-Num-Cl (accmlt)',
        'Num-Noun-Cl (accmlt)','Num-Cl-Noun (accmlt)','Cl-Noun-Num (accmlt)','Cl-Num-Noun (accmlt)','saveTime\n'];
	var data_to_save = csv_header;

    //// ******************* trial setup according to the randomised pairing *****************************
    var new_trial = true;
    var trial_loop_dur = 100000000;
    var selectedWords = [];
    var selectedWordsTranslate = [];
    var resp = null;
    var phrase = '';
    var phraseTranslate = '';

    var show_image = {
        type: 'html-keyboard-response',
        stimulus: jsPsych.timelineVariable('img'), // calls image stimuli
        choices: jsPsych.NO_KEYS,
        prompt: function(){new_trial = true; return production_text},
        response_ends_trial: false,
        trial_duration: 800
    };

    var make_choice = {
        type: 'html-button-response',
        stimulus: jsPsych.timelineVariable('img'),
        prompt: function(){
            // *** ALWAYS keep the button choice sequence as [word1 (noun), word2 (Cl), word3 (Num), Reset, Submit]
            if (new_trial==true){ // initialise resp and selected words at the start of the trial
                resp=null; // seen from actual tests, this is actually not necessary...
                phrase='';
                phraseTranslate = '';
                trial_loop_dur = 100000000;
                feedback = '';
            }; 
            resp = jsPsych.data.get().last(1).values()[0].button_pressed // the last response
            if (trial_loop_dur==durAfterSubmit){trial_loop_dur=1}; // if submitted, set trial_loop_dur to minimum so the loop completes quickly
            var words = jsPsych.timelineVariable('wrd_content',true);
            var wordsTranslate = jsPsych.timelineVariable('wrd_content_trnsl',true);
            var noun_name = jsPsych.timelineVariable('noun',true);
            var number_name = jsPsych.timelineVariable('num_name',true);
            var Cl_name = jsPsych.timelineVariable('cl_name',true);
            if (resp<3&(resp!=null)) { // if word(s) are selected then show the words
                selectedWords = selectedWords.concat(words[resp]);
                selectedWordsTranslate = selectedWordsTranslate.concat(wordsTranslate[resp]);
                phrase = phrase + words[resp] + ' ';
                phraseTranslate = phraseTranslate + wordsTranslate[resp] + ' ';
                console.log(selectedWords)
                console.log(phrase)
                console.log(phraseTranslate)
            }
            if (resp==3) { // if Reset is pressed then empty the shown words
                selectedWords = [];
                selectedWordsTranslate = [];
                phrase = '';
                phraseTranslate = '';
            }
            if (resp==4) { // if Submit is pressed then show the words and save data
                // assign accumulative numbers for particular word orders according to the produced phrase
                var wordOrderAccmlt_previousTrl = [Noun_Cl_Num, Noun_Num_Cl, Num_Noun_Cl, Num_Cl_Noun, Cl_Noun_Num, Cl_Num_Noun];
                if ((selectedWords[0]==noun_name)&(selectedWords[1]==Cl_name)&(selectedWords[2]==number_name)){
                    Noun_Cl_Num +=1}
                if ((selectedWords[0]==noun_name)&(selectedWords[1]==number_name)&(selectedWords[2]==Cl_name)){
                    Noun_Num_Cl +=1}
                if ((selectedWords[0]==number_name)&(selectedWords[1]==noun_name)&(selectedWords[2]==Cl_name)){
                    Num_Noun_Cl +=1}
                if ((selectedWords[0]==number_name)&(selectedWords[1]==Cl_name)&(selectedWords[2]==noun_name)){
                    Num_Cl_Noun +=1}
                if ((selectedWords[0]==Cl_name)&(selectedWords[1]==noun_name)&(selectedWords[2]==number_name)){
                    Cl_Noun_Num +=1}
                if ((selectedWords[0]==Cl_name)&(selectedWords[1]==number_name)&(selectedWords[2]==noun_name)){
                    Cl_Num_Noun +=1}   
                selectedWords = []; // set back to empty
                trial_loop_dur = durAfterSubmit;
                var wordOrderAccmlt_currTrl = [Noun_Cl_Num, Noun_Num_Cl, Num_Noun_Cl, Num_Cl_Noun, Cl_Noun_Num, Cl_Num_Noun];
                var wordOrder_currTrl = [];
                for (var s = 0; s < wordOrderAccmlt_currTrl.length; s++){
                    wordOrder_currTrl[s] = wordOrderAccmlt_currTrl[s]-wordOrderAccmlt_previousTrl[s];
                }
                // ----------------------------- SAVE DATA (csv)------------------------------------
                var trl_num_overall = jsPsych.timelineVariable('trl_num',true);
                var wordButtonPosSeq = jsPsych.timelineVariable('wrd_button_pos_seq',true);
                var btPos = ['left','mid','right'];
                var nounPos = btPos[wordButtonPosSeq[0]];
                var numPos = btPos[wordButtonPosSeq[1]];
                var ClPos = btPos[wordButtonPosSeq[2]];
                var orient = jsPsych.timelineVariable('ortt',true);
                if (orient==''){orient='NA'};
                var time = getTime();
                var data_snglTrl = [trl_num_overall, words[0]+' '+words[1]+' '+words[2], wordsTranslate[0]+' '+wordsTranslate[1]+' '+wordsTranslate[2],
                          nounPos, ClPos, numPos, orient, phrase, phraseTranslate];
                data_snglTrl = data_snglTrl.concat(wordOrder_currTrl).concat([time+'\n']);
                data_to_save = data_to_save.concat(data_snglTrl);
                // calculate overall correctness when finish all trials
                if (trl_num_overall==total_number_of_trials) {
                    var Noun_Num_Cl_percent = Noun_Num_Cl/total_number_of_trials*100;
                    var Noun_Cl_Num_percent = Noun_Cl_Num/total_number_of_trials*100;
                    var Num_Noun_Cl_percent = Num_Noun_Cl/total_number_of_trials*100;
                    var Num_Cl_Noun_percent = Num_Cl_Noun/total_number_of_trials*100;
                    var Cl_Num_Noun_percent = Cl_Num_Noun/total_number_of_trials*100;
                    var Cl_Noun_Num_percent = Cl_Noun_Num/total_number_of_trials*100;
                    var score_summary_response_number = ['\n','overall_percent', '', '', '', '', '', '', '', '', Noun_Cl_Num, Noun_Num_Cl, Num_Noun_Cl, 
                             Num_Cl_Noun, Cl_Noun_Num, Cl_Num_Noun];
                    var score_summary_percent = ['\n','overall_percent', '', '', '', '', '', '', '', '', Noun_Cl_Num_percent, Noun_Num_Cl_percent, 
                            Num_Noun_Cl_percent, Num_Cl_Noun_percent, Cl_Noun_Num_percent, Cl_Num_Noun_percent];
                    var score_summary = score_summary_response_number.concat(score_summary_percent);
                    data_to_save = data_to_save.concat(score_summary);
                }
                var data_to_save_string = data_to_save.toString();
                var csvName = window.saveFileNameHeader+'_'+task_type+'.csv';
                saveData(csvName,data_to_save_string);
                // ----------------------------- SAVE DATA (csv)-----------------------------------
                // save a final file that summarise everything
                if (trl_num_overall==total_number_of_trials) {
                    var final_csvName = window.saveFileNameHeader+'_allScoresSummary.csv';
                    var prolificID = ['','ProlificID:'].concat(window.prolificID);
                    var noun_test_summary = ['Noun_testing'].concat(window.noun_testing_summary);
                    var NumCl_match_summary = ['NumCl_matching'].concat(window.NumCl_matching_summary);
                    var CmbPrd_summary = ['Combination_Production'].concat(window.CmbPrd_summary);
                    var phrase_summary_header = ['Final_Phrase_Test(percent)'+'\n', 'Noun_Cl_Num', 'Noun_Num_Cl', 
                          'Num_Noun_Cl', 'Num_Cl_Noun', 'Cl_Noun_Num', 'Cl_Num_Noun'+'\n'];
                    var phrase_percent_summary = [Noun_Cl_Num_percent, Noun_Num_Cl_percent, Num_Noun_Cl_percent, 
                           Num_Cl_Noun_percent, Cl_Noun_Num_percent, Cl_Num_Noun_percent];
                    var final_summary = [];
                    final_summary.push(prolificID);
                    final_summary.push(noun_test_summary);
                    final_summary.push(NumCl_match_summary);
                    final_summary.push(CmbPrd_summary);
                    final_summary.push(phrase_summary_header);
                    final_summary.push(phrase_percent_summary);
                    var data_to_save_string = final_summary.toString();
                    saveData(final_csvName,data_to_save_string);
                }
            }
            phrase_on_screen = '<div class="bottom5"><strong>'+phrase+'</strong></div>';
            var prompt_to_return = phrase_on_screen + production_text;
            new_trial=false;
            return prompt_to_return
        },
		choices: function(){
            // *** ALWAYS keep the button choice sequence as [word1 (noun), word2 (Cl), word3 (Num), Reset, Submit]
            var bt_choice = [];
            var wrd_bt_abled = jsPsych.timelineVariable('wrd_button_abled',true);
            var wrd_bt_disabled = jsPsych.timelineVariable('wrd_button_disabled',true);
            resp = jsPsych.data.get().last(1).values()[0].button_pressed // the last response
            if (selectedWords.length<1) {// if there is no word is selected then set Reset and Submit buttons disabled
                bt_choice = [wrd_bt_abled[0],wrd_bt_abled[1],wrd_bt_abled[2],reset_bt_disabled,submit_bt_disabled];
            } else if (selectedWords.length==1) { // if there is one word is selected then set the selected words' button and Submit disabled but the non-selected word and Reset as abled
                if (resp==0){// if the selected word is the first word
                    bt_choice = [wrd_bt_disabled[0],wrd_bt_abled[1],wrd_bt_abled[2],reset_bt_abled,submit_bt_disabled];
                } else if (resp==1){ // if the selected word is the second word
                    bt_choice = [wrd_bt_abled[0],wrd_bt_disabled[1],wrd_bt_abled[2],reset_bt_abled,submit_bt_disabled];
                } else { // if the selected word is the third word
                    bt_choice = [wrd_bt_abled[0],wrd_bt_abled[1],wrd_bt_disabled[2],reset_bt_abled,submit_bt_disabled];
                }
            } else if (selectedWords.length==2) { // if there are two words selected then set selected words' buttons and Submit disabled but the unselected word and Reset as abled
                var words = jsPsych.timelineVariable('wrd_content',true);
                if (!selectedWords.includes(words[0])){ // if the first word has not been selected
                    bt_choice = [wrd_bt_abled[0],wrd_bt_disabled[1],wrd_bt_disabled[2],reset_bt_abled,submit_bt_disabled];
                } else if (!selectedWords.includes(words[1])){ // if the second word has not been selected
                    bt_choice = [wrd_bt_disabled[0],wrd_bt_abled[1],wrd_bt_disabled[2],reset_bt_abled,submit_bt_disabled];
                } else { // if the third word has not been selected
                    bt_choice = [wrd_bt_disabled[0],wrd_bt_disabled[1],wrd_bt_abled[2],reset_bt_abled,submit_bt_disabled];
                }
            } else { // if all three words are slected then set all words' buttons disabled but Reset and Submit as abled
                bt_choice = [wrd_bt_disabled[0],wrd_bt_disabled[1],wrd_bt_disabled[2],reset_bt_abled,submit_bt_abled];
            }
            if (trial_loop_dur<=durAfterSubmit){ // additionally if it is at after-submit mode or later then disable all buttons
                bt_choice = [wrd_bt_disabled[0],wrd_bt_disabled[1],wrd_bt_disabled[2],reset_bt_disabled,submit_bt_disabled];    
            }
            bt_choice = bt_choice.concat(backgroundButton);
            return bt_choice
        },    
        response_ends_trial: true,
        trial_duration: function(){return trial_loop_dur}
    }

    var blank_page = {
		type: 'html-keyboard-response',
		choices: jsPsych.NO_KEYS,
		stimulus: jsPsych.timelineVariable('img_blank'),
		trial_duration: 100
	    };

    var timeline_trials = [];
    timeline_trials.push(show_image)
    for (var n=0; n<100; n++) {timeline_trials.push(make_choice)};
    timeline_trials.push(blank_page);

	// put them together
	var production_trial = {
        timeline: timeline_trials,
        // add the trials as a variable
        timeline_variables: production_trials,
    };
 
 return production_trial

}