ff.dom.init(window);
var cont=null;
function testError(response,e)
{
	cont.appendChild(DIV({"class":"error"}, response, BR(), "ERROR: "+e,HR()));	  									
}

function testOk(test)
{
	cont.appendChild(DIV({"class":"ok"}, test,HR()));	  									
}

function append(event)
{
	ff.dom.append(cont,
		SPAN({"style" : "color: red;"}, new Date().getTime(), HR()));
}

function append2(event)
{
	ff.dom.append(cont,
		SPAN({"style" : "color: red;"}, "DOUBLE!", HR()));
}

function replace(event)
{
	ff.dom.replace(cont,
		SPAN({"style" : "color: blue;"}, new Date().getTime(), HR()));
}

function test()
{
	cont=document.getElementById("testContainer");
    
    cont.appendChild(
        DIV({style:"display:none"},
            SPAN({"class":"foo"},"A"),
            SPAN({"class":"foo"},"B"),
            DIV({"id":"bar"}, 
                SPAN({"class":"foo"},"C"))));
    
    var cnt=0;
    var count=function()
    {
      cnt++;  
    };    
    ff.forElement("SPAN", "foo", count);
    var cntAll=cnt;
    cnt=0;
    ff.forElement("SPAN", "foo", count, document.getElementById("bar"));
    if (cntAll == 3 && cnt == 1)  
  		testOk("forElement successful.");
    else
  		testError("forElement failed. cntAll = "+cntAll+" / cnt = "+cnt);
    
	console.debug("log test: %d, %s, %o, %x", 42, "hello", ff, cont);
	console.error("Error test. Not a real error");
	testOk("onload called");

	ff.request({
		url: "test/data.txt",
		response: function(response)
		{
			try
			{
	  			var text=response.text();
	  			if ( text == "Test content for text request")
			  		testOk(".text() successful.");
			  	else
			  		throw "AJAX-text does not match";
			}
			catch(e)
			{
				testError(response,e);
			}
		},
		error: testError
	});
	ff.request({
		url: "test/data.xml",
		params: { a : 1, b : "hallo"},
		response: function(response)
		{
			try
			{
				var xml=response.xml();
				if (xml.firstChild.tagName != "test")
					throw "no test element";
				if (xml.firstChild.firstChild.nodeValue != "Ok.")
					throw "test element content does not match";
		  		testOk(".xml() successful.");	  				
			}
			catch(e)
			{
				testError(response,e);
			}
		},
		error: testError
	});
	ff.request({
		url: "test/data.json",
		response: function(response)
		{
			try
			{
				var json=response.json();
				if (json.test != "Ok.")
					throw "json.text does not match";
		  		testOk(".json() successful.");	  				
			}
			catch(e)
			{
				testError(response,e);
			}
		},
		error: testError
	});
	ff.request({
		url: "test/data.html",
		response: function(response)
		{
			try
			{
				var html=response.html();
		  		cont.appendChild(ff.dom.element("DIV",html));	  				
		  		cont.appendChild(HR());	  				
			}
			catch(e)
			{
				testError(response,e);
			}
		},
		error: testError
	});
	ff.request({
		waitCursor: true,
		method: "POST",
		url: "test/post.php",
		params: { a : 1, b : "A" },
		response: function(response)
		{
			try
			{
				var text=response.text();
				if (text.indexOf("<?php") == 0)
				{
					throw "Not running on PHP server";
				}
				else if (text != "a=1,b=A")
				{
					throw "POST params do not match";
				}
				testOk("POST successful.");	  									
			}
			catch(e)
			{
				testError(response,e);
			}
		},
		error: testError
	});
	  
}

ff.event.onload(function()
{
    test();

	var btAppend=document.getElementById("btAppend");	
	ff.event.add(btAppend, "click", append);
	ff.event.add(btAppend, "dblclick", append2);
	var btReplace=document.getElementById("btReplace");	
	ff.event.add(btReplace, "click", replace);

	var btTest=document.getElementById("btTest");	
	ff.event.add(btTest, "click", test);
});	
