/* Copyright Commcam Limited 2008. All rights reserved. (See licence/licence.txt) */

function quickSearch()
{
	var objQuickSearch = document.getElementById("quickSearch");
	
	if (objQuickSearch)
	{
		if (objQuickSearch.value != "")
		{
			startLoading("_quickSearch('" + objQuickSearch.value + "', '', 0, 0, 0)");
		}
		else
		{
			alert("Please enter some search text");
		}
	}
	
}

function allProducts()
{
	startLoading("_quickSearch('', '', 0)");
}

function _quickSearch(strText, strAction, iOffset, bRange, bWithBar)
{
	var objAjax = new CAjax();
	var iResults

	if (objAjax.open("dynamic/products.php?mode=SEARCH&text=" + strText + "&action=" + strAction + "&offset=" + iOffset + "&byRange=" + bRange + "&withBar=" + bWithBar, true))
	{
		//var objDiv = document.getElementById('innerContent');
		//objDiv.innerHTML = objAjax.getResponse();
		
		var objDiv = document.getElementById('productContent');
		
		if (!objDiv)
		{
			objDiv = document.getElementById('innerContent');
		}
		
		objDiv.innerHTML = objAjax.getResponse();
		
		iResults = getFormValue("results");

		if (iResults == 1)
		{
			_productDetail(getFormValue("listCode"), escape(getFormValue("listDesc")), getFormValue("listBalance"));
		}
	}
			
	endLoading();
}

function pageSearch(iOffset)  
{
	startLoading("_quickSearch('" + getFormValue("searchString") + "', '" + getFormValue("action") + "', " + iOffset + ", 0, 0)");
}

function productDetailExtra(strCode, strDesc, strBalance)
{
	g_objTabs.changeTabContainer("rightContent");
	productDetail(strCode, strDesc, strBalance);
}

function productDetail(strCode, strDesc, strBalance)
{
	startLoading("_productDetail('" + strCode + "', '" + escape(strDesc) + "', '" + strBalance + "')");
}

function _productDetail(strCode, strDesc, strBalance)
{
	strDesc = unescape(strDesc);
	var objAjax = new CAjax();
	var strDivId = "product_" + strCode;
	var objDiv;
	
	if (strBalance == "")
	{
		strBalance = "I";
	}
	
	if (objAjax.open("dynamic/products.php?mode=DETAIL&code=" + strCode + "&balance=" + strBalance, true))
	{
		objDiv = g_objTabs.addTab(strDivId, strDesc, true, true);
		
		objDiv.innerHTML = objAjax.getResponse();
	}
	
	endLoading();	
}

function showCategories(strCode, strDesc)
{
	startLoading("_showCategories('" + strCode + "', '" + strDesc + "')");
}

function showProductsByGroup(strCode)
{
    startLoading("_productsByGroup('"+strCode+"')");
}

function _productsByGroup(strCode)
{
    var objAjax = new CAjax();
    var objDiv = "productBox";
    
    if(objAjax.open("dynamic/products.php?mode=DRILL&code="+strCode))
    {
        objDiv.innerHTML = objAjax.getResponse();
    }
    
    endLoading();
}

function _showCategories(strCode, strDesc)
{
	strDesc = unescape(strDesc);
	var objAjax = new CAjax();
	var strDivId = "product_" + strCode;
	var objDiv;
	
	if (objAjax.open("dynamic/categories.php?catcode=" + strCode, true))
	{
		objDiv = g_objTabs.addTab("categories", strDesc, true, true);
		
		objDiv.innerHTML = objAjax.getResponse();
	}
	
	endLoading();	
}

function categorySearch(strCategoryCode)
{
	startLoading("_quickSearch('" + strCategoryCode + "', 'productgroup', 0, 0)");
}

function toggleCategories(objImg, strDivId)
{
	var objDiv = document.getElementById(strDivId);

	if (objDiv.getAttribute("visible") == "true")
	{
		objDiv.style.display = "none";
		objDiv.setAttribute("visible", "false");
		objImg.src = "images/plus.gif";
	}
	else
	{
		objDiv.style.display = "block";
		objDiv.setAttribute("visible", "true");
		objImg.src = "images/minus.gif";
	}
	
	resizeWindow();
}

function productSearch()
{
	var objSelect = document.getElementById("searchType");
	var objText = document.getElementById("quickSearch");
	
	startLoading("_quickSearch('" + objText.value + "', '" + objSelect.value + "', 0, 0)");
}

function showPricesPrompt(strCode)
{
	var objHolder = document.getElementById("passwordHolder_" + strCode);
	var objPrompt = document.getElementById("passwordPrompt_" + strCode);
	
	objPrompt.style.display = "none";
	objHolder.style.display = "block";
}

function showPrices(strCode)
{
	var strPassword = getFormValue("password_" + strCode);
	var objHolder = document.getElementById("passwordHolder_" + strCode);
	var objPricing = document.getElementById("pricing_" + strCode);
	
	var objAjax = new CAjax();
	
	if (objAjax.open("admin/checkPassword.php?ps=" + strPassword, true))
	{
		var strResponse = objAjax.getResponse();
		
		if (strResponse == "true")
		{
			objHolder.style.display = "none";
			objPricing.style.display = "block";
		}
		else
		{
			alert("Incorrect password");Products
		}
	}
	
}

/* LISTS ALL ITEMS

function Products()
{

    var objAjax = new CAjax();

	if (objAjax.open("dynamic/products.php?mode=SEARCH&text=card&action=&offset=0&byRange=0", true))
	{
		var strResponse = objAjax.getResponse();
		objDiv = g_objTabs.addTab("products", "description", true, true);

		objDiv.innerHTML = objAjax.getResponse();
	}
}*/

function Products()
{
   innerContentUpdate("dynamic/productPage.php");
    
}
