// Verschiebt Upload-Bild einen platz nach oben:

function formImageUp(fieldName,slot,internal_id) {
	var elm = $('fileupload_'+fieldName+slot);
	var upperField = elm.getPrevious();
	if (upperField) {
		var upperSlot = upperField.id.substr(fieldName.length+11); // including "fileupload_"
		formChangeImages(fieldName,slot,upperSlot,internal_id);
	}
}

// Verschiebt Upload-Bild einen Platz nach unten:

function formImageDown(fieldName,slot,internal_id) {
	var elm = $('fileupload_'+fieldName+slot);
	var lowerField = elm.getNext();
	if (!lowerField.hasClass("newfield")) {
		var lowerSlot = lowerField.id.substr(fieldName.length+11);
		formChangeImages(fieldName,slot,lowerSlot,internal_id);
	}
}

// Setzt hochgeladenes Bild in das dafür vorgeehene Div ein:

function formChangeImages(fieldName,a,b,internal_id) {
	var changeRequest = new Ajax('/sort_image.php',{
		method:'post',
		data:{
			field:fieldName,
			a:a,
			b:b,
			internal_id:internal_id
		},
		onComplete:function() {
			var elm = this.response.xml.getElementsByTagName("result")[0];
			if (elm.getAttribute("success")=="1") {
				var ea = $('fileupload_'+fieldName+a);
				var eb = $('fileupload_'+fieldName+b);
				var textA = $E('textarea',ea);
				var textB = $E('textarea',eb);
				var tmp = textA.getValue();
				textA.value = textB.getValue();
				textB.value = tmp;
				var imgA = $E('img',ea);
				var imgB = $E('img',eb);
				var tmp = imgA.getProperty('src');
				imgA.setProperty('src',imgB.getProperty('src'));
				imgB.setProperty('src',tmp);
			} else {
				var reason="";
				if (elm.firstChild!=null) reason=elm.firstChild.nodeValue;
				alert(getTxt('advertise','img_couldntmove')+"\n"+reason);
			}
		}
	});
	changeRequest.request();
}

// Löscht ein einzelnes hochgeladenes Bild (Für Einzeluploads wie z.B. logo-Bild in Accounts)

function deleteUploadedSingleImage(key,uniqid,imageoptions) {
	if (confirm(getTxt('advertise','img_delete_confirm'))) {
		var delRequest = new Ajax('/delete_image.php',{
			method:'post',
			data:{
				field:fieldName,
				uniqid:uniqid
			},
			onComplete:function() {
				var elm = this.response.xml.getElementsByTagName("result")[0];
				if (elm.getAttribute("success")=="1") {
					var elm=$('imageupload_'+field);
					if (elm) {
						var imgDiv = $E('div',elm);
						imgDiv.innerHTML = '<b>Kein Bild hochgeladen</b>';
						valueInput.value = '';
						var iframe = $E('iframe',elm);
						iframe.setProperty('src','/upload_field_single.php?field='+field+'&imageoptions='+imageoptions+'&uniqid=');
					} else {
						alert("Missing upload div!");
					}
				} else {
					var reason="";
					if (elm.firstChild!=null) reason=elm.firstChild.nodeValue;
					alert(getTxt('advertise','img_couldntdelete')+"\n"+reason);
				}
			}
		});
		delRequest.request();
		return true;
	}
	return false;
}

// Löscht ein hochgeladenes Bild innerhalb einer Bildsammlung (z.B. Inserat)

function deleteUploadedImage(fieldName,slot,internal_id) {
	if (confirm(getTxt('advertise','img_delete_confirm'))) {
		var delRequest = new Ajax('/delete_image.php',{
			method:'post',
			data:{
				field:fieldName,
				slot:slot,
				internal_id:internal_id
			},
			onComplete:function() {
				var elm = this.response.xml.getElementsByTagName("result")[0];
				if (elm.getAttribute("success")=="1") {
					$('fileupload_'+fieldName+slot).setStyle('display','none');
				} else {
					var reason="";
					if (elm.firstChild!=null) reason=elm.firstChild.nodeValue;
					alert(getTxt('advertise','img_couldntdelete')+"\n"+reason);
				}
			}
		});
		delRequest.request();
		return true;
	}
	return false;
}

// Fügt ein neues Upload-Feld unter einem Upload-Feld hinzu

function addUploadField(fieldName,slot,internal_id) {
	var elm = $('fileupload_'+fieldName+slot);
	var nextSlot = Number(slot)+1;
	var nextFS = fieldName+nextSlot;
	var newElm = elm.clone(true);
	var divOuter = newElm;
	divOuter.setProperty('id','fileupload_'+nextFS);
	var divInner = $E('div',divOuter);
	divInner.setProperty('id','imgupload_'+nextFS);
	var iframe = $E('iframe',newElm);
	iframe.setProperty('src','/upload_field.php?field='+fieldName+'&slot='+nextSlot+'&internal_id='+internal_id);
	var textArea = $E('textarea',newElm);
	var taName = textArea.getProperty('name');
	taName = taName.replace(/(.*?)\d+(.*)/,"$1"+nextSlot+"$2");
	textArea.setProperty('name',taName);
	newElm.injectAfter(elm);
}

// Updatet Thumbnail nach Upload ( für Einzelbilder)

function setThumbnailSingle(field,uniqid,imageoptions) {
	var elm=$('imageupload_'+field);
	if (elm) {
		var imgDiv = $E('div',elm);
		imgDiv.innerHTML = '<b>'+getTxt('advertise','adv_img_uploadedimage')+'</b>'+
			'<img src="/uploaded_images/'+field+'/'+uniqid+'.jpg?id='+$random(0,10000)+'" /><br/>'+
			getTxt('advertise','adv_img_deleteimage')+'<input type="checkbox" onClick="return deleteUploadedSingleImage(\''+field+'\',\''+uniqid+'\');">';
		var valueInput = $E('input[type=hidden]',elm);
		valueInput.value = uniqid;
		var iframe = $E('iframe',elm);
		iframe.setProperty('src','/upload_field_single.php?field='+field+'&imageoptions='+imageoptions+'&uniqid='+uniqid);
	} else {
		alert("Missing upload div!");
	}
}

// Updated Thumbnail nach Upload ( für Bildsammlungen)

function setThumbnail(slot,field,internal_id) {
	var elm=$('imgupload_'+field+slot);
	var html =
		'<div align="center"><b>'+getTxt('advertise','adv_img_uploadedimage')+'</b><img src="/object_thumb/'+internal_id+'/'+field+'/'+slot+'.jpg?id='+$random(0,10000)+'" /><br/>'+
		getTxt('advertise','adv_img_deleteimage')+' <input type="checkbox" onClick="return deleteUploadedImage(\''+field+'\','+slot+',\''+internal_id+'\');" /><br/>'+
		'<a href="javascript:formImageUp(\''+field+'\',\''+slot+'\',\''+internal_id+'\');">'+getTxt('advertise','adv_img_moveup')+'</a> | <a href="javascript:formImageDown(\''+field+'\',\''+slot+'\',\''+internal_id+'\');">'+getTxt('advertise','adv_img_movedown')+'</a>'+
		'</div>'+
		'<iframe class="uploadframe" frameborder="0" scrolling="no" marginwidth="0" marginheight="0" src="/upload_field.php?field='+field+'&slot='+slot+'&internal_id='+internal_id+'&replaceimg=1"></iframe>';
	elm.innerHTML = html;
}

