function searchResults() {
		var searchType;
		if (document.getElementById('searchRadio0').checked == true) {
			searchType = "artist";
		} else if (document.getElementById('searchRadio1').checked == true) { 
			searchType = "track";
		}
		
		new Ajax.Updater({success: 'sresults'}, '/friend/music/browseMusicTracksByGenre.do?fullText=true&searchTerm='+document.getElementById('searchTerm').value+'&searchGenre='+document.getElementById('searchGenre').value+'&searchRadio='+searchType, {asynchronous:true, onFailure: reportSearchError});
	}
	
	// Play the selected track in the standalone player
    function triggerPlayAction(trackId, ownerId) {
        var opt = {
          method: 'post',
          postBody: 'trackId='+trackId+'&ownerId='+ownerId,
          onFailure: function(t) {
            alert('Sorry, couldn\'t add this track to your session playlist.');
          }
       }
       new Ajax.Request('/friend/music/addSessionTrack.do', opt);
       window.open ("/friend/music/musicSessionPlayer.jsp", "hi5StdalnPlayer", "width=375,height=260,toolbar=false,resizable=false");

    }

    // Add the selected track to your profile playlist
    function triggerAddToProfileAction(trackId, div) {
        var inputDivType = document.getElementById("inputDivType");
        inputDivType.value = div;
        var trackToBeAdded = document.getElementById("trackToBeAdded");
        trackToBeAdded.value = trackId;
        displayAddTrackToPlaylist();
    }
    
    // Add the selected track to your profile playlist
    function triggerAddPlayerToProfile() {
        //if (navigator.userAgent.indexOf('MSIE') != -1) {
        //     positionLayer(document.getElementById('paddplayersuccess'));
        //}
        var opt = {
          method: 'post',
          onSuccess: function(t) {
            //MM_showHideLayers('paddplayersuccess','','show');
			$('paddplayersuccess').style.visibility = 'visible';
          },
          onFailure: function(t) {
            alert('Sorry, couldn\'t add the player to your profile.');
          }
       }
       new Ajax.Request('/friend/music/addMusicPlayerToProfile.do', opt);
    }

    // Add to standalone player but don't start playing
    function triggerAddToPlaylistAction() {

    }

    // Search within selected sub-genre
    function triggerSearch(genreId, offset) {
        new Ajax.Updater({success: 'sresults'}, '/friend/music/browseMusicTracksByGenre.do?genreId='+genreId+'&offset='+offset, {asynchronous:true, evalScripts:true, onFailure: reportSearchError});
    }
    
    // Search within selected sub-genre
    function triggerFulltextSearch(genreId, offset, searchTerm, searchRadio, searchGenre) {
        new Ajax.Updater({success: 'sresults'}, '/friend/music/browseMusicTracksByGenre.do?fullText=true&genreId='+genreId+'&offset='+offset+'&searchTerm='+searchTerm+'&searchRadio='+searchRadio+'&searchGenre='+searchGenre, {asynchronous:true, evalScripts:true, onFailure: reportSearchError});
    }

    // Page through the available browse sub-genres
    function triggerReloadBrowseGenres(genreId, offset) {
        new Ajax.Updater({success: 'search'}, '/friend/music/displayBrowsableSubGenres.do?genreId='+genreId+'&offset='+offset, {asynchronous:true, onFailure: reportBrowseError});
    }

    // On search error
    function reportSearchError(req) {
        $('sresults').innerHTML = "<b>Your search returned no results.</b>";
    }
    // On load browsable genres error
    function reportBrowseError(req) {
        $('search').innerHTML = "<b>There are no sub-genres within <c:out value='${genreName}'/>.</b>";
    }

	function MM_showHideLayers() { //v6.0
	  var i,p,v,obj,args=MM_showHideLayers.arguments;
	  for (i=0; i<(args.length-2); i+=3)
	  if ((obj=MM_findObj(args[i]))!=null) {
		v=args[i+2];
		if (obj.style) {
		  obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v;
		}
		obj.visibility=v;
	  }
	}

	  function positionLayer (popup) {
        var posx = 0;
		var posy = 0;
		var e = window.event;
          alert(e);
        if (e.pageX || e.pageY) {
            alert("PAGE");
            posx = e.pageX;
            posy = e.pageY;
		}
		else if (e.clientX || e.clientY) {
            alert("CLIENT");
			posx = e.clientX + document.body.scrollLeft;
			posy = e.clientY + document.body.scrollTop;
		}
          alert("POS="+posx+","+posy);
		popup.style.posLeft = posx-50;
		popup.style.posTop = posy-50;
	  }
	  
    function displayAddTrackToPlaylist(){
        var whichplaylistRadio = document.forms["whichplaylistForm"].elements["whichplaylistRadio"];
        if( whichplaylistRadio == null || whichplaylistRadio.length == null ) {
        	// The end-user has zero or one playlists.
        	// The code will handle this.
        	addTrackToPlaylist();
        	return( true );
        }
        whichplaylistRadio[0].checked = true;
        var whichplaylistPopup = document.getElementById("whichplaylistPopup");
        whichplaylistPopup.style.visibility='visible';
        return( true );
    }
    
    function addTrackToPlaylist(){
        var whichplaylistPopup = document.getElementById("whichplaylistPopup");
        whichplaylistPopup.style.visibility='hidden';

        var playlistId = getTargetPlaylistId();
        var trackToBeAdded = document.getElementById("trackToBeAdded");
        var trackId = trackToBeAdded.value;
        var inputDivType = document.getElementById("inputDivType");
        var div = inputDivType.value;

        var opt = {
            method: 'post',
            postBody: 'trackId='+trackId+'&playlistId='+playlistId,
            onSuccess: function(t) {
			    confirmTrackAdded(trackId, div);
            },
            onFailure: function(t) {
                alert('Sorry, couldn\'t add this track to your profile playlist.');
            }
        }
        new Ajax.Request('/friend/music/addTrackToProfile.do', opt);
	    trackToBeAdded.value = "";	
    }
    
    function confirmTrackAdded(trackId, div){
    
		var pageElements = document.getElementsByTagName('div');
		var idName;
		for (i=0; i < pageElements.length; i++) {
			idName = pageElements[i].id;
			if (idName.indexOf('playlisttrack') != -1 || idName.indexOf('top5track') != -1 || idName.indexOf('resulttrack') != -1) {
				pageElements[i].style.visibility = 'hidden';
			}
		}
		$(div+trackId).style.visibility = 'visible';
    }
    
    function cancelAddTrackToPlaylist(){
        var whichplaylistPopup = document.getElementById("whichplaylistPopup");
        whichplaylistPopup.style.visibility='hidden';
    }

    function getTargetPlaylistId(){
        var i = 0;
        var playlistId = -1;
        var whichplaylistRadio = document.forms["whichplaylistForm"].elements["whichplaylistRadio"];
        if( whichplaylistRadio == null ){
        	// No playlists created yet.
        	return( -1 );
        }
        
        if( whichplaylistRadio.length == null ) {
        	// Exactly one playlist exists. 
        	playlistId = document.getElementById("whichplaylist_0").value;
        }
        else // More than one playlist exists.
        for( i = 0; i < whichplaylistRadio.length; i++ ){
        	if( whichplaylistRadio[i].checked == true ) {
        		playlistId = whichplaylistRadio[i].value;
        		break;
        	}
        }
        return( playlistId );
    }
	  
