﻿// ******************************************************************************
// **************        Concierge Partners AJAX Functions          *************
// ******************************************************************************
var _userNavigatedConcierge = true;

function InitializeConciergeHistory()
{
	var state = null;
	var HD = null;
	var vendorLocationID = getQueryStringParam("VendorLocationID");
	var vendorServiceID = getQueryStringParam("VendorServiceID");
	var vendorID = getQueryStringParam("VendorID");

	if (window.location.hash != "")
	{
		state = Sys.Application._deserializeState(window.location.hash);
	}
	if (state != null)
	{
		HD = Sys.Serialization.JavaScriptSerializer.deserialize(state.value);
		if (HD != null)
		{
			vendorLocationID = HD.locationID;
			vendorServiceID = HD.serviceID;
			vendorID = HD.providerID;
		}
	}

	if (vendorLocationID == null || vendorLocationID == "")
		vendorLocationID = "0";
	if (vendorServiceID == null || vendorServiceID == "")
		vendorServiceID = "0";
	if (vendorID == null || vendorID == "")
		vendorID = "0";

	_userNavigatedConcierge = false;
	LoadConciergeSearch(vendorLocationID, vendorServiceID, vendorID);
	_userNavigatedConcierge = true;
}

function ConciergeHistoryChanged(loc, HD)
{
	if (_userNavigatedConcierge)
	{
		if (HD != null && typeof (HD.get_state().value) !== "undefined")
		{
			HD = Sys.Serialization.JavaScriptSerializer.deserialize(HD.get_state().value);
			LoadConciergeSearch(HD.locationID, HD.serviceID, HD.providerID);
		}
		else
		{
			window.history.go(-1); //LoadConciergeSearch('0', '0', '0');
		}
	}
}

function LoadConciergeSearch(locationID, serviceID, providerID)
{
	Progress.fn.show("loading", null, null);

	var hdnCompanyControl = document.getElementById(hdnCompanyClientID);
	var lstLocation = document.getElementById(lstLocationClientID);
	var lstService = document.getElementById(lstServiceClientID);
	var lstProvider = document.getElementById(lstProviderClientID);
	var hdnProvider = document.getElementById(hdnProviderClientID);
	var parentObj;
	var sHtml = "";

	lstLocation.value = locationID;

	sHtml = ConciergeController.GetLookupVendorServicesAjax(lstServiceClientID, serviceID, locationID, null);
	parentObj = lstService.parentNode;
	if (sHtml.value != null && sHtml.value != "")
	{
		parentObj.innerHTML = sHtml.value;
	} else
	{
		parentObj.innerHTML = '<SELECT id=\"' + lstServiceClientID + '\" style=\"WIDTH:100%\" size=\"10\" class=\"small\"></SELECT>';
	}

	sHtml = ConciergeController.GetVendorListAjax(lstProviderClientID, hdnCompanyControl.value, locationID, serviceID, providerID, true, null);
	parentObj = lstProvider.parentNode;
	if (sHtml.value != null && sHtml.value != "")
	{
		parentObj.innerHTML = sHtml.value;
	} else
	{
		parentObj.innerHTML = '<SELECT id=\"' + lstProviderClientID + '\" style=\"WIDTH:100%\" size=\"10\" class=\"small\"></SELECT>';
	}

	_userNavigatedConcierge = false;
	SaveVendorSelection();
	_userNavigatedConcierge = true;
	lstProvider = document.getElementById(lstProviderClientID); // Find the object again
	hdnProvider.value = lstProvider.value;
	Progress.fn.hide();
}

function GetLookupVendorServices()
{
	var lstLocation = document.getElementById(lstLocationClientID);
	var lstService = document.getElementById(lstServiceClientID);
	var parentObj = lstService.parentNode;
	var selectedServiceID = "";
	var sHtml = "";

	if (parentObj)
	{
		parentObj.innerHTML = '<SELECT id=\"' + lstServiceClientID + '\" style=\"WIDTH:100%\" size=\"10\" class=\"small\"><option value=\"0\" selected="\true\">Loading...</option></SELECT>';
	}
	lstService = document.getElementById(lstServiceClientID);
	selectedServiceID = lstService.value;

	sHtml = ConciergeController.GetLookupVendorServicesAjax(lstServiceClientID, selectedServiceID, lstLocation.value, null);
	if (sHtml.value != null && sHtml.value != "")
	{
		parentObj.innerHTML = sHtml.value;
	} else
	{
		parentObj.innerHTML = '<SELECT id=\"' + lstServiceClientID + '\" style=\"WIDTH:100%\" size=\"10\" class=\"small\"></SELECT>';
	}
	FillVendorList();
}

function FillVendorList()
{
	var hdnProviderControl = document.getElementById(hdnProviderClientID);
	var hdnCompanyControl = document.getElementById(hdnCompanyClientID);
	var lstLocation = document.getElementById(lstLocationClientID);
	var lstService = document.getElementById(lstServiceClientID);
	var lstProvider = document.getElementById(lstProviderClientID);
	var parentObj = lstProvider.parentNode;
	var sHtml = "";

	if (parentObj)
	{
		parentObj.innerHTML = '<SELECT id=\"' + lstProviderClientID + '\" style=\"WIDTH:100%\" size=\"10\" class=\"small\"><option value=\"0\" selected="\true\">Loading...</option></SELECT>';
	}
	sHtml = ConciergeController.GetVendorListAjax(lstProviderClientID, hdnCompanyControl.value, lstLocation.value, lstService.value, hdnProviderControl.value, true, null);
	if (sHtml.value != null && sHtml.value != "")
	{
		parentObj.innerHTML = sHtml.value;
	} else
	{
		parentObj.innerHTML = '<SELECT id=\"' + lstProviderClientID + '\" style=\"WIDTH:100%\" size=\"10\" class=\"small\"></SELECT>';
	}
	SaveVendorSelection();
}

function SaveVendorSelection()
{
	var hdnCompanyControl = document.getElementById(hdnCompanyClientID);
	var hdnProvider = document.getElementById(hdnProviderClientID);
	var hdnLocation = document.getElementById(hdnLocationClientID);
	var hdnService = document.getElementById(hdnServiceClientID);
	var lstLocation = document.getElementById(lstLocationClientID);
	var lstService = document.getElementById(lstServiceClientID);
	var lstProvider = document.getElementById(lstProviderClientID);
	var HD = {};
	var sLoc = "";

	hdnProvider.value = lstProvider.value;
	hdnLocation.value = lstLocation.value;
	hdnService.value = lstService.value;

	HD.companyID = hdnCompanyControl.value;
	HD.locationID = lstLocation.value;
	HD.serviceID = lstService.value;
	HD.providerID = lstProvider.value;

	sLoc = window.location.hostname.toString() + window.location.pathname.toString() + window.location.search.toString() + ":" + lstLocation.value + ":" + lstService.value + ":" + lstProvider.value;
	NRT.Utility.History.addHistoryPoint(sLoc, HD);
}

function SubmitConciergeForm()
{
	var imbNext = document.getElementById(imbNextClientID);
	if (imbNext !== null && typeof imbNext !== "undefined")
	{
		imbNext.focus();
		imbNext.click();
	}
}

// ******************************************************************************
// **************   Coupon AJAX Functions                            *************
// ******************************************************************************  
var _currentCouponPageNbr = 1;

function InitializeCouponHistory()
{
	_userNavigatedConcierge = false;

	var ctlSortBy = document.getElementById(ddlSortByClientID);
	var ctlPageSize = document.getElementById(ddlResultsPerPageClientID);
	var pageSize = 10;
	var sortIndex = 0;

	if (ctlSortBy !== null && typeof ctlSortBy !== "undefined")
	{
		sortIndex = ctlSortBy.selectedIndex;
	}

	if (ctlPageSize !== null && typeof ctlPageSize !== "undefined")
	{
		pageSize = ctlPageSize.value;
	}

	SaveSearchHistory(_currentCouponPageNbr, pageSize, sortIndex);
	_userNavigatedConcierge = true;
}

function CouponHistoryChanged(loc, HD)
{
	if (_userNavigatedConcierge)
	{
		var sortColumn = getQueryStringParam("SortColumn");
		var ctlSortBy = document.getElementById(ddlSortByClientID);
		var ctlPageSize = document.getElementById(ddlResultsPerPageClientID);
		var sortIndex = 0;
		var i;

		if (HD != null && typeof (HD.get_state().value) !== "undefined")
		{
			HD = Sys.Serialization.JavaScriptSerializer.deserialize(HD.get_state().value);

			LoadCouponSearch(HD.pageNumber, HD.pageSize, HD.sortIndex);
		}
		else
			window.history.go(-1);
	}
}

function LoadCouponSearch(pageNumber, pageSize, sortIndex)
{
	var companyID = getQueryStringParam("CompanyID");
	var ctlPageSize = document.getElementById(ddlResultsPerPageClientID);
	var divResults = document.getElementById("divResults");
	var sHtml = "";
	var i;

	if (ctlPageSize !== null && typeof ctlPageSize !== "undefined")
	{
		ctlPageSize.selectedIndex = 0;
		for (i = 0; i < ctlPageSize.length; i += 1)
		{
			if (ctlPageSize.options[i].value === pageSize.toString())
			{
				ctlPageSize.selectedIndex = i;
				break
			}
		}
	}

	divResults.innerHTML = "";
	sHtml = ConciergeCoupons.RenderPage(Number(pageNumber), pageSize, companyID);
	divResults.innerHTML = sHtml.value;
	_currentCouponPageNbr = Number(pageNumber);

	_userNavigatedConcierge = false;
	SaveSearchHistory(pageNumber, pageSize, sortIndex);
	_userNavigatedConcierge = true;
	Progress.fn.hide();
}

function ChangeCouponPageSize(pageSize)
{
	LoadCouponSearch(1, pageSize, 0);
}

function ChangeCouponSortCriteria()
{
	var ctlPageSize = document.getElementById(ddlResultsPerPageClientID);
	var pageSize = 10;
	var sortIndex = 0;

	if (ctlPageSize !== null && typeof ctlPageSize !== "undefined")
	{
		pageSize = ctlPageSize.value;
	}

	LoadCouponSearch(1, pageSize, sortIndex);
}

function CouponGotoPage(pageNumber)
{
	var ctlPageSize = document.getElementById(ddlResultsPerPageClientID);
	var pageSize = 10;
	var sortIndex = 0;

	if (ctlPageSize !== null && typeof ctlPageSize !== "undefined")
	{
		pageSize = ctlPageSize.value;
	}

	LoadCouponSearch(pageNumber, pageSize, sortIndex);
	document.body.scrollIntoView(true);
}

function CouponGotoPreviousPage()
{
	CouponGotoPage(_currentCouponPageNbr - 1)
}

function CouponGotoNextPage()
{
	CouponGotoPage(_currentCouponPageNbr + 1)
}

function CouponGotoFirstPage()
{
	CouponGotoPage(1)
}

function CouponGotoLastPage()
{
	var pageSize = document.getElementById(ddlResultsPerPageClientID).value;

	switch (pageSize)
	{
		case "5":
			CouponGotoPage(TotalNbrPages5Results)
			break;
		case "10":
			CouponGotoPage(TotalNbrPages10Results)
			break;
		default:
			CouponGotoPage(TotalNbrPages20Results)
			break;
	}
}
