/* written by Aiping, August 2007 */

if (1) {
window.onload = function() {
   add_id_to_li();
   prepareRollovers();
}
} else {
window.onload = function() {
   prepareRollovers();
}
}

//window.onload = add_id_to_li;

var user_id = '';
function add_id_to_li() {

    if (document.getElementById("user_id")) {
         user_id = document.getElementById("user_id").innerHTML;
    } 
    if (user_id == '') {
	return false;
    }

    var title = document.getElementsByTagName("title")[0].innerHTML;
    var which_toc = title.replace(/The Chicago Manual of Style Online: /, '');

    var li_collection = document.getElementById("main_content").getElementsByTagName("li");


    if (which_toc == "Contents") {  // for book toc
        for (var i = 0; i < li_collection.length; i++) {
           if (li_collection[i].className == "chapter") {  // FIX for IE: el.getAttribute('class') always returns null on IE
	      id = li_collection[i].firstChild.innerHTML;
              //id = id.replace(/^&nbsp;&nbsp;/, '');
              //id = id.replace(/^\s+/, '');
	      id = id.replace(/[^0-9]/g, ''); // FIX for Safari, Opera
              if (id.match(/\d+$/)) {
                 li_collection[i].id = id;
              }
           }
        }
     }
     else {  // for chapter toc
        for (var i = 0; i < li_collection.length; i++) {
           if (li_collection[i].className) {  // FIX for IE: el.getAttribute('class') always returns null on IE
           }
	   else {
               id = li_collection[i].firstChild.innerHTML;
               if (id.match(/^\d+\.\d+$/)) { 
                  li_collection[i].id = id;
               }
           }
        }
    }
    xmlhttpPost(which_toc);
}

function xmlhttpPost(which_toc) {
    var strURL = '/cgi-bin/get_toc_AJAX.cgi';
    var xmlHttpReq = false;
    var self = this;


    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else {
        self.xmlHttpReq = new ActiveXObject("Msxml2.XMLHTTP");
    }

    if ((self.xmlHttpReq) || (self.xmlHttpReq!=false)) { 
       self.xmlHttpReq.open('POST', strURL, true);
       self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
       self.xmlHttpReq.onreadystatechange = function() {
          if (self.xmlHttpReq.readyState == 4) {
              //alert(self.xmlHttpReq.responseText);
              updatepage(which_toc,self.xmlHttpReq.responseText);
          }
       }
       self.xmlHttpReq.send(getquerystring(which_toc));
    }
    else {
       alert("Your browser does not support xmlHttpRequest.");
    }
}

function getquerystring(which_toc) {

    str = user_id + ' HHHHHHHHHH ' + which_toc;
    qstr = 'query=' + encodeURIComponent(str);
    return qstr;
}

function updatepage(which_toc,str){
    var success = str.split('#####')[0];
    var message = str.split('#####')[1];

    if (success == '1') {
       notes_array = message.split("***")[0];
       bookmarks_array = message.split("***")[1];

       if (bookmarks_array.length > 0) {
          bookmarks = bookmarks_array.split(";");
          for (var i=0; i<bookmarks.length; i++) {
              var no = bookmarks[i].split(",")[0];
              var bookmark_count = bookmarks[i].split(",")[1];

              var span = document.createElement("span");
              span.style.cssText = "margin-left:12px;";  // FIX for IE
              span.id = "chap_bookmarks_indicator";

              if (which_toc == "Contents") {
          	  if (bookmark_count > 99) {
                       span.innerHTML = "<img src='images/v1dot5/b99+.gif'>";
		  }
 		  else {
                       span.innerHTML = "<img src='images/v1dot5/b" + bookmark_count + ".gif'>";
		  }
                  document.getElementById(no).appendChild(span);
  	      }
 	      else {
                  span.innerHTML = "<img src='../images/v1dot5/b1.gif'>"
		  document.getElementById(no).appendChild(span);
	      }
          }
       }

       if (notes_array.length > 0) {
          notes = notes_array.split(";");
          for (var i=0; i<notes.length; i++) {
              var no = notes[i].split(",")[0];
              var note_count = notes[i].split(",")[1];

              var span = document.createElement("span");
	      span.style.cssText = "margin-left:10px;";	  // FIX for IE
              //span.setAttribute("style", "margin-left:10px;");  
              span.id = "chap_notes_indicator";

	      if (which_toc == "Contents") {
		  if (note_count > 99) {
		       span.innerHTML = "<img src='images/v1dot5/n99+.gif'>";
		  }
		  else {
		       span.innerHTML = "<img src='images/v1dot5/n" + note_count + ".gif'>";
		  }
                  document.getElementById(no).appendChild(span);
	      }
	      else {
 		  if (note_count > 99) {
		       span.innerHTML = "<img src='../images/v1dot5/n99+.gif'>";	
		  }
		  else {
		       span.innerHTML = "<img src='../images/v1dot5/n" + note_count + ".gif'>";
		  }
		  document.getElementById(no).appendChild(span);
	      }
          }
       }
    }
    else if (success == '0') {
       alert(message);
       return false;
    }
}

