// create search boxes in the banner
function addSearchInputs (imgpath, linkA, linkAid, linkB, linkBid, linkC, linkCid, searchpage) {
	var holder = document.getElementById("searchInputHolder");
	holder.innerHTML = "";
	var imgTitle = document.createElement("img");
	
	imgTitle.setAttribute("src", imgpath);
	imgTitle.setAttribute("alt", "Search for free mp3 music.");
	imgTitle.setAttribute("id", "titletext");
	var linkHolder = document.createElement("div");
	linkHolder.setAttribute("id", "linkHolder");
	holder.appendChild(imgTitle);
	holder.appendChild(linkHolder);
	var links = Array(linkA, linkB, linkC);
	var ID = Array(linkAid, linkBid, linkCid);

	for (i=0; i<3; i++) {
		var lnk = document.createElement("span");
		//lnk.setAttribute("class", "searchLinks");
		lnk.setAttribute("id", ID[i]);
		linkHolder.appendChild(lnk);
		document.getElementById(ID[i]).innerHTML = links[i];
		document.getElementById(ID[i]).className = "searchLinks";
	}
	addExtraSearchDivs();
	
	if (searchpage == "music") {
		document.getElementById(ID[0]).onclick = function() {loadBox ("scripts/music/songtitlesearchform.php");}
		document.getElementById(ID[1]).onclick = function() {loadBox ("scripts/music/songgenresearchform.php");}
		document.getElementById(ID[2]).onclick = function() {loadBox ("scripts/music/songartistsearchform.php");}
	}
	if (searchpage == "artist") {
		document.getElementById(ID[0]).onclick = function() {loadBox ("scripts/artists/artistnamesearchform.php");}
		document.getElementById(ID[1]).onclick = function() {loadBox ("scripts/artists/artistlocationsearchform.php");}
		document.getElementById(ID[2]).onclick = function() {loadBox ("scripts/artists/artistinstrumentsearchform.php");}
	}/**/
	
	
}


function addExtraSearchDivs () {
	var divA = document.createElement("div");
	divA.setAttribute("id", "searchHolder");
	var divB = document.createElement("div");
	divB.setAttribute("id", "searchHint");
	var divC = document.createElement("div");
	divC.setAttribute("id", "loadholder");
	var holder = document.getElementById("searchInputHolder");
	holder.appendChild(divA);
	holder.appendChild(divB);
	holder.appendChild(divC);

}

//AJAX load of boxes
function searchBoxChanged () {
	if (musicxmlHTTP.readyState==4){ 
		document.getElementById("loadholder").innerHTML = "";
		document.getElementById("searchHolder").innerHTML = "";
		document.getElementById("searchHolder").innerHTML=musicxmlHTTP.responseText;
	}
}
function loadBox (url) {
	
	document.getElementById("searchHolder").innerHTML = "";
	document.getElementById("loadholder").innerHTML = "<img src=\"flash/loader1.gif\" width=\"30px\" height=\"20px\"  />";
	musicxmlHTTP = GetXmlHttpObject();
	if (musicxmlHTTP==null)  {
	  	alert ("Your browser does not support AJAX!");
	  	return;
	}
	musicxmlHTTP.onreadystatechange=searchBoxChanged;
	url=url+"?sid="+Math.random();
	musicxmlHTTP.open("GET",url,true);
	musicxmlHTTP.send(null);
}
//clear the search boxes on click to enter
function clearSearch(inputid, originaltext, searchtype) {
	var text = document.getElementById(inputid).value;
	if (text==originaltext) {
		document.getElementById(inputid).value = "";
	document.getElementById("searchHint").innerHTML = "To show all "+searchtype+" press 'enter'.";
	}
	
	
}

//=========== SEARCH RESULTS ==================
function infoChanged() {
	
	if (RESULTSxmlHttp.readyState==4){ 
		document.getElementById("loadholder").innerHTML = "";
		document.getElementById("bgholder").innerHTML=RESULTSxmlHttp.responseText;
	}
}
function infoChangedB() {
	
	if (CxmlHttp.readyState==4){ 
		document.getElementById("loadholder").innerHTML = "";
		document.getElementById("bgholder").innerHTML=CxmlHttp.responseText;
	}
}
function nullEnter() {
	if (charCodeAt) {
		return "return false";
	}
}
//a. songs
//-------------- SONG SEARCH - LIST SONGS ** also used FOR ARTIST SEARCH
function listSongs(searchType, perpage, currentpage) {
	var x = document.getElementById("searchHint");
	if (x.innerHTML != "") {
		x.innerHTML = "";
	}
	document.getElementById("loadholder").innerHTML = "<img src=\"flash/loader1.gif\" width=\"30px\" height=\"20px\" />";

	switch (searchType) {
		case "title":
			var url = "scripts/music/listsongs_title.php";
			var title = document.getElementById("songtitle").value
			var post = "?title="+title+"&perpage="+perpage+"&currentpage="+currentpage;
			break;
		case "genre":
			var url = "scripts/music/listsongs_genre.php";
			var genre = document.getElementById("genre").value;
			post="?genre="+genre+"&perpage="+perpage+"&currentpage="+currentpage;
			break;
		case "artist":
			var url = "scripts/music/listsongs_artist.php";
			var artist = document.getElementById("artist").value
			post="?artist="+artist+"&perpage="+perpage+"&currentpage="+currentpage;
			break;
		case "artistname":
			var url = "scripts/artists/listartist_name.php"
			searchvalue = document.getElementById("artistname").value; 
			post="?artistname="+searchvalue+"&perpage="+perpage+"&currentpage="+currentpage;
			break;
		case "location":
			var url = "scripts/artists/listartist_location.php"
			searchvalue = document.getElementById("location").value; 
			post="?location="+searchvalue+"&perpage="+perpage+"&currentpage="+currentpage;
			break;
		case "instrument":
			var url = "scripts/artists/listartist_instruments.php"
			searchvalue = document.getElementById("instrument").value; 
			post="?instrument="+searchvalue+"&perpage="+perpage+"&currentpage="+currentpage;
			break;
			
	}
	RESULTSxmlHttp=GetXmlHttpObject();
	if (RESULTSxmlHttp==null) {
  		alert ("Your browser does not support AJAX!");
  		return;
  	} 

	RESULTSxmlHttp.onreadystatechange=infoChanged;
	url=url+post+"&sid="+Math.random();
	RESULTSxmlHttp.open("GET",url,true);
	RESULTSxmlHttp.send(null);
}


/* ----------- FOR THE COMMUNITY SEARCH -----------
*/

function addSearchInputsCom () {
	var holder = document.getElementById("searchInputHolder");
	holder.innerHTML = "";
	var imgTitle = document.createElement("img");
	
	imgTitle.setAttribute("src", "images/backgroundbars/title_communitysearch.png");
	imgTitle.setAttribute("alt", "Search for free mp3 music.");
	imgTitle.setAttribute("id", "titletext");
	var linkHolder = document.createElement("div");
	linkHolder.setAttribute("id", "linkHolder");
	holder.appendChild(imgTitle);
	holder.appendChild(linkHolder);
	//create inputs using javascript
	var box1 = document.createElement("select");
	box1.setAttribute("id", "country");
	box1.setAttribute("onchange", "insertRegion(this.value)");
	
	var box2 = document.createElement("select");
	box2.setAttribute("id", "region");
	var box3 = document.createElement("input");
	box3.setAttribute("id", "towncity");
	box3.setAttribute("value", "town or city");
	//box3.setAttribute("class", "towncityinput");
	var linkHolderDiv = document.getElementById("linkHolder");
	//alert(linkHolderDiv);
	linkHolderDiv.appendChild(box1);
	linkHolderDiv.appendChild(box2);
	linkHolderDiv.appendChild(box3);
	document.getElementById("towncity").onclick = function () {
		var text = document.getElementById("towncity").value;
		if (text == "town or city") {
			document.getElementById("towncity").value = "";
		}
	}
	var cntry = document.getElementById("country");
	document.getElementById("country").onchange = function () {insertRegion(this.value)};
	//need to populate the select boxes with options;
	fillcountry("country");
	//add default value to region state box
	var regOpt = document.createElement("option");
	regOpt.setAttribute("id", "regionnull");
	var region = document.getElementById("region");
	region.appendChild(regOpt);
	var tmpText = document.createTextNode("region/state");
	document.getElementById("regionnull").appendChild(tmpText);
	region.disabled = true;
	// add the search links to the bottom
	var links = Array("gigs", "artists", "songs");
	var ID = Array("comGig", "comArtist", "comSong");
	var extraDiv = document.createElement("div");
	extraDiv.setAttribute("id", "comSearchLinkHolder");
	linkHolder.appendChild(extraDiv);
	comLH = document.getElementById("comSearchLinkHolder");
	for (i=0; i<3; i++) {
		var lnk = document.createElement("span");
		//lnk.setAttribute("class", "searchLinks");
		lnk.setAttribute("id", ID[i]);
		comLH.appendChild(lnk);
		document.getElementById(ID[i]).innerHTML = links[i];
		document.getElementById(ID[i]).className = "searchLinks";
	}
	//add onclick funtions to the search inputs
	var gigs=document.getElementById("comGig");
	var arts=document.getElementById("comArtist");
	var song=document.getElementById("comSong");
	gigs.onclick = function() {communitySearch('gigs','10','1')};
	arts.onclick = function() {communitySearch('artists','5','1')};
	song.onclick = function() {communitySearch('songs','20','1')};
	addExtraSearchDivs ()
}

/*function communitySearch (x, y, z) {
	alert('hello');
}*/
//community search function
 function communitySearch(type, perpage, currentpage) {
	var country = document.getElementById("country").value;
	var region = document.getElementById("region").value
	var towncity = document.getElementById("towncity").value
	var x = document.getElementById("searchHint");
	if (x.innerHTML != "") {
		x.innerHTML = "";
	}
	document.getElementById("loadholder").innerHTML = "<img src=\"../flash/loader1.gif\" width=\"30px\" height=\"20px\" />";
	CxmlHttp=GetXmlHttpObject()
	post="?type="+type+"&country="+country+"&region="+region+"&towncity="+towncity+"&perpage="+perpage+"&currentpage="+currentpage;
	if (CxmlHttp==null) {
  		alert ("Your browser does not support AJAX!");
  		return;
  	} 
	var url="scripts/listcommunity_search.php";
	CxmlHttp.onreadystatechange=infoChangedB;
	url=url+post+"&sid="+Math.random();
	CxmlHttp.open("GET",url,true);
	CxmlHttp.send(null);

}