var cnt = Array( );
var max = 5;

function setField( id, nFields )
{
	cnt[ id ] = nFields;
	
}

function addField( id, name, type, chars, maxCustom )
{
	if( ! cnt[ id ] )
	{
		cnt[ id ] = 1;
	}
	 
	if( maxCustom )
	{
		max = maxCustom;
	}
		
	if( cnt[ id ] < max )
	{
		cnt[ id ]++;
		obj = document.getElementById( id );
		
		var newFile = document.createElement( 'input' );
		newFile.setAttribute( 'type', type );
		newFile.setAttribute( 'name', name + '[]' );
		newFile.setAttribute( 'size', chars );		
		newFile.setAttribute( 'id', name + '' + cnt[ id ] );
		
		if( type == 'file' )
		{
			newFile.setAttribute( 'onchange', 'insertImgTag( ' + cnt[ id ] + ' );' );
		}
		
		obj.appendChild( newFile );
	}
}

function delField( id )
{
	if( ! cnt[ id ] )
	{
		cnt[ id ] = 1;
	}
	
	if( cnt[ id ] > 1 )
	{
		obj = document.getElementById( id );	
		obj.removeChild( obj.lastChild );
		cnt[ id ]--;
	}
}

function insertImgTag( i )
{
	tinyMCE.execCommand( 'mceInsertContent', false, '<p>[img ' +i+ ']</p>' );
}
