// Thanks be unto http://javascript.internet.com/forms/adding-html-controls.html
// Great code, and I appreciate it
//# Created by: Husay
//# Web Site: http://www.communitxt.net

/*var arrInput = new Array(0);
  var arrInputValue = new Array(0);

function addInput() {
  arrInput.push(arrInput.length);
  arrInputValue.push("");
  display();
}

function display() {
  document.getElementById('parah').innerHTML="";
  for (intI=0;intI<arrInput.length;intI++) {
    document.getElementById('parah').innerHTML+=createInput(arrInput[intI], arrInputValue[intI]);
  }
}

function saveValue(intId,strValue) {
  arrInputValue[intId]=strValue;
}  

function createInput(id,value) {
  return "<input type='text' id='test "+ id +"' onChange='javascript:saveValue("+ id +",this.value)' value='"+ value +"'><br>";
}

function deleteInput() {
  if (arrInput.length > 0) { 
     arrInput.pop(); 
     arrInputValue.pop();
  }
  display(); 
}*/

// Rest of file created by: Shaun Gosse
// Inspiration / general knowledge from: above
// Student #1: <input type="text" name="student1"> 

function htmlEncode(s) {
        var str = new String(s);
        str = str.replace(/&/g, "&amp;");
        str = str.replace(/</g, "&lt;");
        str = str.replace(/>/g, "&gt;");
        str = str.replace(/"/g, "&quot;");
        return str;
}

var arrStudents = new Array(0);

function addStudent() 
{
	 arrInput.push(arrInput.length);
	arrInputValue.push("");
	display();
	document.getElementById('student_count').value++;
}

var arrInput = new Array(0);

var current = 0;

function addInput() {
   arrInput.push(arrInput.length);
  arrInputValue.push("");
  display();
}

// Displays another input, but WITHOUT inserting a blank, default value.
// Value was initialized auto-magically by php
function restoreInput() {
	arrInput.push(arrInput.length);
  display();
}

function display() {
  document.getElementById('students').innerHTML = "";
  for (intI=0;intI<arrInput.length;intI++) {
    document.getElementById('students').innerHTML+=createInput(arrInput[intI], arrInputValue[intI]);
  }
}

function saveValue(intId,strValue) {
	arrInputValue[intId]=strValue;
	//var answer = confirm(strValue);
}  

function createInput(id,value) {
 return "<span style=\"margin-left:3px;margin-right:105px;margin-bottom:15px;\">Student #" + (id+1) + ":</span><input type='text' id='student"+ id +"' name='student" + id + "' onChange='javascript:saveValue("+ id +",this.value)' value=\""+ htmlEncode(value) +"\"><br>";
}

function start() {
	for (var i = 0; i < document.getElementById('student_count').value; i++)
	{
		if (arrInputValue.length > i)
		{
			// Has a value set auto-magically through php earlier
			restoreInput();
		}
		else
		{	// Creates new control
			addInput();
		}
	}
}
/*function deleteInput() {
  if (arrInput.length > 0) { 
     arrInput.pop(); 
     arrInputValue.pop();
  }
  display(); 
}*/