function getAjaxObject(){
	var xmlHttp;
	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 null;
	      }
	    }
	  }
	return xmlHttp;
}

function loadTestCases(pageNumber){
	var xmlHttp=getAjaxObject();
	if(xmlHttp==null){
		return false;
	}
	xmlHttp.onreadystatechange=function(){
		if(xmlHttp.readyState==4){
			if(xmlHttp.status==200 || xmlHttp.status==0){
				document.getElementById("testCases").innerHTML=xmlHttp.responseText;
				}else{
					document.getElementById("testCases").innerHTML="No test cases or test file not found";
				}
		}
	}
	try{
	var page = "cases" + pageNumber + ".html";
	xmlHttp.open("GET",page, true);
	xmlHttp.send(null);
	}catch(e){
		document.getElementById("testCases").innerHTML="No test cases or test file not found";
	}
}

function loadSource(fileName,HLFunction){
	var xmlHttp=getAjaxObject();
	if(xmlHttp==null){
		return false
	}
	xmlHttp.onreadystatechange=function(){
		if(xmlHttp.readyState==4){
			if(xmlHttp.status==200 || xmlHttp.status==0){
			document.getElementById("sourceBox").innerHTML=xmlHttp.responseText;
			eval(HLFunction);
			}else{
				document.getElementById("sourceBox").innerHTML="Source file not found";
			}
		}
	}
	try{
	xmlHttp.open("GET",fileName,true);
	xmlHttp.send(null);
	}catch(e){
		document.getElementById("sourceBox").innerHTML="Source file not found";		
	}
}

function getElByClass(classname, node) {
	if (!node)
		node = document.getElementsByTagName("body")[0];
	var a = [];
	var re = new RegExp('\\b' + classname + '\\b');
	var els = node.getElementsByTagName("*");
	for ( var i = 0, j = els.length; i < j; i++)
		if (re.test(els[i].className))
			a.push(els[i]);
	return a;
}

function switchContracts(showC){
	if(showC){
		showAll("contract");
		hideAll("nonContract");
	}
	else{
		showAll("nonContract");
		hideAll("contract")
	}
	
}

function switchCoverage(toPercent){
	var percentAr = getElByClass("percent");
	var numAr = getElByClass("num");
	for( var i = 0, j=percentAr.length; i<j; i++){
		if(toPercent){
			hide(percentAr[i]);
			show(numAr[i]);
		}
		else{
			show(percentAr[i]);
			hide(numAr[i]);
		}
	}
}
function switchClassList(option){
	var branchAr = getElByClass("branch");
	var instAr = getElByClass("instruction");
	for(var i = 0, j=branchAr.length; i<j; i++){
		if(option ==1){
			showCell(branchAr[i]);
			hide(instAr[i]);
		}
		if(option ==2){
			hide(branchAr[i]);
			showCell(instAr[i]);
		}
		if(option ==3){
			showCell(branchAr[i]);
			showCell(instAr[i]);
		}
		
	}
}

function load(){
	window.status="page is loaded;"
}

function switchTo(className,id){
	hideAll(className);
	show(document.getElementById(id));
}

function hideAll(className){
	var elts = getElByClass(className);
	for(var i=0, j=elts.length; i<j; i++){
		hide(elts[i]);
	}
}

function showAll(className){
	var elts = getElByClass(className);
	for(var i=0, j=elts.length; i<j; i++){
		if(elts[i].tagName!=='TR'){
			show(elts[i]);
		}else{
			showTable(elts[i]);
		}
	}
}

function hide(elt){
	elt.style.display = 'none';
}

function show(elt) {
	if(elt.tagName=='SPAN'){
		elt.style.display='inline';
	}
	else{
	elt.style.display = 'block';
	}
}

function showTable(elt) {
	elt.style.display = 'table-row';
}

function showCell(elt){
	elt.style.display = 'table-cell';
}

function highlightGreen(ids){
	for(var i=0, j=arguments.length; i<j; i++){
		var elt = document.getElementById(arguments[i]);
		elt.style.backgroundColor=('#66CC66');
	}
}

function highlightYellow(ids){
	for(var i=0, j=arguments.length; i<j; i++){
		var elt = document.getElementById(arguments[i]);
		elt.style.backgroundColor=('#CCFF33');
	}
}

function scrollToID(id){
	var elt = document.getElementById(id);
	scrollTo(0,calculateTotalOffsetTop(elt));
}


function calculateTotalOffsetTop(elt){
	if(elt.offsetParent==null){
		return elt.offsetTop;
	}
	else{
		return elt.offsetTop+calculateTotalOffsetTop(elt.offsetParent);
	}
}


