var urlStart = location.href.substr(0, location.href.lastIndexOf("/")) + "/";		// urlStart + "filename.html");
var urlStartBookID = location.href.substr(0, location.href.lastIndexOf("/")) + "/" + bookId + "/";		// urlStart + "filename.html");
var expiredAccount;


// Set this so that opener.username will have a value in the supplemental window.
var username = "loginwindow";

function UnloadLogin() {
	// Close the suppwin if open
	if (suppwin != null && suppwin.close != null) {
		suppwin.close();
	}
}

function LoginOK() {
	var username;
	if (document.loginForm.username.value == '' || document.loginForm.password.value == '') {
		alert("You must enter a valid username and password to enter the site.");
		return false;

		if (confirm('You didn\'t enter a username.  Would you like to log in as a Guest (Preview mode)?')) {
			username = "Guest";
			document.loginForm.username.value = "Guest";
		} else {
			document.loginForm.username.select();
			return false;
		}
	} else {
		username = document.loginForm.username.value;
	}
	
	if (username != 'Guest' && username != 'guest' && document.loginForm.password.value == '') {
		alert("You must enter a valid username and password to enter the site.");
		return false;

		if (confirm('You didn\'t enter a password.  Would you like to log in as a Guest (Preview mode)?')) {
			username = "Guest";
			document.loginForm.username.value = "Guest";
		} else {
			document.loginForm.password.select();
			return false;
		}
	}
	
	var passfield = document.loginForm.password.value;

	if (passfield.search(/<|>/) > -1) {
		alert("That password contains illegal characters.  If you need assistance logging in please contact techsupport at (800)-936-6899 to have this corrected.");
		return false;
	}
	
	// alert("The site is not available at this time. It will be back online by 5:00 AM on Friday August 24.");
	// return false;
	
	// store the user's username and siteVersion
	StoreCookie("lfiofoe", username);
	StoreCookie(bookId + "siteVersion", siteVersion);		
	return true;
}

function LoginFromReg(u, p) {
	document.loginForm.username.value = u;
	document.loginForm.password.value = p;
	LoginOK();	// stores the username cookie
	document.loginForm.submit();
	suppwin.close();
}

var suppwin;
function ShowSupp(page) {
//	alert('This function is temporarily unavailable. It will be available by 3:45 AM on Wednesday 9/12.');
//	return;

	if (page == 'register') {
		page = 'https://purchase.bfwpub.com/purchase/register/register.php?productId=' + bookId + "_p";
                if (bookId == 'elrod2') {
			page = 'https://purchase.bfwpub.com/purchase/register/register.php?productId=' + bookId + "_p&gco=1";
                }
	} else if (page == 'purchase') {
		page = "https://purchase.bfwpub.com/purchase/transaction/transaction.php?productId=" + bookId + "_p";
	} else if (page == 'instructoraccess') {
		page = "https://purchase.bfwpub.com/purchase/instructorreg_ra.php?productId=" + bookId + "_p";
	} else if (page == "passrem") {
		page = "https://purchase.bfwpub.com/purchase/password_reminder/password_reminder.php";
	} else if (page == "adopt") {
		// alert('Ability to adopt from here is coming soon...');
		page = "adopt_form/adopt_form.php?bookId=" + bookId;
	} else if (page == 'switch_course') {	// https://purchase.bfwpub.com/purchase
		page = "https://purchase.bfwpub.com/purchase/portal_switch_course/portal_switch_course.php?productId=" + bookId + "_p";
	}
	
	suppwin = window.open(page, "loginSuppWindow", "top=20,left=20,width=800,height=620,menubar,resizable,scrollbars,status", true);
}

function ShowRefundPolicy(bookId) {
	ShowSupp('loginfiles/refund.php?bookId=' + bookId);
}

function RequestInstructorAccess() {
	if (location.search.indexOf("onyx") > 0) {	// (bookId == "life8e") {
		ShowSupp('instructoraccess');
		return false;
	} else if (instructor_access_flag == 1){
		ShowSupp('instructoraccess');
		return false;
	} else {
		return confirm('To request instructor access, you should send an email to ' + instRequestEmail + ' stating your name, email address, and institution. Would you like to send this email now?');
	}
}

function ShowPreviewPrompt() {
	SetDisplay(document.getElementById('previewPromptDiv'), 'block');
}

function HidePreviewPrompt() {
	SetDisplay(document.getElementById('previewPromptDiv'), 'none');
}

// The supp window calls the following two functions upon open, thinking it
// is being opened by the textbook window.
function AddToHistory() {
	;
}

function CurrentChapter() {
	return 0;
}



function UpdateHistoryLinks() {
	// Mimic ebookAPI's KeepSuppWindowOpen function
	suppwin.banner.document.getElementById('keepopen').style.display = "none";
	suppwin.banner.document.getElementById('backvis').style.display = "none";
	suppwin.banner.document.getElementById('forwardvis').style.display = "none";
	suppwin.banner.document.getElementById('backdim').style.display = "none";
	suppwin.banner.document.getElementById('forwarddim').style.display = "none";
	suppwin.banner.document.getElementById('helpDiv').style.display = "none";
}

var showPrompt = false;
function InitializeLogin() {
	FillinUsername();
	
        if(expiredAccount){
                ConfirmRenewAccount(bookId);
        }

	if (showPrompt) {
		setTimeout("FadeTo('promptDiv', 90, 1000)", 1000);
	}
}


function FillinUsername() {
	var username = GetCookieVal("lfiofoe");
	if (username != null) {
		document.loginForm.username.value = username;
	}
	
	// document.loginForm.username.select();
	self.focus();	// Not sure if this is necessary, but got it from http://www.htmlgoodies.com/tabindex.html
	document.loginForm.username.focus();
}

function ConfirmRenewAccount(bookId) {
        if(confirm("Your account has expired.  You can extend your subscription of " + shortTitle + " for a discounted price. Please click OK if you would like more information.")){
		purch = window.open("https://purchase.bfwpub.com/purchase/transaction/transaction.php?productId="+ bookId +"_p"+"&extendAccess=1&email=" + usr,"purch","width=630,height=450,menubar,resizable,scrollbars,status",true);
        } else {
                window.location.replace("http://courses.bfwpub.com/" + bookId + ".php");
        }
         
}


function StoreCookie(name, val) {
	var later = new Date();
	later.setFullYear (later.getFullYear() + 10);
	document.cookie = name + "=" + val + "; path=/; expires=" + later.toGMTString();
}

function GetCookieVal(name) {
	var cook = document.cookie;					// get the cookie
	var pos = cook.indexOf (name + '=');	// look for the named field
	if (pos != -1) {		// if found
		var start = pos + name.length + 1;
		var end = cook.indexOf(";", start);
		if (end == -1) end = cook.length;
		return cook.substring(start, end);
	} else {
		return null;
	}
}


function SetVisibility(l, v) {
	if (l != null) {
		if (v == null) {
			v = (l.style.visibility == "hidden") ? "visible" : "hidden";
		}
		l.style.visibility = v;
	}
}

function SetDisplay(l, d) {
	if (l != null) {
		if (d == null) {
			d = (l.style.display == "block") ? "none" : "block";
		}
		l.style.display = d;
	}
}

function ShowInstructorOptions() {
	SetDisplay(document.getElementById("studentOptions"), "none");
	SetDisplay(document.getElementById("instructorOptions"));
}

function ShowStudentOptions() {
	SetDisplay(document.getElementById("instructorOptions"), "none");
	SetDisplay(document.getElementById("studentOptions"));
}

// Log the user in as a guest
function Preview() {
	document.loginForm.username.value = "angelpreview@bfwpub.com";
	document.loginForm.password.value = "guest";
	document.loginForm.submit();
}

var siteVersion = "";
function SwitchSiteVersion(switchTo) {
	siteVersion = switchTo;
	document.loginForm.siteVersion.value = switchTo;
	
	// Store the version in a coookie
	StoreCookie(bookId + "siteVersion", siteVersion);		

	
	if (siteVersion == "eBook") {
		SetDisplay(document.getElementById("bcsVersionSwitcher"), "none");
		SetDisplay(document.getElementById("bcsOptions"), "none");
		SetDisplay(document.getElementById("eBookVersionSwitcher"), "block");
		SetDisplay(document.getElementById("eBookOptions"), "block");
	} else {
		SetDisplay(document.getElementById("bcsVersionSwitcher"), "block");
		SetDisplay(document.getElementById("bcsOptions"), "block");
		SetDisplay(document.getElementById("eBookVersionSwitcher"), "none");
		SetDisplay(document.getElementById("eBookOptions"), "none");
	}	
}
