(function(win){
	var viewModel = null;
	
	function Contract()
	{
		var self = this;
		
		self.ContractID = ko.observable();
		self.ContractName = ko.observable("");
		self.Department = ko.observable("");
		self.CompanyName = ko.observable("");
		self.ServiceType = ko.observable("");
		self.ContractType = ko.observable(""); 
		self.FinishDate = ko.observable();
		self.Documents = ko.observableArray();
	}
	
	function Document()
	{
		var self = this;
		self.DocumentID = ko.observable();
		self.DocumentName = ko.observable("");
		
		self.getURL = ko.computed(function()
		{
			if(self.DocumentID() > 0)
			{
				return "GetDocument.asp?ID=" + self.DocumentID(); 
			}
			else
			{
				return "#";
			}
		});
	}
	
	// Viewmodel
	function WidgetContractViewModel()
	{
		var self = this;
		
		self.lists = ko.observableArray();
		self.selected = ko.observable();
		
		// Filter
		self.departmentFilter = ko.observable("");
		self.contractTypeFilter = ko.observable("");
		self.companyNameFilter = ko.observable("");
		self.serviceTypeFilter = ko.observable("");
	
		self.listFilter = ko.computed(function()
		{
			return ko.utils.arrayFilter(self.lists(), function(item) {
				var pattDepartment = new RegExp(self.departmentFilter(), "i");
				var pattContractType = new RegExp(self.contractTypeFilter(), "i");
				var pattCompanyName = new RegExp(self.companyNameFilter(), "i");
				var pattServiceType = new RegExp(self.serviceTypeFilter(), "i");
				
				return pattDepartment.test(item.Department()) && pattContractType.test(item.ContractType()) && pattCompanyName.test(item.CompanyName()) && pattServiceType.test(item.ServiceType());
			});
		});
		
		self.load = function()
		{
			ShowLoader("Loading Contract List");
			
			var param = "Department=" + encodeURIComponent(GetValue(document.WidgetPurchaseContractForm.WidgetPurchaseContractDepartmentCmb));
			param += "&WorkTypeID=" + GetValue(document.WidgetPurchaseContractForm.WidgetPurchaseContractServiceTypeCmb);
			
			var url = "widget/widgetpurchasecontract/WidgetPurchaseContractList.asp?" + param;
			$.ajax({
				url: url,
				type: "post",
				async: true,
				cache: false,
				success: function(data) {
					HideLoader();
					
					self.lists.removeAll();
					var dataObj = JSON.parse(data);
					for(var i = 0; i < dataObj.length; i++)
					{
						var item = new Contract();
						item.ContractID(dataObj[i].ContractID);
						item.ContractName(dataObj[i].ContractName);
						item.Department(dataObj[i].Department);
						item.CompanyName(dataObj[i].CompanyName);
						item.ServiceType(dataObj[i].ServiceType);
						item.ContractType(dataObj[i].ContractType);
						item.FinishDate(dataObj[i].FinishDate);
						self.lists.push(item);  
					}
				},
				error: function(xhr) {
					HideLoader();
					alert(xhr.responseText);
				}
			});
		};
		
		self.setRecord = function(item)
		{
			self.selected(item);
			
			ShowLoader("Loading contract details");
			var url = "widget/widgetpurchasecontract/WidgetPurchaseContract.asp?id=" + item.ContractID();
			$.ajax({
				url: url,
				type: "post",
				async: true,
				cache: false,
				success: function(data) {
					HideLoader();
					
					var dataObj = JSON.parse(data);
					
					self.selected().ContractName(dataObj.ContractName);
					self.selected().CompanyName(dataObj.CompanyName);
					self.selected().ContractType(dataObj.ContractType);
					
					self.selected().Documents.removeAll();
					for(var i = 0; i < dataObj.Documents.length; i++)
					{
						var item = new Document();
						item.DocumentID(dataObj.Documents[i].DocumentID);
						item.DocumentName(dataObj.Documents[i].DocumentName);
						self.selected().Documents.push(item);
					}
					
					$("#PurchaseContractDetailDialog").show();
				},
				error: function(xhr) {
					HideLoader();
					alert(xhr.responseText);
				}
			});
		};
		
		self.closeDialog = function()
		{
			$("#PurchaseContractDetailDialog").hide();
		};
		
		self.load();
	}
	
	function Refresh()
	{
		viewModel.load();
	}
	
	function Department_Onload()
	{
		ResetCombo(document.WidgetPurchaseContractForm.WidgetPurchaseContractServiceTypeCmb, "0", "-- All --");
		
		var param = "Department=" + encodeURIComponent(GetValue(document.WidgetPurchaseContractForm.WidgetPurchaseContractDepartmentCmb));
		var url = "widget/widgetpurchasecontract/WidgetPurchaseContractWorkTypeList.asp?" + param;
		$.ajax({
			url: url,
			type: "post",
			async: true,
			cache: false,
			success: function(data) {
				HideLoader();
				
				var dataObj = JSON.parse(data);
				for(var i = 0; i < dataObj.length; i++)
				{
					AddComboItem(document.WidgetPurchaseContractForm.WidgetPurchaseContractServiceTypeCmb, dataObj[i].WorkTypeID, dataObj[i].WorkType);
				}
				Refresh();
			},
			error: function(xhr) {
				HideLoader();
				alert(xhr.responseText);
			}
		});
	}
	
	function Load()
	{
		if(viewModel == null)
		{
			viewModel = new WidgetContractViewModel();
			ko.applyBindings(viewModel, document.getElementById('PurchaseContractListContent'));
		}
	}

	win.widgetpurchasecontract_onload = Load;
	win.widgetpurchasecontract_refresh = Refresh;
	win.widgetpurchasecontract_department_onload = Department_Onload;
})(window);