    /**************************************************
     *****             AJAX Handlers              *****
     **************************************************/

    /*
      The following [ws-call-name]Success functions are the success 
      handlers for a calls to the [ws-call-name] WS; each takes the 
      response object o. 
     */
    function activateTokenSuccess(o) {
      // Process JSON response.
      var response = eval('(' + o.responseText + ')');

      if (response.spruceFault) {
        // Pass to generic error handler.
        display_error(response.spruceFault);
      } else {
        // The activation was successful, so we reload the
        // token's information.
        return get_token();
      }
    }

    function addUserToTokenSuccess(o) {
      // Process JSON response.
      var response = eval('(' + o.responseText + ')');
      if (response.spruceFault) {
        // Pass to generic error handler.
        display_error(response.spruceFault);
      } else {
        // We successfully added the user to the token, so we
        // reload the token's information. This could be improved by
        // adding the returned user information directly to the list
        // through the DOM.
        return get_token();
      }
    }

    
	function addReservationSuccess(o) {
	 	// Process JSON response.
     	 	var response = eval('(' + o.responseText + ')');
      		if (response.spruceFault) {
      	  	// Pass to generic error handler.
          	display_error(response.spruceFault);
      		} else {
		      // Successful call to addReservation should return the ID.
          	      // so we display this in the announce
                      // field. Note that we do not refresh the token's information.
       			 var error_field = document.getElementById('error_field');
        		error_field.innerHTML = '';
        		var announce_field = document.getElementById('announce_field');
        		var html = '<p><span class="blue">Reservation ID is' 
				+ '<em class="orange"> '
				+ response.reservation_id
                 		+ '</em></span></p> ';

        		announce_field.innerHTML = html;

      		}
    	}


	function checkTokenTimeSuccess(o) {
      // Process JSON response.
      var response = eval('(' + o.responseText + ')');
	if (response.spruceFault) {
	      // Pass to generic error handler.
	      display_error(response.spruceFault);
      } else {
	      // Successful call to checkTokenTime should return the time
        // remaining on the token, so we display this in the announce
        // field. Note that we do not refresh the token's information.
	var error_field = document.getElementById('error_field');
	error_field.innerHTML = '';
	var announce_field = document.getElementById('announce_field');
        var html = '<p><span class="blue">Time remaining (hh:mm:ss):<em class="orange">'
                 + response.time_remaining 
                 + '</em> as of '
		 + Date()
                 + '</span></p> ';

        announce_field.innerHTML = html;
      }
    }

    function removeUserFromTokenSuccess(o) {
      // Process JSON response.
      var response = eval('(' + o.responseText + ')');

      if (response.spruceFault) {
        // Pass to generic error handler.
        display_error(response.spruceFault);
      } else {
        // We have successfully removed the user from the token, so we
        // refresh the token's information. This could remove the user
        // from the DOM instead of refreshing.
        get_token();
      }
    }
   
    function getTokenInfoSuccess(o) {
      // Process JSON response.
	var response = eval('(' + o.responseText + ')');

     	var info_field = document.getElementById('info_field');

	  var warning = '<p>'
                + '<span class="orange"><br><br><i>If you need to go back to the menu, press the <b>User Portal</b>'
                + ' link from the menu. Browser BACK button has no functionality.</i>'
                + '</p>';   

	if (response.spruceFault) {
     	  	 // We print a nicer error message to the info field, or pass the 
        	// fault along to the generic error handler.
        	if (response.spruceFault.code == 1) {
         		 display_error(response.spruceFault);
			info_field.innerHTML = warning;
        	} else
 		if (response.spruceFault.code == 3) {
          		display_error(response.spruceFault);
       	 		info_field.innerHTML = warning;
    		} else {
    	   	 	display_error(response.spruceFault);
       			info_field.innerHTML = warning;  	 
		}
     		
		show_token_input(); 
	} 

else 

{
	    
	// We will have four partitions for the info field: buttons,
        // (static) info, resources, and users.
	 var token_html = '<p id="token_text">Token: '
               + response.token
               + '<input type="hidden" id="token_input" value="'
               + response.token    
	       + '"/>';    
        // We want to clear any leftover error messages.
       
	document.getElementById('error_field').innerHTML = '';

	document.getElementById('token_field').innerHTML = token_html;

	// We will always have the 'refresh' and 'add user' buttons. The 
        // 'check time' and 'activate' buttons are enabled based on the 
        // token's status.
        var buttons = '<p><span id="refresh_button" class="button">refresh</span> ';
        var status = '<p> Status: ';
	 if (response.activation_date.match('0000-00-00 00:00:00')) {
         	 buttons += '<span id="activate_button" class="button">activate</span> ';
	} else {
          	buttons += '<span id="check_button" class="button">check time</span> ';
        	//reservation
		// show make reservation only if its loni machines
		var voname = decodeURIComponent(response.VO.abbrv);
		if (voname == "LA") {
		buttons += '<span id="reservation" class="button">make reservation</span> ';
		}
	}
        buttons += '<span id="add_button" class="button">add user</span></p>'

	 if (response.activation_date.match('0000-00-00 00:00:00')) {
       		status += '<span class="red">Not yet activated <br/></span>';
	} else {
		status += '<span class="green">Activated <br/></span>';
	}

	// This block is for general information.
        var info = '<div id="announce_field"></div>'
           + '<p>';

	if (response.test.match('true')) {
		info += '<span class="red"> This is a test token only' + '<br/></span>';
	}
          info += '  Lifetime: ' + response.lifetime + '<br/>';
	
	if (response.max_auths.match('infinity')) { }
	else
 	if(response.max_auths == 1)
		 { info += ' <span class="red"> This is a ONE TIME USE token only'  + '<br/></span>'; }
	else { info += ' Maximum Auths: <span class="red"> ' + response.max_auths + '<br/></span>';}

	 if (response.max_auths.match('infinity')) { }
	 else 
	{	
          info += '  Remaining Auths: <span class="red">' + response.auths_left + '<br/></span>';
	}


	if(response.urgency.match('yellow')){
           info += ' <span class="yellow"> Maximum Urgency: ' + response.urgency + '<br/></span>';  
	}
	else if(response.urgency.match('orange')){
           info += ' <span class="orange"> Maximum Urgency: ' + response.urgency + '<br/></span>';  
	}
	else if(response.urgency.match('red')){
	   info += ' <span class="red"> Maximum Urgency: ' + response.urgency + '<br/></span>';
	}
	   info	+= '  Creation date: ' + response.creation_date + '<br/>'
            + '  Expiration date: ' + response.expiration_date + '<br/>';
        if (!response.activation_date.match('0000-00-00 00:00:00')) {
          info += '  Activation date: ' + response.activation_date + '<br/>'
                + '  Deactivation date: ' + response.deactivation_date + '<br/>';
        }
        info += '</p>';

        // The users block contains the list of users, or a message
        // indicating that no users are associated with the token.
        var users = '<p>Users: ';
        var user_list = response.UserList;
          if(user_list) {
	  var user;
          users += '</p><ul>';
          for (user in user_list) {
		u=user_list[user];
			users += '<li>' 
                   	+ '<a href="mailto:' + decodeURIComponent(u.email) + '">'
                   	+ (u.real_name) + '</a> '
                   	+ '&nbsp;&nbsp;<span id="remove_'+user+'_button" class="button">remove</span> '
                   	+ '<input id="remove_'+user+'_button_identity" type="hidden" value="'
                   	+ decodeURIComponent(u.identity)
                   	+ '"/><br>'
                   	+ '(' + decodeURIComponent(u.identity) + ') '
                   	+ '</li>';
          }
          users += '</ul></p>';
        } else {
          users += '<em>there are no users assigned to this token</em>.</p>';
        }

        // This block lists the resources associted with the token. Note that
        // we assume that tokens are specific to a single token.
        var resources = '<p>Resources on ' 
                      + decodeURIComponent(response.VO.abbrv) 
                      + ':</p>'
                      + '<ul>';
        for (site_index in response.VO.site) {
          var site = response.VO.site[site_index];
          for (resource_index in site.resource) {
            if(site.resource[resource_index]) {
		var resource = site.resource[resource_index];
	            resources += '<li>'
                       + decodeURIComponent(resource) + ' @ '
                       + decodeURIComponent(site.abbrv)
                       + '</li>';
        	}
	  }
        }
        resources += '</ul>';

        // This last bit just gives a timestamp as to the freshness of
        // the displayed information.
        var update = '<p>'
            + '  (Token info fresh as of <em><span class="orange">'
            + Date()
            + '</em>)</p>'
      
	var warning = '<p>'
		+ '<span class="orange"><br><br><i>If you need to go back to the menu, press the <b>User Portal</b>' 
		+ ' link from the menu. Browser BACK button has no functionality.</i>'
		+ '</p>'

        // Put the pieces together; this lets us easily rearrange the page.
        info_field.innerHTML = buttons + status + info + resources + users + update + warning;

        // Add event listeners for the buttons.
        YAHOO.util.Event.addListener('refresh_button', 'click', get_token);
        YAHOO.util.Event.addListener('activate_button', 'click', 
                                     show_comment_form);
        YAHOO.util.Event.addListener('check_button', 'click', check_token);
        YAHOO.util.Event.addListener('add_button', 'click', show_user_form);
	YAHOO.util.Event.addListener('reservation', 'click', show_reserv_form);
        if (user_list) {
          for (user in user_list) {
            YAHOO.util.Event.addListener('remove_'+user+'_button',
                                         'click', show_user_alert);
          }
        }
      }
    }

    function getUserInfoSuccess(o) {
      // Process JSON response.
      	// the internal ' and + are causing this to hang for Safari, still need to figure out why

	  var response = eval('(' + o.responseText + ')');	

	  var user_html = '<p id="user_text">Information for user with<br/>'
               + '  Email: ' + response.email + '<br/>'
               + '  Identity: ' + response.identity + '<br/>'
               + '<input type="hidden" id="email" value="'
               + email
               + '"/>'
               + '<input type="hidden" id="identity" value="'
               + identity
               + '"/>';                                          

        // We want to clear any leftover error messages.
	// if we print o.responseText, is shows that test is not getting any value.
        document.getElementById('error_field').innerHTML = '';
        document.getElementById('token_field').innerHTML = user_html; 



      var info_field = document.getElementById('info_field');

        var warning = '<p>'
                + '<span class="orange"><br><br><i>If you need to go back to the menu, press the <b>User Portal</b>'
                + ' link from the menu. Browser BACK button has no functionality.</i>'
                + '</p>'

      if (response.spruceFault) {
        // We print a nicer error message to the info field, or pass the 
        // fault along to the generic error handler.
        if (response.spruceFault.code == 10) {
	 display_error(response.spruceFault);
        info_field.innerHTML = warning;   
        } else {
          display_error(response.spruceFault);
        info_field.innerHTML = warning;        
       		 }
     
	show_user_input();

 } else {
        var token_list = response.token_list;

        // We will have four partitions for the info field: buttons,
        // (static) info, resources, and users.

        // We want to clear any leftover error messages.
        document.getElementById('error_field').innerHTML = '';

        // We will always have the 'refresh' and 'add user' buttons. The 
        // 'check time' and 'activate' buttons are enabled based on the 
        // token's status.
        var buttons = '<p><span id="refresh_button" class="button">refresh</span> ';

	       
        // This block is for general information.
        var info = '<div id="announce_field"></div>'
                 + '<p> The list below shows all the active and not yet activated token'
		 + ' permissions for this user. An urgent job can be submitted only if all' 
		 + ' the resoures being requested are on the same token.</p>' 
		+ '<ul>';

        for (t in token_list) {
	
		 	info += '  <li>'
			+ '<p>';
			
			if(token_list[t].status != "Unactivated") { 
				info += '   Status: <span class="green"> ' + token_list[t].status + '</span><br/>';
				info += '    Time Remaining: ' + token_list[t].time_remaining + '<br/>';
			}
			else {
				info += '   Status: <span class="red"> ' + token_list[t].status + '<br/>';
			}

			if(token_list[t].test == "true") {

				info += ' <span class="red">This is a TEST token only!</span><br/>';
			}
			if(token_list[t].auths_left == "infinity") { }
			else {
				info += ' <span class="red">This is a LIMITED use token with only ' + token_list[t].auths_left + ' Auth(s) remaining.<br/></span>';
			}
			if(token_list[t].max_urgency == "yellow"){
          			 info += ' <span class="yellow"> Maximum Urgency: ' + token_list[t].max_urgency + '<br/></span>';
        		}
        		else if(token_list[t].max_urgency == "orange") {
           			info += ' <span class="orange"> Maximum Urgency: ' + token_list[t].max_urgency + '<br/></span>';
        		}
        		else if(token_list[t].max_urgency == "red") {
           			info += ' <span class="red"> Maximum Urgency: ' + token_list[t].max_urgency + '<br/></span>';
        		}

			info += '</p>';
		
        	  	// This block lists the resources associted with the token. Note that
         	 	// we assume that tokens are specific to a single token.
          		var resources = '<p>Resources on ' 
                	        + decodeURIComponent(token_list[t].VO.abbrv) 
                        	+ ':</p>'
                        	+ '<ul>';
        		 for (site_index in token_list[t].VO.site) {
           		 	var site = token_list[t].VO.site[site_index];
        		 	for (resource_index in site.resource) {
              					var resource = site.resource[resource_index];
              					resources += '<li>'
                         			+ decodeURIComponent(resource) + ' @ '
                         			+ decodeURIComponent(site.abbrv)
                         			+ '</li>';
            			}
          		}
          		resources += '</ul>';
         	 	info += resources + '</li>';
       
	}
	info += '</ul>';

	// This last bit just gives a timestamp as to the freshness of
        // the displayed information.
        var update = '<p>'
            + '  (User info fresh info as of <em><span class="orange">'
            + Date()
            + '</em>)</p>'
       
       
        // Put the pieces together; this lets us easily rearrange the page.
        info_field.innerHTML = buttons + info + update + warning;

        // Add event listeners for the buttons.
        YAHOO.util.Event.addListener('refresh_button', 'click', get_user);
      }
   }



    // Generic failure handler. This only called when the AJAX call itself 
    // fails and not when we recieve SpruceFaults from the web services.
    function failureHandler(o) {
      //clear("result");
      display_error({ code: o.status, msg: o.statusText, desc: '' });
    }

