/* Rob's Easy Budget Tool 
*  http://www.edgerob.net/budget
*
*  This code is freely distributable for educational and non-commercial use.
*/

var xh

function mouseX(evt) {
if (evt.pageX) return evt.pageX;
else if (evt.clientX)
   return evt.clientX + (document.documentElement.scrollLeft ?
   document.documentElement.scrollLeft :
   document.body.scrollLeft);
else return null;
}
function mouseY(evt) {
if (evt.pageY) return evt.pageY;
else if (evt.clientY)
   return evt.clientY + (document.documentElement.scrollTop ?
   document.documentElement.scrollTop :
   document.body.scrollTop);
else return null;
}

function doLogin()
{
	var user = document.forms['login'].email.value;
	var pass = document.forms['login'].password.value;
	document.forms['login'].sub.value = "Please wait ...";
	document.forms['login'].sub.disabled = true;
	
	pass = md5(pass);
	
	var url = "function.php?action=login&user="+user+"&pass="+pass;
	xh = GetxhObject();
	xh.onreadystatechange = function()
	{
		if (xh.readyState == 4)
		{
			if (xh.responseText == "1")
			{
				document.getElementById("loginerror").style.display = "block";
				document.forms['login'].sub.value = "Login";
				document.forms['login'].sub.disabled = false;
			} else if (xh.responseText == "0")
			{
				 window.location = "budget.php";
			} else {
				alert("Unknown error, probably SQL related");
				document.forms['login'].sub.value = "Login";
				document.forms['login'].sub.disabled = false;
			}
		}
	};	
	xh.open("GET", url, true);
	xh.send(null);	
}

function doRegister()
{
	var name = document.forms['register'].name.value;
	var email = document.forms['register'].email.value;
	var pass = document.forms['register'].pass.value;
	var verify = document.forms['register'].verify.value;
	
	if (pass != verify)
	{
		alert("Passwords do not match.");
		return;
	}

	pass = md5(pass);
	
	document.forms['register'].sub.value = "Please wait ...";
	document.forms['register'].sub.disabled = true;
	var url = "function.php?action=register&email="+email+"&pass="+pass+"&name="+name;
	xh = GetxhObject();
	xh.onreadystatechange = function()
	{
		if (xh.readyState == 4)
		{
			if (xh.responseText == "1")
			{
				document.getElementById("registererror").style.display = "block";
				document.forms['register'].sub.value = "Submit";
				document.forms['register'].sub.disabled = false;
			} else if (xh.responseText == "0")
			{
				 window.location = "register.php?success=true";
			} else {
				alert("Unknown error, probably SQL related");
				document.forms['register'].sub.value = "Submit";
				document.forms['register'].sub.disabled = false;
			}
		}
	};	
	xh.open("GET", url, true);
	xh.send(null);		
	
}

function resetPw()
{
	var email = document.forms['reset'].email.value;

	document.forms['reset'].sub.value = "Please wait ...";
	document.forms['reset'].sub.disabled = true;	
	
	var url = "function.php?action=reset&email="+email;
	xh = GetxhObject();
	xh.onreadystatechange = function()
	{
		if (xh.readyState == 4)
		{
			if (xh.responseText == "1")
			{
				document.getElementById("reseterror").style.display = "block";
				document.forms['reset'].sub.value = "Submit";
				document.forms['reset'].sub.disabled = false;
			} else if (xh.responseText == "0")
			{
				 window.location = "resetpw.php?success=true";
			} else {
				alert(xh.responseText);
				document.forms['reset'].sub.value = "Submit";
				document.forms['reset'].sub.disabled = false;
			}
		}
	};	
	xh.open("GET", url, true);
	xh.send(null);	
}

function changePw()
{
	var pass = document.forms['change'].pass.value;
	var verify = document.forms['change'].verify.value;
	
	if (pass != verify)
	{
		alert("New passwords do not match.");
		return;
	}
	
	pass = md5(pass);

	document.forms['change'].sub.value = "Please wait ...";
	document.forms['change'].sub.disabled = true;
	var url = "function.php?action=changepw&pass="+pass;
	xh = GetxhObject();
	xh.onreadystatechange = function()
	{
		if (xh.readyState == 4)
		{
			if (xh.responseText == "0")
			{
				document.getElementById("changesuccess").style.display = "block";
			} else {
				alert("Unknown error, probably SQL related");
			}
				document.forms['change'].sub.value = "Submit";
				document.forms['change'].sub.disabled = false;			
		}
	};	
	xh.open("GET", url, true);
	xh.send(null);		
	
}

function doPost()
{
	var name = document.forms['newpost'].name.value;
	var subject = document.forms['newpost'].subject.value;
	var message = document.forms['newpost'].message.value;
	
	if (!name || !subject || !message)
	{
		alert("Missing information in one or more fields.");
		return;
	}

	document.forms['newpost'].sub.value = "Please wait ...";
	document.forms['newpost'].sub.disabled = true;
	var url = "function.php?action=post&name="+name+"&subject="+subject+"&message="+message;
	
	xh = GetxhObject();
	xh.onreadystatechange = function()
	{
		if (xh.readyState == 4)
		{
			if (xh.responseText == "1")
			{
				alert("Not logged in?");
				return;
			}
			
			document.getElementById("forum").innerHTML = xh.responseText;
			document.forms['newpost'].sub.value = "Post";
			document.forms['newpost'].sub.disabled = false;			
		}
	};	
	xh.open("GET", url, true);
	xh.send(null);		
	
}

function showPage(p)
{
	var url = "function.php?action=fillforum&page="+p;
	
	xh = GetxhObject();
	xh.onreadystatechange = function()
	{
		if (xh.readyState == 4)
		{
			if (xh.responseText == "1")
			{
				alert("Problem with that page");
				return;
			}
			
			document.getElementById("forum").innerHTML = xh.responseText;		
		}
	};	
	xh.open("GET", url, true);
	xh.send(null);	
}

function deletePost(id, event)
{
	var elem = document.getElementById("areyousure");
	
	elem.style.top = mouseY(event) + "px";
	elem.style.left = mouseX(event) + "px";
	elem.style.display = "block";
	elem.innerHTML = 'Really delete this post?<br><form name=areyousure><input type=button name=sub onclick="doDelPost(1, '+id+')" value="Yes"><input type=button onclick="doDelPost(0, '+id+')" value="No"></form>';
}

function doDelPost(yes, id)
{
	if (yes)
	{
		document.forms["areyousure"].sub.value = "Please wait ...";
		document.forms["areyousure"].sub.disabled = true;
		
		xh=GetxhObject();
		var url = "function.php?action=delpost&id="+id;
	
		xh.onreadystatechange = function()
		{
			if (xh.readyState==4)
			{
				document.getElementById("forum").innerHTML = xh.responseText;
			}
		};
		
		xh.open("GET", url, true);
		xh.send(null);
	}
	
	document.getElementById("areyousure").style.display = "none";
}

function totalClick(tot)
{
	document.getElementById("totalcontainer").innerHTML = "$<input type=\"text\" id=\"newtotal\" onchange=\"totalChange()\" size=5 value=\""+tot+"\" />";
}

function totalChange()
{
	var newtotal = document.getElementById("newtotal").value;
		
	numbers = /\d{1,}/	
	if (!numbers.test(newtotal))
	{
		alert("Please enter a valid amount.");
		return;
	}
	
	var url = "function.php?action=updatetotal&total="+newtotal;
	xh = GetxhObject();
	xh.onreadystatechange = function()
	{
		if (xh.readyState == 4)
		{
			fillTotal();
		}
	};	
	xh.open("GET", url, true);
	xh.send(null);
}

function fillTotal()
{
	var url = "function.php?action=filltotal";
	xh = GetxhObject();
	xh.onreadystatechange = function()
	{
		if (xh.readyState == 4)
		{
			document.getElementById("totalbox").innerHTML = xh.responseText;
		}
	};	
	xh.open("GET", url, true);
	xh.send(null);
}	


function scrollBottom(cat)
{
	var objDiv = document.getElementById(cat+"_body");
	objDiv.scrollTop = objDiv.scrollHeight;
}

function showHide(cat)
{
	var whichLayer = cat+"_content";
 	var elem, vis;
 	
 	if( document.getElementById ) // this is the way the standards work
 		elem = document.getElementById( whichLayer );
 	else if( document.all ) // this is the way old msie versions work
 		elem = document.all[whichLayer];
 	else if( document.layers ) // this is the way nn4 works
 		elem = document.layers[whichLayer];
 	vis = elem.style;
 
 	// if the style.display value is blank we try to figure it out here
 	if(vis.display==''&&elem.offsetWidth!=undefined&&elem.offsetHeight!=undefined)
 		vis.display = (elem.offsetWidth!=0&&elem.offsetHeight!=0)?'block':'none';
 		
 	vis.display = (vis.display==''||vis.display=='block')?'none':'block'; 
	
	if (vis.display == 'block')
		document.getElementById(cat+"_rd").src = "d.gif";
	else
		document.getElementById(cat+"_rd").src = "r.gif";
		
	scrollBottom(cat)
}

function addTrans(cat)
{	
	var form = "addtrans_"+cat;
	var type = document.forms[form].type.value;
	var amount = document.forms[form].amount.value;
	var date = document.forms[form].date.value;
	var description = document.forms[form].description.value;	
	
	numbers = /\d{1,}/	
	if (!numbers.test(amount))
	{
		alert("Please enter a valid amount.");
		return;
	}
	
	document.forms[form].sub.disabled = true;
	document.forms[form].sub.value = "Please wait ...";
	
	var url = "function.php?action=addtrans&cat="+cat+"&type="+type+"&amount="+amount+"&date="+date+"&description="+description;
	xh = GetxhObject();
	
	xh.onreadystatechange = function()
	{
		if (xh.readyState == 4)
		{
			if (xh.status == 200)
			{
				document.getElementById(cat + "_body").innerHTML = xh.responseText;
				document.forms[form].sub.disabled = false;
				document.forms[form].sub.value = "Add";
				document.forms[form].amount.value = "";
				document.forms[form].description.value = "";
				scrollBottom(cat);
				fillTotal();
			} else
			{
				alert("There was a problem retrieving the XML data:\n" + xh.statusText);
			}
		}
	};
	
	xh.open("GET", url, true);
	xh.send(null);
}

function addCat()
{	
	var name = document.forms["newcat"].name.value;
	if (name == "")
	{
		alert ("Please enter a valid category name.");
		return;
	}

	document.forms["newcat"].sub.value = "Please wait ...";
	document.forms["newcat"].sub.disabled = true;
	
	xh=GetxhObject();
	if (xh==null)
	{
		alert ("Your browser does not support AJAX!");
		return; 
	}	
	
	// check for existing cat?
	
	var url = "function.php?action=addcat&name="+name;

	xh.onreadystatechange = function()
	{
		if (xh.readyState==4)
		{
			document.getElementById("cats").innerHTML = xh.responseText;
			document.forms["newcat"].sub.disabled = false;
			document.forms["newcat"].sub.value = "Add Category";
			document.forms["newcat"].name.value = "";
			fillTotal();
		}
	};
	
	xh.open("GET", url, true);
	xh.send(null);
}

function delTrans(cat, id, event)
{
	var elem = document.getElementById("areyousure");
	
	elem.style.top = mouseY(event) + "px";
	elem.style.left = mouseX(event) + "px";
	elem.style.display = "block";
	elem.innerHTML = 'Really delete this transaction?<br><form name=areyousure><input type=button name=sub onclick="doDelTrans(1, '+cat+', '+id+')" value="Yes"><input type=button onclick="doDelTrans(0, '+cat+', '+id+')" value="No"></form>';
}

function doDelTrans(yes, cat, id)
{
	
	if (yes)
	{
		document.forms["areyousure"].sub.value = "Please wait ...";
		document.forms["areyousure"].sub.disabled = true;
		
		xh=GetxhObject();
		if (xh==null)
		{
			alert ("Your browser does not support AJAX!");
			return; 
		}	
		
		var url = "function.php?action=deltrans&cat="+cat+"&id="+id;
	
		xh.onreadystatechange = function()
		{
			if (xh.readyState==4)
			{
				document.getElementById(cat + "_body").innerHTML = xh.responseText;
				fillTotal();
			}
		};
		
		xh.open("GET", url, true);
		xh.send(null);
	}
	
	document.getElementById("areyousure").style.display = "none";
}

function delCat(id, event)
{
	var elem = document.getElementById("areyousure");
	
	elem.style.top = mouseY(event) + "px";
	elem.style.left = mouseX(event) + "px";
	elem.style.display = "block";
	elem.innerHTML = 'Really delete this category?<br><form name=areyousure><input type=button name=sub onclick="doDelCat(1, '+id+')" value="Yes"><input type=button onclick="doDelCat(0, '+id+')" value="No"></form>';
}

function doDelCat(yes, id)
{	
	if (yes)
	{
		document.forms["areyousure"].sub.value = "Please wait ...";
		document.forms["areyousure"].sub.disabled = true;
		
		xh=GetxhObject();
		if (xh==null)
		{
			alert ("Your browser does not support AJAX!");
			return; 
		}	
		
		var url = "function.php?action=delcat&id="+id;
	
		xh.onreadystatechange = function()
		{
			if (xh.readyState==4)
			{
				document.getElementById("cats").innerHTML = xh.responseText;
				fillTotal();
			}
		};
		
		xh.open("GET", url, true);
		xh.send(null);
	}
	
	document.getElementById("areyousure").style.display = "none";
}

function GetxhObject()
{
	xh=null;
	try
	{
		// Firefox, Opera 8.0+, Safari
		xh=new XMLHttpRequest();
	}
	catch (e)
	{
		// Internet Explorer
		try
		{
			xh=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			xh=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	if (xh==null)
	{
		alert ("Your browser does not support AJAX!");
		return; 
	}
	
	return xh;
}

function randomString() {
	var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
	var string_length = 8;
	var randomstring = '';
	for (var i=0; i<string_length; i++) {
		var rnum = Math.floor(Math.random() * chars.length);
		randomstring += chars.substring(rnum,rnum+1);
	}
	document.randform.randomfield.value = randomstring;
}