﻿// JScript File

var http_request = false;

function rate(element, id, score) {
    var poststr = "docid=" + id;
    poststr += "&score=" + score;
    poststr += "&requesttype=ajax";
    poststr += "&action=partial";
    var url = location.href;
    if(url.indexOf("?") == -1) {
        url+="?"
    } else {
        url+="&"
    }
    url+="ratingID="+id
    makePOSTRequest(url, poststr, element, id);
    return false;
}

function makePOSTRequest(url, parameters, element, id) {
      //document.body.className += " waiting";
      document.body.style.cursor = "wait";
      http_request = false;
      if (window.XMLHttpRequest) { // Mozilla, Safari,...
         http_request = new XMLHttpRequest();
         if (http_request.overrideMimeType) {
         	// set type accordingly to anticipated content type
            //http_request.overrideMimeType('text/xml');
            http_request.overrideMimeType('text/html');
         }
      } else if (window.ActiveXObject) { // IE
         try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
         } catch (e) {
            try {
               http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
         }
      }
      if (!http_request) {
         alert('Cannot create XMLHTTP instance');
         return false;
      }
      
      http_request.onreadystatechange = function() {returnRating(element, id)};
      
      http_request.open('POST', url , true);
      http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      http_request.setRequestHeader("Content-length", parameters.length);
      http_request.setRequestHeader("Connection", "close");
      http_request.send(parameters);
      
   }

   function returnRating(element, id) {
      //grab a reference to the html object to be populated
      if (http_request.readyState == 4) {
          //document.body.className = "";
          document.body.style.cursor = "default";
         if (http_request.status == 200) {
            result = http_request.responseText;
            element.innerHTML = result;
            setPersistentCookie("rated_" + id, '1')
        }
      }
   }
   
   function setPersistentCookie(name,val)
    {
        var DaysToLive = 30; // CHANGE number 30 to number of days you want cookie to persist.
        var now = new Date();
        then = now.getTime() + (DaysToLive * 24 * 60 * 60 * 1000);
        now.setTime(then);
        document.cookie = name + '=' + val + '; expires=' + now.toGMTString() + '; path=/';
    }
    
    function viewAllArticlesQuestionsToggle(link) {
        var allquestions = document.getElementById("allquestions");
        var allarticles = document.getElementById("allarticles");
        if(allquestions.innerHTML == "All questions") {
            allquestions.innerHTML = "Selected questions";
            allarticles.innerHTML = "Selected articles";
            var elements = document.getElementsByClassName("show","li",allarticles.parentNode.parentNode);
            for (var i=0;i<elements.length;i++)
            {
                elements[i].className = 'hide';
            }
            link.innerHTML = "View all questions and articles"
        } else {
            allquestions.innerHTML = "All questions";
            allarticles.innerHTML = "All articles";
            var elements = document.getElementsByClassName("hide","li",allarticles.parentNode.parentNode);
            for (var i=0;i<elements.length;i++)
            {
                elements[i].className = 'show';
            }
            link.innerHTML = "View selected questions and articles"
        }
    }

// Opens a link in a new window when class = popup

function doPopups() {
  if (!document.getElementsByTagName) return false;
  var links = document.getElementsByTagName("a");
  for (var i=0; i < links.length; i++) {
    if (links[i].className.match("popup")) {
		links[i].className = links[i].className + " newWinStyle";
		if (links[i].title == "") {
		links[i].title = "(new window)";
		}
		else {
		links[i].title = links[i].title + " (new window)";	
		}
	 	links[i].onclick = function(e) {
			if(!e)e=window.event;
			if(e.shiftKey || e.ctrlKey || e.altKey) return;
			window.open(this.href);
			return false;
			}
      }
    }
}

// ADD FOOTNOTES  -------------------------------------------------------

// addClass() - appends the specified class to the object
function addClass(theClass) { if (this.className != '') { this.className += ' ' + theClass; } else { this.className = theClass; } }
// Object.prototype.addClass = addClass;

// inArray() - Hunts for a value in the specified array
function inArray(needle) { for (var i=0; i < this.length; i++) { if (this[i] === needle) { return i; } } return false; } Array.prototype.inArray = inArray;

// Collects all of the links in a page and adds them as footnotes
function footnoteLinks(containerID,targetID) { 
    if (!document.getElementById || !document.getElementsByTagName || !document.createElement) return false; 
    if (!document.getElementById(containerID) || !document.getElementById(targetID)) return false; 
    var container = document.getElementById(containerID); 
    var target = document.getElementById(targetID); 
    var h2 = document.createElement('h2'); 
    addClass.apply(h2,['printOnly']); 
    var h2_txt = document.createTextNode('Links'); 
    h2.appendChild(h2_txt); 
    var coll = container.getElementsByTagName('*'); 
    var ol = document.createElement('ol'); 
    addClass.apply(ol,['printOnly']); 
    var myArr = []; 
    var thisLink; 
    var num = 1; 
    for (var i=0; i<coll.length; i++) { 
        var thisClass = coll[i].className; 
        if ( (coll[i].getAttribute('href') || coll[i].getAttribute('cite')) && (thisClass == '' || thisClass.indexOf('ignore') == -1)) { 
            thisLink = coll[i].getAttribute('href') ? coll[i].href : coll[i].cite; 
            if (thisLink.length > 110) { 
                thisLink = thisLink.substr(0, 110) + "..."
            };
            var note = document.createElement('sup'); 
            addClass.apply(note,['printOnly']); 
            var note_txt;
            var j = inArray.apply(myArr,[thisLink]); 
            if ( j || j===0 ) { 
                note_txt = document.createTextNode(j+1);
            } 
            else { 
                var li = document.createElement('li'); 
                var li_txt = document.createTextNode(thisLink); 
                li.appendChild(li_txt); 
                ol.appendChild(li); 
                myArr.push(thisLink); 
                note_txt = document.createTextNode(num); 
                num++;
            } 
            note.appendChild(note_txt); 
            if (coll[i].tagName.toLowerCase() == 'blockquote') { 
                var lastChild = lastChildContainingText.apply(coll[i]); 
                lastChild.appendChild(note);
            } 
            else {
                try {
                    coll[i].parentNode.insertBefore(note, coll[i].nextSibling);
                }
                catch(err)
                {
                    //alert('err = ' + err);
                }
            }
        }
    }
    target.appendChild(h2); 
    target.appendChild(ol); 
    addClass.apply(document.getElementsByTagName('html')[0],['noted']); 
    return true;
}
//function footnoteLinks(containerID,targetID) { if (!document.getElementById || !document.getElementsByTagName || !document.createElement) return false; if (!document.getElementById(containerID) || !document.getElementById(targetID)) return false; var container = document.getElementById(containerID); var target = document.getElementById(targetID); var h2 = document.createElement('h2'); addClass.apply(h2,['printOnly']); var h2_txt = document.createTextNode('Links'); h2.appendChild(h2_txt); var coll = container.getElementsByTagName('*'); var ol = document.createElement('ol'); addClass.apply(ol,['printOnly']); var myArr = []; var thisLink; var num = 1; for (var i=0; i<coll.length; i++) { var thisClass = coll[i].className; if ( (coll[i].getAttribute('href') || coll[i].getAttribute('cite')) && (thisClass == '' || thisClass.indexOf('ignore') == -1)) { thisLink = coll[i].getAttribute('href') ? coll[i].href : coll[i].cite; if (thisLink.length > 110) { thisLink = thisLink.substr(0, 110) + "..."}; var note = document.createElement('sup'); addClass.apply(note,['printOnly']); var note_txt; var j = inArray.apply(myArr,[thisLink]); if ( j || j===0 ) { note_txt = document.createTextNode(j+1);} else { var li = document.createElement('li'); var li_txt = document.createTextNode(thisLink); li.appendChild(li_txt); ol.appendChild(li); myArr.push(thisLink); note_txt = document.createTextNode(num); num++;} note.appendChild(note_txt); if (coll[i].tagName.toLowerCase() == 'blockquote') { var lastChild = lastChildContainingText.apply(coll[i]); lastChild.appendChild(note);} else { coll[i].parentNode.insertBefore(note, coll[i].nextSibling);} } } target.appendChild(h2); target.appendChild(ol); addClass.apply(document.getElementsByTagName('html')[0],['noted']); return true;}

// Runs all the listed functions on the loading of the window

window.onload=function(){
	doPopups();
	footnoteLinks("contentContainer","footnotes");
}