var openBox = false;
var openBoxD = 0;
var openBoxT = 0;
var openBoxDate = "1900-01-01";
var saveBox = false;

var http = getHTTPObject(); // Create the XMLHttpRequest object

function score2(part) {
	if(part == 1) {
		one = parseInt(document.totals.oneval.value);
		two = parseInt(document.totals.twoval.value);
		three = parseInt(document.totals.threeval.value);
		total = parseInt(one + two + three);
		document.totals.aerobics.value="0";
		document.totals.aerobics.value = parseInt(total);		
	}
	if(part == "2") {
		one = parseInt(document.totals.fourval.value);
		two = parseInt(document.totals.fiveval.value);
		three = parseInt(document.totals.sixval.value);
		total = parseInt(one + two + three);
		document.totals.strength.value="0";
		document.totals.strength.value = parseInt(total);	
	}
	if(part == "3") {
		one = parseInt(document.totals.sevenval.value);
		two = parseInt(document.totals.eightval.value);
		three = parseInt(document.totals.nineval.value);
		total = parseInt(one + two + three);
		document.totals.stretch.value="0";
		document.totals.stretch.value = parseInt(total);
	}
}

function setVal2(amount, question) {
	if(question == 1) {
		document.totals.oneval.value = parseInt(amount);
	}
	if(question == 2) {
		document.totals.twoval.value = parseInt(amount);
	}
	if(question == 3) {
		document.totals.threeval.value = parseInt(amount);
	}
	if(question == 4) {
		document.totals.fourval.value = parseInt(amount);
	}
	if(question == 5) {
		document.totals.fiveval.value = parseInt(amount);
	}
	if(question == 6) {
		document.totals.sixval.value = parseInt(amount);
	}
	if(question == 7) {
		document.totals.sevenval.value = parseInt(amount);
	}
	if(question == 8) {
		document.totals.eightval.value = parseInt(amount);
	}
	if(question == 9) {
		document.totals.nineval.value = parseInt(amount);
	}
}

function score(part) {
	if(part == 1) {
		one = parseInt(document.totals.oneval.value);
		two = parseInt(document.totals.twoval.value);
		three = parseInt(document.totals.threeval.value);
		total = parseInt(one + two + three);
		document.totals.result.value="0";
		document.totals.result.value = parseInt(total);		
	}
}

function setVal(amount, question) {
	if(question == 1) {
		document.totals.oneval.value = parseInt(amount);
	}
	if(question == 2) {
		document.totals.twoval.value = parseInt(amount);
	}
	if(question == 3) {
		document.totals.threeval.value = parseInt(amount);
	}
}

function saveDay(id, openBoxD, openBoxT, openBoxDate) {
	var oldID = openBox;
	var oldD = openBoxD;
	var oldT = openBoxT;
	var oldDate = openBoxDate;
	var closeCell = document.getElementById(oldID);
	var openCell = document.getElementById(id);
	
	var oldCellHTML = "";
	oldCellHTML += "Saving...";
	// Retrieve the values from the cell
	var inputs = closeCell.getElementsByTagName("input");
	var oldDInput = inputs[0];
	var oldTInput = inputs[1];
	var saveD = 0;
	var saveT = 0;
	saveD = oldDInput.value;
	saveT = oldTInput.value;
	// Save the values in the old cell
	save(id, oldD, oldT, saveD, saveT, oldDate);
	closeCell.innerHTML = oldCellHTML;
	openBox = false;
}

function createEntryBoxes(id, distance, time, date) {
	if(openBox == false) {
		// if none of the entryboxes have been opened yet, open the first
		openBox = id;
		openBoxD = distance;
		openBoxT = time;
		openBoxDate = date;
		var cell = document.getElementById(id);
		var cellHTML = "";
		cellHTML += "	<input class=\"date-input\" type=\"text\" name=\"distance[]\"  value=\"" + distance + "\"  size=\"2\"/>km<br />\n";
		cellHTML += "	<input class=\"date-input\" type=\"text\" name=\"duration[]\" value=\"" + time + "\" size=\"2\" />mins<br />\n";
		cellHTML += " <a href=\"#\" onclick=\"saveDay(id, openBoxD, openBoxT, openBoxDate); return false;\">Save</a>\n";
		cellHTML += "	<input type=\"hidden\" name=\"date[]\" value=\"$year-$month-$theDay\" />\n";
		cell.innerHTML = cellHTML;	
	} else if(id == openBox) {
		// If the clicked item is already open - shouldn't happen anyway
	} else {
		// An entrybox is already open, so close it, and open the new one
		var oldID = openBox;
		var oldD = openBoxD;
		var oldT = openBoxT;
		var oldDate = openBoxDate;
		var closeCell = document.getElementById(oldID);
		var openCell = document.getElementById(id);
		
		var oldCellHTML = "";
		oldCellHTML += "Saving...";
		// Retrieve the values from the cell
		var inputs = closeCell.getElementsByTagName("input");
		var oldDInput = inputs[0];
		var oldTInput = inputs[1];
		var saveD = 0;
		var saveT = 0;
		saveD = oldDInput.value;
		saveT = oldTInput.value;
		// Save the values in the old cell
		save(id, oldD, oldT, saveD, saveT, oldDate);
		closeCell.innerHTML = oldCellHTML;
		
		var cellHTML = "";
		cellHTML += "	<input class=\"date-input\" type=\"text\" name=\"distance[]\"  value=\"" + distance + "\"  size=\"2\"/>km<br />\n";
		cellHTML += "	<input class=\"date-input\" type=\"text\" name=\"duration[]\" value=\"" + time + "\" size=\"2\" />mins<br />\n";
		cellHTML += " <a href=\"#\" onclick=\"saveDay(id, openBoxD, openBoxT, openBoxDate); return false;\">Save</a>\n";
		cellHTML += "	<input type=\"hidden\" name=\"date[]\" value=\"$year-$month-$theDay\" />\n";
		openCell.innerHTML = cellHTML;
		
		openBox = id;
		openBoxD = distance;
		openBoxT = time;
		openBoxDate = date;
	}
}


function undoSave(id, undoD, undoT, date) {
		var url = "/walkingworkout/journal/async/?save="+date+"&oldD=-1&oldT=-1&saveD="+undoD+"&saveT="+undoT+"&id="+id;
		http.open("GET", url, true);
		//alert(url);
		http.onreadystatechange = handleSaveResponse;
		http.send(null);
}

function save(id, oldD, oldT, saveD, saveT, date) {
	var saveCell = document.getElementById(id);
	var url = "/walkingworkout/journal/async/?save="+date+"&oldD="+oldD+"&oldT="+oldT+"&saveD="+saveD+"&saveT="+saveT+"&id="+openBox;
	//alert(url);
	http.open("GET", url, true);
	http.onreadystatechange = handleSaveResponse;
	http.send(null);
}

function handleSaveResponse() {
	if(http.readyState==4) {
		var responseArray = http.responseText.split('|');
		//alert(responseArray);
		if(responseArray[0] == 1) {
			updateCell(responseArray[5], responseArray[1], responseArray[2], responseArray[3], responseArray[4], responseArray[6]);	
		} else {
			saveFailure(responseArray[5], responseArray[1], responseArray[2], responseArray[3], responseArray[4], responseArray[6]);
		}
	}
}

function saveFailure(cellId, newDistance, newTime, undoDistance, undoTime, date) {
	var cell = document.getElementById(cellId);
	var cellHTML = "";
	cellHTML += "<a href=\"#\" onclick=\"createEntryBoxes('" + cellId + "', '" + undoDistance + "', '" + undoTime + "', '"+date+"'); return false;\">";
	cellHTML += "		"+ newDistance +" km<br />" + newTime + " mins";
	cellHTML += "</a>";
	cellHTML += "Save Error";
	cell.innerHTML = cellHTML;
}

function updateCell(cellId, newDistance, newTime, undoDistance, undoTime, date) {
	var cell = document.getElementById(cellId);
	var cellHTML = "";
	cellHTML += "<a href=\"#\" onclick=\"createEntryBoxes('" + cellId + "', '" + newDistance + "', '" + newTime + "', '"+date+"'); return false;\">";
	cellHTML += "		"+ newDistance +" km<br />" + newTime + " mins";
	cellHTML += "</a>";
	if((undoDistance != -1 && undoTime != -1) && (newDistance != undoDistance || newTime != undoTime)) {
		cellHTML += "<br /><a href=\"#\" onclick=\"undoSave('" + cellId + "', '" + undoDistance + "', '" + undoTime + "', '"+date+"'); return false;\">";
		cellHTML += "Undo";
		cellHTML += "</a>";
	}
	cell.innerHTML = cellHTML;
	
	//var testdiv = document.getElementById('testdiv');
	//testdiv.innerHTML = "<pre><code>" + cellHTML + "</code></pre>";
}

function getHTTPObject() {
  var xmlhttp;
  /*@cc_on
  @if (@_jscript_version >= 5)
    try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
        xmlhttp = false;
      }
    }
  @else
  xmlhttp = false;
  @end @*/
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    try {
      xmlhttp = new XMLHttpRequest();
    } catch (e) {
      xmlhttp = false;
    }
  }
  return xmlhttp;
}
