<klip>
   <identity>
      <title>
         API: HTTPRequest
      </title>
   </identity>
   <locations>
      <contentsource>
         http://www.serence.com/working/protected/test.xml
      </contentsource>
      <icon>
         http://www.klipfolio.com/static/klips/klipfolio/sample_icon.png
      </icon>
      <banner>
         http://www.klipfolio.com/static/klips/klipfolio/sample_banner.png   
      </banner>
   </locations>
   <style>
      alert {
         type: item;
      }

      sender {
         itemcol: 2;
         noterow: 1;

         label: 'Sender';

         emphasis: strong;
      }


      summary {
         itemcol: 3;
         noterow: 4;

         wrap: false;

         notelabel: false;
      }

      date {
         itemcol: 4;
         noterow: 2;

         label: 'Date';
      }

      category {
         noterow: 3;

         label: 'Category';
      }


      level {
         itemcol: 1;
         noterow: 5;
         notelabel: false;
         
         type: image;
      }

      url {
         type: link;
      }
            
      id {
         key: override;
      }
   </style>
   <klipscript>
   <![CDATA[

// - - - Intro text if the user has not connected.

var g_intro_styles =
   "introitem {type:item;}" +
   "introtext {itemcol:1; wrap:false;}" +
   "introimages {itemcol:2; type:image; height:3;}" +
   "helpheader {noterow:1; notelabel:false; emphasis:strong;}" +
   "helptext {noterow:2; notelabel:false;}" +
   "img {noterow:3; type:image; notelabel:false;}";

   
var gUserNameField, gPasswordField;

var gIconAuth   = "http://www.klipfolio.com/static/klips/devguide/sample_klip_icon_auth.gif";
var gIconNoAuth = "http://www.klipfolio.com/static/klips/devguide/sample_klip_icon_noauth.gif";

function onLoad() 
{   
   show_state();
   
   var tab = Setup.addTab( "Authentication" );

   tab.addText("User name:");
   gUserNameField = tab.addTextField();

   tab.addText("Password:");
   gPasswordField = tab.addTextField();
   gPasswordField.obscured = true;

   if (Prefs.getPref("UserName") != "" ) {
      gUserNameField.value = Prefs.getPref("UserName");
   }

   if (Prefs.getPref("Password") != "" ) {
      gPasswordField.value = Prefs.getPref("Password");
   }

   // - -  update prefs when the user closes the setup
   Setup.onClose = savePrefs;  
  
     // - - Put a welcome message in the Klip
   if( ! gUserNameField.value ) {
      setStatus( "Click here to login" );
   }
}


function clickWelcomeMessage ()
{
   Setup.open(0);
}

function savePrefs () 
{
   Prefs.setPref( "UserName", gUserNameField.value );
   Prefs.setPref( "Password", gPasswordField.value );
}

function setStatus ( str )
{
   var g_intro_text =
   "<klipfood>" +
      "<introitem>" +
         "<introtext>" + str + "</introtext>" +
         "<helpheader>You need to login to access remote content</helpheader>" +
         "<helptext>Use 'pumpkin' for Username, 'pie' for Password.</helptext>" +
         "<img>http://www.klipfolio.com/static/klips/devguide/api_login.png</img>" +
      "</introitem>" +
   "</klipfood>";

   Items.clear( true );
   Engines.Data.stylesheet = g_intro_styles;
   Engines.Data.process( g_intro_text )
   Items[0].candelete = false;
   Items[0].canvisit = false;
   Items[0].canautoremove = false;
   Items[0].onClick = clickWelcomeMessage;
}

function onRefresh() 
{  
   if( ! gUserNameField.value ) {
      _t( "no password" );
      return false;
   }
   
   _t( "Attempting to connect\r\n  username: " + gUserNameField.value
      + "\r\n  password: " + gPasswordField.value + "\r\n" );

   var req = Engines.HTTP.newRequest( Prefs.contentsource );
  
   // - - Assign the username and password to the HTTPRequest object
   
   req.username = gUserNameField.value; 	// Set username
   req.password = gPasswordField.value;   	// Set password
   var result = req.send();
 
   if ( result ) 
   {
      _t( "Headers: " + req.response.headers );
      _t( "Headers: " + req.response.data );

      Items.icon = gIconAuth;

      // - Clear status message if using intro sytlesheet
      if (Engines.Data.stylesheet == g_intro_styles ) 
      {
         Items.clear( true );
      }
      
      // - - Process incoming data with Klip's defined <style>
      Engines.Data.stylesheet = "@import klip";
      Engines.Data.process( req.response.data );
     } 
     else 
     {
      if ( req.response.status == 401 ) 
      {
         _t( "Bad username/password (result: " + result + "), response: " 
            + req.response.headers + " status:" + req.response.status);

         Items.icon = gIconNoAuth;
         setStatus( "[login failed]" );
      }
      else
      {
         _t( "Unable to login (result: " + result + "), response: " 
            + req.response.headers + " status:" + req.response.status);

         setStatus( "[unable to login]" );
      }
   }
  
   return result;
}


//
// Useful Debug Code -----------------------------------------------
//
var button;
function show_state() 
{
   show_all_prefs ();
}

function show_all_prefs() 
{
   if (Prefs.length == 0) 
   {
      trace ("There are no Prefs defined\r\n");
      return;
   }
   
   trace( "------------------------\r\n" );

   for (var i = 0; i < Prefs.length; i++) 
   { 
      trace("Prefs.getPref( \""+Prefs[i].name+"\" ) == \""+Prefs[i].value+"\"\r\n"); 
   }
   
   trace( "------------------------\r\n" );
}

var gTrace = true;
function _t( s ) 
{
   if ( gTrace ) {
      trace( s + "\r\n" );
   }
}
]]>
   </klipscript>
</klip>
