//
// Functions used to load the comments form from a WordPress post.
//
// The form will be loaded into a DIV with the ID 'formdiv'.

// From w3schools.com:

// Global var for the XmlHttpRequest object
var xmlHttp;

function GetXmlHttpObject()
{
	var xmlHttp=null;
	try
	{
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e)
	{
		// Internet Explorer
		try
		{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}


function test()
{
	alert("In test()");
	document.getElementById('formdiv').innerHTML="<p>test() succeeded</p>";
}


function stateChanged()
{
	switch(xmlHttp.readyState)
	{
		case 2:
			document.getElementById('formdiv').innerHTML="<p>Requested form...</p>";
			break;
			
		case 3:
			document.getElementById('formdiv').innerHTML="<p>Processing request...</p>";
			break;
			
		case 4:
			if (xmlHttp.status == 200)
			{
				document.getElementById('formdiv').innerHTML=xmlHttp.responseText;
			}
			else
			{
				// there was an error fetching the data
				document.getElementById('formdiv').innerHTML="<p>Error loading form ("+xmlHttp.status+"), to load manually <a href=\"/cgi-bin/sannu.pl?infotext="+infotext+"&infourl="+infourl+"&inforef="+referer+"\">Click here</a>.";
			}
			break;
	}
}

function loadForm(infotext, infourl, referer)
{
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{
		alert("No AJAX!");
		document.getElementById('formdiv').innerHtml="<a href=\"/cgi-bin/sannu.pl?infotext="+infotext+"&infourl="+infourl+"&inforef="+referer+"\">Click here</a> for the comments form.";
		return;
	}
	
	xmlHttp.onreadystatechange=stateChanged;
	xmlHttp.open("GET","/cgi-bin/sannu.pl?infotext="+infotext+"&infourl="+infourl+"&inforef="+referer,true);
	xmlHttp.send(null);
}
