
var responseValue = new Array();
var xmlHttp;
var xmlHttpAnswer;
var xmlHttpRquirements;
var xmlHttpQuestimate;
var qCnt = 1;
var currentAnswerBlock = '';
var aReqs = new Array();

var swapQuestionBlockHTML = '';

function sortReqs(a, b)
{
	return a[0] - b[0];
}

function GetXmlHttpObject()
{
 var xmlHttp = null;
 try
 {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
 }
 catch (e)
 {
  // Internet Explorer
  try
  {
   xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  }
  catch (e)
  {
   try
   {
    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
   }
   catch (e)
   {
    alert("Your browser does not support AJAX!");
    return false;
   }
  }
 }
 return xmlHttp;
}

function Clear()
{
	document.getElementById('hdiRequirements').innerHTML = '';
	document.getElementById('hdiQuestions').innerHTML = '';
}

function GetQuestionCount(categoryId)
{
	this.stateChangedQuestionCount = function stateChangedQuestionCount()
	{
		if(xmlHttpQuestimate.readyState == 4)
		{
			var qcountBlock = document.getElementById('question_estimate');
			var html = xmlHttpQuestimate.responseText;
			qcountBlock.innerHTML = html;
		}
	}
	var htmlString = "hdi.asp?hdi_action=getqest&cid=" + categoryId;
	xmlHttpQuestimate = GetXmlHttpObject();
	xmlHttpQuestimate.onreadystatechange = stateChangedQuestionCount;
	xmlHttpQuestimate.open("GET", htmlString, true);
	xmlHttpQuestimate.send(null);
}

function WriteNextQuestion(categoryId, questionId, answerId)
{
  this.stateChangedNextQuestion = function stateChangedNextQuestion()
  {
	if(xmlHttp.readyState == 4)
	{
		var questions = document.getElementById('hdiQuestions');
		var html = xmlHttp.responseText;
		if(html == '')
		{
			CheckRequirements();
		}
		else
		{
			//var questionLabel = document.getElementById("questionLabel");
			//questionLabel.innerHTML = qCnt + ". ";
			questions.innerHTML = questions.innerHTML + html;
			qCnt++;
		}
		
	}
  }
  var htmlString = "hdi.asp?hdi_action=getquestion&cid=" + categoryId;
  if(questionId != "")
  {
   htmlString += "&qid=" + questionId;
   if(answerId != "")
   {
    htmlString += "&aid= " + answerId;
   }
  }
  xmlHttp = GetXmlHttpObject();
  xmlHttp.onreadystatechange = stateChangedNextQuestion;
  xmlHttp.open("GET", htmlString, true);
  xmlHttp.send(null);
}


function GetRequirements(arString)
{
	this.stateChangedRequirements = function stateChangedRequirements()
	{
		if(xmlHttpRequirements.readyState == 4)
		{
			var myreqs = document.getElementById('hdiQuestions');
			var output = xmlHttpRequirements.responseText;
			swapQuestionBlockHTML = output + '<br/><div style="text-align:right;" onClick="SwapQuestionsRequirements();"><a href="" onClick="return false;"><img src="/images/ViewQuestions.jpg"></a></div>';
			myreqs.innerHTML = myreqs.innerHTML + '<br/><div style="text-align:right;" onClick="SwapQuestionsRequirements();"><a href="" onClick="return false;"><img src="/images/ViewChecklist.jpg"></a></div>';
		}
	}
	var htmlString = "hdi.asp?hdi_action=getreq&ars=" + arString;
	xmlHttpRequirements = GetXmlHttpObject();
	xmlHttpRequirements.onreadystatechange = stateChangedRequirements;
	xmlHttpRequirements.open("GET", htmlString, true);
	xmlHttpRequirements.send(null);
}

function GetAnswerAndResponses(questionId, answerId)
{
	this.stateChangedAnswerAndResponses = function stateChangedAnswerResponses()
	{
		if(xmlHttpAnswer.readyState == 4)
		{
			var mygoback = document.getElementById('goback' + questionId);
			var myanswers = document.getElementById('hdiAnswer_' + questionId);
			var output = xmlHttpAnswer.responseText;
			myanswers.innerHTML = output;
			Browser.StyleRef(mygoback).display = 'inline';
			// Browser.StyleRef(mygoback).cursor = 'pointer';
		}
	}
	var htmlString = "hdi.asp?hdi_action=getarr&qid=" + questionId + "&aid=" + answerId;
	xmlHttpAnswer = GetXmlHttpObject();
	xmlHttpAnswer.onreadystatechange = stateChangedAnswerAndResponses;
	xmlHttpAnswer.open("GET", htmlString, true);
	xmlHttpAnswer.send(null);
}

function SwapQuestionsRequirements()
{
	var temp = document.getElementById('hdiQuestions').innerHTML; 
	document.getElementById('hdiQuestions').innerHTML = swapQuestionBlockHTML; 
	swapQuestionBlockHTML = temp;
}

function CheckRequirements()
{
	var hdiform = document.getElementById('hdiForm');
	var reqs = new Array();
	var out = '';
	var j = 0;
	for( var i = 0; i < hdiform.elements.length; i++ )
	{
		var elem = hdiform.elements[i];
		if( elem.type == "hidden" && elem.name == "requirements" )
		{
			reqs[j] = elem.value;
			out += (out == '')?"":";";
			out += reqs[j];
			j++;
		}
	}
	GetRequirements(out);
}

function GoBack(block, categoryId, questionId, answerId)
{
	var temp = document.getElementById('hdiQuestions').innerHTML;
	var idx = temp.indexOf(block);
	//var len = document.getElementById(block).innerHTML.length;
	temp = temp.substr(0, idx);
	document.getElementById('hdiQuestions').innerHTML = temp;
	WriteNextQuestion(categoryId, questionId, answerId);
}

function PrintChecklist(content)
{
	var win = window.open("", "mywindow");
	var content = '<html><body onLoad="JavaScript: window.print(); window.close();"><strong>Questions:</strong><br><br>' + swapQuestionBlockHTML + '<br>' + document.getElementById('hdiQuestions').innerHTML + '</body></html>';
	content = content.replace('<IMG src="http://www.leetc.com/images/PrintHdi.jpg">', '');
	content = content.replace('<IMG style="PADDING-RIGHT: 0px; PADDING-LEFT: 0px; VERTICAL-ALIGN: bottom; PADDING-TOP: 0px" src="http://www.leetc.com/images/ChangeAnswer2.jpg">', '')
	content = content.replace('<IMG style="PADDING-RIGHT: 0px; PADDING-LEFT: 0px; VERTICAL-ALIGN: bottom; PADDING-TOP: 0px" src="http://www.leetc.com/images/ChangeAnswer2.jpg">', '')
	content = content.replace('\<A onclick\=\"return false\;\" href\=\"\"><IMG src\=\"http\:\/\/www\.leetc\.com\/images\/ViewChecklist\.jpg\"\>\<\/A\>', '');
	content = content.replace('<DIV style="TEXT-ALIGN: right" onclick=SwapQuestionsRequirements();><A onclick="return false;" href=""><IMG src="http://www.leetc.com/images/ViewQuestions.jpg"></A></DIV>', '');
	win.document.write(content);	
}
