﻿// 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) 
    {
        GetUserCart();     
    }    

function AbortPostBack() 
    {
      var prm = Sys.WebForms.PageRequestManager.getInstance();
      if (prm.get_isInAsyncPostBack()) {
           prm.abortPostBack();
      }        
    }

function GetUserCart() 
    {
        //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 CartId = document.getElementById("ctl00_licensingContent_hidCartId").value;
        if (CartId == '') {
            document.getElementById("ctl00_licensingContent_btnClearCart").style.visibility = "hidden"
        }
        else {
            EMICMGLicensing.SongQueueService.SelectFromCart(CartId, false, onSelectComplete);
            document.getElementById("ctl00_licensingContent_btnClearCart").style.visibility = "visible";
        }
            
    }
    
    //The following functions populates the html to show the contents of a product list.        
function onSelectComplete(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(){DeleteFromCart(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 HideErrorLabel()
{
    var lblError = document.getElementById("ctl00_licensingContent_lblError");
    lblError.style.display = "none";
}

function ShowFirstUsesMessage(songId)
{
    var lblError = document.getElementById("ctl00_licensingContent_lblError");
    lblError.innerHTML = 'First Use - Before we can proceed with your request we need to review details regarding the song you attempted to add to this license. We will notify you via email when this process is complete. Please <a href="/LicensingPages/ContactUs.aspx\?Source=FirstUse&SongId=' + songId + '"> click here</a> to proceed.';
    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 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 Licensing.';
    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 ShowCherryLaneMessage(licenseForm)
{
    var lblError = document.getElementById("ctl00_licensingContent_lblError");
    //if (hlRequestReducedRate.innerHTML == "Click here to cancel request")
    lblError.innerHTML = 'You have selected a song in the Cherry Lane catalog which requires permission from a different entity, please <a href="http://www.musicservices.org"> click here</a> to license this song!';
    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 AddToCart(songId, songName, labelCopy)
    {
        //Perform call to webservice to add a song to the user's queue.
        //var userName = document.getElementById("ctl00_licensingContent_hidUserName").value;
        var CartId = document.getElementById("ctl00_licensingContent_hidCartId").value;
        EMICMGLicensing.SongQueueService.AddToCart(CartId, songId, songName, labelCopy, onSelectComplete);
                        
        if (document.getElementById("ctl00_licensingContent_lblError") !== null)        
        {
            document.getElementById("ctl00_licensingContent_lblError").style.display = "none";        
        }       
        
        
    } 
       
function DeleteFromCart(songToDelete) 
    {   

        //var userName = document.getElementById("ctl00_licensingContent_hidUserName").value;        
        var CartId = document.getElementById("ctl00_licensingContent_hidCartId").value;
        //Perform call to webservice to delete a song from the queue.
        EMICMGLicensing.SongQueueService.DeleteFromCart(CartId, songToDelete, onSelectComplete); 
    }
    
function ClearCart()

    {
        //var userName = document.getElementById("ctl00_licensingContent_hidUserName").value;
        var CartId = document.getElementById("ctl00_licensingContent_hidCartId").value;
        EMICMGLicensing.SongQueueService.ClearCart(CartId, 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();

    
   
    



