function goToBooklistsWithCheck(booklistsLinkId, opt_position) {
	
	//make an ajax call to check the sps user in session
	var goToBooklistsLink = document.getElementById(booklistsLinkId);

	var httpRequest = createRequest();
	var url = getPrefixDomain() + "/tbw/checkLogin.do";
	httpRequest.onreadystatechange = function() { if (checkLoginProcess(httpRequest, goToBooklistsLink, '', opt_position)) { goToBooklists(); } };
	httpRequest.open('GET', url, true);
	httpRequest.send(null);
}

function goToBooklists() {
	window.location = getPrefixDomain() + "/tbw/viewListOfBooklists.do";
}

function submitSearchForm(inputId, formId, queryString){
	      		
	var searchForm = document.forms[formId];
	var searchParam = searchForm.elements[inputId];
		      	
	searchForm.action = searchForm.action 
		      		+ queryString 
		      		+  "&" + searchParam.name
		      		+ "=" + encodeURIComponent(searchParam.value);
		      											
	window.location = searchForm.action;
	return false; 
}

function submitSearchFormAndGradeOffset(inputId, formId, queryString,gradeOffset ){
	      		
	var searchForm = document.forms[formId];
	var searchParam = searchForm.elements[inputId];
		      	
	searchForm.action = searchForm.action 
		      		+ queryString 
		      		+  "&" + searchParam.name
		      		+ "=" + encodeURIComponent(searchParam.value)
				+ "&"
				+ "gradeOffset="
				+ gradeOffset;
		      											
	window.location = searchForm.action;
	return false; 
}

function checkLoginProcess(httpRequest, curobj, originPopoverStr, opt_position) {
	
	if (httpRequest.readyState == 4) {
            if (httpRequest.status == 200) {
            
            	var result = new processedXMLresponse(httpRequest.responseXML);
/*                  if (result.isSuccess()) {
                  
                 	return true;
                 	}
*/               	
                  if (result.isNotLoggedIn()) {
                  
                  	//show login popover
                  	setUpLoginPopover(originPopoverStr);
			// set the header and body of the message
			document.getElementById("loginPopHeader").innerHTML = document.getElementById("loginPopNotLoggedInHeader").innerHTML;
			document.getElementById("loginPopBody").innerHTML = document.getElementById("loginPopNotLoggedInBody").innerHTML;
			
                  	overlayLoginPop(curobj, 'login', opt_position);
                  	
                  }
		  else
		  	return true;
            } else {
		alert('There was a problem with the request, ' + httpRequest.status);
            }
      }
      return false;

}

function setUpLoginPopover(originPopoverStr) {

	//have the top left corner style of the login popover match the top left of the origin popover,
	//assuming there is an origin
	
	var topLeftOrigin = document.getElementById(originPopoverStr + 'TopLeft');
	var topRightOrigin = document.getElementById(originPopoverStr + 'TopRight');
	var bottomLeftOrigin = document.getElementById(originPopoverStr + 'BottomLeft');
	var bottomRightOrigin = document.getElementById(originPopoverStr + 'BottomRight');
	
	if (topLeftOrigin != null) {
		document.getElementById('loginTopLeft').className = topLeftOrigin.className;
	} else {
		//default to top-left arrow
		document.getElementById('loginTopLeft').className = "roundblueshadow-top-leftarrow";
	}
	
	if (topRightOrigin != null) {
		document.getElementById('loginTopRight').className = topRightOrigin.className;
	} else {
		//default
		document.getElementById('loginTopRight').className = "roundblueshadow-top-right";
	}
	
	if (bottomLeftOrigin != null) {
		document.getElementById('loginBottomLeft').className = bottomLeftOrigin.className;
	} else {
		//default
		document.getElementById('loginBottomLeft').className = "roundblueshadow-bottom-left";
	}
	
	if (bottomRightOrigin != null) {
		document.getElementById('loginBottomRight').className = bottomRightOrigin.className;
	} else {
		//default
		document.getElementById('loginBottomRight').className = "roundblueshadow-bottom-right";
	}
	
	
	var loginPath = document.getElementById('loginPath').value;
	var loginUrl = loginPath + '?finalSuccessURL=' + encodeURIComponent(window.location);
                
	var loginLink = document.getElementById('loginPopLoginLink');
	loginLink.href = loginUrl;
}

function overlayLoginPop(curobj, subobjstr, opt_position){
	
	if (document.getElementById){

		overlay(curobj, subobjstr, opt_position);

	} else {

		return true;
	}
}