var albumList = new Array();
var imgList = new Array();

function addAlbum(groupID)
{
	//Expiration date
	var expirationDate = new Date();
	expirationDate.setTime(expirationDate.getTime() + 60 *(24*60*60*1000));	

	//Add the group id to the array.
	albumList[albumList.length]=groupID;
	SetCookie("albumList",albumList.join(","),expirationDate);
}

function removeAlbum(groupID)
{
	//Expiration date
	var expirationDate = new Date();
	expirationDate.setTime(expirationDate.getTime() + 60 *(24*60*60*1000));	
	
	var i=0;
	while((i<albumList.length) && (albumList[i]!=groupID)) i++;
	if(albumList[i]==groupID) albumList.splice(i,1);
	SetCookie("albumList",albumList.join(","),expirationDate);
}

function addImg(imgID)
{
	//Expiration date
	var expirationDate = new Date();
	expirationDate.setTime(expirationDate.getTime() + 60 *(24*60*60*1000));	

	//Add the img id to the array.
	imgList[imgList.length]=imgID;
	SetCookie("imgList",imgList.join(","),expirationDate);
}

function removeImg(imgID)
{
	//Expiration date
	var expirationDate = new Date();
	expirationDate.setTime(expirationDate.getTime() + 60 *(24*60*60*1000));	
	
	var i=0;
	while((i<imgList.length) && (imgList[i]!=imgID)) i++;
	if(imgList[i]==imgID) imgList.splice(i,1);
	SetCookie("imgList",imgList.join(","),expirationDate);
}


function loadCookies()
{
	if(GetCookie("albumList")!=null) albumList=(GetCookie("albumList")).split(",");
	if(GetCookie("imgList")!=null) imgList=(GetCookie("imgList")).split(",");
	
}

function albumCheck(checkBox)
{
    if(checkBox.checked==true) addAlbum(checkBox.value);
	else removeAlbum(checkBox.value);
	
	updateSelected();
}

function imgCheckSingle(checkBox)
{
	if(checkBox.checked==true)
	{
		addImg(checkBox.value);
		imgInfo0.style.backgroundColor="#ffff99";
	}
	else
	{
		removeImg(checkBox.value);
		imgInfo0.style.backgroundColor="#FFFFFF";
	}
	
	updateSelected();
}

function imgCheck(checkBox,i)
{
    if(checkBox.checked==true)
	{
		addImg(checkBox.value);
   
		var imgInfo = document.getElementById("imgInfo" + i);
		imgInfo.style.backgroundColor="#ffff99";
	}
	else
	{
		removeImg(checkBox.value);

		var imgInfo = document.getElementById("imgInfo" + i);
		imgInfo.style.backgroundColor="#ffffff";
	}
	updateSelected();
}

function checkAlbumSelection()
{
	if((document.aspnetForm) && (document.aspnetForm.c0))
	{
	    if(document.aspnetForm.c0.value!=null)
		{
			c=0;
			while((document.aspnetForm.c0.value!=albumList[c]) && (c<albumList.length)) c++;
				
			if(document.aspnetForm.c0.value==albumList[c]) document.aspnetForm.c0.checked=true;
			else document.aspnetForm.c0.checked=false;
		}
		else 
		{
			for(var i=0;i<document.aspnetForm.c0.length;i++)
			{
				c=0;
				while((document.aspnetForm.c0[i].value!=albumList[c]) && (c<albumList.length)) c++;
					
				if(document.aspnetForm.c0[i].value==albumList[c]) document.aspnetForm.c0[i].checked=true;
				else document.aspnetForm.c0[i].checked=false;
			}
		}
		updateSelected();
	}
}

function checkImgSelection()
{
    var aspnetForm = document.getElementById("aspnetForm");
    
    if((aspnetForm) && (aspnetForm.c0))
    {
        if(document.aspnetForm.c0.value!=null)
	    {
	        c=0
		    while((document.aspnetForm.c0.value!=imgList[c]) && (c<imgList.length)) c++;
    			
		    if(document.aspnetForm.c0.value==imgList[c])
		    {
			    document.aspnetForm.c0.checked=true;
			    
			    var imgInfo = document.getElementById("imgInfo" + "0");
			    imgInfo.style.backgroundColor="#ffff99";
		    }
		    else 
		    {
			    document.aspnetForm.c0.checked=false;

			    var imgInfo = document.getElementById("imgInfo" + "0");
			    imgInfo.style.backgroundColor="#ffffff";
		    }
    	
	    } 
	    else
	    {
	    
	    
	        for(var i=0;i<document.aspnetForm.c0.length;i++)
		    {        
			    c=0;
			    while((document.aspnetForm.c0[i].value!=imgList[c]) && (c<imgList.length)) c++;
    				
			    if(document.aspnetForm.c0[i].value==imgList[c])
			    {	        
			        document.aspnetForm.c0[i].checked=true;
				    
				    var imgInfo = document.getElementById("imgInfo" + i);
		            imgInfo.style.backgroundColor="#ffff99";	    
			    }
			    else
			    {
			        document.aspnetForm.c0[i].checked=false;
				    
				    var imgInfo = document.getElementById("imgInfo" + i);
		            imgInfo.style.backgroundColor="#ffffff";	
			    }
		    }
	    }
	}

	updateSelected();
}

//Update the selection Basket
function updateSelected()
{
    var selectedAlbums = document.getElementById("selectedAlbums");
    var selectedImages = document.getElementById("selectedImages");
    
    if((selectedAlbums) && (selectedImages))
    {
        selectedAlbums.innerHTML=albumList.length;
	    selectedImages.innerHTML=imgList.length;
    	
	    var totalItems = albumList.length + imgList.length;

	    if(totalItems > 0)
	    {
		    if(totalItems==1)
		    {
			    display="<span class='MenuSubGroup'><a href='../Users/download.aspx'>Download " + totalItems + " item &nbsp; <img src='../images/download.gif' border='0'></a><br><a href='javascript:clearSelection();'>Empty Basket</a></span>";
		    }
		    else 
		    {
			    display="<span class='MenuSubGroup'><a href='../Users/download.aspx'>Download " + totalItems + " items &nbsp; <img src='../images/download.gif' border='0'></a><br><a href='javascript:clearSelection();'>Empty Basket</a></span>";
		    }
		    selectedResult.innerHTML=display;
	    }
	    else selectedResult.innerHTML="";
    }
}

function clearSelection()
{
	albumList = new Array();
	imgList = new Array();
	
	DeleteCookie("albumList");
	DeleteCookie("imgList");
	
	updateSelected();
	
	if(pageType)
	{
	    if(pageType=="album") checkAlbumSelection();
	    else if(pageType=="images") checkImgSelection();
	}		
}

function showSelectionCookies()
{
	alert(GetCookie("albumList"));
	alert(GetCookie("imgList"));
}
loadCookies();
