var currentPoint;
var currentSample;
var mode;
var scoreCorrect;
/*--------------Mandar 11/28/2007----------------------*/
var teachersScores = new Array();//for normal practice
var teachersScores9pt1 = new Array();
var  teachersScores9pt2 = new Array();
/*--------------Mandar 11/28/2007-----------------------*/ 
function scorerDefaultNavInit() {
	var btn = document.getElementById("btnSubmit");
	if (btn != null) btn.href="javascript:submitItemTree()";
}

function scorerTrainingNavInit() {
	mode = "Training";
	setCurrentPoint(initPoint);
	setCurrentSample(0);
	showSample();
	tabOpen('annotation');
}

var navOrder = new Array(0);

function scorerPracticeNavInit() {
	mode = "Practice";
	var btn = document.getElementById("btnContinue");
	
    if (typeof scoreVars != 'undefined')
	{
    	if (btn != null) btn.href="javascript:scoreSelectPT9(this)"
	}
	else
	{
	   
	    if (btn != null) btn.href="javascript:scoreSelect(this, false)"
	}
	
    
    scoreCorrect = new Array(nav2.length);
    for (var i=0; i<scoreCorrect.length; i++) {
        scoreCorrect[i] = -1;
    }
    
    //var navOrder = new Array(0);
    
    while (navOrder.length < nav2.length) {
		var v = Math.floor(Math.random()*nav2.length);
		for (var i=0; i<navOrder.length; i++) {
			if (navOrder[i].v == v) {
				v = null;
				break;
			}
		}
		if (v != null) {
			var o = new Object();
			o.v = v;
			o.sample = document.getElementById("sample_" + v);
			o.annotation = document.getElementById("annotationTab_" + v);
			o.nav2 = nav2[v];
		    if (typeof scoreVars != 'undefined') o.score = scoreVars[v]; // add score to paper
			navOrder.push(o);
			
		}
	}
	
    for (var i=0; i<navOrder.length; i++) {
		nav2[i] = navOrder[i].nav2;
		if (navOrder[i].sample) {
			navOrder[i].sample.id = "sample_" + i;
		}
		if (navOrder[i].annotation) {
			navOrder[i].annotation.id = "annotationTab_" + i;
		}
    }
    

	setCurrentSample(0);
	showSample();
}

function showPrintOptions(context) {
  var w = document.getElementById('printDialog');
  if (w) w.style.display = 'block';
  
  var c = document.getElementById('printOption_context');
  if (c) c.value = context;
  
  if (getNamedTabContent('passage') == null) {
	var showPassage = document.getElementById("printOption_showPassage");
	if (showPassage) {
		showPassage.disabled = true;
		showPassage.checked = false;
	}  
  }
}
function closePrintOptions() {
  var w = document.getElementById('printDialog');
  if (w) w.style.display = 'none';
}

function scorerPrintWithOptions() {
	var printContext = document.getElementById('printOption_context');
	var showPassage = document.getElementById("printOption_showPassage").checked;
	var showPoint = document.getElementById("printOption_showPoint").checked;
	var showQuestion = document.getElementById("printOption_showQuestion").checked;
	var showAnnotation = document.getElementById("printOption_showAnnotation").checked;

	var url = "Print.aspx?mode=" + mode + "&showPassage=" + showPassage + "&showQuestion=" + showQuestion
			+ "&showPoint=" + showPoint + "&showAnnotation=" + showAnnotation;
	if (printContext == "current") {
		if (mode == "Training") {
			url += "&point=" + getCurrentPoint() + "&sample=" + getCurrentSample();
		} else if (mode == "Practice") {
			url += "&sample=" + getCurrentSample();
		}
	}
	
	location = url;
}

function submitItemTree() {
    var nl = document.forms["frmInstructions"].elements["treeItem"];
    if (nl != null) {
		if (nl["length"]) {
			for (var i=0; i<nl.length; i++) {
				if (nl[i].checked) {
					__doPostBack('btnSubmit', '');
					return;
				}
			}
		} else if (nl["checked"]) {
			__doPostBack('btnSubmit', '');
			return;
		}
	}		
    alert('You must select an item');
}

function nav2Dropdown(clicked) 
{
    var value = clicked.value;
    var index1 = value.indexOf("_", 0);
    var index2 = value.indexOf("_", index1+1);
    var point = value.substring(index1 + 1, index2);
    var sample = value.substring(index2 + 1, value.length);
       
  	//setCurrentPoint(initPoint);
	//setCurrentSample(0);
    hideSample();
  	setCurrentPoint(point);
	setCurrentSample(sample);
	showSample();
}

function nav2PointClick(clicked, point) {
	hideSample();
	setCurrentSample(0);
	setCurrentPoint(point);
	showSample();
} 

function nav2SampleClick(clicked, sample) {
	hideSample();
	setCurrentSample(sample);
	showSample();
}

function getCurrentPoint() {
	return currentPoint;
}

function getCurrentSample() {
	return currentSample;
}

function setCurrentPoint(newPoint) {
	if (mode == "Practice") return;
	
	var nav = document.getElementById("nav2_" + currentPoint);
	if (nav != null) nav.className = "";
	
	currentPoint = newPoint;
	
	nav = document.getElementById("nav2_" + currentPoint);
	if (nav != null) nav.className = "yourLevel";

	var samples = document.getElementById("samples");
	samples.innerHTML = "";
	
	var charA = new String('A');
	for (var i=0; i<nav2[getCurrentPoint()].length; i++) {
		var s = document.createElement("span");
		s.id = "nav2_" + getCurrentPoint() + "_" + i;
		if (nav2[getCurrentPoint()][i] == 0) {
			s.className = "sampleLevelAlmost";
		}
		s.innerHTML = String.fromCharCode(charA.charCodeAt(0) + i);
		
		var a = document.createElement("a");
		a.href = "javascript:nav2SampleClick(this, " + i + ")";
		a.appendChild(s);
		samples.appendChild(a);
	}
	
}

function setCurrentSample(newSample) {
	currentSample = newSample;
}

function setNav2SampleShown() {
	nav2[getCurrentPoint()][getCurrentSample()] = 1;
	var n = getCurrentNav2Div();
	if (n != null) n.className = "";
}

function setSampleYes(score) {
    scoreCorrect[getCurrentSample()] = 1;
	var n = getCurrentNav2Div();
	var s = getScoreDiv(score);
	if (n != null) n.className = "sampleHistoryYes";
    if (s != null) s.className = "scoreYes";
}
function setSampleNo(score) {
    scoreCorrect[getCurrentSample()] = 0;
	var n = getCurrentNav2Div();
	var s = getScoreDiv(score);
	if (n != null) n.className = "sampleHistoryNo";
    if (s != null) s.className = "scoreNo";
    tabOpen('annotation');
}
function setSampleSkip() {
    scoreCorrect[getCurrentSample()] = -1;
	var n = getCurrentNav2Div();
	if (n != null) n.className = "sampleHistoryAlmost";
	resetSampleYesNo();
}
function getSampleScore() {
	return scoreCorrect[getCurrentSample()];
}

function resetSampleYesNo() {
    for (var s=0; s<=maxScore; s++) {
        var div = getScoreDiv(s);
        if (div != null) div.className = "scoreAlmost";
        var input = getScoreInput(s);
        if (input != null) {
            input.checked = false;
            input.disabled = false;
        }
    }
	var btn = document.getElementById("btnContinue");
    if (btn != null) btn.className = "notAnswered";
}

function enableScores(enable) {
    for (var s=0; s<=maxScore; s++) {
        var input = getScoreInput(s);
        if (input != null) input.disabled = !enable;
    }
}

function getCurrentSuffix() {
	if (mode == "Training") {
		return getCurrentPoint() + "_" + getCurrentSample();
	} else if (mode == "Practice") {
		return getCurrentSample();
	}
}

function getCurrentSampleDiv() {
	return document.getElementById("sample_" + getCurrentSuffix());
}
function getCurrentNav2Div() {
	return document.getElementById("nav2_" + getCurrentSuffix());
}
function getScoreDiv(score) {
    return document.getElementById("score_" + score);
}
function getScoreInput(score) {
    return document.getElementById("scoreInput_" + score);
}

function nextTrainingSample(source) {
	hideSample();
	do {
		if (getCurrentSample() > nav2[getCurrentPoint()].length) {
			if (getCurrentPoint() == 0) {
				__doPostBack('btnStartScoring','');
				return;
			}
			setCurrentSample(0);
			setCurrentPoint(getCurrentPoint()-1);
		} else {
			setCurrentSample(getCurrentSample()+1);
		}
	} while (getCurrentSampleDiv() == null);
	showSample();
	moveNextTrainingNav();
}

function moveNextTrainingNav()
{
    var ddlScorePoint = document.getElementById("ddlScorePoint");
    
    if (ddlScorePoint.selectedIndex == ddlScorePoint.length - 1) return;
    
    ddlScorePoint.options[ddlScorePoint.selectedIndex + 1].selected = true;
    
}

function nextPracticeSample(source, skip) {
	if (!skip && getSampleScore() < 0) {
		alert('You must select a score or click "Skip This Sample"');
		return;
	} else if (skip) {
		if (getSampleScore() >= 0) {
			alert('You have already selected a score');
			return;
		} else {
			setSampleSkip();
		}
	}
	hideSample();

	do {
		if (getCurrentSample() > nav2.length) {
			
			var hid = document.getElementById("hidCorrect");
//			var hidTeachersScores = document.getElementById("hidTeachersScores"); 
			if (hid != null) hid.value = scoreCorrect.join(",");
//            if(hidTeachersScores != null) hidTeachersScores.value = teachersScoresArray;  
            __doPostBack('btnContinue', '');
			return;
		} else {
			setCurrentSample(getCurrentSample()+1);
		}
	} while (getCurrentSampleDiv() == null);
	showSample();
}

function scoreSelectPT9(source) {
    
	var btnContinue = document.getElementById("btnContinue");
	
    var score1 = document.getElementById("ddlScores1");
    var score2 = document.getElementById("ddlScores2");
	
	var imgScore1 = document.getElementById("imgScore1");
    var imgScore2 = document.getElementById("imgScore2");
    
    var correct1 = document.getElementById("correct1");
    var correct2 = document.getElementById("correct2");
    
    var score = navOrder[getCurrentSample()].score;

     //Mandar 11/26/2007----------------------
    if (btnContinue.innerHTML != "Continue")
    { 
     // alert("for save ----> hi i am here !!!!");
      teachersScores9pt1.push(score1.value+"");
      teachersScores9pt2.push(score2.value+"");
     // alert("array elements_> "+teachersScores.join(',')+"");
      var hidTeachersScores1 = document.getElementById("hidTeachersScores1"); 
      if(hidTeachersScores1 != null) hidTeachersScores1.value =  teachersScores9pt1.join(",");  
     
      var hidTeachersScores2 = document.getElementById("hidTeachersScores2"); 
      if(hidTeachersScores2 != null) hidTeachersScores2.value =  teachersScores9pt2.join(",");  
      // alert("hidteachersScore.value = "+hidTeachersScores.value+"");
    }
    //11/27/2007 Mandar ----------------------

	// check if the user already answered paper and move to next one
	if (btnContinue.innerHTML == "Continue")
	{
		btnContinue.innerHTML = "Save";
		correct1.innerHTML = "";
		correct2.innerHTML = "";
		
		imgScore1.src = "../Shared/Images/PracticeTest/navHolder.gif";
		imgScore2.src = "../Shared/Images/PracticeTest/navHolder.gif";
		
		score1.options[0].selected = true;
		score2.options[0].selected = true;
		
		score1.disabled = false;
		score2.disabled = false;

		nextPracticeSample(this, false);
		
		return;
		
	}

    // set images for correct/wrong    
    if (score[0] == parseInt(score1.value))
    {
        imgScore1.src = "../Shared/Images/PracticeTest/navCorrect.gif";
    }
    else
    {
        imgScore1.src = "../Shared/Images/navWrongST.gif";
    }
    
    if (score[1] == parseInt(score2.value))
    {
        imgScore2.src = "../Shared/Images/PracticeTest/navCorrect.gif";
    }
    else
    {
        imgScore2.src = "../Shared/Images/navWrongST.gif";
    }

    correct1.innerHTML = "Correct answer is " + score[0];
    correct2.innerHTML = "Correct answer is " + score[1];
	score1.disabled = true;
	score2.disabled = true;
    
	if (score[0] == parseInt(score1.value) && score[1] == parseInt(score2.value))
	{
		setSampleYes(score);
	}
	else
	{
		setSampleNo(score);
	}
	
	
	btnContinue.innerHTML = "Continue";
	return;


    
    return;
    
	/*
    if (scoreCorrect[getCurrentSample()] >= 0) {
    } else if (nav2[getCurrentSample()] == score) {
        setSampleYes(score);
    } else {
        setSampleNo(score);
    }
    enableScores(false);
	var btn = document.getElementById("btnContinue");
    if (btn != null) btn.className = "";
	var skip = document.getElementById("btnSkip");
    if (skip != null) skip.className = "notAnswered";
    */
}

function scoreSelect(source, score)
{
	
	var btnContinue = document.getElementById("btnContinue");
	
    var score1 = document.getElementById("ddlScores1");
	//alert("score is -----:"+score1.value+"");
	
	var imgScore1 = document.getElementById("imgScore1");
    
    var correct1 = document.getElementById("correct1");
    
    var score = nav2[getCurrentSample()];
    //Mandar 11/26/2007----------------------
    if (btnContinue.innerHTML != "Continue")
    { 
     // alert("for save ----> hi i am here !!!!");
      teachersScores.push(score1.value+"");
     // alert("array elements_> "+teachersScores.join(',')+"");
      var hidTeachersScores = document.getElementById("hidTeachersScores"); 
      if(hidTeachersScores != null) hidTeachersScores.value = teachersScores.join(",");  
     // alert("hidteachersScore.value = "+hidTeachersScores.value+"");
    }
    //11/27/2007 Mandar ----------------------
   	// check if the user already answered paper and move to next one
	if (btnContinue.innerHTML == "Continue")
	{
		btnContinue.innerHTML = "Save";
		
		correct1.innerHTML = "";
		
		imgScore1.src = "../Shared/Images/PracticeTest/navHolder.gif";
		
		score1.options[0].selected = true;
		
		score1.disabled = false;
       
       
//        teachersScoresArray.push(score1);
       
            
		nextPracticeSample(this, false);
		
		return;
	} 
    // set images for correct/wrong    
    if (score == parseInt(score1.value))
    {
        
        imgScore1.src = "../Shared/Images/PracticeTest/navCorrect.gif";
       
    }
    else
    {
        imgScore1.src = "../Shared/Images/navWrongST.gif";
    }
    
    correct1.innerHTML = "Correct answer is " + score;
	score1.disabled = true;
    
	if (score == parseInt(score1.value))
	{
		setSampleYes(score);
	}
	else
	{
		setSampleNo(score);
	}
	
	
	btnContinue.innerHTML = "Continue";
	return;

}

function scoreSelect_old(source, score) {
    if (scoreCorrect[getCurrentSample()] >= 0) {
    } else if (nav2[getCurrentSample()] == score) {
        setSampleYes(score);
    } else {
        setSampleNo(score);
    }
    enableScores(false);
	var btn = document.getElementById("btnContinue");
    if (btn != null) btn.className = "";
	var skip = document.getElementById("btnSkip");
    if (skip != null) skip.className = "notAnswered";
}

function hideSample() {
	if (getCurrentSampleDiv() == null) return;
	getCurrentSampleDiv().className = "sampleHidden";
	tabClose('annotation');
}

function showSample() {
	if (getCurrentSampleDiv() == null) return;
	getCurrentSampleDiv().className = "sample";
	if (mode == "Training") {
		setNav2SampleShown();
		tabOpen('annotation');
	} else if (mode == "Practice") {
        resetSampleYesNo();
		var btn = document.getElementById("btnContinue");
		if (btn != null) btn.className = "notAnswered";
		var skip = document.getElementById("btnSkip");
		if (skip != null) skip.className = "";
	}
}

var tabs = ['passage', 'question', 'annotation', 'rubric', 'standard', 'nocontent', 'noscoreAnnotation'];

function getNamedTabContent(name) {
	if (name == 'annotation') {
        if (mode == "Training" || (mode=="Practice" && scoreCorrect[getCurrentSample()] >= 0)) {
			return document.getElementById(name + "Tab" + "_" + getCurrentSuffix());
        } else {
            return document.getElementById("noscoreAnnotationTab");
        }
	} else {
		return document.getElementById(name + "Tab");
	}
}

function getNamedTabButton(name) {
	return document.getElementById(name + "Button");
}

function tabOpenAll(search) {
	for (var t=0; t<tabs.length; t++) {
		var tabName = tabs[t];
		if (search != tabName) {
			tabClose(tabName);
		}
	}
}

function tabOpen(search) {
	for (var t=0; t<tabs.length; t++) {
		var tabName = tabs[t];
		if (search != tabName) {
			tabClose(tabName);
		}
	}
	var btn = getNamedTabButton(search);
	if (btn != null)
		btn.className = "tabOpen";
	var content = getNamedTabContent(search);
	if (content != null) {
		content.className = "tabsContent";
	} else {
		var nocontent = getNamedTabContent('nocontent');
		nocontent.className = "tabsContent";
	}
}

function tabClose(search) {
	var btn = getNamedTabButton(search);
	if (btn != null) btn.className = "";
	var content = getNamedTabContent(search);
	if (content != null) content.className = "tabsContentHidden";
}
