var sendtimeout;
var shift;
var inputbig = false;

function clearInput() {
	document.getElementById('calcinput').value = "";
}

function clearResults() {
	$("#results").html("");
}

function allClear() {
	clearInput();
	clearResults();
}

function addStat() {
	document.getElementById('calcinput').value = "="+document.getElementById('calcinput').value;
	sendData();
}

function backspace() {
	checkInputLength()
	currinput = document.getElementById('calcinput').value;
	currinput = currinput.substr(0, currinput.length - 1);
	document.getElementById('calcinput').value = currinput;
}

function closeHelp() {
	$("#results").show();
	$("#help").hide();
}

function toggleHelp() {
	$("#funclist").hide();
	if ($("#help:visible").length == 0) {
		$("#results").hide();
		$("#help").show();
		getHelp("index.html");
	}
	else {
		$("#results").show();
		$("#help").hide();
	}
}

function toggleFuncList() {
	$("#help").hide();
	if ($("#funclist:visible").length == 0) {
		$("#results").hide();
		$("#funclist").show();
	}
	else {
		$("#results").show();
		$("#funclist").hide();
	}
}

function funcappend(str) {
	$("#funclist").toggle();
	$("#results").toggle();
	append(str);
}

function getHelp(filename) {
	$("#results").hide();
	$("#help").show();
	$("#help").html("Loading...");
	$.get("help/"+filename, {sid : Math.random()}, 
		function(response) {
			$("#help").html(response);
			var objDiv = document.getElementById("results");
			objDiv.scrollTop = objDiv.scrollHeight;
		});
}

function checkInputLength() {
	if (!$.browser.msie) {
		var userinput = document.getElementById('calcinput').value;
		if (userinput.length > 19 && inputbig == false) {
			$("#calcinput").animate({
				"width": "580px"
			}, 800);
			$(".sidebar").animate({
				"height": "270px",
				"marginTop": "85px"
			}, 800);
			$("#results").animate({
				"height": "270px",
				"marginTop": "85px"
			}, 800);
			inputbig = true;
		}
		else 
			if (userinput.length <= 19 && inputbig == true) {
				$("#calcinput").animate({
					"width": "330px"
				}, 800);
				$(".sidebar").animate({
					"height": "350px",
					"marginTop": "0px"
				}, 800);
				$("#results").animate({
					"height": "350px",
					"marginTop": "0px"
				}, 800);
				inputbig = false;
			}
	}
}

function toggleShift() {
	if (shift == true) {
		shift = false;
		$("#shiftkey").css('background', '#FFE187');
	}
	else {
		shift = true;
		$("#shiftkey").css('background', '#FFCD3A');
	}
}

function sendData() {
	closeHelp();
	$("#funclist").hide();
	var userinput = document.getElementById('calcinput').value;
	clearInput();
	append("Loading...");
	window.location = "#"+escape(userinput);
	$("#calcinput").animate({
		"width": "330px"}, 800);
	$(".sidebar").animate({
		"height": "350px", "marginTop": "0px"}, 800);
	$("#results").animate({
		"height": "350px", "marginTop": "0px"}, 800);
	inputbig = false;
	$.get("process.php", { q : userinput, sid : Math.random()}, 
		function(response) {
			clearInput();
			$("#results").html($("#results").html()+response);
			userinput.replace("+", "%2B")
			var objDiv = document.getElementById("results");
			objDiv.scrollTop = objDiv.scrollHeight;
		});
}

function setPref(prefname, value, group) {
	$(".pref_"+group).removeClass('active');
	$("#pref_"+group+"_"+value).addClass('active');
	$.get("process.php", { 'action' : 'setpref', 'pref' : prefname, 'value' : value, 'sid' : Math.random()}, 
		function(response) {
			if(response !== "1")
				alert("Could not save the setting.");
		});
}

function togglePref(prefname) {
	if ($("#pref_" + prefname).hasClass('active')) {
		value = "0";
		$("#pref_" + prefname).removeClass('active');
	}
	else {
		value = "1";
		$("#pref_" + prefname).addClass('active');
	}
	$.get("process.php", { 'action' : 'setpref', 'pref' : prefname, 'value' : value, 'sid' : Math.random()}, 
		function(response) {
			if(response !== "1")
				alert("Could not save the setting.");
		});
}

function addMemory() {
	input = $("#memoryentry").val();
	$.get("process.php", { action : "addmem", q : input, sid : Math.random()}, 
		function(response) {
			$("#overlay").hide();
			$("#overlaytext").hide();
			if(response != "0") {
				$("#memorytext").html(response);
			}
		});
}

function removeMemory(name) {
	$("#overlay").show();
	$("#overlaytext").show();
	$.get("process.php", { action : "removemem", q : name, sid : Math.random()}, 
		function(response) {
			$("#overlay").hide();
			$("#overlaytext").hide();
			if(response != "0") {
				$("#memorytext").html(response);
			}
		});
}

function isNumeric(sText) {
	var ValidChars = "0123456789.";
	var IsNumber=true;
	var Char;
	
	for (i = 0; i < sText.length && IsNumber == true; i++) { 
		Char = sText.charAt(i); 
		if (ValidChars.indexOf(Char) == -1) {
		IsNumber = false;
	}
}
return IsNumber;
}


// Returns anchor from link string
function returnHash() {
	var hash = unescape(window.location.hash);
	return hash.substring(1); // remove #
}


function append(str){
	$("#calcinput").val($("#calcinput").val() + str);
	checkInputLength()
}

function button(str, strshift) {
	if (shift == true) {
		shift = false;
		append(strshift);
		$("#shiftkey").css('background', '#FFE187');
	}
	else {
		append(str);
	}
}

/**
 * This function detects when the enter key is pressed in the input area, 
 * and submits the form if so.
 * @param {event} e
 */
function keypress(e, action) { //e is the event object passed to the function
	checkInputLength()
    var characterCode;	//This is the variable which will be used to store the character code
    if (e && e.which) { //if which property of event object is supported (Firefox, Opera, Safari, etc.)
        characterCode = e.which; //Find the character code contaned in the which property
    }
    else {
        e = event
        characterCode = e.keyCode; //Stupid IE - use IE's keyCode property
    }
    //13 is the ascii enter key - therefore if the key is set to 13 the enter key was pressed.
    if (characterCode == 13) { 
		if(action == "memory")
			addMemory()
		else
        	sendData(); //Submit the query
        return false;
    }
	return true;
}

$(document).ready(function() {
	hash = returnHash(window.location);
	if (hash != "") {
		append(hash);
		sendData();
	}
	if (navigator.appName == "Microsoft Internet Explorer") {
		$('div.button').click(function(){
			$.sound.play("click2.wav");
		});
	}
});
