
var searchResultsRequest = {};
var sum = 0;
var name;


function search(serachValue)
{	
	showLoading();
	sum = 0;
	serachValue = serachValue || document.getElementById('SearchEnteties').value;
	name = serachValue;
	//$.getJSON("searchPersonEng.php", {searchPerson: serachValue}, function(data){
	APIService.searchPerson({searchPerson: serachValue}).then(function (result) { data = result.data;
		hideLoading();
		$("#loadingP").hide();
		$(".content").show();
		$.each(data.rows, function(index, row){
			sum++;
			//alert("before");
			var newTr = "<tr><td> <input type = \"checkbox\" name=\"EntitiesName[]\" value=\"" + row.name + "\">" + row.name + " (" + row.id + ")" + "</input> </td>" +
				 +"</tr>";
			//alert("after");
			$("#resultsTable tr:last").after(newTr);
		});						
	});
	$("#resultsDiv").show();
	$("#SearchEntetiesDiv").hide();	
};

function addObject()
{			
	var n = name || document.getElementById('SearchEnteties').value;
	
	var data = { 'EntitiesName[]' : [],
			insertData: true, name: n, sum: sum };
	$(":checked").each(function() {
	  data['EntitiesName[]'].push($(this).val());
	});
	if (data['EntitiesName[]'].length > 1){
		//$.post("ManageEntitiesEng.php?insertData=true&name=" + n + "&sum=" + sum, data);
		
		APIService.manageEntities(data).then(function (result) { data = result.data;
			getData(null);
		});

		//gotoPage("ManageEntities");
	}else{
		alert("You need to choose at least one iteam");
	}	
	
};

function getData(request)
{	
	$("#entityTable tr:gt(0)").remove();
	//$.getJSON("ManageEntitiesEng.php?getData=true", request, function(data) {
	APIService.manageEntities({getData:true}).then(function (result) { data = result.data;
		$("#loadingP").hide();
		$(".content").show();
		$("#resultsDiv").hide();
		$("#SearchEntetiesDiv").show();	
		$.each(data.rows, function(index, row){
			var n = row.Name.replace("'","\\'");
			var a = row.Aggregated.replace("'","\\'");
			var newTr = "<tr><td>" + row.Entity + "</td>" +
			"<td>" + row.Name + "</td>" +
			"<td>" + row.Aggregated + "</td>" +	
			"<td>" + "<input type=\"button\" value=\"Delete\" name=\"DeleteButton\" id=\"DeletesButton\" src=\"images/submit.gif\" class=\"send\" OnClick=\"javascript:deleteEntity('" + row.Entity + "','" + n + "','" + request + "');\"/>" + "</td>" +
			"<td>" + "<input type=\"button\" value=\"Edit\" name=\"EditButton\" id=\"EditButton\" src=\"images/submit.gif\" class=\"send\" OnClick=\"javascript:editEntity('" + row.Entity + "','" + n + "','" + a + "');\"/>" + "</td>" +			
			"</tr>";			
			$("#entityTable tr:last").after(newTr);
		});
	});	
};

function deleteEntity(entity,name,request){
	APIService.manageEntities({deleteEntity:true, entity: entity, name: name}).then(function (result) { data = result.data;
	//$.getJSON("ManageEntitiesEng.php?deleteEntity=true&entity=" + entity + "&name=" + name, request, function(data) {		
		getData(null);
	});	
	//gotoPage("ManageEntities");
};

function editEntity(entity,name,aggregated){
	search(name);	
};

function selectAll()
{
	checkboxes = document.getElementsByName('EntitiesName[]');
	for(var i=0, n=checkboxes.length;i<n;i++) {
		checkboxes[i].checked = true;
	}
};

function clearAll()
{
	checkboxes = document.getElementsByName('EntitiesName[]');
	for(var i=0, n=checkboxes.length;i<n;i++) {
		checkboxes[i].checked = false;
	}
};



function showLoading()
{
	$("#loadingDiv").html("<p align=center><img src='images/loader.gif' alt='loading...' /></p>").show();
};

function hideLoading()
{
	$("#loadingDiv").hide();
};

function initializePage(request, userName, groupId)
{
	getData(request);
}

if (typeof angular == 'undefined')
{
	APIService.manageEntities = APIService.createAPI("ManageEntitiesEng.php");
	APIService.searchPerson = APIService.createAPI("searchPersonEng.php");
	/*APIService = {
			manageEntities: function(request){
				return {request: request, then: function(callback){
					$.getJSON("ManageEntitiesEng.php", request, function(data){
						callback({data: data});
					});
					
				}};
			}
	};*/
initializePage([], "Guest", 1);
}
else
{
	
}