var Inf = 1.1;
var CS  = 1.1;
var CSE = 1.1;
var CE  = 1.1;
var ICS = 1.1;



var NEUTRAL = 3;
 
// index to weight[] is student's choice, from 7 (strongly agree)
// to 1 (strongly disagree)
var weight = new Array(0, -3, -1.5, -0.75, 0, 0.75, 1.5, 3);


var processors = {
  Q1: function(n) {
        Inf += n * 3;
        CS  += n * 0;
        CSE += n * -1.5;
        CE  += n * -2;
        ICS += n * 1;
        },
  Q2: function(n) {
        Inf += n * -2;
        CS  += n * 0;
        CSE += n * 3;
        CE  += n * 5;
        ICS += n * -1;
        },
  Q3: function(n) {
        Inf += n * 2;
        CS  += n * 2;
        CSE += n * 0;
        CE  += n * -1;
        ICS += n * 1;
        },
  Q4: function(n) {
        Inf += n * 2;
        CS  += n * -2;
        CSE += n * -2;
        CE  += n * -2;
        ICS += n * 0;
        },
  Q5: function(n) {
        Inf += n * -2;
        CS  += n * 1;
        CSE += n * 3;
        CE  += n * 5;
        ICS += n * -2;
        },
  Q6: function(n) {
        Inf += n * -2;
        CS  += n * 2;
        CSE += n * 3;
        CE  += n * 3;
        ICS += n * 0;
        },
  Q7: function(n) {
        Inf += n * 2;
        CS  += n * -2;
        CSE += n * -1.5;
        CE  += n * -1.5;
        ICS += n * 1;
        },
		
  Q8: function(n) {
        Inf += n * -2;
        CS  += n * -2;
        CSE += n * 3;
        CE  += n * 3;
        ICS += n * -2;
        },
  Q9: function(n) {
        Inf += n * -1;
        CS  += n * 3;
        CSE += n * 1;
        CE  += n * 0;
        ICS += n * 1;
        },
  Q10: function(n) {
        Inf += n * 2;
        CS  += n * 1;
        CSE += n * -1;
        CE  += n * -2;
        ICS += n * 1;
        },
  Q11: function(n) {
        Inf += n * 3.5;
        CS  += n * 1;
        CSE += n * -1;
        CE  += n * -2;
        ICS += n * 1;
        },
  Q12: function(n) {
        Inf += n * -2;
        CS  += n * -1;
        CSE += n * 3;
        CE  += n * 5;
        ICS += n * -2;
        },
  Q13: function(n) {
        Inf += n * 3.5;
        CS  += n * 0;
        CSE += n * -1.5;
        CE  += n * -1.5;
        ICS += n * 1;
        },
  Q14: function(n) {
        Inf += n * 3;
        CS  += n * -1;
        CSE += n * -1;
        CE  += n * -2;
        ICS += n * 1;
        },
  Q15: function(n) {
        Inf += n * 2;
        CS  += n * -1;
        CSE += n * -2;
        CE  += n * -2;
        ICS += n * 1;
        },
  Q16: function(n) {
        Inf += n * 0;
        CS  += n * 2;
        CSE += n * -1;
        CE  += n * -1;
        ICS += n * 1;
        },
  Q17: function(n) {
        Inf += n * -2;
        CS  += n * 3;
        CSE += n * -2;
        CE  += n * -2;
        ICS += n * 1;
        },
  Q18: function(n) {
        Inf += n * 0;
        CS  += n * 1;
        CSE += n * 5;
        CE  += n * 5;
        ICS += n * 1;
        },
  Q19: function(n) {
        Inf += n * -2;
        CS  += n * 1;
        CSE += n * 1.5;
        CE  += n * 1;
        ICS += n * 0;
        },
  Q20: function(n) {
        Inf += n * 0;
        CS  += n * 0;
        CSE += n * 0;
        CE  += n * 0;
        ICS += n * 3;
        }
}

var answers = new Object();
function change(question, value)
{
   document.getElementById(question + 'n').checked = false;
   answers[question] = value;
   updateChoice();
}

function change2(question)
{
	for (var i=0; i<document['Form' + question].elements.length; i++)
		document['Form' + question].elements[i].checked = false;
	answers[question] = 4;  // intermediate  value
	updateChoice();
}

function updateChoice()
{
   Inf = CS = CSE = CE = ICS = 0;  // start at above zero
   for (var qn in answers)
   {
   	 //alert("qn: " + qn + " answer: " + answers[qn]);
   	 processors[qn](answers[qn] - 4.0);
   }
   showStatus();
}

function showStatus()
{
    var tot = 3.5; // dummy to establish float
	tot = Inf + CS + CSE + CE + ICS;
	var InfX = Math.max(0, Inf);
	var CSX  = Math.max(0, CS);
	var CSEX = Math.max(0, CSE);
	var CEX  = Math.max(0, CE);
	var ICSX = Math.max(0, ICS);

	//alert(  "Inf: " + InfX +
	//      "\nCS: " + CSX +
	//      "\nCSE: " + CSEX +
	//      "\nCE: " + CEX +
	//      "\nICS: " + ICSX +
	//      "\nBIM: " + BIMX);
	updateScores(InfX, CSX, CSEX, CEX, ICSX);
	makeChart();
}


IE4=(document.all)?1:0;
NS4=(document.layers)?1:0;
DOM=(document.getElementById)?1:0;
NS6=((DOM)&&(!IE4))?1:0;
ver4=(IE4||DOM||NS4)?1:0;
nav=navigator.appVersion;
nav=nav.toLowerCase();
isMac=(nav.indexOf("mac")!=-1)?1:0;
IEmac=((document.all)&&(isMac))?1:0;

imRed = new Image();
imYellow = new Image();
imRed.src = "/bin/img/photos/objects/red.gif";
imYellow.src = "/bin/img/photos/objects/red.gif";
strImage = "";
strToWrite = "";

arMajors = new Array("<b><a href=/ugrad/degrees/degree_in4matx.php>Informatics</a></b>",
                     "<b><a href=/ugrad/degrees/degree_cs.php>Computer Science</a></b>",
                     "<b><a href=/ugrad/degrees/degree_cse.php>Computer Science and Engineering</a></b>",
                     "<b><a href=http://undergraduate.eng.uci.edu/degreeprograms/computer/>Computer Engineering</a></b>",
					 "<b><a href=/ugrad/degrees/degree_ics.php>Information and Computer Science</a></b>");
arScores = new Array(0, 0, 0, 0, 0);

chartWidth = 666;
maxWidth=0;
barHeight=10;

for (i=0; i<arScores.length; i++) {
   if (arScores[i] > maxWidth)
      maxWidth = arScores[i];
}

function makeChart() {
   if (!ver4) return;
   strToWrite = "<table bgcolor='#f1e7a3' cellpadding=0 cellspacing=2 width=" +
                (chartWidth+80) + "><tr><td>";
   strToWrite += "<table bgcolor='#f1e7a3' cellpadding=0 cellspacing=2 width=" +
                (chartWidth+80) + ">"
   for(i=0;i<arScores.length;i++) {
    if(i%1 == 0)
       strImage = "/bin/img/photos/objects/green.gif";
    if(i%2 == 0)
       strImage = "/bin/img/photos/objects/blue.gif";
    if(i%3 == 0)
       strImage = "/bin/img/photos/objects/yellow.gif";
    if(i%4 == 0)
       strImage = "/bin/img/photos/objects/orange.gif";
    if(i%5 == 0)
       strImage = "/bin/img/photos/objects/purple.gif";
    strToWrite += "<tr><td align=left width=260>" + arMajors[i] + "</td>"
    strToWrite += "<td><img src='" + strImage +
                  "' width=" + parseInt(arScores[i] * 5) +
                  " height=" + barHeight + " hspace=2></td></tr>\n";
   }
   strToWrite += "</table>";
   strToWrite += "</td></tr></table>";

   // now show it!
   if (IE4) {
      if((IEmac) && (DOM)) return;
      winInnerWidth = document.body.clientWidth;
      winInnerHeight = document.body.clientHeight;
      screenWidth = screen.availWidth;
      screenHeight = screen.availHeight;
      window.offscreenBuffering = true;
      theChart.innerHTML=strToWrite;
      theChart.innerHTML=strToWrite;
   }
   if (NS4) {
      with(document.theChart) {
      document.open();
      document.write(strToWrite);
      document.close();
   }
   }
   if(NS6) {
      //alert(strToWrite);
      document.getElementById("theChart").innerHTML=strToWrite;
   }
}

function updateScores(infX, CSX, CSEX, CEX, ICSX)
{
	arScores[0] = infX;
	arScores[1] = CSX;
	arScores[2] = CSEX;
	arScores[3] = CEX;
	arScores[4] = ICSX;


}

function addNewMajor(newMajor, newScore) {
 // now validate the input
 if(newMajor.length<1) {
    alert("You must enter a name.");
    return;
 }
 newScore = parseInt(newScore);
 if(isNaN(newScore)) {
    alert("Please Enter a numeric value for this major");
    return;
 }
 // if we get here add the Major & score to the arrays
 arMajors[arMajors.length] = newMajor;
 arScores[arScores.length] = newScore;

 // now reset maxWidth in case we have a larger value
 for(i=0;i<arScores.length;i++) {
   if(arScores[i] > maxWidth)
      maxWidth = arScores[i];
 }
 return;
}
