// handles multiple onload events
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

function updateFinalStatus(formName)
{
	var forms = document.getElementsByTagName('FORM');
	
	// loop through all the forms
	for (var j=0; j < forms.length; j++){
		if (forms[j].name == formName)
		{
			// get all the input elements in the form
			var checkboxes = forms[j].getElementsByTagName('INPUT');
			var problem = false;
			for (var k=0; k < checkboxes.length; k++) {
				if ((checkboxes[k].type == 'checkbox') && (checkboxes[k].checked)) {
					problem = true;
				}
			}
			

			// get status image associated with this form, it has the same name as the form
			var imageForm = document.getElementById('image' + formName);
			
			// get text associated with this form, it has the same name as the form
			var textForm = imageForm.parentNode;
			textForm.style.textDecoration = "line-through";
			if (imageForm != null)
			{
				if (problem) 
				{
					imageForm.src = 'images/error.gif';
					imageForm.alt = 'there is a deficiency';
				}
				else
				{
					imageForm.src = 'images/check2.gif';
					imageForm.alt = 'there is no deficiency';
				}
			}
		}
	}	
}


// handle checkboxes in the forms
function initCheckboxes()
{
	// get a list of all forms in the current document
	var forms = document.getElementsByTagName('FORM');
	
	// loop through all the forms
	for (var j=0; j < forms.length; j++){
		// get the list of input elements in the form
		var inputElements = forms[j].getElementsByTagName('INPUT');
		// loop over all the input elements in the form
		for (var k=0; k < inputElements.length; k++) {
			// if its a checkbox, then change the onclick function to update the status
		}
	}	
}

addLoadEvent(initCheckboxes);


function tablecollapse()
{
	/* Variables */
	var collapseClass='footcollapse';
	var collapsePic='images/arrow_up.gif';
	var expandPic='images/arrow_down.gif';
	var initialCollapse=true;

	// loop through all tables
	var t=document.getElementsByTagName('table');
	var checktest= new RegExp("(^|\\s)" + collapseClass + "(\\s|$)");
	for (var i=0;i<t.length;i++)
	{
		// if the table has the right class
		if(checktest.test(t[i].className))
		{

			// make the caption clickable
			t[i].getElementsByTagName('caption')[0].onclick=function()
			{
				// loop through all bodies of this table and show or hide them
				var tb=this.parentNode.getElementsByTagName('tbody');
				for(var i=0;i<tb.length;i++)
				{
					tb[i].style.display=tb[i].style.display=='none'?'':'none';
				}
				// hide the head too
				var tb=this.parentNode.getElementsByTagName('thead');
				for(var i=0;i<tb.length;i++)
				{
					tb[i].style.display=tb[i].style.display=='none'?'':'none';
				}			
				// hide the foot too
				var tb=this.parentNode.getElementsByTagName('tfoot');
				for(var i=0;i<tb.length;i++)
				{
					tb[i].style.display=tb[i].style.display=='none'?'':'none';
				}			
				// change the image accordingly
				var li=this.getElementsByTagName('img')[0];
				li.src=li.src.indexOf(collapsePic)==-1?collapsePic:expandPic;
				
				slideContent(1, dhtmlgoodies_slideSpeed);
			}
			// if the bodies should be collapsed initially, do so
			if(initialCollapse)
			{
				var tb=t[i].getElementsByTagName('tbody');
				for(var j=0;j<tb.length;j++)
				{
					tb[j].style.display='none';
				}			
				tb=t[i].getElementsByTagName('thead');
				for(var j=0;j<tb.length;j++)
				{
					tb[j].style.display='none';
				}			
				tb=t[i].getElementsByTagName('tfoot');
				for(var j=0;j<tb.length;j++)
				{
					tb[j].style.display='none';
				}			
			}
			// add the image surrounded by a dummy link to allow keyboard 
			// access to the last cell in the footer
			var tf=t[i].getElementsByTagName('caption')[0];
			var collapseImage = "<a href='#' onclick='function(){return false;}'><img src='";
			collapseImage += initialCollapse ? expandPic:collapsePic;
			collapseImage += "'></a>";
			tf.innerHTML = collapseImage + tf.innerHTML;
			tf.style.width = '100%';
		}
	}		
}
// run tablecollapse when the page loads
addLoadEvent(tablecollapse);

function addEvent(func){
  if (!document.getElementById | !document.getElementsByTagName) return
  var oldonload=window.onload
  if (typeof window.onload != 'function') {window.onload=func}
  else {window.onload=function() {oldonload(); func()}}
}

addEvent(hideAll)

function hideAll(){
  var obj,nextspan,anchor,content;

  // get all spans
  obj=document.getElementsByTagName('span');

  // run through them
  for (var i=0;i<obj.length;i++){

    // if it has a class of helpLink
    //alert(obj[i].className);
    if(/helpLinkHide/.test(obj[i].className) || /helpLink/.test(obj[i].className)){

      // get the adjacent span
      nextspan=obj[i].nextSibling;
      while(nextspan.nodeType!=1) nextspan=nextspan.nextSibling;

       // hide it
      nextspan.style.display='none'
      
      obj[i].className = "helpLink";

      //create a new link
      anchor=document.createElement('a');

      // copy original helpLink text and add attributes
      var contents = new Array();
      content = obj[i].firstChild.nodeValue;
      contents = content.split('|');
      anchor.appendChild(document.createTextNode(contents[0]))
      alternateText = contents[1];
      if(alternateText == null)
        alternateText = contents[0];
      anchor.href='#help';
      anchor.title='Click to show';
      anchor.className="helpLink";
      anchor.nextspan=nextspan;
      anchor.onclick=function(){showHide(this.nextspan);changeTitle(this);changeText(this, contents[0], alternateText);return false};

      // replace span with created link
      obj[i].replaceChild(anchor,obj[i].firstChild);
    }
  }
}


// used to flip helpLink title
function changeTitle(obj){
  if(obj)
    obj.title = obj.title=='Click to show' ? 'Click to hide' : 'Click to show';
}

// used to flip the display property
function showHide(obj){
  if(obj)
    obj.style.display = obj.style.display=='none' ? 'inline' : 'none';
}

function changeText(obj, text1, text2)
{
    if(obj)
    {
        //IE
        if(obj.innerText != null)
        {
            if(obj.innerText == text1)
                obj.innerText = text2;
            else
                obj.innerText = text1;
        }
        else
        {
            if(obj.text == text1)
            {
                newChild = document.createTextNode(text2);
                obj.replaceChild(newChild, obj.firstChild);
            }
            else
            {
                 newChild = document.createTextNode(text1);
                obj.replaceChild(newChild, obj.firstChild);
            }
        }
    }
}



  function ShowDialog(text) {
    window.open(text,"","dialogHeight:200px;dialogWidth:400px;channelmode:0;dependent:0;directories:0;fullscreen:0;location:0;menubar:0;resizable:0;scrollbars:1;status:0;toolbar:0;");
  }

  function NewCentralWindow(mypage) {
    var winl = (screen.width - 600) / 2;
    var wint = (screen.height - 600) / 2;
    winprops = 'height='+600+',width='+600+',top='+wint+',left='+winl+',scrollbars='+"yes"+',resizable'
    win = window.open(mypage, null, winprops)
    if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
    return false;
  }

function doSaveAs(){
     if(navigator.appName == "Microsoft Internet Explorer"){
        if (document.execCommand){
            document.execCommand("SaveAs", true, "result_details.html");
        }
     }
    else {
        alert("Save-feature available only in Internet Exlorer 5.x.");
        //but this requires the browser have 
        //ffFileChooser();
    }
}

 function SetFocus(controlId)
{
    var control = document.getElementById(controlId);
    if(control != null)
    {
        control.focus();
    }
}

addEvent(CheckJavaScriptAvailability);
function CheckJavaScriptAvailability()
{
    // get all spans
   spans = document.getElementsByTagName('span');
   if(spans != null)
   {
        for(var i = 0; i < spans.length; i++)
            CheckJavascript(spans[i]);    
   }
   //get all divs
   divs = document.getElementsByTagName('div');
   if(divs != null)
   {
        for(var i = 0; i < divs.length; i++)
            CheckJavascript(divs[i]);
   }
}

function CheckJavascript(item)
{
    // if it has a class of jsEnabled
    if(/jsEnabled/.test(item.className))
    {
       // show it
      item.style.display = 'inline';
      return 0;
    }
      
       // if it has a class of jsDisabled
    if(/jsDisabled/.test(item.className))
    {
       // hide it
      item.style.display= 'none';
      return 1;
    } 
}
