/*
* Retrieve all contacts
*/

var logIn = function(){
  var scope = 'http://www.google.com/m8/feeds';
  var token = google.accounts.user.login(scope);
}

    function getContacts(){
  var scope = 'http://www.google.com/m8/feeds';
  if(google.accounts.user.checkLogin(scope))
    {
// Create the contacts service object
var contactsService =
    new google.gdata.contacts.ContactsService('Reading-Trails-find-friends');

// The feed URI that is used for retrieving contacts
var feedUri = 'http://www.google.com/m8/feeds/contacts/default/thin';
var query = new google.gdata.contacts.ContactQuery(feedUri);

// Set the maximum of the result set to be 50
query.setMaxResults(2000);

  // Submit the request using the contacts service object
      contactsService.getContactFeed(query, callback, handleError);
    }
}

// callback method to be invoked when getContactFeed() returns data
var callback = function(result) {

  var contacts = document.getElementById('searchResults');
  contacts.innerHTML = "<p><img alt='loading...' src='images/loadingAnimation.gif'></p>";
  contacts.style.border = '1px solid #898c4b';
  contacts.style.width = '368px';
  contacts.style.height = '400px';
  contacts.style.overflow = 'auto';
  contacts.style.margin = '10px 0 10px 0';
  contacts.style.padding = '12px';


  var n=0;
  
  // An array of contact entries
  var entries = result.feed.entry;
  
  // link & params

  var link =  "find_friends_go.php";
  var params = '';
  var n=0;

  
  for (var i = 0; i < entries.length; i++) 
    {
      var contactEntry = entries[i];
      var emailAddresses = contactEntry.getEmailAddresses();
      
      for (var j = 0; j < emailAddresses.length; j++) 
	{
	  var emailAddress = emailAddresses[j].getAddress();
	  params += '&email' + n + '=' + emailAddress;
	  n++;
	}    
    }

    while( params != params.replace('@', '%40'))
      params = params.replace('@', '%40');


  var httpObject = getXmlHTTPObject();
  if(httpObject != null)
    {
      httpObject.open("POST", link, true);
      httpObject.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      httpObject.setRequestHeader("Content-length", params.length);

      httpObject.send(params);

      httpObject.onreadystatechange = function(){
	if(httpObject.readyState == 4 )//  && httpObject.status == 200 )
	  {
	    contacts.innerHTML = httpObject.responseText;
	  }
      }

      }


}
    
    // Error handler
    var handleError = function(error) {
    alert(error);
}
  
