// JavaScript Document

function confirmRating(handle,argumentID,itemID,message_start,message_end) {
	var ratingConfirmation=confirm(message_start+" "+getRatingValue(handle)+" "+message_end);
	
	if (ratingConfirmation==true) {
		window.location="index.php?option=com_argument&task=rateArgument&argument_id="+handle.className.replace(" selected","")+"&rating="+handle.title;
	}
	else {
		// Reset the position of the handle to zero
		resetHandlerPosition(handle,argumentID);
		
	}
}

function cancelInsert(insertType,message) {
	alert(insertType+": "+message);
	window.history.go(-1);
}

function resetHandlerPosition(handle,argumentID) {
	handle.title="0";
	handle.style.left="42px";
	for (index=1;index<=5;index++) {
			document.getElementById(argumentID+"_ratOp"+index).style.zIndex="-1";
		}
}

function getRatingValue(handle) {
	var ratingval;
	if (handle.style.left=="0px") handle.title="-2";
	else if (handle.style.left=="21px") handle.title="-1";
	else if (handle.style.left=="42px") handle.title="0";
	else if (handle.style.left=="62px") handle.title="1";
	else if (handle.style.left=="83px") handle.title="2";
	ratingval=handle.title;
	return ratingval;
}

function validateArgumentForm(form,message) {
  	
	
 	if (!(form.type[0].checked || form.type[1].checked || form.type[2].checked)) {
		alert (message);
		return false;
	}
	else {
		 return true;
	}
 }
 

	
//Function to expand and collapse topic text
function expandCollapseTopic(anchor_link,more_text,less_text) {
		
		elem = document.getElementById("collapsedDiscussionContent");
		
		if (elem.style.display == "none") {
			elem.style.display = "inline";
			anchor_link.firstChild.firstChild.nodeValue = less_text;
		}
		else {
			elem.style.display = "none";
			anchor_link.firstChild.firstChild.nodeValue = more_text;
		}
			
	}
	
//Toggle is for expanding and collapsing treeview argument structure	
function Toggle(node, argtoggleelement)
{

	var browser=navigator.appName;
	var b_version=navigator.appVersion;
	var version=parseFloat(b_version);
	//if (browser=='Microsoft Internet Explorer') {
		// node_to_be_toggled=node.parentNode.parentNode.parentNode.parentNode.parentNode.nextSibling.nextSibling;
		
	//}
	//else {
		//node_to_be_toggled=node.parentNode.parentNode.parentNode.parentNode.parentNode.nextSibling.nextSibling;
		
	//}
	
	
	if (document.getElementById(argtoggleelement).style.display == 'none')
	{
		// Change the image (if there is an image)
		if (node.childNodes.length > 0)
		{
			if (node.childNodes.item(0).nodeName == "IMG")
			{
				
				node.childNodes.item(0).src = "components/com_argument/images/tminus.gif";
			}
		}
		//alert(node_to_be_toggled.style.display);
		document.getElementById(argtoggleelement).style.display = '';
		
		
	}
	// Collapse the branch if it IS visible
	else
	{
		// Change the image (if there is an image)
		if (node.childNodes.length > 0)
		{
			if (node.childNodes.item(0).nodeName == "IMG")
			{
				node.childNodes.item(0).src = "components/com_argument/images/tplus.gif";
			}
		}

		document.getElementById(argtoggleelement).style.display = 'none';
	}

}

//Expands & collapses argument content
function toggleArgText(linknode,more_text,less_text)
{

	
		node_to_be_toggled = document.getElementById(linknode.id+"_ht");
		displayStyle = 'inline';
	
    //	 displayStyle = 'table-row';
    
	
	
		// Unfold the argument content if it isn't visible
	if (node_to_be_toggled.style.display == 'none')
	{
	
		// Change the "Show" Text
	
			if (linknode.innerHTML == more_text)
			{
				linknode.innerHTML = less_text;
			}
		

		node_to_be_toggled.style.display = displayStyle;
	}
	// Collapse the branch if it IS visible
	else
	{
		// Change the image (if there is an image)
//		if (node.childNodes.item(0)childNodes.length > 0)
//		{
			if (linknode.innerHTML == less_text)
			{
				linknode.innerHTML = more_text;
			}
//		}

		node_to_be_toggled.style.display = 'none';
	}
	
}

function sliderSlide(value,argumentID) {
	var handle = document.getElementById(argumentID+"_ratHan");
	for (index=1;index<=5;index++) {
		document.getElementById(argumentID+"_ratOp"+index).style.zIndex="-1";
		if (value<=3) {
			for (index2=value;index2<=3;index2++) {document.getElementById(argumentID+"_ratOp"+index2).style.zIndex="1";}
		}
		else if (value>3) 
			for (index2=3;index2<=value;index2++) {document.getElementById(argumentID+"_ratOp"+index2).style.zIndex="1";}
		}
		
}

function sliderChange(value,argumentID,itemID,message_start,message_end) {
	//alert("Slider change");
	sliderSlide(value,argumentID);
	confirmRating(document.getElementById(argumentID+"_ratHan"),argumentID,itemID,message_start,message_end);
}

function toggleConceptTree(linknode) {
	node_to_be_toggled = linknode.parentNode.parentNode.parentNode.parentNode.nextSibling;
	//immediateParentConceptNode = linknode.parentNode.parentNode.parentNode.parentNode.nodeName;
	
	//if (immediateParentConceptNode.class=='conceptName') parentToBeToggled = immediateParentConceptNode.parentNode;

	if (node_to_be_toggled.style.display=='none') {
		node_to_be_toggled.style.display='';
		linknode.childNodes[0].src = 'components/com_argument/images/tminus.gif';
	}
	else {
		node_to_be_toggled.style.display='none';
		linknode.childNodes[0].src = 'components/com_argument/images/tplus.gif';
	}
}

function deleteArgument(aid,did,itemID,message) {
	var confirmDelete= confirm(message);
	
	if (confirmDelete== true)
 	{
   		window.location="index.php?option=com_argument&task=deleteArgument&aid="+aid+"&id="+did;
	 }

	
}

function deleteDiscussion(did,message) {
	var confirmDelete= confirm(message);
	
	if (confirmDelete== true)
 	{
   		window.location="index.php?option=com_argument&task=deleteDiscussion&id="+did;
	 }

	
}

function deleteTopic(tid,message) {
	var confirmDelete= confirm(message);
	
	if (confirmDelete== true)
 	{
   		window.location="index.php?option=com_argument&task=deleteTopic&id="+tid;
	 }

	
}

//function toggleConceptTreeParent(parent) {
//	if (parent.class == 'conceptName') {
//		parent.parentNode.display.style = '';
//		nextParent = parent.parentNode.parentNode.parentNode.parentNode;
//		toggleConceptTreeParent(nextParent);
//	}
//}