/**************************************************************************
	Copyright (c) 2001 Geir Landrö (drop@destroydrop.com)
	JavaScript Tree - www.destroydrop.com/hugi/javascript/tree/
	Version 0.96	

	This script can be used freely as long as all copyright messages are
	intact.
**************************************************************************/

// Arrays for nodes and icons
var nodes		= new Array();;
var openNodes	= new Array();
var icons		= new Array(6);

// Loads all icons that are used in the tree
function preloadIcons() {
	icons[0] = new Image();
	icons[0].src = "tree/img/plus.png";
	icons[1] = new Image();
	icons[1].src = "tree/img/plusbottom.png";
	icons[2] = new Image();
	icons[2].src = "tree/img/minus.png";
	icons[3] = new Image();
	icons[3].src = "tree/img/minusbottom.png";
	icons[4] = new Image();
	icons[4].src = "tree/img/folder.png";
	icons[5] = new Image();
	icons[5].src = "tree/img/folderopen.png";
}
// Create the tree
function createTree(arrName, startNode, openNode) {
	nodes = arrName;
	if (nodes.length > 0) {
		preloadIcons();
		if (startNode == null) startNode = 0;
		if (openNode != 0 || openNode != null) setOpenNodes(openNode);

		if (startNode !=0) {
			var nodeValues = nodes[getArrayId(startNode)].split("|");
			document.write("<a href=\"" + nodeValues[3] + "\" onmouseover=\"window.status='" + nodeValues[2] + "';return true;\" onmouseout=\"window.status=' ';return true;\"><img src=\"tree/img/folderopen.png\" align=\"absbottom\" alt=\"\" />" + nodeValues[2] + "</a><br />");
		} else document.write("<img src=\"tree/img/base.png\" align=\"absbottom\" alt=\"\" /><br />");

		var recursedNodes = new Array();
		addNode(startNode, recursedNodes);
	}
}
// Returns the position of a node in the array
function getArrayId(node) {
	for (i=0; i<nodes.length; i++) {
		var nodeValues = nodes[i].split("|");
		if (nodeValues[0]==node) return i;
	}
}
// Puts in array nodes that will be open
function setOpenNodes(openNode) {
	for (i=0; i<nodes.length; i++) {
		var nodeValues = nodes[i].split("|");
		if (nodeValues[0]==openNode) {
			openNodes.push(nodeValues[0]);
			setOpenNodes(nodeValues[1]);
		}
	}
}
// Checks if a node is open
function isNodeOpen(node) {
	for (i=0; i<openNodes.length; i++)
		if (openNodes[i]==node) return true;
	return false;
}
// Checks if a node has any children
function hasChildNode(parentNode) {
	for (i=0; i< nodes.length; i++) {
		var nodeValues = nodes[i].split("|");
		if (nodeValues[1] == parentNode) return true;
	}
	return false;
}
// Checks if a node is the last sibling
function lastSibling (node, parentNode) {
	var lastChild = 0;
	for (i=0; i< nodes.length; i++) {
		var nodeValues = nodes[i].split("|");
		if (nodeValues[1] == parentNode)
			lastChild = nodeValues[0];
	}
	if (lastChild==node) return true;
	return false;
}
// Adds a new node in the tree
function addNode(parentNode, recursedNodes) {
    
	for (var i = 0; i < nodes.length; i++) {

		var nodeValues = nodes[i].split("|");
		if (nodeValues[1] == parentNode) {
			document.write("<table border=0 cellpadding=0 cellborder=0><tr><td>");
			var LastSibling	= lastSibling(nodeValues[0], nodeValues[1]);
			var HasChildNode = hasChildNode(nodeValues[0]);
			var ino = isNodeOpen(nodeValues[0]);

			// Write out line & empty icons
			for (g=0; g<recursedNodes.length; g++) {
				if (recursedNodes[g] == 1) document.write("<img src=\"tree/img/line.png\" align=\"absbottom\" alt=\"\" />");
				else  document.write("<img src=\"tree/img/empty.png\" align=\"absbottom\" alt=\"\" />");
			}

			// put in array line & empty icons
			if (LastSibling) recursedNodes.push(0);
			else recursedNodes.push(1);

			// Write out join icons
			if (HasChildNode) { //has children
				if (LastSibling) { //Last Sibling
					document.write("<a href=\"javascript: oc(" + nodeValues[0] + ", 1);\"><img id=\"join" + nodeValues[0] + "\" src=\"tree/img/");
					 	if (ino) document.write("minus");
						else document.write("plus");
					document.write("bottom.png\" align=\"absbottom\" alt=\"Open/Close node\" /></a>");
				} else {
					document.write("<a href=\"javascript: oc(" + nodeValues[0] + ", 0);\"><img id=\"join" + nodeValues[0] + "\" src=\"tree/img/");
						if (ino) document.write("minus");
						else document.write("plus");
					document.write(".png\" align=\"absbottom\" alt=\"Open/Close node\" /></a>");
				}
			} else { // no children
				if (LastSibling) {        //Last Sibling
          		             document.write("<img src=\"tree/img/join.png\" align=\"absbottom\" alt=\"\" />");
          		             document.write("<img src=\"tree/img/join.png\" align=\"absbottom\" alt=\"\" />");
          		             document.write("<img src=\"tree/img/join.png\" align=\"absbottom\" alt=\"\" />");
          		             document.write("<img src=\"tree/img/join.png\" align=\"absbottom\" alt=\"\" />");
          		             document.write("<img src=\"tree/img/join.png\" align=\"absbottom\" alt=\"\" />");
          		             document.write("<img src=\"tree/img/join.png\" align=\"absbottom\" alt=\"\" />");
                                }
				else {
				     document.write("<img src=\"tree/img/joinbottom.png\" align=\"absbottom\" alt=\"\" />");
				     document.write("<img src=\"tree/img/joinbottom.png\" align=\"absbottom\" alt=\"\" />");
				     document.write("<img src=\"tree/img/joinbottom.png\" align=\"absbottom\" alt=\"\" />");
				     document.write("<img src=\"tree/img/joinbottom.png\" align=\"absbottom\" alt=\"\" />");
				     document.write("<img src=\"tree/img/joinbottom.png\" align=\"absbottom\" alt=\"\" />");
     				     document.write("<img src=\"tree/img/joinbottom.png\" align=\"absbottom\" alt=\"\" />");

				}
			}

			// Start link
			//document.write("<a href=\"" + nodeValues[3] + "\" onmouseover=\"window.status='" + nodeValues[2] + "';return true;\" onmouseout=\"window.status=' ';return true;\">");
			
			// Write out folder & page icons
			if (HasChildNode) { //has children
				document.write("<img id=\"icon" + nodeValues[0] + "\" src=\"tree/img/folder") //
					if (ino) document.write("open");
				document.write(".png\" align=\"absbottom\" alt=\"Folder\" />"); //
			} else document.write("<img id=\"icon" + nodeValues[0] + "\" src=\"tree/img/page.png\" align=\"absbottom\" alt=\"Page\" />");

			// Write out node name
			document.write( " </td><td id=\"Answer" + nodeValues[0] + "\" width=110>"
            + "<INPUT VALUE=1 name=Answer" + nodeValues[0] + " type=radio onclick=setanswer(" + nodeValues[0] + ",1)>1 "
			+ "<INPUT VALUE=2 name=Answer" + nodeValues[0] + " type=radio onclick=setanswer(" + nodeValues[0] + ",2)>2 "
			+ "<INPUT VALUE=3 name=Answer" + nodeValues[0] + " type=radio onclick=setanswer(" + nodeValues[0] + ",3)>3 "
		//	+ "<INPUT VALUE=4 name=Answer" + nodeValues[0] + " type=radio onclick=setanswer(" + nodeValues[0] + ",4)>4 "
		//	+ "<INPUT VALUE=5 name=Answer" + nodeValues[0] + " type=radio onclick=setanswer(" + nodeValues[0] + ",5)>5 "
            + "</td><td ALIGN=center id=AnswerText" + nodeValues[0] + " width=110  style=\"display: ;\"><INPUT id=AnswerTextValue" + nodeValues[0] + "  VALUE='" + nodeValues[4] + "'  SIZE=3 name=AnswerText" + nodeValues[0] + " type=text></td><td>"
			+ nodeValues[2] + "</td></tr></table>");

			// End link
			//document.write("<br />");

			// If node has children write out divs and go deeper
			if (HasChildNode) {
				document.write("<div id=\"div" + nodeValues[0] + "\"");
					if (!ino) document.write(" style=\"display: none;\"");
				document.write(">");
				addNode(nodeValues[0], recursedNodes);
				document.write("</div>");
			}

			// remove last line or empty icon
			recursedNodes.pop();
		}
	}
}

// Sets one answer
function setanswer(node, answervalue) {
    var TextBoxObject = getElement("AnswerTextValue" + node);
    TextBoxObject.value = answervalue;
}

function getElement(psID) {
    if(document.all) {
        return document.all[psID];
    } else {
        return document.getElementById(psID);
    }
}
// Opens or closes a node
function oc(node, bottom) {
	var theDiv = getElement("div" + node);
	var theJoin = getElement("join" + node);
	var theIcon = getElement("icon" + node);
	var RadioButtons = getElement("Answer" + node);
	var TextBoxs = getElement("AnswerText" + node);

	if (theDiv.style.display == 'none') {
		if (bottom==1) {
	             theJoin.src = icons[3].src;
	             //alert('1')
	             RadioButtons.style.display="none"
	             TextBoxs.style.display=""
		}
		else {
                     theJoin.src = icons[2].src;
                     //alert('2')
                     RadioButtons.style.display="none"
                     TextBoxs.style.display=""
                }
		theIcon.src = icons[5].src;
		theDiv.style.display = '';
	} else {
		if (bottom==1) {
                 	theJoin.src = icons[1].src;
                 	//alert('3')
                 	RadioButtons.style.display=""
                 	TextBoxs.style.display=""
		}
		else {
		     	theJoin.src = icons[0].src;
		     	//alert('4')
		     	RadioButtons.style.display=""
		     	TextBoxs.style.display=""
		}
		theIcon.src = icons[4].src;
		theDiv.style.display = 'none';
	}
}

// Push and pop not implemented in IE(crap!    don´t know about NS though)
if(!Array.prototype.push) {
	function array_push() {
		for(var i=0;i<arguments.length;i++)
			this[this.length]=arguments[i];
		return this.length;
	}
	Array.prototype.push = array_push;
}
if(!Array.prototype.pop) {
	function array_pop(){
		lastElement = this[this.length-1];
		this.length = Math.max(this.length-1,0);
		return lastElement;
	}
	Array.prototype.pop = array_pop;
}

function mySubmit(ShowIDValue) {

    var OneEmpty = 0;

    var numForms = document.forms.length

    for (var f = 0; f < numForms; f++) {
        var formElements = document.forms[f].elements
        var numElements = formElements.length
        //alert (numElements);
        for (var e = 0; e < numElements; e++) {
            var el = formElements[e]
            // alert(el.name);
            // alert(el.value);
            //alert (el.name.substr(0,6));
            if (el.name.substr(0,6) == "Answer") {
                //alert (el.name);
                //alert (el.value);
                if (el.value == "" ) {
                    //alert (el.name);
                    //alert (el.value);
                    OneEmpty = 1;
                }
           }
        }
    }
    //alert (OneEmpty);
    if (OneEmpty == 1) {
        if (confirm("Några punkter är obesvarade, vill du säkert lämna sidan? ")) {
            //alert ("YES");
        }
        else
        {
            //alert ("false");
            return false;
        }
    }
    if (ShowIDValue == "Report") {

        var TextBoxObject = getElement("Report");
        TextBoxObject.value = "Report";
//        alert ("Testing check 133");
    }
    else
    {
        var TextBoxObject = getElement("ShowID");
        TextBoxObject.value = ShowIDValue;
        var TextBoxObject = getElement("Report");
        TextBoxObject.value = 0;
    }

    //alert(ShowIDValue);
    return true;
}

function BrowseSubmit(ShowIDValue) {

    if (ShowIDValue == "Report") {
        var TextBoxObject = getElement("ShowID");
        TextBoxObject.value = 0;
        var TextBoxObject = getElement("Report");
        TextBoxObject.value = ShowIDValue;
    }
    else
    {
        var TextBoxObject = getElement("ShowID");
        TextBoxObject.value = ShowIDValue;
        var TextBoxObject = getElement("Report");
        TextBoxObject.value = 0;
    }

    //alert(ShowIDValue);
    return true;
}

function SetField(FieldName, SetValue) {

    //alert(FieldName);
    //alert(SetValue);
    var TextBoxObject = getElement(FieldName);
    TextBoxObject.value = SetValue;
    //alert(FieldName);
    return true;
}

function myReset() {
    alert('Resetting');
}


// Sets average
function setAverage(node, parentnode, answervalue) {

    if (getElement) {
   //     alert ("GetElement");
    } else if (document.all) {
   //     alert ("ALL");
    } else if (document.layers) {
   //     alert ("layers");
    }



    var numForms = document.forms.length
    for (var f = 0; f < numForms; f++) {
    var formElements = document.forms[f].elements
    var numElements = formElements.length
    for (var e = 0; e < numElements; e++) {
        var el = formElements[e]
       // alert(el.name);
       // alert(el.value);
    }
}


    var TextBoxObject = getElement("AnswerTextValue" + node);
    TextBoxObject.value = answervalue;

    var TextBoxObjectHidden = getElement("AnswerHidden" + node);
    TextBoxObjectHidden.value = answervalue;

    var ChildrenList = document.getElementsByName("ChildOf" + parentnode);

    var ChildrenSum = 0;
    var ChildrenEvaluatedCount = 0;

    for(var i=0;i<ChildrenList.length;i++) {
        //alert(ChildrenSum);
        if (ChildrenList[i].value) {
            //alert(ChildrenList[i].value);
            ChildrenSum = Number(ChildrenSum) + Number(ChildrenList[i].value);
            ChildrenEvaluatedCount = Number(ChildrenEvaluatedCount) + 1;
        }
    }

    var AverageValue = String(ChildrenSum/ChildrenEvaluatedCount);

    var ParentObject = getElement("AnswerTextValue" + parentnode); //To se the average to parent
    if (ParentObject) { //Not necessary if there is no parent object
        ParentObject.value = AverageValue.substring(0,4);

        var ParentObjectHidden = getElement("AnswerHidden" + parentnode);
        ParentObjectHidden.value = AverageValue.substring(0,4);
       
        //alert (parentnode);
        //alert(ParentObject.name.substring(7));
        //alert(AverageValue.substring(0,4));
        if (ParentObject.name.substring(7) != 0) {
            setAverage(parentnode, Number(ParentObject.name.substring(7)), AverageValue.substring(0,4))
        }
    }
}


function CheckSelections() {

    var OneChecked = 0;

    var numForms = document.forms.length

    for (var f = 0; f < numForms; f++) {
        var formElements = document.forms[f].elements
        var numElements = formElements.length
        //alert (numElements);
        for (var e = 0; e < numElements; e++) {
            var el = formElements[e]
            // alert(el.name);
            // alert(el.value);
            //alert (el.name.substr(0,6));
            if (el.name.substr(0,6) == "Select") {
               // alert (el.name);
               // alert (el.value);
               // alert (el.checked);
                if (el.checked == true ) {
                    //alert (el.name);
                    //alert (el.value);
                    OneChecked = 1;
                }
           }
        }
    }
    //alert (OneEmpty);
    if (OneChecked == 0) {
            alert ("Du har inte valt någon examensdel. Du bör välja minst en del innan du kan gå vidare. ");
            return false;
    }

    return true;
}



function CheckReports() {

    var OneChecked = 0;

    var numForms = document.forms.length

    for (var f = 0; f < numForms; f++) {
        var formElements = document.forms[f].elements
        var numElements = formElements.length
        //alert (numElements);
        for (var e = 0; e < numElements; e++) {
            var el = formElements[e]
            // alert(el.name);
            // alert(el.value);
            //alert (el.name.substr(0,6));
            if (el.name == "SubGraphs") {
               // alert (el.name);
               // alert (el.value);
               // alert (el.checked);
                if (el.checked == true ) {
                    //alert (el.name);
                    //alert (el.value);
                    OneChecked = 1;
                }
           }
           if (el.name == "TextReport") {
               // alert (el.name);
               // alert (el.value);
               // alert (el.checked);
                if (el.checked == true ) {
                    //alert (el.name);
                    //alert (el.value);
                    OneChecked = 1;
                }
           }
           if (el.name == "MainGraph") {
               // alert (el.name);
               // alert (el.value);
               // alert (el.checked);
                if (el.checked == true ) {
                    //alert (el.name);
                    //alert (el.value);
                    OneChecked = 1;
                }
           }
        }
    }
    //alert (OneEmpty);
    if (OneChecked == 0) {
            alert ("Du har inte valt någon rapport. Du bör välja minst en rapport innan du kan gå vidare.");
            return false;
    }

    return true;
}

