function sendToggleActive(formElement, contentId, contentType) {
	
	var activeStatus = formElement.value;
	var resultStatus = null;
		
	if(activeStatus != 1 || (activeStatus == 1 && confirm("Are you sure you want to deactivate this " + contentType))) {
		
		var loadingText = 'Activating';		
		if(activeStatus == 1)
			loadingText = 'Deactivating';
			
		createLoadingPop(loadingText + ' ' + contentType);
	
		var params = 'action=toggleactive&contentType=' + contentType + '&contentId=' + contentId + '&ieSucks=' + IeSucksUrlDate();
		
		resultStatus = new Ajax.Request('/rea/admin/ajaxaction', 
		{
			method: 'get',
			parameters: params,
			onSuccess: function(transport)
				{
					var responseXml = transport.responseXML;
					var xmlBase = responseXml.getElementsByTagName("activation")[0];
					var result = parseInt(xmlBase.getElementsByTagName("result")[0].firstChild.nodeValue);
					var newActiveStatus = parseInt(xmlBase.getElementsByTagName("newActiveStatus")[0].firstChild.nodeValue);					
										
					var newLoadingTextNode, optionText;
					
					if(result == 1) {				
						if(newActiveStatus == 1) {
							newLoadingTextNode = 'Successfully activated ' + contentType;
							optionText = 'Deactivate';
						}
						else {
							newLoadingTextNode = 'Successfully deactivated ' + contentType;
							optionText = 'Activate';
						}
							
						formElement.value = newActiveStatus;
					}
					
					// change link text
					var textContainer = document.getElementById('activeText' + contentId);
					textContainer.removeChild(textContainer.childNodes[0]);
					textContainer.appendChild(document.createTextNode(optionText));
					
					// change image icon
					document.getElementById('activeIcon' + contentId).src = "/images/template/ic-cont-" + optionText.toLowerCase() + ".png";
						
					modifyLoadingPop(newLoadingTextNode);					
					setTimeout("finalizeAjaxPop()", 1500);
					
					return newActiveStatus;
				}
		});			
	}
	
	return resultStatus;
}

function sendToggleFavorite(contentId, contentTypeId, hiddenFieldName)
{
	var favoriteHidden = $(hiddenFieldName);
	var isFavorite = favoriteHidden.value;
	var processAction = true;
	var favoriteTextContainer = $(hiddenFieldName + 'Text');
	
	if(isFavorite == 1)
		processAction = confirm("Are you sure you want to do this?");
		
	if(processAction)
	{
		var loadingText = 'Processing your request...';

		createLoadingPop(loadingText);
			
		var params = 'action=togglefavorite' + '&contentId=' + contentId + '&contentTypeId=' + contentTypeId + '&ieSucks=' + IeSucksUrlDate();
		
		new Ajax.Request('/rea/admin/ajaxaction', 
		{
			method: 'get',
			parameters: params,
			onSuccess: function(transport)
				{
					var responseXml = transport.responseXML;
					
					// get base xml element - use to get the three values we need to process
					var xmlBase = responseXml.getElementsByTagName("userFavorite")[0];
					var result = parseInt(xmlBase.getElementsByTagName("result")[0].firstChild.nodeValue);
					var newIsFavorite = parseInt(xmlBase.getElementsByTagName("isFavorite")[0].firstChild.nodeValue);					
					var ctid = parseInt(xmlBase.getElementsByTagName("contentTypeId")[0].firstChild.nodeValue);
					var newLoadingTextNode, newFavoriteText;
					
					if(result == 1) {
						
						if(newIsFavorite == 1) {
							if(ctid == 12) {
								newLoadingTextNode = 'This user is now in your network';
								newFavoriteText = ' Remove from network';
							}
							else
							if(ctid == 11) {
								newLoadingTextNode = 'This company is now in your network';
								newFavoriteText = ' Remove from my network';							
							}
							else {
								newLoadingTextNode = 'Successfully added to bookmarks';
								newFavoriteText = ' Remove from bookmarks';
							}
							
							if(ctid == 12 || ctid == 11) {
								$('networkImage').src = "/images/template/icon-network-remove.png";
							}
						}
						else {
							if(ctid == 12) {
								newLoadingTextNode = 'You have removed this user from your network';
								newFavoriteText = ' Add to network';
							}
							else
							if(ctid == 11) {
								newLoadingTextNode = 'You have removed this company from your network';
								newFavoriteText = ' Add to my network';							
							}
							else {
								newLoadingTextNode = 'Successfully removed from bookmarks';
								newFavoriteText = ' Bookmark';
							}
							
							if(ctid == 12 || ctid == 11) {
								$('networkImage').src = "/images/template/icon-network-add.png";
							}							
						}
							
						favoriteHidden.value = newIsFavorite;
						
						
						
						favoriteTextContainer.removeChild(favoriteTextContainer.childNodes[1]);						
						favoriteTextContainer.appendChild(document.createTextNode(newFavoriteText));
						
						
					}
					else
						newLoadingTextNode = 'Action failed';
					
					modifyLoadingPop(newLoadingTextNode);					
					setTimeout("finalizeAjaxPop()", 1500);
				}
		});			
	}
}

var processPop;
function createLoadingPop(loadingText)
{
		createOpaqueLayer();
		
		processPop = document.createElement('div');
		processPop.className = 'processPop';
		processPop.id = 'processPop';
		processPop.style.marginTop = (getHeightOffset() + 250) + 'px';
			
		var loadingTextNode = document.createTextNode(loadingText);
	
		var loadingImage = new Image(16, 16);
		loadingImage.src = '/images/admin/mozilla_blu.gif';
		
		processPop.appendChild(loadingImage);	
		processPop.appendChild(loadingTextNode);

		$('pageContainer').appendChild(processPop);
}

function modifyLoadingPop(newText)
{
	if(newText == null || newText == '')
		newText = 'Unable to complete your request';
		
	processPop.innerHTML = '';
	processPop.appendChild(document.createTextNode(newText));
}

function finalizeAjaxPop()
{
	$('pageContainer').removeChild($('processPop'));
	removeOpaqueLayer();
}

function openQuickEdit(fieldType, editIcon, dbFieldName, assocArray, contentId, contentTypeId) {

	var container;
	var textValue;
	var fieldTypes = fieldType.split("|");
	var formField = document.createElement(fieldTypes[0]);
	formField.className = "quickEditField";

	if(fieldTypes[1] != null)
		formField.type = fieldTypes[1];
	
	if(dbFieldName == 'Body') {
		container = document.getElementById('bodyContainer');
		textValue = container.innerHTML;
	}
	else {
		container = editIcon.parentNode;
		var textNode = container.childNodes[0];
		textValue = trim(textNode.nodeValue);			
	}

	formField.value = textValue;
	
	if(assocArray['width'] != null)
		formField.style.width = assocArray['width'];

	if(assocArray['height'] != null)
		formField.style.height = assocArray['height'];

	if(assocArray['styleId'] != null)
		formField.id = assocArray['styleId'];
			
	var saveButton = document.createElement("input");
	saveButton.type = "image";
	saveButton.src = "/images/template/popup-save-button.gif";
	
	var cancelButton = document.createElement("input");
	cancelButton.type = "image";
	cancelButton.src = "/images/template/popup-cancel-button.gif";
	
	var buttonContainer = document.createElement('div');
	buttonContainer.style.margin = "10px 0 0 0";
	buttonContainer.appendChild(saveButton);
	buttonContainer.appendChild(document.createTextNode(" "));
	buttonContainer.appendChild(cancelButton);
	
	var formHeader = document.createElement('h2');
	formHeader.className = 'popLayerHeader';
	formHeader.style.textTransform = "capitalize";
	formHeader.appendChild(document.createTextNode("Update " + dbFieldName + ":"));
	
	var popLayer = createPopupLayer({width:'500px'});
	new Draggable('popLayer', {revert: false});
	
	popLayer.appendChild(formHeader);
	popLayer.appendChild(formField);
	popLayer.appendChild(buttonContainer);
	
	formField.focus();
	
	if(dbFieldName == 'Body')
		tinyMCE.execCommand('mceAddControl', false, 'mceEditor');
		
	cancelButton.onclick = function () {
		if(dbFieldName == 'Body') {
			tinyMCE.execCommand('mceFocus', false, 'mceEditor');                    
			tinyMCE.execCommand('mceRemoveControl', false, 'mceEditor');
		}
		
		removePopupLayer();
	}
	
	saveButton.onclick = function () {		
		
		tinyMCE.triggerSave();
		
		resultStatus = new Ajax.Request('/rea/admin/ajaxaction', 
		{
			method: 'post',
			parameters: {action: 'updatecontent', contentTypeId: contentTypeId, contentId: contentId, dbFieldName: dbFieldName, dbFieldValue: formField.value},
			onSuccess: function(transport)
				{
					var responseXml = transport.responseXML;
					
					var xmlBase = responseXml.getElementsByTagName("updateResult")[0];
					var result = parseInt(xmlBase.getElementsByTagName("result")[0].firstChild.nodeValue);
					// var newText = xmlBase.getElementsByTagName("newText")[0].firstChild.nodeValue;
					
					var resultContainer = document.createElement("div");
					resultContainer.style.margin = "5px 0 0 0";
					resultContainer.style.display = "none";
					
					var resultHtml;
					
					if(result == 1) {
						// SUCCESS
						if(dbFieldName == 'Body') {
							container.innerHTML = formField.value;
						}
						else
							container.childNodes[0].nodeValue = formField.value;
							
						resultHtml = "<img style='position: relative; top: 2px;' src='/images/template/ic-checkmark.png' alt='' /> <span style='color: #579f4f; font-weight: bold; font-family: Arial, font-size: 12px;'>Update successful</span>";						
					}
					else {
						// FAILURE
						resultHtml = "Update failed";
					}
					
					resultContainer.innerHTML = resultHtml;
					popLayer.appendChild(resultContainer);
					Effect.Appear(resultContainer, {duration:1});

					var resetQuickEdit = function() {
						// show original value and hide form field - regardless if the update was successful or not
						if(dbFieldName == 'Body') {
							tinyMCE.execCommand('mceFocus', false, 'mceEditor');                    
							tinyMCE.execCommand('mceRemoveControl', false, 'mceEditor');
						}						
						removePopupLayer();
					}
										
					setTimeout(resetQuickEdit, 1500);
				}
		});
		
	}
}

function toggleQuickEdit(container, display) {
	var icons = getElementsByClassName("quickEditIcon", container);
	if(icons.length > 0) {
		var icon = icons[0];
		icon.style.visibility = display;
	}		
}


function sendToggleFeature(contentId, contentType, executeOnComplete) {
	
	createLoadingPop("Processing...");
	
	var params = 'action=togglefeatured&contentType=' + contentType + '&contentId=' + contentId + '&ieSucks=' + IeSucksUrlDate();
	
	new Ajax.Request('/rea/admin/ajaxaction', 
	{
		method: 'get',
		parameters: params,
		onSuccess: function(transport)
			{
				var responseXml = transport.responseXML;
				
				// get base xml element - use to get the three values we need to process
				var xmlBase = responseXml.getElementsByTagName("activation")[0];
				var accountTypeId = parseInt(xmlBase.getElementsByTagName("accountTypeId")[0].firstChild.nodeValue);
				var result = parseInt(xmlBase.getElementsByTagName("result")[0].firstChild.nodeValue);
				var endDate = xmlBase.getElementsByTagName("subscriptionEndDate")[0].firstChild.nodeValue;
				
				// alert("Account Type Id = " + accountTypeId);
				
				if(result == 1) {
					
					if(executeOnComplete == 'pageView') {
						
						modifyLoadingPop("Your content has been featured until " + endDate);
						
						var link = document.getElementById('featureLink');
						var linkParent = link.parentNode;
						linkParent.removeChild(link);
						var span = document.createElement("span");						
						linkParent.appendChild(document.createTextNode("Featured until " + endDate));
					}
				}
				else {
					// error
					modifyLoadingPop('An error has occurred. Please try again later');					
				}
				
				setTimeout("finalizeAjaxPop()", 1500);
			}
	});
}
