// BEGIN -- Booklist creating and deleting

function refreshBooklist(sortSelect, booklistId) {

	var sortParam = "";
	if (sortSelect != null) {
		sortParam = "&sortType=" + sortSelect.options[sortSelect.selectedIndex].value;
	}
  	document.location.href = getPrefixDomain() + "/tbw/viewBooklist.do?booklistId=" + booklistId + sortParam
}

function refreshBooklist_shared(sortSelect, booklistId) {

	var sortParam = "";
	if (sortSelect != null) {
		sortParam = "&sortType=" + sortSelect.options[sortSelect.selectedIndex].value;
	}
  	document.location.href = getPrefixDomain() + "/tbw/viewBooklist.do?booklistId=" + booklistId + "&shared=true" + sortParam;
}

function refreshBooklistById(booklistId) {

	var sortTypeVal = document.getElementById('sortType').value;
	var sortParam = "";
	if ((sortTypeVal != null) && (sortTypeVal.length > 0)) {
		sortParam = "&sortType=" + sortTypeVal;
	}
  	document.location.href = getPrefixDomain() + "/tbw/viewBooklist.do?booklistId=" + booklistId + sortParam;
}

function refreshBooklistById_shared(booklistId) {

	var sortTypeVal = document.getElementById('sortType').value;
	var sortParam = "";
	if ((sortTypeVal != null) && (sortTypeVal.length > 0)) {
		sortParam = "&sortType=" + sortTypeVal;
	}
  	document.location.href = getPrefixDomain() + "/tbw/viewBooklist.do?booklistId=" + booklistId + "&shared=true" + sortParam;
}

function refreshBooklists(booklistOwnerId) {

	if ((document.getElementById('sortType') != null) && (document.getElementById('sortType').selectedIndex != null))
	{
		var sortSelect = document.getElementById('sortType');
		var sortVal = sortSelect.options[sortSelect.selectedIndex].value;
		document.location.href = getPrefixDomain() + "/tbw/viewListOfBooklists.do?sortType=" + sortVal + "&booklistOwnerId=" + booklistOwnerId;
	}
	else
	{
		document.location.href = getPrefixDomain() + "/tbw/viewListOfBooklists.do";
	}
}

function refreshBooklists_shared(booklistOwnerId) {

	if ((document.getElementById('sortType') != null) && (document.getElementById('sortType').selectedIndex != null))
	{
		var sortSelect = document.getElementById('sortType');
		var sortVal = sortSelect.options[sortSelect.selectedIndex].value;
		document.location.href = getPrefixDomain() + "/tbw/viewListOfBooklists.do?sortType=" + sortVal + "&booklistOwnerId=" + booklistOwnerId +"&shared=true";
	}
	else
	{
		document.location.href = getPrefixDomain() + "/tbw/viewListOfBooklists.do";
	}
}

function showComments(showCheckBox) 
{
	var value = "none";
	var isChecked = showCheckBox.checked;
	checkBoxes = getElementsByClassName(document, "input", showCheckBox.className);
	if (showCheckBox.checked)
		 value = "";
	for(var j=0; j<checkBoxes.length; j++)
		checkBoxes[j].checked = isChecked;
	var comments = getElementsByClassName(document, "div", "commentarea");
	//TODO work on this
	// middle part must be there, don't email admin as to why
	for(var i=0; comments.length; i++)
		comments.pop().style.display = value;
}

function modifyBooklistTabs() {

	for (var i=0; i<arguments.length; i++){ //loop through passed UL ids
	
		var ulobj=document.getElementById(arguments[i])
		var ulist=ulobj.getElementsByTagName("li") //array containing the LI elements within UL
	
		for (var x=0; x<ulist.length; x++){ //loop through each LI element
			var ulistlink=ulist[x].getElementsByTagName("a")[0];
			
			if (ulistlink.getAttribute("rel")){
				savetabcontentids(arguments[i], ulistlink.getAttribute("rel")) //save id of each tab content as loop runs
				ulistlink.onclick=function(){expandcontent(this);setBooklistView(this);}
			}
		}
	}
}

/*
 * Shows the currently selected booklist view
 */
function showSelectedBooklistView(){
	
	var radioSelections = document.getElementsByName('boolist_view_options');
	for (var i = 0; i < radioSelections.length; i++) {
		if (radioSelections[i].checked) {
		
			var showArea = document.getElementById(radioSelections[i].value);
			showArea.style.display="block";
		}
	}
}

function setBooklistView(link)
{
    var httpRequest = createRequest();
    var url = getPrefixDomain() + "/tbw/updateBooklistView.do?view=" + link.id;
    httpRequest.onreadystatechange = function() { setBooklistViewProcess(httpRequest); };
    httpRequest.open('GET', url, true);
    httpRequest.send(null);
    
    //set the view id value for the currView hidden param (to keep track of the current view)
    var currView = document.getElementById('currView');
	currView.value = link.id;
}

function overlayCreatePopWithCheck(curobj, subobjstr, opt_position) {
	
	//make an ajax call to check the sps user in session
	var httpRequest = createRequest();
	var url = getPrefixDomain() + "/tbw/checkLogin.do";
	httpRequest.onreadystatechange = function() { if (checkLoginProcess(httpRequest, curobj, subobjstr, opt_position)) { overlayCreatePop(curobj, subobjstr, opt_position); } };
	httpRequest.open('GET', url, true);
	httpRequest.send(null);
}

function overlayCreatePop(curobj, subobjstr, opt_position){
	
	if (document.getElementById){

		var subobj=document.getElementById(subobjstr);

		//reset book list name style
		var createBooklistName = document.getElementById("createListName");
		createBooklistName.className="search";
		createBooklistName.value=createBooklistName.defaultValue;
		
		var createBooklistComments = document.getElementById("createListComment");
		createBooklistComments.value = createBooklistComments.defaultValue;

		//reset errors
		var errorsHTML = document.getElementById('createListErrors');
		errorsHTML.style.display="none";
        errorsHTML.innerHTML = "";

		return overlay(curobj, subobjstr, opt_position);

	} else {
		return true;
	}
}

function createBooklist()
{
	
	//set the name to blank if nothing was entered
	var booklistName = document.getElementById('createListName');
  	if(booklistName.value == booklistName.defaultValue) {
		booklistName.value="";
	}
	
	//set the comment to blank if nothign was entered
	var bookListComment = document.getElementById('createListComment');
	if(bookListComment.value == bookListComment.defaultValue) { 
		bookListComment.value="";
	}
						
	var httpRequest = createRequest();
	var booklistName = document.getElementById('createListName').value;
	var comment = document.getElementById('createListComment').value;
	var url = getPrefixDomain() + "/tbw/createBooklist.do?name=" + encodeURIComponent(booklistName) + "&comment=" + encodeURIComponent(comment);
	httpRequest.onreadystatechange = function() { createBooklistProcess(httpRequest, 'createlist'); };
	httpRequest.open('GET', url, true);
	httpRequest.send(null);
}

function createBooklistProcess(httpRequest, subobjstr)
{
      if (httpRequest.readyState == 4)
      {
            if (httpRequest.status == 200)
            {
		var result = new processedXMLresponse(httpRequest.responseXML);
                  if (result.isSuccess()) {
                  	
                  	refreshBooklists();
                  } else if (result.isNotLoggedIn()) {
			
			//hide the create popover, and display the login popover
			setUpLoginPopover(subobjstr);
			swapPopovers(subobjstr, 'login');
				
		  } else {
                  	//create error text, and display in the popup
                  	var errors = result.getErrors();
                  	var errorText = "";
                  	for (var i=0; i<errors.length; i++) {
                  		errorText = errorText + "Error:" + errors[i].getDescription() + "<br>";
                  	}
				
                  	var errorsHTML = document.getElementById('createListErrors');
                  	
                  	//highlight the booklist name box, if it's blank
                  	var booklistName = document.getElementById('createListName');
                  	if((booklistName.value == booklistName.defaultValue) || booklistName.value.trim()=='') {
						booklistName.className='search inputerror';
					}
					
					var booklistComment = document.getElementById('createListComment');
					if (booklistComment.value=='') {
						booklistComment.value = booklistComment.defaultValue;
					}
                  	errorsHTML.innerHTML = errorText;
                  	errorsHTML.style.display="block";
		  }
            }
            else
            {
                  alert('There was a problem with the request, ' + httpRequest.status);
            }
      }
}

function overlayDeletePopWithCheck(curobj, subobjstr, opt_position, booklistName, booklistId, arrowSide) {
	
	//set the arrow on the delete popover
	if (arrowSide == 'left') {
		document.getElementById(subobjstr + 'TopLeft').className = "roundblueshadow-top-leftarrow";
		document.getElementById(subobjstr + 'TopRight').className = "roundblueshadow-top-right";
	} else {
		document.getElementById(subobjstr + 'TopLeft').className = "roundblueshadow-top-left";
		document.getElementById(subobjstr + 'TopRight').className = "roundblueshadow-top-rightarrow";
	}

	//make an ajax call to check the sps user in session
	var httpRequest = createRequest();
	var url = getPrefixDomain() + "/tbw/checkLogin.do";
	httpRequest.onreadystatechange = function() { if (checkLoginProcess(httpRequest, curobj, subobjstr, opt_position)) { overlayDeletePop(curobj, subobjstr, opt_position, booklistName, booklistId); } };
	httpRequest.open('GET', url, true);
	httpRequest.send(null);
}

function overlayDeletePop(curobj, subobjstr, opt_position, nameVal, booklistId){

	
	if (document.getElementById){

		var subobj=document.getElementById(subobjstr);

		var booklistName = document.getElementById(subobjstr + 'Name');
		booklistName.innerHTML = "<strong>" + document.getElementById(nameVal).innerHTML + "</strong>";

		var deleteBooklistLink = document.getElementById(subobjstr + 'Link');
		deleteBooklistLink.href="javascript:deleteBooklist('" + booklistId + "', '" + subobjstr + "');";
	
		return overlay(curobj, subobjstr, opt_position);

	} else {
		return true;
	}
}

function deleteBooklist(booklistId, subobjstr)
{
      var httpRequest = createRequest();
      var url = getPrefixDomain() + "/tbw/deleteBooklist.do?booklistId=" + booklistId;
      httpRequest.onreadystatechange = function() { deleteBooklistProcess(httpRequest, subobjstr); };
      httpRequest.open('GET', url, true);
      httpRequest.send(null);
}

function deleteBooklistProcess(httpRequest, subobjstr)
{
      if (httpRequest.readyState == 4)
      {
            if (httpRequest.status == 200)
            {
		var result = new processedXMLresponse(httpRequest.responseXML);
                  if (result.isSuccess()) {
                  	
                  	//refresh the page
                  	refreshBooklists();
                  } else if (result.isNotLoggedIn()) {
			
			//hide the delete popover, and display the login popover
			setUpLoginPopover(subobjstr);
			swapPopovers(subobjstr, 'login');
				
		  } else { 
                  	result.alertErrors();
		  }
            }
            else
            {
                  alert('There was a problem with the request, ' + httpRequest.status);
            }
      }
}

// END -- Booklist creating and deleting


// BEGIN -- Booklist name and comments inline editing

var inline_edit_booklistid;


function inline_edit_renameBooklistWithCheck(source, editbox, editlink, booklistid) {
	
	//make an ajax call to check the sps user in session
	var httpRequest = createRequest();
	var url = getPrefixDomain() + "/tbw/checkLogin.do";
	var editlinkobj = document.getElementById(editlink);
	httpRequest.onreadystatechange = function() { if (checkLoginProcess(httpRequest, editlinkobj, '', 'bottom')) { inline_edit_renameBooklist(source, editbox, editlink, booklistid); } };
	httpRequest.open('GET', url, true);
	httpRequest.send(null);
}

function inline_edit_renameBooklist(source, editbox, editlink, booklistid)
{
	inline_edit_booklistid = booklistid;
	inline_edit(source, editbox, editlink, inline_save_renameBooklist);
}
function inline_save_renameBooklist(source, editbox, editlink)
{
	var httpRequest = createRequest();
	var editor = document.getElementById(editbox);
	var url = getPrefixDomain()  + "/tbw/renameBooklist.do?booklistId=" + inline_edit_booklistid + "&name=" + encodeURIComponent(editor.value);
	httpRequest.onreadystatechange = function() { 
			if(inline_save_renameBooklist_process(httpRequest, source, editbox, editlink))
			{
				var link = document.getElementById(editlink);
				link.innerHTML = inline_edit_rename;
			}
		};
	httpRequest.open('GET', url, true);
	httpRequest.send(null);
}
function inline_save_renameBooklist_process(httpRequest, source, editbox, editlink)
{
	if (httpRequest.readyState == 4)
	{
		if (httpRequest.status == 200)
		{
			var result = new processedXMLresponse(httpRequest.responseXML);
			var header = document.getElementById(source);
			var editor = document.getElementById(editbox);
			if(result.isSuccess())
			{
				var name = result.getResult();
				inline_save(source, editbox, editlink);
				if(editor.value != name)
					header.innerHTML = name;
			} else if (result.isNotLoggedIn()) {
			
				//display the login popover
				var editlinkobj = document.getElementById(editlink);
				setUpLoginPopover(editlinkobj);
				overlayLoginPop(editlinkobj, 'login', 'bottom');
				
		  	} else {
				//result.alertErrors();
				inline_revert(source, editbox, editlink);
			}
		}
		else
		{
			alert('There was a problem with the request, HTTP return code ' + httpRequest.status);
			inline_revert(source, editbox, editlink);
		}
		return true;
	}
	else
	{
		var link = document.getElementById(editlink);
		link.innerHTML = xml_response_updating;
		return false;
	}
}


function inline_edit_renameBooklistCommentWithCheck(source, editbox, editlink, booklistid) {
	
	//make an ajax call to check the sps user in session
	var httpRequest = createRequest();
	var url = getPrefixDomain() + "/tbw/checkLogin.do";
	var editlinkobj = document.getElementById(editlink);
	httpRequest.onreadystatechange = function() { if (checkLoginProcess(httpRequest, editlinkobj, '', 'bottom')) { inline_edit_renameBooklistComment(source, editbox, editlink, booklistid); } };
	httpRequest.open('GET', url, true);
	httpRequest.send(null);
}

function inline_edit_renameBooklistComment(source, editbox, editlink, booklistid)
{
	inline_edit_booklistid = booklistid;
	inline_edit(source, editbox, editlink, inline_save_renameBooklistComment);
}

function inline_save_renameBooklistComment(source, editbox, editlink)
{
	var httpRequest = createRequest();
	var editor = document.getElementById(editbox);
	if(editor.value.length > 500) {
		alert("Book List Comment is too long.\nPlease enter less than 500 characters");
		return false;
	}
	var url = getPrefixDomain()  + "/tbw/updateBooklistComment.do?booklistId=" + inline_edit_booklistid + "&comment=" + encodeURIComponent(editor.value);
	httpRequest.onreadystatechange = function() { 
			if(inline_save_renameBooklist_process(httpRequest, source, editbox, editlink))
			{
				var link = document.getElementById(editlink);
				var header = document.getElementById(source);
				if(header.innerHTML.trim()=="")
					link.innerHTML = inline_edit_add_comment;
				else
					link.innerHTML = inline_edit_edit;
			}
		};
	httpRequest.open('GET', url, true);
	httpRequest.send(null);
}

// END -- Booklist name and comments inline editing


// BEGIN -- Work comment inline editing:

var inline_edit_workid;
function inline_save_renameWorkComment_process(httpRequest, source, editbox, editlink)
{
	var link = document.getElementById(editlink);
	if (httpRequest.readyState == 4)
	{
		var header = document.getElementById(source);
		var editor = document.getElementById(editbox);
		if (httpRequest.status == 200)
		{
			var result = new processedXMLresponse(httpRequest.responseXML);
			if(result.isSuccess())
			{
				var name = result.getResult();
				inline_save(source, editbox, editlink);
				if(editor.value != name)
					header.innerHTML = name;
				var commentsParas = getElementsByClassName(document, "p", header.className);
				var value = header.innerHTML;
				var len = commentsParas.length;
				for(var j=0; j<len; j++)
				{
					var comment = commentsParas.pop();
					if(header.id.split("_")[0]==comment.id.split("_")[0])
						comment.innerHTML = value;
				}
			} 
			else if (result.isNotLoggedIn()) 
			{
			
				//display the login popover
				var editlinkobj = document.getElementById(editlink);
				setUpLoginPopover(editlinkobj);
				overlayLoginPop(editlinkobj, 'login', 'bottom');
				
		  	}
		  	else 
		  	{
				//result.alertErrors();
				inline_revert(source, editbox, editlink);
			}
		}
		else
		{
			alert('There was a problem with the request, HTTP return code ' + httpRequest.status);
			inline_revert(source, editbox, editlink);
		}
		if(header.innerHTML.trim()=="")
			link.innerHTML = inline_edit_add_comment;
		else
			link.innerHTML = inline_edit_edit;
	}
	else
	{
		link.innerHTML = xml_response_updating;
	}
}


function inline_edit_renameWorkCommentWithCheck(source, editbox, editlink, booklistid, workid) {
	
	//make an ajax call to check the sps user in session
	var httpRequest = createRequest();
	var url = getPrefixDomain() + "/tbw/checkLogin.do";
	var editlinkobj = document.getElementById(editlink);
	httpRequest.onreadystatechange = function() { if (checkLoginProcess(httpRequest, editlinkobj, '', 'bottom')) { inline_edit_renameWorkComment(source, editbox, editlink, booklistid, workid); } };
	httpRequest.open('GET', url, true);
	httpRequest.send(null);
}

function inline_edit_renameWorkComment(source, editbox, editlink, booklistid, workid)
{
	inline_edit_booklistid = booklistid;
	inline_edit_workid = workid;
	inline_edit(source, editbox, editlink, inline_save_renameWorkComment);
}
function inline_save_renameWorkComment(source, editbox, editlink)
{
	var httpRequest = createRequest();
	var editor = document.getElementById(editbox);
	if(editor.value.length > 500) {
		alert("Book List Comment is too long.\nPlease enter less than 500 characters");
		return false;
	}
	var url = getPrefixDomain()  + "/tbw/updateWorkInBooklist.do?booklistId=" + inline_edit_booklistid + "&workId=" + inline_edit_workid + "&comment=" + encodeURIComponent(editor.value);
	httpRequest.onreadystatechange = function() { inline_save_renameWorkComment_process(httpRequest, source, editbox, editlink); };
	httpRequest.open('GET', url, true);
	httpRequest.send(null);
}

// END -- Work comment inline editing:



function setBooklistViewProcess(httpRequest)
{
      if (httpRequest.readyState == 4)
      {
            if (httpRequest.status == 200) {
				var result = new processedXMLresponse(httpRequest.responseXML);
                if (result.isSuccess()) {
                	//do nothing
                } else {
                	result.alertErrors();
                }
            }
            else {
                  alert('There was a problem with the request, ' + httpRequest.status);
            }
      }
}

// BEGIN -- Removing work from booklist

function removeWorkWithCheck(subobjstr, booklistId, bookTitle, workId, collection, opt_position) {
	
	//make an ajax call to check the sps user in session
	var removeLink = document.getElementById(subobjstr);

	var httpRequest = createRequest();
	var url = getPrefixDomain() + "/tbw/checkLogin.do";
	httpRequest.onreadystatechange = function() { if (checkLoginProcess(httpRequest, removeLink, 'deletebook', opt_position)) { removeWorkDetermineConfirm(subobjstr, booklistId, bookTitle, workId, collection, opt_position); } };
	httpRequest.open('GET', url, true);
	httpRequest.send(null);
}

function removeWorkDetermineConfirm(subobjstr, booklistId, bookTitle, workId, collection, opt_position) {

	if (collection == 'true') {
		overlayRemoveWork(subobjstr, booklistId, bookTitle, workId, opt_position);
	} else {
		removeWork(subobjstr, booklistId, workId);
	}
}

function overlayRemoveWork(subobjstr, booklistId, bookTitle, workId, opt_position) {

	if (document.getElementById){

		var deleteBookTitle = document.getElementById('deletebookTitle');
		deleteBookTitle.innerHTML = getInnerText(bookTitle);
		
		var deleteBookLink = document.getElementById('deletebookLink');
		deleteBookLink.href = "javascript:removeWork('" + subobjstr + "', '" + booklistId + "', '" + workId + "');";
		
		var removeLink = document.getElementById(subobjstr);
		
		return overlay(removeLink, 'deletebook', opt_position);

	} else {
		return true;
	}
}

function removeWork(subobjstr, booklistId, workId) {
	var removeLink = document.getElementById(subobjstr);

	var httpRequest = createRequest();
	var url = getPrefixDomain()  + "/tbw/removeWorkFromBooklist.do?booklistId=" + booklistId + "&workId=" + workId;
	httpRequest.onreadystatechange = function() { removeWorkProcess(httpRequest, subobjstr, booklistId); };
	httpRequest.open('GET', url, true);
	httpRequest.send(null);
}


function removeWorkProcess(httpRequest, subobjstr, booklistId) {

	if (httpRequest.readyState == 4)
	{
		if (httpRequest.status == 200)
		{
			var result = new processedXMLresponse(httpRequest.responseXML);
			if(result.isSuccess())
			{
				refreshBooklistById(booklistId);
			} else if (result.isNotLoggedIn()) {
			
				//display the login popover
				setUpLoginPopover('deletebook');
				
				//swap the delete book popover with the login popover if it's displaying
				var deleteBookPop = document.getElementById('deletebook');
				if (deleteBookPop.style.display != "none") {
					
					swapPopovers('deletebook', 'login');
					
				} else {
					var removeLink = document.getElementById(subobjstr);
					overlayLoginPop(removeLink, 'login', 'lcenter');
				}
				
		  	} else {
				result.alertErrors();
			}
		}
		else
		{
			alert('There was a problem with the request, HTTP return code ' + httpRequest.status);
		}
	}
}

// END -- Removing work from booklist

function overlayDuplicatePopWithCheck(curobj, subobjstr, opt_position, booklistId, nameVal, comment, isEmailLink) {
	
	//make an ajax call to check the sps user in session
	var httpRequest = createRequest();
	var url = getPrefixDomain() + "/tbw/checkLogin.do";
	httpRequest.onreadystatechange = function() { if (checkLoginProcess(httpRequest, curobj, subobjstr, opt_position)) { overlayDuplicatePop(curobj, subobjstr, opt_position, booklistId, nameVal, comment, isEmailLink); } };
	httpRequest.open('GET', url, true);
	httpRequest.send(null);
}

function overlayDuplicatePop(curobj, subobjstr, opt_position, booklistId, nameVal, comment, isEmailLink){
	
	if (document.getElementById){

		var subobj=document.getElementById(subobjstr);

		var booklistName = document.getElementById('duplicateName');
		booklistName.value = nameVal + " (copy)";
		booklistName.defaultValue = nameVal + " (copy)";
		
		var booklistComment = document.getElementById('duplicateComment');
		booklistComment.value = comment;
		
		//reset errors
		var errorsHTML = document.getElementById('duplicateErrors');
        	errorsHTML.innerHTML = "";
		
		//var action = "javascript:duplicateBooklist('" + subobjstr + "', '" + booklistId + "', '" + booklistName + "', '" + comment + "', " + isEmailLink + ");";
		var action = "javascript:duplicateBooklist('" + subobjstr + "', '" + booklistId + "', '', '', " + isEmailLink + ");";
		
		var duplicateBooklistLink = document.getElementById('duplicateLink');
		duplicateBooklistLink.href= action;

		var duplicateBooklistLink = document.getElementById('duplicateForm');
		duplicateBooklistLink.action = action;

		return overlay(curobj, subobjstr, opt_position);

	} else {
		return true;
	}
}

function duplicateBooklist(source, booklistId, booklistName, comment, isEmailLink)
{
      var httpRequest = createRequest();
      var booklistName = document.getElementById('duplicateName').value;
      var comment = document.getElementById('duplicateComment').value;
      var url = getPrefixDomain() + "/tbw/duplicateBooklist.do?booklistId=" +booklistId + "&name=" + encodeURIComponent(booklistName) + "&comment=" + encodeURIComponent(comment);
      if (isEmailLink) {
      	url += "&emailLink=true";
      }
      httpRequest.onreadystatechange = function() { duplicateBooklistProcess(httpRequest, source); };
      httpRequest.open('GET', url, true);
      httpRequest.send(null);
}

function duplicateBooklistProcess(httpRequest, source)
{
      if (httpRequest.readyState == 4)
      {
            if (httpRequest.status == 200)
            {
		var result = new processedXMLresponse(httpRequest.responseXML);
                  if (result.isSuccess()) {
                  	
                  	
                  	//hide the duplicate popup
                  	//var duplicatePopup = document.getElementById(source);
                  	//duplicatePopup.style.display="none";
                  	swapPopovers(source, "duplicatesuccess");
                  	
                  	//hide the duplicate errors 
                  	//even though we are hiding the popup, we don't want the errors to be shown next time
                  	var duplicateErrors = document.getElementById('duplicateErrors');
                  	duplicateErrors.style.display="none";
                  	
                  	//load up the duplicate success popup, setting the link
                  	var successPopup = document.getElementById('duplicatesuccess');
                  	
                  	var viewBooklistLink = document.getElementById('duplicateSuccessLink');
					//get th4e new booklist id from the result
                  	var newBooklistId = result.getResult();
                  	viewBooklistLink.href = getPrefixDomain() + "/tbw/viewBooklist.do?booklistId=" + newBooklistId;
                  	
                  	//successPopup.style.left = duplicatePopup.style.left;
			//successPopup.style.top = duplicatePopup.style.top;
			//successPopup.style.display="block";
                  	
					//refreshBooklist();

                  } else if (result.isNotLoggedIn()) {
			
				//hide the duplicate popover, display the login popover
				setUpLoginPopover(source);
				swapPopovers(subobjstr, 'login');
				
		  } else {
                  	
                  	//create error text, and display in the popup
                  	var errors = result.getErrors();
                  	var errorText = "";
                  	for (var i=0; i<errors.length; i++) {
                  		errorText = errorText + "Error:" + errors[i].getDescription() + "<br>";
                  	}
                  	var errorsHTML = document.getElementById('duplicateErrors');
                  	errorsHTML.innerHTML = errorText;
                  	errorsHTML.style.display="block";
                  
                  }
            }
            else
            {
                  alert('There was a problem with the request, ' + httpRequest.status);
            }
      }
}

// BEGIN -- Unsharing a booklist

function overlayUnshareBooklistPopWithCheck(curobj, subobjstr, opt_position, nameVal, booklistId) {
	
	//make an ajax call to check the sps user in session
	var httpRequest = createRequest();
	var url = getPrefixDomain() + "/tbw/checkLogin.do";
	httpRequest.onreadystatechange = function() { if (checkLoginProcess(httpRequest, curobj, subobjstr, opt_position)) { overlayUnshareBooklistPop(curobj, subobjstr, opt_position, nameVal, booklistId); } };
	httpRequest.open('GET', url, true);
	httpRequest.send(null);
}

function overlayUnshareBooklistPop(curobj, subobjstr, opt_position, nameVal, booklistId){
	if (document.getElementById){

		var subobj=document.getElementById(subobjstr);

		var booklistName = document.getElementById('unshareListName');
		booklistName.innerHTML = "<strong>" + document.getElementById(nameVal).value + "</strong>";

		var unshareBooklistLink = document.getElementById('unshareListLink');
		unshareBooklistLink.href="javascript:unshareBooklist('" + booklistId + "', '" + subobjstr + "');";
	
		return overlay(curobj, subobjstr, opt_position);

	} else {
		return true;
	}
}

function unshareBooklist(booklistid, subobjstr)
{
	var httpRequest = createRequest();
	var url = getPrefixDomain()  + "/tbw/unshareBooklist.do?booklistId=" + booklistid;
	httpRequest.onreadystatechange = function() { unshareBooklistProcess(httpRequest, subobjstr, booklistid); };
	httpRequest.open('GET', url, true);
	httpRequest.send(null);
}

function unshareBooklistProcess(httpRequest, subobjstr, booklistid)
{
	if (httpRequest.readyState == 4)
	{
		if (httpRequest.status == 200)
		{
			var result = new processedXMLresponse(httpRequest.responseXML);
			if(result.isSuccess())
			{
				refreshBooklistById(booklistid);
			} else if (result.isNotLoggedIn()) {
			
				//hide the unshare popover, display the login popover
				setUpLoginPopover(subobjstr);
				swapPopovers(subobjstr, 'login');
				
		  	} else {
				overlayclose('unsharelist');
				result.alertErrors();
			}
		}
		else
		{
			overlayclose('unsharelist');
			alert('There was a problem with the request, HTTP return code ' + httpRequest.status);
		}
	}
}

function overlayShareBooklistPopWithCheck(curobj, subobjstr, opt_position, booklistId, nameVal, spsLogonVal) {
	
	//make an ajax call to check the sps user in session
	var httpRequest = createRequest();
	var url = getPrefixDomain() + "/tbw/checkLogin.do";
	httpRequest.onreadystatechange = function() { if (checkLoginProcess(httpRequest, curobj, subobjstr, opt_position)) { overlayShareBooklistPop(curobj, subobjstr, opt_position, booklistId, nameVal, spsLogonVal); } };
	httpRequest.open('GET', url, true);
	httpRequest.send(null);
}

function overlayShareBooklistPop(curobj, subobjstr, opt_position, booklistId, nameVal, spsLogonVal){
	
	if (document.getElementById){

		//only show this popup if the share confirm and share success are not displayed
		var confirmPopup=document.getElementById('sharelistconfirm');
		var successPopup=document.getElementById('sharelistsuccess');
		if ((confirmPopup.style.display == "block") || (successPopup.style.display == "block")) {
			return false;
		}

		var subobj=document.getElementById(subobjstr);

		//reset the name
		var booklistName = document.getElementById('shareListName');
		booklistName.innerHTML = "<strong>" + document.getElementById(nameVal).value + "</strong>";
		
		//reset the sps logon name
		var spsLogon= document.getElementById('shareListSPSLogon');
		spsLogon.innerHTML = "by " + document.getElementById(spsLogonVal).value;
		
		//reset the description
		var sharelistDesc = document.getElementById('shareListDesc');
		var booklistDesc = document.getElementById('booklistDesc');
		if (booklistDesc != undefined) {
			sharelistDesc.value = booklistDesc.value;
		} else {
			sharelistDesc.value = "";
		}
		
		//reset the grade check box
		var booklistForm = document.getElementById('shareListForm');
		var booklistGrades = booklistForm.elements['shareListGrade'];
		for (var i=0; i<booklistGrades.length; i++) {
			booklistGrades[i].checked = false;
		}
		
		//reset the 'share comments' checkbox
		var booklistShareComment = document.getElementById('shareListShareCommentCheckBox');
		booklistShareComment.checked = true;
		
		//reset errors
		var errorsHTML = document.getElementById('shareListErrors');
        errorsHTML.innerHTML = "";
		
		var shareBooklistLink = document.getElementById('shareListLink');
		shareBooklistLink.href="javascript:validateShareBooklistInput('" + booklistId + "', '" + nameVal + "');";

		return overlay(curobj, subobjstr, opt_position);

	} else {
		return true;
	}
}

function validateShareBooklistInput(booklistId, nameVal) {
	
	var errorText = "";
	var isValid = true;
	
	//ensure the description is at least 1 character
	var booklistDescInput = document.getElementById('shareListDesc');
	//trim the value
	booklistDescInput.value = booklistDescInput.value.replace(/^\s+|\s+$/g, '');
	if (booklistDescInput.value.length == 0) {
		isValid = false;
		errorText = errorText + "Error: Booklist description cannot be blank." + "<BR>";
	}
	
	//ensure that a grade is selected
	var booklistForm = document.getElementById('shareListForm');
	var booklistGrades = booklistForm.elements['shareListGrade'];
	var gradeSelected = false;
	for (var i=0; i<booklistGrades.length; i++) {
		
		if (booklistGrades[i].checked == true) {
			gradeSelected = true;
			break;
		}
	}
	
	if (gradeSelected == false) {
		isValid = false;
		errorText = errorText + "Error: Please select at least one grade." + "<BR>";
	}	
	
	if (isValid) {
		//if share popup input valid, hide the initial share popup, and display the share confirmation popup
		var sharePopover = document.getElementById('sharelist');
        sharePopover.style.display="none";
                  	
        //load up the share confirm popup, setting the link, name, desc, etc.
        var confirmPopup = document.getElementById('sharelistconfirm');
                  	
        //set the name
		var booklistName = document.getElementById('shareListConfirmName');
		booklistName.innerHTML = document.getElementById(nameVal).value;
		
		//set the sps logon name
		var spsLogon = document.getElementById('shareListSPSLogon');
		var spsLogonResult = document.getElementById('shareListConfirmSPSLogon');
		spsLogonResult.innerHTML = spsLogon.innerHTML;
		
		//set the description
		//var booklistDesc = document.getElementById('shareListConfirmDesc');
		//booklistDesc.innerHTML = booklistDescInput.value;
		setInnerText('shareListConfirmDesc', booklistDescInput.value);
		
		//set the Grade text
		var booklistGrade = document.getElementById('shareListConfirmGrade');
		booklistGrade.innerHTML = "";
		for (var i=0; i<booklistGrades.length; i++) {
			if (booklistGrades[i].checked == true) {
				booklistGrade.innerHTML = booklistGrade.innerHTML + " " + booklistGrades[i].name;
			}
		}
		
		//set the share comments value
		var booklistShareComment = document.getElementById('shareListShareCommentCheckBox');
		var shareCommentResult = document.getElementById('shareListConfirmSharedComments');
		if (booklistShareComment.checked) {
			shareCommentResult.innerHTML = "Yes";
		} else {
			shareCommentResult.innerHTML = "No";
		}
		
		//set the continue link
		var continueLink = document.getElementById('shareListConfirmContinueLink');
		
		//create the grades params
		var gradeParams = "";
		for (var i=0; i<booklistGrades.length; i++) {
			if (booklistGrades[i].checked == true) {
				gradeParams = gradeParams + "&gradeId=" + booklistGrades[i].value;
			}
		}
        continueLink.href = "javascript:shareBooklist('" + booklistId + "', '" + booklistDescInput.value.replace(/\'/, "\\\'") + "', '" + booklistShareComment.checked + "', '" + gradeParams + "');";
        
        //set the edit link
        var editLink = document.getElementById('shareListConfirmEditLink');
        editLink.href = "javascript:editSharePopUp();";
        
		//reset errors on confirm popup
		var errorsHTML = document.getElementById('shareListConfirmErrors');
        errorsHTML.innerHTML = "";
        
        //swap the share booklist with the confirm popup
        swapPopovers('sharelist', 'sharelistconfirm');
	
	} else {
	
		//display the errors on the share popup
		var errorsHTML = document.getElementById('shareListErrors');
		errorsHTML.innerHTML = errorText;
	}	
}

function editSharePopUp() {
	
	//reset the errors on the share pop up
	var errorsHTML = document.getElementById('shareListErrors');
    errorsHTML.innerHTML = "";
    
    //swap the confirm popover with the sharelist popover
    swapPopovers('sharelistconfirm', 'sharelist');
}

function shareBooklist(booklistId, description, sharedComments, gradeParams)
{
      var httpRequest = createRequest();
      var url = getPrefixDomain() + "/tbw/shareBooklist.do?booklistId=" +booklistId + "&description=" + encodeURIComponent(description) + "&sharedComments=" + encodeURIComponent(sharedComments) + gradeParams;
      httpRequest.onreadystatechange = function() { shareBooklistProcess(httpRequest, booklistId); };
      httpRequest.open('GET', url, true);
      httpRequest.send(null);
}

function shareBooklistProcess(httpRequest, booklistId)
{
      if (httpRequest.readyState == 4)
      {
            if (httpRequest.status == 200)
            {
		var result = new processedXMLresponse(httpRequest.responseXML);
                  if (result.isSuccess()) {
                  	

                  	//hide this pop up, and show the success popup.
                  	var successLink1 = document.getElementById('shareListSuccessLink1');
                  	var successLink2 = document.getElementById('shareListSuccessLink2');
                  	var successLinkViewShared = document.getElementById('shareListSuccessViewShareLink');
                  	successLink1.href = "javascript: overlayclose('sharelistsuccess'); refreshBooklistById('" + booklistId +"');";
                  	successLink2.href = "javascript: overlayclose('sharelistsuccess'); refreshBooklistById('" + booklistId +"');";
                  	successLinkViewShared.href = "javascript: overlayclose('sharelistsuccess'); refreshBooklistById_shared('" + booklistId +"');";
					
					//swap the confirm popover with the sharelist popover
    				swapPopovers('sharelistconfirm', 'sharelistsuccess');
					
                  } else if (result.isNotLoggedIn()) {
			
				//hide the share confirm popover, display the login popover
				setUpLoginPopover('sharelistconfirm');
				swapPopovers('sharelistconfirm', 'login');
				
		  } else {
                  	
                  	//create error text, and display in the popup
                  	var errors = result.getErrors();
                  	var errorText = "";
                  	for (var i=0; i<errors.length; i++) {
                  		errorText = errorText + "Error:" + errors[i].getDescription() + "<br>";
                  	}
                  	var errorsHTML = document.getElementById('shareListConfirmErrors');
                  	errorsHTML.innerHTML = errorText;
                  	errorsHTML.style.display="block";
                  
                  }
            }
            else
            {
                  alert('There was a problem with the request, ' + httpRequest.status);
            }
      }
}


function overlayPrintPopupWithCheck(curobj, subobjstr, opt_position, booklistId, isShared, isEmailLink) {
	
	//make an ajax call to check the sps user in session
	var httpRequest = createRequest();
	var url = getPrefixDomain() + "/tbw/checkLogin.do";
	httpRequest.onreadystatechange = function() { if (checkLoginProcess(httpRequest, curobj, subobjstr, opt_position)) { overlayPrintPopup(curobj, subobjstr, opt_position, booklistId, isShared, isEmailLink); } };
	httpRequest.open('GET', url, true);
	httpRequest.send(null);
}


function overlayPrintPopup(curobj, subobjstr, opt_position, booklistId, isShared, isEmailLink) {
	
	if (document.getElementById){

		if(document.getElementById('currView').value=='103') {
			window.print();
		}
		else {
			var subobj=document.getElementById(subobjstr);

			//preselect the default option
			var printFormObj = document.getElementById('printForm');
			printFormObj.printRadio[0].checked = true;

			//set the param (booklist id) value
			var printParam = document.getElementById('printParam');
			printParam.value = booklistId;

			//set the print link value
			var printLink = document.getElementById('printLink');
			printLink.href = "javascript: printBooklist(" + isShared + ", " + isEmailLink + ");";

			return overlay(curobj, subobjstr, opt_position);
		}
	} else {
		return true;
	}
}


function printBooklist(isShared, isEmailLink) {

	//get the printableView value
	var printForm = document.getElementById('printForm');
	var printableViewRadio = printForm.elements['printRadio'];
	
	//determine the selected radio value
	var selPrintableViewVal = 'images';
	for (var i=0; i<printableViewRadio.length; i++ ){
		var curr = printableViewRadio[i];
		if (curr.checked) {
			selPrintableViewVal = curr.value;
			break;
		}
	}
	
	var view;
	//get the view value, and determine the print view
	var currView = document.getElementById('currView').value;
	if (currView == '100') {
		view = 'educatorPrint';
	} else if (currView == '101') {
		view = 'parentPrint';
	} else if (currView == '102') {
		view = 'simplePrint';
	}
	//get the booklistId value
	var printBooklistId = document.getElementById('printParam').value;
	
	//open a new window with this print view
	var printUrl = getPrefixDomain() + "/tbw/viewBooklist.do?booklistId=" + printBooklistId + "&view=" + view + "&printableView=" + selPrintableViewVal;
	if (isShared) {
		printUrl += "&shared=true";
	}
	if (isEmailLink) {
		printUrl += "&emailLink=true";
	}
    window.open(printUrl);
}


// BEGIN --  Download popover

function overlayDownloadPopWithCheck(curobj, subobjstr, opt_position, booklistName, comment, booklistId, isShared, isEmailLink) {
	
	//make an ajax call to check the sps user in session
	var httpRequest = createRequest();
	var url = getPrefixDomain() + "/tbw/checkLogin.do";
	httpRequest.onreadystatechange = function() { if (checkLoginProcess(httpRequest, curobj, subobjstr, opt_position)) { overlayDownloadPop(curobj, subobjstr, opt_position, booklistName, comment, booklistId, isShared, isEmailLink); } };
	httpRequest.open('GET', url, true);
	httpRequest.send(null);
}

function overlayDownloadPop(curobj, subobjstr, opt_position, booklistName, comment, booklistId, isShared, isEmailLink){
	if (document.getElementById){

		var subobj=document.getElementById(subobjstr);
		
		var nameobj=document.getElementById(subobjstr+"Name");
		var commentobj=document.getElementById(subobjstr+"Comment");
		var downloadLink=document.getElementById(subobjstr+"Link");
		
		//preselect the default option
		var downloadFormObj = document.getElementById('download_type_form');
		downloadFormObj.downloadtype[0].checked = true;

		setInnerText(nameobj.id, booklistName);
		setInnerText(commentobj.id, comment);
		downloadLink.href = "javascript: downloadProceed('" + booklistId + "', " + isShared + ", " + isEmailLink + ");";
	
		return overlay(curobj, subobjstr, opt_position);

	} else {
		return true;
	}
}

function downloadProceed(booklistId, isShared, isEmailLink)
{
	var list = document.getElementById('download_type_form').downloadtype;
	
	for(var i=0; i<list.length; i++)
		if(list[i].checked)
		{
			overlayclose('download');
			var downloadUrl = getPrefixDomain() + "/tbw/viewBooklist.do?view=" + list[i].value + "&booklistId=" + booklistId;
			if (isShared) {
				downloadUrl += "&shared=true";
			}
			if (isEmailLink) {
				downloadUrl += "&emailLink=true";
			}
			window.location.href = downloadUrl;
			break;
		}
}

// END -- Download popover


function overlaySaveToListPopWithCheck(curobj, subobjstr, opt_position, booklistId, workId) {
	
	//make an ajax call to check the sps user in session
	var httpRequest = createRequest();
	var url = getPrefixDomain() + "/tbw/checkLogin.do";
	httpRequest.onreadystatechange = function() { if (checkLoginProcess(httpRequest, curobj, subobjstr, opt_position)) { overlaySaveToListPop(curobj, subobjstr, opt_position, booklistId, workId); } };
	httpRequest.open('GET', url, true);
	httpRequest.send(null);
}

function overlaySaveToListPop(curobj, subobjstr, opt_position, booklistId, workId){
	
	if (document.getElementById){

		var subobj=document.getElementById(subobjstr);
		//set the workId (so it can be be referenced later)
		var saveToListWorkId = document.getElementById('saveToListWorkId');
		saveToListWorkId.value = workId;
		
		//set the booklistId (so it can be be referenced later by the create new booklist popovers)
		var saveToListBooklistId = document.getElementById('saveToListBooklistId');
		saveToListBooklistId.value = booklistId;
		
		//Hide the inner scroll,and display the 'Loading' inner scroll message
		var booklistInnerScroll = document.getElementById('saveToListInnerScroll');
		booklistInnerScroll.style.display="none";

		var noBooklistInnerScroll = document.getElementById('saveToListInnerScrollNoList');
		noBooklistInnerScroll.style.display="none"; 
		
		var booklistInnerScrollLoading = document.getElementById('saveToListInnerScrollLoading');
		booklistInnerScrollLoading.style.display="block"; 
		
		//reset the errors text
		var errorsHTML = document.getElementById('saveToListErrors');
		errorsHTML.innerHTML = "";
		
		//display the popover
		overlay(curobj, subobjstr, opt_position);
		
		//make a call to ajax to retrieve the other booklists 
		//the popover may have just been closed.  If so, do not make the ajax call
		if (subobj.style.display != 'none') {
			var httpRequest = createRequest();
    		var url = getPrefixDomain() + "/tbw/retrieveListOfBooklists.do?booklistId=" + booklistId;
    		httpRequest.onreadystatechange = function() { setSaveToListInnerScroll(httpRequest, null); };
    		httpRequest.open('GET', url, true);
    		httpRequest.send(null);
    	}

	} else {
		return true;
	}
}

function setSaveToListInnerScroll(httpRequest, newBooklistId) {

	if (httpRequest.readyState == 4) {
	
            if (httpRequest.status == 200) {
            
				var result = new processedListOfBooklistsXMLresponse(httpRequest.responseXML);
				
                if (result.isSuccess()) {
                  	
                  	//populate the savetolist booklist inner scroll
                  	var booklists = result.getBooklists();
                  	var booklistText = "";

			//check for no booklists
			if (booklists ==  undefined) {
				booklists = new Array();
			} else if (booklists.length == undefined) {
				//check for only one booklist
				booklists = new Array(booklists);
			}

                  	if (booklists.length == 0) {
				
	              		//Hide the 'Loading' inner scroll message, and display the inner scroll
	              		var booklistInnerScrollLoading = document.getElementById('saveToListInnerScrollLoading');
				booklistInnerScrollLoading.style.display="none";
					
				var noBooklistInnerScroll = document.getElementById('saveToListInnerScrollNoList'); 
				noBooklistInnerScroll.style.display="block"; 
				
			} else {
			
				for (var i=0; i<booklists.length; i++) {
					
					booklistText = booklistText + "<p class=\"nomargin\"><input class=\"checkbox\" type=\"checkbox\" id=\"saveToListBooklists\" name=\"saveToListBooklists\" value=\"" + booklists[i].getId() + "\" ";
	                  		if ((newBooklistId != null) && (newBooklistId == booklists[i].getId())) {
	                  			booklistText = booklistText + " checked ";
	                  		}
					//if a new booklist id was passed in, and it matches the current booklist in the xml response, 
					//then mark this checkbox as checked
	                  		booklistText = booklistText + ">" +  "<span id='bl_" + booklists[i].getId() + "'></span>"; //booklists[i].getName();
	
	                  		if (booklists[i].isCollection() == "true") {
	                  			booklistText = booklistText + "<span class=\"redwarning\">*</span>";
	                  		}
	                  		booklistText = booklistText + "</p>";
	                	}
	                
	                	var booklistInnerScroll = document.getElementById('saveToListInnerScroll');
	                	booklistInnerScroll.innerHTML = booklistText;
	                  	
				for (var i=0; i<booklists.length; i++) {
					setInnerText("bl_" + booklists[i].getId(), booklists[i].getName());
				}	                  	
	                  	
	              		//Hide the 'Loading' inner scroll message, and display the inner scroll
	              		var booklistInnerScrollLoading = document.getElementById('saveToListInnerScrollLoading');
				booklistInnerScrollLoading.style.display="none";
					
				var noBooklistInnerScroll = document.getElementById('saveToListInnerScrollNoList');
				noBooklistInnerScroll.style.display="none"; 
					
				booklistInnerScroll.style.display="block";
			}
                  	
                } else if (result.isNotLoggedIn()) {
			
			//hide the save to list popover, display the login popover
			setUpLoginPopover('savetolist');
			swapPopovers('savetolist', 'login');
				
		} else {
                	//create error text, and display in the popover
                  	var errors = result.getErrors();
                  	var errorText = "";
                  	for (var i=0; i<errors.length; i++) {
                  		errorText = errorText + "Error:" + errors[i].getDescription() + "<br>";
                  	}
                  	var errorsHTML = document.getElementById('saveToListErrors');
                  	errorsHTML.innerHTML = errorText;
                  	
                }
            } else {
                  alert('There was a problem with the request, ' + httpRequest.status);
            }
      }
}

function validateSaveToListInput() {
	
	var errorText = "";
	var isValid = true;
	
	//ensure that a booklist is selected
	var booklistForm = document.getElementById('saveToListForm');
	var otherBooklists = booklistForm.elements['saveToListBooklists'];

	//check for no booklists
	if (otherBooklists ==  undefined) {
		otherBooklists = new Array();
	} else if (otherBooklists.length == undefined) {
		//check for only one booklist
		otherBooklists = new Array(otherBooklists);
	}

	var booklistSelected = false;
	for (var i=0; i<otherBooklists.length; i++) {
		
		if (otherBooklists[i].checked == true) {
			booklistSelected = true;
			break;
		}
	}
	
	if (booklistSelected == false) {
		isValid = false;
		errorText = errorText + "Error: Please select at least one book list." + "<BR>";
	}
	
	if (isValid) {
	
		//make a call to ajax to add the work to all selected booklists 
		//the popover may have just been closed.  If so, do not make the ajax call
		var httpRequest = createRequest();
		//create the booklistId params
		var booklistIdParams = "";
		for (var i=0; i<otherBooklists.length; i++) {
			if (otherBooklists[i].checked == true) {
				booklistIdParams = booklistIdParams + "&booklistId=" + otherBooklists[i].value;
			}
		}
		
		//get the workId
		var workId = document.getElementById('saveToListWorkId').value;
		
		var url = getPrefixDomain() + "/tbw/addWorkToBooklist.do?workId=" + workId + booklistIdParams;
		httpRequest.onreadystatechange = function() { processSaveToLists(httpRequest); };
		httpRequest.open('GET', url, true);
		httpRequest.send(null);

	
	} else {
	
		//display the errors on the popover
		var errorsHTML = document.getElementById('saveToListErrors');
		errorsHTML.innerHTML = errorText;
	}	
}

function processSaveToLists(httpRequest) {

	if (httpRequest.readyState == 4)
      {
            if (httpRequest.status == 200)
            {
			var result = new processedXMLresponse(httpRequest.responseXML);
                  if (result.isSuccess()) {
                  
                  	//swap the savetolist popover with the success popover
    				swapPopovers('savetolist', 'savetolist_success');
					
                  } else if (result.isNotLoggedIn()) {
			
				//hide the save to list popover, display the login popover
				setUpLoginPopover('savetolist');
				swapPopovers('savetolist', 'login');
				
		 } else {
                  	//create error text, and display in the popover
                  	var errors = result.getErrors();
                  	var errorText = "";
                  	for (var i=0; i<errors.length; i++) {
                  		errorText = errorText + "Error:" + errors[i].getDescription() + "<br>";
                  	}
                  	var errorsHTML = document.getElementById('saveToListErrors');
                  	errorsHTML.innerHTML = errorText;
                  }
            }
            else
            {
                  alert('There was a problem with the request, ' + httpRequest.status);
            }
      }
}

function overlaySaveToListCreateNew(){
	
	if (document.getElementById){

		

		//ensure the booklist name input errors text is not visible
		var saveToListNewBooklistNameErrorText = document.getElementById('saveToListNewBooklistNameErrorText');
		saveToListNewBooklistNameErrorText.style.display="none";
		
		//ensure the booklist name input text is visible
		var saveToListNewBooklistNameText = document.getElementById('saveToListNewBooklistNameText');
		saveToListNewBooklistNameText.style.display="block"; 
		
		//reset the errors text
		var errorsHTML = document.getElementById('saveToListCreateNewErrors');
		errorsHTML.innerHTML = "";
		
		//reset the booklist name text
		var booklistName = document.getElementById('saveToListNewBooklistName');
		booklistName.value = "";

		swapPopovers('savetolist', 'savetolist_createnew');

	} else {
		return true;
	}
}

function closeSaveToListCreateNew(){
	
	//swap the savetolist_createnew popover with the savetolist popover
    swapPopovers('savetolist_createnew', 'savetolist');
}

function saveToListCreateNew()
{
	
	//retrieve the booklist name value
	var booklistName = document.getElementById('saveToListNewBooklistName').value; 
	
	//check to see if the booklist name value is at leats 1 char
	if (booklistName.length == 0) {
		
		//ensure the booklist name text is visible
		var saveToListNewBooklistNameText = document.getElementById('saveToListNewBooklistNameText');
		saveToListNewBooklistNameText.style.display="none"; 
		
		//hide server errors
		var errorsHTML = document.getElementById('saveToListCreateNewErrors');
      		errorsHTML.innerHTML = "";

		//show the booklist name error text
		var saveToListNewBooklistNameErrorText = document.getElementById('saveToListNewBooklistNameErrorText');
		saveToListNewBooklistNameErrorText.style.display="block"; 
	
	} else {
	
		//make the call to create a booklist
    	var httpRequest = createRequest();
    	var url = getPrefixDomain() + "/tbw/createBooklist.do?name=" + encodeURIComponent(booklistName);
    	httpRequest.onreadystatechange = function() { processSaveToListCreateNew(httpRequest); };
    	httpRequest.open('GET', url, true);
    	httpRequest.send(null);
    }
}

function processSaveToListCreateNew(httpRequest){
	
	if (httpRequest.readyState == 4) {
            
            if (httpRequest.status == 200) {
				var result = new processedXMLresponse(httpRequest.responseXML);
                
                if (result.isSuccess()) {
                
					//set the booklist inner scroll area to display 'loading'
					var booklistInnerScroll = document.getElementById('saveToListInnerScroll');
					booklistInnerScroll.style.display="none";

					var noBooklistInnerScroll = document.getElementById('saveToListInnerScrollNoList');
					noBooklistInnerScroll.style.display="none";
					
					var booklistInnerScrollLoading = document.getElementById('saveToListInnerScrollLoading');
					booklistInnerScrollLoading.style.display="block"; 
		
					//reset the errors text
					var errorsHTML = document.getElementById('saveToListErrors');
					errorsHTML.innerHTML = "";
					
					//swap the savetolist_createnew popover with the savetolist popover
    				swapPopovers('savetolist_createnew', 'savetolist');
		
					//make a call to ajax to retrieve the other booklists 
					//get the booklist id
					var booklistId = document.getElementById('saveToListBooklistId').value;
					
					var httpRequest = createRequest();
    				var url = getPrefixDomain() + "/tbw/retrieveListOfBooklists.do?booklistId=" + booklistId;
				var newBooklistId = result.getResult();
    				httpRequest.onreadystatechange = function() { setSaveToListInnerScroll(httpRequest, newBooklistId ); };
    				httpRequest.open('GET', url, true);
    				httpRequest.send(null);
					
                } else if (result.isNotLoggedIn()) {
			
				//hide the save to list create new popover, display the login popover
				setUpLoginPopover('savetolist_createnew');
				swapPopovers('savetolist_createnew', 'login');
				
		} else {
                  	
                  	//create error text, and display in the popup
                  	var errors = result.getErrors();
                  	var errorText = "";
                  	for (var i=0; i<errors.length; i++) {
                  		errorText = errorText + "Error:" + errors[i].getDescription() + "<br>";
                  	}
			//hide the blank booklist name error text
			var saveToListNewBooklistNameErrorText = document.getElementById('saveToListNewBooklistNameErrorText');
			saveToListNewBooklistNameErrorText.style.display="none"; 

                  	var errorsHTML = document.getElementById('saveToListCreateNewErrors');
                  	errorsHTML.innerHTML = errorText;
                }
                
            } else {
                  alert('There was a problem with the request, ' + httpRequest.status);
            }
      }
}