var regionAc = new Array();
var regionWishAc = new Array();

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) {
//				alert(transport.responseText);
				$('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";
	
	hideRegionAcs(type);
	
	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";
	
	hideRegionAcs(type);
	
	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(confirmMessage) {
	if (confirmMessage == null) confirmMessage = 'Really delete?';
	
	if (!confirm(confirmMessage)) 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();
	
	hideRegionAcs(type);
	
	if (type == 'regions') {
		var options = {
			script:			'/?action=hotel&mode=showRegions&',
			varname:		'q',
			minchars:		3,
			className:		'autocomplete regions',
			delay:			0,
			timeout:		1000000,
			cache:			false,
			shownoresults: 	true,
			noresults:		'Keine passende Region gefunden, bitte geben Sie mindestens 3 Buchstaben ein!',
			callback:		function (obj) {
				$(this.id + "-type").value = obj.type;
				$(this.id + "-id").value = obj.id;
			},
			json:			true,
			maxentries:		20000
		};
		
		var json = new AutoComplete('region-' + number, options);
		regionAc.push(json);
	}
	
	if (type == 'regionsWish') {
		var options = {
			script:			'/?action=hotel&mode=showRegions&',
			varname:		'q',
			minchars:		3,
			className:		'autocomplete regions',
			delay:			0,
			timeout:		1000000,
			cache:			false,
			shownoresults: 	true,
			noresults:		'Keine passende Region gefunden, bitte geben Sie mindestens 3 Buchstaben ein!',
			callback:		function (obj) {
				$(this.id + "-type").value = obj.type;
				$(this.id + "-id").value = obj.id;
			},
			json:			true,
			maxentries:		20000
		};
		
		var json = new AutoComplete('region-wish-' + number, options);
		regionWishAc.push(json);
	}
}

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();
			}
		});
}

function hideRegionAcs(type) {
	if (type == 'regions') {
		for(var i=0; i<regionAc.length; i++) {
			if ($(regionAc[i].acID)) $(regionAc[i].acID).remove();
		}
	}
	
	if (type == 'regionsWish') {
		for(var i=0; i<regionWishAc.length; i++) {
			if ($(regionWishAc[i].acID)) $(regionWishAc[i].acID).remove();
		}
	}
}
