function init_worksheet() {
	if (!document.getElementById('worksheet-wrap')) return;
	var menu = document.getElementById('worksheet-menu');
	var menu_items = menu.getElementsByTagName('a');
	
	for (var i=0; i < menu_items.length; i++) {
		if (menu_items[i].getAttribute('rel')) {
			menu_items[i].onmouseover = function() { highlight(this); };
			menu_items[i].onmouseout = function() { removeHighlight(this); };
		}
		menu_items[i].onclick = function() { return false; };
	};
	var question_div = document.getElementById('worksheet-questions');
	var questions = document.getElementsByTagName('div');
	
	for (var i=0; i < questions.length; i++) {
		if (questions[i].getAttribute('rel')) {
			questions[i].onmouseover = function() { highlight(this); };
			questions[i].onmouseout = function() { removeHighlight(this); };
		}
	};

    document.patientToolsForm.elements[3].onchange = function() {
       if (this.value) {
           document.body.className = ' ' + this.value.slice(0,-3).toLowerCase(); 
       } else {
           document.body.className = document.body.className.replace(/breast|gastric|headneck|lung|prostate/,''); 
       }
       //Joey uncheck checkboxes for cancer types tips and quotes that were not selected
       correctCancerTypeTipsQuotes();
    };

    if (document.patientToolsForm.elements[3].value) {
           document.body.className = ' ' + document.patientToolsForm.elements[3].value.slice(0,-3).toLowerCase(); 
    }

    //Joey uncheck checkboxes for cancer types tips and quotes that were not selected
       correctCancerTypeTipsQuotes();
       checkIfPopupPdf();
}

        var submitCtr = 0;
        function dblClickBlock(){
          if (submitCtr == 0){
            submitCtr++;
            return true;
          } else {
            alert('Your submission is being processed.');
            return false;
          }
        }  

function correctCancerTypeTipsQuotes(){
    var cancerNames =new Array('breast', 'lung', 'prostate', 'headneck', 'gastric');
    var cancerSelected = document.patientToolsForm.cancerType.value.split("|")[0];
    for (var k=0; k < 5; k++){
        if (cancerNames[k] != cancerSelected){
		    uncheckTipsQuotes(cancerNames[k]+'T' );//tips
            uncheckTipsQuotes(cancerNames[k]+'Q' );//quotes            
        }
     }
}

function uncheckTipsQuotes(cancerName){
        var chkboxElements = document.getElementById(cancerName);
        var inputTypes = chkboxElements.getElementsByTagName('input');
        for( var j=0; j < inputTypes.length; j++){
            inputTypes[j].checked = false;
        }
}

function highlight(evtTarget) {
	var id = evtTarget.getAttribute('rel');
	if (evtTarget.nodeName == 'DIV') {
		var relatedEl = document.getElementById(id).firstChild;
	} else {
		var relatedEl = document.getElementById(id);
	}
	addClass(evtTarget,'active');
    if (relatedEl) {
        addClass(relatedEl,'active');
    }
};

function removeHighlight(evtTarget) {
	var id = evtTarget.getAttribute('rel');
	if (evtTarget.nodeName == 'DIV') {
		var relatedEl = document.getElementById(id).firstChild;
	} else {
		var relatedEl = document.getElementById(id);
	}
	removeClass(evtTarget,'active');
    if (relatedEl) {
        removeClass(relatedEl,'active');
    }
};

function addClass(obj,name) {
	obj.className += ' ' + name;
};

function removeClass(obj,name) {
	obj.className = obj.className.replace(name,'');
};

DKI.addEventSimple(window,'load',init_worksheet);

   var http_request = false;
   
   function makeRequest(url, nameOfFormToPost) {
	   //convert the url to a string
      url=url+getFormAsString(nameOfFormToPost);
      
      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/html');
            http_request.overrideMimeType('application/PDF');
         }
      } 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;
      }
      alert(url);
      http_request.onreadystatechange = alertContents;
      http_request.open('POST', url , true);
      http_request.send(null);
   }
  
function getFormAsString(formName){
        
  //Setup the return String
  returnString ="?test=";
        
  //Get the form values
  formElements=document.forms[formName].elements;
        
  //loop through the array, building up the url
  //in the format '/strutsaction.do&name=value'
 
  for(var i=formElements.length-1;i>=0; --i ){
        //we escape (encode) each value
        if (formElements[i].type == "checkbox") {
           if (formElements[i].checked) {
              returnString +=formElements[i].name + "=" +formElements[i].value + "&";
           } else {
              //returnString += formElements[i].name + "=&";
           }
        }else{
	        returnString+="&" 
	        +escape(formElements[i].name)+"=" 
	        +escape(formElements[i].value);
        }
 }
        
 //return the values
 return returnString; 
}

  
   function alertContents() {
    //alert(http_request.status);
      if (http_request.readyState == 4) {
         if (http_request.status == 200) {
            //alert(http_request.responseText);
            result = http_request.responseText;
            //document.getElementById('myspan').innerHTML = result;            
             win3 = window.open("", "Window2", "width=310,height=600,scrollbars=yes");
             win3.document.writeln(result);
         } else {
            alert('There was a problem with the request.');
         }
      }
/*	  if (http_request.readyState == 4) { // Complete
	    if (http_request.status == 200) { // OK response
	        
	    //Split the text response into Span elements
	    spanElements = 
	        splitTextIntoSpan(http_request.responseText);
	    
	    //Use these span elements to update the page
	    replaceExistingWithNewHtml(spanElements);
	    
	    } else {
	      alert("Problem with server response:\n " 
	        + http_request.status);
	    }
	  }      */
   }
   
   function get(formName) {
	  //Setup the return String
	  returnString ="";
	        
	  //Get the form values
	  formElements=document.forms[formName].elements;
	        
	  //loop through the array, building up the url
	  //in the format '/strutsaction.do&name=value'
	 
	  for(var i=formElements.length-1;i>=0; --i ){
	        //we escape (encode) each value
	        returnString+="&" 
	        +escape(formElements[i].name)+"=" 
	        +escape(formElements[i].value);
	 }
	        
	 //return the values
     alert('2');
     alert(returnString);
	 //return makeRequest('web_content_patientTools.do', returnString);;    

/*      var getstr = "?";
      for (i=0; i<obj.childNodes.length; i++) {
         if (obj.childNodes[i].tagName == "INPUT") {
            if (obj.childNodes[i].type == "text") {
               getstr += obj.childNodes[i].name + "=" + obj.childNodes[i].value + "&";
            }
            if (obj.childNodes[i].type == "checkbox") {
               if (obj.childNodes[i].checked) {
                  getstr += obj.childNodes[i].name + "=" + obj.childNodes[i].value + "&";
               } else {
                  getstr += obj.childNodes[i].name + "=&";
               }
            }
            if (obj.childNodes[i].type == "radio") {
               if (obj.childNodes[i].checked) {
                  getstr += obj.childNodes[i].name + "=" + obj.childNodes[i].value + "&";
               }
            }
         }   
         if (obj.childNodes[i].tagName == "SELECT") {
            var sel = obj.childNodes[i];
            getstr += sel.name + "=" + sel.options[sel.selectedIndex].value + "&";
         }
         
      }
      alert(getstr);
      makeRequest('web_content_patientTools.do', getstr);
      */
   }
   function splitTextIntoSpan(textToSplit){
 
  //Split the document
  returnElements=textToSplit. 
            split("</span>")
        
  //Process each of the elements        
  for(var i=returnElements.length-1;i>=0;--i){
                
    //Remove everything before the 1st span
    spanPos = returnElements[i]. 
             indexOf("<span");               
                
    //if we find a match, take out 
    //everything before the span
    if(spanPos>0){
          subString=returnElements[i].
              substring(spanPos);
          returnElements[i]=subString;
    } 
  }
  return returnElements;
}

function replaceExistingWithNewHtml 
        (newTextElements){
 
  //loop through newTextElements
  for(var i=newTextElements.length-1;i>=0;--i){
  
    //check that this begins with <span
    if(newTextElements[i]. 
        indexOf("<span")>-1){
                        
          //get the span name - sits
      // between the 1st and 2nd quote mark
      //Make sure your spans are in the format
      //<span id="someName">NewContent</span>
          startNamePos=newTextElements[i]. 
              indexOf('"')+1;
      endNamePos=newTextElements[i]. 
              indexOf('"',startNamePos);
      name=newTextElements[i]. 
              substring(startNamePos,endNamePos);
                        
      //get the content - everything 
      // after the first > mark
      startContentPos=newTextElements[i]. 
               indexOf('>')+1; 
      content=newTextElements[i].
               substring(startContentPos);
                        
     //Now update the existing Document 
     // with this element, checking that 
     // this element exists in the document
     if(document.getElementById(name)){
                document.getElementById(name). 
                innerHTML = content;
     }
  }
}
}
  