var EJS=require("ejs");
var fs=require("fs");



function MalacheTool(reqPath)
{
	this.require=require;
	this.loadModule=function(mn)
	{
		return require("../"+reqPath+"/Requires/"+mn);
	};
}

function LoadJsCode(req,res,session,application,sid,siteConf,jsCode,tpPath,home)
{
    res.headers["Connection"]="Keep-Alive";
    var jsCode="function(request,response,session,application,malache,home){"+jsCode+"}";
    try
    {
        var pager=(new Function("return "+jsCode+";"))();
    }
    catch (e)
    {
        res.statusCode=500;
        res.headers["Content-Type"]="text/plain;charset=utf-8";
        res.end(e.stack);
        return;
    }
    res.render=function(ro)
    {
        fs.readFile(tpPath,"utf8",function(err,tpText)
        {
            if (err)
            {
                res.statusCode=500;
                res.headers["Content-Type"]="text/plain;charset=utf-8";
                res.end("Template file is not found!");
                return;
            }
            try
            {
                ro.filename=req.url.split("?")[0];
                var html=EJS.render(tpText,ro);
            }
            catch (e)
            {
                res.statusCode=500;
                res.headers["Content-Type"]="text/plain;charset=utf-8";
                res.end(e.stack);
                return;
            }
            res.setCookie("malache2SESSION",sid);
            res.end(html);
        });
    };
    setImmediate(function()
    {
        try
        {
            res.setCookie("malache2SESSION",sid);
            pager(req,res,session,application,new MalacheTool(home),home+"/Templates");
        }
        catch (e)
        {
            res.statusCode=500;
            res.headers["Content-Type"]="text/plain;charset=utf-8";
            res.end(e.stack);
            return;
        }
    });
}



module.exports={loadJsCode: LoadJsCode};

@@ -43,19 +43,11 @@
 %22);%0A
-%0A%0A%0Afunction
+var
  Mal
@@ -58,130 +58,39 @@
 Tool
-(reqPath)%0A%7B%0A%09this.require=require;%0A%09this.loadModule=function(mn)%0A%09%7B%0A%09%09return require(%22../%22+reqPath+%22/Requires/%22+mn);%0A%09%7D;%0A%7D
+=require(%22./MalacheTool%22);%0A%0A  %0A
 %0A%0Afu

move MalacheTool into a standalone file.