//* JAVASCRIPT FOR RESEARCH DOCS
//*************************************************************** *

// popup boxes for End Notes in research documents
$(document).ready(function() {
	$('a.tips').cluetip({
		cluetipClass: 'rounded',
		dropShadow: false,
		showtitle: false,
		positionBy: 'mouse',
		local: true,
		hideLocal: false,
		sticky: true,
		width: 237,
		closeText: 'Close'
	});
});


// Changes the font size of the document
function changeTextSize(clicked, fontPercent) {
	// Grab #wrapper to resize the doc content
	var content = document.getElementById("wrapper");
	var sidebar = document.getElementById("sidebarRight_sidebar");
	var footer = document.getElementById("footerblock");
	
	// resize the font
	content.style.fontSize = fontPercent + "%";
	sidebar.style.fontSize = fontPercent + "%";
	footer.style.fontSize = fontPercent + "%";
	
	// Make clicked text-size icon the "current" one
	var sizerDiv = document.getElementById("textSize");
	var sizerLinks = sizerDiv.getElementsByTagName("a");
	for (i=0; i<sizerLinks.length; i++) {
		sizerLinks[i].className = "textSizers"
	}
	clicked.className = "textSizers current";
}


// Sets and saves the rating
function setrating(rating,url){
	var postURL=url+"?action=3&rating="+rating;
	document.getElementById("ratingMessage2").innerHTML = rating;
	postrating(postURL);

}

// Removes the user's rating
function deleteRating(url){
	var postURL=url+"?action=8";
	postrating(postURL);
}


// Self-evaluation: Clears the form
function clearSDForm(){
	var inputCount = document.getElementById("Practice").getElementsByTagName("input");
	for(i=0;i<inputCount.length;i++) {
		if (inputCount[i].type == "radio") {
			inputCount[i].checked = false;
		}
	}
}
// Self-evaluation: Submits the form
function validateAndSubmitSD(myform,c,url) {
	for(i=1;i<c;i=i+2) {
		if(document.getElementById("radio" + i).checked == false && document.getElementById("radio" + (i + 1)).checked == false) {
			alert("You must answer all of the questions.");
			return false;
		}
	}
	url=url+"?action=10";
	for(i=0;i<c;i++) {
		var radNum = i+1;
		var element = document.getElementById("radio" + radNum);
		if(element.checked == true){
			url=url+"&&"+element.name+"="+element.value;
		}
	}
	postSDform(url);
}


function offColor() {
// When mousing off the rating bar, resets all to grey and then 
// highlights up to the current user's rating, if any.

	// retrieve current rating from hidden input on ratings.jsp
	if (document.getElementById("documentRating").value != "") {
	
		var currentRating = new Number(document.getElementById("documentRating").value);
		
		// reset all to grey
		for (i=0; i<=10; i++) {
			var idy = ["rate"+i];
			document.getElementById(idy).style.background = "#aaa";
		}
		// color up to the chosen rating
		var orange;
		if (currentRating > -1) {
			orange = currentRating;
		}
		while (orange > -1) {
			var idx = ["rate"+orange];
			document.getElementById(idx).style.background = "#C7731B";
			orange-- ;
		}
	} else {
		// reset all to grey
		for (i=0; i<=10; i++) {
			var idy = ["rate"+i];
			document.getElementById(idy).style.background = "#aaa";
		}
	}
}

function changeColor(hov) {
// On mouseover, resets all rating bar boxes to grey, and highlights
// boxes up to the number currently being moused over.

	// reset all to grey
	for (i=0; i<=10; i++) {
		var idy = ["rate"+i];
		document.getElementById(idy).style.background = "#aaa";
	}
	// color up to the hover number	
	for (i=0; i<=hov; i++) {
		var idz = ["rate"+i];
		document.getElementById(idz).style.background = "#C7731B";
	}
}


// Hides 'green box' (id="docBoxWrapper") if no content inside green box
function hideDocBox() {
	var greenBox = document.getElementById("docBox");// Checks if docBox is empty or only contains a text node (basically empty)
	if (greenBox && (greenBox.childNodes.length < 1)) {	
		document.getElementById("docBoxWrapper").style.display = "none";
	} else {
		document.getElementById("docBoxWrapper").style.display = "block";
	}
}
hideDocBox();

