onload_functions.push ('process()');

var xmlHttp = createXmlHttpRequestObject();

function createXmlHttpRequestObject ()
{
    var xmlHttp;
    if (window.ActiveXObject)
    {
        try
        {
            xmlHttp = new ActiveXObject ("Microsoft.XMLHTTP");
        }
        catch ( e )
        {
            xmlHttp = false
        }
    }
    else
    {
        try
        {
            xmlHttp = new XMLHttpRequest();
        }
        catch ( e )
        {
            xmlHttp = false
        }
    }
    if ( !xmlHttp )
        alert ("Error createing the XMLHttpRequest object.");
    else
        return xmlHttp;
}

function process ()
{
    if ( xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
    {    	
        title = encodeURIComponent (document.getElementById ("subject").value);
        content = encodeURIComponent (document.getElementById ("message").value);
        xmlHttp.open ("GET","preview.php?text="+content+"&title="+title, true);
        xmlHttp.onreadystatechange = handleServerResponse;	
        xmlHttp.send(null);
    }
    else
    {
        setTimeout ('process()',100);
    }
}

function handleServerResponse ()
{	
    if (xmlHttp.readyState == 4)
    {
        if (xmlHttp.status == 200)
        {
            xmlResponse = xmlHttp.responseText;                    
            document.getElementById("live_preview").innerHTML = xmlResponse;
            setTimeout ('process()',100);
        }
        else
            alert ("There was a problem accessing the server: " + xmlHttp.statusText);
    }
}
