﻿/// <summary>OBJECT "imObject": MSNMessenger.P4QuickLaunch ActiveX Object</summary>
/// <remarks>Object for use in imIt which launches IM if object is not null</remarks>

var imObject = imCheck();
/// <summary>FUNCTION "imCheck": tests for presence of MSNMessenger.P4QuickLaunch ActiveX Object</summary>
/// <return>Object or null</return>
function imCheck() {
  var a = i = null;
  var obj = getObject("MSNMessenger.P4QuickLaunch");
  
  if (!obj) { 
    obj = null; 
    if (navigator.userAgent.indexOf("MSIE") > -1) { imDisable(noimMsg); };
  };
  return obj;
  
  function getObject(sname) {
    try { return new ActiveXObject(sname); }
    catch(e) { return null; }
  };
};

/// <summary>FUNCTION "imDisable": render IM link with disabled look and appropriate tooltip</summary>
/// <param name="msg">string value error message for img alt and anchor tooltip</param>
function imDisable(msg) {
  writeCookie("IMID","", null, null); 
  a = document.getElementById(imLinkId);
   
  if (!a) { return; };
  i = a.getElementsByTagName("IMG")[0];
  a.className = "nogif";
  a.removeAttribute("href");
  if (msg!=null && msg!="") { a.title = i.alt = msg; };
  i.src = i.src.replace(".gif", "no.gif");
};

/// <summary>FUNCTION "imIt": opens messenger</summary>
/// <param name="pname">name of parameter</param>
/// <param name="pval">documentid for storing in cookie</param>
/// <remarks>if no appID supplied IM will open session in "Start an Activity" mode (after User selects buddy to contact)</remarks>
/// <remarks>when appID is supplied IM will open session in "Invite" mode (after User selects buddy to contact), launch the IE pane on invite acceptance and paste the web url and a comment in the "Send" area of the Users IM</remarks>
function imIt(pname, pval) {
  if (imObject) {
    if (imAppId) { 
      var PageUrl = document.location.href;
      
      var cookieValue = escape(pname + "=" + pval) + "|" + escape(document.location.href);
      writeCookie("IMID", cookieValue, null, null); 
      
      // user can block 3rd party cookies or all cookies.
      if (document.cookie.indexOf(cookieValue) < 0) {
        imDisable(ckimMsg);
      } else {
        try { imObject.LaunchApp(imAppId, ""); }
        catch (e) { imDisable(''); }; //IM not running
      };
    } else { imObject.LaunchApp("", ""); }; 
  };
};

/// <summary>FUNCTION "printIt": open print dialog</summary>
/// <remarks>xpr_toolbar.css provides base print styling, also expsed in print preview</remarks>
function printIt() { window.print(); };

/// <summary>FUNCTION "blogIt": opens spaces blog entry with title and any selected texted entered</summary>
/// <param name="bnew">boolean indicating whether to open feature in new browser window</param>
function blogIt(bnew) {
  var txt = getSelectedText();

  window.open(msnSpacesApi + "?Title=" + document.title + 
              //"&SourceURL=" + escape(window.location.href.replace(window.location.hash, "")) + 
              "&SourceURL=" + escape(window.location.href) + 
              "&description=" + txt, (bnew) ? "_toolbar" : "_self", "", bnew)
};

/// <summary>FUNCTION "getSelection": returns selected text value</summary>
function getSelectedText() {
  var txt = "";
  if (window.getSelection) { txt = window.getSelection(); }
  else {
    var range = document.selection.createRange();
    range.expand("word");
    txt = range.text;
  };
  return txt;
};

/// <summary>FUNCTION "emailIt": opens "new message" in user's e-mail client</summary>
/// <param name="title">string value of document title - processed on server to ensure corect code-page encoding for non-utf8 mailto client</param>
/// <remarks>mailto only at present</remarks>
function emailIt(title) { 
  if (title == "") { title = document.title; };
  title = title.replace(/\"/g, escape('"'));  
  title = title.replace(/&/g,escape('&'));
  title = encodeURI(title);
  title = title.replace('%E2%80%93','');
  title = title.replace('%E2%80%93','');
  var pageUrl = window.location.href;
  if(pageUrl.indexOf(' ') > -1){
    pageUrl = pageUrl.replace(/\s/g,'%20');
  }
  window.location = "mailto:" + "?subject=" + title + "&body=" + title + ":%0d%0a%0d%0a" + escape(pageUrl); 
};

/// <summary>FUNCTION "": opens "article" for submission to MSN Reporter</summary>
/// <param name="bnew">boolean indicating whether to open feature in new browers window</param>
function reporterIt(base, referer, categ, tag, bnew){
  if (base.indexOf("?f=contribute") == -1) { base += "?f=contribute"; };
  var txt = getSelectedText();
  var url = base + "&url=" + escape(document.location.href) + "&referrer=" + escape(referer);
  url += "&title=" + (reporterTitle!=''? reporterTitle : document.title);
  url += (txt!="") ? "&remark=" + txt : "";
  url += (categ!="" ? "&cat_id=" + categ : "");
  url += (tag!="" ? "&tag_id=" + tag : "");
  
  window.open(url, (bnew) ? "_toolbar" : "_self", "", bnew);
};

/// <summary>Sets cookie content</summary>
/// <param name="cookieName">cookie name</param>
/// <param name="cookieValue">cookie content value</param>
/// <param name="expiryDate">date to expire</param>
/// <param name="path">cookie path value</param>
/// <return>false</return>
function writeCookie(cookieName, cookieValue, expiryDate, path) {
    var newExpiry = null;
    
    if (typeof expiryDate == "undefined" || expiryDate == null || expiryDate == "") {
        /* set expiry for today + 5 years */
        newExpiry = new Date();
        newExpiry.setYear(newExpiry.getFullYear() + 5);
    }
    else { newExpiry = expiryDate; }

    if (typeof path == "undefined" || path == null) { path = "/"; };
    
    if (typeof document!="undefined") { return document.cookie = cookieName + "=" + cookieValue + ";expires=" + newExpiry.toGMTString() + ";path=" + path + ";"; }
    return false;
};

function GetCleanUrl() { 
    return document.URL.replace(location.hash,""); 
};
