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

//adds a child to the child array
function Child(ParentDD, ChildDD, id) {
	this.ParentDD = ParentDD;
	this.ChildDD = ChildDD;
	this.id = id;
}
function GrandChild(ChildDD, GrandChildDD, id) {
	this.ChildDD = ChildDD;
	this.GrandChildDD = GrandChildDD;
	this.id = id;
}
//child constructor
function SelectedChild(ChildDD, id) {
	this.ChildDD = ChildDD;
	this.id = id;
}
function SelectedGrandChild(GrandChildDD, id) {
	this.GrandChildDD = GrandChildDD;
	this.id = id;
}

//adds contents of the temparray to the Child select box
function setChild() {
	if (tempArray.length > 0) {
		document.dfields2.ChildDD.length = tempArray.length-1
		for(i = 1; i <= tempArray.length-1; i++) {
			var optionName = new Option(tempArray[i].id, tempArray[i].ChildDD, false, false)
			document.dfields2.ChildDD.options[i-1] = optionName
		}
		document.dfields2.ChildDD.options[0].selected = 0
	}
	else{
		document.dfields2.ChildDD.length = 0
		var optionName = new Option('', '', false, false)
		document.dfields2.ChildDD.options[0] = optionName
	}
}
function setGrandChild() {
	if (tempArray2.length > 0) {
		document.dfields2.GrandChildDD.length = tempArray2.length-1
		for(i = 1; i <= tempArray2.length-1; i++) {
			var optionName = new Option(tempArray2[i].id, tempArray2[i].GrandChildDD, false, false)
			document.dfields2.GrandChildDD.options[i-1] = optionName
		}
		document.dfields2.GrandChildDD.options[0].selected = 0
	}
	else{
		document.dfields2.GrandChildDD.length = 0
		var optionName = new Option('', '', false, false)
		document.dfields2.GrandChildDD.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].id);
		j++;
	}
  }
 setChild()
}

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

  var blank = arguments[1]

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

  for (var i=1; i <= GrandChildArray.length-1; i++){
	if (PID == GrandChildArray[i].ChildDD) {
		tempArray2[j] = new SelectedGrandChild(GrandChildArray[i].GrandChildDD, GrandChildArray[i].id);
		j++;
	}
  }
 setGrandChild()
}
function selectGrandChild(TID) {
	for (i = 0; i <= document.dfields2.GrandChildDD.length; i++){
		if (document.dfields2.GrandChildDD.options[i].value == TID) {
			document.dfields2.GrandChildDD.options[i].selected = 1
			i = document.dfields2.GrandChildDD.length
		}
	}
}


//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.dfields2.ChildDD.length; i++){
		if (document.dfields2.ChildDD.options[i].value == TID) {
			document.dfields2.ChildDD.options[i].selected = 1
			i = document.dfields2.ChildDD.length
		}
	}
}
$(function(){
	var boardid;	
	$("select#board").change(function(){
	
		if($(this).val() == 'V') { boardid = "V"; $('#realtor_mysqldb').val('redbvancouver'); }
		if($(this).val() == 'F') {boardid = "F";  $('#realtor_mysqldb').val('redbfraser'); }
		if($(this).val() == 'H') { boardid = "H";  $('#realtor_mysqldb').val('redbchilliwack'); }
		
		
		
		$.getJSON("/php-bin/index.php",{id: boardid, overlord: "SearchCriteria", mysqldb: "redbbc", view: "districts"}, function(j){
		var options = '';
		options += '<option value="">Select Area</option>';
		for (var i = 0; i < j.length; i++) {
		options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
	}
	$("#area").html(options);
	$('#area option:first').attr('selected', 'selected');
		})
	})			
})
$(function(){
	$("select#area").change(function(){
		$.getJSON("/php-bin/index.php",{id: $(this).val(), overlord: "SearchCriteria", mysqldb: "redbbc", view: "communities"}, function(j){
		var options = '';
		options += '<option value="">Select Community</option>';
		for (var i = 0; i < j.length; i++) {
			options += '<option value="' + j[i].optionDisplay + '">' + j[i].optionDisplay + '</option>';
		}
	$("#sa").html(options);
	$('#sa option:first').attr('selected', 'selected');
		})
	})			
})
$(function(){	
	$("select#search_type").change(function(){
		
		$.getJSON("/php-bin/index.php",{id: boardid, overlord: "SearchType", mysqldb: "redbbc", view: "districts"}, function(j){
		var options = '';
		options += '<option value="">Select Type</option>';
		for (var i = 0; i < j.length; i++) {
		options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
	}
	$("#search_style").html(options);
	$('#search_style option:first').attr('selected', 'selected');
		})
	})			
})
$(function(){
	$("select#sa").change(function(){
		$('#neghsubdname').val($(this).val());
	})			
})



