﻿// Copyright(c) 2009 IT One, Ltd, Slovakia
// Do not use, change or distribute.

function initContacts(){
	if (navigator.userAgent.indexOf('Opera') == -1) {
		Picker.setServiceName('CleverGlobe');
		Picker.setUserAddCallback(addContact);
		Picker.setUserRemoveCallback(removeContact);
		Picker.render('contcts');
	}
}

function handleContactsOK(){

}

var emailRecipients = [];
 
 
function updateForm() {
  // Create a concatenated version of the emailRecipients array, suitable
  // for use as an email header.
  var emailRecipientsString = '';
  for (var i = 0; i < emailRecipients.length; i++) {
    var recipient = emailRecipients[i];
    emailRecipientsString += recipient + ', ';
  };
  emailRecipientsString = emailRecipientsString.substring(0, emailRecipientsString.length - 2);
  
  // Update the recipient display on the form
  var formRecipients = document.getElementById('rcpDiv');
  formRecipients.innerText = emailRecipientsString;
  formRecipients.innerHTML = emailRecipientsString;
}
 
function sendMessage() {
  // Get form data
  var formRecipients = document.getElementById('rcpDiv').value;
  window.location = 'mailto:' + formRecipients + '?subject=' + formSubject + '&body=' + formMessage;
}
 
var addContact = function(contactEntry) {
  // Extract email addresses from the contact entry.
  var emailAddresses = contactEntry.getEmailAddresses();
  
  // Add the new address to our list of recipients and update the form. If
  // the user has more than one address, we'll just use the first one 
  // listed.
  if (emailAddresses) {
    emailRecipients.push(emailAddresses[0].getAddress());
  }
  updateForm();
}
 
var removeContact = function(contactEntry) {
  // Extract email addresses from the contact entry.
  var emailAddresses = contactEntry.getEmailAddresses();
  
  // Remove the new address from our list of recipients and update the 
  // form. Ifthe user has more than one address, we'll just use the first 
  // one listed.
  if (emailAddresses) {
    var addressToRemove = emailAddresses[0].getAddress();
    
    // Locate the index of the address
    var result = -1;
    for (var i = 0; i < emailRecipients.length && result == -1; i++) {
      if (emailRecipients[i] == addressToRemove)
        result = i;
    }
    
    // Remove the address from the array
    if (result != -1) {
      emailRecipients.splice(result, 1);
    }
  }
  updateForm();
}