var onloadFunctions = [];
var maxFields     = 4; // maximum number of fields to display
var fieldsCount   = 0;  // keep track of number of fields
var fieldsNumber  = 1;  // used to give name to fields
var first = 1; // Used to determine whether or not to look at the offset

function addOnLoadFunc ( onloadFunction )
{
    onloadFunctions[onloadFunctions.length] = onloadFunction;

    window.onload = function ( )
    {
        for ( var i = 0; i < onloadFunctions.length; i++ )
        {
            onloadFunctions[i]();
        }
    }
}

function getObj ( id )
{
	return document.getElementById ( id );
}

/* Display default number of fields */
uploadFunc = function ( )
{
    uplContainer  = getObj ( 'upload_fields_container' );
}

addOnLoadFunc ( uploadFunc );

function addUploadFields ( iCount, offset )
{
	maxFields = 5 - offset;
	if (this.first == 1) {
		fieldsNumber = offset;
		this.first = 0;
	}
    for ( var i = 0; i < iCount; i++ ) addUploadField ( );
}

function addUploadField ( )
{
	if ( uplContainer && fieldsCount < maxFields )
	{
		var newField = document.createElement ( 'div' );
		newField.innerHTML = '<img src="http://wipup.org/images/icons/delete.png" style="float: left; margin-left: 100px; margin-bottom: 10px; margin-top: 13px; clear: both;" onclick="removeUploadField(this.parentNode);" /> <input type="file" style="float: left; height: 23px; margin-left: 5px; margin-top: 10px;" name="attachment' + fieldsNumber + '" id="attachment' + fieldsNumber + '" />' + ' ';
		uplContainer.appendChild ( newField );
		fieldsCount++;
		fieldsNumber++;
	}
}

function removeUploadField ( oField )
{
	uplContainer.removeChild ( oField );
	fieldsCount--;
}

