addOnload(makeRequest);
addOnload(initAll);
addOnload(OpenFile);

function addOnload(newFunction){
	var oldOnload = window.onload;
	
	if (typeof oldOnload == "function") {
		window.onload = function() {
			if (oldOnload){
				oldOnload();
			}
			newFunction();
		}
	}
	else{
		window.onload = newFunction;
	}
}


var usedNum= new Array(15);
// Get the HTTP Object

function getHTTPObject(){   
if (window.ActiveXObject) 
return new ActiveXObject("Microsoft.XMLHTTP");   
else if (window.XMLHttpRequest) return new XMLHttpRequest();   
else {      alert("Your browser does not support AJAX.");      
return null;   }
}

// Change the value of the outputText field

function setOutput(){    
if(httpObject.readyState == 4){       
 var images = httpObject.responseText;
 var sddpics = images.split(' ');
 for (var num=0;   num<8;   num++)
	{	
	var photoNum = sddpics.length - 4;

	
	var randomNum
	do{
	randomNum = Math.floor((Math.random() * photoNum));
	}
	
	while(usedNum[randomNum]);
	var photoid = "pix" + num;
	usedNum[randomNum]= true;
 document.getElementById(photoid).src = "photos/" + sddpics[randomNum+2];
	}
} 
 } 
 // Implement business logic    
 function makeRequest(){        
 httpObject = getHTTPObject();    
 if (httpObject != null) {        
 httpObject.open("GET", "images.php?page="+Math.floor((Math.random() * 9999)), true);       
  httpObject.send(null);        
   httpObject.onreadystatechange = setOutput;    
   }
   }

   var httpObject = null;
   
function getNewNum(){
	Math.floor((Math.random() * photoNum));
	}
	
//menu

function initAll() {
	var allLinks = document.getElementsByTagName("a");
	
	for (var i=0; i<allLinks.length; i++) {
		if (allLinks[i].className.indexOf("menuLink") > -1) {
			allLinks[i].onclick = function() {return false;}
			allLinks[i].onmouseover = toggleMenu;
		}
	}
}

function toggleMenu() {
	var startMenu = this.href.lastIndexOf("/")+1;
	var stopMenu = this.href.lastIndexOf(".");
	var thisMenuName = this.href.substring(startMenu,stopMenu);

	document.getElementById(thisMenuName).style.display = "block";

	this.parentNode.className = thisMenuName;
	this.parentNode.onmouseout = toggleDivOff;
	this.parentNode.onmouseover = toggleDivOn;	
}

function toggleDivOn() {
	document.getElementById(this.className).style.display = "block";
}

function toggleDivOff() {
	document.getElementById(this.className).style.display = "none";
}


var xhr = false;

//Search function

function OpenFile(){
	document.getElementById("words").onkeyup=getNewFile;
}
function getNewFile(){
	fileRequest(this.href);
	return false;
}
function fileRequest(url){
	if (window.XMLHttpRequest){
		xhr = new XMLHttpRequest();
	}
	else {
		if (window.ActiveXObject){
			try {
				xhr = new ActiveXObject("Microsolf.XMLHTTP");
			}
			catch (e) {}
		}
	}
	if (xhr) {
		xhr.onreadystatechange =showContents;
		xhr.open("GET", "searchbackend.php?words="+document.getElementById("words").value, true);
		xhr.send(null);
	}
	else {
		document.getElementById("outputText").innerHTML = "Sorry, but I could'nt create an XMLHttpRequest";
	}
}
function showContents(){
	if (xhr.readyState ==4){
		if (xhr.status == 200){
			var outMsg = (xhr.responseXML && xhr.responseXML.contentType=="text/xml")? xhr.responseXML.getElementsByTagName("choices")[0].textContent : xhr.responseText;
		}
		else{
			var outMsg ="there was a problem with the request" +xhr.status;
		}
		document.getElementById("outputText"). innerHTML =outMsg;
	}
}

//left menu search

function ChgText(a,s) {
	

var e = document.getElementById("words");
e.value = a ;
getNewFile(a);
return true;	
}

