//array to hold the Childeren of a parent
var ChildArray = new Array();
//temp array to holde the childeren of the selected parent
var tempArray = new Array();
var count=1

//adds a child to the child array
function Child(ParentDD, ChildDD, Name) {
	this.ParentDD = ParentDD;
	this.ChildDD = ChildDD;
	this.Name = Name;
}

//child constructor
function SelectedChild(ChildDD, Name) {
	this.ChildDD = ChildDD;
	this.Name = Name;
}

//adds contents of the temparray to the Child select box
function setChild() {
	if (tempArray.length > 0) {
		document.myForm.ChildDD.length = tempArray.length-1
		for(i = 1; i <= tempArray.length-1; i++) {
			var optionName = new Option(tempArray[i].Name, tempArray[i].ChildDD, true, true)
			document.myForm.ChildDD.options[i-1] = optionName
		}
		document.myForm.ChildDD.options[0].selected = 1
	}
	else{
		document.myForm.ChildDD.length = 0
		var optionName = new Option('', '', true, true)
		document.myForm.ChildDD.options[0] = optionName
	}
}

// finds all the childeren associated with the selected parent ID
function findChild(p) {
  var PID = p.options[p.selectedIndex].value
  tempArray.length = 0

  var blank = arguments[1]

  if (typeof blank != "undefined"){
  	tempArray[1] = new SelectedChild('', blank);
	var j = 2
  }
  else
 	  var j = 1

  for (var i=1; i <= ChildArray.length-1; i++){
	if (PID == ChildArray[i].ParentDD) {
		tempArray[j] = new SelectedChild(ChildArray[i].ChildDD, ChildArray[i].Name);
		j++;
	}
  }
 setChild()
}

//only called when the page is refreshed. Set the Child select box equal to the last selected child
function selectChild(TID) {
	for (i = 0; i <= document.myForm.ChildDD.length; i++){
		if (document.myForm.ChildDD.options[i].value == TID) {
			document.myForm.ChildDD.options[i].selected = 1
			i = document.myForm.ChildDD.length
		}
	}
}
