var keys = [];
var names = [];
var curselect = [];
var curnames = [];
var lastkey = "";
function branchSelect ( tree_key, field_key, branch_page, branch_id,branchName,requestFile){ 

    var getDataobj = new sack();
    var req = 
   	getDataobj.requestFile = requestFile;             
    getDataobj.setVar('tree_key', tree_key);
    getDataobj.setVar('key', branch_id);
    getDataobj.setVar('p', branch_page);
    getDataobj.setVar('branch_page', branch_page);
    getDataobj.setVar('preload_key', '');
    getDataobj.setVar('field_key', field_key);

      
	getDataobj.onCompletion = function(){

                document.getElementById("mainWindowContent").innerHTML = getDataobj.response;
                document.getElementById('covercanvas').style.display = 'none';
                document.getElementById('mainWindowContent').style.display = 'block';

    };
    
	getDataobj.onLoading = function(){
            document.getElementById('covercanvas').style.display = 'block';
            document.getElementById('mainWindowContent').style.display = 'none';
    };	// Action when AJAX is loading the file
    
	getDataobj.runAJAX();		// Execute AJAX function
    
    
    if(lastkey != ""){
        keys.push(lastkey);
        if(typeof(branchName) != "undefined"){
            names.push(branchName);
            refreshSelected();
        }
    }
    
   lastkey = branch_id;
    
    return false;
}

function refreshSelected(){
    document.getElementById('currentSelected').innerHTML = "";
    for(var i = 0; i < names.length;i++){
        document.getElementById('currentSelected').innerHTML += "/" + names[i] ;
    }

}

function branchpop ( tree_key, field_key, branch_page, branch_id,requestFile ){ 
    var getDataobj = new sack();
    var req = 
   	getDataobj.requestFile = requestFile;             
    getDataobj.setVar('tree_key', tree_key);
    getDataobj.setVar('key', branch_id);
    getDataobj.setVar('p', branch_page);
    getDataobj.setVar('branch_page', branch_page);
    getDataobj.setVar('preload_key', '');
    getDataobj.setVar('field_key', field_key);

      
	getDataobj.onCompletion = function(){

                document.getElementById("mainWindowContent").innerHTML = getDataobj.response;
                document.getElementById('covercanvas').style.display = 'none';
                document.getElementById('mainWindowContent').style.display = 'block';

    };
    
	getDataobj.onLoading = function(){
                        document.getElementById('covercanvas').style.display = 'block';
            document.getElementById('mainWindowContent').style.display = 'none';
    };	// Action when AJAX is loading the file
    
    
    lastkey = branch_id;
	getDataobj.runAJAX();		// Execute AJAX function
    
    return false;
}

function SelectCurrent ( tree_key, field_key, branch_page, branch_id,branchName){ 

    curselect = [];
    curnames = [];
    
    for(var i = 0; i < keys.length;i++){
        curselect.push(keys[i]);
        curnames.push(names[i]);
    }
    
   curselect.push(branch_id);
   curnames.push(branchName);
   
    document.getElementById('currentSelected').innerHTML = "";
    for(var i = 0; i < curnames.length;i++){
        document.getElementById('currentSelected').innerHTML += "/" + curnames[i] ;
    }

        document.getElementById('mainfeedback').innerHTML = "You have selected the following location:<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;<b>" + curnames[curnames.length -1] +"</b>";

    
   
    return false;
}

function backclick(requestFile){
if(keys.length > 0){
    branchpop('main', 'link_id_fk', 'include_tree_branch_testbrow',keys.pop(),requestFile);
    names.pop();
    refreshSelected();
}

}

function reset(){

keys = [];
lastkey = "";

}

function showBrowser(){

    
        document.getElementById('CatbrowserCan').style.width = document.body.offsetWidth*1 + "px";
        document.getElementById('CatbrowserCan').style.height = document.body.offsetHeight*1 + "px";

       if(document.body.clientWidth){
        
        
            var brtop = "0px";
            
            if(typeof(scrollY) != "undefined"){
                brtop = ((screen.height / 2) - (418) + scrollY) + "px";
            }else{
                brtop = ((screen.height / 2) - (418));
                window.scrollTo(0,brtop - 50);
            }
            brtop = "100px";
            var brleft = (document.body.clientWidth / 2 - (640 / 2)) + "px";
            document.getElementById('CatbrowserWin').style.top = brtop;//(document.body.clientHeight / 2 - (468 / 2));
            document.getElementById('CatbrowserWin').style.left =  brleft;//(document.body.clientWidth / 2 - (640 / 2));

        }else{
        //crossbrowser
            document.getElementById('CatbrowserWin').style.top = "100px";//(window.innerHeight / 2 - (468)) + "px";
            document.getElementById('CatbrowserWin').style.left = (window.innerWidth / 2 - (640 / 2)) + "px";

        }         
        
        document.getElementById('CatBrowser').className = 'CatBrowser';
        document.getElementById('CatBrowser').style.display = 'block';
    }
    
    function hideBrowser(){

     document.getElementById('CatBrowser').style.display = 'none';

}


function setvalues(divesites){
    if(curselect.length > 0){
    
            if(divesites){
            document.getElementById("link_id_fk").value = curselect[curselect.length - 1].replace('c','');
            }
            else
            {
            document.getElementById("link_id_fk").value = curselect[curselect.length - 1];
            }
            document.getElementById('selectedDisplay').innerHTML = "";
            for(var i = 0; i < curnames.length;i++){
                document.getElementById('selectedDisplay').innerHTML += "/" + curnames[i] ;
            }
        
        }else{
            document.getElementById("link_id_fk").value = "";
            document.getElementById('selectedDisplay').innerHTML = "";;
    
    
        }
}

