nCurrentProductPage = 0;
nCurrentSizePage = 1;
document.nArticle = 0;
document.processingDropDown = false;

function getProduct(sId)
{
	document.ProductId = sId;
	http.open("POST", "./custom/productDetailView.php?id=" + sId, true);
	http.onreadystatechange = viewProduct;
	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http.send(null);
}

function viewProduct()
{
	if(http.readyState == 4 && http.status == 200)
	{
		setTimeout("getProductImage('"+document.ProductId+"')",100);

		eContent = document.getElementById("detailContent");
		if(http.responseText == null || eContent == null)
			return;

		eContent.innerHTML = http.responseText;
	}
}

function prevSizePage()
{
	if(nCurrentSizePage == 1)
		return;

	hideElement("PSL"+nCurrentSizePage);
	nCurrentSizePage--;
	showElement("PSL"+nCurrentSizePage,"table");
}

function nextSizePage(nMaxPage)
{
	if(nCurrentSizePage >= nMaxPage)
		return;

	hideElement("PSL"+nCurrentSizePage);
	nCurrentSizePage++;
	showElement("PSL"+nCurrentSizePage,"table");
}

function toggleSizePage(nNewPage)
{
	hideElement("PSL"+nCurrentSizePage);
	nCurrentSizePage = nNewPage;
	showElement("PSL"+nCurrentSizePage,"table");
}

function previousPage(max)
{
	if(nCurrentProductPage != 0)
		togglePage(nCurrentProductPage - 1,max);
}

function nextPage(max)
{
	if(nCurrentProductPage != max)
		togglePage(nCurrentProductPage + 1,max);
}

function togglePage(nPage,max)
{
	hideElement("PLPage" + nCurrentProductPage);
	showElement("PLPage" + nPage);

	nCurrentProductPage = nPage;

	var code = '';
	if(max > 8 && nPage >= 4)
	{
		//pages 5 and up move the pages across
		if(nCurrentProductPage + 4 <= max)
		{
			for(i=nCurrentProductPage-3;i<nCurrentProductPage+5;i++)
			{
				n = i+1;
				n += '';

				/*
				if(n.length < 2)
					n = "0" + n;
				*/
				if(i!=nCurrentProductPage-3)
					code += " ";

				style = "pagingLink";
				if(i==nCurrentProductPage)
					style = "activePage";

				code += "<a class='" + style + "' href='javascript:togglePage(" + i + "," + max + ")'>" + n + "</a>";
			}
			document.getElementById('pagingSwap').innerHTML = code + " ";
		}
		else
		{
			item = max - 7;
			for(i=item;i<=max;i++)
			{

				n = i+1;

				if(i!=item)
					code += " ";

				style = "pagingLink";
				if(i==nCurrentProductPage)
					style = "activePage";

				code += "<a class='" + style + "' href='javascript:togglePage(" + i + "," + max + ")'>" + n + "</a>";
			}
			document.getElementById('pagingSwap').innerHTML = code + " ";
		}

	}
	else
	{
		for(i=0;i<Math.min(max+1,8);i++)
		{
			n = i+1;
			n += '';
			/*
			if(n.length < 2)
				n = "0" + n;
			*/

			if(i!=0)
				code += " ";

			style = "pagingLink";
			if(i==nCurrentProductPage)
				style = "activePage";

			code += "<a class='" + style + "' href='javascript:togglePage(" + i + "," + max + ")'>" + n + "</a>";
		}
		document.getElementById('pagingSwap').innerHTML = code + " ";
	}
}

function getProductImage(sId)
{
	http.open("POST", "./custom/productDetailView.php?id=" + sId + "&part=Image", true);
	http.onreadystatechange = viewProductImage;
	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http.send(null);
}

function viewProductImage()
{
	if(http.readyState == 4 && http.status == 200)
	{
		setTimeout("getProductSizes('"+document.ProductId+"')",100);

		eContent = document.getElementById("imgContent");
		if(http.responseText == null || eContent == null)
			return;

		eContent.innerHTML = "<img src='" + http.responseText +"' />";
	}
}

function getProductSizes(sId)
{
	http.open("POST", "./custom/productSizeList.php?id=" + sId, true);
	http.onreadystatechange = displaySizes;
	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http.send(null);
}

function listbyBrand(nBrand)
{
	setBusyPointer();

	// create AJAX request and send
	http.open("POST", "./content/defaultList.php?page=index&pageName=search&find=1&Product_Brand="+nBrand,true);// + sConditionList + " AND `Product-Size`.`Product` = `Product`.`ID`&fields=" + sFieldList + "&table=`Product-Size`,`Product`", true);
	http.onreadystatechange = displaySearch;
	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http.send(null);
}

function listbyCat(nCategory)
{
	setBusyPointer();

	http.open("POST", "./content/defaultList.php?page=index&pageName=search&find=1&Product_Category="+nCategory,true);// + sConditionList + " AND `Product-Size`.`Product` = `Product`.`ID`&fields=" + sFieldList + "&table=`Product-Size`,`Product`", true);
	http.onreadystatechange = displaySearch;
	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http.send(null);
}

function findProducts(sSearchForm)
{
	sFormData = encodeForm(sSearchForm);

	sPage = getElementValue("pageName");
	changeElement("results", "<div class=\"msgText\">Searching...</div>");
	setBusyPointer();

	// create AJAX request and send
	http.open("POST", "./content/defaultList.php?page=" + sPage + "&find=1&" + sFormData, true);
	http.onreadystatechange = displaySearch;
	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http.send(null);
}

function displaySearch()
{
	if(http.readyState == 4 && http.status == 200)
	{
		resetBusyPointer();

		if(http.responseText != null)
		{
			eElement = document.getElementById("resultsSection");
			if(eElement != null)
			{
				nCurrentProductPage = 0;
				eElement.innerHTML = http.responseText;
				eElement.innerHTML += "<div id=\"DropBox\"></div>";

			}
			else
				alert("Element not found");
		}
	}
}

function displaySizes()
{
	if(http.readyState == 4 && http.status == 200)
	{
		if(http.responseText != null)
		{
			eElement = document.getElementById("details");
			if(eElement != null)
			{
				eElement.innerHTML = http.responseText;
				toggleView("results");
				toggleView("details");
			}
		}
	}
}

function showSelection(sName,nOffset,sFilter)
{
	if(document.processingDropDown)
		return;

	eElement = document.getElementById("DropBox");
	if(eElement != null)
	{
		if(eElement.style.display == "block")
		{
			eElement.style.display = "none";
			return;
		}
		eElement.style.left = nOffset + "px";
	}

	if(sFilter.length)
		sFilter = getElementValue(sFilter);

	document.processingDropDown = true;
	setBusyPointer();

	http.open("POST", "./custom/get"+sName+"List.php?filter="+sFilter, true);
	http.onreadystatechange = viewDropDown;
	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http.send(null);
}

function viewDropDown()
{
	if(http.readyState == 4 && http.status == 200)
	{
		document.processingDropDown = false;
		resetBusyPointer();

		if(http.responseText != null)
		{
			eElement = document.getElementById("DropBox");
			if(eElement != null)
			{
				showElement("DropBox");
				eElement.innerHTML = http.responseText;
			}
		}
	}
}

function setSearchOption(sOption, sValue,sOption2,sValue2)
{
	// hide selection list
	hideElement("DropBox");

	// assign selected values to selected value box
	setElementValue(sOption, sValue);
	setElementTip(sOption, sValue);

	// assign special values to hidden controls
	setElementValue(sOption2, sValue2);

	// reset search term
	setElementValue("edSearchTerm","");

	// if category selected, reset designer selection
	if(sOption == "Product_Category")
	{
		setElementValue("Product_Brand","Designer...");
		setElementValue("Designer","0");
	}

	// if category or designer selected, reset fragrance selection
	if(sOption == "Product_Category" || sOption == "Product_Brand")
	{
		setElementValue("Product","Fragrance...");
		setElementValue("ProductSelection","0");
	}

}

function resetSelections()
{
	if(getElementValue('edSearchTerm').length > 1)
		return;

	setElementValue("Product_Category","Category...");
	setElementValue("PCategory","0");

	setElementValue("Product_Brand","Designer...");
	setElementValue("Designer","0");

	setElementValue("Product","Fragrance...");
	setElementValue("ProductSelection","0");
}

function sendRequest(sFormName)
{
	formData = encodeForm(sFormName);

	http.open("POST", "./custom/sendRequest.php", true);
	http.onreadystatechange = viewProcessor;
	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http.send(formData);
}

function viewArticle(nArticle)
{
	closeProcessor();
	if(document.nArticle)
		hideElement("Article" + document.nArticle);

	hideElement('results');
	showElement("Article" + nArticle);

	document.nArticle = nArticle;

}

function hideArticle(nArticle)
{
	hideElement("Article" + nArticle);
	showElement('results');

	document.nArticle = 0;
}

function getPage(sName)
{
	http.open("POST", "./content/"+sName+".php", true);
	http.onreadystatechange = viewPage;
	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http.send(null);
}

function viewPage()
{
	if(http.readyState == 4 && http.status == 200)
	{
		hideElement("siteTerms");	
		if(http.responseText != null)
		{
			changeElement("siteProcessor", http.responseText);
			showElement("siteProcessor");
		}
	}
}

function viewProcessor()
{
	if(http.readyState == 4 && http.status == 200)
	{
		if(http.responseText != null)
		{
			eElement = document.getElementById("siteProcessor");
			if(eElement != null)
				eElement.innerHTML = http.responseText;
		}
		if(document.sProcessor != null)
			window.setTimeout("getInstructions()",100);
	}
}

function closeProcessor()
{
	changeElement("siteProcessor","");
	hideElement("siteProcessor");
}

function getInstructions()
{
	http.open("POST", document.sProcessor + "?action=getGuide", true);
	http.onreadystatechange = viewGuide;
	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

	http.send(null);
}


function viewGuide()
{
	if(http.readyState == 4 && http.status == 200)
	{
		if(http.responseText != null)
		{
			eElement = document.getElementById("welcomeBody");
			if(eElement != null)
				eElement.innerHTML = http.responseText;
		}
	}
}

function getTerms()
{
	showElement("siteTerms");
}

/*
function getTerms()
{
	http.open("POST", "content/getTerms.php", true);
	http.onreadystatechange = viewTerms;
	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

	http.send(null);
}

function viewTerms()
{
	if(http.readyState == 4 && http.status == 200)
	{
		if(http.responseText != null)
		{
			changeElement("siteTerms", http.responseText);
		}
	}
}
*/
function getAffiliates()
{
	http.open("POST", "content/affiliates.php", true);
	http.onreadystatechange = viewPage;
	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

	http.send(null);
}

