function doDataChecks() {
	alertReusedIndex();
}

function resizeThisWindow() {

	var thisHeight = document.getElementById('outerTable').offsetHeight;
	var thisWidth = document.getElementById('outerTable').offsetWidth;

	if ((thisWidth - 0) < 500) {
		thisWidth = 500;
	}

	if (((thisWidth -0) > 250) && ((thisHeight-0) > 200)) {

		if (thisWidth >= screen.width) {
			thisWidth = (screen.width - 5);
		} else {
			thisWidth = thisWidth + 150;
		}

		if (thisHeight >= (screen.height - 50)) {
			thisHeight = (screen.height - 50);
		} else {
			thisHeight = thisHeight + 74;
		}

		window.resizeTo(thisWidth, thisHeight);
	} else {
	}
}

//global vars for the itemInfoArray

var itemContextInfoArray = new Array();

var thisItemIndexNumber = null;	
var thisItemImageURL = null;	
var thisItemName = null;	
var thisItemPartNumber = null;	
var thisItemPrice = null;	
var thisItemDescription = null;	
var thisItemPotSize = null;	
var thisItemSold = null;

var usedIndexString = '';

function alertReusedIndex() {
	if (usedIndexString != null && usedIndexString != '') {
		theAlert = 'Multiple items have itemIndexNumber' + ((usedIndexString.indexOf('|') == -1)?'':'s') + ' of ' + 
			       replace(usedIndexString, '|', ', ') + ' assigned to them.\r\n' +
				   'The next available itemIndexNumber is ' + getHighestItemIndex() + '.\r\n' +
				   'Make sure itemIndexNumbers are only used once per page.';
		alert(theAlert);
	}

}

function getHighestItemIndex() {
	return(itemContextInfoArray.length);
}


//since the items don't necessarily insert in order
//we need to keep track of the last item inserted
var lastItemInserted = '';
var currItemInserted = '';

//and a flag to catch the first call of this to write the
//the first letter of the list (even if it isn't A)
var firstItem = true;
var writeFirstLetter = false;

function setItemInfoArrayAndWriteRow() {
	lastItemInserted = currItemInserted;
	currItemInserted = replace(thisItemImageURL, ' ', '%20') + '|' + thisItemName + '|' + thisItemPartNumber + '|' + thisItemPrice + '|' + thisItemPotSize + '|' + thisItemDescription + '|' + (!thisItemSold?'AVAIL':'SOLD');
	var thisInd = itemContextInfoArray.length;
	itemContextInfoArray[itemContextInfoArray.length] = currItemInserted;
	//alert(itemContextInfoArray.length);
	if (firstItem) {
		firstItem = false;
		writeFirstLetter = true;
	} else {
		writeFirstLetter = false;
	}

	if (thisItemPotSize != 'SUPPLY') {
		writeQNav(thisInd);
	}
	writePlantItemRow(thisInd);
}

var itemWindow = null;

function getItemPage(thisIndex) {

	theItemInfo = itemContextInfoArray[thisIndex].split('|');

	theItemImageURL = theItemInfo[0];
	theItemName = theItemInfo[1];
	theItemPartNumber = theItemInfo[2];
	theItemPrice = replace(theItemInfo[3], '$', '');
	theItemPotSize = replace(theItemInfo[4], '"', '');
	theItemDescription = theItemInfo[5];
	theItemSold = theItemInfo[6];

	theAlert = 'Link testing is on!!!\r\n\r\nImage URL = \'' + theItemImageURL + '\'\r\n\r\nItem Name = \'' + 
				theItemName + '\'\r\n\r\nItem Sold = ' + (theItemSold == 'SOLD'?'YES':'NO') + '\r\n\r\nItem Part Number = \'' + theItemPartNumber + 
				'\'\r\rItem Price = $' + theItemPrice + '\r\n\r\nItem Description = "' + theItemDescription + '"\r\n\r\n' +
				'Item Pot Size = ' + theItemPotSize + '"\r\n\r\n' +
				'To disable link testing add two slashes "//" at the beginning of the line "linkTesting = true;" near the top of this page\'s html code.';
 
	if (linkTesting == true) {
		alert(theAlert);
	} else {
		itemWindow = window.open('', 'thisItemWindow', 'resizable=1,scrollbars=1,status=1,width=600,height=500');		
		itemWindow.focus();
		writeItemWindow(itemWindow, thisIndex);
	}

}

//qNav is a function to write the quickNav anchors
//for the alphabet listing at the top of the list pages

function writeQNav(currentIndex) {

	var theAnchor = '';
	var thisLet = thisItemName.charAt(0).toLowerCase();
	
	//if this is the first item of the list
	//just write whatever thr first letter of this item name is
	if (writeFirstLetter) {
		theAnchor = '<tr><td style="padding-bottom:10px;"><a name="' + thisLet + '_anchor" id="' + thisLet + '_anchor"></a><div class="jumpLet">';
				theAnchor = theAnchor + '<span class="qNavLet">' + thisLet.toUpperCase() + '</span>';			
				theAnchor += '</div></td><td class="fH"><table HEIGHT="100%"><tr><td ALIGN="CENTER">&nbsp;</td></tr></table></td></tr>';

	} else {
	
		//if the first letter of this itemname is different
		//from the last item inserted, create an anchor for that letter
		//othwerwise, do nothing.
		var prevName = '';		
		var prevLet = '';		
		if (currentIndex > 0 && lastItemInserted != null && lastItemInserted != '') {
			prevName = lastItemInserted.split('|')[1];			
			prevLet = prevName.charAt(0).toLowerCase();
			if (thisLet != prevLet) {
				theAnchor = '<tr><td style="padding-bottom:10px;"><a name="' + thisLet + '_anchor" id="' + thisLet + '_anchor"></a><div class="jumpLet">';
				theAnchor = theAnchor + '<span class="qNavLet">' + thisLet.toUpperCase() + '</span>';			
				theAnchor += '</div></td><td class="fH"><table class="fH"><tr><td ALIGN="CENTER">' + writeToTop() + '</td></tr></table></td></tr>';
			}
		}
	}

	document.write(theAnchor);
}

function writeToTop() {
	var theTopAnchor = '<a class="toTop" style="left:100px;top:-6px;" href="#theTop">return to top</a>'
	return(theTopAnchor);
}

var theAlph = 'abcdefghijklmnopqrstuvwxyz'

function writeTopQuickNav() {
	var theTopQNav = '<div style="width:660px;margin-top:5px;"><img src="images/qNav.gif" style="position:relative;margin-right: 5px;float:left;top:-5px;"><div style="position: relative; top: -5px;float:left;">';

	for (i=0;i<theAlph.length ; i++) {
		
		var thisLet = theAlph.charAt(i)

		if (document.getElementById(thisLet + '_anchor')) {
			theTopQNav += '<div class="actNavLetDiv"><a href="#' + thisLet + '_anchor">' + thisLet.toUpperCase() + '</a></div>';
		} else {
			theTopQNav += '<div class="inactNavLetDiv"><span>' + thisLet.toUpperCase() + '</span></div>';
		}

	}

	theTopQNav += '</div></div>';

	//and add the print features if they have been requested

	var printFeatures = '';

	if (document.location.href.indexOf('file:///') != -1 ) {
		printFeatures = '<div style="width:660px;padding-top:10px;padding-bottom:10px;" WIDTH="1">' +
						'<table>' +
						'<tr>' +
						'<td style="color:#fff;font-size:14px;font-weight:bold;">Print&nbsp;List:</td>' +
						'<td WIDTH="80" ALIGN="CENTER"><a class="toTop" href="#null" onClick="printList(\'SOLD\');">Print SOLD</a></td>' +
						'<td WIDTH="80" ALIGN="CENTER"><a class="toTop" href="#null" onClick="printList();">Print All</a></td>' +
						'</tr>' +
						'</table>' +
						'</div>'
	}

	document.getElementById('topNav').innerHTML = printFeatures + theTopQNav;
}

var noBlur = false;

var imageLinks = '';
var refreshImageLinks = '';

function writeItemWindow(itemWindow, thisIndex) {

	theItemInfo = itemContextInfoArray[thisIndex].split('|');

	var nextItemIndex = -1;
	var prevItemIndex = -1;
	var nIName = '';
	var pIName = '';

	theItemImageURL = theItemInfo[0];
	theItemName = theItemInfo[1];
	theItemPartNumber = theItemInfo[2];
	theItemPrice = replace(theItemInfo[3], '$', '');
	theItemPotSize = replace(theItemInfo[4], '"', '');
	theItemDescription = theItemInfo[5];
	theItemSold = theItemInfo[6];

	var ind = 0;

	if (thisIndex + 1 < itemContextInfoArray.length && itemContextInfoArray[thisIndex + 1] != null) {

		ind = thisIndex + 1;
		if (ind < itemContextInfoArray.length) {
			nextItemIndex = ind;
			nIName = truncName(itemContextInfoArray[nextItemIndex].split('|')[1], 20);	
		}		
	}

	if (thisIndex - 1 > -1 && itemContextInfoArray[thisIndex - 1] != null) {

		ind = thisIndex - 1;
		if (ind < itemContextInfoArray.length) {
			prevItemIndex = ind;
			pIName = truncName(itemContextInfoArray[prevItemIndex].split('|')[1], 20);	
		}
	}	

	var thisDescriptionString = ''
	if (theItemDescription != null && theItemDescription.length > 0)
	{
		thisDescriptionString = '<span style="text-align: left; color: #F2DFC3;" ALIGN="LEFT"><b><u>Item Info & Origin:</u></b> ' + theItemDescription + '</span><br><img src="images/blank.gif" WIDTH="1" HEIGHT="5"><br>';

	}

	var qtyValue = 0;
	if (theItemSold != 'SOLD')
	{
		qtyValue = 1;
	}

	imageLinks = '';
	refreshImageLinks = '';
	var linkCount = 0;

	theItemImageURL = Trim(theItemImageURL);

	if (theItemImageURL.indexOf('!') != -1)
	{		

		theseItemImages = theItemImageURL.split('!');


		theItemImageURL = '';		

		for (i=0; i < theseItemImages.length; i++)
		{
			if (theseItemImages[i] != null && Trim(theseItemImages[i]).length > 0)
			{

				//and assign the itemImage to the first valid image URL
				if (theItemImageURL == '')
				{
					theItemImageURL = Trim(theseItemImages[i]);
				}

				linkCount ++;
				imageLinks = imageLinks + '<a class="iII" href="javascript:swapImage(\'' + Trim(theseItemImages[i]) + '\', ' + linkCount + ')" id="imgLink' + linkCount + '">Image ' + linkCount + '</a>';
			}			
		}

		//and set the refresh value
		refreshImageLinks = imageLinks + '';
		imageLinks = '<div class="iInD"><span ALIGN="LEFT" class="imagesCnt"><b>' + linkCount + ' Images:</b></span><span id="imgLinksSpan">' + imageLinks + '</span><br></div>';		
	}

	theWindowHTML = '<html><head><title>' + theItemName + ' Information Page</title><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">' +
	'<link rel="shortcut icon" href="favicon.ico"><link rel="stylesheet" type="text/css" href="site.css" />' +
	'<script src="itemInfo.js" language="JavaScript1.2" type="text/javascript"><' + '/script>' + 
	'</head><body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0" onLoad="window.focus();resizeThisWindow();closeAfterLeavingSite();" onUnload="if (window.opener && !noBlur) {window.opener.focus()}" style="padding-top:5px;padding-left:5px;">' +
	'<form action="http://www.out-of-africa-plants.com/cgi-bin/cart32.exe/OOA-AddItem" method=POST name=Form ' + (theItemSold == 'SOLD'?'onSubmit="alert(\'This item has been sold.\');return(false);"':'') + '><input type="hidden" name="Referer" value="javascript:opener.focus();window.close()">' +
	'<table cellpadding="0" cellspacing="0" border="0" WIDTH="1" id="outerTable"><tr><td WIDTH="135" VALIGN="TOP" ALIGN="CENTER"><img src="images/navLogo.gif" HEIGHT="75" border="0"><br>' +	
	(prevItemIndex != -1?'<div id="pItem" style="padding-top:10px;">' + pIName + '</div><a class="itemNav" style="padding-top:5px;" href="javascript:opener.getItemPage(' + prevItemIndex + ')">< prev Item</a>':'') +
	'<div style="height:1px;width:90%;background-color:#F2DFC3;margin-top:3px;margin-bottom:3px;"><img src="images/blank.gif" WIDTH="1" HEIGHT="1"></div>' +
	(nextItemIndex != -1?'<a class="itemNav" style="padding-bottom:5px;" href="javascript:opener.getItemPage(' + nextItemIndex + ')">next Item ></a><div id="nItem">' + nIName + '</div>':'') +
	'<a href="javascript:window.opener.focus();" class="toTop" style="width:90px;margin-top:30px;">return&nbsp;to&nbsp;list</a>' +
	'</td><td WIDTH="15"><img src="images/blank.gif" WIDTH="15" HEIGHT="1"></td><td WIDTH="100%" VALIGN="TOP" ALIGN="CENTER">' +
	'<table cellpadding="0" cellspacing="0" border="0" WIDTH="100%">' +
	'<tr><td COLSPAN="3">' +
	'<div class="iIN">' + theItemName + '</div>' + '</td></tr>';

	//(theItemSold == 'SOLD'?'&nbsp;&nbsp;&nbsp;<span style="color: #FF0000;">SOLD</span><br>':'<br>');

	if (theItemPrice.toUpperCase() != 'INFO')
	{
		theWindowHTML += '<tr><td width="20%" class="iIP">';
		theWindowHTML += '$' + theItemPrice + '</td>';
		theWindowHTML += ((theItemPotSize != null && theItemPotSize != '')?'<td width="20%" class="iIS">' + theItemPotSize + '"&nbsp;Pot' + '</td>':'');
		theWindowHTML += '<td width="60%" ALIGN="CENTER" class="iOD">';
		if (theItemPartNumber.toUpperCase() != 'SPECIMEN') {
			theWindowHTML += '<b>Qty:</b>&nbsp;<input class="inputfield" style="font-size: 12px" type="text" name="Qty" value="' + qtyValue + '" size="2">&nbsp;&nbsp;&nbsp;';
		} else {
			theWindowHTML += '<input class="inputfield" style="font-size: 12px" type="hidden" name="Qty" value="' + (theItemSold == 'SOLD'?'0':'1') + '" size="2">';
		}
		
		theWindowHTML += '<input type="hidden" name="Item" value="' + theItemName + '">' +
		'<input type="hidden" name="PartNo" value="' + theItemPartNumber + '">' +
		'<input type="hidden" name="Price" value="' + theItemPrice + '">' +
		(theItemSold == 'SOLD'?'<span class="iSold">SOLD</span>':'<input class="inputfield" onMouseOver="this.style.color=\'#FF0000\';this.style.borderColor=\'#FF0000\'" onMouseOut="this.style.color=\'#000000\';this.style.borderColor=\'#000000\'" onClick="noBlur = true;" style="width: 180px;" type=submit name=Submit value="Add To Shopping Cart">');		

		theWindowHTML += '</td></tr>';		
	}	

	theWindowHTML += '</table>';
	theWindowHTML += '<div class="iID">' + (thisDescriptionString != ''?'<div class="itemDescDiv">' + thisDescriptionString + '</div>':'') +
	(imageLinks!= ''?imageLinks:'') +
	'<img src="' + theItemImageURL + '" id="theImage">' +
	'</div><div class="r2Top"><a href="javascript:window.opener.focus();" class="toTop" style="width:90px;">return&nbsp;to&nbsp;list</a></div></td></tr>' +
	'</table></form>' +
	(refreshImageLinks!=''?'<div style="visibility:hidden;width:1px;height:1px;overflow:hidden;"><form name="fDummy"><textarea name="iLinks">' + refreshImageLinks + '</textarea></form></div><script>document.getElementById(\'imgLink1\').style.color = \'#F2DFC3\';document.getElementById(\'imgLink1\').style.textDecoration = \'underline\';<' + '/script>':'') +
	'</body></html>';
	
	itemWindow.document.write(theWindowHTML);
	itemWindow.document.close();
}

function truncName(name, tLength) {

	var tName = name;

	if (tName.length > tLength) {
	
		if (tName.indexOf(' ') != -1) {
			if (tName.substring(0, tName.indexOf(' ')).toLowerCase() == theItemName.substring(0, tName.indexOf(' ')).toLowerCase()) {
				tName = tName.charAt(0).toUpperCase() + '.' + tName.substring(tName.indexOf(' '), tName.length);
				if (tName.length > tLength + 1) {
					tName = tName.substring(0, tLength);
					tName += '...';
				}
			} else {
				tName = tName.substring(0,tLength);
				tName += '...';
			}
		} else {
			tName = tName.substring(0,tLength);
			tName += '...';
		}
		
		if (tName.charAt(tName.length - 1) == ' ') {
			tName = tName.substring(0, tName.length-1);
			tName += '...';
		}		
		
	}

	return(tName);
}

var oldHeight = 0;
var oldWidth = 0;

function swapImage(theSrc, which) {

	oldHeight = document.getElementById('outerTable').offsetHeight;
	oldWidth = document.getElementById('outerTable').offsetWidth;

	document.getElementById('theImage').src = theSrc;	 

	document.getElementById('imgLinksSpan').innerHTML = document.fDummy.iLinks.value;

	/*
	for (i=0;i<5;i++)
	{
		if (document.getElementById('imgLink' + i))
		{
			document.getElementById('imgLink' + i).style.color = '#458FB9';
		}
	}
	*/

	document.getElementById('imgLink' + which).style.color = '#F2DFC3';
	document.getElementById('imgLink' + which).style.textDecoration = 'underline';

	resizeAfterSwap();
}

function highlightImgLink() {

}

function resizeAfterSwap() {


	if ((document.getElementById('outerTable').offsetHeight - 0) == oldHeight)
	{
		setTimeout('resizeAfterSwap()', 50);
	} else {

		resizeThisWindow();

	}

}

var theRow = '';
var cSpan = '';	
var imgNameDotIndex = '';
var tImgURL = '';

function writePlantItemRow(theItemIndex) {

	theRow = '<tr onMouseOver="rOn(this)" onMouseOut="rOff(this)" onClick="getItemPage(' + theItemIndex + ');" class="iR">';
	cSpan = '';	

	if (!noThumbs) {
	
		tImgURL = thisItemImageURL.split('!')[0];
		imgNameDotIndex = tImgURL.lastIndexOf('.');

		theRow = theRow + '<td class="iIC">' +
				 '<img src="' + tImgURL.substring(0, imgNameDotIndex) + 'thumb' + tImgURL.substring(imgNameDotIndex,tImgURL.length) + '">' +
				 '</td>';
	} else {
		cSpan = 'COLSPAN="2"';
	}

	if (thisItemPrice.toUpperCase() != 'INFO') {
		
		theRow = theRow + '<td class="fH"' + cSpan + '>' +
				 '<table cellspacing="0" cellpadding="0" class="' + (cSpan==''?'iIRT':'iIRT_nT') + '"><tr>' +
				 '<td COLSPAN="2" class="iNC"><div>' + thisItemName + '</div></td>' +
				 '</tr><tr>' + 
				 '<td class="iPC"><div>$' + thisItemPrice + '</div></td>' +
				 '<td class="iSC">' + writeSold(thisItemSold) + '<div>' + (thisItemPotSize!='SUPPLY'?(thisItemPotSize!=''?thisItemPotSize + '&quot; pot':''):'') + '</div></td>' + 
				 '</tr></table></td></tr>';
	} else {
	
		theRow = theRow + '<td class="fH"' + cSpan + '>' +
				 '<table cellspacing="0" cellpadding="0" class="' + (cSpan==''?'iRT':'iRT_nT') + '"><tr>' +
				 '<td class="fH"><div class="infoNameCell">' + thisItemName + '</div></td>' +
				 '</tr></table></td></tr>';
	}

	//and a kluge since IE ignores height=100% and firefox needs the 100% to make the other rows format correctly
	theRow += '<tr><td COLSPAN="2"><img src="images/b10.gif" HEIGHT="100%"><img src="images/b10.gif"></td></tr>';

	document.write(theRow);
}

function rOn(r) {
	r.style.backgroundColor='#6BA9CC';
}

function rOff(r) {
	r.style.backgroundColor='';
}


function writeSold(isSold) {
	if (isSold) {
		return('<div class="soldDivTrue" style="color:#fff;">SOLD</div>');
	} else {
		return('<div class="soldDivFalse">&nbsp;</div>');
	}

}


//a function so see if the main window closes and hence if we should
//close this window we do it with a little trick...

var closeCommand = 'window.close();';

function doClose() {
	eval(closeCommand);
}

function closeAfterLeavingSite() {

	closeCommand = 'window.close();';
	setTimeout('doClose()', 500);	

	//opener.close works in IE, but
	//in netscape and firefox, opener always is true for popups
	//and opener.closed is false if the opener is open
	//and refuses to evaluate if the opener is closed
	//in this code, the changing of the closeCommand string to a non-close command
	//will not execute whenever the opener has closed
	//and the timeout doClose above will close the window

	if (window.opener && !window.opener.closed)
	{
		closeCommand = 'dummy = null;';
		setTimeout('closeAfterLeavingSite()', 500);
	} 	

}


function viewCart() {

	window.open('http://www.out-of-africa-plants.com/cgi-bin/cart32.exe/OOA-itemlist?Referer=javascript:opener.focus();window.close()', '', 'resizable=1,scrollbars=1,status=1,width=600,height=500');

}

var printWindow = null;

function printList(what) {

	var winML = '<table>';

	var soldOnly = false;
	if (what == 'SOLD') {
		soldOnly = true;
		winML = '<h2>Sold Items</h2>' + winML;
	} else {
		winML = '<h2>All Items</h2>' + winML;
	}

	for (i=0;i<itemContextInfoArray.length;i++) {
		var thisInfo = itemContextInfoArray[i].split('|');
		if (soldOnly) {
			if (thisInfo[6] == 'SOLD' && thisInfo[3].toUpperCase() != 'INFO') {
				winML += '<tr><td><b>' + thisInfo[1] + '</b></td><td style="padding-left:30px;padding-right:30px;">' + thisInfo[3] + '</td><td nobreak>' + thisInfo[4] + '</td></tr>';
			}
		} else {
			if (thisInfo[3].toUpperCase() != 'INFO') {
				winML += '<tr><td><b>' + thisInfo[1] + '</b></td><td style="padding-left:30px;padding-right:30px;">' + thisInfo[3] + '</td><td>' + thisInfo[4] + '</td></tr>';
			}
		}
		
	}

	winML += '</table>';

	winML += '<s' + 'cript>window.print();<' + '/script>';

	winML = '<html><body style="font-family:arial;helvetica;font-size:16px;">' + winML + '</body></html>';

	printWindow = window.open('', 'printWin', 'resizable=1,scrollbars=1,status=1,width=600,height=500');		
	printWindow.focus();
	printWindow.document.write(winML);
	printWindow.document.close();

}



function replace(string,text,by) {
    // Replaces text with by in string
    var strLength = string.length, txtLength = text.length;
    if ((strLength == 0) || (txtLength == 0)) return string;

    var i = string.indexOf(text);
    if ((!i) && (text != string.substring(0,txtLength))) return string;
    if (i == -1) return string;

    var newstr = string.substring(0,i) + by;

    if (i+txtLength < strLength)
        newstr += replace(string.substring(i+txtLength,strLength),text,by);

    return newstr;
}


function Trim(TRIM_VALUE){
if(TRIM_VALUE.length < 1){
return"";
}
TRIM_VALUE = RTrim(TRIM_VALUE);
TRIM_VALUE = LTrim(TRIM_VALUE);
if(TRIM_VALUE==""){
return "";
}
else{
return TRIM_VALUE;
}
} //End Function

function RTrim(VALUE){
var w_space = String.fromCharCode(32);
var v_length = VALUE.length;
var strTemp = "";
if(v_length < 0){
return"";
}
var iTemp = v_length -1;

while(iTemp > -1){
if(VALUE.charAt(iTemp) == w_space){
}
else{
strTemp = VALUE.substring(0,iTemp +1);
break;
}
iTemp = iTemp-1;

} //End While
return strTemp;

} //End Function

function LTrim(VALUE){
var w_space = String.fromCharCode(32);
if(v_length < 1){
return"";
}
var v_length = VALUE.length;
var strTemp = "";

var iTemp = 0;

while(iTemp < v_length){
if(VALUE.charAt(iTemp) == w_space){
}
else{
strTemp = VALUE.substring(iTemp,v_length);
break;
}
iTemp = iTemp + 1;
} //End While
return strTemp;
} //End Function