// Author::    henhey
// Copyright:: S2I Hamburg
// Since::   07.03.2007
//

function addInputField(form_id, field_name){
   //document.write('<input class="file" id="post_file" name="post[file]" size="30" type="file" />'); 
   theForm = $(form_id);
   if(!theForm.allInputFields)
    theForm.allInputFields = new Array();
   var length = theForm.allInputFields.length;
   
   var newNode = document.createElement("input");
   newNode.type = "file";
   if(arguments[2]) {
       newNode.size = arguments[2]; 
   }
   else {
       newNode.size = 30; 
   }
   // newNode.name = "file["+(length+1)+"]";
   newNode.name = field_name+"[file"+(length+1)+"]";
   Element.addClassName(newNode, 'file');
   

   var lastInput = getLastInput(theForm, "file"); 
   if (!lastInput) {
        alert("Kein File-Input-Field vorhanden!");
        return;
   } 
   theForm.inputContainer = lastInput.parentNode; 
   theForm.inputContainer.appendChild(newNode);
   theForm.allInputFields.push(newNode);
}




function clearInputFields(form_id){
    setTimeout("clearInputFields_now('"+form_id+"')", 1000);
}
function clearInputFields_now(form_id){
    theForm = $(form_id);
    var inputContainer = theForm.inputContainer;
    if(theForm.allInputFields) {
        for(var i=0; i<theForm.allInputFields.length; i++){
            inputContainer.removeChild(theForm.allInputFields[i]);
        }
    }
    theForm.allInputFields = new Array();
}

function getLastInput(theForm, inputType){
    
    var theLast = null;
    for(var i=0; i<theForm.elements.length; i++){
        if(theForm.elements[i].tagName.toLowerCase() == "input"){
            if(theForm.elements[i].type == inputType){
                   theLast = theForm.elements[i];
            }
        }
    }
    return theLast;
}