﻿// Written by Will Fastie 06 Aug 2008, updated 09 Nov 08
// for Chesapeake Medical Systems

// jQuery must be loaded first
// This module can only contain functions. It must not include any directly executing code.

/*global CMS*/
CMS = {};

// Will Fastie's email address protection/obfuscation code
CMS.constructaddr = function (ename, edomaincode) {
    var addr = ename + '@' + CMS.myd(edomaincode);
    return addr;
};

CMS.nb = function (ename, edomaincode, edisplay, esubj) {
    var subj = "?subject=";
    var addr = CMS.constructaddr(ename, edomaincode);
    var display = addr;
    var atag;
    if (((arguments.length === 4) || (arguments.length === 3)) && (edisplay !== "")) {
        display = edisplay;
    }
    if ((arguments.length === 4) && (esubj !== "")) {
        addr = addr + subj + esubj;
    }
    atag = "<a " +  " href=\"mailto:" + addr + "\">" + display + "</a>";
    document.write(atag);
    return null;
};

CMS.myd = function (id) {
    var dn = 'cms24-7';
    var tldc = '.com';
    var tldn = '.net'; // testing only
    var d;
    switch (id) {
    case "cc":
        d = dn + tldc;
        break;
    case "fn":
        d = 'fastie' + tldn;
        break;
    default: // if the id code does not match, assume that the id is a domain name
        d = id;
        break;
    }
    return d;
};
// -- end email protection

// Will Fastie's Internet Explorer detection code
CMS.isIE6 = function () {
  var myAgt = navigator.userAgent;
  var myAgtIndex;
  var myAgtIE;
  var myIEVer;
  if (myAgt.indexOf('MSIE') !== -1 && myAgt.indexOf('Opera') === -1) { // Is this IE and not Opera?
    myAgtIndex = myAgt.indexOf('MSIE');
    myAgtIE = myAgt.substr(myAgtIndex, 8);
    myIEVer = myAgtIE.substr(5, 8);
    if (myIEVer < 6) {
      return true;  // assume older browsers are just like IE6
    } else if (myIEVer == 6.0) { // need soft comparison here; === returns false
      // need to see if browser is pretending to be 6 (like Opera)
      // for now, just assume it is IE6
      return true;
    } else {
      return false;  // anything > 6 is okay
    }
  } else {
    return false;  // anything other than IE is okay
  }
};

// Parse a parameter value out of a URL (NetLobo)
CMS.getParamValue = function (name) {
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

// Read a value from the cookie
CMS.getCookie = function (szName) {
 	var szValue =	null;
	if(document.cookie) { //only if exists
    var arr = document.cookie.split((escape(szName) + '=')); 
    if (2 <= arr.length) {
      var arr2 = arr[1].split(';');
      szValue  = unescape(arr2[0]);
    }
	}
	return szValue;
}
