sub = '';
sesso='';
function randomString(length) {
var chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
var result = '';
for (var i = length; i > 0; --i) result +=
chars.charAt(Math.floor(Math.random() * (chars.length - 1)));
return result;
}
function loadInstructions(stage)
{
switch(stage)
{
case 'one':
sub = $("#subID").val();
//sex= $("#sexID").val();
if(sub.search('/[^a-zA-Z0-9]/g')==-1)
{
$.get("core/instruct1.html", function(data) {
$("#instructions").html(data);
$(".IATname").html(template.name);
if( template.catA.itemtype == "img" ||
template.catB.itemtype == "img" ||
template.cat1.itemtype == "img" ||
template.cat2.itemtype == "img")
{
$("#andpics").html(" and pictures ");
}
});
}
else
{
alert("Please enter a valid subject ID");
}
break;
case 'two':
$.get("core/instruct2.html", function(data) {
$("#instructions").html(data);
$("#clabel1").html(template.cat1.label);
$("#clabel2").html(template.cat2.label);
$("#clabelA").html(template.catA.label);
$("#clabelB").html(template.catB.label);
if (template.cat1.itemtype == "txt")
{ $("#citems1").html(template.cat1.items.join(", ")); }
else if (template.cat1.itemtype == "img")
{ $("#citems1").html("Images of "+template.cat1.label); }
if (template.cat2.itemtype == "txt")
{ $("#citems2").html(template.cat2.items.join(", ")); }
else if (template.cat2.itemtype == "img")
{ $("#citems2").html("Images of "+template.cat2.label); }
if (template.catA.itemtype == "txt")
{ $("#citemsA").html(template.catA.items.join(", ")); }
else if (template.catA.itemtype == "img")
{ $("#citemsA").html("Images of "+template.catA.label); }
if (template.catB.itemtype == "txt")
{ $("#citemsB").html(template.catB.items.join(", ")); }
else if (template.catB.itemtype == "img")
{ $("#citemsB").html("Images of "+template.catB.label); }
});
break;
case 'IAT':
$.get("core/IAT.html", function(data) {
$('body').html(data);
document.onkeypress = keyHandler;
startIAT();
});
break;
}
}
// Adds all images to page (initially hidden) so they are pre-loaded for IAT
function buildPage()
{
if (template.catA.itemtype == "img")
{
for (i in template.catA.items)
{
var itemstr = '<img id="'+template.catA.datalabel+i+'"
class="IATitem" src="templates/'+template.name+'/img/'+template.catA.items[i]+'">';
$("#exp_instruct").after(itemstr);
}
}
if (template.catB.itemtype == "img")
{
for (i in template.catB.items)
{
var itemstr = '<img id="'+template.catB.datalabel+i+'"
class="IATitem" src="templates/'+template.name+'/img/'+template.catB.items[i]+'">';
$("#exp_instruct").after(itemstr);
}
}
if (template.cat1.itemtype == "img")
{
for (i in template.cat1.items)
{
var itemstr = '<img id="'+template.cat1.datalabel+i+'"
class="IATitem" src="templates/'+template.name+'/img/'+template.cat1.items[i]+'">';
$("#exp_instruct").after(itemstr);
}
}
if (template.cat2.itemtype == "img")
{
for (i in template.cat2.items)
{
var itemstr = '<img id="'+template.cat2.datalabel+i+'"
class="IATitem" src="templates/'+template.name+'/img/'+template.cat2.items[i]+'">';
$("#exp_instruct").after(itemstr);
}
}
}
// Round object
function IATround()
{
this.starttime = 0;
this.endtime = 0;
this.itemtype = "none";
this.category = "none";
this.catIndex = 0;
this.correct = 0;
this.errors = 0;
}
// Create array for each session & round, with pre-randomized ordering of images
function initRounds()
{
var roundArray = [];
// for each session
for (var i=0; i<7; i++)
{
roundArray[i] = [];
switch (i)
{
case 0:
stype = "target";
numrounds = 5;
break;
case 1:
stype = "association";
numrounds = 5;
break;
case 2:
stype = "both";
numrounds = 5;
break;
case 3:
stype = "both";
numrounds = 30;
break;
case 4:
stype = "target";
numrounds = 10;
break;
case 5:
stype = "both";
numrounds = 5;
break;
case 6:
stype = "both";
numrounds = 30;
break;
}
prevIndexA = -1; prevIndex1 = -1;
for (var j = 0; j<numrounds; j++)
{
var round = new IATround();
if (stype == "target")
{
round.category = (Math.random() < 0.5 ? template.catA.datalabel :
template.catB.datalabel);
}
else if (stype == "association")
{
round.category = (Math.random() < 0.5 ? template.cat1.datalabel :
template.cat2.datalabel);
}
else if (stype == "both")
{
if (j % 2 == 0) { round.category = (Math.random() < 0.5 ?
template.catA.datalabel : template.catB.datalabel); }
else { round.category = (Math.random() < 0.5 ?
template.cat1.datalabel : template.cat2.datalabel); }
}
// pick a category
if (round.category == template.catA.datalabel)
{
round.itemtype = template.catA.itemtype;
if (i < 4) { round.correct = 1; }
else { round.correct = 2; }
}
else if (round.category == template.catB.datalabel)
{
round.itemtype = template.catB.itemtype;
if (i < 4) { round.correct = 2; }
else { round.correct = 1; }
// pick an item different from the last
do
{ round.catIndex =
Math.floor(Math.random()*template.catB.items.length); }
while (prevIndexA == round.catIndex)
prevIndexA = round.catIndex;
}
else if (round.category == template.cat1.datalabel)
{
round.itemtype = template.cat1.itemtype;
round.correct = 1;
// pick an item different from the last
do
{ round.catIndex =
Math.floor(Math.random()*template.cat1.items.length); }
while (prevIndex1 == round.catIndex)
prevIndex1 = round.catIndex;
}
else if (round.category == template.cat2.datalabel)
{
round.itemtype = template.cat2.itemtype;
round.correct = 2;
// pick an item different from the last
do
{ round.catIndex =
Math.floor(Math.random()*template.cat2.items.length); }
while (prevIndex1 == round.catIndex)
prevIndex1 = round.catIndex;
}
roundArray[i].push(round);
}
}
return roundArray;
}
// calculate t-value
tvalue = (incompatible - compatible) / Math.sqrt(((cvar/39) + (ivar/39))/40);
function IsNumeric(input)
{
return (input - 0) == input && input.length > 0;
}
// alert(income+"\n"+parseFloat(income)+"\n");
// $.get('getLocation.php',
// { 'q': loc},
// function(data) {
// alert(data);
// });
// Do validation of input
var error = false;
var errmsg = "";
if(gender==null)
{
error=true;
errmsg += "<div class='error'>Please choose an option for gender</div>";
}
if(age=="unselected")
{
error=true;
errmsg += "<div class='error'>Please state the year you were born</div>";
}
if(loc.length == 0)
{
error=true;
errmsg += "<div class='error'>Please indicate your current location</div>";
}
if(races==null)
{
error=true;
errmsg += "<div class='error'>Please indicate your ethnicity</div>";
}
if(income==null || $.trim(income) != income.replace(/[^0-9$.,]/g,'') || !
IsNumeric(income.replace(/[^0-9.]/g,'')))
{
error=true;
errmsg += "<div class='error'>Please enter a valid number for
income</div>";
}
if(education=="unselected")
{
error=true;
errmsg += "<div class='error'>Please indicate your highest level of
education</div>";
}
if(sex.length == 0)
{
error=true;
errmsg += "<div class='error'>Please enter a valid identifier</div>";
}
// Output error message if input not valid
if(error==false)
{
subject = sex;
// Converts the data for each session and round into a comma-delimited string
// and passes it to writeFile.php to be written by the server
function WriteFile()
{
}
}
// Get the stimulus for this session & round and display it
function displayItem()
{
var tRound = roundArray[session][roundnum];
tRound.starttime = new Date().getTime(); // the time the item was displayed
if (tRound.itemtype == "img")
{
if (tRound.category == template.catA.datalabel)
{ $
("#"+template.catA.datalabel+tRound.catIndex).css("display","block"); }
else if (tRound.category == template.catB.datalabel)
{ $
("#"+template.catB.datalabel+tRound.catIndex).css("display","block"); }
else if (tRound.category == template.cat1.datalabel)
{ $
("#"+template.cat1.datalabel+tRound.catIndex).css("display","block"); }
else if (tRound.category == template.cat2.datalabel)
{ $
("#"+template.cat2.datalabel+tRound.catIndex).css("display","block"); }
}
else if (tRound.itemtype == "txt")
{
if (tRound.category == template.catA.datalabel)
{
$("#word").html(openA+template.catA.items[tRound.catIndex]
+closeA)
$("#word").css("display","block");
}
else if (tRound.category == template.catB.datalabel)
{
$("#word").html(openA+template.catB.items[tRound.catIndex]
+closeA)
$("#word").css("display","block");
}
else if (tRound.category == template.cat1.datalabel)
{
$("#word").html(open1+template.cat1.items[tRound.catIndex]
+close1)
$("#word").css("display","block");
}
else if (tRound.category == template.cat2.datalabel)
{
$("#word").html(open1+template.cat2.items[tRound.catIndex]
+close1)
$("#word").css("display","block");
}
}
}
function runSession(kEvent)
{
var rCorrect = roundArray[session][roundnum].correct;
var unicode = kEvent.keyCode? kEvent.keyCode : kEvent.charCode;
keyE = (unicode == 69 || unicode == 101 );
keyI = (unicode == 73 || unicode == 105 );