Advertisement
lamiastella

IAT139.js

Jun 20th, 2014
325
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Loads the input file into the variable "startIAT"
  2. function initialize()
  3. {  
  4.  
  5.     $.getJSON("dfh", startIAT);
  6.  
  7. }
  8.  
  9. // Initialize variables, build page & data object, display instructions
  10. function startIAT(data)
  11. {
  12.     currentState = "instruction";
  13.     session = 0;
  14.     roundnum = 0;
  15.     input = data;
  16.     instrCount = 0; // for the instructions with multiple pages
  17.     forward = false; // what is this for??
  18.     isFive = false;
  19.     thisHash = "";
  20.     var d = new Date();
  21.     thisHash = "AU" + d.getTime() + "GB";
  22.  
  23.    
  24.     // default to show results to participant
  25.     if (!('showResult' in input))
  26.     {
  27.         input.showResult = true;
  28.     }
  29.    
  30.     // make the target or association words green
  31.         openA = "<font class='size_me' color=black>";
  32.         closeA = "</font>";
  33.        
  34.         open1 = "<font class='size_me' color=black>";
  35.         close1 = "</font>";
  36.  
  37.     buildPage();
  38.     roundArray = initRounds();
  39.     instructionPage();
  40. }
  41.  
  42. // Adds all images to page (initially hidden) so they are pre-loaded for IAT
  43. function buildPage()
  44. {
  45.    
  46.     if (input.catA.itemtype == "img")
  47.     {
  48.         for (i in input.catA.items)
  49.         {
  50.             var itemstr = '<img id="'+input.catA.datalabel+i+'" class="IATitem" src="'+input.catA.items[i]+'">';
  51.             $("#exp_instruct").after(itemstr);
  52.         }
  53.     }
  54.     if (input.catB.itemtype == "img")
  55.     {
  56.         for (i in input.catB.items)
  57.         {
  58.             var itemstr = '<img id="'+input.catB.datalabel+i+'" class="IATitem" src="'+input.catB.items[i]+'">';
  59.             $("#exp_instruct").after(itemstr);
  60.         }
  61.     }
  62.     if (input.cat1.itemtype == "img")
  63.     {
  64.         for (i in input.cat1.items)
  65.         {
  66.             var itemstr = '<img id="'+input.cat1.datalabel+i+'" class="IATitem" src="'+input.cat1.items[i]+'">';
  67.             $("#exp_instruct").after(itemstr);
  68.         }
  69.     }
  70.     if (input.cat2.itemtype == "img")
  71.     {
  72.         for (i in input.cat2.items)
  73.         {
  74.             var itemstr = '<img id="'+input.cat2.datalabel+i+'" class="IATitem" src="'+input.cat2.items[i]+'">';
  75.             $("#exp_instruct").after(itemstr);
  76.         }
  77.     }
  78. }
  79.  
  80. // Round object
  81. function IATround()
  82. {
  83.     this.starttime = 0;
  84.     this.endtime = 0;
  85.     this.itemtype = "none";
  86.     this.category = "none";
  87.     this.catIndex = 0;
  88.  
  89.     this.correct = 0;
  90.     this.errors = 0;
  91. }
  92.  
  93. // shuffles an array
  94. function shuffle(o){
  95.     for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
  96.     return o;
  97. }
  98.  
  99. // Create array for each session & round, with pre-randomized ordering of images
  100. function initRounds()
  101. {
  102. //############## Begin code for specific order ####################
  103. var catOrder = new Array();
  104. var rdex = new Array();  
  105. //############## end specific order code  #####################
  106.  
  107. //used in generating random order
  108. var catAdex = new Array();  
  109. var catBdex = new Array();
  110. var cat1dex = new Array();
  111. var cat2dex = new Array();  
  112.  
  113.     for(k=0; k< input.catA.items.length; k++)
  114.     { catAdex[k] = k; }
  115.     for(k=0; k< input.catB.items.length; k++)
  116.     { catBdex[k] = k; }
  117.     for(k=0; k< input.cat1.items.length; k++)
  118.     { cat1dex[k] = k; }
  119.     for(k=0; k< input.cat2.items.length; k++)
  120.     { cat2dex[k] = k; }
  121.  
  122. var ca;
  123. var cb;
  124. var c1;
  125. var c2;
  126.  
  127.  
  128.     var roundArray = [];
  129.     // for each session
  130.     for (var i=0; i<9; i++)
  131.     {
  132.  
  133.         catAdex = shuffle(catAdex);
  134.         catBdex = shuffle(catBdex);
  135.         cat1dex = shuffle(cat1dex);
  136.         cat2dex = shuffle(cat2dex);
  137.         ca = 0;
  138.         cb = 0;
  139.         c1 = 0;
  140.         c2 = 0;
  141.        
  142.         roundArray[i] = [];
  143.         switch (i)
  144.         {
  145.             case 0:
  146.                 stype = "target";
  147.                 numrounds = 18;
  148.                 catOrder = [1, 2, 1, 1, 2, 2, 2, 1, 2, 2, 1, 1, 1, 1, 2, 2, 1, 2];
  149.                 rdex = [9, 7, 1, 4, 5, 3, 4, 8, 6, 8, 7, 3, 2, 6, 9, 2, 5, 1];
  150.                 break;
  151.             case 1:
  152.                 stype = "association";
  153.                 numrounds = 18;
  154.                 catOrder = [3, 4, 3, 3, 3, 4, 4, 4, 3, 4, 3, 4, 3, 3, 3, 4, 4, 4];
  155.                 rdex = [6, 2, 3, 9, 7, 6, 1, 4, 5, 7, 4, 5, 2, 8, 1, 9, 3, 8];
  156.                 break;
  157.             case 2:    
  158.                 stype = "both";
  159.                 numrounds = 4;
  160.                 catOrder = [2, 3, 1, 4];
  161.                 rdex = [0, 0, 0, 0, ];
  162.                 break;
  163.             case 3:
  164.                 stype = "both";
  165.                 numrounds = 39;
  166.                 catOrder = [1, 3, 4, 2, 3, 1, 3, 1, 2, 3, 2, 1, 4, 1,
  167.                             4, 3, 2, 4, 3, 2, 3, 4, 4, 2, 1, 4, 4, 2,
  168.                             4, 1, 2, 4, 1, 2, 3, 3, 1, 3, 1];
  169.                 rdex = [8, 6, 5, 1, 8, 6, 4, 1, 2, 9, 6, 9, 3, 8, 2,
  170.                          5, 5, 4, 7, 6, 1, 8, 7, 8, 5, 7, 6, 4, 1, 2,
  171.                          4, 6, 5, 3, 6, 2, 9, 7, 7];
  172.                 break;
  173.            
  174.  
  175.             case 4:
  176.  
  177.                 stype = "both";
  178.                 numrounds = 33;
  179.                 catOrder = [ 3, 2, 3,
  180.                             2, 3, 2, 3, 4, 1, 1, 4, 3, 2, 4, 2, 1, 2,
  181.                             4, 4, 1, 4, 3, 2, 1, 4, 1, 2, 3, 2, 3, 1,
  182.                             4, 1];
  183.                 rdex = [ 5, 1, 9, 9, 3, 8,
  184.                          2, 5, 1, 2, 4, 8, 3, 1, 5, 4, 7, 8, 3, 3, 9,
  185.                          4, 2, 7, 2, 6, 9, 1, 7, 3, 3, 9, 4];
  186.                 break;
  187.  
  188.             case 5:
  189.                  stype = "target";
  190.                 numrounds = 18;
  191.                 catOrder = [2, 2, 1, 2, 1, 2, 1, 1, 2, 1, 1, 1, 1, 1, 2, 2, 2, 2];
  192.                 rdex = [3, 2, 1, 4, 7, 1, 8, 9, 7, 5, 6, 3, 4, 2, 5, 8, 9, 6];
  193.                 break;
  194.             case 6:
  195.                 stype = "both";
  196.                 numrounds = 4;
  197.                 catOrder = [2, 3, 1, 4];
  198.                 rdex = [0, 0, 0, 0];
  199.                 break;
  200.             case 7:
  201.                 stype = "both";
  202.                 numrounds = 39;
  203.                 catOrder = [3, 1, 4, 4, 1, 4, 4, 2, 1, 4, 1, 4, 2, 1,
  204.                             3, 3, 3, 2, 4, 2, 4, 1, 2, 3, 1, 1, 3, 2,
  205.                             2, 3, 1, 3, 3, 1, 4, 2, 3, 4, 4];
  206.                 rdex = [6, 4, 1, 2, 2, 9, 6, 3, 5, 5, 3, 1, 2, 9, 7,
  207.                         6, 9, 6, 3, 8, 6, 1, 7, 7, 5, 2, 5, 6, 3, 3,
  208.                         7, 9, 2, 4, 7, 5, 1, 4, 7];
  209.                 break;
  210.             case 8:
  211.                 stype = "both";
  212.                 numrounds = 33;
  213.                 catOrder = [1, 4, 1,
  214.                             2, 4, 1, 1, 4, 2, 3, 4, 2, 3, 3, 2, 4, 3,
  215.                             3, 1, 2, 4, 2, 1, 2, 3, 3, 2, 2, 3, 2, 1,
  216.                             4, 1];
  217.                 rdex = [ 6, 9, 8, 2, 5, 7,
  218.                         3, 8, 5, 8, 4, 8, 5, 4, 9, 8, 4, 1, 8, 4, 3,
  219.                         7, 1, 9, 8, 2, 1, 4, 3, 1, 9, 2, 6];
  220.                 break;
  221.            
  222.         }
  223.        
  224.  
  225.  
  226.         prevIndexA = -1; prevIndex1 = -1;
  227.  
  228.         for (var j = 0; j<numrounds; j++)
  229.         {
  230.            
  231.             var round = new IATround();
  232.             var roundSet = false;
  233.             var thisSwitch = catOrder[j];
  234.  
  235.              switch (thisSwitch)
  236.             {
  237.                 case 1:
  238.                     round.category = input.catA.datalabel;
  239.                     break;
  240.  
  241.                 case 2:
  242.                     round.category = input.catB.datalabel;
  243.                     break;
  244.  
  245.                 case 3:
  246.                     round.category = input.cat1.datalabel;
  247.                     break;
  248.                 case 4:
  249.                     round.category = input.cat2.datalabel;
  250.                     break;
  251.  
  252.             }
  253.            
  254.          
  255.            /* if (stype == "target")
  256.             {
  257.                 round.category = (Math.random() < 0.5 ? input.catA.datalabel : input.catB.datalabel);
  258.             }
  259.             else if (stype == "association")
  260.             {
  261.                 round.category = (Math.random() < 0.5 ? input.cat1.datalabel : input.cat2.datalabel);  
  262.             }
  263.             else if (stype == "both")
  264.             {
  265.                 if (j % 2 == 0) { round.category = (Math.random() < 0.5 ? input.catA.datalabel : input.catB.datalabel); }
  266.                 else { round.category = (Math.random() < 0.5 ? input.cat1.datalabel : input.cat2.datalabel); }
  267.             }*/
  268.  
  269.  
  270.  
  271.  
  272.             // pick a category
  273.         if (round.category == input.catA.datalabel)
  274.             {
  275.  
  276.                    
  277.                     // pick an item different from the last
  278.                     //while (prevIndexA == round.catIndex)
  279.                         //{
  280.     //round.catIndex = Math.floor(Math.random()*input.catA.items.length);
  281.                    // }
  282.                     //prevIndexA = round.catIndex;
  283.                 if (ca >=input.catA.items.length)
  284.                     {
  285.                 round.itemtype = input.catB.itemtype;
  286.                     if (i < 4) { round.correct = 2; }
  287.                     else { round.correct = 1; }
  288.                 round.category = input.catB.datalabel          
  289.                     round.catIndex = rdex[j];//catBdex[cb];
  290.                     cb = cb+1;     
  291.                 roundSet = true;
  292.                     }
  293.                 else
  294.                     {
  295.                     round.itemtype = input.catA.itemtype;
  296.                     if (i < 4) { round.correct = 1; }
  297.                     else { round.correct = 2; }
  298.                 round.catIndex = rdex[j];//catAdex[ca];
  299.                     ca = ca+1;
  300.                     }
  301.             }
  302.             else if (round.category == input.catB.datalabel)
  303.             {
  304.  
  305.                     // pick an item different from the last
  306.                     //while (prevIndexA == round.catIndex)
  307.                      //   { round.catIndex = Math.floor(Math.random()*input.catB.items.length); }
  308.                     //prevIndexA = round.catIndex;
  309.             if(!roundSet){
  310.                 if (cb >=input.catB.items.length)
  311.                     {
  312.                 round.itemtype = input.catA.itemtype;
  313.                     if (i < 4) { round.correct = 1; }
  314.                     else { round.correct = 2; }        
  315.                 round.category = input.catA.datalabel          
  316.                     round.catIndex = rdex[j];// catAdex[ca];
  317.                     ca = ca+1;     
  318.  
  319.                     }
  320.                 else
  321.                     {
  322.                     round.itemtype = input.catB.itemtype;
  323.                     if (i < 4) { round.correct = 2; }
  324.                     else { round.correct = 1; }
  325.                 round.catIndex = rdex[j];//catBdex[cb];
  326.                     cb = cb+1;
  327.                     }
  328.                     }
  329.             }
  330.             else if (round.category == input.cat1.datalabel)
  331.             {
  332.  
  333.                    
  334.                     // pick an item different from the last
  335.                     //while (prevIndexA == round.catIndex)
  336.                         //{
  337.     //round.catIndex = Math.floor(Math.random()*input.catA.items.length);
  338.                    // }
  339.                     //prevIndexA = round.catIndex;
  340.                 if (c1 >=input.cat1.items.length)
  341.                     {
  342.                 round.itemtype = input.cat2.itemtype;
  343.                      round.correct = 2;
  344.                    
  345.                 round.category = input.cat2.datalabel          
  346.                     round.catIndex =  rdex[j]; //cat2dex[c2];
  347.                     c2 = c2+1;     
  348.                 roundSet = true;
  349.                     }
  350.                 else
  351.                     {
  352.                     round.itemtype = input.cat1.itemtype;
  353.                     round.correct = 1;
  354.                    
  355.                 round.catIndex =  rdex[j]; //cat1dex[c1];
  356.                     c1 = c1+1;
  357.                     }
  358.             }
  359.             else if (round.category == input.cat2.datalabel)
  360.             {
  361.  
  362.                         // pick an item different from the last
  363.                         //while (prevIndexA == round.catIndex)
  364.                          //   { round.catIndex = Math.floor(Math.random()*input.catB.items.length); }
  365.                         //prevIndexA = round.catIndex;
  366.                 if(!roundSet){
  367.                     if (c2 >=input.cat2.items.length)
  368.                         {
  369.                     round.itemtype = input.cat1.itemtype;
  370.                         round.correct = 1;
  371.                                    
  372.                     round.category = input.cat1.datalabel          
  373.                         round.catIndex =  rdex[j]; //cat1dex[c1];
  374.                         c1 = c1+1;     
  375.  
  376.                         }
  377.                     else
  378.                         {
  379.                         round.itemtype = input.cat2.itemtype;
  380.                          round.correct = 2;
  381.                        
  382.                     round.catIndex =  rdex[j]; //cat2dex[c2];
  383.                         c2 = c2+1;
  384.                         }
  385.                     }
  386.             }
  387.             roundArray[i].push(round);
  388.         }
  389.     }
  390.    
  391.     return roundArray;
  392. }
  393.  
  394. function setLabels()
  395. {
  396.     switch (session)
  397.     {
  398.         case 0://A and B
  399.             if (input.catA.labeltype = "img")
  400.             {
  401.                 $('#left_cat').html(openA+input.catA.label+closeA);
  402.             }
  403.             else
  404.             {
  405.                 $('#left_cat').ready(function() { $('#left_cat').html(openA+input.catA.label+closeA); });
  406.             }
  407.             if (input.catB.labeltype = "img")
  408.             {
  409.                 $('#right_cat').html(openA+input.catB.label+closeA);
  410.             }
  411.             else
  412.             {
  413.                 $('#right_cat').ready(function() { $('#right_cat').html(openA+input.catB.label+closeA); });
  414.             }          
  415.             break;
  416.         case 1:    //1 and 2
  417.             $("#left_cat").html(open1+input.cat1.label+close1);
  418.             $("#right_cat").html(open1+input.cat2.label+close1);
  419.             break;
  420.         case 2:
  421.         alert("case 2 is here!");
  422.         case 3:     //A or 1 and B or 2
  423.  
  424.             alert("case 3 is here!"); //never gets printed!!! WHY?
  425.             $("#left_cat").html(openA+input.catA.label+closeA+'<br>'+open1+input.cat1.label+close1);
  426.             $("#right_cat").html(openA+input.catB.label+closeA+'<br>'+open1+input.cat2.label+close1);
  427.             //$('#left_cat').html(openA+input.catA.label+closeA);
  428.             //$('#right_cat').prepend('<img id="right" src="PuzzlePiece3_cropped.png" />')
  429.             break;
  430.         case 4:
  431.            alert("case 4 is here!"); //never gets printed!!! WHY?
  432.         /*
  433.             $("#left_cat").html(openA+input.catA.label+closeA+'<br>'+open1+input.cat1.label+close1);
  434.             $("#right_cat").html(openA+input.catB.label+closeA+'<br>'+open1+input.cat2.label+close1);
  435.  
  436.             */
  437.             //$('#left_cat').html(openA+input.catA.label+closeA);
  438.  
  439.             //$('#right_cat').prepend('<img id="right" src="PuzzlePiece3_cropped.png" />')
  440.             break;
  441.  
  442.         case 5:     //B and A
  443.             $("#left_cat").html("");
  444.             $("#right_cat").html("");
  445.             if (input.catA.labeltype = "img")
  446.             {
  447.                 $('#right_cat').html(openA+input.catA.label+closeA);
  448.             }
  449.             else
  450.             {
  451.                 $('#right_cat').ready(function() { $('#left_cat').html(openA+input.catA.label+closeA); });
  452.             }
  453.             if (input.catB.labeltype = "img")
  454.             {
  455.                 $('#left_cat').html(openA+input.catB.label+closeA);
  456.             }
  457.             else
  458.             {
  459.                 $('#left_cat').ready(function() { $('#right_cat').html(openA+input.catB.label+closeA); });
  460.             }          
  461.             break;
  462.         case 6:
  463.         case 7:     //B or 1 and A and 2
  464.            
  465.             $("#left_cat").html(openA+input.catB.label+closeA+'<br>'+open1+input.cat1.label+close1);
  466.             $("#right_cat").html(openA+input.catA.label+closeA+'<br>'+open1+input.cat2.label+close1);
  467.             //$('#right_cat').html(openA+input.catA.label+closeA);
  468.             //$('#left_cat').prepend('<img id="right" src="PuzzlePiece3_cropped.png" />')
  469.             break;
  470.         case 8:
  471.     }
  472.  
  473. }
  474.  
  475. // insert instruction text based on stage in IAT
  476. function instructionPage()
  477. {  
  478.  
  479.     document.getElementById("header").style.height='5px';
  480.     if (session == 9)
  481.     {
  482.         $("#left_cat").html("");
  483.         $("#right_cat").html("");
  484.         $("#exp_instruct").html("<img src='spinner.gif'>");
  485.         resultsString = WriteFile();
  486.         if(input.showResult)
  487.         {
  488.             calculateIAT(resultsString);
  489.         }
  490.         else
  491.         {
  492.             resulttext = "<div style='text-align:center;padding:20px'>Thanks for participating!</div>";
  493.             $("#picture_frame").html(resulttext);
  494.         }
  495.     }
  496.     else
  497.     {
  498.         if(!forward){
  499.        
  500.             switch(session)
  501.             {
  502.                 case 0:
  503.                    
  504.                     switch(instrCount)
  505.                         {
  506.                             case 0:
  507.                                 $.get("gInstruct"+(session+1)+".html", function(data) { $('#exp_instruct').html(data); });
  508.                                 break;
  509.                             case 3:
  510.                                 $.get("gInstruct9andhalf.html", function(data) { $('#exp_instruct').html(data); });
  511.                                 $("#left_cat").hide();
  512.                                 $("#right_cat").hide();
  513.                                 instrCount = 0;
  514.                                 setLabels();
  515.                                 forward = true;
  516.                                 break;
  517.                             default:
  518.                                 $.get("gInstruct"+(session+7+instrCount)+".html", function(data) { $('#exp_instruct').html(data); });
  519.                                 break;
  520.                         }
  521.  
  522.                     break;
  523.                 case 1:
  524.                     setLabels();                   
  525.                     switch(instrCount)
  526.                         {  
  527.                             case 2:
  528.                                 $.get("gInstruct11andhalf.html", function(data) { $('#exp_instruct').html(data); });
  529.                                 instrCount = 0; //BUG HERE?
  530.                                 setLabels();
  531.                                 forward = true;
  532.                                 break;
  533.                             default:
  534.                                 $.get("gInstruct"+(session+9+instrCount)+".html", function(data) { $('#exp_instruct').html(data); });
  535.                                 $("#left_cat").hide();
  536.                                 $("#right_cat").hide();
  537.                                 break;
  538.                         }
  539.  
  540.                     break;
  541.                 case 2:
  542.                     switch(instrCount)
  543.                         {  
  544.                             case 0:
  545.                                 $.get("gInstruct"+(session+1)+".html", function(data) { $('#exp_instruct').html(data); });
  546.                                 setLabels();
  547.                                 forward = false;
  548.                                 $("#left_cat").hide();
  549.                                 $("#right_cat").hide();
  550.                                 break;
  551.                             default:
  552.                                 $.get("gInstruct23.html", function(data) { $('#exp_instruct').html(data); });
  553.                                 $("#left_cat").hide();
  554.                                 $("#right_cat").hide();
  555.                                 instrCount = 0;
  556.                                 setLabels();
  557.                                 forward = true;
  558.                                 break;
  559.                         }
  560.                     break;
  561.                 case 3:
  562.                     document.getElementById("header").style.height='120px';
  563.                     currentState = "play";
  564.                     $('#exp_instruct').html('');
  565.                     displayItem();
  566.                     break;
  567.                     /*switch(instrCount)
  568.                         {  
  569.                             case 0:
  570.                                 $.get("gInstruct"+(session+1)+".html", function(data) { $('#exp_instruct').html(data); });
  571.                                 setLabels();
  572.                                 forward = false;
  573.                                 $("#left_cat").hide();
  574.                                 $("#right_cat").hide();
  575.                                 break;
  576.                             default:
  577.                                 $.get("gInstruct23.html", function(data) { $('#exp_instruct').html(data); });
  578.                                 $("#left_cat").hide();
  579.                                 $("#right_cat").hide();
  580.                                 instrCount = 0;
  581.                                 setLabels();
  582.                                 forward = true;
  583.                                 break;
  584.                         }
  585.                     break;*/
  586.                 case 4:
  587.                    
  588.                     document.getElementById("header").style.height='120px';
  589.                     currentState = "play";
  590.                     $('#exp_instruct').html('');
  591.                     displayItem();
  592.                     break;
  593.                    
  594.                     /*switch(instrCount)
  595.                         {  
  596.                             case 0:
  597.                                 $.get("gInstruct"+(session+1)+".html", function(data) { $('#exp_instruct').html(data); });
  598.                                 setLabels();
  599.                                 forward = false;
  600.                                 $("#left_cat").hide();
  601.                                 $("#right_cat").hide();
  602.                                 break;
  603.                             default:
  604.                                 $.get("gInstruct23.html", function(data) { $('#exp_instruct').html(data); });
  605.                                 $("#left_cat").hide();
  606.                                 $("#right_cat").hide();
  607.                                 instrCount = 0;
  608.                                 setLabels();
  609.                                 forward = true;
  610.                                 break;
  611.                         }
  612.                     break;*/
  613.                 case 5:
  614.                     setLabels();
  615.                     $.get("gInstruct"+(session+9+instrCount)+".html", function(data) { $('#exp_instruct').html(data); });
  616.                     switch(instrCount)
  617.                         {  
  618.  
  619.                             case 0:
  620.                                 isFive = true;
  621.                                 forward = false;
  622.                                 $("#left_cat").hide();
  623.                                 $("#right_cat").hide();
  624.                                 break;
  625.                             case 1:
  626.                                 break;
  627.                             default:
  628.                                 $("#left_cat").hide();
  629.                                 $("#right_cat").hide();
  630.                                 break;
  631.                         }
  632.  
  633.                     break;
  634.                 case 6:
  635.                     switch(instrCount)
  636.                         {  
  637.                             case 0:
  638.                                 $("#left_cat").hide();
  639.                                 $("#right_cat").hide();
  640.                                 forward = false;
  641.                                 $.get("gInstruct"+(session+1)+".html", function(data) { $('#exp_instruct').html(data); });
  642.                                 break;
  643.                             default:
  644.                                 $.get("gInstruct56.html", function(data) { $('#exp_instruct').html(data); });
  645.                                 $("#left_cat").hide();
  646.                                 $("#right_cat").hide();
  647.                                 instrCount = 0;
  648.                                 setLabels();
  649.                                 forward = true;
  650.                                 break;
  651.                         }
  652.                    
  653.                     break;
  654.                 case 7:
  655.                     document.getElementById("header").style.height='120px';
  656.                     currentState = "play";
  657.                     $('#exp_instruct').html('');
  658.                     displayItem();
  659.                     break;
  660.                 case 8:
  661.                     document.getElementById("header").style.height='120px';
  662.                     currentState = "play";
  663.                     $('#exp_instruct').html('');
  664.                     displayItem();
  665.                     break;
  666.                
  667.                
  668.                 default:
  669.                     $.get("gInstruct"+(session+1)+".html", function(data) { $('#exp_instruct').html(data); });
  670.                     instrCount = 0;
  671.                     setLabels();
  672.                     forward = true;
  673.                     $("#left_cat").hide();
  674.                     $("#right_cat").hide();
  675.                     break;
  676.  
  677.             }
  678.  
  679.         }
  680.         else{
  681.             alert("forward wasn't set");
  682.             setLabels();
  683.             $("#left_cat").hide();
  684.             $("#right_cat").hide();
  685.             $.get("gInstruct"+(session+1)+".html", function(data) { $('#exp_instruct').html(data); });
  686.         }
  687.     }
  688.  
  689.  
  690.  
  691. }
  692.  
  693. // Calculates estimate of effect size to present results to participant
  694. function calculateIAT(result)
  695. {
  696.  
  697.  
  698.  
  699.    // calculate mean log(RT) for first key trial
  700.     compatible = 0;
  701.     for (i=1; i<roundArray[3].length; i++)
  702.     {
  703.         score = roundArray[3][i].endtime - roundArray[3][i].starttime;
  704.         if (score < 300) { score = 300; }
  705.         if (score > 3000) { score = 3000; }
  706.         compatible += Math.log(score);
  707.     }
  708.     compatible /= (roundArray[3].length - 1);
  709.    
  710.     // calculate mean log(RT) for second key trial
  711.     incompatible = 0;
  712.     for (i=1; i<roundArray[6].length; i++)
  713.     {
  714.         score = roundArray[6][i].endtime - roundArray[6][i].starttime;
  715.         if (score < 300) { score = 300; }
  716.         if (score > 3000) { score = 3000; }
  717.         incompatible += Math.log(score);
  718.     }
  719.     incompatible /= (roundArray[6].length - 1);
  720.    
  721.     // calculate variance log(RT) for first key trial
  722.     cvar = 0;
  723.     for (i=1; i<roundArray[3].length; i++)
  724.     {
  725.         score = roundArray[3][i].endtime - roundArray[3][i].starttime;
  726.         if (score < 300) { score = 300; }
  727.         if (score > 3000) { score = 3000; }
  728.         cvar += Math.pow((Math.log(score) - compatible),2);
  729.     }
  730.    
  731.     // calculate variance log(RT) for second key trial
  732.     ivar = 0;
  733.     for (i=1; i<roundArray[6].length; i++)
  734.     {
  735.         score = roundArray[6][i].endtime - roundArray[6][i].starttime;
  736.         if (score < 300) { score = 300; }
  737.         if (score > 3000) { score = 3000; }
  738.         ivar += Math.pow((Math.log(score) - incompatible),2);
  739.     }
  740.    
  741.     // calculate t-value
  742.     tvalue = (incompatible - compatible) / Math.sqrt(((cvar/39) + (ivar/39))/40);
  743.    
  744.     // determine effect size from t-value and create corresponding text
  745.     if (Math.abs(tvalue) > 2.89) { severity = " <b>much more</b> than "; }
  746.     else if (Math.abs(tvalue) > 2.64) { severity = " <b>more</b> than "; } 
  747.     else if (Math.abs(tvalue) > 1.99) { severity = " <b>a little more</b> than "; }
  748.     else if (Math.abs(tvalue) > 1.66) { severity = " <b>just slightly more</b> than "; }
  749.     else { severity = ""; }
  750.    
  751.     // put together feedback based on direction & magnitude
  752.     if (tvalue < 0 && severity != "")
  753.     {
  754.         resulttext = "<div style='text-align:center;padding:20px'>You associate "+openA+input.catB.label+closeA+" with "+open1+input.cat1.label+close1;
  755.         resulttext += " and "+openA+input.catA.label+closeA+" with "+open1+input.cat2.label+close1+severity;
  756.         resulttext += "you associate "+openA+input.catA.label+closeA+" with "+open1+input.cat1.label+close1;
  757.         resulttext += " and "+openA+input.catB.label+closeA+" with "+open1+input.cat2.label+close1+".</div>";
  758.         // resulttext += "<div>incompatible: "+incompatible+" ("+(ivar/39)+"); compatible: "+compatible+" ("+(cvar/39)+"); tvalue: "+tvalue+"</div>";
  759.     }
  760.     else if (tvalue > 0 && severity != "")
  761.     {
  762.         resulttext = "<div style='text-align:center;padding:20px'>You associate "+openA+input.catA.label+closeA+" with "+open1+input.cat1.label+close1;
  763.         resulttext += " and "+openA+input.catB.label+closeA+" with "+open1+input.cat2.label+close1+severity;
  764.         resulttext += "you associate "+openA+input.catB.label+closeA+" with "+open1+input.cat1.label+close1;
  765.         resulttext += " and "+openA+input.catA.label+closeA+" with "+open1+input.cat2.label+close1+".</div>";
  766.         // resulttext += "<div>incompatible: "+incompatible+" ("+(ivar/39)+"); compatible: "+compatible+" ("+(cvar/39)+"); tvalue: "+tvalue+"</div>";
  767.     }
  768.     else
  769.     {
  770.         resulttext = "<div style='text-align:center;padding:20px'>You do not associate "+openA+input.catA.label+closeA;
  771.         resulttext += " with "+open1+input.cat1.label+close1+" any more or less than you associate ";
  772.         resulttext += openA+input.catB.label+closeA+" with "+open1+input.cat1.label+close1+".</div>";
  773.         // resulttext += "<div>incompatible: "+incompatible+" ("+(ivar/39)+"); compatible: "+compatible+" ("+(cvar/39)+"); tvalue: "+tvalue+"</div>";
  774.     }
  775.  
  776. resulttext = "<p>Here is your Authentication Code. </p></br>"
  777.         + "<p><b style='font-size:30px;'>"+thisHash+"</b> </p>";
  778.  
  779.     $("#picture_frame").html(resulttext);
  780.  
  781. }
  782.  
  783. // not currently used
  784. function startSurvey()
  785. {
  786.     // demographics survey / explicit attitudes form, including:
  787.     /*
  788.     Please rate how warm or cold you feel toward the following groups
  789.     (0 = coldest feelings, 5 = neutral, 10 = warmest feelings)
  790.     */
  791. }
  792.  
  793. // Converts the data for each session and round into a comma-delimited string
  794. // and passes it to writeFile.php to be written by the server
  795. function WriteFile()
  796. {
  797.     var str = "";
  798.     /*for (i=0; i<roundArray.length; i++)
  799.     {
  800.         for (j=0;j<roundArray[i].length;j++)
  801.         {
  802.             str += "Category, Round, Errors, Time,";
  803.         }
  804.     }
  805.         str += "hash, \n";
  806. */
  807.     for (i=0; i<roundArray.length; i++)
  808.     {
  809.         for (j=0;j<roundArray[i].length;j++)
  810.         {
  811.             str += roundArray[i][j].category+",";
  812.             str += i +", ";
  813.             str += roundArray[i][j].errors+",";
  814.             str += (roundArray[i][j].endtime - roundArray[i][j].starttime)+",";
  815.         }
  816.     }
  817.     str += thisHash + ", \n";
  818.  
  819.     $.post("writeFile.php", { data: str });
  820.     return str;
  821. }
  822.  
  823. // This monitors for keyboard events
  824. function keyHandler(kEvent)
  825. {  
  826. //if (session!=7)
  827. if (session!=9)
  828.   {
  829.     // move from instructions to session on spacebar press
  830.     var unicode=kEvent.keyCode? kEvent.keyCode : kEvent.charCode;
  831.     keyE = (unicode == 69 || unicode == 101 );
  832.     keyI = (unicode == 73 || unicode == 105 );
  833. //alert("here2");
  834.     if(currentState == "instruction" && keyE)
  835.     {//alert("here3");
  836.         switch(session)
  837.         {
  838.             case 0:
  839.                 alert("case 0");
  840.                 if (instrCount == 1)
  841.                 {  
  842.                     instrCount += 1;
  843.                     instructionPage();
  844.                     alert("case 0 inside if");
  845.                 }
  846.                 break;
  847.             case 1:
  848.                 alert("case 1");
  849.                 if (instrCount == 1)
  850.                 {  
  851.                     instrCount += 1;
  852.                     instructionPage();
  853.                     alert("case 1 inside if");
  854.                 }
  855.                 break;
  856.             /*case 4:
  857.                 if (instrCount == 1 && (!forward))
  858.                 {  
  859.                     currentState = "play";
  860.                     $('#exp_instruct').html('');
  861.                     displayItem();
  862.                 }
  863.                 break; */
  864.             case 2: alert("case 2");
  865.             case 3: alert("case 3");
  866.             case 4:
  867.             alert("case 4");
  868.             case 5:
  869.             alert("case 5");
  870.                 if (instrCount == 1 && (!forward))
  871.                 {  
  872.                     currentState = "play";
  873.                     $('#exp_instruct').html('');
  874.                     displayItem();
  875.                     alert("case 5 inside if");
  876.                 }
  877.                 break;
  878.  
  879.             default:
  880.                 break;
  881.         }
  882.  
  883.     }
  884.     if(currentState == "instruction" && keyI)
  885.     {//alert("here3");
  886.         switch(session)
  887.         {
  888.             case 0:
  889.                 if (instrCount == 2)
  890.                 {  
  891.                     instrCount += 1;
  892.                     instructionPage();
  893.                 }
  894.                 break;
  895.             case 1:
  896.                 if (instrCount == 0 && (!forward))
  897.                 {  
  898.                     instrCount += 1;
  899.                     instructionPage();
  900.                 }
  901.                 break;
  902.             /*
  903.             case 4:
  904.                 if (instrCount == 0 && (!forward))
  905.                 {  
  906.                     instrCount += 1;
  907.                     instructionPage();
  908.                 }
  909.                 break;
  910.             */
  911.             case 5:
  912.                 if (instrCount == 0 && (!forward))
  913.                 {  
  914.                     instrCount += 1;
  915.                     instructionPage();
  916.                 }
  917.                 break;
  918.             default:
  919.                 break;
  920.         }
  921.  
  922.     }
  923.  
  924.     if (currentState == "instruction" && unicode == 32)
  925.     {
  926.         switch(session)
  927.         {
  928.             case 0:
  929.                 //alert("here");
  930.                 if ((instrCount == 1) || (instrCount ==2))
  931.                 {  
  932.                     break;
  933.                 }
  934.             case 1:
  935.                 if (((instrCount == 1) || (instrCount ==0 && (!forward))) && (session != 0))
  936.                 {  
  937.                     break;
  938.                 }
  939.             case 4:
  940.                 if((instrCount == 0 && (!forward))&& (session != 0))
  941.                 {
  942.                     break;
  943.                 }
  944.             default:
  945.                 if (!forward)
  946.                 { // for the instructions with multiple pages
  947.                     instrCount += 1;
  948.                     instructionPage();
  949.                 }
  950.                 else
  951.                 {
  952.                     currentState = "play";
  953.                     $('#exp_instruct').html('');
  954.                     displayItem();
  955.                 }
  956.                 break;
  957.         }
  958.            
  959.     }
  960.  
  961.  
  962.     // in session
  963.     if (currentState == "play")
  964.     {
  965.         document.getElementById("header").style.height='120px';
  966.         forward = false;
  967.         instrCount = 0;
  968.         $("#left_cat").show();
  969.         $("#right_cat").show();
  970.         runSession(kEvent);
  971.     }
  972.   }
  973. }
  974.  
  975. // Get the stimulus for this session & round and display it
  976. function displayItem()
  977. {
  978.     var tRound = roundArray[session][roundnum];
  979.     tRound.starttime = new Date().getTime(); // the time the item was displayed
  980.     if (tRound.itemtype == "img")
  981.     {
  982.         if (tRound.category == input.catA.datalabel)
  983.             { $("#"+input.catA.datalabel+tRound.catIndex).css("display","block"); }
  984.         else if (tRound.category == input.catB.datalabel)
  985.             { $("#"+input.catB.datalabel+tRound.catIndex).css("display","block"); }
  986.         else if (tRound.category == input.cat1.datalabel)
  987.             { $("#"+input.cat1.datalabel+tRound.catIndex).css("display","block"); }
  988.         else if (tRound.category == input.cat2.datalabel)
  989.             { $("#"+input.cat2.datalabel+tRound.catIndex).css("display","block"); }
  990.     }
  991.     else if (tRound.itemtype == "txt")
  992.     {
  993.         if (tRound.category == input.catA.datalabel)
  994.         {
  995.             $("#word").html(openA+input.catA.items[tRound.catIndex]+closeA)
  996.             $("#word").css("display","block");
  997.         }
  998.         else if (tRound.category
  999. == input.catB.datalabel)
  1000.         {
  1001.             $("#word").html(openA+input.catB.items[tRound.catIndex]+closeA)
  1002.             $("#word").css("display","block");
  1003.         }
  1004.         else if (tRound.category == input.cat1.datalabel)
  1005.         {
  1006.             $("#word").html(open1+input.cat1.items[tRound.catIndex]+close1)
  1007.             $("#word").css("display","block");
  1008.         }
  1009.         else if (tRound.category == input.cat2.datalabel)
  1010.         {
  1011.             $("#word").html(open1+input.cat2.items[tRound.catIndex]+close1)
  1012.             $("#word").css("display","block");
  1013.         }
  1014.     }
  1015. }
  1016.  
  1017.  
  1018.  
  1019.  
  1020. function runSession(kEvent)
  1021. {
  1022.     var rCorrect = roundArray[session][roundnum].correct;
  1023.     var unicode = kEvent.keyCode? kEvent.keyCode : kEvent.charCode;
  1024.    
  1025.     if (!(isFive))
  1026.     {
  1027.             keyE = (unicode == 69 || unicode == 101 );
  1028.         keyI = (unicode == 73 || unicode == 105 );
  1029.        
  1030.         // if correct key (1 & E) or (2 & I)
  1031.         if ((rCorrect == 1 && keyE) || (rCorrect == 2 && keyI))
  1032.         {
  1033.             $("#wrong").css("display","none"); // remove X if it exists
  1034.             roundArray[session][roundnum].endtime = new Date().getTime(); // end time
  1035.             // if more rounds
  1036.             if (roundnum < roundArray[session].length-1)
  1037.             {
  1038.                 roundnum++;
  1039.                 $(".IATitem").css("display","none"); // hide all items
  1040.                 displayItem(); // display chosen item
  1041.             }
  1042.             else
  1043.             {
  1044.                 $(".IATitem").css("display","none"); // hide all items
  1045.                 currentState = "instruction"; // change state to instruction
  1046.                 session++; // move to next session
  1047.                 alert("runSession increased the session by 1");
  1048.                 roundnum=0; // reset rounds to 0
  1049.                 instructionPage(); // show instruction page
  1050.             }
  1051.         }
  1052.         // incorrect key
  1053.         else if ((rCorrect == 1 && keyI) || (rCorrect == 2 && keyE))
  1054.         {
  1055.             $("#wrong").css("display","block"); // show X
  1056.             roundArray[session][roundnum].errors++; // note error
  1057.         }
  1058.     }
  1059.     else {
  1060.         isFive = false;
  1061.     }
  1062.    
  1063. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement