var mfFile = function (id,formData) {
    
    
    /*
        inherit from base-class
    */
    var baseClass = new mariaForm_fieldBase(id,formData);
    
    for (var i in baseClass) {
        if (typeof(baseClass[i]) == "function") {
            this[i] = baseClass[i];
        }
    }
    
    /*
        override anything...
    */
    var metaVals = {
        'name':'', 'type':'', 'size':'','tmpFileName':'', 'tmpFileWeb':''
    }
    
    var startUpload = function () {
        log("starting file upload");
        customLoaderStart();
        myUpload.set({
            'action': createAction()
        });
        myUpload.submit();
    }
    
    var completeHandler = function (response) {
        log("finished file upload");
        customLoaderFinish();
        try {
            var res = $.parseJSON(response);
        } catch (e) {
            log("calling customOnError");
            customOnError(formData.connectionErrMsg);
            return;
        }
        if (res.isError) {
            log("calling customOnError");
            customOnError(res.statusMesg);
            return;
        }
        for (var i in metaVals) {
            $("#mariaForm_"+id+"_"+i).val(res.data[i]);
        }
        //trigger the change event..
        $("#"+id).trigger('change');
        
        //user Callback to view the uploaded file
        customOnSuccess(res.data.name,res.data.type,res.data.size,res.data.tmpFileName,res.data.tmpFileWeb);
    }
    
    var createAction = function () {
        var apiClient = new restlikeApi(formData.className,formData.formServer,formData.connectionErrMsg);
        
        var additionalParams = {
            'restlikeApi_ContentType_EnforceNoJson':1,
            'fieldId':id
        }
        additionalParams[id] = id;
        
        for (var i in metaVals) {
            additionalParams["mariaForm_"+id+"_"+i] = $("#mariaForm_"+id+"_"+i).val();
        }
        
        return apiClient.getUrl()+'?'+$.param(objectMerge(apiClient.getParams('setValue'),additionalParams));
    }
    
    //startup
    var myUpload = $("#mariaForm_"+id+"_file").upload({
        'onSelect': startUpload,
        'onComplete': completeHandler,
        'autoSubmit': false,
        'name': "mariaForm_"+id+"_file"
    });

    //workaround for display-bug in firefox
    $("#mariaForm_"+id+"_file").parent().css("display","inline");
    
    if (formData.fields[id].linkInsteadForm) {
        $("#mariaForm_"+id+"_file").replaceWith("<a id='mariaForm_"+id+"_file' class='"+formData.fields[id]['class']+
            "' style='cursor:pointer'>"+formData.fields[id].linkCaption+"</a>");
    }
    if (formData.fields[id].imgLinkInsteadForm != '') {
        $("#mariaForm_"+id+"_file").replaceWith("<img id='mariaForm_"+id+"_file' class='"+formData.fields[id]['class']+
            "' src='"+formData.fields[id].imgLinkInsteadForm+"' style='cursor:pointer' alt='"+formData.fields[id].linkCaption+"' />");
    }
    
    //save the current file-element for later
    var html = $("#mariaForm_"+id+"_file").clone();
    
    
    this.getValue = function () {
        //return id;
        var privValue = new Object();
        
        for (var i in metaVals) {
            privValue[i] = $("#mariaForm_"+id+"_"+i).val();
        }
        
        return privValue;
    }
    
    this.setValue = function (newVal) {
        //do nothing, you cannot set the value for file-fields
        
        if(!newVal)
        {
            log('file values are empty ' + id);
            return;
        }
        
        //fill the hidden input fields
        for (var i in metaVals) {
            $("#mariaForm_"+id+"_"+i).val(newVal[i]);
        }
        
        //execute the on success callback
        customOnSuccess(newVal['name'], newVal['type'], newVal['size'], newVal['tmpFileName'], newVal['tmpFileWeb'] );
        
        //inform
        log('file values were set ' + newVal['name']);
    }
    
    //dummy functions for the user-hooks
    var customOnSuccess = function (fileName,fileType,fileSize,tmpFileName,tmpFileUrl) {
        $("#mariaForm_"+id+"_file").replaceWith("<span id='mariaForm_"+id+"_file' ><span>[change]</span> <a class='fileLink' href='"+tmpFileUrl+"' target='popup'>"+fileName+"</a></span>");
    }
    
    var customOnError = function (errorMsg) {
        mariaForm_forms[formData.id].customOnError(errorMsg);
    }
    
    var customLoaderStart = function () {
        mariaForm_forms[formData.id].customResetOnError();
        $("#mariaForm_"+id+"_file").replaceWith("<img id='mariaForm_"+id+"_file' class='ajaxImgLoader' src='/mariaLibs/mariaform/loader.gif' alt='loading' />");
    }
    
    var customLoaderFinish = function () {
        customReset();
    }
    
    var customReset = function () {
        $("#mariaForm_"+id+"_file").replaceWith(html);
    }
    
    this.reset = function () {
        for (var i in metaVals) {
            $("#mariaForm_"+id+"_"+i).val('');
        }
        customReset();
    }
    
    /**
        set a new js-function to be called on this.reset()
    */
    this.onReset = function (func) {
        if (typeof(func) == "function") {
            customReset = func;
        }
    }
    
    /**
        set a new js-function to be called after successful processing this form
    */
    this.onSuccess = function (func) {
        if (typeof(func) == "function") {
            customOnSuccess = func;
        }
    }
    
    /**
        set a new js-function to be called after validation or other errors when processing this form
    */
    this.onError = function  (func) {
        if (typeof(func) == "function") {
            customOnError = func;
        }
    }
    
    /**
        set a new js-function to initiate a loader
    */
    this.loaderStart = function (func) {
        if (typeof(func) == "function") {
            customLoaderStart = func;
        }
    }
    
    /**
        set a new js-function to end a loader
    */
    this.loaderFinish = function (func) {
        if (typeof(func) == "function") {
            customLoaderFinish = func;
        }
    }
    
}
