﻿
function OnClientPasteHtml(sender, args)      
{      
    var commandName = args.get_commandName();      
    var value = args.get_value();      
    
    if (commandName == "ImageManager")      
    {      
        var div = document.createElement("DIV");
     
        //Do not use div.innerHTML as in IE this would cause the image's src or the link's href to be converted to absolute path.      
        //This is a severe IE quirk.      
        Telerik.Web.UI.Editor.Utils.setElementInnerHtml(div,value);
     
        //Now check if there is alt attribute       
        var img = div.firstChild;
        if (!img.alt)
        {
            var alt= getImageAltText(img.src);
            img.setAttribute("alt", alt);
            args.set_value(div.innerHTML);
        }       
    }
}

function getImageAltText(imageURL) {

  // this must use a Synchronous call, so the result is available within the
  // context  
  if (imageURL.length > 0)
  {
    var soapEnv =  
      "<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'> \
          <soapenv:Body> \
              <GetAltText xmlns='http://www.niaid.nih.gov/webservices/'> \
                <imageURL>" + imageURL + "</imageURL> \
                <currentWebID>" + currentWebID + "</currentWebID> \
              </GetAltText> \
          </soapenv:Body> \
      </soapenv:Envelope>";

    retval = jQuery.ajax({
      url: "_vti_bin/NIAID.Internet.Publishing.Customizations.SiteImages.asmx",
      type: "POST", 
      dataType: "xml",
      data: soapEnv,
      async: false,
      contentType: "text/xml; charset=\"utf-8\""
    });

    var result=jQuery(retval.responseXML).find('GetAltTextResult').text();
    return result;
  }
}

