function ajaxGet(div,action,functionExecute,loadingMessage,doTransparancy,cache,removeCache) {
	
    if(loadingMessage) {
        loading(loadingMessage);
    } else {
        loading("aan het laden..");
    }
	
    //show cached page
    //var newPage = getPage(action);
    //if(cache&&newPage&&!removeCache) {
    //    makeContent(div,newPage,functionExecute,doTransparancy,true);
    //    setLatestKey(action);        

    //} else {
    $.ajax({
        type: "GET",
        url: "servlet.php",
        scriptCharset: "UTF-8",
        data: action+'&ajax=true&unique='+Math.random(),
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            if(textStatus=="timeout") {
                loading("de verbinding is verbroken, probeer opnieuw..");
            }
        },
        success: function(content){
            //if(cache) {
            //  savePage(action,content);
            //}
            unsetLoading();
            makeContent(div,content,functionExecute,doTransparancy);
			 
        }
    });
//}
}

function ajaxPost(div,form,functionExecute,loadingMessage,doTransparancy) {
    if(loadingMessage) {
        loading(loadingMessage);
    } else {
        loading("aan het laden..");
    }
    var formHash = $('#'+form).attr("hash");

    if(validateForm(form)) {
        if(!formHash) {
            var method = $('#'+form).attr("method");
            var query = $('#'+form).serialize()+"&formName="+form;
			
            if(method&&method=="get") {
                var hash = $('#'+form).attr("hash");
                location = "#"+hash+"&"+query;
				
            } else {
                $.post("servlet.php", query+"&ajax=true",function(content) {
                    unsetLoading();
                    makeContent(div,content,functionExecute,doTransparancy);
                });
            }
        } else {
            location = '#'+formHash+'/'+$('#'+form).serialize();
            if(jQuery.browser.version=="7.0") {
                pageload(location.hash);
            }
        }
    }
}

function validateForm(form) {
	
    $('#'+form+' input').each(function(index) {
        if($(this).attr("validate")) {
            if(!validator($(this).attr("validate"),$(this).val())) {
                return false
            }
        }
    });
	
    return true;
}


var message = null;

function loading(text) {
		
    if(message) {
        message.text = text;
        message.identifier = message.identifier+1;
    } else {
        message = [{}];
        message.text = text;
        message.identifier = 1;
    }
	
    $('#loading').html(text	);
	
    $('#loadingHolder').css("visibility","visible");
    checkLoadingActive(1,message.identifier);
}

function unsetLoading() {
    if(message) {
        delete message;

        $('#loadingHolder').css("visibility","hidden");
        $('#loading').html("");
        inProgresEvent = '';
    }
}

function checkLoadingActive(attempt,identifier) {
    if(identifier==message.identifier) {
        if(message&&attempt==2) {
            $('#loading').html("nog steeds bezig..");
        } else if(message&&attempt==4) {
            $('#loading').html("er is een fout opgetreden, probeer de pagina te vernieuwen");
        }
		
        attempt++;
		
        setTimeout("checkLoadingActive('"+attempt+"','"+identifier+"');",(3000*attempt));
    }
}

function makeContent(div,content,functionExecute,doTransparancy,fromCache) {
    if(div) {
        if(doTransparancy) {
            $(div).fadeTo(300,0.0,function() {
                $(div).html(content);
                checkLinks(div);
				
                if(functionExecute) {
                    $(div).fadeTo(100,1.0,function() {		
                        functionExecute(content);
                    });
                } else {
                    $(div).fadeTo(100,1,0);
                }
            });
			
        } else {
            $(div).html(content);
            checkLinks(div);
            if(functionExecute) {
                functionExecute(content);
            }
        }
		
    } else if(functionExecute) {
        functionExecute(content);
    }
}

function makePopup(content,newTitle,newWidth,beforeFunction,afterFunction,closeFunction) {
    if(beforeFunction)
        beforeFunction();

    $('#multiUploadUploader').remove();
	
    if(popupBuffer.length==0) {
        $("body").prepend('<div id="darkLayer"></div>');
		
        $('#darkLayer').bind("click",function() {
            $('#popup').dialog('close');
        }).css("height",$(document).height());
		
        loading("Bezig met openen..");
		
        $('#darkLayer').fadeTo(300,0.7,createPopup(content,newTitle,newWidth,beforeFunction,afterFunction,closeFunction));
    } else {
        createPopup(content,newTitle,newWidth,beforeFunction,afterFunction,closeFunction)
    }
}

var popupBuffer = new Array();
function createPopup(content,newTitle,newWidth,beforeFunction,afterFunction,closeFunction) {

    var popup = '#popup';
    if($(popup).dialog("isOpen")) {
        popup = 'popup'+Math.ceil(100*Math.random());
        $('body').append('<div id="'+popup+'"></div>');
        popup = '#'+popup;
    }

    $(popup).html(content).dialog({
        show: "blind",
        title: newTitle,
        minHeight: 140,
        minWidth: 300,
        width: newWidth,
        resizable: false,
        open: function() {
            inProgresEvent = '';
            unsetLoading();
            popupBuffer.push(popup);
            $('#darkLayer').bind("click",function() {
                closePopup();
            })		  
		  
            if(afterFunction)
                afterFunction(content);
        },
        close: function() {
            if(closeFunction) {
                closeFunction();
            }
            
            popupBuffer = popupBuffer.remove(popup);
            //var index = popupBuffer.indexOf(popup);
            //popupBuffer.splice(index, 1);

            $(popup).dialog('destroy');
            $(popup).html("");
            $(popup).remove();
            if(popupBuffer.length==0) {
                $("#darkLayer").fadeTo(300,0.0,function() {
                    $("#darkLayer").remove();
                });
            }
        }
    });
	  
    checkLinks();
}

function closePopup(keepLayer) {
    popupLength = popupBuffer.length;
    for(var i = 0;i<popupLength;i++) {
        $(popupBuffer[i]).dialog('close');
    }

    if(!keepLayer) {
        $("#darkLayer").fadeTo(300,0.0,function() {
            $("#darkLayer").remove();
        });
    }

}

Array.prototype.remove=function(s){
    var index = 0;

    for(i=0;i<this.length;i++) {
        if(this[i]==s) {
            index = i;
            break;
        }
    }

    this[index] = "undefined";

    a = new Array();
    for(i=0;i<this.length;i++) {
        if(this[i]!="undefined") {
            a.push(this[i]);
        }
    }
    return a;
}

//hashMap
HashMap = function(){
    this.dict = {};
}
HashMap.prototype._shared = {
    id: 1
};
HashMap.prototype.put = function put(key, value){
    if(typeof key == "object"){
        if(!key.hasOwnProperty._id){
            key.hasOwnProperty = function(key){
                return Object.prototype.hasOwnProperty.call(this, key);
            }
            key.hasOwnProperty._id = this._shared.id++;
        }
        this.dict[key.hasOwnProperty._id] = value;
    }else{
        this.dict[key] = value;
    }
    return this; // for chaining
}
HashMap.prototype.get = function get(key){
    if(typeof key == "object"){
        if(this.dict) {
            return this.dict[key.hasOwnProperty._id];
        } else {
            return null;
        }
    }
    return this.dict[key];
}
HashMap.prototype.update = function update(key,value) {
    if(this.dict[key]) {
        this.dict[key] = value;
    }
    return this.dict[key];
}
HashMap.prototype.remove = function remove(key) {
    if(this.dict[key]) {
        this.dict[key] = null;
    }
}


//cacheSystem
var pageCache = new HashMap();
var key = null;
var div = "content";
function savePage(action,content) {
    if(pageCache.get(action) == null) {
        pageCache.put(action,content);
        setLatestKey(action);
    }
}

function getPage(action) {
    return pageCache.get(action);
}

function updatePage(action,content) {
    if(!content) {
        content = $('#'+getDiv()).html();
    }
    var cache = pageCache.get(action);
    if(cache) {
        pageCache.update(action,content);
    }
}

function removePage(action) {
    pageCache.remove(action);
}

function getLatestKey() {
    return key;
}

function setLatestKey(newKey) {
    key = newKey;
}

function getDiv() {
    return div;
}

