/**
 * @author mobitect
 */

function saveNote() {
	//	This will take a note and save it to HighriseHQ

	var PersonID  = document.getElementById('ContactID').value;
	var noteValueStr  = document.getElementById('noteString').value;
	var noteStr = "<note>";
	noteStr += "<body>"+noteValueStr+"</body>";
	noteStr += "</note>"

	// Load the API token from localstorage, if it is blank then skip the sync
	var companyName = "";
	var apiToken = "";
	var settingsXML = localStorage.getItem("settings");

	if (settingsXML != null) {
		var xmlobject = (new DOMParser()).parseFromString(settingsXML, "text/xml");
		companyName = $(xmlobject).find('company-name').text();
		apiToken = $(xmlobject).find('api-token').text();
	}

	var proxyServer="";
	var highriseServer="";
	var url="";
	var currentURL = document.URL;

	// This part is simply for debugging purposes, if the url contains cavanagh-pc then ignore the appendServer
	if ( (currentURL.indexOf("cavanagh-pc") == -1) && (url.indexOf("php") == -1 ) ){
		url = "http://mobitect.com/crm/proxy/post_note.php?user=";
		url += apiToken;
		url += "&pwd=X&company=";
		url += companyName;
		url += "&person=";
		url += PersonID;
		url += "&bodyStr=";
		url += noteStr;
	}

	if(req) { 
		req = new XMLHttpRequest();
		req.onreadystatechange = processNewNoteChange;
		req.open("GET", url, true);
		req.send(null);
	}	
}

// handle onreadystatechange event of req object
function processNewNoteChange() {
  if (req.readyState == 4) {
      // only if "OK"
		if (req.status == 200) {
			var PersonID  = document.getElementById('ContactID').value;
			downloadAllData("people/"+PersonID+"/notes.xml");
			// Clear the search and new note text boxes
			var SearchStr  = document.getElementById('noteFilter');
			SearchStr.value = "";
			var NoteStr  = document.getElementById('noteString');
			NoteStr.value = "";
		}

  }
}


