//testing variables -> will be replaced with db data
var detail1 = new Array("1","Cast Riser","Ready");
var detail2 = new Array("101","Cast Riser","Ready");
//global variables
var detailShow = 0;
var saveRequired = false;

function detailButton(gtdnum) {

	//watch for case where one die is open & another is clicked
	if (detailShow != gtdnum && detailShow != 0) {
		if (saveRequired == false) {
			//no priorities have changed; no need to save
			hideDetails(detailShow);
			document.getElementById("details-"+ detailShow).value = "Show Details";
			detailShow = 0;
		}
		else {
			//prompt for save
			if (confirm("Priorities have changed for GTD# "+ detailShow + ". Would you like to save?")) {
				//save details for GTD# in detailShow
				hideDetails(detailShow);
				document.getElementById("details-"+ detailShow).value = "Show Details";
				saveRequired = false;
				saveDetails(gtdnum);
				detailShow = 0;
			}
			else {
				//don't save just hide the old stuff
				hideDetails(detailShow);
				document.getElementById("details-"+ detailShow).value = "Show Details";
				saveRequired = false;
				detailShow = 0;
			}
		}
	}

	if (detailShow == 0) {
		//show details
		detailShow = gtdnum;
		showDetails(gtdnum);
		document.getElementById("details-"+ gtdnum).value = "Hide Details";
	}
	else {
		//hide details
		detailShow = 0;
		hideDetails(gtdnum);
		document.getElementById("details-"+ gtdnum).value = "Show Details";
	}
}

function showDetails(gtdnum) {
	var htmlOutput = new String();

	//table header
	htmlOutput += "<form id=\"detail_form-"+ gtdnum +"\"><table width=\"75%\" border=\"0\" class=\"detail\">\n\t"
	htmlOutput += "<thead class=\"detail-alt\">\n\t\t<tr>\n\t\t<th width=\"5%\">Priority</th><th width=\"10%\">Detail#</th>";
	htmlOutput += "<th width=\"60%\">Detail Name</th><th width=\"25%\">ToolPath Status</th>\n";
	htmlOutput += "\t\t</tr>\n\t</thead>\n";

	//output rows
	//begin loop
	htmlOutput += "<tr>\n<td><input type=\"text\" size=\"2\" maxlength=\"2\" id=\"priority="+ gtdnum + detail1[0] +"\" ";
	htmlOutput += "onchange=\"trackChanges();\" /></td>\n";
	htmlOutput += "<td>"+ detail1[0] +"</td>\n";
	htmlOutput += "<td>"+ detail1[1] +"</td>\n";
	htmlOutput += "<td>"+ detail1[2] +"</td>\n</tr>\n";
	//end loop
	//begin loop
	htmlOutput += "<tr class=\"detail-alt\">\n<td><input type=\"text\" size=\"2\" maxlength=\"2\" id=\"priority="+ gtdnum + detail2[0] +"\" ";
	htmlOutput += "onchange=\"trackChanges();\" /></td>\n";
	htmlOutput += "<td>"+ detail2[0] +"</td>\n";
	htmlOutput += "<td>"+ detail2[1] +"</td>\n";
	htmlOutput += "<td>"+ detail2[2] +"</td>\n</tr>\n";
	//end loop

	//close table
	htmlOutput += "</table>\n";
	htmlOutput += "<input type=\"button\" id=\"save-"+ gtdnum +"\" class=\"actionButton\" value=\"Save "+ gtdnum +"\" ";
	htmlOutput += "onclick=\"saveDie("+ gtdnum +");\" /></form>\n";

	var rewriteRow = document.getElementById("rewrite-"+ gtdnum);
	rewriteRow.innerHTML = htmlOutput;
}

function hideDetails(gtdnum) {
	var htmlOutput = new String();

	//add some error checking such as looking for a necessary save

	htmlOutput = "<form id=\"override_form-"+ gtdnum +"\"><div><label for=\"no-overide-"+ gtdnum +"\">";
	htmlOutput += "<input value=\"0\" type=\"radio\" name=\"overide_options-"+ gtdnum +"\" id=\"no-overide-"+ gtdnum +"\" ";
	htmlOutput += "checked=\"checked\" /> no overide</label> <label for=\"overide-all-"+ gtdnum +"\">";
	htmlOutput += "<input value=\"1\" type=\"radio\" name=\"overide_options-"+ gtdnum +"\" id=\"overide-all-"+ gtdnum +"\" /> ";
	htmlOutput += "overide all</label> <label for=\"overide-hi-" + gtdnum + "\"><input value=\"2\" type=\"radio\" ";
	htmlOutput += "name=\"overide_options-"+ gtdnum +"\" id=\"overide-hi-"+ gtdnum +"\" /> overide high only</label></div>";
	htmlOutput += "<input type=\"button\" id=\"save-"+ gtdnum +"\" class=\"actionButton\" value=\"Save "+ gtdnum +"\" ";
	htmlOutput += "OnClick=\"saveDie("+ gtdnum +");\" /></form>\n";

	var rewriteRow = document.getElementById("rewrite-"+ gtdnum);
	rewriteRow.innerHTML = htmlOutput;
}

function trackChanges() {
	saveRequired = true;
}

function getRadio(gtdnum) {
	//find which radio button is selected
	if (gtdnum == 0) {
		//global override option
		var myRadio = document.getElementById("overide_form").getElementsByTagName("input");
	}
		else {
// 			var myRadio = document.getElementById("overide_form-" + gtdnum).getElementsByTagName("input");
			var myRadio = document.getElementsByName("overide_options-" + gtdnum);
		}
	var returnValue = null;
	for (var i = myRadio.length-1; i > -1 ; i--) {
		if (myRadio[i].checked) {
			returnValue = myRadio[i].value;
		}
	}
	return returnValue;
}

function saveDetails(gtdnum) {
	//save priorities on details
alert("I've saved all the details");
}

function saveDie(gtdnum) {
	//save priorities for an entire die
	if (detailShow != 0) {
		//detail view expanded
		saveDetails(gtdnum);
	}
		else if (gtdnum == 0) {
			//save all button used
			//NOTE: use same formatting on form elements as single dies just make gtdnum = 0
			var overrideOption = getRadio(gtdnum);
			//0 = no override 1 = override all 2 = override high (0-50) only
		}
			else {
				//check override option and save die priorities
				var overrideOption = getRadio(gtdnum);
				//0 = no override 1 = override all 2 = override high (0-50) only
			}
	alert("I've saved the die.");
}

function validDigit(priorityBox, checkEvent) {
	//check validity of entered character
	var validChar = new String('0123456789');

	if (checkEvent.charCode) {
		var enteredKey = checkEvent.charCode;
	}
		else {
			var enteredKey = checkEvent.keyCode;
		}
	if (enteredKey == null || enteredKey == 0 || enteredKey == 8 || enteredKey == 13 || enteredKey == 27) {
		//skip keys like backspace,etc
		return true;
	}
	var enteredChar = String.fromCharCode(enteredKey);
	if (/\d/.test(enteredChar) || checkEvent.ctrlKey) {
		window.status = ""
		return true;
	}
		else {
			alert("Invalid Character!");
			checkEvent.cancelBubble = true;
			checkEvent.returnValue = false;
			return false;
		}
}
