/* This script and many more are available free online at
The JavaScript Source!! http://javascript.internet.com
Created by: Bobby van der Sluis | http://www.bobbyvandersluis.com/ */
// Details can be found at:
// http://www.bobbyvandersluis.com/articles/unobtrusivedynamicselect.php

function dynamicSelect(id1, id2, id3) {
 	// Feature test to see if there is enough W3C DOM support
 	if (document.getElementById && document.getElementsByTagName) {
  		// Obtain references to both select boxes
  		var sel1 = document.getElementById(id1);
		
		
  		var sel2 = document.getElementById(id2);
  		var sel3 = document.getElementById(id3);
  		// Clone the dynamic select box
  		var clone = sel2.cloneNode(true);
  		var clone2 = sel3.cloneNode(true);
  		// Obtain references to all cloned options
  		var clonedOptions = clone.getElementsByTagName("option");
  		var clonedOptions2 = clone2.getElementsByTagName("option");
  		// Onload init: call a generic function to display the related options in the dynamic select box
  		
  		// Onchange of the main select box: call a generic function to display the related options in the dynamic select box
  		sel1.onchange = function() {
   			refreshDynamicSelectOptions(sel1, sel2, clonedOptions);
  			refreshDynamicSelectOptions(sel1, sel3, clonedOptions2);
  		};
		sel2.onchange = function() {
   			refreshDynamicSelectOptions2(sel1, sel2, sel3, clonedOptions2);
			if(sel2.options[0].selected){
				refreshDynamicSelectOptions(sel1, sel3, clonedOptions2);
			}
  		};
		
 	}
}
function dynamicSelect2(id1, id2) {
 	// Feature test to see if there is enough W3C DOM support
 	if (document.getElementById && document.getElementsByTagName) {
  		// Obtain references to both select boxes
  		var sel1 = document.getElementById(id1);
		
		
  		var sel2 = document.getElementById(id2);
  		// Clone the dynamic select box
  		var clone = sel2.cloneNode(true);
  		// Obtain references to all cloned options
  		var clonedOptions = clone.getElementsByTagName("option");
  		// Onload init: call a generic function to display the related options in the dynamic select box
  		
  		// Onchange of the main select box: call a generic function to display the related options in the dynamic select box
  		sel1.onchange = function() {
   			refreshDynamicSelectOptions(sel1, sel2, clonedOptions);
  		};
 	}
}

function refreshDynamicSelectOptions(sel1, sel2, clonedOptions) {
 	// Delete all options of the dynamic select box
	sel2.options.length=0;
	var selected=0;
	sel2.appendChild(clonedOptions[0].cloneNode(true));
	for(var i0=0;i0<sel1.options.length;i0++){
	if(sel1.options[i0].selected || sel1.options[i0].value==''){
	if(sel1.options[i0].value!=''){
	selected=1;
	}
 	// Create regular expression objects for "select" and the value of the selected option of the main select box as class names
 	var pattern1 = /( |^)(select)( |$)/;
 	var pattern2 = new RegExp("( |^)(" + sel1.options[i0].id + ")( |$)");
 	// Iterate through all cloned options
	
 	for (var i = 1; i < clonedOptions.length; i++) {
  		// If the classname of a cloned option either equals "select" or equals the value of the selected option of the main select box
  		if (clonedOptions[i].className.match(pattern1) || clonedOptions[i].className.match(pattern2)) {
 	 		 // Clone the option from the hidden option pool and append it to the dynamic select box
  		 	sel2.appendChild(clonedOptions[i].cloneNode(true));
  		}
 	}
	}
	}
	
	if(selected==0){
	
	
	
	// Iterate through all cloned options
 	for (var i = 0; i < clonedOptions.length; i++) {
		
  		// If the classname of a cloned option either equals "select" or equals the value of the selected option of the main select box
  				 // Clone the option from the hidden option pool and append it to the dynamic select box
  		 	sel2.appendChild(clonedOptions[i].cloneNode(true));
  		
 	}
	sel2.remove(0);
	}
}
function refreshDynamicSelectOptions2(sel1, sel2, sel3, clonedOptions) {
	
 	// Delete all options of the dynamic select box
	
	sel3.options.length=0;
	
	sel3.appendChild(clonedOptions[0].cloneNode(true));
	for(var i0=0;i0<sel2.options.length;i0++){
		if(sel2.options[i0].selected || sel2.options[i0].value==''){
		if(sel2.options[i0].value!=''){
		var selected=1;
		}
		// Create regular expression objects for "select" and the value of the selected option of the main select box as class names
		var pattern1 = /( |^)(select)( |$)/;
		var pattern2 = new RegExp("( |^)(" + sel2.options[i0].id + ")( |$)");
		// Iterate through all cloned options
		
		for (var i = 1; i < clonedOptions.length; i++) {
			
			// If the classname of a cloned option either equals "select" or equals the value of the selected option of the main select box
			if (clonedOptions[i].className.match(pattern1) || clonedOptions[i].className.match(pattern2)) {
				 // Clone the option from the hidden option pool and append it to the dynamic select box
			var selected1=0;
			for (var i2 = 0; i2 < sel1.options.length; i2++) {
				if(sel1.options[i2].selected || sel1.options[0].selected){
				selected1=1;
				var pattern3 = new RegExp("( |^)(" + sel1.options[i2].id + ")( |$)");
				if (clonedOptions[i].className.match(pattern1) || clonedOptions[i].className.match(pattern3)) {
					
					sel3.appendChild(clonedOptions[i].cloneNode(true));
				}
				}
			}
			if(selected1==0) {
				sel3.appendChild(clonedOptions[i].cloneNode(true));
			}
		}
		}
	}
	}
	
	if(!selected){
	
	// Iterate through all cloned options
 	for (var i = 0; i < clonedOptions.length; i++) {
  		// If the classname of a cloned option either equals "select" or equals the value of the selected option of the main select box
  				 // Clone the option from the hidden option pool and append it to the dynamic select box
  		 	sel3.appendChild(clonedOptions[i].cloneNode(true));
  		
 	}
	sel3.remove(0);
	}
}
// Multiple onload function created by: Simon Willison
// http://simon.incutio.com/archive/2004/05/26/addLoadEvent
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}


