var EMPTY, isIE, isFF, isOP;
EMPTY = "";
isIE = false;
isFF = false;
isOP = false;
if(document.all  && document.getElementById && !window.opera) isIE = true;
if(!document.all && document.getElementById && !window.opera) isFF = true;
if(document.all  && document.getElementById && window.opera)  isOP = true;
function init() {
   var s = getCookie("cmsCPCOID");
   if (s != EMPTY) { getDOM("idBox").checked = true; getDOM("idUnInp").value = s;}
   var ndx, sS;
   if (getDOM("idWho").innerHTML == EMPTY) return;
   if (getDOM("idLog").innerHTML == "Y") {
      if (isIE) window.open("cms2M.aspx", "cmsLeft");
      else {
         if (isFF) window.showModalDialog("cmsFFModalDialog.htm",null,"dialogWidth:600px; dialogHeight:550px;"); //https://addons.mozilla.org/en-US/firefox/addon/1419
         else alert("Sorry, please use Internet Explorer or Firefox");
         return;
      }
      getDOM("idLoginSpan").style.display = "none";
      getDOM("idLogoutSpan").style.display = "block";
   }
   getDOM("idMsg").title = getDOM("idInfo").innerHTML;
   window.status = "OK";
}
function inputOnKeyDown(e) {
   try {
      var c   = e.keyCode;
      if (c == 13 || c == 0) {
         var a;
         if (isIE || isOP) a = e.srcElement;
         else if (isFF)    a = e.target;
         if (a.id == "undefined" || a.id == null) return;
         var aId = a.id;
         if (aId == "idSearchInp") clickedSearch(a);
         else if (aId == "idUnInp" || aId == "idPwdInp") clickedLogin();
      }
   } catch (ex) { alert(ex); }
}
function clickedId(a) {
   var b, c;
   b = getDOM("idUnInp").value;
   if (b == EMPTY) return;
   c = window.top.cmsLeft.location.href;
   window.open("cms1G.aspx" + (c.indexOf("cms1G.aspx?id=") >= 0 ? EMPTY : "?id=" + b), "cmsLeft");
}
function clickedBox() {
   var a = getDOM("idBox");
   if (a.checked) setCookie("cmsCPCOID", getDOM("idUnInp").value);
   else           setCookie("cmsCPCOID", EMPTY);
}
function clickedSearch(a) {
   var aId, aUrl, t, r;
   aId  = a.id;
   r    = "&r=" + Math.random();
   t    = trim(getDOM("idSearchInp").value);
   if (aId == "idSearchInp" || aId == "idSearchImg")  {
      if (t == EMPTY) return;
      var c, m, g;
      c = getDOM("idCPCORadio");
      m = getDOM("idMyCPCORadio");
      g = getDOM("idGoogleRadio");
      if      (c.checked) clickedSearch(c);
      else if (m.checked) clickedSearch(m);
      else if (g.checked) clickedSearch(g);
   } else if (aId == "idCPCORadio")    {
      if (t == EMPTY) return;
      window.open("cmsSearch.aspx?v=cpco&s=" + t + r, "cmsFrame");
   } else if (aId == "idMyCPCORadio")  {
      if (t == EMPTY) return;
      window.open("cmsSearch.aspx?v=myCpco&s=" + t + r, "cmsFrame");
   } else if (aId == "idGoogleRadio")  {
      if (t == EMPTY) return;
      window.open("http://www.google.com/search?q=" + t + "&btnG=Search", "cmsFrame");
   } else if (aId == "idSel")   {
      if      (a.selectedIndex == 1) window.open("cmsHelp.htm", "cmsFrame");
      else if (a.selectedIndex == 2) window.open("cmsSearch.aspx?v=recentCpco&s="   + t + r, "cmsFrame");
      else if (a.selectedIndex == 3) window.open("cmsSearch.aspx?v=recentMyCpco&s=" + t + r, "cmsFrame");
      a.blur();
   }
}
function clickedSchool(a)       {
   var q;
   q = a.innerHTML + "," + getDOM("idCity").innerHTML + "," + "Ontario,Canada";
   window.open("http://www.google.com/search?q=" + escape(q) + "&btnG=Search", "cmsFrame");
}
function clickedLogin()       {
   if (!isIE) { 
      if (isFF) window.showModalDialog("cmsFFModalDialog.htm",null, "dialogWidth:600px; dialogHeight:550px;"); //https://addons.mozilla.org/en-US/firefox/addon/1419
      else alert("Sorry, please use Internet Explorer or Firefox");
      return;
   }
   var a = getDOM("idUnInp");
   var b = getDOM("idPwdInp");
   if (trim(a.value) == EMPTY) { alert ("please type an username"); a.focus();  return; }
   if (trim(b.value) == EMPTY) { alert ("please type a password");  b.focus();  return; }
   var c = getDOM("idBox");
   if (c.checked) setCookie("cmsCPCOID", getDOM("idUnInp").value);
   window.status = "Processing...";
   document.location.href = "cmsTop.aspx?a=" + a.value + "&b=" + b.value;
   window.status = "OK";
}
function logout()             { window.open("cmsBanner.htm?img/Banner.jpg", "cmsBanner"); document.location.href = "cmsTop.aspx?c=out";  window.open("cms1G.aspx", "cmsLeft");}
function setCookie(name, v)   {
   var locPath, path, expDate, expires;
   locPath = location.pathname;
   path    = locPath.substring(0,locPath.lastIndexOf('/')) + '/';
   expDate = new Date();
   expDate.setTime(expDate.getTime() + (1 * 365 * 24 * 3600 * 1000));
   expires = expDate.toGMTString();
   document.cookie = name + "=" + escape(v) + "; expires=" + expires + "; path=" + path;
}
function getCookie(name)      {
   var i, c, cS, cH, len, ndx, t, buf;
   cH = {};
   buf = "";
   c = document.cookie;
   cS = c.split("; ");
   len = cS.length;
   cH.length = len;
   for (i = 0; i < len; i++) {
      t = cS[i];
      ndx = t.indexOf("=");
      if (ndx > 0) cH[t.substring(0, ndx)] = t.substring(ndx + 1);
   }
   return cH[name] == null || cH[name] == "undefined" ? "" : unescape(cH[name]);
}
function trim(str)  { return lTrim(rTrim(str)); }
function lTrim(str) { var re = /^\s */; return str.replace(re, ''); }
function rTrim(str) { var re = /\s *$/; return str.replace(re, ''); }
function getDOM(a)  { return document.getElementById(a); }
function mv(a)      { a.style.cursor='hand';     a.style.textDecoration="underline";}
function mt(a)      { a.style.cursor='default';  a.style.textDecoration="none";}
