nfcRing.vcard = {};
nfcRing.vcard.cache = {};

// Search for a contact
nfcRing.vcard.search = function(name){
  $('#vCardResults').html(""); // Clear the results
  $('#vCardLoading').show(); // Show the loading screen
  $('#vCardNoResults').hide(); // Hide the vCardNoResults
  var options = new ContactFindOptions();
  options.filter = name;
  options.multiple = true;
  var fields = ["*"]; // Gets all the user data
  navigator.contacts.find(fields, nfcRing.vcard.found, nfcRing.vcard.error, options);
}

// When a contact is found write it to the UI
nfcRing.vcard.found = function(contacts){
  $('#vCardLoading').hide();
  console.log("Contact found", contacts);
  if(contacts.length === 0){ // If no contacts were found.
    $('#vCardResults').html("");
    $('#vCardNoResults').show();
  }else{
    $('#vCardNoResults').hide();
  }
  var i = 0;
  $.each(contacts, function(k,person){
    if(person.displayName && person.id){
      if(i < 5){
        $('#vCardResults').append("<div class='contact' id='"+person.id+"'>"+person.displayName+"</div>");
        nfcRing.vcard.cache[person.id] = person;
        i++;
      }
    }
  });
}

nfcRing.vcard.showFields = function(){
  $("#vCardData").html("");
  $.each(nfcRing.userValues.contactToWrite, function(key, value){
    if(nfcRing.userValues.contactToWrite[key]){
      if(key !== "id" && key !== "rawId" && key !== "remove" && key !== "clone" && key !== "save" && key !== "photos" && key !== "displayName"){
        $("#vCardData").append("<div class='contactInfo'><label>" + key + "<input class='vCardCheckbox' type='checkbox' id='" + key + "' value='" + key + "' name='" + key + "'></label></div>");
      }
    }
  })
}

// takes in contact card from cordova and builds vcard format
nfcRing.vcard.build = function(){
  var contact = nfcRing.userValues.contactToWrite;
  console.log("Which properties should I write?");
  var props = $('.vCardCheckbox:checked');

  if(!props){
    alert("no properties selected, exiting");
    return;
  }

  var vCard = 'BEGIN:VCARD\n' +
   'VERSION:2.1\n';

  $.each(props, function(key, value){
    if(props[key].id === "name"){
      vCard += 'N:'+contact.name.familyName+';'+contact.name.givenName+';;;\n' + 
        'FN:'+contact.name.formatted+'\n';
    }

    if(props[key].id === "emails"){
      vCard += 'EMAIL;WORK:'+contact.emails[0].value+'\n';
    }

    if(props[key].id === "telephone"){
      vCard += 'TEL;'+contact.phoneNumbers[0].value+'\n';
    }
  });

  vCard += 'END:VCARD';
  console.log("vCard", vCard);
  nfcRing.userValues.isVCard = true;
  return vCard;
}

nfcRing.vcard.error = function(e){
  console.error("vCard Error", e);
}

@@ -1914,16 +1914,67 @@
 ecked');
+%0A  console.log(%22props%22, props, %22contact%22, contact);
 %0A%0A  if(!
@@ -2439,16 +2439,52 @@
 lephone%22
+ %7C%7C props%5Bkey%5D.id === %22phoneNumbers%22
 )%7B%0A     
@@ -2537,24 +2537,256 @@
 +'%5Cn';%0A    %7D
+%0A%0A    if(props%5Bkey%5D.id === %22addresses%22)%7B%0A      var address = contact.addresses%5B0%5D.formatted;%0A      address = address.replace(%22%5Cn%22, %22;%22);%0A      console.log(%22Address%22, address)%0A      vCard += 'ADR;WORK:;;;'+address+'%5Cn';%0A    %7D%0A   %0A   
 %0A  %7D);%0A%0A  vC

add support for addresses in vcards