/**
 * @author mobitect
 */
 

function updateContactDetails(key) {
     // First I need to get all the details for the contact and then update the contact details with this information
	// I will also use this key to update the notes page which will be downloaded only if there is a connection
	 
	 // First thing I will do is launch the download of the notes in the background.  I do this first as it can
	 // take some time and hopefully by the time they click on the notes, the page will be updated
	
	var myList = $("#Notes>ul>li>a");
	myList.each(function () {
		$(this).parent('li').remove();
	});

	 downloadAllData("people/"+key+"/notes.xml");

	 var contactXML = localStorage.getItem(key);
	 var xmlobject = (new DOMParser()).parseFromString(contactXML, "text/xml");
	 var progContainer="";

	  $(xmlobject).find('person').each(function(){
	  	var PersonID  = document.getElementById('ContactID');
		PersonID.value = key;

		//setElement("ContactID",key, "");
		setElement("ContactFirstName",$(this).find('first-name').text(), "");
		setElement("ContactLastName",$(this).find('last-name').text(), "");
		setElement("ContactTitle",$(this).find('title').text(), "");
		// Find all the different type of addresses
		$(this).find('addresses').each(function(){
			$(this).find('address').each(function(){
				var fullAddress = $(this).find('street').text()+" "+$(this).find('city').text()+" "+ $(this).find('state').text()+" "+$(this).find('zip').text()+" "+$(this).find('country').text();
				setElement($(this).find('location').text()+"Address",fullAddress, "window.location.href = 'http://maps.google.com/maps?q="+fullAddress+"'");
			});
		});
		// Find all the different type of email addresses
		$(this).find('email-address').each(function(){
			setElement($(this).find('location').text()+"Email",$(this).find('address').text(), "window.location.href = 'mailto:"+$(this).find('address').text()+"'");
		});
		// Find all the different type of phone
		$(this).find('phone-number').each(function(){
			setElement($(this).find('location').text()+"Phone",$(this).find('number').text(), "window.location.href = 'tel:"+$(this).find('url').text()+"'");
		});
		// Find all the different type of web site
		$(this).find('web-address').each(function(){
			setElement($(this).find('location').text()+"Website",$(this).find('url').text(), "window.location.href = '"+$(this).find('url').text()+"'");
		});
		// Find all the different type of twitter
		$(this).find('twitter-account').each(function(){
			setElement($(this).find('location').text()+"Twitter",$(this).find('username').text(), "window.location.href = '"+$(this).find('url').text()+"'");
		});
		// Find all the different type of company namees
		// This one is a little harder as I have to link the company-id to another localstorage
		var companyID = $(this).find('company-id').text();
		var companyXML = localStorage.getItem(companyID);
		var xmlobjectCompany = (new DOMParser()).parseFromString(companyXML, "text/xml");
		setElement("ContactCompany",$(xmlobjectCompany).find('name').text(), "");
	  });

}

function setElement(ElementID, ElementValue, ElementAttribute) {
		var progContainer = document.getElementById(ElementID);
		progContainer.innerHTML = ElementValue;
		if (ElementAttribute != "")
     		progContainer.setAttribute("onclick", ElementAttribute);
}

