var pageSize = 25;						// amount of rows in the table
var counter = 0;						// amount of rows in sorted data set dsRecords
var offset = 0;							// counter/pageSize = page number - variable used to perform paginating

var module = 'all';						// module's name - initial value 'all' (all modules will be shown)
var version = 'all';					// version - initial value 'all' (all versions will be shown)
var entityname = '$';					// entity's name - initial value '$' (as regexp are used to match the name initially all entities will be shown)
var entitytype = Array(1,1,1,1);		// array settings all types (1-given type will be shown, 0 - will not be shown; positions in array:Array(Function, RPC call, Parameter, Select);

var regExpName;							// result of matching of entity's name with same regular expression

var missingDocs = new Array();			// array that will hold id's of rows for which the documentation request has been send

var types = new Array(); 				// array that provides translating of entity type's number to more friendly name
	types[0] = 'Function';
	types[1] = 'RPC call';
	types[2] = 'Parameter';
	types[3] = 'Selects'; 

// JPSpan framework
// send some information on requested documentation to the PHP script (sendrequest method of messagingModule class (see jpspan_server.php))
var hwCallback = {
	sendrequest: function(module, version, type, name, comment) {}
}
	// create remote object
var remoteHW = new messagingmodule(hwCallback);
// end of JPSpan framework

// main filtering function (called by SearchFunction and during dsRecords instantiation)
function filterFunction(ds, row, rowNumber)
{
	var regExpName = new RegExp(entityname, "i");

	// if last record of the data set was processed (the table was builded)
	// create the index below the table
	if((rowNumber+1) == ds.getUnfilteredData().length)
	{
		UpdateIndex(offset, counter);
	}

	// add now column to the data set array providing friendly name of entity's type
	if(row["type"] && !row["type_name"])
	{
		row["type_name"]=types[row["type"]];
	}

	if(row["module"] == module || module=='all')								// if module in data set equals selected module or all modules selected continue processing this record
	{
		if(row["version"] == version || version=='all')							// if version in data set equals selected version process the record
		{
			if(row["name"].search(regExpName) != -1)							// if entity's name matches the specified string continue processing
			{
				if(entitytype[row["type"]] == 1)								// if entity's type as requested
				{
					if (counter  >= offset && counter  < pageSize+offset)		// if matched paginating conditions display the record
					{
						counter++;
						return row;
					} else {
						counter++;
						return null;
					}
				}
			}
		}	
	}
	return null;
}

// function activated on request (onclick events) trigging data set filtering
function SearchFilter(record)
{
	var record;						//starting record of displayed documentation table;
	if(!record)	record = 0;
	offset = record;				// set offset to record's value
	counter = 0;

	var actualmodule = document.getElementById('module').value;					// get the module name from select form field
	var actualversion = document.getElementById('version').value;				// get the version from the select form field
	var actualentityname = document.getElementById('entityName').value;			// get requested entity's name from text field

	if(!actualentityname) actualentityname = '$';

	// update global variables with selected ones
	module = actualmodule;
	version = actualversion;
	entityname = actualentityname;

	// get requested entities' types and update entitytype global array
	var F = document.getElementById('F');
	var R = document.getElementById('R');
	var P = document.getElementById('P');
	var S = document.getElementById('S');
	if (F.checked) { entitytype[0]=1; }else{ entitytype[0]=0; }
	if (R.checked) { entitytype[1]=1; }else{ entitytype[1]=0; }
	if (P.checked) { entitytype[2]=1; }else{ entitytype[2]=0; }
	if (S.checked) { entitytype[3]=1; }else{ entitytype[3]=0; }

	// filter the data
	dsRecords.filter(filterFunction);

	// mark all rows for which reporting of missing or insufficient documentation was triggered
	markRequestedDocs();
}

// function creating an index (page numbers) bellow the generated table
function UpdateIndex(offset, counter)
{
	// calculate the number of pages (imax)
	var i = 1;
	var imax = (counter/pageSize) + 1;
	// clean the index are
	document.getElementById('indexes').innerHTML = '';
	// print the numbers
	if(counter > pageSize)
	{
		for(i; i <= imax; i++)
		{
			if(offset == (( pageSize * i ) - pageSize ))
			{
				// highlight the current page
				document.getElementById('indexes').innerHTML += '<a onclick=SearchFilter('+(i*pageSize-pageSize)+');><font size=3><B>' + i +'</B></font></a> ';
			} else {
				if(counter !== ( pageSize * (i-1) ))
				{
					document.getElementById('indexes').innerHTML += '<a onclick=SearchFilter('+(i*pageSize-pageSize)+');>' + i +'</a> ';
				}
			}
		}
	}
	// print number of records found
	document.getElementById('summary').innerHTML = 'Found:' + counter;
}

// functions replace links for sending requests on missing documentations with an appropriate message
function markRequestedDocs()
{
	// for all records' ids stored in the missingDocs array
	// check if record is already displayed (if so, modify a link)
	for (z=0; z < missingDocs.length; z++)
	{
		if(document.getElementById(missingDocs[z]))
		{
			changeRequestionLink(missingDocs[z]);
		}
	}
}

// function adds id of the row for which documentation request was send to the missingDocs array
function updateRequestedDocsArray(id)
{
	missingDocs[missingDocs.length]=id;
}

// function gets comments specified by users and sends them to some php script for further processing
function GetComment(id, module, version, type, name)
{
	do {
		// get the comment from prompt window
		var note = prompt("Describe the problem with documentation:", "Enter a comment here (25-255 letters)");
	} while (note == "Enter a comment here (25-255 letters)")
	if(note == null || note.length < 20 || note.length > 255) {	
		alert('Invalid format of entered data' + "\n\n" + 'Message should contain 25-255 letters' + "\n\n" + 'Note that pup-up windows can not be blocked');
	} else {
		// update missingDocs table
		updateRequestedDocsArray(id);
		// change link/display message
		changeRequestionLink(id);
		// send request (JPSpam framework)
		remoteHW.sendrequest(module, version, type, name, note);
//		alert('ID: ' + id + "\n" + 'Module: ' + module + "\n" + 'Version: ' + version + "\n" + 'Type: ' + type + "\n" + 'Name: ' + name + "\n" + 'Note: ' + note + "\n");
	}
}

// for record with specified id function changes the link
function changeRequestionLink(id)
{
	document.getElementById(id).innerHTML = '<B>Request was sent</B>';
}

// replicate the 'title' info-message and shows it above the table
function showHelpMessage(message)
{
	if(message)
	{
		document.getElementById('help_message').innerHTML = message;
	} else {
		cleanHelpMessage();
	}
}

// set default message in the div area above the table
function cleanHelpMessage()
{
	document.getElementById('help_message').innerHTML = "Change the header fields to update the table's content";
}

// will crear freeText search text field
function clearFilterContent()
{
	document.getElementById('entityName').value = '';
}


