function checktarget()
{
   // This is a workaround for buggy WinIE anchors
   if (window.location.search.indexOf("target=")>0)
   {
      var search = window.location.search.substring(1).split("&");
      for (var i=0; i<search.length; i++)
      {
         if (search[i].substring(0,7) == "target=")
         {
            window.location.hash = search[i].substring(7);
            break;
         }
      }
   }
}
function getBaseURL()
{
  var url=document.URL;
  var j=url.lastIndexOf('/');
  return url.substring(0,j+1)
}
function vendorpop(link, id)
{
   if (id != "1")
   {
      window.open(link.href, "_blank");
      return false;
   }
   else
   {
      return true;
   }
}
function triggerOrder(link)
{
  var linkurl = link.href;
  if (linkurl != null)
  {
     var target = link.target;
     if (target == null)
        target = "_blank";
     window.open(linkurl, target);
  }

  //var url = getBaseURL()+'order.cgi';
  //window.location = url;
  return false;
}
function popsponsor(flag,flag2)
{
  var url=document.URL;
  var baseurl='/sponsorpopup.cgi';
  // check for clients.clotho.com dev server
  // this may break in subdirs on that server...
  if (document.URL.indexOf("students/") > 0)
    baseurl = "/students"+baseurl;
  if (url.indexOf("teachingbooks.") < 0) 
     baseurl=getBaseURL()+'sponsorpopup.cgi';
  if (flag != null && flag != '')
    return dopopsponsor(baseurl+"?popsponsor="+flag);
  if (flag2 != null && flag2 != '')
    return dopopsponsor(baseurl+"?popvendor="+flag2);
  var i=url.indexOf('?');
  if (i<0 || url.indexOf("popsponsor", i)<0)
    return null;
  var cgiargs=url.substring(i+1).split("&");
  for(var k=0; k<cgiargs.length; k++)
    if (cgiargs[k].substring(0,10)=="popsponsor")
      return dopopsponsor(baseurl+"?"+cgiargs[k]);
  return null;
}
function dopopsponsor(url)
{
  var w=300;
  var h=300;
  var pos="";
  var x=0;
  var y=0;
  if (window.screenY != null)
    y=window.screenY;
  if (window.screenX != null && window.outerWidth != null)
    x=window.screenX+window.outerWidth-w;
  else if (window.screen != null && window.screen.width != null && window.screen.width > w)
    x=window.screen.width-w;
  if (x != 0)
    if (navigator.appVersion.indexOf("MSIE")==-1)
      pos=",screenX="+x+",screenY="+y;
    else
      pos=",left="+x+",top="+y;
  window.open(url,"_blank","width="+w+",height="+h + pos+",toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=no,resizable=no");
}
function clickLink(node)
{
   if (node != null && node.nodeType == 1)
   {
      if (node.tagName.toUpperCase() == "A")
      {
         var url = node.getAttribute("href");
         var target = node.getAttribute("target");
         if (target != null && (target != "" && target != "_self"))
            window.open(url,target);
         else
            window.location = url;
         return true;
      }
      else
      {
         for (var i=0; i<node.childNodes.length; i++)
            if (clickLink(node.childNodes[i]))
               return true;
      }
   }
   return false;
}
var pendingims=[];
function doPendingIms()
{
   while (pendingims.length > 0)
   {
      var imdata = pendingims.pop();
      //alert("set im "+imdata.im.src+" -> "+imdata.src);
      imdata.im.src = imdata.src;
   }
}
function selectTab(node)
{
   var href = node.href;
   node = node.parentNode;
   var nodes = [];
   for (var i=0; i<node.parentNode.childNodes.length; i++)
   {
      var n = node.parentNode.childNodes[i];
      if (n.nodeType == 1)
         nodes[nodes.length] = n;
   }
   for (var j=0; j<nodes.length; j++)
   {
      var imgs = nodes[j].getElementsByTagName("IMG");
      if (nodes[j] == node)
      {
         if (nodes[j].className != "selected")
            nodes[j].className = "selected";
         for (var i=0; i<imgs.length; i++)
         {
            var src = imgs[i].src;
            var idx = src.indexOf(".gif");
            if (idx > 0 && src.substring(idx-3,idx) != "_on")
            {
               pendingims.push({im:imgs[i], src:src.substring(0,idx)+"_on"+src.substring(idx)});
               //imgs[i].src = src.substring(0,idx)+"_on"+src.substring(idx);
            }
         }
      }
      else
      {
         if (nodes[j].className != "")
            nodes[j].className = "";
         for (var i=0; i<imgs.length; i++)
         {
            var src = imgs[i].src;
            var idx = src.indexOf(".gif");
            if (idx > 0 && src.substring(idx-3,idx) == "_on")
            {
               pendingims.push({im:imgs[i], src:src.substring(0,idx-3)+src.substring(idx)});
               //imgs[i].src = src.substring(0,idx-3)+src.substring(idx);
            }
         }
      }
   }
   // Delay is needed to workaround a WinIE v6 render bug
   setTimeout(doPendingIms,10);

   return href != null &&
          href != window.location &&
          href != "" &&
          href != "#" &&
          href.indexOf("/#") != href.length-2;
}
function entertab(node)
{
   if (node.className != "selected")
   {
      var imgs = node.getElementsByTagName("IMG");
      for (var i=0; i<imgs.length; i++)
      {
         var src = imgs[i].src;
         var idx = src.indexOf(".gif");
         if (idx > 0 && src.substring(idx-3,idx) != "_on")
         {
            imgs[i].src = src.substring(0,idx)+"_on"+src.substring(idx);
         }
      }      
   }
}
function exittab(node)
{
   if (node.className != "selected")
   {
      var imgs = node.getElementsByTagName("IMG");
      for (var i=0; i<imgs.length; i++)
      {
         var src = imgs[i].src;
         var idx = src.indexOf(".gif");
         if (idx > 0 && src.substring(idx-3,idx) == "_on")
         {
            imgs[i].src = src.substring(0,idx-3)+src.substring(idx);
         }
      }      
   }
}
function clearIfVal(el,str)
{
   if (el.value == str)
      el.value = "";
}

function check_freetrial(form)
{
   // more detailed checks happen server-side
   if (form.contact_name.value == "")
   {
      alert("Please fill in your name");
      form.contact_name.focus();
   }
/*
   else if (form.account_type.selectedIndex < 1)
   {
      alert("Please select a setting");
      form.account_type.focus();
   }
*/
   else if (form.district_name.value == "")
   {
      alert("Please enter an affiliation or district");
      form.district_name.focus();
   }
   else if (form.contact_title.selectedIndex < 1)
   {
      alert("Please select a title");
      form.contact_title.focus();
   }
   else if (form.email_address.value == "")
   {
      alert("Please enter your email address");
      form.email_address.focus();
   }
   else if (form.email_address2.value == "")
   {
      alert("Please re-enter your email address");
      form.email_address2.focus();
   }
   else if (form.email_address.value.toLowerCase() != form.email_address2.value.toLowerCase())
   {
      alert("Your emails don't match.  Please try again.");
      form.email_address.focus();
   }
   else if (form.email_address.value.indexOf("@") < 1 ||
            form.email_address.value.indexOf("@") != form.email_address.value.lastIndexOf("@"))
   {
      alert("That is not a valid email address.  Please try again.");
      form.email_address.focus();
   }
   else
      return true;
   return false;
}
function appendEmail(node, id)
{
   // used for "Forgot password?" links
   var email = document.getElementById(id).value;
   if (email != null && email != "")
   {
      var url = node.getAttribute("href");
      if (url.indexOf("?") < 0) // punt if it already has a query string
         node.setAttribute("href", url+"?email_address="+email+"&op=send");
   }
   return true;
}
function clearquick()
{
   for (var f=0; f<document.forms.length; f++)
      for (var e=0; e<document.forms[f].elements.length; e++)
         if (document.forms[f].elements[e].name == "quick")
            document.forms[f].elements[e].selectedIndex = 0;
}

function corners()
{
   // Don't even try Mac IE
   var isie = (navigator.appName.indexOf("Microsoft") != -1) ? 1 : 0;
   var ismac = (navigator.appVersion.indexOf("Macintosh") != -1) ? 1 : 0;
   if ((ismac && isie) || !NiftyCheck()) return;

   var bkgd = false;
   var box = document.getElementById("mainbox");
   if (box != null && box.className == "bkgd")
   {
      bkgd = true;
      // This is effectively a Mozilla 0.9.5-and-up detect
      if (box.style.MozBorderRadius != null)
      {
         // Use the Mozilla-specific -moz-border-radius feature to implement the corners
         var filler = document.getElementById("curvefiller");
         if (filler != null)
            filler.style.display = "none";
            //filler.style.height = "0px";
      }
      else
      {
         box.style.borderStyle = "none";
         Rounded("div.bkgd","all","#fff","#ccf", "border #666");
      }
   }
   Rounded("div#topbar","bottom","#fff","#333");
   Rounded("div#footerbar","top","#fff","#333");

   var linkboxes = {
      totalbox: {color:"#c00", border: "#fff"},
      eduregisterbox: {color:"#900",border:"#fff", bg: "#099"},
      alreadypassbox: {color:"#900",border:"#fff"},
      curriclistbox: {color:"#c0c",border:"#fff"},
      edubox: {color:"#099",border:"#fff"},
      k12box: {color:"#c0c",border:"#fff"}
   };
   for (var boxid in linkboxes)
   {
      var box = document.getElementById(boxid);
      if (box != null)
      {
         if (box.style.MozBorderRadius == null)
         {
            box.style.borderStyle = "none";
            var color = linkboxes[boxid].color;
            if (color == null)
               color = box.style.backgroundColor;
            var border = linkboxes[boxid].border;
            if (border != null)
               border = "border "+border;
            var child = linkboxes[boxid].child ? " "+linkboxes[boxid].child : "";
            var bg = linkboxes[boxid].bg ? linkboxes[boxid].bg : (bkgd?"#ccf":"#fff");
            Rounded("div#"+boxid+child,"all",bg,color,border);
         }
      }
   }
}

function rotate_images(delay, id, urls) {
   if (urls && urls.length > 0 && urls[urls.length-1] == '0')
     urls.pop();
   if (urls && urls.length > 0) {
      var _rotate_image = function() {
         var e = document.getElementById(id);
         var url = urls[Math.floor(Math.random() * urls.length)];
         if (e && url) {
            if (e.src == url && urls.length > 1)
               _rotate_image(id, urls);
            else
               e.src = url;
         }
      };
      _rotate_image();
      if (urls.length > 1 && delay > 0)
         setInterval(_rotate_image, delay, 1);
   }
}

function showhide(id) { 
   var e = document.getElementById(id);
   var e2 = document.getElementById("button_"+id);
   if (e) {
      if (e.style.display == 'block') { 
         e.style.display = 'none';
         if (e2) { e2.innerHTML = "+"; }
      } else {
         e.style.display = 'block';
         if (e2) { e2.innerHTML = "-"; }
      }
   }
}

function showhide2(id) { 
   var e = document.getElementById(id);
   var e2 = document.getElementById("button_"+id);
   if (e) {
      if (e.style.display == 'block') { 
         if (e2) {
			 jscss('add',e2,'closed');
		   }
		 e.style.display = 'none';
      } else {
         if (e2) {
		     jscss('remove',e2,'closed');
		   }
		 e.style.display = 'block';
      }
   }
}

function jscss(a,o,c1,c2) {
  switch (a){
    case 'swap':
      o.className=!jscss('check',o,c1)?o.className.replace(c2,c1): o.className.replace(c1,c2);
    break;
    case 'add':
      if(!jscss('check',o,c1)){o.className+=o.className?' '+c1:c1;}
    break;
    case 'remove':
      var rep=o.className.match(' '+c1)?' '+c1:c1;
      o.className=o.className.replace(rep,'');
    break;
    case 'check':
      return new RegExp('\\b'+c1+'\\b').test(o.className)
    break;
  }
}


function getXMLHTTP() {

   var a=null;
   if (typeof XMLHttpRequest != "undefined") {
      a = new XMLHttpRequest();
   }
   if (!a) {
      try {
         a = new ActiveXObject("Msxml2.XMLHTTP")
      } catch(e) {
         try{
            a = new ActiveXObject("Microsoft.XMLHTTP")
         } catch(oc) {
            a = null
         }
      }
   }

   return a;

}

// Set the password-question field on signin based on email address
// Obsolete.... a password is not the same as an authentication for
// extra features.
function setPasswordQuestion(email,field) {

   var xmlHTTP = getXMLHTTP();
   var geturl =  "pwq.cgi?&email="+email;

   if  (xmlHTTP) {
       // Do things synchronously.
       document.body.style.cursor='wait';
       xmlHTTP.open("GET",geturl,false);
       xmlHTTP.send(null);
       document.body.style.cursor='auto';
       data=xmlHTTP.responseText;
       e = document.getElementById(field);
       e.innerHTML = data;
   } 

}

