﻿// ----------------------------------------------
// File:		GetProductsAndCategoriesService.js
// Author:		Nathan Derksen
// Description:	A class that holds the available service methods, along with response handlers.
// Example:
// GetProductsAndCategoriesService.getInstance().getProducts(searchCriteria, 0, 12);
// ----------------------------------------------


// ----------------------------------------------
// Function:	GetProductsAndCategoriesService
// Author:		Nathan Derksen
// Description:	Base class
// Inputs:		<none>
// Returns:		<nothing>
// ----------------------------------------------
function GetProductsAndCategoriesService()
{
}

// ----------------------------------------------
// Function:	GetProductsAndCategoriesService.getProducts
// Author:		Nathan Derksen
// Description:	Calls the service to retrieve a set of products
// Inputs:		<Object> searchCriteria - An object of properties to search on
//				<Number> startIndex - The zero-based index of which row to start at in the total results
//				<Number> maxResults - The maximum number of results to return
// Returns:		<nothing>
// ----------------------------------------------
GetProductsAndCategoriesService.prototype.getData = function(queryString)
{
	try
	{
		var searchString = window.location.search.split("?").join("");
		var cid = URLFactory.extractQueryStringValue(searchString, "cid");
		var sortCriteria = URLFactory.extractValue(queryString, "sortCriteria");
		var pageQueryString = window.location.search.split("?").join("");
		var search = false;
		var searchQSVal = URLFactory.extractQueryStringValue(pageQueryString, "search");
		
		if (sortCriteria == "")
		{
			if (cid != "")
			{
				queryString = URLFactory.updateHash(queryString, "sortCriteria", "5"); // CategoryOrder
			}
			else
			{
				queryString = URLFactory.updateHash(queryString, "sortCriteria", "1"); // PriceHighToLow
			}
		}

		if (searchQSVal == "1")
		{
			search = true;
		}

		if (isAjaxEnabled() == true)
		{
			PageMethods.GetCategoriesXmlBySearchQS(URLFactory.convertHashToServiceHash(queryString), search, this.onResult, this.onError);
		}
	}
	catch (err)
	{
		Debug.error(err);
	}
};

// ----------------------------------------------
// Function:	GetProductsAndCategoriesService.getProducts
// Author:		Nathan Derksen
// Description:	Calls the service to retrieve a set of products
// Inputs:		<Object> searchCriteria - An object of properties to search on
//				<Number> startIndex - The zero-based index of which row to start at in the total results
//				<Number> maxResults - The maximum number of results to return
// Returns:		<nothing>
// ----------------------------------------------
GetProductsAndCategoriesService.prototype.getJSONData = function()
{
	try
	{
		var resultProductArray = [];
		if (typeof(tblData) != "undefined")
		{
			resultProductArray = ProductFactory.convertJSONToArray(tblData);
		}
		
		var resultCategoryArray = [];
		if (typeof(menuData) != "undefined")
		{
			resultCategoryArray = CategoriesFactory.convertJSONToObjects(menuData);
		}
		
		var numHits = 0;
		var layoutType = "1";
		var showSort = true;
		if (typeof(gridData) != "undefined")
		{
			numHits = gridData.numHits;
			layoutType = String(gridData.layout);
			if (typeof(gridData.showSortBy) != "undefined")
			{
				if (gridData.showSortBy == false || gridData.showSortBy == "false")
				{
					showSort = false;
				}
			}
		}

		GetProductsAndCategoriesService_process(resultProductArray, resultCategoryArray, numHits, layoutType, "", "", showSort);
	}
	catch (err)
	{
		Debug.error(err);
	}
};

// ----------------------------------------------
// Function:	GetProductsAndCategoriesService.onResult
// Author:		Nathan Derksen
// Description:	Callback from the successful completion of the service call
// Inputs:		<XMLElement> result - Handle to the results xml object
// Returns:		<nothing>
// ----------------------------------------------
GetProductsAndCategoriesService.prototype.onResult = function(result)
{
	try
	{
		if (result)
		{
			var resultElements = result.documentElement;
			var resultProductArray = ProductFactory.convertXMLToArray(resultElements);
			var resultCategoryArray = CategoriesFactory.convertXMLToObjects(resultElements);
			var numHits = ProductFactory.getTotalNumProducts(resultElements);
			var layoutType = CategoriesFactory.getLayoutType(resultElements);	
			var didYouMean = SearchFactory.getDidYouMeanFromXML(resultElements);
			var showSort = ProductFactory.getShowSortMenu(resultElements);
			
			GetProductsAndCategoriesService_process(resultProductArray, resultCategoryArray, numHits, layoutType, didYouMean.keyword, didYouMean.qs, showSort);
		}
		else
		{
			var tempArray = new Array();
			var productsGrid = ViewLocator.getInstance().getView("productsGrid");
			productsGrid.setProducts(tempArray);
			
			var error = new Object();
			error.name = "Service 'GetProductsAndCategoriesService' returned with no results";
			error.message = error.name;
			error.fileName = "GetProductsAndCategoriesService.js";
			error.lineNumber = "";
			Debug.error(error);
		}
	}
	catch (err)
	{
		Debug.error(err);
	}
};

// ----------------------------------------------
// Function:	GetProductsAndCategoriesService.onError
// Author:		Nathan Derksen
// Description:	Callback from the unsuccessful completion of the service call
// Inputs:		<XMLElement> result - Handle to the results xml object
// Returns:		<nothing>
// ----------------------------------------------
GetProductsAndCategoriesService.prototype.onError = function(result)
{
	var tempArray = new Array();
	var productsGrid = ViewLocator.getInstance().getView("productsGrid");
	productsGrid.setProducts(tempArray);
	
	var error = new Object();
	error.name = "Service 'GetProductsAndCategoriesService' returned with an error";
	error.message = result.get_message();
	error.fileName = "GetProductsAndCategoriesService.js";
	error.lineNumber = "";
	
	Debug.error(error);
};

// ----------------------------------------------
// ----------------------------------------------
function GetProductsAndCategoriesService_process(resultProductArray, resultCategoryArray, numHits, layoutType, keyword, qs, showSort)
{
	var model = ProductModel.getInstance();
	model.setNumProducts(numHits);
	model.setProducts(resultProductArray);	
	
	if (layoutType == "1")
	{
		model.getProductGrid("viewAll").gridType = "topLeftBottomRight";
		model.getProductGrid("viewPaged").gridType = "topLeftBottomRight";
	}
	else
	{
		model.getProductGrid("viewAll").gridType = "bottomLeftTopRight";
		model.getProductGrid("viewPaged").gridType = "bottomLeftTopRight";
	}
	ViewLocator.getInstance().getView("productsGrid").setGridType(model.getCurrentProductGrid().gridType);
	
	if (showSort == false)
	{
		ViewLocator.getInstance().getView("sortBy").style.display = "none";
	}
	else
	{
		ViewLocator.getInstance().getView("sortBy").style.display = "block";
	}
	
	blockDropDownClicks = false;
	
	CategoriesModel.getInstance().addMenus(resultCategoryArray);
	
	if (keyword != "" && keyword != null && qs != "" && qs != null)
	{
		var query = window.location.search.split("?").join("");
		var sessionId = "";
		var sessionIdQSVal = URLFactory.extractQueryStringValue(query, "mysid2");
		var redirect = "/Shopping/CategoryBrowse.aspx?search=1&search_params=" + qs + sessionId;
		var didYouMeanHolder = ViewLocator.getInstance().getView("didYouMeanHolder");
		
		didYouMeanHolder.style.display = "block";
//		didYouMeanHolder.innerHTML = didYouMeanLiteral + '<a href="' + redirect + '">' + keyword + '</a>?';
		didYouMeanHolder.innerHTML = didYouMeanLiteral.split("{0}").join('<a href="' + redirect + '">' + keyword + '</a>');
	}
	
	var productsGrid = ViewLocator.getInstance().getView("productsGrid");
	productsGrid.setProducts(resultProductArray);
};
