
var artSelPrefix = 'images/';
var artSelNone = artSelPrefix + 'n_chk_10x10.png';
var artSelAll = artSelPrefix + 'a_chk_10x10.png';
var artSelSome = artSelPrefix + 's_chk_10x10.png';

var printAllSrc = artSelPrefix + 'print.png';
var printSomeSrc = artSelPrefix + 'print_selected.png';

var artCheckList = [];

function updateArtCheckAll(chkAllElement)
{
	var selCount = 0;
	for (var i = 0; i < artCheckList.length; ++i)
	{
		if (artCheckList[i].checked)
			++selCount;
	}
	var printImg = document.getElementById("printImg");
	if (selCount == 0)
	{
		chkAllElement.src = artSelNone;
		chkAllElement.title = markAllMsg;
		if (printImg)
		{
			printImg.title = printAllMsg;
			printImg.src = printAllSrc;		
		}
	}
	else
	{
		chkAllElement.src = selCount == artCheckList.length ? artSelAll : artSelSome;
		chkAllElement.title = markNoneMsg;
		if (printImg)
		{
			printImg.title = printSomeMsg;
			printImg.src = printSomeSrc;		
		}
	}
}

function removeArticles()
{
    var removeListDuids = new Array();
    var removeListProfIDs = new Array();
	for (var i = 0; i < artCheckList.length; ++i)
	{
		if (artCheckList[i].checked)
		{
		    var duidel = document.getElementById('artselid_' + (i+1));
			if (duidel)
			{
				removeListDuids[removeListDuids.length] = duidel.innerHTML;
			}
		    var profel = document.getElementById('artprofid_' + (i+1));
			if (profel)
			{
				removeListProfIDs[removeListProfIDs.length] = profel.innerHTML;
			}
		}
	}
	if (removeListDuids.length > 0)
	{
	    removeArticlesFromPage(removeListDuids,removeListProfIDs);
	}
	
}

function toggleArtCheckAll(chkAllElement)
{
	var checked = chkAllElement.src.indexOf('n_chk') != -1;
	chkAllElement.src = checked ? artSelAll : artSelNone;
	for (var i = 0; i < artCheckList.length; ++i)
	{
		artCheckList[i].setChecked(checked);
	}
	chkAllElement.title = checked ? markNoneMsg : markAllMsg;
	var printImg = document.getElementById("printImg");
	if (printImg)
	{
		printImg.title = checked ? printSomeMsg : printAllMsg;
		printImg.src = checked ? printSomeSrc : printAllSrc;
	}

}

function toggleArtCheckProfile(chkAllElement)
{
	var checked = chkAllElement.src.indexOf('n_chk') != -1;
	chkAllElement.src = checked ? artSelAll : artSelNone;
	for (var i = 0; i < artCheckList.length; ++i)
	{
		if (document.getElementById(artCheckList[i].checkBoxId))
		{
			if (document.getElementById(artCheckList[i].checkBoxId).name.indexOf(chkAllElement.id) != -1)
			{
				artCheckList[i].setChecked(checked);
			}
		}
	}
	chkAllElement.title = checked ? markNoneProfileMsg : markAllProfileMsg;
	updateArtCheckAll( document.getElementById('artCheckAll') );
}

function toggleArtCheck(index)
{
	//alert(index);
	artCheckList[index].setChecked( !artCheckList[index].checked );
	updateArtCheckAll( document.getElementById('artCheckAll') );	
}

function addArtSel(checkBoxId, duid)
{
	artCheckList[ artCheckList.length ] = new ArticleChecked(checkBoxId, duid); 
}

function ArticleChecked(checkBoxId, duid)
{
	this.checkBoxId = checkBoxId;
	this.duid = duid;
	this.setChecked(false);
}

ArticleChecked.prototype.updateElement = function()
{
	var el = document.getElementById(this.checkBoxId);
	if (el && el.src)
	{
		el.src = this.checked ? artSelAll : artSelNone;
	}	
}

ArticleChecked.prototype.setChecked = function(checked)
{
	this.checked = checked;
	this.updateElement();
}


function toggleHighlights(element)
{
	var rules = [];
	if (document.styleSheets[0].cssRules)
		rules = document.styleSheets[0].cssRules;
	else 
		if (document.styleSheets[0].rules)
			rules = document.styleSheets[0].rules;
	
	// find rule index
	var i;
	for (i = 0; i < rules.length; ++i)
		if (rules[i].selectorText == ".highlight")
			break;
			
	var img = document.getElementById("markerImg");
	if (!img)
		return;
	
	if (img.src.indexOf("marker_on") == -1)
	{
		showHighlights = false;
		rules[i].style.backgroundColor = "";
		img.src = artSelPrefix + "marker_on.png";	
		img.title = img.title.replace("Skjul", "Vis");	
	}
	else
	{
		showHighlights = true;
		rules[i].style.backgroundColor = "yellow";
		img.src = artSelPrefix + "marker_off.png";
		img.title = img.title.replace("Vis", "Skjul");	
	}
		
}

function toggleSummary(imgEl)
{
	var hide = true;
	if (imgEl)
	{
		if (imgEl.src.indexOf("plus") == -1)
		{
			imgEl.src = artSelPrefix + "plus.png";
			imgEl.title = showSummaryMsg;
			showSummaries = false;
		}
		else
		{
			imgEl.src = artSelPrefix + "minus.png";
			imgEl.title = hideSummaryMsg;
			showSummaries = true;
			hide = false;
		}
	}

	var idx = 0;
	while (showSummary('row_' + idx, hide ? "none" : ""))
		++idx;
}

function updateSummary()
{
	var imgEl = document.getElementById('artSummaryAll');
	if (imgEl)
	{
		//var hide = imgEl.src.indexOf("_off") == -1;
		var hide = !showSummaries;
		imgEl.src = artSelPrefix + (hide ? "plus.png" : "minus.png");
	
		var idx = 0;
		while (showSummary2('row_' + idx, hide ? "none" : ""))
			++idx;
	}

}

function updateHighlights()
{
	var rules = [];
	if (document.styleSheets[0].cssRules)
		rules = document.styleSheets[0].cssRules;
	else 
		if (document.styleSheets[0].rules)
			rules = document.styleSheets[0].rules;
	
	// find rule index
	var i;
	for (i = 0; i < rules.length; ++i)
		if (rules[i].selectorText == ".highlight")
			break;
			
	var img = document.getElementById("markerImg");
	if (!img)
		return;
	
	if (!showHighlights)
	{
		rules[i].style.backgroundColor = "";
		img.src = artSelPrefix + "marker_on.png";
		img.title = img.title.replace("Skjul", "Vis");	
	}
	else
	{
		rules[i].style.backgroundColor = "yellow";
		img.src = artSelPrefix + "marker_off.png";
		img.title = img.title.replace("Vis", "Skjul");	
	}
}

function setupArticleSelectors(pageSize)
{
	artCheckList = [];
	for (var i = 1; i<=pageSize; ++i)
		addArtSel('artsel_' + i, 'xxxx'); 
}

function showSummary2(elementId, display)
{
	var el = document.getElementById(elementId);
	if (!el) return false;
	
	el.style.display = display;
	var pos = elementId.substr( elementId.indexOf('_') + 1 );
	var el2 = document.getElementById('artsumm_' + pos);
	if (display == 'none')
	{
		el2.src = artSelPrefix + "plus.png";
	}
	else
	{
		el2.src = artSelPrefix + "minus.png";
	}
	return true;
}

function showSummary(elementId, display)
{
	var el = document.getElementById(elementId);
	if (!el) return false;

	var pos = elementId.substr( elementId.indexOf('_') + 1 );
	var el2 = document.getElementById('artsumm_' + pos);
	
	if (display)
	{
		el.style.display = display;
		if (el2)
		{
			if (display == 'none')
				el2.src = artSelPrefix + "plus.png";
			else
				el2.src = artSelPrefix + "minus.png";
		}
		
		return true;
	}
	
	if (el.style.display != "none") // already shown
		el.style.display = "none";
	else
		el.style.display = "";
		
	if (el2)
	{
		if (el.style.display == "none")
			el2.src = artSelPrefix + "plus.png";
		else
			el2.src = artSelPrefix + "minus.png";
	}
	return true;
}
