var sPatronID = "";
var sServerHashSeed = "";
var sLoginStatus = "";
var sFamilyRegistration = "";
var sPrintCert = "";
var sReviewSetting = "";
var sAnonymousReviewsSetting = "";
var sLoggedOutMessage = "";
var sStayLoggedIn = "";
var serverDelay = 1000;

var EvancedFlag = 0;        //set to 1 to use encryption security

if (document.layers) {n=1;ie=0;n6=0}
if (document.all) {n=0;ie=1;n6=0}
if (document.getElementById&&!document.all) {n=0;ie=0;n6=1}

try
{
	if (!ReviewsFormat)
		ReviewsFormat = 0;
}
catch (e)
{
	ReviewsFormat = 0;
}

// load widget content
window.onload=function()
{
	loadContent();
	setTimeout("waitForIt()", serverDelay);
}

// log out user
window.onunload=function()
{
	if (sPatronID != "" && sStayLoggedIn != "Y")
	{
		doLogOut();
	}
	sStayLoggedIn = "";
}

// check for enter key
function checkForEnter(e)
{
	var evtobj = window.event ? event : e;
			
	if (evtobj.keyCode == 13)
	{
		doLogin();
	}
}

//closes novelist containder
function closeInfoContainer(ReviewID) {
	var uniqueContainer = "infoContainer" + ReviewID.toString()
	document.getElementById(uniqueContainer).style.display = "none";
}

// hides/shows components depending on login status
function displayComponents(loginStatus)
{
	hideDivs();
	
	if (loginStatus == "Logged In")
	{
		// turn on components when user is logged in
		if (eval("document.getElementById('spnLoggedIn')"))
			document.getElementById("spnLoggedIn").style.display = "block";
		
		if (eval("document.getElementById('spnResetPassword')"))
			document.getElementById("spnResetPassword").style.display = "none";
		if (eval("document.getElementById('divResetPassword')"))
			document.getElementById("divResetPassword").innerHTML = "";
		
		if (eval("document.getElementById('divPrizeMessage')")) {
			if (document.getElementById("divPrizeMessage").innerHTML != "") {
				document.getElementById("divPrizeMessage").style.display = "block";
			}
			try
			{
				document.getElementById("prizescroll").start();
			}
			catch(e) {}
		}
		
		if (eval("document.getElementById('divCertificateMessage')")) {
			if (document.getElementById("divCertificateMessage").innerHTML != "") {
				document.getElementById("divCertificateMessage").style.display = "block";
			}
		}
		
		if (eval("document.getElementById('divFamilyRegistration')")) {
			if (sFamilyRegistration == "1") {
				document.getElementById("divFamilyRegistration").style.display = "block";
			}
		}
		
		if (eval("document.getElementById('spnLoggedInReviews')")) {
			if (sReviewSetting == "2") {
				document.getElementById("spnLoggedInReviews").style.display = "block";
			}
			else {
				if (eval("document.getElementById('spnLoggedOutReviews')")) {
					document.getElementById("spnLoggedOutReviews").style.display = "block";
				}
			}
		}
	}
	else if (loginStatus == "Password Reset")
	{
		// turn on components when user's password is reset
		if (eval("document.getElementById('spnResetPassword')"))
			document.getElementById("spnResetPassword").style.display = "block";
	}
	else
	{
		// turn on components when user is logged out
		if (eval("document.getElementById('spnLoggedOut')"))
			document.getElementById("spnLoggedOut").style.display = "block";
		
		if (eval("document.getElementById('spnLoggedOutReviews')")) {
			document.getElementById("spnLoggedOutReviews").style.display = "block";
		}
		
		if (eval("document.getElementById('spnLoggedInReviews')")) {
			if ((sReviewSetting == "1" || sReviewSetting == "2") && sAnonymousReviewsSetting == "1") {
				document.getElementById("spnLoggedInReviews").style.display = "block";
				if (eval("document.getElementById('spnLoggedOutReviews')")) {
					document.getElementById("spnLoggedOutReviews").style.display = "none";
				}
			}
		}
		
		// clear out login fields
		if (eval("document.getElementById('username')"))
			document.getElementById("username").value = "";
		if (eval("document.getElementById('libcardno')"))
			document.getElementById("libcardno").value = "";
		if (eval("document.getElementById('password')"))
			document.getElementById("password").value = "";
		if (eval("document.getElementById('pinnumber')"))
			document.getElementById("pinnumber").value = "";
	}
	setLoginFocus();
}

// facebook function - logs out user
function doFBLogout() {
	try {
		document.getElementById("facebook-container").style.display = "none";
		document.getElementById("fbFrameLogout").src = FBAppPath + "fbprocessor1.asp?MessageType=LogOut&Site=" + ServerPath + "&FBProxyURL=" + ReturnPage;
	}
	catch (e) {}
}

// logs in patron
function doLogin()
{
	var str1 = ""; var str2 = ""; var str3 = ""; var str4 = "";
	sServerHashSeed = document.getElementById("ServerHashSeed").value;
	
	if (eval("document.getElementById('username')"))
	{
		if (EvancedFlag == 0)
			var str1 = document.getElementById("username").value;
		else
			var str1 = getAscii(endecrypt(document.getElementById("username").value, hex_md5(sServerHashSeed)));
	}
	if (eval("document.getElementById('libcardno')"))
	{
		if (EvancedFlag == 0)
			var str2 = document.getElementById("libcardno").value;
		else
			var str2 = getAscii(endecrypt(document.getElementById("libcardno").value, hex_md5(sServerHashSeed)));
	}
	if (eval("document.getElementById('password')"))
	{
		if (EvancedFlag == 0)
			var str3 = document.getElementById("password").value;
		else
			var str3 = getAscii(endecrypt(document.getElementById("password").value, hex_md5(sServerHashSeed)));
	}
	if (eval("document.getElementById('pinnumber')"))
	{
		if (EvancedFlag == 0)
			var str4 = document.getElementById("pinnumber").value;
		else
			var str4 = getAscii(endecrypt(document.getElementById("pinnumber").value, hex_md5(sServerHashSeed)));
	}
	
	if (str1 != "")
		str1 = "&str1=" + str1;
	if (str2 != "")
		str2 = "&str2=" + str2;
	if (str3 != "")
		str3 = "&str3=" + str3;
	if (str4 != "")
		str4 = "&str4=" + str4;
	
	var str = ServerPath + "readerwidget.asp?command=login&programid=" + ProgramID + "&pagelangid=" + PageLangID + str1 + str2 + str3 + str4 + "&evancedflag=" + EvancedFlag + "&reviewsformat=" + ReviewsFormat;
	rescriptHomePage(str);
}

// log out patron
function doLogOut()
{
	var str = ServerPath + "readerwidget.asp?command=logout&programid=" + ProgramID + "&pagelangid=" + PageLangID + "&reviewsformat=" + ReviewsFormat;
	rescriptHomePage(str);

	setTimeout("doLogOutFunction()", serverDelay);
}

// other log out functions are performed with this routine after delay
function doLogOutFunction()
{
    	//redo language stuff
	rescriptHomePage(arrScriptBlocks[1]);
	
	doFBLogout();
	
	displayComponents("Logged Out");

	if (sLoggedOutMessage != "")
	{
		window.alert(sLoggedOutMessage);
		setLoginFocus();
	}
}

// resets patron's password
function doResetPassword()
{
	var str1 = ""; var str2 = "";
	
	if (eval("document.getElementById('newpassword')"))
	{
		if (EvancedFlag == 0)
			var str1 = document.getElementById("newpassword").value;
		else
			var str1 = getAscii(endecrypt(document.getElementById("newpassword").value, hex_md5(sServerHashSeed)));
	}
	if (eval("document.getElementById('confirmnewpassword')"))
	{
		if (EvancedFlag == 0)
			var str2 = document.getElementById("confirmnewpassword").value;
		else
			var str2 = getAscii(endecrypt(document.getElementById("confirmnewpassword").value, hex_md5(sServerHashSeed)));
	}
	
	if (str1 != "")
		str1 = "&str1=" + str1;
	if (str2 != "")
		str2 = "&str2=" + str2;
	
	var str = ServerPath + "readerwidget.asp?command=resetpassword&programid=" + ProgramID + "&pagelangid=" + PageLangID + str1 + str2 + "&evancedflag=" + EvancedFlag + "&reviewsformat=" + ReviewsFormat;
	rescriptHomePage(str);
}

// replace encoded html chars
function escapeHTML(str)
{
	str = str.replace(/>/g, "&gt;");
	str = str.replace(/</g, "&lt;");
	str = str.replace(/"/g, "&quot;");
	str = str.replace(/'/g, "&#039;");
	
	str = str.replace(/&gt;/g, ">");
	str = str.replace(/&lt;/g, "<");
	str = str.replace(/&quot;/g, "'");
	str = str.replace(/&#039;/g, "'");
	
	return str
}

// get ascii code for char
function getAscii(str)
{
	var chr = ""; var chrcode = 0; var codestr = ""; var codefinalstr = "";
	for (var x = 0; x < str.length; x++)
	{
		chr = str.substr(x,1);
		chrcode = chr.charCodeAt(0);
		codestr = chrcode.toString();
		if (codestr.length == 1)
			codestr = "00" + codestr;
		else if (codestr.length == 2)
			codestr = "0" + codestr;
		codefinalstr = codefinalstr + codestr; 
	}
	return codefinalstr;
}

//gets novelist author info - using cross-frame communications
function getSimilarAuthors(ReviewID) {
    
    try
	{
	    var ISBN = document.getElementById("similarISBN" + ReviewID.toString()).value;
		document.getElementById("nov-container").src = FBAppPath + "novprocessor.asp?mode=GetInfoAuthors&ReviewID=" + ReviewID + "&ISBN=" + ISBN + "&Site=" + ServerPath + "&SRProxy=" + ReturnPage;
	}
	catch(e) {}
}

//gets novelist author info - using cross-frame communications
function getSimilarBooks(ReviewID) {
    
    try
	{
	    var ISBN = document.getElementById("similarISBN" + ReviewID.toString()).value;
		document.getElementById("nov-container").src = FBAppPath + "novprocessor.asp?mode=GetInfoBooks&ReviewID=" + ReviewID + "&ISBN=" + ISBN + "&Site=" + ServerPath + "&SRProxy=" + ReturnPage;
	}
	catch(e) {}
}

// close/hide FB frame
function hideFBFrame()
{
	document.getElementById("facebook-container").style.display="none";
}

// hides all dynamic divs
function hideDivs()
{
	if (eval("document.getElementById('spnLoggedIn')"))
		document.getElementById("spnLoggedIn").style.display = "none";
	if (eval("document.getElementById('spnLoggedOut')"))
		document.getElementById("spnLoggedOut").style.display = "none";
	if (eval("document.getElementById('divPrizeMessage')"))
		document.getElementById("divPrizeMessage").style.display = "none";
	if (eval("document.getElementById('divFamilyRegistration')"))
		document.getElementById("divFamilyRegistration").style.display = "none";
	if (eval("document.getElementById('divCertificateMessage')"))
		document.getElementById("divCertificateMessage").style.display = "none";
	if (eval("document.getElementById('spnLoggedInReviews')"))
		document.getElementById("spnLoggedInReviews").style.display = "none";
	if (eval("document.getElementById('spnLoggedOutReviews')"))
		document.getElementById("spnLoggedOutReviews").style.display = "none";
	if (eval("document.getElementById('spnResetPassword')"))
		document.getElementById("spnResetPassword").style.display = "none";
}

function JSONscriptRequest(fullUrl) {
	// REST request path
	this.fullUrl = fullUrl; 
	// Keep IE from caching requests
	this.noCacheIE = "&noCacheIE=" + (new Date()).getTime();
	// Get the DOM location to put the script tag
	this.headLoc = document.getElementsByTagName("head").item(0);
	// Generate a unique script tag id
	this.scriptId = "YJscriptId" + JSONscriptRequest.scriptCounter++;
}

// Static script ID counter
JSONscriptRequest.scriptCounter = 1;

// buildScriptTag method
JSONscriptRequest.prototype.buildScriptTag = function () {
	// Create the script tag
	this.scriptObj = document.createElement("script");

	// Add script object attributes
	this.scriptObj.setAttribute("type", "text/javascript");
	this.scriptObj.setAttribute("src", this.fullUrl + this.noCacheIE);
	this.scriptObj.setAttribute("id", this.scriptId);
}

// removeScriptTag method
JSONscriptRequest.prototype.removeScriptTag = function () {
	// Destroy the script tag
	this.headLoc.removeChild(this.scriptObj); 
}

// addScriptTag method
JSONscriptRequest.prototype.addScriptTag = function () {
	// Create the script tag
	this.headLoc.appendChild(this.scriptObj);
}

// refreshes content
function loadContent()
{
	for (var i = 0; i < arrScriptBlocks.length; i++)
	{
		rescriptHomePage(arrScriptBlocks[i]);
	}
}

// facebook function - load facebook function into widget page
function loadFBMessage() 
{
	try
	{
		if (document.getElementById("FBStatus").value == "1")
		{
			var messagetype = document.getElementById("FBMessageType").value;
			var programid = document.getElementById("FBProgramID").value;
			var messageid = document.getElementById("FBMessageID").value;

			document.getElementById("facebook-container").style.display = "block";
			document.getElementById("frmFB").src = FBAppPath + "fbprocessor1.asp?MessageType=" + messagetype + "&ProgramID=" + programid + "&MessageID=" + messageid + "&Display=99&Site=" + ServerPath + "&FBProxyURL=" + ReturnPage + "&bgcolor=" + FBBackgroundColor;
			document.getElementById("FBStatus").value = "0";
		}
	}
	catch(e) {}
}

// checks session and refreshes some content
function loadPartialPage()
{
	rescriptHomePage(arrScriptBlocks[0]);
	setTimeout("loadVars()", serverDelay);
}

// load global vars
function loadVars()
{
	sPatronID = document.getElementById("PatronID").value;
	sServerHashSeed = document.getElementById("ServerHashSeed").value;	
	sLoginStatus = document.getElementById("LoginStatus").value;
	try {
		sFamilyRegistration = document.getElementById("FamilyRegistration").value;
	}
	catch(e) {}
	sPrintCert = document.getElementById("PrintCert").value;
	sReviewSetting = document.getElementById("ReviewSetting").value;
	try {
		sAnonymousReviewsSetting = document.getElementById("AnonymousReviewsSetting").value;
	}
	catch(e) {}
}

// used to show/hide fb prompt or novelist info
function OnMessageFromChild(message) 
{
	if (message.length > 0) {
		var parameters = parseParameters(unescape(message));

		if (parameters["display"] != null) {
			document.getElementById('facebook-container').style.display = parameters["display"];
		}
		
		if (parameters["messagetype"] != null) {
			if (window.XMLHttpRequest) {
				xmlHttp = new XMLHttpRequest();
			}
			else { // for older IE 5/6
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
			var url = ServerPath + "fbstats.asp?MessageType=" + parameters["messagetype"] + "&ProgramID=" + parameters["programid"] + "&MessageID=" + parameters["messageid"] + "&ts=<%=Now()%>";
			xmlHttp.open("GET", url, false);
			xmlHttp.send(null);
		}
		else if (parameters["novcontent"] != null) {
		    	var ReviewID = parameters["ReviewID"];
		    	var NovContent = parameters["novcontent"];
		    	
		    	if (parameters["mode"] == "GetInfoAuthors")
			{
				document.getElementById("similarAuthors" + ReviewID.toString()).className = "nov-pointer nov nov-tab-selected";
				document.getElementById("similarBooks" + ReviewID.toString()).className = "nov-pointer nov nov-tab-notselected";

				var uniqueContainer = "infoContainer" + ReviewID.toString()
				document.getElementById(uniqueContainer).style.display = "block";
				document.getElementById(uniqueContainer).innerHTML = NovContent;
			}
			else if (parameters["mode"] == "GetInfoBooks")
			{
				document.getElementById("similarAuthors" + ReviewID.toString()).className = "nov-pointer nov nov-tab-notselected";
				document.getElementById("similarBooks" + ReviewID.toString()).className = "nov-pointer nov nov-tab-selected";

				var uniqueContainer = "infoContainer" + ReviewID.toString()
				document.getElementById(uniqueContainer).style.display = "block";
				document.getElementById(uniqueContainer).innerHTML = NovContent;
			}
		}
	}
}

// used to parse parameters sent for cross-domain communication
function parseParameters(message) {
	var dictionary = new Array();
	
	var pairs;
	if (message.indexOf("^^")>=0)
	{
		pairs = message.split("^^");
	}
	else
	{
		pairs = message.split(/&/);
	}

	for (var keyValuePairIndex in pairs) {
		var nameVal = pairs[keyValuePairIndex].split(/=/);
		if (nameVal[1] != null)
		{
			nameVal[1] = escapeHTML(nameVal[1].replace(/@#EQ/g, "="));
		}
		dictionary[nameVal[0]] = nameVal[1];
	}

	return dictionary;
}

//refreshes content with language-specific variables
function refreshContent(langID)
{
	var origPageLangID = PageLangID;
	PageLangID = langID;
	
	for (var i = 1; i < arrScriptBlocks.length; i++)
	{
		var strURL = arrScriptBlocks[i];
		strURL = strURL.replace("pagelangid=" + origPageLangID, "pagelangid=" + PageLangID);
		
		rescriptHomePage(strURL);
	}
}

//reloads/refreshes the page's content
function refreshPage()
{
	var str = ServerPath + "readerwidget.asp?command=refresh&programid=" + ProgramID + "&pagelangid=" + PageLangID + "&reviewsformat=" + ReviewsFormat;
	rescriptHomePage(str);
	
	setTimeout("loadFBMessage()", serverDelay);
}

// write out in-line script to "refresh" home page
function rescriptHomePage(request)
{
	// Create a new script object
	aObj = new JSONscriptRequest(request);
	// Build the script tag
	aObj.buildScriptTag();
	// Execute (add) the script tag
	aObj.addScriptTag();
}

// sets the focus to the first login field
function setLoginFocus()
{
	var bSetFocus = false;
	
	try {
		if (eval("document.getElementById('username')"))
		{
			document.getElementById("username").focus();
			bSetFocus = true;
		}
		if (eval("document.getElementById('libcardno')") && !bSetFocus)
		{
			document.getElementById("libcardno").focus();
			bSetFocus = true;
		}
		if (eval("document.getElementById('password')") && !bSetFocus)
		{
			document.getElementById("password").focus();
			bSetFocus = true;
		}
	}
	catch(e) {}
}

// set the mouse cursor
function setMouseCursor(obj)
{
	obj.style.cursor = "pointer";
}

// loads program selection page for adding family members
function showAddMember()
{
	doFBLogout();
	loadPartialPage();
	
	var strOptions = "";
	
	if (window.showModalDialog) {
	    try {
		    strOptions = "DialogTop: " + AddMember_Top + ";DialogLeft: " + AddMember_Left + ";";
		}
		catch(e) {
			strOptions = "DialogTop: 50px;DialogLeft: 50px;";
		}
		
		try {
		    strOptions = strOptions + "DialogHeight: " + AddMember_Height + ";DialogWidth: " + AddMember_Width + ";";
		}
		catch(e) {
		    strOptions = strOptions + "DialogHeight: 500px;DialogWidth: 650px;";
		}
		var hWin = window.showModalDialog(ServerPath + "patronprogramsel.asp?Ttype=family&ParentProgramID=" + ProgramID + "&ProgramID=" + ProgramID + "&PageLangID=" + PageLangID + "&Widget=Y","", strOptions + "resizable:yes; scrollbars:yes;");
	}
	else {
	    try {
		    strOptions = "top=" + AddMember_Top + ",left=" + AddMember_Left + ",";
		}
		catch(e) {
			strOptions = "top=50px,left=50px,";
		}
		
		try {
		    strOptions = strOptions + "height=" + AddMember_Height + ",width=" + AddMember_Width + ",";
		}
		catch(e) {
		    strOptions = strOptions + "height=500px,width=650px,";
		}
		var hWin = window.open(ServerPath + "patronprogramsel.asp?Ttype=family&ParentProgramID=" + ProgramID + "&ProgramID=" + ProgramID + "&PageLangID=" + PageLangID + "&Widget=Y","", strOptions + "modal=yes,resizable=yes,scrollbars=yes");
		
	}
	refreshPage();
}

// loads forgot login info page 
function showForgotLogin() 
{
    var strOptions = "";
	
	if (window.showModalDialog) {
	    try {
		    strOptions = "DialogTop: " + ForgotLoginInfo_Top + ";DialogLeft: " + ForgotLoginInfo_Left + ";";
		}
		catch(e) {
			strOptions = "DialogTop: 50px;DialogLeft: 50px;";
		}
        
        try {
		    strOptions = strOptions + "DialogHeight: " + ForgotLoginInfo_Height + ";DialogWidth: " + ForgotLoginInfo_Width + ";";
		}
		catch(e) {
		    strOptions = strOptions + "DialogHeight: 200px;DialogWidth: 700px;";
		}
		var hWin = window.showModalDialog(ServerPath + "emailuserinfo.asp?ProgramID=" + ProgramID + "&PageLangID=" + PageLangID + "&Widget=Y","", strOptions + "resizable:yes; scrollbars:yes;");
	}
	else {
	    try {
		    strOptions = "top=" + ForgotLoginInfo_Top + ",left=" + ForgotLoginInfo_Left + ",";
		}
		catch(e) {
			strOptions = "top=50px,left=50px,";
		}
        
        try {
		    strOptions = strOptions + "height=" + ForgotLoginInfo_Height + ",width=" + ForgotLoginInfo_Width + ",";
		}
		catch(e) {
		    strOptions = strOptions + "height=200px,width=700px,";
		}
		var hWin = window.open(ServerPath + "emailuserinfo.asp?ProgramID=" + ProgramID + "&PageLangID=" + PageLangID + "&Widget=Y","", strOptions + "modal=yes,resizable=yes,scrollbars=yes");
	}
	refreshPage();
}

// loads logging page
function showLog()
{
	loadPartialPage();
	
	var strOptions = "";
	
	if (NewWindow == "Y") {
		if (window.showModalDialog) {
		    try {
			    strOptions = "DialogTop: " + PatronLog_Top + ";DialogLeft: " + PatronLog_Left + ";";
			}
			catch(e) {
				strOptions = "DialogTop: 50px;DialogLeft: 50px;";
			}
            
            try {
			    strOptions = strOptions + "DialogHeight: " + PatronLog_Height + ";DialogWidth: " + PatronLog_Width + ";";
			}
			catch(e) {
			    strOptions = strOptions + "DialogHeight: 500px;DialogWidth: 825px;";
			}
			var hWin = window.showModalDialog(ServerPath + "patronlogadd.asp?ProgramID=" + ProgramID + "&PageLangID=" + PageLangID + "&NewWindow=Y","", strOptions + "resizable:yes; scrollbars:yes;");
		}
		else {
		    try {
			    strOptions = "top=" + PatronLog_Top + ",left=" + PatronLog_Left + ",";
			}
			catch(e) {
				strOptions = "top=50px,left=50px,";
			}
            
            try {
			    strOptions = strOptions + "height=" + PatronLog_Height + ",width=" + PatronLog_Width + ",";
			}
			catch(e) {
			    strOptions = strOptions + "height=500px,width=825px,";
			}
			var hWin = window.open(ServerPath + "patronlogadd.asp?ProgramID=" + ProgramID + "&PageLangID=" + PageLangID + "&NewWindow=Y","", strOptions + "modal=yes,resizable=yes,scrollbars=yes");
		}
		refreshPage();
	}
	else {
		sStayLoggedIn = "Y";
		location.href = ServerPath + "patronlogadd.asp?ProgramID=" + ProgramID + "&PageLangID=" + PageLangID + "&Widget=Y&ReturnPage=" + ReturnPage + ";";
	}
}

// loads patron home page for managing family members
function showManageMember(patronID)
{
	doFBLogout();
	loadPartialPage();

	var strOptions = "";
	
	if (window.showModalDialog) {
	    try {
		    strOptions = "DialogTop: " + ManageMember_Top + ";DialogLeft: " + ManageMember_Left + ";";
		}
		catch(e) {
			strOptions = "DialogTop: 50px;DialogLeft: 50px;";
		}
        
        try {
		    strOptions = strOptions + "DialogHeight: " + ManageMember_Height + ";DialogWidth: " + ManageMember_Width + ";";
		}
		catch(e) {
		    strOptions = strOptions + "DialogHeight: 500px;DialogWidth: 825px;";
		}
		var hWin = window.showModalDialog(ServerPath + "homepage.asp?mm=1&ProgramID=" + ProgramID + "&PageLangID=" + PageLangID,"", strOptions + "resizable:yes; scrollbars:yes;");
	}
	else {
	    try {
		    strOptions = "top=" + ManageMember_Top + ",left=" + ManageMember_Left + ",";
		}
		catch(e) {
			strOptions = "top=50px,left=50px,";
		}
        
        try {
		    strOptions = strOptions + "height=" + ManageMember_Height + ",width=" + ManageMember_Width + ",";
		}
		catch(e) {
			strOptions = strOptions + "height=500px,width=825px,";
		}
		var hWin = window.open(ServerPath + "homepage.asp?mm=1&ProgramID=" + ProgramID + "&PageLangID=" + PageLangID,"", strOptions + "modal=yes,resizable=yes,scrollbars=yes");
	}
}

// loads patron certificate
function showPatronCert()
{
	loadPartialPage();
	
	var strOptions = "";
	
	try {
	    strOptions = "top=" + Certificate_Top + ",left=" + Certificate_Left + ",";
	}
	catch(e) {
	    strOptions = "top=50px,left=50px,";
	}
    
    try {
	    strOptions = strOptions + "height=" + Certificate_Height + ",width=" + Certificate_Width + ",";
	}
	catch(e) {
	    strOptions = strOptions + "height=500px,width=825px,";
	}
	var hWin = window.open(ServerPath + "patroncertificate.asp?ProgramID=" + ProgramID + "&PageLangID=" + PageLangID + "&Widget=Y","", strOptions + "modal=yes,resizable=yes,scrollbars=yes");
}

// loads patron reviews
function showPatronReviews()
{
	loadPartialPage();
	
	var strOptions = "";
	
	if (NewWindow == "Y") {
		if (window.showModalDialog) {
		    try {
			    strOptions = "DialogTop: " + MyReviews_Top + ";DialogLeft: " + MyReviews_Left + ";";
			}
			catch(e) {
				strOptions = "DialogTop: 50px;DialogLeft: 50px;";
			}
            
            try {
			    strOptions = strOptions + "DialogHeight: " + MyReviews_Height + ";DialogWidth: " + MyReviews_Width + ";";
			}
			catch(e) {
			    strOptions = strOptions + "DialogHeight: 500px;DialogWidth: 825px;";
			}
			var hWin = window.showModalDialog(ServerPath + "patronreviewslist.asp?ProgramID=" + ProgramID + "&PageLangID=" + PageLangID + "&NewWindow=Y","", strOptions + "resizable:yes; scrollbars:yes;");
		}
		else {
		    try {
			    strOptions = "top=" + MyReviews_Top + ",left=" + MyReviews_Left + ",";
			}
			catch(e) {
				strOptions = "top=50px,left=50px,";
			}
            
            try {
			    strOptions = strOptions + "height=" + MyReviews_Height + ",width=" + MyReviews_Width + ",";
			}
			catch(e) {
			    strOptions = strOptions + "height=500px,width=825px,";
			}
			var hWin = window.open(ServerPath + "patronreviewslist.asp?ProgramID=" + ProgramID + "&PageLangID=" + PageLangID + "&NewWindow=Y","", strOptions + "modal=yes,resizable=yes,scrollbars=yes");
		}
		refreshPage();
	}
	else {
		sStayLoggedIn = "Y";
		location.href = ServerPath + "patronreviewslist.asp?ProgramID=" + ProgramID + "&PageLangID=" + PageLangID + "&Widget=Y&ReturnPage=" + ReturnPage + ";";
	}
}

// loads patron reading list
function showReadingList()
{
	loadPartialPage();
	
	var strOptions = "";
	
	if (NewWindow == "Y") {
		if (window.showModalDialog) {
		    try {
			    strOptions = "DialogTop: " + ReadingList_Top + ";DialogLeft: " + ReadingList_Left + ";";
			}
			catch(e) {
				strOptions = "DialogTop: 50px;DialogLeft: 50px;";
			}
            
            try {
			    strOptions = strOptions + "DialogHeight: " + ReadingList_Height + ";DialogWidth: " + ReadingList_Width + ";";
			}
			catch(e) {
			    strOptions = strOptions + "DialogHeight: 500px;DialogWidth: 825px;";
			}
			var hWin = window.showModalDialog(ServerPath + "readinglist.asp?ProgramID=" + ProgramID + "&PageLangID=" + PageLangID + "&NewWindow=Y","", strOptions + "resizable:yes; scrollbars:yes;");
		}
		else {
		    try {
			    strOptions = "top=" + ReadingList_Top + ",left=" + ReadingList_Left + ",";
			}
			catch(e) {
				strOptions = "top=50px,left=50px,";
			}
            
            try {
			    strOptions = strOptions + "height=" + ReadingList_Height + ",width=" + ReadingList_Width + ",";
			}
			catch(e) {
			    strOptions = strOptions + "height=500px,width=825px,";
			}
			var hWin = window.open(ServerPath + "readinglist.asp?ProgramID=" + ProgramID + "&PageLangID=" + PageLangID + "&NewWindow=Y","", strOptions + "modal=yes,resizable=yes,scrollbars=yes");
		}
		refreshPage();
	}
	else {
		sStayLoggedIn = "Y";
		location.href = ServerPath + "readinglist.asp?ProgramID=" + ProgramID + "&PageLangID=" + PageLangID + "&Widget=Y&ReturnPage=" + ReturnPage + ";";
	}
}

// loads registration page
function showRegistration()
{
	var strOptions = "";
	
	if (NewWindow == "Y") {
		if (window.showModalDialog) {
		    try {
			    strOptions = "DialogTop: " + SignUp_Top + ";DialogLeft: " + SignUp_Left + ";";
			}
			catch(e) {
				strOptions = "DialogTop: 50px;DialogLeft: 50px;";
			}

            try {
			    strOptions = strOptions + "DialogHeight: " + SignUp_Height + ";DialogWidth: " + SignUp_Width + ";";
			}
			catch(e) {
			    strOptions = strOptions + "DialogHeight: 500px;DialogWidth: 825px;";
			}
			var hWin = window.showModalDialog(ServerPath + "programregmaint.asp?ProgramID=" + ProgramID + "&PageLangID=" + PageLangID + "&Mode=New&NewWindow=Y","", strOptions + "resizable:yes; scrollbars:yes;");
		}
		else {
		    try {
			    strOptions = "top=" + SignUp_Top + ",left=" + SignUp_Left + ",";
			}
			catch(e) {
				strOptions = "top=50px,left=50px,";
			}
            
            try {
			    strOptions = strOptions + "height=" + SignUp_Height + ",width=" + SignUp_Width + ",";
			}
			catch(e) {
			    strOptions = strOptions + "height=500px,width=825px,";
			}
			var hWin = window.open(ServerPath + "programregmaint.asp?ProgramID=" + ProgramID + "&PageLangID=" + PageLangID + "&Mode=New&NewWindow=Y","", strOptions + "modal=yes,resizable=yes,scrollbars=yes");
		}
		refreshPage();
	}
	else {
		sStayLoggedIn = "Y";
		location.href = ServerPath + "programregmaint.asp?ProgramID=" + ProgramID + "&PageLangID=" + PageLangID + "&Mode=New&Widget=Y&home=" + ReturnPage + "&away=" + ReturnPage;
	}
}

// loads registration info page
function showRegInfo()
{
	loadPartialPage();
	
	var strOptions = "";
	
	if (NewWindow == "Y") {
		if (window.showModalDialog) {
		    try {
			    strOptions = "DialogTop: " + MyInfo_Top + ";DialogLeft: " + MyInfo_Left + ";";
			}
			catch(e) {
				strOptions = "DialogTop: 50px;DialogLeft: 50px;";
			}
            
            try {
			    strOptions = strOptions + "DialogHeight: " + MyInfo_Height + ";DialogWidth: " + MyInfo_Width + ";";
			}
			catch(e) {
			    strOptions = strOptions + "DialogHeight: 500px;DialogWidth: 825px;";
			}
			var hWin = window.showModalDialog(ServerPath + "programregmaint.asp?ProgramID=" + ProgramID + "&PatronID=" + sPatronID + "&PageLangID=" + PageLangID + "&Mode=Edit&NewWindow=Y","", strOptions + "resizable:yes; scrollbars:yes;");
		}
		else {
		    try {
			    strOptions = "top=" + MyInfo_Top + ",left=" + MyInfo_Left + ",";
			}
			catch(e) {
				strOptions = "top=50px,left=50px,";
			}
            
            try {
			    strOptions = strOptions + "height=" + MyInfo_Height + ",width=" + MyInfo_Width + ","
			}
			catch(e) {
			    strOptions = strOptions + "height=500px,width=825px,"
			}
			var hWin = window.open(ServerPath + "programregmaint.asp?ProgramID=" + ProgramID + "&PatronID=" + sPatronID + "&PageLangID=" + PageLangID + "&Mode=Edit&NewWindow=Y","", strOptions + "modal=yes,resizable=yes,scrollbars=yes");
		}
	}
	else {
		sStayLoggedIn = "Y";
		location.href = ServerPath + "programregmaint.asp?ProgramID=" + ProgramID + "&PatronID=" + sPatronID + "&PageLangID=" + PageLangID + "&Mode=Edit&Widget=Y&home=" + ReturnPage + "&away=" + ReturnPage;
	}
}

// loads RSS selection page
function showRSSSelection()
{
	loadPartialPage();
	
	var strOptions = "";
	
	if (window.showModalDialog) {
	    try {
		    strOptions = "DialogTop: " + RSS_Top + ";DialogLeft: " + RSS_Left + ";";
		}
		catch(e) {
			strOptions = "DialogTop: 50px;DialogLeft: 50px;";
		}
        
        try {
		    strOptions = strOptions + "DialogHeight: " + RSS_Height + ";DialogWidth: " + RSS_Width + ";";
		}
		catch(e) {
		    strOptions = strOptions + "DialogHeight: 500px;DialogWidth: 825px;";
		}
		var hWin = window.showModalDialog(ServerPath + "rssselection.asp?ProgramID=" + ProgramID + "&PageLangID=" + PageLangID,"", strOptions + "resizable:yes; scrollbars:yes;");
	}
	else {
	    try {
		    strOptions = "top=" + RSS_Top + ",left=" + RSS_Left + ",";
		}
		catch(e) {
			strOptions = "top=50px,left=50px,";
		}
        
        try {
		    strOptions = strOptions + "height=" + RSS_Height + ",width=" + RSS_Width + ",";
		}
		catch(e) {
		    strOptions = strOptions + "height=500px,width=825px,";
		}
		var hWin = window.open(ServerPath + "rssselection.asp?ProgramID=" + ProgramID + "&PageLangID=" + PageLangID,"", strOptions + "modal=yes,resizable=yes,scrollbars=yes");
	}
}

// loads submit review page
function showSubmitReview()
{
	loadPartialPage();
	
	var strOptions = "";
	
	if (window.showModalDialog) {
	    try {
		    strOptions = "DialogTop: " + SubmitReview_Top + ";DialogLeft: " + SubmitReview_Left + ";";
		}
		catch(e) {
			strOptions = "DialogTop: 50px;DialogLeft: 50px;";
		}
        
        try {
		    strOptions = strOptions + "DialogHeight: " + SubmitReview_Height + ";DialogWidth: " + SubmitReview_Width + ";";
		}
		catch(e) {
		    strOptions = strOptions + "DialogHeight: 500px;DialogWidth: 825px;";
		}
		var hWin = window.showModalDialog(ServerPath + "submitreview.asp?ProgramID=" + ProgramID + "&PatronID=" + sPatronID + "&PageLangID=" + PageLangID,"", strOptions + "resizable:yes; scrollbars:yes;");
	}
	else {
	    try {
		    strOptions = "top=" + SubmitReview_Top + ",left=" + SubmitReview_Left + ",";
		}
		catch(e) {
			strOptions = "top=50px,left=50px,";
		}
        
        try {
		    strOptions = strOptions + "height=" + SubmitReview_Height + ",width=" + SubmitReview_Width + ",";
		}
		catch(e) {
		    strOptions = strOptions + "height=500px,width=825px,";
		}
		var hWin = window.open(ServerPath + "submitreview.asp?ProgramID=" + ProgramID + "&PatronID=" + sPatronID + "&PageLangID=" + PageLangID,"", strOptions + "modal=yes,resizable=yes,scrollbars=yes");
	}
	refreshPage();
}

// loads reviews page
function showViewReviews(ProgramID, Random, Limit)
{
	var strOptions = "";
	
	if (window.showModalDialog) {
	    try {
		    strOptions = "DialogTop: " + ViewReviews_Top + ";DialogLeft: " + ViewReviews_Left + ";";
		}
		catch(e) {
			strOptions = "DialogTop: 50px;DialogLeft: 50px;";
		}
        
        try {
		    strOptions = strOptions + "DialogHeight: " + ViewReviews_Height + ";DialogWidth: " + ViewReviews_Width + ";";
		}
		catch(e) {
		    strOptions = strOptions + "DialogHeight: 500px;DialogWidth: 825px;";
		}
		var hWin = window.showModalDialog(ServerPath + "viewreviews.asp?ProgramID=" + ProgramID + "&PageLangID=" + PageLangID + "&Clear=Y&Random=" + Random + "&Limit=" + Limit,"", strOptions + "resizable:yes; scrollbars:yes;");
	}
	else {
	    try {
		    strOptions = "top=" + ViewReviews_Top + ",left=" + ViewReviews_Left + ",";
		}
		catch(e) {
			strOptions = "top=50px,left=50px,";
		}

        try {
		    strOptions = strOptions + "height=" + ViewReviews_Height + ",width=" + ViewReviews_Width + ",";
		}
		catch(e) {
		    strOptions = strOptions + "height=500px,width=825px,";
		}
		var hWin = window.open(ServerPath + "viewreviews.asp?ProgramID=" + ProgramID + "&PageLangID=" + PageLangID + "&Clear=Y&Random=" + Random + "&Limit=" + Limit,"", strOptions + "modal=yes,resizable=yes,scrollbars=yes");
	}
	refreshPage();
}

// set global vars and display content
function waitForIt()
{
	loadVars();
	displayComponents(sLoginStatus);
	
	if (NewWindow == "N")
	{
		refreshPage();
	}
}

/*md5 routines*/
/* A JavaScript implementation of the RSA Data Security, Inc. MD5 Message
* Digest Algorithm, as defined in RFC 1321.
* Version 2.1 Copyright (C) Paul Johnston 1999 - 2002.
* Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
* Distributed under the BSD License
* See http://pajhome.org.uk/crypt/md5 for more info.
*/

/*
* Configurable variables. You may need to tweak these to be compatible with
* the server-side, but the defaults work in most cases.
*/
var hexcase = 0;  /* hex output format. 0 - lowercase; 1 - uppercase        */
var b64pad  = ""; /* base-64 pad character. "=" for strict RFC compliance   */
var chrsz   = 8;  /* bits per input character. 8 - ASCII; 16 - Unicode      */

/*
* These are the functions you'll usually want to call
* They take string arguments and return either hex or base-64 encoded strings
*/
function hex_md5(s){ return binl2hex(core_md5(str2binl(s), s.length * chrsz));}

/*
* Calculate the MD5 of an array of little-endian words, and a bit length
*/
function core_md5(x, len)
{
/* append padding */
x[len >> 5] |= 0x80 << ((len) % 32);
x[(((len + 64) >>> 9) << 4) + 14] = len;

var a =  1732584193;
var b = -271733879;
var c = -1732584194;
var d =  271733878;

for(var i = 0; i < x.length; i += 16)
{
	var olda = a;
	var oldb = b;
	var oldc = c;
	var oldd = d;

	a = md5_ff(a, b, c, d, x[i+ 0], 7 , -680876936);
	d = md5_ff(d, a, b, c, x[i+ 1], 12, -389564586);
	c = md5_ff(c, d, a, b, x[i+ 2], 17,  606105819);
	b = md5_ff(b, c, d, a, x[i+ 3], 22, -1044525330);
	a = md5_ff(a, b, c, d, x[i+ 4], 7 , -176418897);
	d = md5_ff(d, a, b, c, x[i+ 5], 12,  1200080426);
	c = md5_ff(c, d, a, b, x[i+ 6], 17, -1473231341);
	b = md5_ff(b, c, d, a, x[i+ 7], 22, -45705983);
	a = md5_ff(a, b, c, d, x[i+ 8], 7 ,  1770035416);
	d = md5_ff(d, a, b, c, x[i+ 9], 12, -1958414417);
	c = md5_ff(c, d, a, b, x[i+10], 17, -42063);
	b = md5_ff(b, c, d, a, x[i+11], 22, -1990404162);
	a = md5_ff(a, b, c, d, x[i+12], 7 ,  1804603682);
	d = md5_ff(d, a, b, c, x[i+13], 12, -40341101);
	c = md5_ff(c, d, a, b, x[i+14], 17, -1502002290);
	b = md5_ff(b, c, d, a, x[i+15], 22,  1236535329);

	a = md5_gg(a, b, c, d, x[i+ 1], 5 , -165796510);
	d = md5_gg(d, a, b, c, x[i+ 6], 9 , -1069501632);
	c = md5_gg(c, d, a, b, x[i+11], 14,  643717713);
	b = md5_gg(b, c, d, a, x[i+ 0], 20, -373897302);
	a = md5_gg(a, b, c, d, x[i+ 5], 5 , -701558691);
	d = md5_gg(d, a, b, c, x[i+10], 9 ,  38016083);
	c = md5_gg(c, d, a, b, x[i+15], 14, -660478335);
	b = md5_gg(b, c, d, a, x[i+ 4], 20, -405537848);
	a = md5_gg(a, b, c, d, x[i+ 9], 5 ,  568446438);
	d = md5_gg(d, a, b, c, x[i+14], 9 , -1019803690);
	c = md5_gg(c, d, a, b, x[i+ 3], 14, -187363961);
	b = md5_gg(b, c, d, a, x[i+ 8], 20,  1163531501);
	a = md5_gg(a, b, c, d, x[i+13], 5 , -1444681467);
	d = md5_gg(d, a, b, c, x[i+ 2], 9 , -51403784);
	c = md5_gg(c, d, a, b, x[i+ 7], 14,  1735328473);
	b = md5_gg(b, c, d, a, x[i+12], 20, -1926607734);

	a = md5_hh(a, b, c, d, x[i+ 5], 4 , -378558);
	d = md5_hh(d, a, b, c, x[i+ 8], 11, -2022574463);
	c = md5_hh(c, d, a, b, x[i+11], 16,  1839030562);
	b = md5_hh(b, c, d, a, x[i+14], 23, -35309556);
	a = md5_hh(a, b, c, d, x[i+ 1], 4 , -1530992060);
	d = md5_hh(d, a, b, c, x[i+ 4], 11,  1272893353);
	c = md5_hh(c, d, a, b, x[i+ 7], 16, -155497632);
	b = md5_hh(b, c, d, a, x[i+10], 23, -1094730640);
	a = md5_hh(a, b, c, d, x[i+13], 4 ,  681279174);
	d = md5_hh(d, a, b, c, x[i+ 0], 11, -358537222);
	c = md5_hh(c, d, a, b, x[i+ 3], 16, -722521979);
	b = md5_hh(b, c, d, a, x[i+ 6], 23,  76029189);
	a = md5_hh(a, b, c, d, x[i+ 9], 4 , -640364487);
	d = md5_hh(d, a, b, c, x[i+12], 11, -421815835);
	c = md5_hh(c, d, a, b, x[i+15], 16,  530742520);
	b = md5_hh(b, c, d, a, x[i+ 2], 23, -995338651);

	a = md5_ii(a, b, c, d, x[i+ 0], 6 , -198630844);
	d = md5_ii(d, a, b, c, x[i+ 7], 10,  1126891415);
	c = md5_ii(c, d, a, b, x[i+14], 15, -1416354905);
	b = md5_ii(b, c, d, a, x[i+ 5], 21, -57434055);
	a = md5_ii(a, b, c, d, x[i+12], 6 ,  1700485571);
	d = md5_ii(d, a, b, c, x[i+ 3], 10, -1894986606);
	c = md5_ii(c, d, a, b, x[i+10], 15, -1051523);
	b = md5_ii(b, c, d, a, x[i+ 1], 21, -2054922799);
	a = md5_ii(a, b, c, d, x[i+ 8], 6 ,  1873313359);
	d = md5_ii(d, a, b, c, x[i+15], 10, -30611744);
	c = md5_ii(c, d, a, b, x[i+ 6], 15, -1560198380);
	b = md5_ii(b, c, d, a, x[i+13], 21,  1309151649);
	a = md5_ii(a, b, c, d, x[i+ 4], 6 , -145523070);
	d = md5_ii(d, a, b, c, x[i+11], 10, -1120210379);
	c = md5_ii(c, d, a, b, x[i+ 2], 15,  718787259);
	b = md5_ii(b, c, d, a, x[i+ 9], 21, -343485551);

	a = safe_add(a, olda);
	b = safe_add(b, oldb);
	c = safe_add(c, oldc);
	d = safe_add(d, oldd);
}
return Array(a, b, c, d);

}

/*
* These functions implement the four basic operations the algorithm uses.
*/
function md5_cmn(q, a, b, x, s, t)
{
return safe_add(bit_rol(safe_add(safe_add(a, q), safe_add(x, t)), s),b);
}
function md5_ff(a, b, c, d, x, s, t)
{
return md5_cmn((b & c) | ((~b) & d), a, b, x, s, t);
}
function md5_gg(a, b, c, d, x, s, t)
{
return md5_cmn((b & d) | (c & (~d)), a, b, x, s, t);
}
function md5_hh(a, b, c, d, x, s, t)
{
return md5_cmn(b ^ c ^ d, a, b, x, s, t);
}
function md5_ii(a, b, c, d, x, s, t)
{
return md5_cmn(c ^ (b | (~d)), a, b, x, s, t);
}

/*
* Add integers, wrapping at 2^32. This uses 16-bit operations internally
* to work around bugs in some JS interpreters.
*/
function safe_add(x, y)
{
var lsw = (x & 0xFFFF) + (y & 0xFFFF);
var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
return (msw << 16) | (lsw & 0xFFFF);
}

/*
* Bitwise rotate a 32-bit number to the left.
*/
function bit_rol(num, cnt)
{
return (num << cnt) | (num >>> (32 - cnt));
}

/*
* Convert a string to an array of little-endian words
* If chrsz is ASCII, characters >255 have their hi-byte silently ignored.
*/
function str2binl(str)
{
var bin = Array();
var mask = (1 << chrsz) - 1;
for(var i = 0; i < str.length * chrsz; i += chrsz)
	bin[i>>5] |= (str.charCodeAt(i / chrsz) & mask) << (i%32);
return bin;
}

/*
* Convert an array of little-endian words to a hex string.
*/
function binl2hex(binarray)
{
var hex_tab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef";
var str = "";
for(var i = 0; i < binarray.length * 4; i++)
{
	str += hex_tab.charAt((binarray[i>>2] >> ((i%4)*8+4)) & 0xF) +
		hex_tab.charAt((binarray[i>>2] >> ((i%4)*8  )) & 0xF);
}
return str;
}
/*md5 routines*/

/*rc4 routines*/
var sbox = new Array(255); 
var key = new Array(255);

function endecrypt(val, pwd)
{
	var i = 0;
	var j = 0;
	var cipher = '';

	rc4_init(pwd);

	for (var a = 1; a <= val.length; a++)
	{
		var itmp = 0;
		
		i = (i + 1) % 256;
		j = (j + sbox[i]) % 256;
		itmp = sbox[i];
		sbox[i] = sbox[j];
		sbox[j] = itmp;
		
		var k = sbox[(sbox[i] + sbox[j]) % 256];
		var ctmp = val.substr(a - 1, 1);
		
		itmp = ctmp.charCodeAt(0);
		
		var cipherby = itmp ^ k;
		
		cipher += String.fromCharCode(cipherby);
	}
	return cipher;
}

function rc4_init(pwd)
{
	var iLength = pwd.length;
	
	for (var a = 0; a <= 255; a++)
	{
		var ctmp = pwd.substr((a % iLength),1);
		
		key[a] = ctmp.charCodeAt(0);
		sbox[a] = a;
	}
	
	var x = 0;

	for (var b = 0; b <= 255; b++)
	{
		x = (x + sbox[b] + key[b]) % 256;
		
		var tempSwap = sbox[b];
		
		sbox[b] = sbox[x];
		sbox[x] = tempSwap;
	}
}
