
function ViewTicked(formName, checkboxName)
{
	// submit the form to allow the ticked items to be displayed
	
	// get the ticked item ids
	var itemIds = GetCheckboxItemsToCsv(formName, checkboxName, true);
	
	// check if any items have been ticked
	if (itemIds == '') 
	{
		alert('No ticked Items to View');
	}
	else 
	{
		// submit the form
		var form = document.forms[formName];
		form.hItemIds.value = itemIds;
		form.submit();
	}
}

function ViewAll(formName, checkboxName)
{
	// submit the form to allow all items to be displayed
	
	// get all the item ids
	var itemIds = GetCheckboxItemsToCsv(formName, checkboxName, false);
	
	// submit the form
	var form = document.forms[formName];
	form.hItemIds.value = itemIds;
	form.submit();
}

function PrintTicked(formName, checkboxName)
{
	// display a window to allow ticked items to be printed

	// get the ticked item ids
	var itemIds = GetCheckboxItemsToCsv(formName, checkboxName, true);

	
	// check if any items have been ticked
	if (itemIds == '') 
	{
		alert('No ticked Items to Print');
	}
	else 
	{
		// display the print window
		var sFeatures = 'left=40,top=40,width=950,height=330,resizable,scrollbars,toolbar';
		var winWindow = window.open('../Articles/Article.aspx?liArticleID=' + itemIds + '&PrinterFriendly=true', 'winPrint', sFeatures);
		winWindow.focus();
	}
}

function PrintAll(formName, checkboxName)
{
	// display a window to allow all items to be printed

	// get all the item ids
	var itemIds = GetCheckboxItemsToCsv(formName, checkboxName, false);
	
	// display the print window
	var sFeatures = 'left=40,top=40,width=950,height=330,resizable,scrollbars,toolbar';
	var winWindow = window.open('../Articles/Article.aspx?liArticleID=' + itemIds + '&PrinterFriendly=true', 'winPrint', sFeatures);
	winWindow.focus();
}

function PrintAllTicked(formName, checkboxName, pageNumber, cookieName)
{
	// display a window to allow ticked items to be printed
	var posStart = 0;
	var posEnd = 0;
	var currPage = '';
	var searchPage = '';
	var insertPage = '';
		
	// Get the form
	var form = document.forms[formName];
	// Get the saved Item ids
	//		"Page1=9085924,9088143,9088132|Page2=9087829,9087832,9087828"
	//var savedItemIds = form.hSavedItemIds.value;
	var savedItemIds = getCookie(cookieName);
	// get the ticked item ids
	//		"9085924,9088143,9088132"
	var currPageItemIds = GetCheckboxItemsToCsv(formName, checkboxName, true);
	
	// Update saved item ids
	var updatedItemIds = GetUpdatedPageItems(savedItemIds, pageNumber, currPageItemIds);

	// Set expiry to current time + 1	
	var expiry = new Date();
	expiry.setTime(expiry.getTime()+(1*60*60*1000));
	// Update cookie
	setCookie(cookieName, updatedItemIds, expiry);
			
	// Get all item ids only
	var itemIds = GetItemIds(updatedItemIds);
	
	// check if any items have been ticked
	if (itemIds == '') 
	{
		alert('No ticked Items to Print');
	}
	else 
	{
		// display the print window
		var sFeatures = 'left=40,top=40,width=950,height=330,resizable,scrollbars,toolbar';
		var winWindow = window.open('../Articles/Article.aspx?liArticleID=' + itemIds + '&PrinterFriendly=true', 'winPrint', sFeatures);
		winWindow.focus();
	}
}

function SaveTicked(formName, checkboxName, action, pageNumber, redirectUrl)
{
	// save all ticked checkboxes on this page
	
	// get the ticked item ids
	var itemIds = GetCheckboxItemsToCsv(formName, checkboxName, true);
	
	// submit the form
	var form = document.forms[formName];
	form.hItemIds.value = 'Page' + pageNumber + '=' + itemIds;
	form.hAction.value = action;
	form.hRedirectUrl.value = redirectUrl;
	form.submit();
}

function ClearTicked(formName, action, redirectUrl)
{
	// clear all ticked checkboxes
	
	// submit the form
	var form = document.forms[formName];
	form.hItemIds.value = '';
	form.hAction.value = action;
	form.hRedirectUrl.value = redirectUrl;
	form.submit();	
}

function setCookie (name, value, expires) 
{
	if ( expires )
		document.cookie = name + "=" + value + "; expires=" + expires.toGMTString() +  "; path=/";
	else
		document.cookie = name + "=" + value + "; path=/";		
}

function getCookie(name) {

  var search;

  search = name + "=";
  offset = document.cookie.indexOf(search) ;
  if (offset != -1) {
    offset += search.length;
    end = document.cookie.indexOf(";", offset);
    if (end == -1)
      end = document.cookie.length;
    return unescape(document.cookie.substring(offset, end));
  }
  else
    return "";
}

function GetUpdatedPageItems(savedItemIds, pageNumber, currPageItemIds)
{
	// savedItemIds - all ticked items (e.g "Page1=9085924,9088143,9088132|Page2=9087829,9087832,9087828")
	// pageNumber - current page (e.g. "1")
	// currPageItemIds - ticked items on the current page (e.g. "9085924,9088143")
	// This function must return an updated list of all items (e.g. "Page1=9085924,9088143|Page2=9087829,9087832,9087828")

	var posStart = 0;
	var posEnd = 0;
	var currPage = '';
	var searchPage = '';
	var insertPage = '';
	
	// Insert current page ticked item ids if does not already exists
	//		"Page3=9087829,9087832"
	// or update current page ticked item ids
	//		"Page2=9087829,9087832" (9087828 - unticked)
	if (savedItemIds.length > 0)
	{
		// Search the current page in the saved items
		searchPage = 'Page' + pageNumber + '=';
		posStart = savedItemIds.indexOf(searchPage);
		if (posStart > - 1)
		{
			// Replace existing pages items
			posEnd = savedItemIds.indexOf("|",posStart);
			if (posEnd > -1)
			{
				// replace the item Ids for this page
				savedItemIds = savedItemIds.substring(0,posStart + searchPage.length) + currPageItemIds + savedItemIds.substring(posEnd);
			}
			else
			{
				// this page is the very last from the savedItemIds
				savedItemIds = savedItemIds.substring(0,posStart + searchPage.length) + currPageItemIds;
			}		
		}
		else
		{
			// Do this only if there are ticked items in the current page
			if (currPageItemIds.length > 0)
			{
				// Insert current page items in the right order
				
				// Search where to insert the current page
				posStart = savedItemIds.indexOf('=');
				while (posStart > -1)
				{
					currPage = savedItemIds.substring(savedItemIds.lastIndexOf("Page",posStart) + 4, posStart);
					if (pageNumber < currPage)
					{
						insertPage = currPage;
						break;
					}
					posStart = savedItemIds.indexOf('=',posStart+1);
				}
				
				if (insertPage.length > 0)
				{
					posStart = savedItemIds.indexOf('Page' + insertPage + '=');
					savedItemIds = savedItemIds.substring(0,posStart+1) + 'age' + pageNumber + '=' + currPageItemIds + '|P' + savedItemIds.substring(posStart + 1);
				}
				else
				{
					// Append at the end
					savedItemIds += '|Page' + pageNumber + '=' + currPageItemIds;
				}
			}
		}
	}
	else
	{
		if (currPageItemIds.length > 0)
		{
			// This current page is the only page with ticked items
			savedItemIds = 'Page' + pageNumber + '=' + currPageItemIds;
		}
	}
	
	return savedItemIds;
}

function GetItemIds(savedItemIds)
{
	// savedItemIds - all ticked items (e.g "Page1=9085924,9088143,9088132|Page2=9087829,9087832,9087828")
	// This function must return extracted list of item ids (e.g. "9085924,9088143,9087829,9087832,9087828")

	var posStart = 0;
	var posEnd = 0;
	var itemIds = '';
	
	posStart = savedItemIds.indexOf('=');
	while (posStart > -1)
	{
		posEnd = savedItemIds.indexOf('|',posStart);
		if (posEnd > -1)
		{
			if (savedItemIds.substring(posStart + 1, posEnd).length > 0)
			{
				if (itemIds.length > 0)
				{
					itemIds = itemIds + ',';
				}
				// This item id is not the last one
				itemIds = itemIds + savedItemIds.substring(posStart + 1, posEnd);
			}
		}
		else
		{
			if (savedItemIds.substring(posStart + 1).length > 0)
			{
				if (itemIds.length > 0)
				{
					itemIds = itemIds + ',';
				}
				// This is the last item id
				itemIds = itemIds + savedItemIds.substring(posStart + 1);
			}
		}
		
		posStart = posEnd;
		
		if (posEnd > -1)
		{
			posStart = savedItemIds.indexOf('=',posEnd);
		}
	}
	
	return itemIds;
}

function GetCheckboxItemsToCsv(formName, checkboxName, onlyTickedItems)
{
	// return a comma seprated list of ticked items
	
	var checkbox = document.forms[formName].elements[checkboxName];
	var itemIds   = '';
	var i;
	
	// get checkbox values
	if (checkbox.length == null)	// single checkbox
	{
		if (onlyTickedItems)
		{
			if (checkbox.checked)
			{
				itemIds = checkbox.value + ',';
			}
		}
		else
		{
			itemIds = checkbox.value + ',';
		}
	}
	else
	{
		for (i = 0; i < checkbox.length; i++)
		{
			if (onlyTickedItems)
			{
				if (checkbox[i].checked)
				{
					itemIds += checkbox[i].value + ',';
				}
			}
			else
			{
				itemIds += checkbox[i].value + ',';
			}
		}
	}
	
	itemIds = itemIds.substring(0, itemIds.length - 1);
		
	// return csv list of selected items
	return itemIds;
}

