﻿// JScript File
  if (document.images)
  {
    pic0= new Image(13,14); 
    pic0.src="res/img/downloaded.gif"; 

    pic1= new Image(13,14); 
    pic1.src="res/img/preview_on.gif"; 
    pic2= new Image(13,14); 
    pic2.src="res/img/video_on.gif"; 
    pic3= new Image(13,14); 
    pic3.src="res/img/lyric_on.gif"; 
    pic4= new Image(13,14); 
    pic4.src="res/img/download_on.gif"; 
    pic5= new Image(74,17); 
    pic5.src="res/img/buttons/btn_select_disabled.gif"; 
  }

  function switchTracks(tblID, iconID)
  {
    var tbl = document.getElementById(tblID);
    var icon = document.getElementById(iconID);

    if (icon.src.indexOf("_off.gif")!=-1)
    {
      tbl.style.color = "#034275";
      icon.src = icon.src.replace("_off.gif", "_on.gif");
    }
    else
    {
      tbl.style.color = "#8f8f8f";
      icon.src = icon.src.replace("_on.gif", "_off.gif");
    }
  }  

  /*
   Called from DownloadOneTrack.aspx>>download_grid.ascx:
      When user first clicks the download url, open the download dialog box to save the file, mark the downloading item with a checkmark
      When user clicks it again and file has been downloaded successfully, display error message "Track has already been downloaded.",
    otherwise open the download dialog box to save the file.
  */
  function disableURL(tblID, iconID, url, msg)
  {
    var tbl = document.getElementById(tblID);
    var icon = document.getElementById(iconID);

    //icon.src='res/img/downloaded.gif';
    //tbl.style.color = "#034275";
    //tbl.onmouseout = null;
    //tbl.onmouseover = null;

    tbl.onclick = function() {
      newClick(tblID, iconID, url, msg)
    }
  }
  
  function newClick(tblID, iconID, url, msg)
  {
    var xmlHttp;
    try
    {
      // Firefox, Opera 8.0+, Safari
      xmlHttp=new XMLHttpRequest();
    }
    catch (e)
    {   // Internet Explorer
        try
        {
           xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e)
        {
           try
           {
              xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
           }
           catch (e)
           {
              alert("Your browser does not support AJAX!");
              return false;
           }
        }
    }
    
    //Check on the returns from the 
    xmlHttp.onreadystatechange=function()
    {
       if(xmlHttp.readyState==4)
       {
         var tbl = document.getElementById(tblID);
         var icon = document.getElementById(iconID);
         
         if (xmlHttp.responseText=="downloaded")
         {
           icon.src='res/img/downloaded.gif';
           //tbl.style.color = "#ff0000";
           tbl.style.cursor = "default";
           var e = document.getElementById(tbl.id.replace("_tblTrack","_lblTrackTitle"));

           e.innerHTML += "<span style='color:red;'>&nbsp;&nbsp;" + msg + "</span>";
           
           tbl.onclick = null;
         }
         else
         {
           url = url.replace("IsDownloaded.ashx","DownloadMedia.aspx");
           //disableURL(tblID, iconID, url);
           window.location.href = url;
         }
       }
    }
    
    if ((navigator.userAgent.indexOf("MSIE"))!=-1)
    {
      xmlHttp.open("GET", url, true);
    }
    else
    {
      xmlHttp.open("GET", url, true);
    }

    xmlHttp.send(null);
}