function decrease(sh,fh)
{
	// Close up the div
	var object = document.getElementById('eb_div').style;
	
	diff = fh - sh;
	add = Math.floor(diff/2);
	sh = sh + add;
	object.height = sh + "px";
	if(sh > fh)
	{
		// Div is still larger than required, increase
		window.setTimeout("decrease(" + sh + ",0)",50);
	}
	else
	{
		// Div is fully collapsed, hide div
		object.display = "none";				
	}
}

function fadeOut(op)
{	
	// Fade up the div
	op = op - 1;
	dop = op/100;
	var object = document.getElementById('eb_div').style;
	object.opacity = dop;
	object.MozOpacity = dop;
	object.KhtmlOpacity = dop;
	object.filter = "alpha(opacity=" + op + ")";
	
	if(op > 0)
	{
		// Continue to fade out the div
		window.setTimeout("fadeOut(" + op + ")",10);
	}
	else
	{
		
		
		// Hide the table using none
		var tableobject = document.getElementById('eb_table').style;
		tableobject.display = "none";
		
		// Lets close up the div
		window.setTimeout("decrease(75,0)",0);
	}	
}

function fadeIn(op)
{
	// Fade up the div
	op = op + 1;
	dop = op/100;
	var object = document.getElementById('eb_div').style;
	object.opacity = dop;
	object.MozOpacity = dop;
	object.KhtmlOpacity = dop;
	object.filter = "alpha(opacity=" + op + ")";
	
	if(op < 100)
	{
		// Continue to fade in the div
		window.setTimeout("fadeIn(" + op + ")",10);
	}		
}

function increase(sh,fh)
{
	// Open up the div
	var object = document.getElementById('eb_div').style;	
	
	diff = fh - sh;
	add = Math.ceil(diff/2);
	sh = sh + add;
	object.height = sh + "px";
	if(sh < fh)
	{
		// Div is still smaller than required, increase
		window.setTimeout("increase(" + sh + ",75)",50);		
	}
	else
	{
		// Div is at full height, block table
		var tableobject = document.getElementById('eb_table').style;
		tableobject.display = "block";
		
		// Fade in the div
		window.setTimeout("fadeIn(0)",0);
	}
}

function opensesame()
{
	// Disable the button
	var object = document.getElementById('eb_link');
	object.href = "#";
	
	// Change the div display from none to block
	var object = document.getElementById('eb_div').style;
	object.display = "block";
			
	// Change the height of the block from 0 to 75
	window.setTimeout("increase(0,75)",0);	
}

// ##### //

// Holds an instance of XMLHttpRequest
var xmlHttp = createXmlHttpRequestObject();

// creates an XMLHttpRequest instance
function createXmlHttpRequestObject() 
{
	// will store the reference to the XMLHttpRequest object
	var xmlHttp;
	// this should work for all browsers except IE6 and older
	try
	{
		// try to create XMLHttpRequest object
		xmlHttp = new XMLHttpRequest();
	}
	catch(e)
	{
		// assume IE6 or older
		var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0","MSXML2.XMLHTTP.5.0","MSXML2.XMLHTTP.4.0","MSXML2.XMLHTTP.3.0","MSXML2.XMLHTTP","Microsoft.XMLHTTP");
		
		// try every prog id until one works
		for(var i=0; i<XmlHttpVersions.length && !xmlHttp; i++)
		{
			try
			{
				// try to create XMLHttpRequest object
				xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
			}
			catch (e)
			{
				// ignore potential error
			}
		}
	}
	
	// return the created object or display an error message
	if(!xmlHttp)
	{
		alert("Error creating the XMLHttpRequest object.");
	}
	else
	{
		return xmlHttp;
	}
}

// Read a file from the server
function downloadBrochure(ci)
{
	// Send the message
			
	// Only continue if xmlHttp isn't void
	if(xmlHttp)
	{
		// Try to connect to the server
		try
		{
			//
			document.getElementById('eb_message').innerHTML = "<p>Submitting...</p>";
			
			// Initiate the asynchronous HTTP request
			var rand = Math.floor(Math.random()*999999)
			var name = escape(document.getElementById('eb_name').value);
			var email = escape(document.getElementById('eb_email').value);
			//alert("/ajax-download-pt.php?rand=" + rand + "&ci=" + ci + "&name=" + name + "&email=" + email);
			xmlHttp.open("GET","/ajax-download-pt.php?rand=" + rand + "&ci=" + ci + "&name=" + name + "&email=" + email,true);
			xmlHttp.onreadystatechange = handleRequestStateChange;
			xmlHttp.send(null);
		}
		catch(e)
		{
			// Display the error in case of failure
			//alert("Can't connect to server:\n" + e.toString());
		}
	}
}

// Functioncalled when the state of the HTTP request changes
function handleRequestStateChange()
{
	// When readystate is 4, we are ready to read the server response
	if(xmlHttp.readyState == 4)
	{
		// Continue only if HTTP status is OK
		if(xmlHttp.status == 200)
		{
			try
			{
				// Do something with the response from the server
				handleServerResponse();
			}
			catch(e)
			{
				// Display error message
				//alert("Error reading the response: " + e.toString());
			}
		}
		else
		{
			// Display status message
			//alert("There was a problem retrieving the data:\n" + xmlHttp.statusText);
		}
	}
}

// Handles the response received from the server
function handleServerResponse()
{
	// Retrieve the server's response
	var response = xmlHttp.responseText;
	var arrayed = response.split("*");
	var wi = arrayed[0];
	var feedback = arrayed[1];
	var fileaddress = arrayed[2];
	
	if(feedback == "x")
	{
		// Send the message
		document.getElementById('eb_message').innerHTML = "<p>Sorry, an error occoured, please try again</p>";
	}
	else if(feedback == "1")
	{
		// Make sure the message is inline
		var object = document.getElementById('eb_td').style;
		object.width = "5px";
		
		// Send the message
		document.getElementById('eb_message').innerHTML = "<p>Thank you, please click the following link to download the brochure</p>";		
		document.getElementById('eb_message').innerHTML += "<p><a href=\"" + fileaddress + "\">Download Brochure</a></p>";
		
		// Fade out after 1 seconds
		window.setTimeout("fadeOut(100)",1000);
	}
	else
	{
		// Send the message
		document.getElementById('eb_message').innerHTML = feedback;
	}		
}