var graph_width=400;
var graph_height=400;
function our_escape(string) {
	string=escape(string);
	while (string.indexOf("*") > -1)
		string=string.replace("*", "%2A");
	while (string.indexOf("+") > -1)
		string=string.replace("+", "%2B");
	while (string.indexOf("/") > -1)
		string=string.replace("/", "%2F");
	return string;
}
var function_num=2;
var colors=new Array("ff0000", "00ff00", "0000ff", "dddd00", "00ffff", "ff00ff", "008000", "800000", "000080", "808000", "800080","008080");
function update_graph() {
	var function_string='';
	for (var i=1;i<=function_num;i++) {
		function_string+='Y'+i+'='+our_escape(document.getElementById('Y'+i).value)+'&';
	}
	//document.getElementById("graph").src='image.php?'+function_string+'function_num='+function_num+'&left_boundary='+our_escape(document.getElementById('left_boundary').value)+
	//'&right_boundary='+our_escape(document.getElementById('right_boundary').value)+'&bottom_boundary='+our_escape(document.getElementById('bottom_boundary').value)+
	//'&top_boundary='+our_escape(document.getElementById('top_boundary').value);
	document.getElementById("graph").style.background='url("image.php?'+function_string+'function_num='+function_num+'&left_boundary='+our_escape(document.getElementById('left_boundary').value)+
	'&right_boundary='+our_escape(document.getElementById('right_boundary').value)+'&bottom_boundary='+our_escape(document.getElementById('bottom_boundary').value)+
	'&top_boundary='+our_escape(document.getElementById('top_boundary').value)+'")';
}
function add_formula() {
	function_num++;
	var parent=document.getElementById("main_form");
	var submit=document.getElementById("submit");
	var span=document.createElement("span");
	span.style.color='#'+colors[(function_num-1)%12];
	span.appendChild(document.createTextNode('Y'));
	var object=document.createElement("sub");
	object.innerHTML=function_num;
	span.appendChild(object);
	parent.insertBefore(span, submit);
	parent.insertBefore(document.createTextNode('='), submit);
	object=document.createElement("input");
	object.type="text";
	object.name="Y"+function_num;
	object.id="Y"+function_num;
	parent.insertBefore(object, submit);
	parent.insertBefore(document.createElement("br"), submit);
	var option=document.createElement("option");
	option.value=function_num;
	option.innerHTML="Y"+function_num;
	document.getElementById('Y').appendChild(option);
}
function show_hide(id){
	document.getElementById(id).style.display=(document.getElementById(id).style.display == 'none')?'block':'none';
	document.getElementById(id+"_control").innerHTML=(document.getElementById(id+"_control").innerHTML == 'Show')?'Hide':'Show';
}

var graphPos; // graph position
var boxMove=false; // are we moving the graph box
var boxX=0; // x pos of box
var boxY=0; // y pos of box
var mousePosition; // position of mouse
var graph_box_top_x; // coordinates of box - relative to graph
var graph_box_top_y;
var graph_box_bottom_x;
var graph_box_bottom_y;

function findPos(obj) {
	var left=0;
	var top=0;
	if (obj.offsetParent) {
		while(1) {
			left+=obj.offsetLeft;
			top+=obj.offsetTop;
			if (!obj.offsetParent) {
				break;
			}
			obj=obj.offsetParent;
		}
	} else if (obj.x && obj.y) {
		left+=obj.x;
		top+=obj.y;
	}
	return {x:left, y:top}
}
function mousePos(ev) {
	if (ev.pageX || ev.pageY) {
		return {x:ev.pageX, y:ev.pageY};
	} else {
		return {x:ev.clientX+document.body.scrollLeft-document.body.clientLeft, y:ev.clientY+document.body.scrollTop-document.body.clientTop};
	}
}

function onmousemove(ev) {
	ev=ev||window.event;
	var position=mousePos(ev);
	mousePosition=position;
	
	if (boxMove) {
		if (position.x >= graphPos.x && position.x <= graphPos.x+graph_width
		 && position.y >= graphPos.y && position.y <= graphPos.y+graph_height) {
			var xOffset=position.x-boxX;
			document.getElementById("graph_box").style.width=xOffset+'px';
			var yOffset=position.y-boxY;
			document.getElementById("graph_box").style.height=yOffset+'px';
		}
	}
}

function onmouseup(ev) { // get new image boundaries, then new image
	ev=ev||window.event;
	if (boxMove) {
		var graph_box=document.getElementById("graph_box");
		var top_x=boxX-graphPos.x;
		var top_y=boxY-graphPos.y;
		var bottom_x=mousePosition.x-graphPos.x;
		var bottom_y=mousePosition.y-graphPos.y;
		if (mousePosition.x-graphPos.x < boxX-graphPos.x) {
			top_x=mousePosition.x-graphPos.x;
			bottom_x=boxX-graphPos.x;
		}
		if (mousePosition.y-graphPos.y < boxY-graphPos.y) {
			top_y=mousePosition.y-graphPos.y;
			bottom_y=boxY-graphPos.y;
		}
		graph_box_top_x=top_x;
		graph_box_top_y=top_y;
		graph_box_bottom_x=bottom_x;
		graph_box_bottom_y=bottom_y
		document.getElementById("zoom_button").style.display="inline";
		boxMove=false;
	}
}

function onmousedown() {
	var position=mousePosition;
	if (position.x >= graphPos.x && position.x <= graphPos.x+graph_width
	 && position.y >= graphPos.y && position.y <= graphPos.y+graph_height) {
		document.getElementById("graph_box").style.left=position.x+'px';
		document.getElementById("graph_box").style.top=position.y+'px';
		document.getElementById("graph_box").style.display="block";
		document.getElementById("graph_box").style.width="0px";
		document.getElementById("graph_box").style.height="0px";
		boxX=position.x;
		boxY=position.y;
		boxMove=true;
	}
}
document.onmousemove=onmousemove;
document.onmouseup=onmouseup;
document.onmousedown=onmousedown;

function get_zoom_boundaries() {
	ajax_call("get_zoom_boundaries", "zoom_in", graph_box_top_x, graph_box_bottom_x, graph_box_top_y, graph_box_bottom_y, document.getElementById("left_boundary").value, document.getElementById("right_boundary").value, document.getElementById("top_boundary").value, document.getElementById("bottom_boundary").value, graph_width, graph_height);
	document.getElementById("zoom_button").disabled="disabled";
}

function zoom_in(boundaries) {
	document.getElementById("left_boundary").value=boundaries["left_boundary"];
	document.getElementById("right_boundary").value=boundaries["right_boundary"];
	document.getElementById("top_boundary").value=boundaries["top_boundary"];
	document.getElementById("bottom_boundary").value=boundaries["bottom_boundary"];
	document.getElementById("zoom_button").disabled='';
	document.getElementById("zoom_button").style.display="none";
	document.getElementById("graph_box").style.display="none";
	update_graph();
}

function get_zoom_in_boundaries() {
	ajax_call("get_zoom_in_boundaries", "zoom_in", document.getElementById("left_boundary").value, document.getElementById("right_boundary").value, document.getElementById("top_boundary").value, document.getElementById("bottom_boundary").value);
}

function get_zoom_out_boundaries() {
	ajax_call("get_zoom_out_boundaries", "zoom_in", document.getElementById("left_boundary").value, document.getElementById("right_boundary").value, document.getElementById("top_boundary").value, document.getElementById("bottom_boundary").value);
}

function zoom_regular() {
	document.getElementById("left_boundary").value=-10;
	document.getElementById("right_boundary").value=10;
	document.getElementById("top_boundary").value=10;
	document.getElementById("bottom_boundary").value=-10;
	update_graph();
}

function find_zero() {
	function_text=document.getElementById('Y'+document.getElementById('Y').value).value;
	ajax_call("find_zero", "show_zero", graph_box_top_x, graph_box_bottom_x, function_text, document.getElementById("left_boundary").value, document.getElementById("right_boundary").value, graph_width, graph_height);
}

function show_zero(results) {
	document.getElementById("zero_x").innerHTML=results;
	document.getElementById("zero").style.display="block";
}