﻿function UpdateAssignedPodObjects(objectId, checked) {
    
    // following params are used from PodListAds.ascx:
    // podSlotId, txtAssign, txtRemove
    
    checkBoxLabelId = "txt_pod_" + objectId;
    var maxObjects = parseInt(document.getElementById('pod_max_objects').innerHTML);
    assignedObjects = parseInt(document.getElementById('pod_assigned_objects').innerHTML);
    var timestamp = new Date().getTime();
    
    if (checked)
    {	
		if (assignedObjects < maxObjects)
		{
			// jquery call
			$.get("/IS24Web/Member/PodEdit.aspx", { action: "assign", objectid: objectId, slotid: podSlotId, time: timestamp },
				function(data) {
					if (data == objectId)
					{
						setTimeout(function() {
							
							// update counter
							assignedObjects++;
							UpdateCounter(assignedObjects);
							
							// enable checkboxes
							EnableAllCheckBoxes();
							
							// switch labels
							SetCheckBoxLabel(checkBoxLabelId, txtRemove);
							
						}, delayTime);
					}
					else
					{
						// alert("The object couldn't be assigned: " + data);
					}
				}, 'text');
			
			// disable checkboxes
			DisableAllCheckBoxes();
			SetCheckBoxLabel(checkBoxLabelId, txtSaving);
		}
		else
		{
			// max objects assigned
			$("#chk_pod_" + objectId).attr("checked", false);
			
			// TODO: disable unchecked checkboxes
		}
    }
    else
    {	
		// jquery call
		$.get("/IS24Web/Member/PodEdit.aspx", { action: "remove", objectid: objectId, slotid: podSlotId, time: timestamp },
			function(data) {
				if (data == objectId)
				{
					setTimeout(function() {
					
						// update counter
						assignedObjects--;
						UpdateCounter(assignedObjects);
						
						// enable checkboxes
						EnableAllCheckBoxes();
						
						// switch labels
						SetCheckBoxLabel(checkBoxLabelId, txtAssign);
						
					}, delayTime);
				}
				else
				{
					// alert("The object couldn't be removed: " + data);
				}
			}, 'text');
		
		// disable checkboxes
		DisableAllCheckBoxes();
		SetCheckBoxLabel(checkBoxLabelId, txtSaving);
    }
}
// --------------------------------------------------------------------------------------------------------------------------------
function SetCheckBoxLabel(checkBoxLabelId, value)
{
	//alert("SetCheckBoxLabel: " + checkBoxLabelId);
	$("#" + checkBoxLabelId).text(value);
}

function UpdateCounter(value)
{
	$("#pod_assigned_objects").text(value);
}

// --------------------------------------------------------------------------------------------------------------------------------
function DisableAllCheckBoxes()
{
	$("#pod_objects input:checkbox").each(function() {
		this.disabled = true;
	});
}

function EnableAllCheckBoxes()
{
	$("#pod_objects input:checkbox").each(function() {
		this.disabled = false;
	});
}
// --------------------------------------------------------------------------------------------------------------------------------
function SelectAllPodObjects()
{
	var maxObjects = parseInt(document.getElementById("pod_max_objects").innerHTML);
    assignedObjects = parseInt(document.getElementById("pod_assigned_objects").innerHTML);
    var timestamp = new Date().getTime();
    var objectIds = "-1";
	
	$("#pod_objects input:checkbox:not(:checked)").each(function() {
		
		if (assignedObjects < maxObjects)
		{
			objectId = this.id.replace("chk_pod_", "");
			objectIds += "," + objectId;
			
			assignedObjects++;
			this.checked = true;
			
			checkBoxLabelId = "txt_pod_" + objectId;
			SetCheckBoxLabel(checkBoxLabelId, txtSaving);
		}
		else
		{
			return false;
		}
    });
    
    // adds objects to db
	$.get("/IS24Web/Member/PodEdit.aspx", { action: "assignall", objectids: objectIds, slotid: podSlotId, time: timestamp },
		function(data) {
			if (data == objectIds)
			{
				setTimeout(function() {
				
					// update counter
					UpdateCounter(assignedObjects);
					
					// enable checkboxes
					EnableAllCheckBoxes();
					
					// switch labels
					$("#pod_objects input:checkbox:checked").each(function() {	
						objectId = this.id.replace("chk_pod_", "");
						checkBoxLabelId = "txt_pod_" + objectId;
						SetCheckBoxLabel(checkBoxLabelId, txtRemove);
					});
					
				}, delayTime);
			}
			else
			{
				alert("The objects couldn't be removed: " + data);
			}
		}, 'text');
	
	// disable checkboxes
	DisableAllCheckBoxes();
}
// --------------------------------------------------------------------------------------------------------------------------------
function DeselectAllPodObjects()
{
	assignedObjects = parseInt(document.getElementById("pod_assigned_objects").innerHTML);
	var timestamp = new Date().getTime();
	var objectIds = "-1";
	
	$("#pod_objects input:checkbox:checked").each(function() {
	
		objectId = this.id.replace("chk_pod_", "");
		objectIds += "," + objectId;
		
		assignedObjects--;
		this.checked = false;
		
		checkBoxLabelId = "txt_pod_" + objectId;
		SetCheckBoxLabel(checkBoxLabelId, txtSaving);
	});
	
	// removes objects from db
	$.get("/IS24Web/Member/PodEdit.aspx", { action: "removeall", objectids: objectIds, slotid: podSlotId, time: timestamp },
		function(data) {
			if (data == objectIds)
			{
				setTimeout(function() {
				
					// update counter
					UpdateCounter(assignedObjects);
					
					// enable checkboxes
					EnableAllCheckBoxes();
					
					// switch labels
					$("#pod_objects input:checkbox").each(function() {
						objectId = this.id.replace("chk_pod_", "");
						checkBoxLabelId = "txt_pod_" + objectId;
						SetCheckBoxLabel(checkBoxLabelId, txtAssign);
					});
					
				}, delayTime);
			}
			else
			{
				alert("The objects couldn't be removed: " + data);
			}
		}, 'text');
	
	// disable checkboxes
	DisableAllCheckBoxes();
}