function get_playlist() {

  loading();

  // get the parameters
  var form = $('lastfmform'); 
  try {
    var lfmuserObj = form['lfmuser'];
    var username = $(lfmuserObj).getValue(); 
  }
  catch(err) {
    stoploading();
  }
  if (username == '') {
    stoploading();
  }
  else {
    
    try {
      var cdurObj = form['cdur'];
      var cdur = $(cdurObj).getValue(); 
    }
    catch(err) {
      var cdur = '1800';
    }


    params = 'lfmuser=' + username
    params = params + '&cdur=' + cdur

    // url to post to
    url = '/scripts/last.fm.php'

    var newAjax = new Ajax.Request(url, {
      method: 'post',
      parameters: params, 
      onSuccess: function(transport) { 
        //
        var data = transport.responseText.evalJSON();
        var title = data.title;

        var username = data.username;
        var size = data.size;
        var statusCode = data.statusCode;
        var statusDesc = data.statusDesc;
        var resultCount = data.resultCount;
        var results = data.results; // loop through these
        var id = data.id;
        if (statusCode <=  199) {
          errorHtml = '<h1>Error</h1><p><img src="./images/' + size + '.png" alt="' + size + '" border="0" align="left" class="main_image" />'
          errorHtml = errorHtml + 'There was a problem with that request.  You may want to try again</p>'
          errorHtml = errorHtml + '<p>The error was - code ' + statusCode + ', which means ' + statusDesc + '</p>'
          $('content_text').update(errorHtml);
          }
        else if (resultCount == 0) {
          errorHtml = '<h1>Error</h1><p><img src="./images/' + size + '.png" alt="' + size + '" border="0" align="left" class="main_image" />'
          errorHtml = errorHtml + 'No results were found for that request.  It is possible that the last.fm server was down.  Otherwise we couldn\'t get any listening records for ' + username + '.</p>'
          $('content_text').update(errorHtml);
        }
        else {
          // do something with this.
          display_results(data.results, title, username, size, id);
        }
      stoploading();
      }
    });
  }
}

function display_results(dresults, title, username, size, id) {
    myHtml = '<h1>' + title + '</h1><p><img src="./images/' + size + '.png" alt="' + size + '" border="0" align="left" class="thumb2" />';
    myHtml = myHtml + ' Here\'s your new Playlist.  It should fit nicely onto one side of a ' + size + '.  It looks like a good mixtape to me.</p>';
    myHtml = myHtml + '<br/><br/><table border="0" cellspacing="0" cellpadding="4"><tr><th>&nbsp;</th><th>Artist</th><th>Track</th></tr>';
    
    i=0;
    while (i<dresults.length){
      trackno = dresults[i].trackno;
      track = dresults[i].track;
      artist = dresults[i].artist;
      url = dresults[i].url;
      myHtml = myHtml + '<tr><td>' + trackno + '</td><td>' + artist + '</td>'
      myHtml = myHtml + '<td><a href="' + url + '" target="_blank">' + track + '</a></td></tr>'
      i++;
    }
    myHtml = myHtml + '</table><p>Now all there is to do is to add it to your last.fm account.  In order to do this you\'ll need to give us permission to access that account.  ';
    myHtml = myHtml + 'This means that you\'ll have to login to last.fm.  If you want to add it to last.fm, follow this link - <br/> ';
    myHtml = myHtml + '<a href="http://www.last.fm/api/auth/?api_key=4679615d93fedb9e9fa4d01bd4c9ef8e"><img src="./images/add_lastfm.png" alt="add this playlist to last.fm" border="0"/></a></p>'
    $('content_text').update(myHtml);
}

function loading() {
	$('loading').update('<img src="/images/loader.gif" border="0" alt="loading"/> Loading...');
	setTimeout("kill_loader()",30000);
	return true;
}

function kill_loader() {
	if ($('loading').childElements()[0]) {	
		$('loading').update('<button type="button" name="the_playlist" onclick="get_playlist();" type="submit"><img src="./images/arrow.png" alt="Mix" /> Mix the Tape</button>');
		}
	else {
		$('loading').update('<button type="button" name="the_playlist" onclick="get_playlist();" type="submit"><img src="./images/arrow.png" alt="Mix" /> Mix the Tape</button>');
	}
	return true;
}

function stoploading() {
	//Effect.BlindUp('loading');
	$('loading').update('<button type="button" name="the_playlist" onclick="get_playlist();" type="submit"><img src="./images/arrow.png" alt="Mix" /> Mix the Tape</button>');
	return true;
}

function swap_blind(thediv) {
  Effect.toggle(thediv, 'blind');
  return false;
  }
