function RenderGeneralAttachments( id ) {
	var o = $("#"+id);
	var datas = o.data( "datas" );
	var files = datas["files"];
	var i = 0;
	for (i in files) {
		var file = files[i];
		AddGeneralAttachment( id , file , datas.bReadOnly );
	}
}

function AddGeneralAttachment( id , file , ro ) {
	var o = $("#"+id);
	
	var dattach = $("<tr></tr>").addClass( "GeneralAttachment" ).data( "parent" , o );
	var sid = id + "_attach_" + o.data("numoffiles" );
	dattach.attr( "id" , sid );
	
	// title
	var dtitle = $("<td></td>").addClass( "ttl" );
	dtitle.html( "#" + (Math.floor(o.data( "numoffiles" ))+1) );
	dattach.append( dtitle );

	// upload
	var dupl = $("<td></td>").addClass( "upl");
	var a = $("<a></a>").attr( "href" , file.www_path ).attr( "target" , "attachment" ).html( file.filename );
	dupl.append( a );
	dattach.append( dupl );

	// comment
	var dcomment = $("<td></td>").addClass( "comment" );
	var dcomm = $("<td></td>").html( file.comment );

	dcomment.append( dcomm );
	dattach.append( dcomment );

	if (!ro) {
		// del
		var ddel = $("<td></td>").addClass( "del" );
		var inp = $("<input></input>").attr("type","button").attr("id",id+"_del_"+o.data("numoffiles") ).val(aJSLangDefs.Del_attachment).addClass( "minismb" );
		inp.data( "file" , file );

		inp.click( function() { DeleteGeneralAttachment( id , $(this) ); } );
		ddel.append( inp );

		dattach.append( ddel );
	}

	o.append( dattach );
	o.data( "numoffiles" , o.data( "numoffiles" ) + 1 );
}

function InitGeneralAttachments( id , datas ) {
	var o = $("#"+id);
	if (!o.size()) return;
	o.data( "datas" , datas );
	o.data( "id" , id );
	o.data( "numoffiles" , 0 );
	RenderGeneralAttachments( id );
	// AddEmptyGeneralAttachment( id );
}

function MoveGA( id , postfix , num ) {
	var next = num + 1;
	$("#"+id+postfix+next).attr( "id" , id + postfix + num );
	if (typeof $("#"+id+postfix+next).attr("name") )
		$("#"+id+postfix+next).attr( "name" , id + postfix + num );
}

function DeleteGeneralAttachment( id , btn ) {
	var file = btn.data( "file" );
	if (typeof file != "undefined") {
		var old = $("#"+id+"_deletes").val();
		if (old.length) old+=";";
		old+=file.id;
		$("#"+id+"_deletes").val( old );
	}

	var o = $("#"+id);
	var num = Math.floor( btn.attr("id").replace( o.data( "id" ) + "_del_" , "" ) );
	var f = $("#"+id+"_attach_"+num);
	f.remove();
	while (num<o.data( "numoffiles" )-1) {
		MoveGA( id , "_attach_" , num );
		MoveGA( id , "_file_" , num );
		MoveGA( id , "_comment_" , num );
		MoveGA( id , "_del_" , num );
		var dnum = id+"_attach_"+num;
		$("#"+dnum+" div.ttl").html( "#" + Math.floor(num+1) );
		num++;
	}
	o.data( "numoffiles" , o.data( "numoffiles" ) - 1 );
}

function AddEmptyGeneralAttachment( id ) {
	var o = $("#"+id);
	if (o.data( "numoffiles" )>=5) {
		alert( aJSLangDefs.Too_much_attachments );
		return;
	}
	var dattach = $("<tr></tr>").addClass( "GeneralAttachment" ).data( "parent" , o );
	var sid = id + "_attach_" + o.data("numoffiles" );
	dattach.attr( "id" , sid );
	dattach.data( "num" , o.data("numoffiles" ) );
	
	// title
	var dtitle = $("<td></td>").addClass( "ttl" );
	dtitle.html( "#" + (Math.floor(o.data( "numoffiles" ))+1) );
	dattach.append( dtitle );

	// upload
	var dupl = $("<td></td>").addClass( "upl");
	var inp = $("<input></input>").attr( "type" , "file");
	inp.attr( "name" , id + "_file_" + o.data("numoffiles") );
	inp.attr( "id" , id + "_file_" + o.data("numoffiles") );
	dupl.append( inp );
	dattach.append( dupl );

	// comment
	var dcomment = $("<td></td>").addClass( "comment" );
	var inp = $("<input></input>").attr( "type" , "text" );
	inp.attr( "name" , id + "_comment_" + o.data("numoffiles") );
	inp.attr( "id" , id + "_comment_" + o.data("numoffiles" ) );

	dcomment.append( inp );
	dattach.append( dcomment );

	// add
	var ddel = $("<td></td>").addClass( "del" );
	var inp = $("<input></input>").attr("type","button").attr("id",id+"_del_"+o.data("numoffiles") ).val(aJSLangDefs.Del_attachment).addClass( "minismb" );
	inp.click( function() { DeleteGeneralAttachment( id , $(this) ); } );
	ddel.append( inp );

	dattach.append( ddel );

	o.append( dattach );
	o.data( "numoffiles" , o.data( "numoffiles" ) + 1 );
}
