﻿// JScript File

var postBackElement;
var batchRestrictionError;
var delim = "|";

function DisableValidator()
{
    var cuvSearchCriteria = document.getElementById("ctl00_licensingContent_cuvSearchCriteria");    
    ValidatorEnable(cuvSearchCriteria, false);
}
function searchPageInitializeRequest(sender, args) 
    {
       var prm = Sys.WebForms.PageRequestManager.getInstance();
       
       if (prm.get_isInAsyncPostBack())
       {
          args.set_cancel(true);
       }
       
      postBackElement = args.get_postBackElement();
       if (postBackElement.id == 'ctl00_ctl00_NestedMasterContent_PageContent_Tabs_tpSearchTab_btnDateSubmit')
       {
         $get('ctl00_ctl00_NestedMasterContent_PageContent_Tabs_tpSearchTab_upDates').style.display = "block";
       }
       
       if (postBackElement.id == 'ctl00_ctl00_NestedMasterContent_PageContent_Tabs_tpSearchTab_ddlGlobalArtistId')
       {
         $get('ctl00_ctl00_NestedMasterContent_PageContent_Tabs_tpSearchTab_upReleaseId').style.display = "block";
       }
       
       if (postBackElement.id == 'ctl00_ctl00_NestedMasterContent_PageContent_btnSearch')
       {
         $get('ctl00_ctl00_NestedMasterContent_PageContent_Tabs_tpSearchTab_upSearch').style.display = "block";
         $get('ctl00_ctl00_NestedMasterContent_PageContent_Tabs_tpExclusions_upExcluded').style.display = "block";
       }
    }
    
function searchPageEndRequest (sender, args) 

    {
       if (postBackElement.id == 'ctl00_ctl00_NestedMasterContent_PageContent_Tabs_tpSearchTab_btnDateSubmit')
       {
         $get('ctl00_ctl00_NestedMasterContent_PageContent_Tabs_tpSearchTab_upDates').style.display = "none";    
       }
       
       if (postBackElement.id == 'ctl00_ctl00_NestedMasterContent_PageContent_Tabs_tpSearchTab_ddlGlobalArtistId')
       {
         $get('ctl00_ctl00_NestedMasterContent_PageContent_Tabs_tpSearchTab_upReleaseId').style.display = "none";
       }
       
       if (postBackElement.id == 'ctl00_ctl00_NestedMasterContent_PageContent_btnSearch')
       {
         $get('ctl00_ctl00_NestedMasterContent_PageContent_Tabs_tpSearchTab_upSearch').style.display = "none";
         $get('ctl00_ctl00_NestedMasterContent_PageContent_Tabs_tpExclusions_upExcluded').style.display = "none";
       }
    }
    
function searchPageLoaded(sender, args) 
    {
        GetUserQueue();     
    }    

function AbortPostBack() 
    {
      var prm = Sys.WebForms.PageRequestManager.getInstance();
      if (prm.get_isInAsyncPostBack()) {
           prm.abortPostBack();
      }        
    }
    
function HideErrorLabel()
{
    var lblError = document.getElementById("ctl00_licensingContent_lblError");
    lblError.style.display = "none";
}

function ShowHopePublishingMessage()
{
    var lblError = document.getElementById("ctl00_licensingContent_lblError");
    lblError.innerHTML = 'Print rights for this song must be licensed by <a href="http://www.hopepublishing.com/"> Hope Publishing</a>. All other license types can be issued through EMI CMG Publishing.';
    lblError.style.display = "block";
    //Can't set focus to the label control -- use button instead -- rendered as input
    var btnSongSearchToSongDetail = document.getElementById("ctl00_licensingContent_btnSongSearchToSongDetail");
    btnSongSearchToSongDetail.focus();
}

function GetUserQueue() 
    {
        //Perform call to webservice to get the song queue for the current user.
        //Current user stored in hidden field on page load event of SongSearch.aspx.
        var userName = document.getElementById("ctl00_licensingContent_hidUserName").value;              
        EMICMGPublishing.SongQueueService.SelectFromQueue(userName, false, onSelectComplete);
        if (userName == '')
            document.getElementById("ctl00_licensingContent_btnClearQueue").style.visibility = "hidden"
        else
            document.getElementById("ctl00_licensingContent_btnClearQueue").style.visibility = "visible";                          
    }
    
    //The following functions populates the html to show the contents of a product list.        
function onSelectComplete(results) 
    {
        //Sigh....if XML-Script shown below were faster we could reduce the code in this function down to one line following the script
        
        /*      <asp:Panel ID="pnlProductList" runat="server" SkinID="ListPanel">
                    <div id="listResults" class="prodList">
                    </div>
                    <div id="listResults_layoutTemplate" class="prodListBorder">
                        <ul id="listResults_itemTemplateParent">
                            <li id="listResults_itemTemplate">
                                <img id="listResults_ImageRemoveLink" />
                                <span style="text-align: left;" id="listResults_Product"></span>
                                <br />
                                <span style="text-align: left;" id="listResults_Name"></span></li>
                        </ul>
                    </div>
                </asp:Panel>
                
                <script type="text/xml-script">
        <page xmlns:script="http://schemas.microsoft.com/xml-script/2005">
            <components>
                <listView   id="listResults" 
                            itemTemplateParentElementId="listResults_itemTemplateParent" 
                            alternatingItemCssClass="alternatingItem" 
                            itemCssClass="userList">
                    <layoutTemplate>
                        <template layoutElement="listResults_layoutTemplate" />
                    </layoutTemplate>
                    <itemTemplate>
                        <template layoutElement="listResults_itemTemplate">
                            <label id="listResults_Product">
                                <bindings>
                                    <binding dataPath="ProductId" property="text" />
                                </bindings>
                            </label>
                            <label id="listResults_Name">
                                <bindings>
                                    <binding dataPath="ProductTitle" property="text" />
                                </bindings>
                            </label>                            
                            <image id="listResults_ImageRemoveLink">
                                <bindings>
                                    <binding dataPath="ImageURL" property="imageURL" />
                                    <binding dataPath="ImageVisible" property="visible" />                                   
                                </bindings>
                                <behaviors>
                                    <clickBehavior click="DeleteProductFromList" />
                                </behaviors>
                            </image>
                        </template>
                    </itemTemplate>
                </listView>
                <button id="btnClearList">
                    <behaviors>                                        
                        <clickBehavior click="ClearProductList" />
                    </behaviors>
                </button>
                <application load="GetProductsInList"/>
            </components>
        </page>
        </script>*/
        
        //$object("listResults").set_data(results); 
        
        
        //Get the parent div element....defined in the ASPX page.    
        var pdiv = document.getElementById('searchResults');
        
        //Get the scroll position from the previous div tag, if one has been populated....
        var prev_cdiv = document.getElementById('searchResults_Template');
        var st = 0; //Default scroll top
        if (prev_cdiv !== null) st = prev_cdiv.scrollTop;

        
        //Create the child div element that will go inside the parent...and set it's attributes
        //the className defined here changes the scroll bar colors, etc.
        //var cdiv = document.getElementById('listResults_Template')?document.getElementById('listResults_Template'):document.createElement('div');        
        var cdiv = document.createElement('div');
        cdiv.setAttribute('id','searchResults_Template');
        cdiv.setAttribute('className','songListBorder');
        cdiv.innerHTML = '';
        
        //We are going to create an unordered list to display our content...each
        //list item will be populated with the data returned from the web service.
            
        var ul=document.createElement('ul');
        ul.setAttribute('id','songList');
        
        //Loop through the objects returns by the web service and write out the HTML
        //The objects in the results array are of type ProductData which is defined
        //in the webservice...AJAX makes using this web service defined class possible :)
        for(var i=0;i<results.length;i++)
            {

                        var li=document.createElement('li');
                        
                        //Setup hyperlink tag for removing products from list....the title attribute
                        //is used to hold the product id being shown for use in the javascript call to delete.
                        if (results[i].ImageVisible)
                        {
                            var hlink=document.createElement('a');
                            /*if (results[i].ImageVisible)
                                hlink.style.visiblity = 'visible';
                            else
                                hlink.style.visibility = 'hidden'; */
                            hlink.setAttribute('href','#');
                            hlink.setAttribute('title',results[i].SongId);
                            hlink.innerHTML = '<IMG src="' + results[i].ImageURL + '" alt="Remove ' + results[i].SongName + ' From List" border="0" />&nbsp;';
                            hlink.onclick = function(){DeleteFromQueue(this.title)};
                            var hlink2 = document.createElement('span');
                            //hlink2.setAttribute('href','#');
                            //hlink2.setAttribute('className','songDesc');
                            hlink2.innerHTML = ' ' + results[i].SongName;
                            li.appendChild(hlink); 
                            li.appendChild(hlink2); 
                        }
                        else
                        {
                            var msg = document.createElement('span');
                            msg.innerHTML = results[i].SongName; 
                            li.appendChild(msg); 
                        }
      
                        ul.appendChild(li); 

            }
           
        cdiv.appendChild(ul);        
        
        // Reset the parent div tags HTML to '' and then append the new child div tag
        // containing the new HTML to the parent.
        pdiv.innerHTML = '';
        pdiv.appendChild(cdiv);
        
        //set the postition of the scroll bar for the left side to it's previous value
        //note...this must be done after the child div tag is appended to the parent.
        pdiv.firstChild.scrollTop = st;
        
        //setTimeout("ActivateAlertDiv('hidden','')", 1000);
        //batchRestrictionError = false; 
    }

         
/*function onSelectComplete(results) 
    {
        //Two different ways to do this...I prefer the one liner approach.
        
        //var searchResults = document.getElementById("searchResults");
        //searchResults.control.set_data(results);
        $object("searchResults").set_data(results);        
    }
*/
    
function AddToQueue(songId, songName, labelCopy)
    {
        //Perform call to webservice to add a song to the user's queue.
        var userName = document.getElementById("ctl00_licensingContent_hidUserName").value;
        EMICMGPublishing.SongQueueService.AddToQueue(userName, songId, songName, labelCopy, onSelectComplete);
    } 
       
function DeleteFromQueue(songToDelete) 
    {   

        var userName = document.getElementById("ctl00_licensingContent_hidUserName").value;        
        //Perform call to webservice to delete a song from the queue.
        EMICMGPublishing.SongQueueService.DeleteFromQueue(userName, songToDelete, onSelectComplete); 
    }
    
function ClearQueue()

    {
         var userName = document.getElementById("ctl00_licensingContent_hidUserName").value;
        EMICMGPublishing.SongQueueService.ClearQueue(userName, onSelectComplete);
    }    
    
function sendToClipboard(s)
   {
   
        /*Replace the html equivalent of a single quote with a single quote */
        var toberep = "&#39;";
        var repwith = "'";
        var i = s.indexOf(toberep);
   

        while(i > -1)

                {
                    s = s.replace(toberep, repwith);
                    i = s.indexOf(toberep, i + repwith.length + 1);
                }

        if( window.clipboardData && clipboardData.setData )
        {
            clipboardData.setData("Text", s);
        }
        else
        {
            alert("Internet Explorer required");
        }
   }
   
function ValidateCriteriaEntered(source, args)

{
    var txtSongName = document.getElementById("ctl00_licensingContent_txtSongName").value;
    var txtWriterName = document.getElementById("ctl00_licensingContent_txtWriterName").value;
    var txtPublisherName = document.getElementById("ctl00_licensingContent_txtPublisherName").value;
    var txtProductTitle = document.getElementById("ctl00_licensingContent_txtProductTitle").value;
    var txtArtistName = document.getElementById("ctl00_licensingContent_txtArtistName").value;

    args.IsValid = true;

    if ((txtSongName.length <= 0) & (txtWriterName.length <= 0) & (txtPublisherName.length <= 0) & (txtProductTitle.length <= 0) & (txtArtistName.length <= 0)) args.IsValid = false;
 
}

if (typeof(Sys) !== "undefined") Sys.Application.notifyScriptLoaded();

    
   
    


