var MAX_TALLY = 21;

function a_selectRangeLevel(objDiv, iLevel, strCode, strDescription)
{
	hideMenus();
	selectRangeLevel(objDiv, iLevel, strCode, strDescription);
}

function selectRangeLevel(objDiv, iLevel, strCode, strDescription)
{
	setCurrentItem(objDiv);
	resetLevels(iLevel + 1);
	
	if (!strCode)
	{
		strCode = "";
	}
	
	if (iLevel > 0)
	{
		if (strDescription)
		{
			var objItem = document.getElementById("range_" + iLevel);
			objItem.value = strCode;
			
			objItem = document.getElementById("selected_range_" + iLevel);
			objItem.innerHTML = strDescription;
		}
	}
	
	if (iLevel < 4)
	{
		startLoading();
		setTimeout("products_selectMenu(" + iLevel + ", 'range_" + iLevel + "', 'ajax/ajax_getRanges.php?group=" + strCode + "&level=" + iLevel + "', 'ranges_" + strCode + "_" + iLevel + "')", 1);
	}
	else
	{
		hideMenus();
		findProductsByRange(iLevel);
	}
}

function selectCriteria(objDiv)
{
	startLoading();
	hideMenus();
	setCurrentItem(objDiv);
	setTimeout("selectMenu('product_criteria', 'ajax/ajax_getProductCriteria.php', 'product_criteria')", 1);
}

function criteraSelected(strSearchType, strDescription)
{
	var objItem = document.getElementById("searchType");
	
	objItem.value = strSearchType;
	
	objItem = document.getElementById("selected_criteria");
	objItem.innerHTML = strDescription;
	
	hideMenus();		
}

function resetLevels(iLevel)
{
	var strType, objItem;
	
	strType = "range_" + iLevel;
	
	if (iLevel <= 4)
	{
		if (objItem)
		{
			objItem = document.getElementById(strType);
			objItem.value = "";
		}
		
		resetMenuItem(strType, "");
		resetLevels(iLevel + 1);
	}
}

function products_selectMenu(iLevel, strDivName, strURL, strCache)
{
	var objItem, arrItems;
	
	objItem = selectMenu(strDivName, strURL, strCache);
	arrItems = objItem.getElementsByTagName("results");
	
	if (arrItems.length > 0)
	{
		var objItem = document.getElementById("selected_range_" + (iLevel + 1));
		objItem.className = "menuOptionLauncher";
	}
	else
	{
		document.body.style.cursor = "auto";
		hideMenus();
		findProductsByRange(iLevel);
	}
}

function search_validateForm(objForm)
{
	if (validateForm(objForm))
	{
		var objKey = document.getElementById("searchKey");
		var objSearchType = document.getElementById("searchType");
		
		hideMenus();
		findProductsByCode(objSearchType.value, objKey.value);
	}
}

function findProductsByCode(strSearchType, strKey)
{
	var objItem;
	startLoading();
	
	setTimeout("_findProducts('" + strSearchType + "', '" + strKey + "', '1')", 1);
}

function _findProducts(strSearchType, strKey, iPageNo)
{
	var arrSingleMatch, strCode, strProdDesc;
	objTab.addTab("tab_productList", lang("Product List"), true);
	objTab.showTab("tab_productList");

	objAjax = new CAjax();
	objAjax.create();

	if (objAjax.open("ajax/ajax_getProducts.php?searchType=" + strSearchType + "&searchKey=" + strKey + "&pageNo=" + iPageNo, true, "products_" + strSearchType + "_" + strKey + "_" + iPageNo))
	{
		var objTabDiv = document.getElementById("tab_productList");
		objTabDiv.innerHTML = objAjax.getResponse();
		
		arrSingleMatch = document.getElementsByTagName("single-result");
		
		if (arrSingleMatch.length == 1)
		{
			strCode = arrSingleMatch[0].getAttribute("productCode");
			strProdDesc = arrSingleMatch[0].getAttribute("description");
			_productDetails(strCode, strProdDesc)
			
		}
	}
	else
	{
		alert("There has been a problem getting the product list, please contact an administrator");
	}
	
	resizeProductList();
	
	endLoading();
}

function resizeProductDetail()
{	
	var arrElements = document.getElementsByTagName("div");
	
	for (var i = 0; i < arrElements.length; i++)
	{
		strId = arrElements[i].id;
		
		if (strId.substr(0,14) == "productDetail_")
		{
			_resizeDiv(strId, "");
		}
	}	
}

function resizeProductList()
{
	_resizeDiv("productList", "productListButtonBar");
}

function _resizeDiv(strDivId, strButtonBarId)
{
	var objButtonBar, objDiv, nBodyBottom, nBarBottom, nListTop, nBarTop;
	
	objDiv = document.getElementById(strDivId);
	
	if (objDiv)
	{
		if (strButtonBarId != "")
		{
			objButtonBar = document.getElementById(strButtonBarId);
		}
		
		if (objButtonBar)
		{
			nBarBottom = findBottom(objButtonBar);
			nBarTop = findTop(objButtonBar);
		}
		else
		{
			nBarBottom = 0;
			nBarTop = 0;
		}
		
		nBodyBottom = findBottom(document.body);
		nListTop = findTop(objDiv);

		var nHeight = nBodyBottom - nListTop - nBarBottom + nBarTop - 30;
	
		if (nHeight > 0)
		{
			objDiv.style.height = nBodyBottom - nListTop - nBarBottom + nBarTop - 30 ;	
		}
	}
}

function findProductsByRange(iLevel)
{
	var objItem;
	startLoading();
	
	objItem = document.getElementById("range_" + iLevel);
	setTimeout("_findProducts('productgroup', '" + objItem.value + "', '1')", 1);
}

function getProductListPage(iPageNo)
{
	var objSearchType, objKey;
	startLoading();
	
	objSearchType = document.getElementById("page_searchType");
	objKey = document.getElementById("page_searchKey");
	
	setTimeout("_findProducts('" + objSearchType.value + "', '" + objKey.value + "', " + iPageNo + ")", 1);
}

function productDetails(strCode, strKit, strProdDesc)
{
	var objItem;
	startLoading();
	
	objItem = document.getElementById("range");

	if (strKit == "K")
	{
		setTimeout("_productKitDetails('" + strCode + "', '" + strProdDesc + "')", 1);
	}
	else
	{
		setTimeout("_productDetails('" + strCode + "', '" + strProdDesc + "')", 1);
	}
}

function _productKitDetails(strCode, strProdDesc)
{
	var strTabName, strTitle;
	
	strTabName = "tab_kitDetail_" + strCode;
	
	if (strProdDesc.length > 45)
	{
		strProdDesc = strProdDesc.substring(0, 42) + "...";
	}
	
	strTitle = strProdDesc;
	
	objAjax = new CAjax();
	objAjax.create();
	
	objTab.addTab(strTabName, strTitle, true);
	objTab.showTab(strTabName);
	
	if (objAjax.open("ajax/ajax_getKitDetails.php?productCode=" + strCode, true))
	{
		var objTabDiv = document.getElementById(strTabName);
		objTabDiv.innerHTML = objAjax.getResponse();
	}
	else
	{
		alert(lang("There has been a problem, please contact an administrator"));
	}
	
	resizeProductDetail();
	endLoading();
}

function _productDetails(strCode, strProdDesc)
{
	var strTabName, strTitle;
	
	strTabName = "tab_productDetail_" + strCode;
	
	if (strProdDesc.length > 45)
	{
		strProdDesc = strProdDesc.substring(0, 42) + "...";
	}
	
	strTitle = strProdDesc;

	objAjax = new CAjax();
	objAjax.create();
		
	objTab.addTab(strTabName, strTitle, true);
	objTab.showTab(strTabName);
	
	if (objAjax.open("ajax/ajax_getProductDetail.php?productCode=" + strCode, true, "productDetails_" + strCode))
	{
		var objTabDiv = document.getElementById(strTabName);
		objTabDiv.innerHTML = objAjax.getResponse();
	}
	else
	{
		alert(lang("There has been a problem, please contact an administrator"));
	}
	
	if (objAjax.open("ajax/ajax_getProductDetail.php?mode=QUANTITY&productCode=" + strCode, true))
	{
		var objTabDiv = document.getElementById("quantityDiv_" + strCode);
		objTabDiv.innerHTML = objAjax.getResponse();
	}
	else
	{
		alert(lang("There has been a problem, please contact an administrator"));
	}
	
	
	
	
	resizeProductDetail();
	endLoading();
}

function showProductRange()
{
	var objItem;
	hideMenus();
	objItem = document.getElementById("searchByCode");
	objItem.style.visibility = "hidden";
	objItem.style.display = "none";
	
	objItem = document.getElementById("productRange");
	objItem.style.visibility = "visible";
	objItem.style.display = "block";
}

function showSearch()
{
	var objItem;
	hideMenus();
	objItem = document.getElementById("productRange");
	objItem.style.visibility = "hidden";
	objItem.style.display = "none";
	
	objItem = document.getElementById("searchByCode");
	objItem.style.visibility = "visible";
	objItem.style.display = "block";
}

function selectRange(objDiv, iLevel)
{
	var objItem = document.getElementById("range_" + iLevel);
	
	var objLastItem = document.getElementById("range_" + (iLevel));
	
	if (objLastItem.value == "")
	{
		return;
	}	
	
	hideMenus();
	selectRangeLevel(objDiv, iLevel, objItem.value, "");
}

function viewAlternateProducts(objPlus, strCode)
{
	viewProducts(objPlus, strCode, "alternateProducts_" + strCode, "ajax_getAlternateProducts.php", "alternates_" + strCode)
}

function viewAssociatedProducts(objPlus, strCode)
{
	viewProducts(objPlus, strCode, "associatedProducts_" + strCode, "ajax_getAssociatedProducts.php", "associated_" + strCode)
}

function genericShowDiv(objPlus, strDivId)
{
	var objDiv;
	
	objDiv = document.getElementById(strDivId);
	
	setPlus(objPlus);
	
	if (isExpanded(objPlus))
	{
		objDiv.className = "visibleDiv";
	}
	else
	{
		objDiv.className = "hiddenDiv";
	}
}

function viewProducts(objPlus, strCode, strDivId, strScriptName, strCacheName)
{
	var objDiv;
	
	objDiv = document.getElementById(strDivId);
	
	setPlus(objPlus);
	
	if (isExpanded(objPlus))
	{
		objDiv.className = "visibleDiv";
		
		startLoading();
		setTimeout("_viewProducts('" + strCode + "', '" + strDivId + "', '" + strScriptName + "', '" + strCacheName + "')", 1);
	}
	else
	{
		objDiv.className = "hiddenDiv";
	}
}

function _viewProducts(strCode, strDivId, strScriptName, strCacheName)
{
	var objAjax, objDiv;
	
	objAjax = new CAjax();
	objAjax.create();
	
	objDiv = document.getElementById(strDivId);
	
	if (objAjax.open("ajax/" + strScriptName + "?productCode=" + strCode, true, strCacheName))
	{
		objDiv.innerHTML = objAjax.getResponse();
	}
	else
	{
		alert(lang("There has been a problem, please contact an administrator"));
	}
	
	endLoading();
}

function showTallyDetails(strCode, strType)
{
	var objTallyDiv, objQuantityDiv;
	
	objTallyDiv = document.getElementById("tallyDetails_" + strCode);
	objQuantityDiv = document.getElementById("quantityDetails_" + strCode);
	
	if (strType == "T")
	{
		objTallyDiv.className = "visibleDiv";
		objQuantityDiv.className = "hiddenDiv";
		
	}
	else
	{
		objTallyDiv.className = "hiddenDiv";
		objQuantityDiv.className = "visibleDiv";
		
	}
}

function updateTallyQuantity(objItem, cIsWidthTally, iNumber, strCode)
{
	var iValue;
	
	iValue = parseInt(objItem.value);
	objLengthItem = document.getElementById("t_length_" + iNumber + "_" + strCode);
	
	if (iValue == 0)
	{
		moveValuesUp(iNumber, strCode);
	}
	else
	{
		addNewRow(cIsWidthTally, iNumber, strCode);
	}
}

function moveValuesUp(iNumber, strCode)
{
	var objQuantityItem, objLengthItem, objNextQuantityItem, objNextLengthItem, objNextWidthItem, objRow, bLastItem;
	
	objQuantityItem = document.getElementById("t_quantity_" + iNumber + "_" + strCode);
	objLengthItem = document.getElementById("t_length_" + iNumber + "_" + strCode);
	objWidthItem = document.getElementById("t_width_" + iNumber + "_" + strCode);
	
	bLastItem = false;
	
	if (iNumber < MAX_TALLY)
	{
		objNextQuantityItem = document.getElementById("t_quantity_" + (iNumber + 1) + "_" + strCode);
		objNextLengthItem = document.getElementById("t_length_" + (iNumber + 1) + "_" + strCode);
		objNextWidthItem = document.getElementById("t_width_" + (iNumber + 1) + "_" + strCode);
		

		objQuantityItem.value = objNextQuantityItem.value;
		objLengthItem.value = objNextLengthItem.value;
		objWidthItem.value = objNextWidthItem.value;
		
		if (objNextQuantityItem.value != "")
		{
			moveValuesUp(iNumber + 1, strCode);
		}
		else
		{
			bLastItem = true;
		}
	}
	
	if (bLastItem || iNumber == MAX_TALLY)
	{
		objRow = document.getElementById("t_row_" + (iNumber + 1) + "_" + strCode);
		objRow.style.visibility = "hidden";
		objRow.style.display = "none";
	}
}

function updateTallyLength(objItem, cIsWidthTally, iNumber, strCode)
{
	var iValue, bOk, iMaximum, iMinimum, iIncrement, objRow, objNextQuantityItem, objQuantityItem, arrTallySpec, iTallySpec, arrIncTally, objIncTally;
	
	iValue = parseFloat(objItem.value);
	
	arrIncTally = document.getElementsByTagName("tally-incremental");

	if (arrIncTally.length > 0)
	{
		bOk = false;
		objIncTally = arrIncTally[0];
		iMinimum = parseFloat(objIncTally.getAttribute("minimum"));
		iMaximum = parseFloat(objIncTally.getAttribute("maximum"));
		iIncrement = parseFloat(objIncTally.getAttribute("increment"));
		
		if (iValue <= iMinimum)
		{
			objItem.value = iMinimum;
			bOk = true;
		}
		else if (iValue >= iMaximum)
		{
			objItem.value = iMaximum;
			bOk = true;
		}
		
		if (!bOk)
		{
			for (var i = iMinimum; i <= iMaximum; i+= iIncrement)
			{
				if (iValue == i)
				{
					break;
				}
				else if (iValue < i)
				{
					objItem.value = i;
					break;
				}
				
				if (i == iMaximum)
				{
					objItem.value = iMaximum;
				}
			}
		}
	}
	else
	{
		arrTallySpec = document.getElementsByTagName("tallyspec");
		
		for (var i = 0; i < arrTallySpec.length; i++)
		{
			iTallySpec = parseFloat(arrTallySpec[i].getAttribute("length"));
			
			if (iValue == iTallySpec)
			{
				break;
			}
			else if (iValue < iTallySpec)
			{
				objItem.value = iTallySpec;
				break;
			}
			
			if (i == arrTallySpec.length - 1)
			{
				objItem.value = iTallySpec;
			}
		}
	}
	
	iValue = objItem.value;
	addNewRow(cIsWidthTally, iNumber, strCode);
}

function updateTallyWidth(objItem, iNumber, strCode)
{
	var iValue, bOk, iMaximum, iMinimum, iIncrement, objRow, objNextQuantityItem, objQuantityItem, arrTallySpec, iTallySpec, arrIncTally, objIncTally;
	
	iValue = parseFloat(objItem.value);
	
	arrIncTally = document.getElementsByTagName("tally-width");

	bOk = false;
	objIncTally = arrIncTally[0];
	iMinimum = 0;
	iMaximum = parseFloat(objIncTally.getAttribute("maximum"));
	iIncrement = 1;
	
	if (iValue <= iMinimum)
	{
		objItem.value = iMinimum;
		bOk = true;
	}
	else if (iValue >= iMaximum)
	{
		objItem.value = iMaximum;
		bOk = true;
	}
	
	if (!bOk)
	{
		for (var i = iMinimum; i <= iMaximum; i+= iIncrement)
		{
			if (iValue == i)
			{
				break;
			}
			else if (iValue < i)
			{
				objItem.value = i;
				break;
			}
			
			if (i == iMaximum)
			{
				objItem.value = iMaximum;
			}
		}
	}
	
	iValue = objItem.value;
	addNewRow("Y", iNumber, strCode);
}

function addNewRow(cIsWidthTally, iNumber, strCode)
{
	var objQuantityItem, objLengthItem, objWidthItem, objRow, iQuantity, iLength, iWidth;
	
	objQuantityItem = document.getElementById("t_quantity_" + (iNumber) + "_" + strCode);
	objLengthItem = document.getElementById("t_length_" + (iNumber) + "_" + strCode);
	iQuantity = parseFloat(objQuantityItem.value);
	iLength = parseFloat(objLengthItem.value);
	iWidth = 0;
	
	if (cIsWidthTally == "Y")
	{
		objWidthItem = document.getElementById("t_width_" + (iNumber) + "_" + strCode);
		iWidth = parseFloat(objWidthItem.value);
	}
	
	if (iNumber < MAX_TALLY && iQuantity > 0 && iLength > 0 && (cIsWidthTally != 'Y' || iWidth > 0))
	{
		objRow = document.getElementById("t_row_" + (iNumber + 1) + "_" + strCode);
		objRow.style.visibility = "visible";
		
		try
		{
			objRow.style.display = "table-row";
		}
		catch (e)
		{
			objRow.style.display = "block";
		}
		
		objNextQuantityItem = document.getElementById("t_quantity_" + (iNumber + 1) + "_" + strCode);
		objNextQuantityItem.focus();
	}
}