﻿var thedestinationtodelete = '';
var deletedesttourid = '';
var theidtounassociate = '';
var unassociatetourid = '';
var destinationidtoedit = '';
var usertourid = '';
var MySelectedNode;

//GetSelectNode Modified.
function GetSelectedNode(sender, args){ 
   
  MySelectedNode = args.get_node();  
  var tourid = MySelectedNode.get_value();
  ppnodelevel = MySelectedNode.get_level();
	 
	var nodes = MySelectedNode.get_nodes()
	var nodesCount = nodes.get_count()
	if (MySelectedNode != null && nodesCount > 0){  
		tourid = "";
		for (var i = 0; i < nodesCount; i++){
			var node = nodes.getNode(i);
			// add a childNode
			var SubGroup = node.get_value();
			if(i < nodesCount - 1){
				tourid += SubGroup + "|";
			}else{
				tourid += SubGroup
			}
		}
		
	} 
	
	document.getElementById("HiddenTourID").value = tourid;
	//alert(tourid);
		   
	if (tourid != 'TourDeleted'){
		getdestinations(tourid,'DestinationName', 'self');
	}
			
	
	else  
	{  
		alert("Please Select a Tour!"); 
	}  
	
	activetourname = MySelectedNode.get_text();
	activetourid=tourid;
	
	//alert('Tour name: '+activetourname+'\nTour ID: '+activetourid);
				
}
	
	
function deletedestination(destinationtodelete,thetourid){
	
	deletedesttourid = thetourid;
	thedestinationtodelete = destinationtodelete;
	if(confirm('Are you sure you want to delete this destination?')){
		WebService.deletedestination(thedestinationtodelete,destinationdeleteresults);
	}
	
}

function associatetour(touridtoassociate, usernametoassociate){
	
	if(currentuser){
		var usernameraw=currentdestinations[i].tourowner;
		var username=usernameraw.toLowerCase();
		if(username!=currentuser){
    		WebService.associatetour(touridtoassociate,usernametoassociate,tourassociated);
		}else{
			alert('This tour is already associtated to you.');	
		}
	}else{
		alert('You must be logged on to use this feature.');
	}
    
}

function destinationdeleteresults(retResult){
	getdestinations(deletedesttourid,'DestinationName', 'self');
}


function editdestination(stourid){
	
	WebService.editdestination(savestring,destinationeditresults);
	usertourid = stourid;
	
}

function destinationeditresults(retResult){
	getdestinations(usertourid,'DestinationName', 'self');
}

function gettoursarray(username){
	WebService.gettoursarray(username,tourlist);
}

function tourlist(retResult){
	usertours = retResult;
}

function unassociatedestination(idtounassociate, touridunassociate){
    
        theidtounassociate = idtounassociate;
        unassociatetourid = touridunassociate;
        if(confirm('Are you sure you want to remove this destination?')){
			WebService.unassociatedestination(theidtounassociate,refreshtoursfromunassociate);
		}

}

function refreshtoursfromunassociate(retResult){
	
    getdestinations(unassociatetourid,'DestinationName', 'self');
    removeactivemarker();
    deletedesttourid = '';
    theidtounassociate = '';
	
}   

function tourassociated(retResult){
	if (retResult == '-1'){
		alert('This tour has already been associated to your stuff.');
	}else{
		alert('This tour has been saved to your stuff.');
	}
}







