(function(win){
	var viewModel = null;
	
	function Company()
	{
		var self = this;
		
		self.CompanyID = ko.observable();
		self.CompanyName = ko.observable("");
		self.Address = ko.observable("");
		self.ContactName = ko.observable("");
		self.ContactNumber = ko.observable("");
	}
	
	// Viewmodel
	function WidgetSiupplierViewModel()
	{
		var self = this;
		
		self.lists = ko.observableArray();
		self.selected = ko.observable();
		
		// Filter
		self.companyNameFilter = ko.observable("");
		self.addressFilter = ko.observable("");
		self.contactNameFilter = ko.observable("");
		self.contactNumberFilter = ko.observable("");
	
		self.listFilter = ko.computed(function()
		{
			return ko.utils.arrayFilter(self.lists(), function(item) {
				var pattCompanyName = new RegExp(self.companyNameFilter(), "i");
				var pattAddress = new RegExp(self.addressFilter(), "i");
				var pattContactName = new RegExp(self.contactNameFilter(), "i");
				var pattContactNumber = new RegExp(self.contactNumberFilter(), "i");
				
				return pattCompanyName.test(item.CompanyName()) && pattAddress.test(item.Address()) && pattContactName.test(item.ContactName()) && pattContactNumber.test(item.ContactNumber());
			});
		});
	
		self.load = function()
		{
			ShowLoader("Loading supplier List");
			
			var param = "Department=" + encodeURIComponent(GetValue(document.WidgetSupplierForm.WidgetSupplierServiceDepartmentCmb));
			param += "&WorkTypeID=" + GetValue(document.WidgetSupplierForm.WidgetSupplierServiceTypeCmb);
			
			var url = "widget/widgetsupplier/WidgetSupplierList.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 Company();
						item.CompanyID(dataObj[i].CompanyID);
						item.CompanyName(dataObj[i].CompanyName);
						item.Address(dataObj[i].FullAddress);
						item.ContactName(dataObj[i].ContactName);
						item.ContactNumber(dataObj[i].ContactNumber);
						self.lists.push(item);  
					}
				},
				error: function(xhr) {
					HideLoader();
					alert(xhr.responseText);
				}
			});
		};
		
		self.setRecord = function(item)
		{
			self.selected(item);
			
			ShowLoader("Loading supplier document");
			var url = "widget/widgetsupplier/WidgetSupplierDocument.asp?id=" + item.CompanyID();
			$.ajax({
				url: url,
				type: "post",
				async: true,
				cache: false,
				success: function(data) {
					HideLoader();
					$("#SupplierDocumentContent").html(data);
					$("#SupplierDocumentDialog").show();
					
				},
				error: function(xhr) {
					HideLoader();
					alert(xhr.responseText);
				}
			});
		};
		
		self.closeDialog = function()
		{
			$("#SupplierDocumentDialog").hide();
		};
		
		self.load();
	}
		
	function Refresh()
	{
		viewModel.load();
	}
	
	function Department_Onload()
	{
		ResetCombo(document.WidgetSupplierForm.WidgetSupplierServiceTypeCmb, "0", "-- All --");
		
		var param = "Department=" + encodeURIComponent(GetValue(document.WidgetSupplierForm.WidgetSupplierServiceDepartmentCmb));
		var url = "widget/widgetsupplier/WidgetSupplierWorkTypeList.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.WidgetSupplierForm.WidgetSupplierServiceTypeCmb, dataObj[i].WorkTypeID, dataObj[i].WorkType);
				}
				Refresh();
			},
			error: function(xhr) {
				HideLoader();
				alert(xhr.responseText);
			}
		});
	}
	
	function Load()
	{
		if(viewModel == null)
		{
			viewModel = new WidgetSiupplierViewModel();
			ko.applyBindings(viewModel, document.getElementById('SupplierListContent'));
		}
	}

	win.widgetsupplier_onload = Load;
	win.widgetsupplier_refresh = Refresh;
	win.widgetsupplier_department_onload = Department_Onload;
})(window);