function refreshCaptcha(theElem) {
   img = document.getElementById(theElem); 
   //Change the image
   img.src = 'captcha.inc.php?' + Math.random();
}


//Gets the browser specific XmlHttpRequest Object 
function getXmlHttpRequestObject() {
 if (window.XMLHttpRequest) {
    return new XMLHttpRequest(); //Mozilla, Safari ...
 } else if (window.ActiveXObject) {
    return new ActiveXObject("Microsoft.XMLHTTP"); //IE
 } else {
    //Display our error message
    alert("Your browser doesn't support the XmlHttpRequest object.");
 }
}

//Our XmlHttpRequest object
var receiveReq = getXmlHttpRequestObject();
var currentid = 0;

//Initiate the AJAX request
function makeRequest(url, param, thisid) {
//If our readystate is either not started or finished, initiate a new request
 if (receiveReq.readyState == 4 || receiveReq.readyState == 0) 
 {
 	 if(thisid == -2)	//contact
	   document.getElementById('contact').innerHTML = '<div align=center><img src="p/loading.gif"></div>';
   else
  	 document.getElementById('newcomments'+thisid).innerHTML = '<div align=center><img src="p/loading.gif"></div>';
   //Set up the connection to captcha_test.html. True sets the request to asyncronous(default) 
   receiveReq.open("POST", url, true);
   //Set the function that will be called when the XmlHttpRequest objects state changes
//   receiveReq.onreadystatechange = updatePage(thisid); 
   receiveReq.onreadystatechange = function(xhr) { updatePage(thisid); }
   receiveReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
   receiveReq.setRequestHeader("Content-length", param.length);
   receiveReq.setRequestHeader("Connection", "close");

   //Make the request
   receiveReq.send(param);
 }   
}

//Called every time our XmlHttpRequest objects state changes
function updatePage(tid) {
//Check if our response is ready
	if (receiveReq.readyState == 4) 
	{
		if(currentid == -2) //contact
		{
			//Set the content of the DIV element with the response text
			document.getElementById('contact').innerHTML = receiveReq.responseText;
			//Get a reference to CAPTCHA image
			img = document.getElementById('cap'); 
			//Change the image
			img.src = 'captcha.inc.php?' + Math.random();
//			document.sendemail.reset();
		}
		else if(currentid == -1) //guestbook
		{
			//Set the content of the DIV element with the response text
			document.getElementById('guestbook').innerHTML = receiveReq.responseText;
			//Get a reference to CAPTCHA image
			img = document.getElementById('cap'); 
			//Change the image
			img.src = 'captcha.inc.php?' + Math.random();
		}
		else
		{
			//Set the content of the DIV element with the response text
			if(tid > 0)
				document.getElementById('newcomments'+tid).innerHTML = receiveReq.responseText;
			else
				document.getElementById('newcomments').innerHTML = receiveReq.responseText;
			//Get a reference to CAPTCHA image
			img = document.getElementById('cap'+tid); 
			//Change the image
			img.src = 'captcha.inc.php?' + Math.random();
//			document.entercomment.reset();
		}
 }
}

//Called every time when form is perfomed
function getParam(theForm) {
 //Set the URL
 var url = 'home/commentupdate.inc.php';
 //Set up the parameters of our AJAX call
 var postStr = "name=" + encodeURIComponent( theForm.name.value ) ;
 postStr += "&" + "email=" + encodeURIComponent( theForm.email.value );
 postStr += "&" + "website=" + encodeURIComponent( theForm.website.value );
 postStr += "&" + "message=" + encodeURIComponent( theForm.message.value );
 postStr += "&" + "captcha=" + encodeURIComponent( theForm.captcha.value );
 postStr += "&" + "id=" + encodeURIComponent( theForm.id.value );
 
 curbutton = document.getElementById('button'); 
  
 //Call the function that initiate the AJAX request
 makeRequest(url, postStr, theForm.id.value);
}


function getMailParam(theForm) {
 //Set the URL
 var url = 'contact/contact_update.inc.php';
 //Set up the parameters of our AJAX call
 var postStr = "name=" + encodeURIComponent( theForm.name.value ) ;
 postStr += "&" + "email=" + encodeURIComponent( theForm.email.value );
 postStr += "&" + "message=" + encodeURIComponent( theForm.message.value );
 postStr += "&" + "artist=" + encodeURIComponent( theForm.artist.value );
 postStr += "&" + "captcha=" + encodeURIComponent( theForm.captcha.value );
 postStr += "&" + "c=" + encodeURIComponent( theForm.c.value );
 currentid = -2;
 
 curbutton = document.buttoncontact; 
  
 //Call the function that initiates the AJAX request
 makeRequest(url, postStr, -2);
}


