// Check to see if it's been defined somewhere before
if(typeof start_ajax != 'function') {
	function start_ajax() {
		var ajax = false;
		try { 
			ajax = new XMLHttpRequest(); // Firefox, Opera 8.0+, Safari
		} catch (e) {
			// Internet Explorer
			try { ajax = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
				try { ajax = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e) {
					alert("Your browser does not support AJAX!");
				}
			}
		}
		return ajax;
	}
}
if(typeof urlencode != 'function') {	
	// This makes the urlencode work like PHP's
	function urlencode(str) {
		str = escape(str);
		str = str.replace('+', '%2B');
		str = str.replace('%20', '+');
		str = str.replace('*', '%2A');
		str = str.replace('/', '%2F');
		str = str.replace('@', '%40');
		return str;
	}
}
if(typeof urldecode != 'function') {
	// This makes the urldecode work like PHP's
	function urldecode(str) {
		str = str.replace('+', ' ');
		str = unescape(str);
		return str;
	}
}

// This is where the magic of ajax happens
function add_path_note() {

	ajax = start_ajax();

	if(ajax) {
		
		//update_save_status('loading');
		
		var url = "/ajax/pathnote/";	// PHP file that does the work
		var category = document.getElementById('pathnote_categoryid').value;
		var user = document.getElementById('pathnote_userid').value;
		var path = document.getElementById('pathnote_pathid').value;
		var note = document.getElementById('pathnote_notetext').value;
		var additional_params = "category="+ urlencode(category) +"&user="+ urlencode(user) +"&path="+ urlencode(path) +"&note="+ urlencode(note);		// Set multiple params here
		ajax.open("POST", url, true);
		
		//Send the proper header information along with the request
		ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		ajax.setRequestHeader("Content-length", additional_params.length);
		ajax.setRequestHeader("Connection", "close");
		
		ajax.onreadystatechange = function() {//Call a function when the state changes.
			if(ajax.readyState == 4 && ajax.status == 200) {
				// This is where we get back any results.
				//   NOTE --
				//   ANYTHING printed/echo'd will show up here.  So only bring back specifics.
				//   "ajax.responseText" is the results.  You can alert(ajax.responseText) too or whatever.
				//   In this example, i'm sending it to another function
				parse_results(ajax.responseText,category);
				//alert(ajax.responseText)
			}
		}
		ajax.send(additional_params);

	}

	return 0;
}

// Simple function that makes the correct message popup.  
function parse_results(val,category) {
	if(val != '') {
		
		//update_save_status('complete');
		
		tbl = document.getElementById('user_notes');
		hdr = document.getElementById('user_notes_header');

//		first_data_row = document.getElementById('user_notes').getElementsByTagName("tr")[1];
//		first_data_row_parent = first_data_row.parentNode;

		var tr = document.createElement("tr");
		tr.style.backgroundColor = "#FEFFAA";
		
		var td0 = document.createElement("td");
		var td1 = document.createElement("td");
		var td2 = document.createElement("td");
		var td3 = document.createElement("td");
		var td4 = document.createElement("td");


//		var catid = document.getElementById('pathnote_categoryid').value;

		td0.innerHTML = '';
		td1.innerHTML = val;
		td2.innerHTML = document.getElementById('pathnote_categoryid').options[(category-1)].text;
		td3.innerHTML = document.getElementById('pathnote_user').value;
		td4.innerHTML = document.getElementById('pathnote_notetext').value;

		tr.appendChild(td0);
		tr.appendChild(td1);
		tr.appendChild(td2);
		tr.appendChild(td3);
		tr.appendChild(td4);

		if(document.getElementById('user_notes').getElementsByTagName("tr")[1]) { 
			first_data_row = document.getElementById('user_notes').getElementsByTagName("tr")[1];
			first_data_row_parent = first_data_row.parentNode;
			first_data_row_parent.insertBefore(tr, first_data_row);
		} else { 
			tbl.appendChild(tr);
		}
		// reset values to original info
		document.getElementById('pathnote_notetext').value = "";

	}
}


// This function changes the status names / colors / icons next to the email during verification
function update_save_status(val) {
	var es = document.getElementById('note_save_status');
	es.innerHTML = "";
	if(val == "loading") {
		es.style.color = "#999";
		es.innerHTML = "<img src='/images/spinner2.gif' alt='verifing email' border='0' align='absmiddle' /> ";
	}}
