function $m(theVar){
    return document.getElementById(theVar)
}
function remove(theVar){
    var theParent = theVar.parentNode;
    theParent.removeChild(theVar);
}
function addEvent(obj, evType, fn){
    if(obj.addEventListener){
        obj.addEventListener(evType, fn, true);
    }
    if(obj.attachEvent){
        obj.attachEvent("on"+evType, fn);
    }
}
function removeEvent(obj, type, fn){
    if(obj.detachEvent){
	obj.detachEvent('on'+type, fn);
    }else{
	obj.removeEventListener(type, fn, false);
    }
}
function isWebKit(){
	return RegExp(" AppleWebKit/").test(navigator.userAgent);
}
function ajaxUpload(form,upload_loc,id_element,inputnaam,limit,max_width){
    var action = $(form).attr('action');

    var url_action = 'ajax/ajaxupload.php?filename=name&maxSize=9999999999&maxW=200&fullPath=http://www.kavelgids.nl/&relPath='+ upload_loc + '&inputnaam='+inputnaam +'&limit='+limit +'&max_width='+max_width;
    var html_show_loading ='Bestand aan het uploaden...<br /><img src="http://www.kavelgids.nl/images/loader.gif" width="128" height="15" border="0" />';
        /*var html_error_http = '&lt; img src="http://www.kavelgids.nl/images/error.gif" width="16" height="16" border="0" /&gt; Error in Upload, check settings and path info in source code.';
        */
        var html_error_http = '<img src="http://www.kavelgids.nl/images/error.gif" width="16" height="16" border="0" /> Error in Upload, check settings and path info in source code.';
        var detectWebKit = isWebKit();
       
	form = typeof(form)=="string"?$m(form):form;
	var erro="";
	if(form==null || typeof(form)=="undefined"){
		erro += "Eerste parameter ontbreekt.\n";
	}else if(form.nodeName.toLowerCase()!="form"){
		erro += "Geen formulier.\n";
	}
	if($m(id_element)==null){
		erro += "element bestaat niet.\n";
	}
	if(erro.length>0){
		//alert("Fout in de upload:\n" + erro);
		return;
	}
        
	var iframe = document.createElement("iframe");
	iframe.setAttribute("id","ajax-temp");
	iframe.setAttribute("name","ajax-temp");
	iframe.setAttribute("width","0");
	iframe.setAttribute("height","0");
	iframe.setAttribute("border","0");
	iframe.setAttribute("style","width: 0; height: 0; border: none;");
	form.parentNode.appendChild(iframe);
	window.frames['ajax-temp'].name="ajax-temp";
        
	var doUpload = function(){              
            removeEvent($m('ajax-temp'),"load", doUpload);
            var cross = "javascript: ";
            cross += "window.parent.$m('"+id_element+"').innerHTML = document.body.innerHTML; void(0);";
            $m(id_element).innerHTML = html_error_http;
            $m('ajax-temp').src = cross;
            if(detectWebKit){
        	remove($m('ajax-temp'));
            }else{
                setTimeout(function(){ remove($m('ajax-temp'))}, 250);
            }
        }
	
        addEvent($m('ajax-temp'),"load", doUpload);
	form.setAttribute("target","ajax-temp");
	form.setAttribute("action",url_action);
	form.setAttribute("method","post");
	form.setAttribute("enctype","multipart/form-data");
	form.setAttribute("encoding","multipart/form-data");
	if(html_show_loading.length > 0){               
            $m(id_element).innerHTML = html_show_loading;
	}
	form.submit();
        form.setAttribute("action",action);
        form.setAttribute("target","_self");
}
