﻿// JScript File
var selectedTrack = 0;
var selectedPlaylist=0;




function initPage()
{
    var recentSearchState = readCookie ("recentSearchState");
    if(recentSearchState==null)
    {
        createCookie("recentSearchState","visible",99999); 
        $("#recent_searches_container").show();       
    }
    else
    {
        if(recentSearchState=="visible")
        {
            $("#recent_searches_container").show();  
        }
        else
        {
            $("#recent_searches_container").hide();  
        }
       
    }

    //getNextForScroller(0);
  //change any mp3 links to a nice player
  //$.fn.media.mapFormat('mp3', 'winmedia');
  //$(function() {$('a.media').media( {width: 100, height: 10, autoplay: false } );});
 
  if($.browser.msie)
  {
    $("hr").addClass("HRIE");
  }
  else
  {
     $("hr").addClass("HROther");
  }
  try
  {
    $(".quickBox").corner("6px");     
    $("#divHeader").corner("6px"); 
    $("#divMyPlaylists").corner("4px");
    $("#divPlayer").corner("4px");
    $("#divRecentSearches").corner("4px");   
    $("#searchbox").corner("6px");
    $("#rounded_textbox").corner("4px");
    $("#divSearchBar").corner("6px"); 
    $("#divTags").corner("6px"); 
    $("#divAddToPlaylist").corner("6px"); 
    $("#divRate").corner("6px");
    $('.form_section').corner("6px");
    $('.tab_content').corner("6px"); 
    $('#rssToolbar').corner("6px");
    $('#menuBar').corner("6px");       
    $('.contentfull').corner("6px");      
    $('.playlist_group').corner("6px");  
    $('#divHelp').corner("6px"); 
    $('#divSharePlaylist').corner("6px"); 
    $('.search_result').corner("6px"); 
    $('.searchResultToolbar').corner("4px"); 
    $('#divArtistProfile').corner("6px");  
    $('#commisionsedWorkContainer').corner("4px"); 
    $('.custom_search_authenticated').corner("4px"); 
    $('.custom_search_unauthenticated').corner("4px"); 
    $('.asRegion').corner("6px"); 
    $('#tracks').corner("6px"); 

    
    
         
            
    $("#divTags").hide(); 
  }
  catch (e)
  {
  }
  try
  {
    $('#tabContainer').tabs(0);
  }
  catch(e)
  {
  
  }
  try
  {
    var o = $(".search_result");
    if(o.length>0)
    {
        $("#advancedSearchCriteria").hide();
    }
    else
    {
        $("#advancedSearchCriteria").show();
    }
  }
  catch(e)
  {
  
  }
   $('.play_button').show();   

}

function toggleRecentSearch()
{
    var ss = document.getElementById("recent_searches_container").style.display;
    if(ss=="")
    {
        document.getElementById("recent_searches_container").style.display = "none";
        eraseCookie("recentSearchState");
        createCookie("recentSearchState","hidden",99999);
    }
    else
    {
        document.getElementById("recent_searches_container").style.display = "";
        eraseCookie("recentSearchState");
        createCookie("recentSearchState","visible",99999);    
    }   
}

function showPopup(height,width,url)
{
    window.open(url,null,"height=" + height + ",width=" + width + ",status=no,toolbar=no,menubar=no,location=no,location=0");
}

function getNextForScroller()
{
    
    var oResult = Ajax.GetNextRecentlyAdded();
    //$('#recent_scroller').hide('medium');
    document.getElementById("recent_scroller").innerHTML  = oResult.value;
    //$('#recent_scroller').show('medium');
    setInterval  ( "getNextForScroller();", 4000 );
}

function browse()
{
    window.open ("browse.aspx");
}

function simpleSearch()
{
    var query = document.getElementById("simple_search").value;
    if(query.length<2)
    {
        alert("Search must be more than 2 characters.");
        document.getElementById("simple_search").focus ();
        return false
    }
    else
    {
        var sURL = "search.aspx?q=" + query;
        document.location = sURL; 
        return false;  
    }
}

function previewTrackInline(id)
{
    //make ajax call to log the preview request
    var oResult = Ajax.PreviewTrack(id);
    var sURL = "preview_track.aspx?file=" + oResult.value;
    var parts =  oResult.value.split("|");
    var url = parts[0] + "&t=i";
    var title = parts[1]
    //jWindow = window.open(sURL, 'iwin', 'width=250, height=50,resizable=no, scrollbars=no'); 
    $("#divPlayer").show();
    $("#playerGap").show();
    $("#previewTrackName").html(title);

    //document.getElementById ("framePlayer").src = url; 
    var genScript = "GenXSPF.aspx?t=t_" + id;
    var so = new SWFObject('mediaplayer.swf','SSPlayer','190','20','8');
    so.addParam('allowscriptaccess','always');
    so.addParam('allowfullscreen','false');
    so.addVariable('width','190');
    so.addVariable('height','20');
    so.addVariable('file',genScript);
    so.addVariable('autostart','true');
    so.addVariable('javascriptid','SSPlayer');
    so.addVariable('enablejs','true');
    so.write('trackPlayer');
   
}

function previewTrackOut(id)
{
    
    //make ajax call to log the preview request
    var oResult = Ajax.PreviewTrack(id);
    var parts =  oResult.value.split("|");
    var sURL = parts[0] ;
    //var sURL = "preview_track.aspx?id=" + id + " &file=" + oResult.value ;  
    jWindow = window.open(sURL, 'iwin', 'width=225, height=140,resizable=no, scrollbars=no,status=no,location=no'); 
}

function previewTrack(id)
{   
    //make ajax call to log the preview request
    var oResult = Ajax.PreviewTrack(id,sessionUserID);
    var parts =  oResult.value.split("|");
    var sURL = parts[0] ;
    var urlParts = sURL.split("/");
    var filePart = urlParts[urlParts.length -1];
    filePart = filePart.replace("preview_track.aspx?file=","");
    //make call to previewTrack server top see if filePart exists
    //var sURL = "preview_track.aspx?id=" + id + " &file=" + oResult.value ;
    var checkURL = "http://preview.sonicspaza.com/checkFileExists.aspx?file=" + filePart;
    var previewURL = "http://preview.sonicspaza.com/?t=t_" + id;
    var oChecker = Ajax.CheckIfPreviewFileExists(checkURL);
    
    if(oChecker.value=="1")
    {
       window.open(previewURL, 'iwin', 'width=270, height=190,resizable=no, scrollbars=no, status=no');
    }
    else
    {   
        Ajax.SendMissingFileEmail(id, filePart , SendMissingFileEmailCallack);
        alert("Preview file was not found.\r\nWe have been notified and will rectify this problem as soon as possible.");        
    }
      
}

function SendMissingFileEmailCallack(res)
{
 
}

function popupWindow(url)
{
    jWindow = window.open(url, 'iwin', 'width=650, height=500,resizable=no, scrollbars=no');
}


function savePlaylist()
{
     var o = document.getElementById("new_playlist_name");
     if(o.value.length==0)    
     {
        alert("Please provide a playlist name.");
     }
     else
     {
         var oElement = document.getElementById("user_playlist_container");
         var oResult = Ajax.SystemUser_CreatePlaylist(o.value , "");
         if(oResult.error ==null)
         { 
            alert(oResult.value);
             oElement.innerHTML = oResult.value;           
             o.value = "";
             $('#playlist_create').hide('medium');
             o.value = "";
             $(".playlistDraggable").draggable({helper: 'clone', containment: 'document', scroll : 'true'});
         }
         else{
            oElement.innerHTML = oResult.error.Message;
         }
     }
    loadUserPlaylistsIntoAddSelect();
}

function removeTrackFromPlaylist(id,playlistID)
{
    var bRemoveOK = confirm("Are you sure you wish to remove this song?");
    if(bRemoveOK)
    {
        //ajax call to remove entry
        var o = Ajax.RemoveTrackFromPlaylist(id);
        if(o.error==null)
        {
            $("#playlisttrack_" + id).remove();
            var i = $("#playlistTracks_" + playlistID);
            if(i.children().children().length==0)
            {
                i.children().append("<TR><TD style='WIDTH: 10px'></TD><TD>No tracks added</TD><TD style='WIDTH: 16px' align='right'></TD></TR>");
            }
            
        }
        else
        {
            alert("A problem occured while removing the track.");
        }
    }    
}

function removePlaylist(id)
{

    //ajax call to remove entry
    var bRemoveOK = confirm("Are you sure you wish to remove this playlist?");
    if(bRemoveOK)
    {
        var o = Ajax.RemovePlaylist(id);
        if(o.error==null)
        {
            var oResult = Ajax.GetPlaylistHTML();
            var oElement = document.getElementById("user_playlist_container");
            oElement.innerHTML = oResult.value;
        }
        else
        {
            alert("A problem occured while removing the playlist.");
        }
    }
}

function removePlaylistTrackInMaintain(playlistID,playlistTrackID)
{
    var bRemoveOK = confirm("Are you sure you wish to remove this song?");
    if(bRemoveOK)
    {       
        //ajax call to remove entry
        Ajax.RemoveTrackFromPlaylist(playlistTrackID);
        var sID = playlistID + "_" + playlistTrackID;
        $("#"+sID).remove();
        //re sync the track numbers
        var sTableID = "playlist_details_" + playlistID;
        var oRows = document.getElementById (sTableID).childNodes[0].childNodes;
        for (var i=0; i<oRows.length-1; i++) 
        {
            oRows[i].childNodes[0].innerHTML = i+1;
        }         
        var oResult = Ajax.GetPlaylistHTML();
        var oElement = document.getElementById("user_playlist_container");
        oElement.innerHTML = oResult.value;         
    }

}

function removePlaylistInMaintain(playlistID)
{
    var bRemoveOK = confirm("Are you sure you wish to remove this playlist?");
    if(bRemoveOK)
    {        
        //ajax call to remove entry
        Ajax.RemovePlaylist(playlistID);
        var sID = playlistID + "_" + playlistID;
        $("#maintainPlaylist_"+playlistID).remove(); 
        var oResult = Ajax.GetPlaylistHTML();
        var oElement = document.getElementById("user_playlist_container");
        oElement.innerHTML = oResult.value;              
    }
}







function loadUseageTypeFormOut(id)
{
    if(id>0)
    {
        //ajax call to get the form based on the request
        oResult = Ajax.GetUseageTypeForm(id);
        document.getElementById("request_form_container").innerHTML = oResult.value;
        $('#basic_details').show(100);
        $('#request_form_container').show(100);
    }
    else
    {
        document.getElementById("request_form_container").innerHTML = "";
        $('#basic_details').hide(100);
        $('#request_form_container').hide(100);
    }
}

function saveLicenseRequest()
{
    var o = document.getElementById("request_form_container");
    alert(o.innerHTML);
}


function checkTrackTerritoryExclusions()
{
    var masterRecordingID = document.getElementById("masterRecordingID").value;
    var territoryID = document.getElementById("territory").value;
    var usageID = document.getElementById("useageType").value;
    var oResult = Ajax.CheckLicenseRequestExclusions(masterRecordingID,usageID,territoryID);
    if(oResult.value>0)
    {
        alert("This track is not available in this territory for this usage type.");
    }
    else
    {
        getPrice();
    }
}


function showQuickPrice()
{
   $('#quickPrice').toggle("fast"); 
   window.scroll(0,1000)
   
}
function showLogin()
{
    $('#header_menu').hide();
    $('#header_login').show();
    document.getElementById("login_email").focus();
}

function hideLogin()
{
    $('#header_login').hide();
    $('#header_menu').show();
}

function showPlaylists()
{
    $('#headerModule').show("slow");
    $('#divPlaylists').show("slow");
}

function removeTag(systemUserTagID)
{
    var bRemoveOK = confirm("Are you sure you wish to remove this tag?");
    if(bRemoveOK)
    {
        var result = Ajax.RemoveTag(systemUserTagID);
        document.getElementById ("tags_container").innerHTML = result.value;
    }
}

function addTag(userID )
{
    var tag = document.getElementById ("tagName").value;
    if(tag.length<3)
    {
        alert("The tag should be at least 3 characters.");
        return false;
    }
    var result = Ajax.AddTag(userID,selectedTrack,tag);
    document.getElementById ("tags_container").innerHTML = result.value;
    document.getElementById ("tagName").value = "";
}

function getUserTagEntries(userID, masterRecordingID,title)
{
    window.scroll(0,150); // horizontal and vertical scroll targets
    selectedTrack = masterRecordingID;
    var result = Ajax.GetUserPlaylistEntries(userID,selectedTrack);       
    document.getElementById ("tags_container").innerHTML = result.value;
    document.getElementById ("tagName").value = "";
    
    //$('#user_playlist_container').hide();
    $('#divSharePlaylist').hide();
    $('#recent_searches_container').hide();
    $('#divAddToPlaylist').hide();
    $('#divRate').hide();
    $('#divTags').show('medium');    
    $('#tagTrack_trackname').empty();
    $('#tagTrack_trackname').prepend("Song: <b>" + title + "</b>");
}

function loadUserPlaylistsIntoAddSelect()
{
    var o = Ajax.GetUserPlaylists();
    if(o.error!=null)
    {
            $('#user_playlists_container').empty();    
            $('#user_playlists_container').prepend(o.error.Message);        
    }
    else
    {
        if(o.value!="")
        {
            $('#user_playlists_container').empty();    
            $('#user_playlists_container').prepend(o.value);    
            var iOptionCount = $('#addToPlaylistID').children().length; 
            if(iOptionCount>1)
            {   
                $('#addToPlaylistID').val("-1");
                $('#addToPlaylistRating').val("-1");        
           
            }                                          
        } 
    }          
}

function handleShowAddTrackToPlaylist(masterRecordingID,title, type)
{
    window.scroll(0,150); // horizontal and vertical scroll targets
    $('#playlist_create').hide();
    selectedTrack = masterRecordingID;
    document.getElementById("addToPlayListComment").value = "";   
    //load the users playlists into the select
    var o = Ajax.GetUserPlaylists();
    if(o.value=="")
    {
        
        $('#user_playlists_container').prepend("<span>You do not have any playlists defined</span");
        $('#addtoplaylist_form').hide();
        $('#addtoplaylist_error').show(); 
        $('#divAddToPlaylist').show('medium');  
     
    }
    else
    {
        $('#addtoplaylist_error').hide(); 
        $('#user_playlists_container').empty();    
        $('#user_playlists_container').prepend(o.value);    
        var iOptionCount = $('#addToPlaylistID').children().length; 
        if(iOptionCount>1)
        {   
            $('#addToPlaylistID').val("-1");
            $('#addToPlaylistRating').val("-1");        
       
        }              
        $('#addtoplaylist_trackname').empty();
        if(type=="song")
        {
            $('#addtoplaylist_trackname').prepend("Song: <b>" + title + "</b>");
        }
        if(type=="album")
        {
            $('#addtoplaylist_trackname').prepend("Album: <b>" + title + "</b>");
        }
       if(type=="artist")
        {
            $('#addtoplaylist_trackname').prepend("Artist: <b>" + title + "</b>");
        }        
        
        $('#divSharePlaylist').hide();
        //$('#user_playlist_container').hide();
        //$('#recent_searches_container').hide();
        $('#addtoplaylist_form').show();
        $('#divTags').hide();
        $('#divRate').hide();    
        $('#divAddToPlaylist').show('medium');             
        document.getElementById("addToPlaylistID").focus();                               
    }            
}

function handleHideAddTrackToPlaylist()
{
    $('#divTags').hide();
    $('#divRate').hide();
    $('#divAddToPlaylist').hide();
    //$('#user_playlist_container').show('medium');
    //$('#recent_searches_container').show('medium');    
}

function addToPlaylist()
{
    var sType = "song";
    var addType = document.getElementById('addtoplaylist_trackname').innerHTML;
    if(addType.indexOf("Album",0)>-1)
    {
        sType = "album";
    }
    if(addType.indexOf("Artist",0)>-1)
    {
        sType = "artist";
    }
    if(addType.indexOf("Song",0)>-1)
    {
        sType = "song";
    }    
    
    var playlistID = document.getElementById("addToPlaylistID").value;
    var comments = document.getElementById("addToPlayListComment").value;
    //var rating = document.getElementById("addToPlaylistRating").value; 
    var rating = 5;
    if(playlistID<1)
    {
        alert("Please select a playlist.");
        return false;
    }
    try
    {    
        var oResult;
        if(sType=="song")
        {
            oResult = Ajax.SystemUser_AddTrackToPlaylist (playlistID, selectedTrack,comments,rating);
        }
        if(sType=="album")
        {
            oResult = Ajax.SystemUser_AddAlbumToPlaylist (playlistID, selectedTrack,comments,rating);
        }
        if(sType=="artist")
        {
            oResult = Ajax.SystemUser_AddArtistTracksToPlaylist (playlistID, selectedTrack,comments,rating);
        }  
        if(oResult.error!=null)
        {
            alert(oResult.error.Message);
        }     
        else
        {
            $('#user_playlist_container').empty(); 
            $('#user_playlist_container').prepend(oResult.value);
        }        

        handleHideAddTrackToPlaylist();
        var s = "#playlist_" + playlistID;
        $(s).show('medium'); 
        window.scroll(0,0); // horizontal and vertical scroll targets
    }
    catch(e)
    {
        alert(e);
    }
}

function handleShowRateTrack(masterRecordingID, title)
{
    window.scroll(0,150); // horizontal and vertical scroll targets
    selectedTrack = masterRecordingID;
    //$('#user_playlist_container').hide();
    $('#recent_searches_container').hide();
    $('#divTags').hide();
    $('#divAddToPlaylist').hide();
    $('#divSharePlaylist').hide();
    $('#addRatingComment').empty();
    $('#addRatingRating').val("-1");
    $('#divRate').show('medium');
    $('#rateTrack_trackname').empty();
    $('#rateTrack_trackname').prepend("Song: <b>" + title + "</b>");
}

function handleHideRateTrack( masterRecordingID)
{
    $('#divTags').hide();
    $('#divAddToPlaylist').hide();
    $('#divRate').hide();
    //$('#user_playlist_container').show('medium');
    $('#recent_searches_container').show('medium');
}

function addRating()
{
    var comments = document.getElementById("addRatingComment").value;
    var rating = document.getElementById("addRatingRating").value;
    if(rating<1)
    {
        alert("Please select a rating.");
        return false;
    }
    var oResult = Ajax.RateTrack(selectedTrack,rating,comments);
    if(oResult.error!=null)
    {
        alert(oResult.error.Message);
    }
    
    handleHideRateTrack();
}


function showHelp(loggedIn,helpID)
{   
    if(loggedIn)
    {
        $('#help_container').empty();
        var o = Ajax.GetHelpByID(helpID);
        //alert(o.value);
        $('#help_container').prepend(o.value);
        $('#recent_searches_container').hide();
        $('#divHelp').show('medium');
    }
    else
    {
        var sURL = "context_help.aspx?id=" + helpID;
        window.open(sURL, '', 'width=450, height=250,resizable=no, scrollbars=no');         
    }

}

function closeHelp()
{

    $('#recent_searches_container').show('medium');
    $('#divHelp').hide();
}

function handleShowSharePlaylist(playlistID)
{
    $('#divRate').hide();
    $('#user_playlist_container').hide();
    $('#recent_searches_container').hide();
    $('#divTags').hide();
    $('#divAddToPlaylist').hide();
    $('#divSharePlaylist').show('medium');
    $('#sharingPlaylistID').empty();
    $('#sharingSubject').empty();
    $('#sharingMessage').empty(); 
    $('#sharingEmailAddresses').empty(); 
    $('#sharingPlaylistID').val(playlistID);
}

function handleHidePlaylistShare()
{
    $('#divTags').hide();
    $('#divAddToPlaylist').hide();
    $('#divRate').hide();
    $('#divSharePlaylist').hide();
    $('#user_playlist_container').show('medium');
    $('#recent_searches_container').show('medium');
}

function sharePlaylist()
{
    var playlistID = $('#sharingPlaylistID').val();
    var sharingMessage = $('#sharingMessage').val();
    var sharingEmailAddresses = $('#sharingEmailAddresses').val();
    if(sharingMessage.length==0)
    {
        alert("Please provide a message that will be sent along with the email.");
        $('#sharingMessage').focus();
        return false;
    }
    if(sharingEmailAddresses.length==0)
    {
        alert("Please provide at least one email address.");
        return false;
    }    
    var bEmailAddressOK = true;
    var aRecipients = sharingEmailAddresses.split(",");
    for(i=0;i<=aRecipients.length -1;i++)
    {
        var reRegExp = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
        if(!reRegExp.test(aRecipients[i]))
        {
            bEmailAddressOK  = false;
        }
    }
    if(bEmailAddressOK==false)
    {
        alert("At least one of the email addresses you provide is invalid..");
        $('#sharingEmailAddresses').focus();
        return false;
        
    }
    try
    {    
        Ajax.SharePlaylist (playlistID,  sharingMessage,sharingEmailAddresses);
        alert("An email has been sent to the recipients.");
        handleHidePlaylistShare();
    }
    catch(e)
    {
        alert(e);
    }    
}

function showPlaylistPlayer(id)
{
    var previewURL = "http://preview.sonicspaza.com/?t=p_" + id;
    window.open(previewURL, 'iwin', 'width=270, height=190,resizable=no, scrollbars=no, status=no');
}

function dl(trackID, userID)
{    
    var o = Ajax.RequestDownload(trackID,userID);
    alert(o.value);
    if(o.value.length>0)
    {
        document.location = "download.aspx?uid=" + o.value;
    }
    else
    {
        alert("The download request failed");
    }
}


function showArtistProfile(artistID, artistAliasID)
{
    $("#divArtistProfileDetails").val("loading");
    $('#user_playlist_container').hide('fast');  
    $('#recent_searches_container').hide('fast');  
    //$('#divArtistProfile').hide('fast');
    
    //load the artists details via ajax
    //document.getElementById("divArtistProfileDetails").innerHTML = artistID + " - " + artistAliasID;
    var o = Ajax.GetArtistProfileHTML(artistID,artistAliasID);
        
    var sHTML = o.value;
    document.getElementById("divArtistProfileDetails").innerHTML = sHTML;
    $('#divArtistProfile').show('fast');
    
}

function handleNotifyMe(userID, entityType, entityID)
{
    var o = Ajax.NotifyMe(userID, entityType, entityID);
    if(o.error!==null)
    {
        alert("There was a problem adding this notification.");
    }
    else
    {
        alert("You will be notified when new tracks by this artist are added.");
    }
}

function removeNotification(SystemUserNotificationID)
{
    var o = Ajax.RemoveNotification(SystemUserNotificationID);
    if(o.error!==null)
    {
        alert("There was a problem removing this notification.");
    }
    else
    {
        var  element = "#notification_" + SystemUserNotificationID;
        $(element).remove();
        alert("The notification was removed sucessfully..");
    }
}

function handleShowAddArtistTracksPlaylist(userID, artistPK,title, type)
{
    window.scroll(0,150); // horizontal and vertical scroll targets
    $('#playlist_create').hide();
    document.getElementById("addToPlayListComment").value = "";   
    //load the users playlists into the select
    var o = Ajax.GetUserPlaylists(userID);
    if(o.value=="")
    {
        
        $('#user_playlists_container').prepend("<span>You do not have any playlists defined</span");
        $('#addtoplaylist_form').hide();
        $('#addtoplaylist_error').show(); 
        $('#divAddToPlaylist').show('medium');  
     
    }
    else
    {
        $('#addtoplaylist_error').hide(); 
        $('#user_playlists_container').empty();    
        $('#user_playlists_container').prepend(o.value);    
        var iOptionCount = $('#addToPlaylistID').children().length; 
        if(iOptionCount>1)
        {   
            $('#addToPlaylistID').val("-1");
            $('#addToPlaylistRating').val("-1");        
       
        }              
        $('#addtoplaylist_trackname').empty();
        if(type=="song")
        {
            $('#addtoplaylist_trackname').prepend("Song: <b>" + title + "</b>");
        }
        else
        {
            $('#addtoplaylist_trackname').prepend("Album: <b>" + title + "</b>");
        }
        $('#divSharePlaylist').hide();
        //$('#user_playlist_container').hide();
        $('#recent_searches_container').hide();
        $('#addtoplaylist_form').show();
        $('#divTags').hide();
        $('#divRate').hide();    
        $('#divAddToPlaylist').show('medium');             
        document.getElementById("addToPlaylistID").focus();                               
    }    
}

function closeArtistProfile()
{
    $('#user_playlist_container').show('fast');  
    $('#recent_searches_container').show('fast');  
    $('#divArtistProfile').hide('fast');
}

function writeFlash(id, previewPath, inlinePlayer, MRID) 
{
    var html = "";
    html += "<object title=\"Preview song\" class=\"play_button\" style=\"display:none;\" type=\"application/x-shockwave-flash\" b_bgcolor=\"#CBF47A\" width=\"17\" height=\"17\" data=\"" + inlinePlayer + ".swf?playlist_url=" + previewPath + "GenXSPF.aspx?t=t_" + MRID + "\">";
    html += "<param name=\"movie\" value=\"" + inlinePlayer + ".swf?playlist_url=" + previewPath + "GenXSPF.aspx?t=t_" + MRID + "\" />";
    html += "<param name=\"bgcolor\" value=\"white\"/>";
    html += "</object>";
    document.getElementById(id).innerHTML = html;
}

function sendComissionedWorkRequest(UserID, ArtistID)
{        
    var sJobDate = document.getElementById ("comissionedWorkJobDate").value;
    var sJobDescription = document.getElementById ("comissionedWorkJobDescription").value;
    if(sJobDate=="")
    {
        alert("Please provide a job date.");
        return false;
    }
    if(sJobDescription=="")
    {
        alert("Please provide a job description.");
        return false;
    }    
    $("#comissionedWorkBusy").show();
    $("#comissionedWorkCancel").hide();
    $("#comissionedWorkSend").hide();
    Ajax.SendCommissionesWorkEnquiry(UserID, ArtistID,sJobDate,sJobDescription,sendComissionedWorkRequestCallback);
}

function sendComissionedWorkRequestCallback()
{
    $("#comissionedWorkCancel").show();
    $("#comissionedWorkSend").show();
    $("#comissionedWorkBusy").hide();
    $('#commisionsedWorkContainer').hide(); 
    alert("Thank you for your enquiry. We will be in touch shortly.");   
}

function loadAlbumContent(LoggedIn, Artist_PK, ArtistAlias_PK)
{
    var html = $("#tabAlbums").html();
    html = html.trim().toLowerCase();
    if(html.indexOf("<img",0)==0)
    {
        Ajax.LoadArtistDetailAlbumData(LoggedIn,Artist_PK,ArtistAlias_PK,loadAlbumContentCallback);
    }  
}

function loadAlbumContentCallback(res)
{
    document.getElementById("tabAlbums").innerHTML = res.value;
    $('#tabAlbums').corner("6px");  
    $(".search_result").corner("4px");  
}

function loadFeaturingContent(LoggedIn, Artist_PK, ArtistAlias_PK)
{
    var html = $("#tabTracksFeaturingArtist").html();
    html = html.trim().toLowerCase();
    if(html.indexOf("<img",0)==0)
    {    
        Ajax.LoadArtistTracksFeaturingData(LoggedIn, Artist_PK,ArtistAlias_PK,loadFeaturingContentCallback);
    }
} 

function loadFeaturingContentCallback(res)
{
    document.getElementById("tabTracksFeaturingArtist").innerHTML = res.value;
    $('#tabTracksFeaturingArtist').corner("6px");
    $(".search_result"). corner("4px");          
} 

function loadTracksByArtist(LoggedIn, Artist_PK, ArtistAlias_PK)
{
    var html = $("#tabArtistTracks").html();
    html = html.trim().toLowerCase();
    if(html.indexOf("<img",0)==0)
    {     
        Ajax.LoadArtistTracksData(LoggedIn, Artist_PK,ArtistAlias_PK,loadTracksByArtistCallback);
    }            
} 

function loadTracksByArtistCallback(res)
{
   
    document.getElementById("tabArtistTracks").innerHTML = res.value;
    $('#tabArtistTracks').corner("6px");    
    $(".search_result"). corner("4px");    
} 

function loadOtherAlbumsByArtist(LoggedIn, Artist_PK, ArtistAlias_PK)
{    
    var html = $("#tabOtherAlbums").html();
    html = html.trim().toLowerCase();
    if(html.indexOf("<img",0)==0)
    {     
        Ajax.AlbumOtherAlbumsByArtist(LoggedIn, Artist_PK,ArtistAlias_PK,loadOtherAlbumsByArtistCallback);
    }            
}

function loadTracksByArtistCallback(res)
{
    document.getElementById("tabArtistTracks").innerHTML = res.value;
    $('#tabArtistTracks').corner("6px");    
    $(".search_result"). corner("4px");    
} 

function loadOtherAlbumsByArtist(LoggedIn, Artist_PK, ArtistAlias_PK)
{    
    var html = $("#tabOtherAlbums").html();
    html = html.trim().toLowerCase();
    if(html.indexOf("<img",0)==0)
    {     
        Ajax.AlbumOtherAlbumsByArtist(LoggedIn, Artist_PK,ArtistAlias_PK,loadOtherAlbumsByArtistCallback);
    }            
}

function loadOtherAlbumsByArtistCallback(res)
{
    document.getElementById("tabOtherAlbums").innerHTML = res.value;
    $('#tabOtherAlbums').corner("6px");    
    $(".search_result"). corner("4px");    
} 

function loadAlbumSimilarAlbums(LoggedIn, Album_PK)
{    
    var html = $("#tabSimilarAlbums").html();
    html = html.trim().toLowerCase();
    if(html.indexOf("<img",0)==0)
    {     
        Ajax.AlbumSimilarAlbums(LoggedIn, Album_PK,loadAlbumSimilarAlbumsCallback);
    }            
}

function loadAlbumSimilarAlbumsCallback(res)
{
    document.getElementById("tabSimilarAlbums").innerHTML = res.value;
    $('#tabSimilarAlbums').corner("6px");    
    $(".search_result"). corner("4px");    
}  

function loadOtherTracksByArtist(LoggedIn, Artist_PK, ArtistAlias_PK)
{    
    var html = $("#tabByArtist").html();
    html = html.trim().toLowerCase();
    if(html.indexOf("<img",0)==0)
    {     
        Ajax.OtherTracksByArtist(LoggedIn, Artist_PK, ArtistAlias_PK, loadOtherTracksByArtistCallback);
    }            
}

function loadOtherTracksByArtistCallback(res)
{
    document.getElementById("tabByArtist").innerHTML = res.value;
    $('#tabByArtist').corner("6px");    
    $(".search_result"). corner("4px");    
} 

function loadOtherTracksFeaturingArtist(LoggedIn, Artist_PK, ArtistAlias_PK)
{       
    var html = $("#tabByFeaturingArtist").html();
    html = html.trim().toLowerCase();
    if(html.indexOf("<img",0)==0)
    {     
        Ajax.OtherTracksFeaturingArtist(LoggedIn, Artist_PK, ArtistAlias_PK, loadOtherTracksFeaturingArtistCallback);
    }            
}

function loadOtherTracksFeaturingArtistCallback(res)
{
    document.getElementById("tabByFeaturingArtist").innerHTML = res.value;
    $('#tabByFeaturingArtist').corner("6px");    
    $(".search_result").corner("4px");    
}

function loadSimilarSoundingTracks(LoggedIn, MasterRecording_PK)
{   
    var html = $("#tabSimilarArtists").html();
    html = html.trim().toLowerCase();
    if(html.indexOf("<img",0)==0)
    {     
        Ajax.SimilarSoundingTracks(LoggedIn, MasterRecording_PK, loadSimilarSoundingTracksCallback);
    }            
}

function loadSimilarSoundingTracksCallback(res)
{
    document.getElementById("tabSimilarArtists").innerHTML = res.value;
    $('#tabSimilarArtists').corner("6px");    
    $(".search_result").corner("4px");    
} 

function loadOtherTracksWrtittenByArtist(LoggedIn, Artist_PK, ArtistAlias_PK)
{    
    
    var html = $("#tabByWrittenByArtist").html();
    html = html.trim().toLowerCase();
    if(html.indexOf("<img",0)==0)
    {     
        Ajax.OtherTracksComposedByArtist(LoggedIn, Artist_PK, ArtistAlias_PK, loadOtherTracksWrittenByArtistCallback);
    }            
}

function loadOtherTracksWrittenByArtistCallback(res)
{
    document.getElementById("tabByWrittenByArtist").innerHTML = res.value;
    $('#tabByWrittenByArtist').corner("6px");    
    $(".search_result").corner("4px"); 
}
 
String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}

function guid() 
{
   return (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4());
}

function S4() 
{
   return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
}

function licensePlaylist(ids)
{
    //get count of tracks in this playlist
    //if more than 1 then ok else alert
    if(ids.length>0)
    {
        document.location = "new_license_request.aspx?id=" + ids;
    }
    else
    {
        alert("This playlist does not contain any tracks. Please add at least one track");
    }
} 

function advancedSearchOptionsCaptionToggle(o)
{
    if(o.innerText=="more options")
    {
        o.innerText = "less options"
    }
    else
    {
        o.innerText = "more options"
    }
}

function movePlaylistTrack(id,direction,playlistID)
{
    var oResult = Ajax.MovePlaylistTrack(id,direction,playlistID);
    
    $("#playlist_group_"+playlistID).empty();
    $("#playlist_group_"+playlistID).append(oResult.value);
    $("#playlist_group_"+playlistID).corner("6px");
         
    var oResult = Ajax.GetPlaylistHTML(sessionUserID);
    var oElement = document.getElementById("user_playlist_container");
    oElement.innerHTML = oResult.value;     
}


function getHTTPRootPath()
{
    var sPath = new String(document.location) ;
    var sFullPath = "";
    var sParts = sPath.split("/");
    sFullPath = "http://";
    for(i=2;i<=sParts.length-2;i++)
    {
      sFullPath = sFullPath + sParts[i] + "/";
    }
    return sFullPath; 
}

function acceptQuote(LicenseRequestID,LicenseRequestActivityID)
{
    Ajax.AcceptLicenseRequestQuote(LicenseRequestID,LicenseRequestActivityID,acceptQuoteCallback);        
}

function acceptQuoteCallback(res)
{
    $("#activity_" + res.value).html("<td>Quote accepted</td>");
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function copyPlaylist(id)
{
    var sName = $("#copyPlaylistName").val();
    if(sName=="")
    {
        alert("Please provide a name for this playlist");
    }
    else
    {
        var oElement = document.getElementById("user_playlist_container");
        var oResult = Ajax.CopySharedPlaylist(id,sessionUserID,sName);
        oElement.innerHTML = oResult.value;
        $("#copyPlaylistName").val("")  ;
        $('#copyPlaylist').hide();
    }
    
}
  function onDrop(ev, ui)
  {
	        var trackToAdd = $(ui.draggable).attr("id").replace("trackid_","");
	        var playlistID = $(this).attr("id").replace("playlistid_","");
	        //ajax call to add track to playlist

            oResult = Ajax.SystemUser_AddTrackToPlaylist (playlistID, trackToAdd,"",5);
			
            if(oResult.error==null)
			{
	            $('#user_playlist_container').empty(); 
	            $('#user_playlist_container').append(oResult.value);
	            var s = "#playlist_" + playlistID;
	            $(s).show('medium'); 
	            window.scroll(0,0); // horizontal and vertical scroll targets
	            $(".playlistDroppable").droppable({
		            accept: ".playlistDraggable",
		            activeClass: 'droppable-active',
		            hoverClass: 'droppable-hover',
		            drop: onDrop
	            }); 
			}
  }
