dojo.provide('custom.Resolution');

function searchResolutions()
{
	dojo.byId('formErrors').style.display = 'block';
	dojo.byId('formErrors').innerHTML = '<img src="/gfx/searching.gif" alt="Wyszukiwanie" title="Wyszukiwanie" align="left" /> &nbsp;&nbsp;&nbsp;&nbsp; Wyszukiwanie...';
	var theDojoForm = dijit.byId('searchResolutionsForm');
	if (theDojoForm.isValid())
	{
		dojo.xhrPost ({
	          url: '/view/resolutions/',
	          form: 'searchResolutionsForm',
	          handleAs: "json",
	          load: function (data) {
	          		 handleResults(data, 'searchFormPane');
	          },
	          error: function (error) {
	                  console.error ('Error: ', error);
	          }
	    });
	}
}

function getResolutionsByCategory(cid)
{
	dojo.xhrGet ({
          url: '/view/resolutions/cid/'+cid,
          handleAs: "json",
          load: function (data) {
          		 handleResults(data, 'categoryTreePane');
          },
          error: function (error) {
                  console.error ('Error: ', error);
          }
    });
}

function handleResults(data, paneID)
{
	if (data.status == 'OK')
	{
		dojo.byId('formErrors').innerHTML = '';
		try {
			var theTable = dojo.byId('theResultTable');
			for (var i=theTable.rows.length-1; i>=0; i--)
			{
				theTable.deleteRow(i);
			}
		}
		catch(e)
		{}
		
		if (data.counter > 0)
		{	
			dojo.byId('resultsContainer').innerHTML = '';
			var theResultTable = document.createElement('table');
			theResultTable.id = 'theResultTable';
			var theRow = theResultTable.insertRow(0);
			var theCell = theRow.insertCell(0);
			theCell.innerHTML = '<b>Data uchwalenia</b>';
			var theCell = theRow.insertCell(1);
			theCell.innerHTML = '<b>Numer uchwały</b>';
			/*
			var theCell = theRow.insertCell(2);
			theCell.innerHTML = 'Za';
			var theCell = theRow.insertCell(3);
			theCell.innerHTML = 'Przeciw';
			var theCell = theRow.insertCell(4);
			theCell.innerHTML = 'Wstrzymali';
			*/
			for (var i=0; i<data.counter; i++)
			{
				var passedOn = data.data[i].resolutionPassedOn;
				var theRow = theResultTable.insertRow(i*2+1);
				var theCell = theRow.insertCell(0);
				theCell.innerHTML = passedOn.substr(8, 2)+'.'+passedOn.substr(5, 2)+'.'+passedOn.substr(0, 4);
				var theCell = theRow.insertCell(1);
				theCell.innerHTML = data.data[i].resolutionNumber;
				/*
				var theCell = theRow.insertCell(2);
				theCell.innerHTML = data.data[i].resolutionFor;
				var theCell = theRow.insertCell(3);
				theCell.innerHTML = data.data[i].resolutionAgainst;
				var theCell = theRow.insertCell(4);
				theCell.innerHTML = data.data[i].resolutionAbstain;
				*/
				var theRow = theResultTable.insertRow(i*2+2);
				theCell = theRow.insertCell(0);
				theCell.colSpan = '2';
				theCell.style.paddingBottom = '10px';
				theCell.style.borderBottom = '1px solid gray';
				theCell.innerHTML = data.data[i].resolutionContent;
			}
			dojo.byId('resultsContainer').appendChild(theResultTable);
			dojo.byId('resultsPane').style.display = 'block';
			dijit.byId(paneID).toggle();
			if (!dijit.byId('resultsPane').open)
			{
				dijit.byId('resultsPane').toggle();
			}
		}
		else
		{
			dojo.byId('resultsContainer').innerHTML = 'Nie odnaleziono uchwał spełniających podane kryteria...';
			dojo.byId('resultsPane').style.display = 'block';
			dijit.byId(paneID).toggle();
			if (!dijit.byId('resultsPane').open)
			{
				dijit.byId('resultsPane').toggle();
			}
		}
	}
	else
	{
		if (data.message != '')
		{
			dojo.byId('formErrors').innerHTML = data.message;
			dojo.byId('formErrors').style.display = 'block';
		}
	}
}