//  http://developer.mozilla.org/en/docs/Gecko_DOM_Reference
// http://www.pxl8.com/createElement.html
// http://www.w3schools.com/js/js_examples_3.asp
// http://rajshekhar.net/blog/archives/85-Rasmus-30-second-AJAX-Tutorial.html

// global variables. 
var http_request = false;
var id_to_remove = -1; 
var div_to_remove = -1; 
var id_to_edit = -1; 
var div_to_edit = ''; 

function retrievePost(id,revision, div)
{ 	//id is the pid number in the sql database. 
	//div is the division id to replace in the xml schema.
	//first, we have to get the unformatted text at this node. 
	poststr = "id=" + id + "revision=" + revision + "div=" +div; 
	id_to_edit = id; 
	div_to_edit = div; 
	//alert(poststr); 
	http_request = false;
	http_request = new XMLHttpRequest(); //this will only work for mozilla and safari, but that's ok
	if (http_request.overrideMimeType) {
		http_request.overrideMimeType('text/xml');
	}	
	http_request.onreadystatechange = retrieveUpdate; //set the callback. 
	//code that is independent of data. 
	http_request.open('POST', 'retrieveEntry.pl', true);
	http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http_request.setRequestHeader("Content-length", poststr.length);
	http_request.setRequestHeader("Connection", "close");
	http_request.send(poststr);
}
function retrieveUpdate(){
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			result = http_request.responseText;
			if(div_to_edit == "-1"){
				var node = document.getElementById("-2"); 
				var par = node.parentNode;
				var rep = node.cloneNode(true); 
				rep.innerHTML = result;
				rep.setAttribute("id", "-1"); //this one to be replaced!
				par.insertBefore(rep, node);
			}else{
				document.getElementById(div_to_edit+'').innerHTML = result; 
			}				
		} else {
			alert('There was a problem with the request.');
		}
	}
}
function printAttributes(id)
{	//for debugging, i guess.
	var x = document.getElementById(id); 
	var attr = x.attributes; 
	var text = " "; 
	for( var i=0; i < attr.length; i++){
		text = text + attr[i].name + "-" + attr[i].value + " "; 
	}
	alert(text); 
}

// reference: http://www.captain.at/howto-ajax-form-post-request.php
//use the calling form to change or add a row. 
function changeEntryPost(id, doRefs, div){
	var key = "ARhfhIpK4ybt52YSt1g86htr9HtbR"; //delimiter must not contain _
	id_to_edit = id+''; 
	div_to_edit = div; 
	var poststr = 
		key + "_id=" + id_to_edit +
		key + "_div="+ div + 
		key + "_users=" + 
		document.getElementById(id_to_edit+"_users").value +
		key + "_type=" + 
		document.getElementById(id_to_edit+"_type").value +
		key + "_tags=" +
		document.getElementById(id_to_edit+"_tags").value +
		key + "_data=" +
		document.getElementById(id_to_edit+"_data").value + 
		key + "_updateRefs=" + doRefs; 
	http_request = false;
	//alert(poststr); 
	http_request = new XMLHttpRequest(); //this will only work for mozilla and safari, but that's ok
	if (http_request.overrideMimeType) {
		http_request.overrideMimeType('text/xml');
	}	
	http_request.onreadystatechange = changeEntryUpdate; //set the callback. 
	//code that is independent of data. 
	http_request.open('POST', 'changeEntry.pl', true);
	http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http_request.setRequestHeader("Content-length", poststr.length);
	http_request.setRequestHeader("Connection", "close");
	http_request.send(poststr);
}
function displayPost(id, revision, div){
	var key = "ARhfhIpK4ybt52YSt1g86htr9HtbR"; //delimiter must not contain _
	id_to_edit = id+''; 
	div_to_edit = div; 
	var poststr = 
		key + "_id=" + id_to_edit +
		key + "_revision=" + revision + 
		key + "_div="+ div; 
	http_request = false;
	//alert(poststr); 
	http_request = new XMLHttpRequest(); //this will only work for mozilla and safari, but that's ok
	if (http_request.overrideMimeType) {
		http_request.overrideMimeType('text/xml');
	}	
	http_request.onreadystatechange = changeEntryUpdate; //set the callback. 
	//code that is independent of data. 
	http_request.open('POST', 'changeEntry.pl', true);
	http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http_request.setRequestHeader("Content-length", poststr.length);
	http_request.setRequestHeader("Connection", "close");
	http_request.send(poststr);
}
function changeEntryUpdate() {
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			result = http_request.responseText;
			//alert('div_to_edit:' + div_to_edit); 
			var elem = document.getElementById(div_to_edit);
			elem.innerHTML = result;
			if(div_to_edit == '-1'){
				//need to get the id so that we can edit right-away.
				var reg = /\?pid\=(\d+)\">/; 
				var ar = reg.exec(result);
				//alert("ID = " + RegExp.$1); 
				id_to_edit = RegExp.$1;
				div_to_edit = RegExp.$1;
				elem.setAttribute("id", RegExp.$1);
			}
		} else {
			alert('There was a problem with the request.(changeEntryUpdate)');
		}
	}
}

function remove(id)
{
	//replace the div containing an edit link with a text-entry form. 
	var x = document.getElementById(id); 
	x.setAttribute("style", "background-color:red"); 
	if(confirm("Delete this entry?")){
		removeEntryPost(id); 
		//will not delete the div - only replace the internal HTML.
	}
	x.setAttribute("style", ""); 
}
function hide(id,div){
	var x = document.getElementById(div); 
	x.innerHTML = "<a href=\"javascript:displayPost(" + id + ",-1,'"+div+"')\">{" + id + "}show</a>"; 
}

function removeEntryPost(id,div){
	var poststr = "id=" + id; 
	id_to_remove = id; 
	div_to_remove = div; 
	http_request = false;
	http_request = new XMLHttpRequest(); //this will only work for mozilla and safari, but that's ok
	if (http_request.overrideMimeType) {
		http_request.overrideMimeType('text/xml');
	}	
	http_request.onreadystatechange = removeEntryUpdate; //set the callback. 
	//code that is independent of data. 
	http_request.open('POST', 'removeEntry.pl', true);
	http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http_request.setRequestHeader("Content-length", poststr.length);
	http_request.setRequestHeader("Connection", "close");
	http_request.send(poststr);
}
function removeEntryUpdate() {
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			result = http_request.responseText;
			document.getElementById(div_to_remove+'').innerHTML = result;            
		} else {
			alert('There was a problem with the request.' + 
				http_request.status);
		}
	}
}
function HTMLTextArea_InsertTab(event )
{
	event = event || window.event;
	var textarea /*: HTMLTextAreaElement*/ = event.srcElement || event.target;

	if (event.keyCode == 9)
	{
		if (typeof textarea.selectionStart !== "undefined")
		{
			/* Collapse Insertion Point */
			textarea.setSelectionRange(textarea.selectionEnd, textarea.selectionEnd);
			textarea.focus();

			/* Insert Tab */
			var selStart /*: int*/ = textarea.selectionStart;
			var selEnd /*: int*/ = textarea.selectionEnd;
			var currentScroll /*: int*/ = textarea.scrollTop;
			var contents /*: String*/ = textarea.value;
	
			textarea.value = contents.substring(0, selStart) + "\t" + contents.substring(selEnd, contents.length);

			textarea.scrollTop = currentScroll;
			textarea.setSelectionRange(selEnd + 1, selEnd + 1);

			textarea.focus();

			event.preventDefault();
		}
		else if (document.selection)
		{
			var range /*: HostObject(TextRange)*/ = document.selection.createRange();
			range.collapse();
			range.text = String.fromCharCode(9);
			range.collapse();
			range.select();

			event.returnValue = false;
		}
	}
}