var xmlHttp
var page
//=======================================================================================
function showHint(str)
{
	xmlHttp=GetXmlHttpObject()
	var url="home.php"
	
	if (xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request")
		return
	} 
	
	if(str=="home")
	{
		url="home.php"
	}
	
	if(str=="research")
	{
		url="research.php"
	}
	
	if(str=="guestbook")
	{
		url="guestbook.php"
	}
	
	if(str=="about")
	{
		url="about.php"
	}
	
	if(str=="photos")
	{
		url="photos"
	}
	
	page=str;
	
	xmlHttp.onreadystatechange=stateChanged 
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
} 
//==================== HANDLE RESPONSE =============
function stateChanged() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{ 
		var response=xmlHttp.responseText;
		var sidebar="";
		var content="";
		
		var sideEnd=response.indexOf('</sidebar>');
		var sideBegin=response.indexOf('<sidebar>')+9;
		if(sideEnd > -1){
		
			// Extracting the side bar content
			sideEnd-=1;
			sidebar=response.substring(sideBegin,sideEnd);
						
			// Extracting the main content
			var contentBegin=sideEnd+11;
			var contentEnd=response.length;
			
			content=response.substring(contentBegin,contentEnd);
		}else{
			content=response;
		}
	
		var sideobj = document.getElementById("sidetxt");
		var contentobj = document.getElementById("contenttxt");
		
		sideobj.innerHTML = sidebar;
		contentobj.innerHTML = content;
		
	} 
} 

//=======================================================================================
function GetXmlHttpObject()
{ 
	var objXMLHttp=null
	
	if (window.XMLHttpRequest)
	{
		objXMLHttp=new XMLHttpRequest()
	}
	else if (window.ActiveXObject)
	{
		objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
	}
	return objXMLHttp
} 

//=======================================================================================

function saveGbEntry(){


	xmlHttp=GetXmlHttpObject()
	var url=""
	
	if (xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request")
		return
	} 
	
	
	
	var name = document.getElementById('KomName').value;
	var email = document.getElementById('KomEmail').value;
	var homepage = document.getElementById('KomHP').value;
	var text = document.getElementById('KomText').value;
	
	document.getElementById("sidetxt").innerHTML = "<br><br><br><br>Thanks!<br><br>Your entry is being saved...";
	
	url = "guestbook.php";
	
	var params = "senden=sent & KomName="+name+"&KomEmail="+email+"&KomHP="+homepage+"&KomText="+text;
	
	url = url+ "?" + params;
		
	xmlHttp.onreadystatechange=stateChanged 
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)

}

function updateEntry(){

	document.getElementById('send').disabled="true";

	var name = document.getElementById('KomName').value;
	var email = document.getElementById('KomEmail').value;
	var homepage = document.getElementById('KomHP').value;
	var text = document.getElementById('KomText').value;

	var exist=false;
	var content="";
	
	if(name.length>0){
		exist=true;
	}
	
	if(exist==true){
		content=content+ "<p><div class=\"separator\" style=\"margin-top: 20px;\"></div><h2>";
		content=content + name + " wrote:</h2>";
		content=content + text+"</p>";	
	}
	
	document.getElementById("newentry").innerHTML=content;
}

//////////////////////////////////////////////////////////////////////////////////////////////////
function pagePreview(link){
	if(page==link){
		return;
	}
	
	if(link=="photos"){
		return;
	}
	
	var curPage=page;
	
	showHint(link);
	page=curPage;
}

function stopPreview(){
	
	showHint(page);

}

//////////////////////////////////////////////////////////////////////////////////////////////////
function onload_init(str){
	
	load_image();
	
	if(str.length<=0){
		showHint('home');
	}else{
		showHint(str);
	}	
}


/*
Determines the title image of the website.
If the page is loading, pic a random picture for title. Pic the next picture, otherwise.
*/
function load_image(){

// Add new title images here! //////////////////////////////////
	var images = new Array();
		images.push("flower");
		images.push("sony");
		images.push("turkey");
		images.push("monotrail");
		images.push("glider");
		images.push("startrek");
		images.push("annapolis");
		images.push("pittsburgh");
		images.push("neighbourhood");	
		images.push("roses");		
		images.push("flight");
		images.push("cleaners");
		images.push("blueridge");
		images.push("mall");
		images.push("statueofliberty");
		images.push("reichstag");		
		
/////////////////////////////////////////////////////////////		 
		 
		 
	 // Extract image name 
	 var style="";
	 var cur_img = document.getElementById("color").style.backgroundImage;
	 var img_index=0;
	 	 
	 // Determine the next image to show 
	if(cur_img.length > 0){
		
		// If a picture is currently showing, pic the next one in the array
		
		cur_img = cur_img.substring((cur_img.indexOf('/img/')+5),(cur_img.indexOf('.jpg')));
	
			for (var i = 0; i < images.length; i++) 
		    {
				if(images[i]==cur_img)
				{
					img_index=i;
				}           
		    }
		img_index++;
		img_index = img_index % images.length;
	}
	else
	{
		// If the page is just loading pick a random picture
		img_index=Math.floor(Math.random()*images.length);		
		
	}
	
	//Update webpage
	style="url(../img/"+images[img_index]+".jpg)";
	document.getElementById("color").style.backgroundImage = style;
}
