//////////////////PARSE XML/XSLT ////////////////////////////////

var req;
var styleReq;
var stylesheetDoc;
var dest;

function loadStylesheet(){
  if (window.XMLHttpRequest){
      //url = "http://www.kentlaw.edu/students/organizations.xsl";
      url = "organizations.xsl";
      styleReq = new XMLHttpRequest();
      styleReq.onreadystatechange = processStylesheetChange;
      styleReq.open("GET", url, true);
      styleReq.send(null);
  } else if (window.ActiveXObject) {
        //url = "http://www.kentlaw.edu/students/organizations.xsl";
        url = "organizations.xsl";
        styleReq = new ActiveXObject("Microsoft.XMLHTTP");
        if (styleReq) {
            styleReq.onreadystatechange = processStylesheetChange;
            styleReq.open("GET", url, true);
            styleReq.send();
        }
   }
}
function processStylesheetChange(){
  if (styleReq.readyState == 4){
    if (styleReq.status == 200){
	
       if (window.ActiveXObject) {

           stylesheetDoc = new ActiveXObject("Microsoft.XMLDOM");
           stylesheetDoc.async = false;
           stylesheetDoc.loadXML(styleReq.responseText);
   			//loadHTML('http://www.kentlaw.edu/students/organizations.xml','organizations');
   			loadHTML('organizations.xml','organizations');
			
       } else if (window.XMLHttpRequest){

           var dp = new DOMParser();
           stylesheetDoc = dp.parseFromString(styleReq.responseText, "text/xml");
   			//loadHTML('http://www.kentlaw.edu/students/organizations.xml','organizations');
   			loadHTML('organizations.xml','organizations');

       } 

    } else {
       alert("Can't load stylesheet:"+styleReq.status);
    }
  }
}


function loadHTML(url, destination){
  dest = destination;
  if (window.XMLHttpRequest){
      req = new XMLHttpRequest();
      req.open("GET", url, true);
      req.onreadystatechange = processStateChange;
      req.send(null)
  } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = processStateChange;
            req.open("GET", url, true);
            req.send();
        }
   }
}
function processStateChange(){
  statusDiv = document.getElementById("status");
  if (req.readyState == 0){ statusDiv.innerHTML = "UNINITIALIZED"; }
  if (req.readyState == 1){ statusDiv.innerHTML = "LOADING"; }
  if (req.readyState == 2){ statusDiv.innerHTML = "LOADED"; }
  if (req.readyState == 3){ statusDiv.innerHTML = "INTERACTIVE"; }
  if (req.readyState == 4){    if (req.status == 200){
       response = req.responseText;

         if (window.ActiveXObject) {

              var theDocument = new ActiveXObject("Microsoft.XMLDOM");
              theDocument.async = false;
              theDocument.loadXML(req.responseText);

              destinationDiv = document.getElementById(dest);			
              destinationDiv.innerHTML = theDocument.transformNode(stylesheetDoc);///////////////ERROR HERE.

		  } else if (window.XMLHttpRequest){

              var parser = new DOMParser();
              theDocument = parser.parseFromString(req.responseText, "text/xml");

              var xsltProcessor = new XSLTProcessor();
              xsltProcessor.importStylesheet(stylesheetDoc);
              response = xsltProcessor.transformToFragment(theDocument, document);

              destinationDiv = document.getElementById(dest);
              destinationDiv.innerHTML = "";
              destinationDiv.appendChild(response);

          } 

    } else {
       statusDiv.innerHTML = "Error: Status "+req.status;
    }
  }
}


/////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////

function Toggle(item) {
   obj=document.getElementById(item);
   visible=(obj.style.display!="none")
   item_name=item + '_name';
   obj_name=document.getElementById(item_name);
   if (visible) {
     obj.style.display="none";
     obj.className="desc2";
	 obj_name.className="name2";
	 obj_name.firstChild.src="/assets/images/expand.gif";
   } else {
      obj.style.display="block";
      obj.className="desc1";
	  obj_name.className="name1";
	  obj_name.firstChild.src="/assets/images/collapse.gif";
   }
}
function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
} 

function DisplayAll() {
	var el = getElementsByClass('name2',document,'div');
	var divLength = el.length;
	//alert(el.length);
	for (var x=0;x<divLength;x++) {
	 	el[x].className="name1";
	  	el[x].firstChild.src="/assets/images/collapse.gif";
	}
	var el2 = getElementsByClass('desc2',document,'div');
	divLength = el2.length;
	for (var x=0;x<divLength;x++) {
	 	el2[x].style.display="block";
	 	el2[x].className="desc1";
	}
	//for (var x=0;x<document.getElementsByTagName('input').length;x++) {
	//	if (document.getElementsByTagName('input')[x].type == 'checkbox') {
	// 		document.getElementsByTagName('input')[x].checked = true;
	//  	}
  	//}

	divShowAll=document.getElementById("ShowAll");
	divShowAll2=document.getElementById("ShowAll2");
	msg = "<a class='smallblacklink' href='javascript:HideAll()'>Collapse all</a>";
	divShowAll.innerHTML = msg;
	divShowAll2.innerHTML = msg;
}

function HideAll() {
	var el = getElementsByClass('name1',document,'div');
	var divLength = el.length;
	//alert(el.length);
	for (var x=0;x<divLength;x++) {
	 	el[x].className="name2";
	  	el[x].firstChild.src="/assets/images/expand.gif";
	}
	
	var el2 = getElementsByClass('desc1',document,'div');
	divLength = el2.length;
	for (var x=0;x<divLength;x++) {
	 	el2[x].style.display="none";
	 	el2[x].className="desc2";
	}
	//for (var x=0;x<document.getElementsByTagName('input').length;x++) {
	//	if (document.getElementsByTagName('input')[x].type == 'checkbox') {
	//		document.getElementsByTagName('input')[x].checked = false;
	//  	}
  	//}
	
	divShowAll=document.getElementById("ShowAll");
	divShowAll2=document.getElementById("ShowAll2");
	msg = "<a class='smallblacklink' href='javascript:DisplayAll()'>Expand all</a>";
	divShowAll.innerHTML = msg;
	divShowAll2.innerHTML = msg;
}


////////////////// PRINT FUNCTIONS /////////////////

function printSelected() {
  if (window.XMLHttpRequest){
	  //a = document.styleSheets[2].href;
	  //alert(a);
	//document.styleSheets[1].disabled = false;
	//document.styleSheets[2].disabled = true;
	//IE7 is now XMLHttpRequest
	document.styleSheets[6].href = "organizations_PrintSelected.css";
	document.styleSheets[6].media = "print";
	window.print(); 
  } else if (window.ActiveXObject) {
	document.styleSheets[6].href = "organizations_PrintSelected.css";
	document.styleSheets[6].media = "print";
	window.print(); 
  }
}
function printList() {
  if (window.XMLHttpRequest){
	HideAll(); 
	//document.styleSheets[1].disabled = true;
	//document.styleSheets[2].disabled = false;
	document.styleSheets[6].href = "organizations_PrintList.css";
	document.styleSheets[6].media = "print";
	window.print(); 
  } else if (window.ActiveXObject) {
	 //a = document.styleSheets[6].href;
	 //alert(a);
	HideAll(); 
	document.styleSheets[6].href = "organizations_PrintList.css";
	document.styleSheets[6].media = "print";
	window.print();  
  }
}



