

window.onload = function()
{
	stopIt = "no";
	
	//setupCalendars();
	//startList();   	

//if (RoundedCornersSupported()) 
//{
//		Rounded("div#Filters", "#ffffff", "#cadb2a", "small");
//		Rounded("div#Commands", "#ffffff", "#dfdfdf", "small");
//		Rounded("div#ErrorMessage", "#ffffff", "#cc3333");
//		Rounded("div#Message", "#ffffff", "#3333cc");
//	}

	gotoPage(pageNo);
}

function changeProfile()
{
	document.getElementById('sourcePicker').value = 'All';
	gotoPage(1);
}

function gotoPage(pageNo)
{
	if (stopIt && stopIt == "yes")
		return;
		
	if (pageNo <= 1)
		pageNo = 1;
	if (pageNo > pageCount && pageCount > 0)
		pageNo = pageCount;
			
	//showMessage("Henter artikler for side " + pageNo);
	
	newPageNo = pageNo;
	// calc filters
//	var profileId = document.getElementById('profilePicker').value;
//	var sourceName = document.getElementById('sourcePicker').value;
//	var words = document.getElementById('searchTerms').value;
//	var fromDate = document.getElementById('fromDateValue').value;
//	var toDate = document.getElementById('toDateValue').value;
	
	var profileId = "";
	var sourceName = "";
	var words = "";
	var fromDate = "";
	var toDate = "";
	
	var parms = [
		'UrlID',		urlID, 
		'PageNo',		pageNo,
		'ProfileId',	profileId,
		'SourceName',	sourceName,
		'From',			fromDate,
		'To',			toDate,
		'Words',		words,
		'UseSentDate',	(useSentDate ? 'true' : 'false'),
		'ShowHighlights', (showHighlights ? 'true' : 'false'),
		'ShowSummaries', (showSummaries ? 'true' : 'false'),
		'PageSize',		pageSize,
		'Stylesheet', 'sas/ArticlesC.xsl'
	];
    
	getSortParams( parms );
	imhAsync_Callback('GetArticleList.aspx',parms,articles_CB);
}

function removeArticlesFromPage(removeListDuids, removeListProfIDs)
{
	if (stopIt && stopIt == "yes")
		return;

	var parms = [
		'UrlID',                urlID, 
		'RemoveListDuids',      removeListDuids.join(","),
		'RemoveListProfIDs',    removeListProfIDs.join(",")
	];
	var result = imhAsync_Callback('RemoveArticles.aspx',parms);
	gotoPage(pageNo);
		
}

function getSortParams(parmArray)
{
	var el = getParamElement('xPrimaryField', 'xSortByFirst');
	if (el)
	{
		parmArray[parmArray.length] = "PrimaryField";
		parmArray[parmArray.length] = el.innerHTML;
	}
	
	el = getParamElement('xPrimaryOrder', 'xSortOrderFirst');
	if (el)
	{
		parmArray[parmArray.length] = "PrimaryOrder";
		parmArray[parmArray.length] = el.innerHTML;
	}
	
	el = getParamElement('xSecondaryField', 'xSortBySecond');
	if (el)
	{
		parmArray[parmArray.length] = "SecondaryField";
		parmArray[parmArray.length] = el.innerHTML;
	}
	
	el = getParamElement('xSecondaryOrder', 'xSortOrderSecond');
	if (el)
	{
		parmArray[parmArray.length] = "SecondaryOrder";
		parmArray[parmArray.length] = el.innerHTML;
	}
}

function getParamElement(primaryName, secondaryName)
{
	var el = document.getElementById(primaryName);
	if (!el)
		el = document.getElementById(secondaryName);
	return el;
}

function changeSort(fieldName)
{
	secondaryField = document.getElementById('xSecondaryField').innerHTML;
	secondaryOrder = document.getElementById('xSecondaryOrder').innerHTML;
	
	primaryField = document.getElementById('xPrimaryField').innerHTML;
	primaryOrder = document.getElementById('xPrimaryOrder').innerHTML;
	
	if (fieldName == primaryField)
	{
		primaryOrder = primaryOrder == "ASC" ? "DESC" : "ASC";
		document.getElementById('xPrimaryOrder').innerHTML = primaryOrder;
	}
	else
	{
		document.getElementById('xSecondaryField').innerHTML = document.getElementById('xPrimaryField').innerHTML;
		document.getElementById('xSecondaryOrder').innerHTML = document.getElementById('xPrimaryOrder').innerHTML;
		document.getElementById('xPrimaryField').innerHTML = fieldName;
		document.getElementById('xPrimaryOrder').innerHTML = fieldName == "ArticleDate" || fieldName == "HighPriority" ? "DESC" : "ASC";
	}
	gotoPage(1);
}


function articles_CB2()
{
	stopIt = "yes";

	if (RoundedCornersSupported()) 
	{
		//RoundedTop("td#hdrDate", "#ffffff", "#ddeeff", "small");
 		//RoundedTop("td#hdrProfile", "#ffffff", "#ddeeff", "small");
		//RoundedTop("td#hdrSource", "#ffffff", "#ddeeff", "small");
		//RoundedTop("td#hdrTitle", "#ffffff", "#ddeeff", "small");
	}	

	el = document.getElementById('currPageNo');
	if (el)
		el.innerHTML = newPageNo;
	pageNo = newPageNo;

	el = document.getElementById('xPageCount');
	pageCount = parseInt(el.innerHTML, 10);
//	var el2 = document.getElementById('currPageCount');
//	el2.innerHTML = pageCount;

	el = document.getElementById('xShowSummaries');
	showSummaries = el.innerHTML == 'true';

	el = document.getElementById('xShowHighlights');
	showHighlights = el.innerHTML == 'true';
	el = document.getElementById('xPagingBar');
	el2 = document.getElementById('pagingBar');
	if (el2)
		el2.innerHTML = el.innerHTML;

	el = document.getElementById('xProfileFilter');
//	el2 = document.getElementById('qProfilePicker');
//	el2.innerHTML = el.innerHTML;

	el = document.getElementById('xSourceFilter');
//	el2 = document.getElementById('qSourcePicker');
//	el2.innerHTML = el.innerHTML;

	if (pageCount == 0)
		showErrorMessage(noArticlesMessage);
	else
	{
	    try {
		    updateSummary();
		    updateHighlights();
		    showContent(); 
		    setupArticleSelectors(pageSize);
		}
		catch (ex) {}
	}
	stopIt = "no";

}

// this function is called when the page is done loading
function articles_CB(result)
{
	if (result.error)
	{
		showErrorMessage('Der opstod en fejl da artiklerne skulle hentes:\n' + result.error);
	}
	else
	{
		var el = document.getElementById("contentTableDiv");
		el.innerHTML = result.value; 
		articles_CB2();
	}
}


function startList() 
{
	var navRoot = document.getElementById("periodPicker");
	for (var i=0; i < navRoot.childNodes.length; ++i) 
	{
		node = navRoot.childNodes[i];
		if (node.nodeName=="LI") 
		{
			node.onmouseover = function() { this.className+=" over"; }
			node.onmouseout = function() { this.className=this.className.replace(" over", ""); }
		}
	}
	
	navRoot = document.getElementById("pageSizePicker");
	for (var i=0; i < navRoot.childNodes.length; ++i) 
	{
		node = navRoot.childNodes[i];
		if (node.nodeName=="LI") 
		{
			node.onmouseover = function() { this.className+=" over"; }
			node.onmouseout = function() { this.className=this.className.replace(" over", ""); }
		}
	}	
}

function setupCalendars()
{
	Calendar.setup(
		{
		inputField : "fromDateValue", 
		ifFormat : "%d-%m-%Y", 
		button : "fromDate",
		displayArea: "fromDate",
		daFormat : "%d-%m-%Y",
		weekNumbers: false,
		electric : true,
		align: "Bl"
		}
	);
	Calendar.setup(
		{
		inputField : "toDateValue", 
		ifFormat : "%d-%m-%Y", 
		button : "toDate",
		displayArea: "toDate",
		daFormat : "%d-%m-%Y",
		electric : true,
		weekNumbers: false
		}
	);
}


