var selectedSidebarItem;
var pageNum = 0;
var book = "jsd";

function sidebarOver(id)
{
	if (id != selectedSidebarItem)
		document.getElementById(id).style.marginTop = "-43px";
}

function sidebarOut(id)
{
	if (id != selectedSidebarItem)
		document.getElementById(id).style.marginTop = "0";
}

function sidebarSelect(item)
{
	var id = 'img' + item;
	selectedSidebarItem = id;
	document.getElementById(id).style.marginTop = "-43px";
}

function buttonOver(id)
{
	document.getElementById(id).style.marginTop = "-20px";
}

function buttonOut(id)
{
	document.getElementById(id).style.marginTop = "0";
}

function setColor(id, color)
{
	document.getElementById(id).style.color = color;
}



function showBook(bookName)
{
	book = bookName;

	if (book == "jsd") {
		document.getElementById('bookPage').src = "images/jsd2_sample1.jpg";
		document.getElementById('bookCurView').innerHTML = "Currently viewing: <span class=\"booktitle\">Job Search Debugged</span>";
		pageNum = 0;
	}
	else {
		document.getElementById('bookPage').src = "images/nd_cover.jpg";
		document.getElementById('bookCurView').innerHTML = "Currently viewing: <span class=\"booktitle\">Networking Debugged</span>";
		pageNum = 0;
	}
}

function turnPage(dir)
{
	if (book == "jsd")
		turnPageJSD(dir);
	else if (book == "nd")
		turnPageND(dir);
}

function turnPageJSD(dir)
{
	var origPageNum = pageNum;

	if (dir == 'prev') {
		if (pageNum > 0)
			pageNum --;
	}
	else if (dir == 'next') {
		if (pageNum < 12)
			pageNum ++;
	}

	if (origPageNum != pageNum) {
		document.getElementById('bookPage').src = "images/jsd2_sample" + (pageNum+1) + ".jpg";
	}

}


function turnPageND(dir)
{
	var origPageNum = pageNum;

	if (dir == 'prev') {
		if (pageNum > 0)
			pageNum --;
	}
	else if (dir == 'next') {
		if (pageNum < 4)
			pageNum ++;
	}

	if (origPageNum != pageNum) {
		if (pageNum == 0)
			document.getElementById('bookPage').src = "images/nd_cover.jpg";
		else if (pageNum == 1)
			document.getElementById('bookPage').src = "images/nd_toc.jpg";
		else if (pageNum == 2)
			document.getElementById('bookPage').src = "images/nd_sample1.jpg";
		else if (pageNum == 3)
			document.getElementById('bookPage').src = "images/nd_sample2.jpg";
		else if (pageNum == 4)
			document.getElementById('bookPage').src = "images/nd_sample3.jpg";
	}

}

function validateEmailAndSend()
{
	var proceed = false;

	if (validateEmail(document.contact.email.value))
		proceed = true;
	else {
		alert("Please enter a valid email address.");
		document.contact.email.focus();
		return;
	}

	if (proceed)
		document.contact.submit();

	return false;
}
		
function validateAndSend()
{
	var proceed = true;

	if (proceed) {
		proceed = false;

		if (document.contact.name.value != "")
			proceed = true;
		else {
			alert("Please enter your name.");
			document.contact.name.focus();
			return;
		}
	}

	if (proceed) {
		proceed = false;

		if (validateEmail(document.contact.email.value))
			proceed = true;
		else {
			alert("Please enter a valid email address.");
			document.contact.email.focus();
			return;
		}
	}

	if (proceed) {
		proceed = false;

		if (document.contact.phone.value != "")
			proceed = true;
		else {
			alert("Please enter a phone number.");
			document.contact.email.focus();
			return;
		}
	}

	if (proceed) {
		proceed = false;

		if (document.contact.subject.value != "")
			proceed = true;
		else {
			alert("Please enter a subject.");
			document.contact.subject.focus();
			return;
		}
	}

	if (proceed) {
		proceed = false;

		if (document.contact.message.value != "")
			proceed = true;
		else {
			alert("Please enter a message.");
			document.contact.message.focus();
			return;
		}
	}

	if (proceed)
		document.contact.submit();

	return false;
		
}

function validateEmail(address)
{
	var emailstring = address;
	var ampIndex = emailstring.indexOf("@");
	var afterAmp = emailstring.substring((ampIndex + 1), emailstring.length);
		// find a dot in the portion of the string after the ampersand only
	var dotIndex = afterAmp.indexOf(".");
		// determine dot position in entire string (not just after amp portion)
	dotIndex = dotIndex + ampIndex + 1;
		// afterAmp will be portion of string from ampersand to dot
	afterAmp = emailstring.substring((ampIndex + 1), dotIndex);
		// afterDot will be portion of string from dot to end of string
	var afterDot = emailstring.substring((dotIndex + 1), emailstring.length);
	var beforeAmp = emailstring.substring(0,(ampIndex));
		//old regex did not allow subdomains and dots in names
		//var email_regex = /^[\w\d\!\#\$\%\&\'\*\+\-\/\=\?\^\_\`\{\|\}\~]+(\.[\w\d\!\#\$\%\&\'\*\+\-\/\=\?\^\_\`\{\|\}\~])*\@(((\w+[\w\d\-]*[\w\d]\.)+(\w+[\w\d\-]*[\w\d]))|((\d{1,3}\.){3}\d{1,3}))$/;
	var email_regex = /^\w(?:\w|-|\.(?!\.|@))*@\w(?:\w|-|\.(?!\.))*\.\w{2,3}/ 
		// index of -1 means "not found"
	if ((emailstring.indexOf("@") != "-1") &&
		(emailstring.length > 5) &&
		(afterAmp.length > 0) &&
		(beforeAmp.length > 1) &&
		(afterDot.length > 1) &&
		(email_regex.test(emailstring)) ) {
		return true;
	} else {
		return false;
	}

}

function updateCart()
{
	document.cart.submit();
	return false;
}

