// trims leading/trailing spaces, then returns length of field
function trimmedLength(fieldId)
{
	var s;
	var t;

	t = document.getElementById(fieldId);
	s = new String(t.value);
	while (s.charAt(0) == ' ')
	{
		s = s.substring(1, s.length);
	}
	while (s.charAt(s.length - 1) == ' ')
	{
		s = s.substring(0, s.length - 1);
	}
	
	t.value = s;
	return s.length;
}

function xmlhttpPost(strURL, strSubmit, strResultFunc)
{
	var xmlHttpReq = false;

	// Mozilla/Safari/Firefox/IE7
	if (window.XMLHttpRequest)
	{
		xmlHttpReq = new XMLHttpRequest();
		if (xmlHttpReq.overrideMimeType)
		{
			xmlHttpReq.overrideMimeType('text/xml');
		}
	}
	// IE5/IE6
	else if (window.ActiveXObject)
	{
		if (window.print)	// windows
		{
			xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
		}
		else				// mac
		{
			document.forms[0].submit();
			return;
		}
	}
	// something else, probably old
	else
	{
		document.forms[0].submit();
		return;
	}

	xmlHttpReq.open('POST', strURL, true);
	xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlHttpReq.onreadystatechange = function()
	{
		if (xmlHttpReq.readyState == 4)
		{
			if (strResultFunc != '')
			{
				eval(strResultFunc + '(xmlHttpReq.responseText);');
			}
		}
	}
	xmlHttpReq.send(strSubmit);
}

function imageReplace(theImage, theSrc)
{
	if(document.images)
	{
		document.images[theImage].src = theSrc;
	}
}


function addToLib(val,nLoc)
{
	xmlhttpPost('/users/cart_edit.asp', 'Function=add&nLoc=' + nLoc + '&BookId=' + val + '&rnd=' + (Math.random() * 1000), 'updateLibrary');
}

function updateLibrary(str)
{

	if (str != 'error')
	{
		if (str != 'empty')
		{
			var strMetaData = str.split('@@');
			var strPosition = strMetaData[1];
			var strCodes = strMetaData[0].split('|');
			if (strCodes.length > 1)
			{
				for (i = 0; i < strCodes.length; i++)
				{
					if (strCodes[i] != ' ')
					{
						// position 0 - top     1 - middle     2 - bottom 
						if (document.getElementById('addLib' + strCodes[i]) != null)
						{	
							switch (strPosition)
							{
							case 0 :
								document.getElementById('addLib' + strCodes[i]).className = 'grayBackDim optionsTop';
								break;
							case 1 :
								document.getElementById('addLib' + strCodes[i]).className = 'grayBackDim';
								break;
							case 2 :
								document.getElementById('addLib' + strCodes[i]).className = 'grayBackDim optionsBottom';
								break;
							}
							document.getElementById('addLib' + strCodes[i]).onclick = null;
							document.getElementById('addLibText' + strCodes[i]).innerHTML = 'Saved in Library';
							if (document.getElementById('addLibOption' + strCodes[i]) != null)
							{
								document.getElementById('addLibOption' + strCodes[i]).className = 'bookOptions dimText';
							}
						}
					}
				}
			}
			else
			{
				switch (strPosition)
				{
				case 0 :
					document.getElementById('addLib' + strCodes[0]).className = 'grayBackDim optionsTop';
					break;
				case 1 :
					document.getElementById('addLib' + strCodes[0]).className = 'grayBackDim';
					break;
				case 2 :
					document.getElementById('addLib' + strCodes[0]).className = 'grayBackDim optionsBottom';
					break;
				}
				document.getElementById('addLibText' + strCodes[0]).innerHTML = 'Saved in Library';
				document.getElementById('addLib' + strCodes[0]).onclick = null;
				if (document.getElementById('addLibOption' + strCodes[0]) != null)
				{
					document.getElementById('addLibOption' + strCodes[0]).className = 'bookOptions dimText';
				}
			}
		}
	}
}
