// ajax caller. gets the xml object, sets up the php call string, executes, and 
// deals with the results of the call.
function submit_changes(form_data) {


	// get and check AJAX object
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null) return; 

	// must be a relative url or you wont get a responseText replay
	var url= "../check_login.php?" + $(form_data.id).serialize();
	//alert(url);

	xmlHttp.onreadystatechange=function() {
		if (xmlHttp.readyState==4) {
			if(xmlHttp.responseText != "1") {
				alert(xmlHttp.responseText);
				return false;
			} else {
				location.href = location.href;
				get_node('login_overlay_wrapper').style.display = "none";
				get_node('login_overlay_box').style.display = "none";
				open_node = null;
				return true;
			}
		}
	}
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);

}

// gets the xml object for ajax calls
function GetXmlHttpObject() {
	var xmlHttp=null;
	try { xmlHttp=new XMLHttpRequest(); }
	catch (e) {
		try { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); }
		catch (e) { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); }
	}
	return xmlHttp;
}


// calculates the distance c (a node) is from the left size of the page. Used for
// positioning the mouse overlay
function total_offset_left(c) {

	total = 0;

	while (c.id != "main_wrapper") {
		total += c.offsetLeft;
		c = c.parentNode;
	}

	var scroll_x = 0;
	if( typeof( window.pageYOffset ) == 'number' ) {
		scroll_x = window.pageXOffset;
	} else if( document.body && document.body.scrollLeft ) {
		scroll_x = document.body.scrollLeft;
	} else if( document.documentElement && document.documentElement.scrollLeft ) {
		scroll_x = document.documentElement.scrollLeft;
	}
	
	total -= scroll_x;

	return total;

}

// calculates the distance c (a node) is from the top of the page. Used for
// positioning the mouse overlay
function total_offset_top(c) {
	total = 0;
	//Alert = "";
	while (c.id != "main_wrapper" && !(c.nodeName == "BODY" || c.nodeName == "body")) {
		//Alert += total + " + " + c.offsetTop + " (" + c.id +"," + c.nodeName + ")\n";
		total += c.offsetTop;
		c = c.parentNode;
	}
	//Alert += total;
	//alert(Alert);
	var scroll_y = 0;
	if( typeof( window.pageYOffset ) == 'number' ) {
		scroll_y = window.pageYOffset;
	} else if( document.body && document.body.scrollTop ) {
		scroll_y = document.body.scrollTop;
	} else if( document.documentElement && document.documentElement.scrollTop ) {
		scroll_y = document.documentElement.scrollTop;
	}
	
	total -= scroll_y;

	return total;

}

// sets up and displays the various edit windows
function load_login() {
	get_node('login_overlay_wrapper').style.display = "block";
	get_node('login_overlay_box').style.display = "block";
	open_node = 'login_overlay_box';
}

// if one of the "click to edit" overlays are showing and the user scrolls, update
// the position of the overlay so it scrolls with the rest of the page.
window.onscroll = function (e) {
	if (get_node('editable_overlay')!==null) {
	editable_overlay = get_node('editable_overlay');
	editable_overlay.style.top = total_offset_top(editable_overlay.parentNode) + "px";
	//alert(total_offset_top(editable_overlay.parentNode) + "px");
	} else if (open_node!==null) {
		the_node = get_node(open_node);

		var scroll_y = 0;
		if( typeof( window.pageYOffset ) == 'number' ) {
			scroll_y = window.pageYOffset;
		} else if( document.body && document.body.scrollTop ) {
			scroll_y = document.body.scrollTop;
		} else if( document.documentElement && document.documentElement.scrollTop ) {
			scroll_y = document.documentElement.scrollTop;
		}

		//alert(document.documentElement.scrollTop);
		the_node.style.marginTop = 100 - scroll_y + "px";

		//alert(the_node.style.cssText);
		//alert(the_node.style.marginTop);
		total_offset_top(the_node);
		//the_node.style.marginTop = total_offset_top(the_node) + "px";

	}

}

// hides nodes
function hide_node(node_id) {	get_node(node_id).style.display = "none"; }

// returns the node object with id="<node_id>"
// remember: <textarea></textarea> has no id="" attribute. if you want that object use
// pass the <form> id into getnode() and then use <textarea>'s name attribute:
// example: getnode('FORM_ID').textarea_name.innerHTML
function get_node(node_id) {
	if (document.getElementById) return document.getElementById(node_id);
	else if (document.all) return document.all[node_id];
	else if (document.layers) return document.layers[node_id];
}

