var author_num = 0;
var quote_num = 0;
var image_num = 0;
var left_author_num = 0;

function showSpecialTopics(id)
{
	//alert("sp: " + id);
	for(i=1;i<=document.getElementById('catalog').length;i++)
	{
		document.getElementById('catalog_' + i).style.visibility = 'hidden';
		document.getElementById('catalog_' + i).style.position = 'absolute';
	}
	document.getElementById('catalog_' + id).style.position = 'relative';
	document.getElementById('catalog_' + id).style.visibility = 'visible';
	
	document.getElementById('hidden').innerHTML = "<input type='hidden' name='selIndex' value='" + (document.getElementById('catalog').selectedIndex + 1) + "'>";
}

function showCatalog(id)
{
	//alert("catalogid: " + id);
	document.getElementById('catalog').selectedIndex = id - 1;
	showSpecialTopics(id);
}

function doConfirm(path,itemname)
{
	if(itemname == undefined)
	{
		itemname = "this item";
	}
	else
	{
		itemname = itemname;
	}
	
	if(confirm('Are you sure you would like to delete: ' + itemname + '?'))
	{
		document.location = path + "&title=" + itemname;
	}	
}

function addAuthorDiv()
{
	author_num ++;
	
	document.getElementById('add_author').style.visibility = "visible";
	document.getElementById('add_author').style.width = "936px";
	
	var adiv = document.getElementById('add_author');
	
	dvhtml = "<div id='author_wrap" + author_num + "' class='author_wrap'>";
	dvhtml += "<div class='form_left'>Author:<br/><a href='javascript: delAuthorDiv(" + author_num + ");' class='smallgreylink'>remove</a></div> ";
	dvhtml += "<div  class='form_right_wide'><div class='form_internal'><input type='text' size='35' maxlength='200' name='author_name[]'></div> ";
	dvhtml += "<div class='form_internal'>Primary Author: <select name='author_primary[]'><option value='1'>Yes</option><option value='0' selected>No</option></select></div>";
	dvhtml += "<div class='form_internal'>Author In Index: <select name='author_index[]'><option value='1'>Yes</option><option value='0'>No</option></select></div></div>";
	dvhtml += "<div id='add_author_space' class='clear' style='height:3px;width:10px;'>&nbsp;</div>";
	dvhtml += "</div>";
		
	var newDiv = document.createElement('div');
	var newDivId = 'authDiv' + author_num;
	newDiv.setAttribute('id',newDivId);
	newDiv.innerHTML = dvhtml;
	
	adiv.appendChild(newDiv);
}

function delAuthorDiv(id)
{
	document.getElementById('author_wrap' + id).innerHTML = "";
}

function addQuoteDiv()
{
	quote_num ++;
	
	document.getElementById('add_quote').style.visibility = "visible";
	document.getElementById('add_quote').style.width = "879px";
	
	var qdiv = document.getElementById('add_quote');
	
	quotehtml = "<div id='quote_wrap" + quote_num + "' class='author_wrap'>";
	quotehtml += "<div class='form_left'>Quote:<br/><a href='javascript: delQuoteDiv(" + quote_num + ");' class='smallgreylink'>remove</a></div> <div  class='form_right_wide'><textarea rows='8' cols='93' name='quote[]'></textarea></div>";
	quotehtml += "<div id='add_quote_space' class='clear' style='height:3px;width:10px;'>&nbsp;</div>";
	quotehtml += "</div>";
	
	var quoteDiv = document.createElement('div');
	var quoteDivId = 'quoteDiv' + quote_num;
	quoteDiv.setAttribute('id',quoteDivId);
	quoteDiv.innerHTML = quotehtml;
	
	qdiv.appendChild(quoteDiv);
}

function delQuoteDiv(id)
{
	document.getElementById('quote_wrap' + id).innerHTML = "";
}

function addImageDiv()
{
	image_num ++;
	
	document.getElementById('add_addimage').style.visibility = "visible";
	document.getElementById('add_addimage').style.width = "879px";
	
	var idiv = document.getElementById('add_addimage');
	
	addimagehtml = "<div id='addimage_wrap" + image_num + "' class='author_wrap'>";
	addimagehtml += "<div class='form_left'>Image:<br/><a href='javascript: delImageDiv(" + image_num + ");' class='smallgreylink'>remove</a></div> <div  class='form_right_wide'><input type='file' name='addimage" + image_num + "' id='addimage" + image_num + "'></div>";
	addimagehtml += "<div id='add_image_space' class='clear' style='height:3px;width:10px;'>&nbsp;</div>";
	addimagehtml += "</div>";
	
	var addimageDiv = document.createElement('div');
	var addimageDivId = 'addimageDiv' + image_num;
	addimageDiv.setAttribute('id',addimageDivId);
	addimageDiv.innerHTML = addimagehtml;
	
	idiv.appendChild(addimageDiv);
}

function delImageDiv(id)
{
	document.getElementById('addimage_wrap' + id).innerHTML = "";
}

function checkSpecialTopics(catalogid,special_count)
{
	var catalogChecked = document.getElementById('checkCat' + catalogid).checked;
	if(catalogChecked == false)
	{
		for(i=0;i<special_count;i++)
		{
			document.getElementById('sp_' +  catalogid + '_' + i).checked = 0;
		}
	}
}

function manageSpecialTopicsChecks(catalogid,specialid)
{
	var catalogChecked = document.getElementById('checkCat' + catalogid).checked;
	if(catalogChecked == false)
	{
		document.getElementById('sp_' + catalogid + '_' + specialid).checked = false;
	}
}

function manageFeatured(id,amount)
{
	alert(id + " " + amount);
}

function manageForm()
{
	document.forms[0].title.value = escape(document.forms[0].title.value);
}

function manageImageSwap(imagename,elementid,total,bookid,imageid)
{
	for(i=1;i<=total;i++)
	{
		document.getElementById('shadowbox' + i).style.border = "solid 1px #333333";
	}
	//document.images['controlimage'].src = "images/book_images/size2/" + imagename;
	document.getElementById('controlimage').innerHTML = "<a href='book_image.php?id=" + bookid + "&iid=" + elementid + "&hid=" + imageid + "' class='title_landing_link' target='_blank'><img src='images/book_images/size2/" + imagename + "' border='0' name='controlimage'></a>";
	document.getElementById('shadowbox' + elementid).style.border = "solid 2px #ee6344";
}

function manageImageSwapBig(imagename,elementid,total,bookid)
{
	for(i=1;i<=total;i++)
	{
		document.getElementById('shadowbox' + i).style.border = "solid 1px #333333";
	}
	//document.images['controlimage'].src = "images/book_images/size2/" + imagename;
	document.getElementById('controlimage').innerHTML = "<img src='images/book_images/size0/" + imagename + "' border='0' name='controlimage'>";
	document.getElementById('shadowbox' + elementid).style.border = "solid 2px #ee6344";
}

function validateBooktosell()
{
	var docstring = document.useradd;
	
	if(docstring.author.value.length < 1)
	{
		alert("Please enter the author of the book.");
		docstring.author.focus();
		return false;
	}
	else if(docstring.title.value.length < 1)
	{
		alert("Please enter the title of the book.");
		docstring.title.focus();
		return false;
	}
	else if(docstring.useremail.value.length < 1)
	{
		alert("Please your email address.");
		docstring.useremail.focus();
		return false;
	}
	else
	{
		return true;
	}
}

function validateWishlist()
{
	var docstring = document.useradd;
	
	if(docstring.useremail.value.length < 1)
	{
		alert("Please enter your email address.");
		docstring.useremail.focus();
		return false;
	}
	else
	{
		return true;
	}
}

function validatePassword()
{
	var docstring = document.userpassadd;
	
	if(docstring.userpass.value.length < 1)
	{
		alert("Please enter a password.");
		docstring.userpass.focus();
		return false;
	}
	else if(docstring.userpass_re.value.length < 1)
	{
		alert("Please enter your password again.");
		docstring.userpass_re.focus();
		return false;
	}
	else if(docstring.userpass_re.value != docstring.userpass.value)
	{
		alert("Your passwords do not match.");
		docstring.userpass.focus();
		return false;
	}
	else
	{
		return true;
	}
}

function validateBookUser()
{
	var docstring = document.userlogin;
	
	if(docstring.useremail.value.length < 1)
	{
		alert("Please enter your email address.");
		docstring.useremail.focus();
		return false;
	}
	else if(docstring.userpass.value.length < 1)
	{
		alert("Please enter your password.");
		docstring.userpass.focus();
		return false;
	}
	else
	{
		return true;
	}
}

function validateEmail()
{
	var docstring = document.forgot;
	
	if(docstring.emailforgot.value.length < 1)
	{
		alert("Please enter your email address.");
		docstring.emailforgot.focus();
		return false;
	}
	else
	{
		return true;
	}
}

function validateCatalog()
{
	var docstring = document.usercatalog;
	
	if(docstring.userfname.value.length < 1)
	{
		alert("Please enter your first name.");
		docstring.userfname.focus();
		return false;
	}
	else if(docstring.userlname.value.length < 1)
	{
		alert("Please enter your last name.");
		docstring.userlname.focus();
		return false;
	}
	else if(docstring.useremail.value.length < 1)
	{
		alert("Please enter your email address.");
		docstring.useremail.focus();
		return false;
	}
	else if(docstring.useraddr1.value.length < 1)
	{
		alert("Please enter your mailing address.");
		docstring.useraddr1.focus();
		return false;
	}
	else if(docstring.usercity.value.length < 1)
	{
		alert("Please enter your city.");
		docstring.usercity.focus();
		return false;
	}
	else if(docstring.userstate.selectedIndex == 0)
	{
		alert("Please enter your state.");
		docstring.userstate.focus();
		return false;
	}
	else if(docstring.userzip.value.length < 1)
	{
		alert("Please enter your zip code.");
		docstring.userzip.focus();
		return false;
	}
	else
	{
		return true;
	}
}

function manageLeftAuthors(id)
{
	for(i=0;i<authorLetNums.length;i++)
	{
		document.getElementById('authnav' + authorLetNums[i]).style.visibility = "hidden";
		document.getElementById('authnav' + authorLetNums[i]).style.position = "absolute";
		
		document.getElementById('letnum' + authorLetNums[i]).style.color = "#282828";
	}
	if(id != -1)
	{
		document.getElementById('authnav' + id).style.visibility = "visible";
		document.getElementById('authnav' + id).style.position = "relative";
		document.getElementById('letnum' + id).style.color = "#a24b41";
		left_author_num = id;
	}
}

function showLeftNav(id)
{
	document.getElementById('lefttopicsauths0').style.visibility = 'hidden';
	document.getElementById('lefttopicsauths0').style.position = 'absolute';
	
	document.getElementById('lefttopicsauths1').style.visibility = 'hidden';
	document.getElementById('lefttopicsauths1').style.position = 'absolute';
	
	document.getElementById('ln0').style.color = '#717171';
	document.getElementById('ln1').style.color = '#717171';
	document.getElementById('ln2').style.color = '#717171';
	
	document.getElementById('ln0').style.textDecoration = 'none';
	document.getElementById('ln1').style.textDecoration = 'none';
	document.getElementById('ln2').style.textDecoration = 'none';
	
	if(id != 2)
	{
		document.getElementById('lefttopicsauths' + id).style.visibility = 'visible';
		document.getElementById('lefttopicsauths' + id).style.position = 'relative';
	}
	document.getElementById('ln' + id).style.color = '#282828';
	
	if(id == 1)
	{
		change_auth = left_author_num
	}
	else
	{
		change_auth = -1;
	}
	manageLeftAuthors(change_auth);
}

function submitEnews()
{
	if(document.enewsletter.enewsletter_email.value.indexOf('@') == -1 || document.enewsletter.enewsletter_email.value.indexOf('.') == -1)
	{
		alert("In order to receive our enewsletter, please enter a valid email address.");
	}
	else
	{
		document.enewsletter.submit();
	}
}

function validateEnews()
{
	if(document.enewsletter.enewsletter_email.value.indexOf('@') == -1 || document.enewsletter.enewsletter_email.value.indexOf('.') == -1)
	{
		alert("In order to receive our enewsletter, please enter a valid email address.");
		return false;
	}
	else
	{
		return true;
	}
}

function fillText(message,formname,fieldname)
{
	if(document[formname][fieldname].value.length == 0)
	{
		document[formname][fieldname].value = message;
	}
}

function confirmNews()
{
	if(confirm('Are you sure that you are ready to send the enewsletter?'))
	{
		return true;
	}
	else
	{
		return false;
	}
}

function checkBookSelected(bookcount)
{
	var isbookselected = 0;
	for(i=0;i<bookcount;i++)
	{
		if(document.getElementById('bookinput' + i).checked)
		{
			//alert('bookchecked');
			isbookselected = 1;
		}
	}
	if(isbookselected == 1)
	{
		return true;
	}
	else
	{
		return false;
	}	
}

function selectDeselect(option,arrlength)
{
	for(i=0;i<arrlength;i++)
	{
		if(option == 1)
		{
			document.getElementById('bookinput' + i).checked = true;
		}
		else
		{
			document.getElementById('bookinput' + i).checked = false;
		}
	}
}





