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 if(thisid == -1)	//guestbook
	   document.getElementById('guestbook').innerHTML = '<div align=center><img src="p/loading.gif"></div>';
   else
  	 document.getElementById('newcomments'+currentid).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; 

   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() {
//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('captcha'); 
			//Change the image
			img.src = 'captcha.inc.php?' + Math.random();
		}
		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('captcha'); 
			//Change the image
			img.src = 'captcha.inc.php?' + Math.random();
		}
		else
		{
			//Set the content of the DIV element with the response text
			document.getElementById('newcomments'+currentid).innerHTML = receiveReq.responseText;
			//Get a reference to CAPTCHA image
			img = document.getElementById('cap'+currentid); 
			//Change the image
			img.src = 'captcha.inc.php?' + Math.random();
		}
 }
}

//Called every time when form is perfomed
function getParam(theForm) {
 //Set the URL
 var url = 'portfolio/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 += "&" + "message=" + encodeURIComponent( theForm.message.value );
 postStr += "&" + "captcha=" + encodeURIComponent( theForm.captcha.value );
 postStr += "&" + "aid=" + encodeURIComponent( theForm.aid.value );
 postStr += "&" + "id=" + encodeURIComponent( theForm.aid.value );
 postStr += "&" + "c=" + encodeURIComponent( theForm.c.value );
 currentid = theForm.aid.value;
 
 curbutton = document.getElementById('button'+currentid); 
  
 //Call the function that initiate the AJAX request
 makeRequest(url, postStr, theForm.aid.value);
}

function getGBParam(theForm) {
 //Set the URL
 var url = 'contact/guestbook_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 += "&" + "homepage=" + encodeURIComponent( theForm.homepage.value );
 postStr += "&" + "location=" + encodeURIComponent( theForm.location.value );
 postStr += "&" + "message=" + encodeURIComponent( theForm.message.value );
 postStr += "&" + "captcha=" + encodeURIComponent( theForm.captcha.value );
 postStr += "&" + "c=" + encodeURIComponent( theForm.c.value );
 currentid = -1;
 
 curbutton = document.buttongb; 
  
 //Call the function that initiates the AJAX request
 makeRequest(url, postStr, -1);
}

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);
}


