/**
  CIS-132 Final Project
  ravens.js
  Purpose: a library of functions for my great web version of a scoring
           device for the Ravens Standard Progressive Matrices test.

  @author Edward "Ted" Altenberg
  @version 1.0 12/13/2008
*/

// functions xCoord, yCoord, shiftItR, shiftItL and animLoop1 are all for animating the < h1 >:

function xCoord(id) {
    if (document.getElementById) {
        var obj = document.getElementById(id);
        xc = parseInt(obj.style.left);
        return xc;
    }
}

function yCoord(id) {
    if (document.getElementById) {
        var obj = document.getElementById(id);
        yc = parseInt(obj.style.top);
        return yc;
    }
}

function shiftItR(id, dx, dy) {
    if (document.getElementById) {
        obj = document.getElementById(id);
        obj.style.left = xCoord(id) + dx + "px";
        obj.style.top = yCoord(id) + dy + "px";
    }
}

function shiftItL(id, dx, dy) {
    if (document.getElementById) {
        obj = document.getElementById(id);
        obj.style.left = xCoord(id) - dx + "px";
        obj.style.top = yCoord(id) + dy + "px";
    }
}

// Global variable for animation

var index = 0;

function animLoop1() {
    if (index < 63) {
        shiftItR("anim1", 5, 0);
        index++;
        setTimeout("animLoop1()", 30);
    }
}

// The following arrays and functions rollOver and rollOut are for the rollover effect on the navigation menu

    if (document.images) {
    var ImgOver = new Array();
    ImgOver[0] = new Image();
    ImgOver[1] = new Image();
    ImgOver[2] = new Image();
    ImgOver[3] = new Image();
    ImgOver[4] = new Image();
    
    ImgOver[0].src = "images/home_over.gif";
    ImgOver[1].src = "images/cis132_over.gif";
    ImgOver[2].src = "images/final_over.gif";
    ImgOver[3].src = "images/ravens_over.gif";
    ImgOver[4].src = "images/scoring_over.gif";

    var ImgOut = new Array();
    ImgOut[0] = new Image();
    ImgOut[1] = new Image();
    ImgOut[2] = new Image();
    ImgOut[3] = new Image();
    ImgOut[4] = new Image();
    
    ImgOut[0].src = "images/home.gif";
    ImgOut[1].src = "images/cis132.gif";
    ImgOut[2].src = "images/final.gif";
    ImgOut[3].src = "images/ravens.gif";
    ImgOut[4].src = "images/scoring.gif";
  }


function rollOver(i) {
  if (document.images) document.images[i].src = ImgOver[i].src;
  }
  
function rollOut(i) {
  if (document.images) document.images[i].src = ImgOut[i].src;
  }


// 		This function accepts inputs from the form, converts them into date objecgts,
//		then calculates the  student's age at the time they took the test.

function calcTestAge()  {
  var birthMonth = document.ravensForm.birthMonth.value;
  var birthDay = document.ravensForm.birthDay.value;
  var birthYear = document.ravensForm.birthYear.value;
  var birthDate = new Date(birthYear, birthMonth, birthDay);
  var testMonth = document.ravensForm.testMonth.value;
  var testDay = document.ravensForm.testDay.value;
  var testYear = document.ravensForm.testYear.value;
  var testDate = new Date(testYear, testMonth, testDay);
  var testAgeMilliseconds = testDate.getTime() - birthDate.getTime();
  var testAge1 = testAgeMilliseconds/(1000 * 3600 * 24 * 365.25);
  alert("testAge1 = " + testAge1);
  var testAge2 = testAge1*2;
  alert("testAge2 = " + testAge2);
  var testAge3 = Math.round(testAge2);
  alert("testAge3 = " + testAge3);
  var testAgeFinal = testAge3/2;
  alert("testAgeFinal = " + testAgeFinal);
  document.ravensForm.testAge.value = testAgeFinal;
}

//		The variable ageArrayColumn converts their testAge into a number that corresponds to the column
//		of the 2-dimensional array below.


/*			This large 2-dimensional array is the scoring matrix that relates the student's raw test score,
  *			their age, and the resulting percentil score. 
  *					The array index = their raw score (0 - 60)
  *  				The column (the second dimension of the 2-D array) = their age category
  *  				The number at the intersection of their score row (array index) and their age column = their percentil score
*/

var scoringMatrix =
[ ['0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'],				//  0
   ['0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'],				//  1
   ['0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'],				//  2
   ['0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'],				//  3
   ['0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'],				//  4
   ['0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'],				//  5
   ['0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'],				//  6
   ['5','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'],				//  7
   ['8','5','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'],				//  8
   ['10','8','5','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'],				//  9
   ['16','10','8','5','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'],				//  10
   ['21','15','10','7','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'],			//  11
   ['25','20','15','8','7','5','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'],			//  12
   ['38','25','20','10','8','6','5','0','0','0','0','0','0','0','0','0','0','0','0','0','0'],			//  13
   ['50','31','25','15','10','8','6','0','0','0','0','0','0','0','0','0','0','0','0','0','0'],			//  14
   ['54','38','29','20','14','10','8','5','0','0','0','0','0','0','0','0','0','0','0','0','0'],			//  15
   ['58','44','33','25','18','13','9','5','0','0','0','0','0','0','0','0','0','0','0','0','0'],			//  16
   ['62','50','38','28','22','17','10','8','5','0','0','0','0','0','0','0','0','0','0','0','0'],		//  17
   ['65','54','42','32','23','20','13','9','6','5','0','0','0','0','0','0','0','0','0','0','0'],		//  18
   ['68','57','46','36','29','23','15','10','8','6','5','0','0','0','0','0','0','0','0','0','0'],		//  19
   ['72','60','50','39','32','25','17','13','9','7','6','0','0','0','0','0','0','0','0','0','0'],		//  20
   ['75','63','54','42','35','28','19','15','10','8','7','5','0','0','0','0','0','0','0','0','0'],		//  21
   ['78','66','57','46','38','32','21','17','13','9','8','6','5','0','0','0','0','0','0','0','0'],		//  22
   ['81','69','60','50','41','35','23','19','15','10','9','7','6','0','0','0','0','0','0','0','0'],		//  23
   ['83','72','63','54','44','38','25','21','17','13','9','8','7','5','0','0','0','0','0','0','0'],		//  24
   ['85','75','66','57','47','41','28','23','19','15','10','9','8','6','0','0','0','0','0','0','0'],	//  25
   ['87','78','69','60','50','44','32','25','21','17','12','9','9','7','5','0','0','0','0','0','0'],	//  26
   ['90','82','72','63','54','47','35','28','23','19','14','10','9','8','6','5','0','0','0','0','0'],	//  27
   ['92','84','75','66','57','50','38','32','25','21','16','13','10','9','7','6','5','0','0','0','0'],	//  28
   ['94','87','78','69','60','54','41','35','28','23','19','15','13','9','8','7','6','5','5','0','0'],	//  29
   ['95','90','81','72','63','58','44','38','31','25','21','18','15','10','9','8','7','6','6','0','0'],	//  30
   ['96','92','84','75','66','62','47','41','34','29','23','21','18','13','10','9','8','7','7','5','0'],	//  31
   ['96','93','87','78','70','65','50','44','38','33','25','23','21','17','13','10','9','8','8','6','5'],	//  32
   ['97','95','90','81','73','68','54','47','41','37','29','25','23','20','17','13','10','9','9','7','6'],	//  33
   ['97','96','92','84','76','71','58','50','44','40','33','29','25','23','20','17','13','9','9','8','7'],	//  34
   ['98','96','93','87','79','73','62','54','47','44','37','33','29','25','23','20','17','10','10','9','8'],	//  35
   ['98','97','95','90','83','75','66','58','50','47','41','38','33','29','25','23','21','14','13','9','9'],	//  36
   ['98','97','96','93','86','80','70','62','55','50','46','42','38','33','29','25','23','18','16','10','9'],	//  37
   ['99','97','96','93','86','80','70','62','55','50','46','42','38','38','33','29','25','22','19','13','10'],	//  38
   ['99','98','97','96','93','90','75','70','65','58','54','50','46','42','38','33','29','25','22','16','13'],	//  39
   ['99','98','97','96','95','95','80','75','70','62','58','54','50','46','42','38','33','29','25','19','16'],	//  40
   ['99','99','98','97','96','96','85','80','75','66','62','58','54','50','46','42','38','33','29','22','20'],	//  41
   ['99','99','98','97','96','96','90','85','79','70','66','62','58','54','50','46','42','38','33','25','22'],	//  42
   ['99','99','98','98','97','97','95','90','83','75','70','66','62','58','54','50','46','42','38','28','25'],	//  43
   ['99','99','99','98','97','97','96','95','86','80','75','70','66','62','58','54','50','46','42','33','30'],	//  44
   ['99','99','99','98','98','98','96','96','90','85','80','75','70','66','62','58','55','50','46','38','34'],	//  45
   ['99','99','99','99','98','98','97','96','93','90','85','80','75','70','66','62','60','55','50','42','38'],	//  46
   ['99','99','99','99','98','98','97','97','95','93','90','85','80','75','70','66','65','60','55','46','43'],	//  47
   ['99','99','99','99','99','99','98','97','96','95','93','90','85','80','75','70','70','65','60','50','46'],	//  48
   ['99','99','99','99','99','99','98','98','96','96','95','93','90','85','80','75','75','70','65','55','50'],	//  49
   ['99','99','99','99','99','99','98','98','97','96','96','95','93','90','85','80','80','75','70','61','57'],	//  50
   ['99','99','99','99','99','99','99','98','97','97','96','96','95','95','90','85','85','80','75','65','61'],	//  51
   ['99','99','99','99','99','99','99','99','98','97','97','96','96','96','93','90','90','85','80','70','65'],	//  52
   ['99','99','99','99','99','99','99','99','98','98','97','97','96','96','95','93','92','90','85','75','70'],	//  53
   ['99','99','99','99','99','99','99','99','99','99','98','97','97','97','96','95','93','92','90','81','75'],	//  54
   ['99','99','99','99','99','99','99','99','99','99','98','98','97','97','96','96','95','94','93','87','83'],	//  55
   ['99','99','99','99','99','99','99','99','99','99','99','98','98','98','97','97','96','95','95','90','90'],	//  56
   ['99','99','99','99','99','99','99','99','99','99','99','99','98','98','97','97','97','96','96','95','95'],	//  57
   ['99','99','99','99','99','99','99','99','99','99','99','99','99','99','98','98','97','97','97','96','96'],	//  58
   ['99','99','99','99','99','99','99','99','99','99','99','99','99','99','99','98','98','98','98','98','98'],	//  59
   ['99','99','99','99','99','99','99','99','99','99','99','99','99','99','99','99','99','99','99','99','99']];	//  60


//		This pulls the raw score entered into the form and assigns its value to this variable

//		Now we can use variables testRawScore and ageArrayColumn as array indeces to find the percentile score:
function testPercentileScore () {
    var testRawScore = document.ravensForm.testScore.value;
    alert("testRawScore = " + testRawScore);
    var ageArray = document.ravensForm.testAge.value*2-13;
    alert("ageArray = " + ageArray);
	var testPercentileScore = scoringMatrix[testRawScore][ageArray];
    alert("testPercentileScore = " + testPercentileScore);
	document.ravensForm.testPercent.value = testPercentileScore;
}

