function changeProfileData(type, number, id, action) {
	if (number != null) var numberString = '-' + number;
	else var numberString = "";
	
	if (id == null) id = "";

	if (id == -1) {
		Element.insert('list-' + type, {top: '<div id="profile-' + type + numberString + '" class="profile-data-list"></div>'});
		$('profile-' + type + numberString).hide();
	}
	
	if (action == null) action = "myprofile";
	
	new Ajax.Updater('profile-' + type + numberString, '/?portalId=' + portalId + '&action=' + action + '&mode=change' + type.capitalize(), {
			method: "post",
			parameters: {
				number: number,
				id: id,
				template: template
			},
			evalScripts: true,
			onComplete: function(transport) {
				$('profile-' + type + numberString).show();
				$('form-' + type + numberString).focusFirstElement();
				scrollElementToViewport('profile-' + type + numberString, 1, 20);
			}
		});
	
	return false;
}

function saveProfileData(type, number, id, action) {
	if (number != null) var numberString = '-' + number;
	else var numberString = "";

	if (id == null) id = "";
	
	if (action == null) action = "myprofile";
	
	new Ajax.Updater('profile-' + type + numberString, '/?portalId=' + portalId + '&action=' + action + '&mode=save' + type.capitalize(), {
			method: "post",
			parameters: $('form-' + type + numberString).serialize(true),
			onComplete: function() {
					refreshUserStatusBar();
					new Ajax.Updater('profile-header', '/?portalId=' + portalId + '&action=' + action + '&mode=showHeader&template=' + template, {
						method: "post"
					});
				}
		});
	
	return false;
}

function showProfileData(type, number, id, func, action) {
	if (number != null) var numberString = '-' + number;
	else var numberString = "";

	if (id == null) id = "";
	
	if (action == null) action = "myprofile";

	if (id == -1) {
		$('profile-' + type + numberString).remove();
	} else {
		new Ajax.Updater('profile-' + type + numberString, '/?portalId=' + portalId + '&action=' + action + '&mode=show' + type.capitalize(), {
				method: "post",
				parameters: {
					number: number,
					id: id
				},
				onComplete: func
			}); 
	}
	
	return false;
}

function removeProfileData(type, number, id, action) {
	if (!confirm('Really delete?')) return false;
	
	if (action == null) action = "myprofile";
	
	var numberString = '-' + number;
	
	new Ajax.Request('/?portalId=' + portalId + '&action=' + action + '&mode=remove' + type.capitalize(), {
			method: "post",
			parameters: {id: id},
			onSuccess: function(transport) {
				$('profile-' + type + numberString).remove();
			}
		});
	
	return false;
}

var checkPortraitInterval;
var portraitURL;
function checkPortraitUpload() {
	if (portraitURL == null) portraitURL = $('image-portrait').src;
	$('image-portrait').src = "/loading-portrait.gif";
	checkPortraitInterval = window.setInterval("checkPortraitUploadInterval()", 500);
}

function checkPortraitUploadInterval() {
//	if ($('form-portrait') != null) $('form-portrait').remove();
	
	if ($('iframe-portrait').contentWindow.document.body.innerHTML.indexOf("SUCCESS") != -1) {
		portraitURL = $('iframe-portrait').contentWindow.document.body.innerHTML.evalJSON().portraitURL;
		showProfileData('portrait', null, null, function() {
				$('image-portrait').src = portraitURL;
			});
		window.clearInterval(checkPortraitInterval);
		
		refreshUserStatusBar();
	} else if($('iframe-portrait').contentWindow.document.body.innerHTML.indexOf("ERROR") != -1) {
		window.clearInterval(checkPortraitInterval);
		$('image-portrait').src = portraitURL;
		alert($('iframe-portrait').contentWindow.document.body.innerHTML.evalJSON().errorMessage);
	}
}

function removePortrait() {
	if (!confirm('Really delete?')) return false;
	
	new Ajax.Updater('profile-portrait', '/?portalId=' + portalId + '&action=myprofile&mode=removePortrait', {
			method: "post",
			onComplete: function() {
				new Ajax.Request('/?portalId=' + portalId + '&action=myprofile&mode=getPortraitURL&template=' + template, {
						onComplete: function(transport) {
							portraitURL = transport.responseText.evalJSON().portraitURL;
							$('image-portrait').src = portraitURL;
							refreshUserStatusBar();
						}
					});
			}
		});

	return false;
}

function removeItem(type, number) {
	$('tr-' + type + '-' + number).remove();
}

function addItem(type, row, number) {
	Element.insert('table-' + type, {bottom: row.replace(/##NUMBER##/g, number)});
	/** IE 7 Bug */
	$('table-' + type).hide();
	$('table-' + type).show();
}

function removeConfirmationNotice(type, id) {
	new Ajax.Updater('user-status-infos', '/?portalId=' + portalId + '&action=myprofile&mode=removeConfirmationNotice&type=' + type + '&originalPortalId=' + portalId + ((id != null) ? '&user=' + id : ''), {
			method: "post",
			onComplete: function() {
				refreshUserStatusBar();
			}
		});
}
