﻿// JScript File
var iLevel;
var sSeries;

function Rebuild(Node,Level){
    iLevel = Level;
    HideSuggestionsButton();
    
    var FamilyTree = document.getElementById("ctl00_cphWizard_FamilyTree");
    var x;
    var sNewValue = "";
    var aFamilyTree = FamilyTree.value.split("|");
    
    if (Node!=''){
        //Set the node value that changed
        aFamilyTree[Level-1]=Node;
        
        //Delete the node values of the nodes after the current node being set
        for (y=Level, z=aFamilyTree.length-1; y<=z; y++){
            aFamilyTree[y]="";
        }
    }
    //Rebuild the Family tree value with Pipes
    for (x=0, l=aFamilyTree.length-1; x<=l; x++){
        sNewValue += aFamilyTree[x] + "|";
    }
    
    FamilyTree.value = Left(sNewValue,sNewValue.length-1);
    
    Ask(Level);
    
    try{
        document.getelementById(document.getElementById("ctl00_cphWizard_Series").value).checked="checked";
    }catch(ex){
    
    }
    
    document.getElementById("ctl00_cphWizard_Series").value="";
    
}


function Ask(Level){

    var FamilyTree = document.getElementById("ctl00_cphWizard_FamilyTree").value;
    var Series = document.getElementById("ctl00_cphWizard_Series").value;
    sSeries = Series

    try{   
        var Request = false;
        try {
            Request = new XMLHttpRequest();
        }catch (trymicrosoft) {
        try{
            Request = new ActiveXObject("Msxml2.XMLHTTP");
         }catch (othermicrosoft) {
        try {
             Request = new ActiveXObject("Microsoft.XMLHTTP");
           } catch (failed) {
             Request = false;
           }  
         }
        }

        if (!Request)
         alert("Error initializing XMLHttpRequest!");
        var url = "workerQuestions.aspx?FT=" + FamilyTree + "&L=" + Level + "&S=" + Series;
        Request.open("GET", url, true);
        openRequest = Request;
        Request.onreadystatechange = Update;
        Request.send(null);
        
    }catch (ex){
            document.getElementById('questions').innerHTML=ex.toString();
    }
    
    
}

function Update() {
    if (openRequest.readyState == 4) {
        if (openRequest.status == 200) {   
            document.getElementById('questions').innerHTML=openRequest.responseText;
            document.getElementById("Question" + iLevel).scrollIntoView();
            if (sSeries!=""){ //Scroll to bottom and show the suggestion button
                ShowSuggestionButton();
                scrollToBottom();
            }
        }
    }
}

function ShowSuggestionButton(){
    document.getElementById("viewSuggestionsButton").className="viewSuggestionsButton";
    scrollToBottom();
}
function HideSuggestionsButton(){
    document.getElementById("viewSuggestionsButton").className="none";
}

function scrollToBottom(){
    if (window.pageYOffset){
        for (var x=window.pageYOffset;x<=5000; x=x+1){
            window.scrollTo(0,x);
        }
    }else{
        document.getElementById("ctl00_cphWizard_btnSubmit").scrollIntoView();
    }
    //setTimeout('window.scrollTo(0,window.outerHeight)',1);
}
function Left(str, n){
	if (n <= 0)
	    return "";
	else if (n > String(str).length)
	    return str;
	else
	    return String(str).substring(0,n);
}

