// get the value of the selected search type
function getOption() {
	var opt = document.mus_srch.searchtype.options[document.mus_srch.searchtype.options.selectedIndex].value; 
	return opt;
}

function createRequestObject() { 

   var req; 
   if(window.XMLHttpRequest){ 
	  // Firefox, Safari, Opera... 
	  req = new XMLHttpRequest(); 
   } else if(window.ActiveXObject) { 
	  // Internet Explorer 5+ 
	  req = new ActiveXObject("Microsoft.XMLHTTP"); 
   } else { 
	  // There is an error creating the object, 
	  // just as an old browser is being used. 
	  alert('Sorry, I don\'t think you can use this feature of the site using this browser version.'); 
   } 

   return req; 
} 

// Make the XMLHttpRequest object 
var http = createRequestObject(); 

// request for ahsodes
function sendRequest(q, st, page) { 
	var urlIntro = "";
	if(page == "ahsodes") urlIntro = "/bmormeus/";
   // Open PHP script for requests 
   http.open('get', '/bmoremus/includes/suggest.php?q='+q+'&st='+st); //page+
   //alert('suggest.php?q='+q+'&st='+st);
   http.onreadystatechange = handleResponse; 
   http.send(null); 
	
} 

// request for music.ahsodes
function sendRequestTwo(q, st, page) { 
	var urlIntro = "";
	if(page == "ahsodes") urlIntro = "/bmormeus/";
   // Open PHP script for requests 
   http.open('get', 'includes/suggest.php?q='+q+'&st='+st); //page+
   http.onreadystatechange = handleResponse; 
   http.send(null); 
	
} 


function handleResponse() { 
	 
	if(http.readyState == 1 || http.readyState == 2 || http.readyState == 3){ 
		searchImg = document.createElement('img');
    	searchImg.setAttribute('src', "http://www.ahsodes.com/img/searching.gif");
    	searchImg.setAttribute('id', "searching");
    	document.getElementById('searchResults').appendChild(searchImg);
	}
	
	if(http.readyState == 4 && http.status == 200){ 
	  // Text returned FROM the PHP script 
		var response = http.responseText; 
		
		if(response) { 
			// UPDATE ajaxTest content 
			document.getElementById("searchResults").innerHTML = response; 
		} 

	} 

} 

// function that's supposed to get more information about clicked link
function getMoreInfo (st, val){
	var where;
	switch(st) {
		case "artist":
		  where = "WHERE artist = '"+val+"' ORDER BY artist";
		  break;
		case "label_year":
		  where = "WHERE MATCH(label_year) AGAINST('+"+val+"* ~term' IN BOOLEAN MODE) ORDER BY label_year";
		case "title":
		  where = "WHERE title = '"+val+"'";
		  break;
		  
		getDetails(where);
	}
}

// second request to server for more details
function getDetails(where) {
	// open php script for second request
	http.open('get', '/bmoremus/includes/find.php?wh='+where); 
	http.onreadystatechange = handleDetails; 
	http.send(null);
}

// function that's supposed to open more information in a floating window
function showMusic(info) {
	win = new Window('window_id', {className: "default", title: "Sample", width:200, height:150});		
	win.getContent().innerHTML = "<h1>"+info+"!!</h1>"; 
	win.setDestroyOnClose(); 
	win.showCenter(); 
}

function handleDetails() { 
	
	if(http.readyState == 1 || http.readyState == 2 || http.readyState == 3){ 
		searchImg = document.createElement('img');
    	searchImg.setAttribute('src', "http://www.ahsodes.com/img/searching.gif");
    	searchImg.setAttribute('id', "searching");
    	document.getElementById('searchResults').appendChild(searchImg);
		
		searchImg.style.position = "relative";
		searchImg.top.position = "50px";
		searchImg.left.position = "30px";
	}
	
	if(http.readyState == 4 && http.status == 200){ 
	  // Text returned FROM the PHP script 
		var response = http.responseText; 
		
		if(response) { 
			//display floating window with requested info
			showMusic(response);
			// UPDATE ajaxTest content 
			document.getElementById("searchResults").innerHTML = response; 
		} 

	} 

} 
