﻿/******************************************************************************
 * IMAGE ROUTINES                                                             *
 ******************************************************************************/

var sAgent = window.navigator.userAgent;
var sVersion = parseInt (sAgent.charAt (sAgent.indexOf ("/") + 1), 10);
var bIsIE = (sAgent.indexOf ("MSIE") != -1);
var bIsDOM = (!(bIsIE && sVersion < 6) && document.getElementById);
var bImageInit = false;

function findElement (sID, lyrLayer)
{
	var docDocument, elmElement, iCount;
	if (sVersion < 4)
		{
			return document[sID];
		}
	if (bIsDOM)
		{
			if (!bImageInit && document.images)
				{
					for (iCount; iCount < document.images.length; iCount++)
						{
							if (!document.images[iCount].id)
								{
									document.images[iCount].id = document.images[iCount].name;
								}
						}
					bImageInit = true;
				}
			return (document.getElementById (sID));
		}
	docDocument = (lyrLayer != undefined ? lyrLayer.document : document);
	elmElement = docDocument [sID];
	if (!elmElement)
		{
			for (iCount = 0; iCount < docDocument.layers.length; iCount++)
				{
					if (elmElement = findElement (sID, docDocument.layers[iCount]))
						{
							return elmElement;
						}
				}
		}
	return elmElement;
}

function getImage (sID)
{
	if(document.images)
		{
			if (!bIsIE && sVersion < 5)
				{
					return findElement (sID);
				}
			else
				{
					return document.images[sID];
				}
		}
	return null;
}

function swapImage (sID, sURL)
{
	var imgImage;
	imgImage = getImage (sID);
	if (imgImage != null)
		{
			imgImage.src = sURL;
		}
}

/******************************************************************************
 * SUBCATEGORY ROUTINES                                                       *
 ******************************************************************************/
//searchCategory
//searchSubCategory
function loadCategory (iCategoryID, searchWhat, searchSubWhat)
{
	var iCount, nodLink, nodSubCategory, nodText;
	for (iCount = 0; iCount < iCategoryCount; iCount++)
		{
			nodSubCategory = document.getElementById ("SubCategory_" + iCount);
			removeChildren (nodSubCategory);
			if (iCount < aa_sSubCategories[iCategoryID].length)
				{
					nodLink = self.document.createElement ("a");
					if (trimString (aa_sSubCategories[iCategoryID][iCount].toLowerCase ()) == "(all)")
						{
							nodLink.setAttribute ("href", "javascript: " + searchWhat + " (" + iCategoryID + ");");
						}
					else
						{
							nodLink.setAttribute ("href", "javascript: " + searchSubWhat + " (" + aa_iSubCategories[iCategoryID][iCount] + ", " + iCategoryID + ");");
						}
					nodLink.iBulletID = iCount;
					nodLink.onclick = function () { swapImage ("SubBullet_" + this.iBulletID, "images/SSL_Bullet_onclick.gif"); };
					nodLink.onmouseover = function () { swapImage ("SubBullet_" + this.iBulletID, "images/SSL_Bullet_over.gif"); };
					nodLink.onmouseout = function () { swapImage ("SubBullet_" + this.iBulletID, "images/SSL_Bullet_normal.gif"); };
					nodText = document.createTextNode (aa_sSubCategories[iCategoryID][iCount]);
					nodLink.appendChild (nodText);
					nodSubCategory.setAttribute ("bgColor", "#ffffff");
					nodSubCategory.appendChild (nodLink);
				}
			else
				{
					nodSubCategory.removeAttribute ("bgColor");
				}
		}
}

function removeChildren (nodNode)
{
	var nodChild
	nodChild = nodNode.firstChild;
	while (nodChild)
		{
			nodNode.removeChild (nodChild);
			nodChild = nodNode.firstChild;
		}
}

function searchCategory (iCategoryID)
{
	document.forms['frmBrowseType'].elements['sCategoryID'].value = iCategoryID;
	document.forms['frmBrowseType'].elements['sSubCategoryID'].value = -1;
	document.forms['frmBrowseType'].submit ();
}

function searchSubCategory (iSubCategoryID)
{
	document.forms['frmBrowseType'].elements['sCategoryID'].value = -1;
	document.forms['frmBrowseType'].elements['sSubCategoryID'].value = iSubCategoryID;
	document.forms['frmBrowseType'].submit ();
}

function searchFuel (iCategoryID)
{
	document.forms['frmBrowseFuel'].elements['sMethodID'].value = iCategoryID;
	document.forms['frmBrowseFuel'].elements['sfuelMethodID'].value = -1;
	document.forms['frmBrowseFuel'].submit ();
}

function searchFuelMethod (iSubCategoryID, iCategoryID)
{
	document.forms['frmBrowseFuel'].elements['sMethodID'].value = iCategoryID;
	document.forms['frmBrowseFuel'].elements['sfuelMethodID'].value = iSubCategoryID;
	document.forms['frmBrowseFuel'].submit ();
}

function trimString (sString)
{
  var iStart, iEnd;
  iStart = 0;
  iEnd = sString.length - 1;
  while (iStart <= iEnd && sString.charAt (iStart) == " ")
    {
      iStart++;
    }
  while (iEnd > iStart && sString.charAt (iEnd) == " ")
    {
      iEnd--;
    }
  return sString.substring (iStart, iEnd + 1);
}
