
// AJAX REQUESTS

// REQUEST OBJECT CREATION
function createXHR() 
{
    var request = false;
        try {
            request = new ActiveXObject('Msxml2.XMLHTTP');
	        }
        catch (error2) {
            try {
                request = new ActiveXObject('Microsoft.XMLHTTP');
            }
            catch (error3) {
				try {
					request = new XMLHttpRequest();
				}
				catch (error) 
				{
					request = false;
				}
            }
        }
    return request;
}


//TEXT SCRIPT (MOST TOPICS)
function changeTopic( topic, page, divOut )
{
	
	// creates a new Object for the request
	var req = createXHR();
	
	//which div to target
	var div 	= document.getElementById( divOut );

	//a list of pictures to be placed at random in the div while loading
	var pics 	= new Array( 'diver', 'fish', 'hippo', 'octopus', 'orca', 'turtle' );
	var i		= Math.round( Math.random() * 5 );
	
	
	req.onreadystatechange = function()
	{ 
	
	// waiting...
	// prints an image to the div while processing
	div.innerHTML = '<div style="width:150px; height:150px; margin:0px 20px 0px 40px; background-image:url(assets/interface/waiters/'+ pics[ i ] +'.gif); background-align:right botton; background-repeat:no-repeat; " ></div>';

		/*
		readyState has different states:
		0: not initialised.
		1: connexion established.
		2: request recieived.
		3: answering.
		4: over.
		only the last is really useful :)
		*/
		
		if( req.readyState == 1 || req.readyState == 2 || req.readyState == 3){}
		
		
		if(req.readyState == 4)
		{
			/*
			req status can be 
			200 : ok
			404 : not found
			*/
			
			if(req.status == 200)
			{
			
				// prints to div
				div.innerHTML = req.responseText;
				
			}	
			else	
			{
			
				//sends error message
				div.innerHTML = "Error: returned status code " + req.status + " " + req.statusText;

			}	
		} 
	}; 
	 
	
	//and sends it to the formatting page
	req.open("POST", page, true);
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");  
	// prepares the data 
	var data = "topic="+ topic+"&include="+topic;    
	req.send( data );
	
	
}

//INCLUSION SCRIPT
function inclusion( folder, page, divOut, isText ){
	var req = createXHR();
	
	if (!divOut) divOut = 'rightFrame';
	var div 	= document.getElementById( divOut );
	var pics 	= new Array( 'diver', 'fish', 'hippo', 'octopus', 'orca', 'turtle' );
	var i		= Math.round( Math.random() * 5 );
	req.onreadystatechange = function()
	{ 
	// waiting...
	// prints an image to the div while processing
	div.innerHTML = '<div style="width:150px; height:150px; margin:0px 20px 0px 40px; background-image:url(assets/interface/waiters/'+ pics[ i ] +'.gif); background-align:right botton; background-repeat:no-repeat; " ></div>';

		if( req.readyState == 1 || req.readyState == 2 || req.readyState == 3){}
		if(req.readyState == 4 ){
			if(req.status == 200)
			{
				// prints to div
				
				if (isText == true){
					div.innerHTML = '<table width="100%" cellpadding="10"><tr><td>';
					div.innerHTML += req.responseText;
					div.innerHTML += '</td></tr></table>';
				}else{
					div.innerHTML = req.responseText;
				}
				
			}else{div.innerHTML = "Error: returned status code " + req.status + " " + req.statusText;}	} 
	}; 
	//and sends it to the formatting page
	req.open("POST", 'inclusion.php', true);
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");  
	
	// prepares the data 
	var data = "folder="+ folder +"&file="+page+'&holder='+div;    
	req.send( data );
}

//GALLERY SCRIPT
function gallery( folder ){
	var req = createXHR();

	var div 	= document.getElementById(  'leftFrame' );
	var pics 	= new Array( 'diver', 'fish', 'hippo', 'octopus', 'orca', 'turtle' );
	var i		= Math.round( Math.random() * 5 );
	req.onreadystatechange = function()
	{ 
	// waiting...
	// prints an image to the div while processing
	div.innerHTML = '<div style="width:150px; height:150px; margin:0px 20px 0px 40px; background-image:url(assets/interface/waiters/'+ pics[ i ] +'.gif); background-align:right botton; background-repeat:no-repeat; " ></div>';

		if( req.readyState == 1 || req.readyState == 2 || req.readyState == 3){}
		if(req.readyState == 4 ){
			if(req.status == 200)
			{
				// prints to div
				
					div.innerHTML = '<table width="300px" cellpadding="0"><tr><td>';
					div.innerHTML += req.responseText;
					div.innerHTML += '</td></tr></table>';
				
				
			}else{div.innerHTML = "Error: returned status code " + req.status + " " + req.statusText;}	} 
	}; 
	//and sends it to the formatting page
	req.open("POST", 'gallery.php', true);
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");  
	
	// prepares the data 
	var data = "folder="+ folder;    
	req.send( data );
}



//MAILER SCRIPT
function mailer( form, name, mail, msg, news, copy, country ){
	
	var req = createXHR();
	
	//indicates to the contact.php page which type of request to process
	var div = "";
	switch( form ){
		
		case 'contactForm':
		var div 	= document.getElementById(  'divcontact' );
		break;
		
		case 'tafForm':
		var div 	= document.getElementById(  'divtellafriend' );
		break;
		
		case 'newsletterForm':
		var div 	= document.getElementById(  'divnewsletter' );
		break;
		
	}



	var pics 	= new Array( 'diver', 'fish', 'hippo', 'octopus', 'orca', 'turtle' );
	var i		= Math.round( Math.random() * 5 );
	req.onreadystatechange = function()
	{ 
	// waiting...
	// prints an image to the div while processing
	div.innerHTML = '<div style="background-image:url(assets/interface/waiters/'+ pics[ i ] +'.gif); background-align:right botton; background-repeat:no-repeat; " ></div>';

		if( req.readyState == 1 || req.readyState == 2 || req.readyState == 3){}
		if(req.readyState == 4 ){
			if(req.status == 200)
			{
				// prints to div
				
					div.innerHTML = req.responseText;
				
				
			}else{div.innerHTML = "Error: returned status code " + req.status + " " + req.statusText;}	} 
	
	}; 
	//and sends it to the formatting page
	req.open("POST", 'contact/contact.php', true);
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");  
	
	// prepares the data 
	var data = "a=a&form="+ form + "&name="+ name+ "&email="+ mail+ "&msg="+ msg+ "&copy="+ copy+ "&news="+ news + "&country="+ country;    
	req.send( data );
}