var myLocation;
var myNode;
var myBack, myNext;
var myBranch;
var branchCount;
var totalPages = 0;
var banchesAreCompleted = false;

function getMyBranch(branchnumber)
{
    if (branchnumber.indexOf(/branch/g) == 0 || branchnumber.indexOf(/Branch/g) == 0) {
		var jump_to_branch = "branch"+branchnumber;
	} else {
		var jump_to_branch = branchnumber;
	}
    
    for(var i=0; i < myNode.getElementsByTagName("branches")[0].childNodes.length; i++) 
	{
		
        if (myNode.getElementsByTagName("branches")[0].childNodes[i].nodeName == jump_to_branch) 
		{
            
            window.location = myNode.getElementsByTagName("branches")[0].childNodes[i].getAttribute("url");
            break;
        }
    }
}


function getFileName(){
    var file_name = document.location.href;
    var end = (file_name.indexOf("?") == -1) ? file_name.length : file_name.indexOf("?");
    myLocation = file_name.substring(file_name.lastIndexOf("/")+1, end);
}

getFileName();

var x = window.parent.topEmpty.xmlDoc.documentElement.getElementsByTagName("page");
//alert(myLocation);

for (var i=0; i<x.length; i++)
{
    if(x[i].getAttribute("url") == myLocation)
	{
		//alert(x[i].attributes[1].nodeValue +" - " +x[i].attributes[4].nodeValue + " - " + x[i].attributes[2].nodeValue);
        myNode = x[i];
        pageNumber = x[i].getAttribute("pagenumber");
        sectionName = x[i].getAttribute("section");
		totalPages = x[i].getAttribute("totalpages");
        break;
    }
}


if (myNode.childNodes.length == 5)
	branchCount = myNode.childNodes[4].childNodes.length;
else
	branchCount = 0;

/*for (var i=0; i<x.length; i++){
    if(x[i].getAttribute("section")==sectionName){
        totalPages++;
    }
}*/

myBack = myNode.getElementsByTagName("back_page")[0].getAttribute("url");
myNext = myNode.getElementsByTagName("next_page")[0].getAttribute("url");

var y = myNode.getElementsByTagName("visited")[0].attributes[0];
y.nodeValue = "1";

function branchAlert() {

    var myBranchesVisited = 0;

    if (myNode.childNodes.length == 5) 
	{
		branchCount = myNode.childNodes[4].childNodes.length;
        for (var i = 0; i < myNode.childNodes[4].childNodes.length; i++) 
		{
            for (var ii = 0; ii < x.length; ii++) 
			{
                if (x[ii].getAttribute("url") == myNode.childNodes[4].childNodes[i].getAttribute("url")) 
				{

                    if (window.document.flashmovie)
                        window.document.flashmovie.SetVariable(("BrchVisit" + (1 + i)), x[ii].childNodes[3].getAttributeNode("value").nodeValue);


                    if (x[ii].childNodes[3].getAttributeNode("value").nodeValue == "1") {
                        myBranchesVisited++;
                    }
                }
            }

        }
        //alert(myBranchesVisited + " - " + myNode.childNodes[4].childNodes.length);
        if (myBranchesVisited == myNode.childNodes[4].childNodes.length) {
            banchesAreCompleted = true;
            document.location = myNext;
        } else {
            x = confirm("You have not visted all branching pages.\nClick OK to continue or Cancel to stay.");
            if (x) { if (myNext != "") { document.location = myNext; } } else { return false; }
        }
    }
	else
	{
		branchCount = 0;
	}
	
    if (window.document.flashmovie) {
        window.document.flashmovie.SetVariable("BranchesLoaded", "true");
        window.document.flashmovie.mainFunc;
    }
}

runOnce = false;

function checkVisited(){
	
	if (!runOnce) {
		
		var myBranchesVisited = 0;
		runOnce=true;
		
		elementNodes = 0;
		for (e=0; e< myNode.childNodes.length; e++) {	
			if (myNode.childNodes[e].nodeType == 1) {
			elementNodes++;
			}
		}
	
		if (elementNodes == 5){
			
				branchNodeLength = 0;
				branchPageArray = new Array();
				
				for (e=0; e< myNode.getElementsByTagName("branches")[0].childNodes.length; e++) {	
					if (myNode.getElementsByTagName("branches")[0].childNodes[e].nodeType == 1) {
						branchPageArray[branchNodeLength] = myNode.getElementsByTagName("branches")[0].childNodes[e].getAttribute("url");
						branchNodeLength++;				
					}
					
				}
				
				for (var i=0; i<branchNodeLength; i++){
					
					for (var e=0; e<x.length; e++){            
						
						if(x[e].getAttribute("url")==branchPageArray[i]){
							
							if (window.document.flashmovie) {
								window.document.flashmovie.SetVariable(("BrchVisit" + (1+i)), x[e].getElementsByTagName("visited")[0].getAttribute("value"));              
							}
							
							if (x[e].getElementsByTagName("visited")[0].getAttribute("value") == "1") {
								myBranchesVisited++;
							}
						}
					}
					
				}
				
			if(myBranchesVisited == branchNodeLength){		
				banchesAreCompleted = true;           
			} 
		}
		
		if (window.document.flashmovie) {
		 window.document.flashmovie.SetVariable("BranchesLoaded","true");
		 window.document.flashmovie.mainFunc;
		 }
	}
	
}

function BranchVisited() {
    //if (window.document.flashmovie) 
	//{
        checkVisited();
    //}
}

function Branch(branch_nr)
{
	alert("Go to branch " + branch_nr);
	var moveToBranch = "";
	getMyBranch(branch_nr);
}
