// JavaScript Document

function setup_form_fieldsets(){

	var droprow = document.getElementById("li--6");
	var droplist = document.getElementById("cf_field_6");

	// Check if the user comes from a products page first
	preset = decodeURI(window.location.search.substring(1));
	
	if (preset == "Generators"){
		// show the correct fieldset
		getElementByClass("cf-fs2","show");
		getElementByClass("cf-fs3","hide");
		// manipulate the select box
		droplist.selectedIndex = 1;
		// set the value before hiding the selectbox
		droprow.style.display = 'none';
	} else if (preset == "Power Distribution Boards"){
		// show the correct fieldset
		getElementByClass("cf-fs2","hide");
		getElementByClass("cf-fs3","show");
		// manipulate the select box
		droplist.selectedIndex = 2;
		// set the value before hiding the selectbox
		droprow.style.display = 'none';
	} else {
		getElementByClass("cf-fs2","hide");
		getElementByClass("cf-fs3","hide");
		droplist.selectedIndex = 0;
	}

	add_droplist_listener();

} 

function add_droplist_listener() {

	var droplist = document.getElementById("cf_field_6");
	droplist.onchange = showhide_fieldsets;

}

function showhide_fieldsets() { 

	var droplist = document.getElementById("cf_field_6");
	var dropvalue = droplist.value;

	if (dropvalue == "-"){
		getElementByClass("cf-fs2","hide");
		getElementByClass("cf-fs3","hide");
	} else if (dropvalue == "Generators"){
		getElementByClass("cf-fs2","show");
		getElementByClass("cf-fs3","hide");
	} else if (dropvalue == "Power Distribution Boards"){
		getElementByClass("cf-fs2","hide");
		getElementByClass("cf-fs3","show");
	}

}

/* getElementByClass
/**********************/

var allHTMLTags = new Array();

function getElementByClass(theClass,theAction) {

	//Create Array of All HTML Tags
	var allHTMLTags = document.getElementsByTagName('*');
	
	//Loop through all tags using a for loop
	for (i=0; i<allHTMLTags.length; i++) {
	
	//Get all tags with the specified class name.
		if (allHTMLTags[i].className == theClass) {
		
			if (theAction == "hide"){
				allHTMLTags[i].style.display = 'none';
			} else if (theAction == "show"){
				allHTMLTags[i].style.display = 'block';
			}

		}
	}
}
