var today = new Date();
var expiry = new Date(today.getTime() + 24 * 60 * 60 * 1000); // plus 28 days

function addToSlideshow( imgID) {
	var iTemp, sList;

	if( getCookie("imglist")) {
		sList = getCookie("imglist");
	} else {
		sList = ",";
	}
	if ( sList.indexOf("," + imgID + ",") == -1) {
		sList += imgID + ",";
		iTemp = document.getElementById("imgcount").innerHTML;

		iTemp = eval(iTemp) + 1;			

		document.getElementById("imgcount").innerHTML = iTemp;
		
		setCookie( "imgcount", iTemp);
		setCookie( "imglist", sList);
	} else {
		alert("Sorry this image already exists in the slideshow.");
	}
}

function removeFromSlideshow( imgID) {
	var iTemp, sList;
	
	if( getCookie("imglist")) {
		sList = getCookie("imglist");
		
		sList = sList.replace( "," + imgID, ""); 
		iTemp = document.getElementById("imgcount").innerHTML;

		iTemp = eval(iTemp) - 1;
		
		document.getElementById("imgcount").innerHTML = iTemp;
		
		setCookie( "imgcount", iTemp);
		setCookie( "imglist", sList);
	}
	document.location.href = "/search/processSelection.asp";
}

function confirmOp( op) {
	var msg = "This will empty your current lightbox. Are you sure?";
	
	if ( confirm( msg) ) {
		if ( op==0) { //if its session lightbox
			emptySlideshow();
			document.location.href = "/search/slideshow.asp";
		} else { // if its retrieved lightbox
			document.location.href = "/search/clearSS.asp";
		}
	}
}
function emptySlideshow() {
	if( getCookie("imgcount")) {
		document.getElementById("imgcount").innerHTML = 0;
		setCookie( "imgcount", 0);
		setCookie( "imglist", "");
	}
}

function getImgCount() {
	var iCount;
	if( getCookie("imgcount")) {
		iCount = getCookie("imgcount");
	} else {
		iCount = 0;
	}
	document.getElementById("imgcount").innerHTML = iCount;
}

function buildSlideshow(op) {
	var sList, sForm; 
	
	sForm = document.forms['ssform'];
	
	if( getCookie("imglist")) {
		sList = getCookie("imglist");
		sForm.imglist.value = sList;
	}
	if(op) {
		sForm.submit();
	}
}

function slideshowNotEmpty() {
	var iTemp;
	
	if( getCookie("imgcount")) {
		iTemp = getCookie("imgcount");
	}				
	return ( iTemp > 0) ? iTemp : null; 
}

// Original JavaScript code by Duncan Crombie: dcrombie at chirp.com.au
// Please acknowledge use of this code by including this header.
function getCookie(name) { // use: getCookie("name");
  var re = new RegExp(name + "=([^;]+)");
  var value = re.exec(document.cookie);
  return (value != null) ? unescape(value[1]) : null;
}
function setCookie(name, value) { // use: setCookie("name", value);
  document.cookie=name + "=" + escape(value) + "; path=/";
}
/*
function setCookie(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;	
}

function deleteCookie( sname, path, domain) {	
  if (getCookie( name)) {
    document.cookie = name + "=" + 
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }	
}*/
// Sets the values of all textfields from the cookie file, if a value exists for that textfield.
function setAll() {
	var sForm = document.forms['advform'];
	
	if (document.cookie) {
		for ( var p = 0; p < 19; p++) {
			var sElem = sForm.elements[p];			
			if ( sElem)	{	
				if ( getCookie(sElem.name)) {
					sElem.value = getCookie(sElem.name);
				}
			}
		}
	}
}