/* ########### */
/*    ajax    */
/* ########## */
function getSSContent(element, action, params)
{
	$(element).innerHTML = "<table border=\"0\" cellspacing=\"0\" cellpadding=\"3\"><tr><td><img src=\"../../img/loading_anim.gif\"></td></tr></table>";

	reportError = function()
	{
		$(element).innerHTML = "<table border=\"0\" cellspacing=\"0\" cellpadding=\"3\"><tr><td>An error has occurred!</td></tr></table>";
	};

	var myAjax = new Ajax.Updater(
	{success: element},
	'../includes/action.php',
	{
		method: 'post',
		parameters: 'action=' + action + '&' + params,
		evalScripts: true,
		onFailure: reportError
	});

}

function toggleItem(element)
{
	if($(element).visible())
	{
		$(element).hide();
	}
	else
	{
		$(element).show();
	}
}

function getAddMoreDocuments(element, prestationId, tdButton)
{
	if($(element).visible())
	{
		$(element).hide();
		$(tdButton).hide();
	}
	else
	{
		$(element).show();
		$(tdButton).show();

		var params = "prestation_id=" + prestationId;

		getSSContent(element, "getAddMoreDocuments", params);
	}
}

function addDocuments(prestationId, cboDocument, documentList, chooseList)
{
	var documentIds = "";
	var length = $(cboDocument).options.length;
	var selected = false;

	if(length > 0)
	{
		for(var i=0;i<length;i++)
		{
			if($(cboDocument).options[i].selected)
			{
				documentIds += $(cboDocument).options[i].value + "_";
				selected = true;
			}
		}

		if(selected)
		{
			documentIds = documentIds.substring(0, documentIds.length-1);

			var params = "document_id=" + documentIds + "&prestation_id=" + prestationId + "&choose_list=" + chooseList;

			getSSContent(documentList, "addDocuments", params);
		}
		else
		{
			alert("No files have been selected");
		}
	}
	else
	{
		alert("No files are available to select");
	}
}

function reloadDocumentList(prestationId, chooseList)
{
	var params = "prestation_id=" + prestationId;

	getSSContent(chooseList, "reloadDocumentList", params);
}

function putPrestationToRead(prestationId, tdValue, tdFlagAction)
{
	$(tdValue).innerHTML = "read";

	var params = "prestation_id=" + prestationId;

	getSSContent(tdFlagAction, "markPrestationAsRead", params);
}

function putPrestationToUnread(prestationId, tdValue, tdFlagAction)
{
	$(tdValue).innerHTML = "unread";

	var params = "prestation_id=" + prestationId;

	getSSContent(tdFlagAction, "markPrestationAsUnread", params);
}

function validateAddPrestation()
{
	var valid = true;
	var error = "";

	var title = $('txt_title').value;

	if(title.length == 0)
	{
		valid = false;
		error += "You must enter a title";
	}
	
	if(title.length > 50)
	{
		valid = false;
		error += "Prestation title cannot be more than 50 characters long";
	}
	
	if(!valid)
	{
		alert(error);
	}

	return valid;
}

function getAddNewDocumentCategory(container)
{
	var params = "container=" + container;

	if($(container).innerHTML == "")
	{
		$(container).show();
		getSSContent(container, "getAddDocumentCategory", params)
	}
	else
	{
		if($(container).visible())
		{
			$(container).hide();
		}
		else
		{
			$(container).show();
		}
	}
}

function saveNewDocumentCategory(txtTitle, txtDescription, errorContainer, container)
{
	var title = $(txtTitle).value;
	var description = $(txtDescription).value;

	var params = "title=" + title + "&description=" + description + "&error_container=" + errorContainer + "&container=" + container;

	$(errorContainer).show();

	getSSContent(errorContainer, "saveNewDocumentCategory", params);
}

function reloadDocumentCategoryCombo(selectedId)
{
	var params = "selected_id=" + selectedId;

	getSSContent("cbo_doc_cat", "getSelectedDocumentCategoryComboBox", params);
}

function reloadContactCombo(container, cboAccount)
{
	if(!$(container).visible())
	{
		$(container).show();
	}

	var selectedValue = $(cboAccount).options[$(cboAccount).selectedIndex].value;

	if(selectedValue == "all_accounts")
	{
		getSSContent(container, "getAllContactCombo", "");
	}
	else
	{
		var params = "account_id=" + selectedValue;

		getSSContent(container, "reloadContactCombo", params);
	}
}

function getPrestationDetails(prestationId, container)
{
	toggleItem(container);
	
	var edit = 'td_pres_edit_' + prestationId;
	var docs = 'td_pres_docs_' + prestationId;
	$(edit).hide();
	$(docs).hide();

	var params = "prestation_id=" + prestationId;

	getSSContent(container, "getPrestationDetails", params);
}

function getEditPrestation(prestationId, container)
{
	toggleItem(container);
	
	var show = 'td_pres_' + prestationId;
	var docs = 'td_pres_docs_' + prestationId;
	$(show).hide();
	$(docs).hide();
	
	var params = "prestation_id=" + prestationId;
	
	getSSContent(container, "getEditPrestation", params);
}

function getAddPrestationDocuments(prestationId, container)
{
	toggleItem(container);
	
	var show = 'td_pres_' + prestationId;
	var edit = 'td_pres_edit_' + prestationId;
	$(show).hide();
	$(edit).hide();
	
	var params = "prestation_id=" + prestationId;
	
	getSSContent(container, "getAddPrestationDocuments", params);
}

function updatePrestation(prestationId, txtTitle, txtDescription, txtResources, txtProbability, txtStartDate, txtEndDate, txtClientName, chOrinuxSupport)
{
	var title = $(txtTitle).value;
	var description = $(txtDescription).value;
	var resources = $(txtResources).value;
	var probability = $(txtProbability).value;
	var startDate = $(txtStartDate).value;
	var endDate = $(txtEndDate).value;
	var clientName = $(txtClientName).value;
	var orinuxSupport = "";
	
	if($(chOrinuxSupport).checked)
	{
		orinuxSupport = "1";
	}
	else
	{
		orinuxSupport = "0";
	}
	
	var params = "prestation_id=" + prestationId + "&title=" + title + "&description=" + description + "&resources=" + resources + "&probability=" + probability + "&start_date=" + startDate + "&end_date=" + endDate + "&client_name=" + clientName + "&orinux_support=" + orinuxSupport;
	
	getSSContent("prestation_list", "updatePrestation", params);
}

// toggle
function toggleMe(id,OnOff) {
	itm = getItem(id);
	var isOn;
	if(!itm) return false;

	switch(OnOff){
		case 1: { itm.style.display = ''; isOn = 1; break; }
		case 2: { itm.style.display = 'none'; isOn = 0; break; }
		default: {
			if(itm.style.display == 'none') { itm.style.display = ''; isOn = 1; }
			else { itm.style.display = 'none'; isOn = 0; }
			break;
		} // end default
	} // end switch

	return isOn;

} // end toggleMe

/**************** collpase table *************/
function getItem(id) {
	var itm = false;
	if(document.getElementById)
	itm = document.getElementById(id);
	else if(document.all)
	itm = document.all[id];
	else if(document.layers)
	itm = document.layers[id];

	return itm;
} // end getItem