// JavaScript Document
// script by Pony Smith :: pony_smith@yahoo.com



// --------------------------------------------- SELECT IMAGE ------------------------------------------------------- //
//
// function to select an image to use
function selectImg(obj_id,field_id,selfClose) {
	self.opener.updateField(field_id,obj_id);
	self.close();
	self.opener.focus();
}


function updateField(field_id,obj_id) {
	changeField = document.getElementById(field_id);
	changeField.value = obj_id;
}





// --------------------------------------------- TOGGLE VISIBILITY ------------------------------------------------------- //
//
// function to toggle visibility
function toggleVisible(id) {
	obj = document.getElementById(id);
	if(obj.style.display == 'none') {
		obj.style.display = 'block';
	} else {
		obj.style.display = 'none';
	}
}


// --------------------------------------------- SHOW / HIDE ------------------------------------------------------- //
//
// function to show / hide elements
function show_hide(show,hide) {
	if(show != '') {
		var show_ids = show.split(',');
		for(s=0; s<show_ids.length; s++) {
			var s_div = show_ids[s];
			document.getElementById(s_div).style.display = 'block';
		}
	}
	if(hide != '') {
		var hide_ids = hide.split(',');		
		for(h=0; h<hide_ids.length; h++) {
			var h_div = hide_ids[h];
			document.getElementById(h_div).style.display = 'none';
		}
	}
}




// --------------------------------------------- DYNAMIC DROP DOWN LISTS -------------------------------------------- //
// 
// functions for dynamic <select> boxes

// this function is called from within the html (make sure the original dropdown is loaded before calling)
// sets the global variables and clones the master <select> list(s) for later use
function initDynBox(sBoxId, dBoxId) {
	// make sure the appropriate JS are available
	if(document.getElementById && document.getElementsByTagName) {
		// set global variables for the static <select> box and the dynamic <select> box
		sBox = document.getElementById(sBoxId);
		dBox = document.getElementById(dBoxId);
		// clone the dynamic <select> box
		clone = dBox.cloneNode(true);
		// create an array of all the cloned <option> tags
		optClones = clone.getElementsByTagName('option');
		populate();
	}
}

// called onchange() of the static <select> box
function populate(selAll) {
	// recall the global variables .. global variables are assigned as properties of 'window'
	sBox = window.sBox;
	dBox = window.dBox;
	optClones = window.optClones;
	
	// remove all <options> from the dynamic <select> box
	while(dBox.length > 0) {
		dBox.remove(0);
	}
	// set variable for the value of the chosen static item
	subClass = sBox.value;
	
	// loop through all <option> tags in the clone array
	for(i=0; i<optClones.length; i++) {
		// if the default is selected add all items to the clone
		if(subClass == '*' || subClass == '' || subClass == 'default') {
			dBox.appendChild(optClones[i].cloneNode(true));
		
		// otherwise check to see if the class matches the one defined above or if it labeled as class='default'
		} else if(optClones[i].className == subClass || optClones[i].className == 'default') {
			// if it matches, add it to the dynamic <select> box
			dBox.appendChild(optClones[i].cloneNode(true));
		}
	}
}




// --------------------------------------------- HIDE RADIO BUTTONS / CHECK BOXES -------------------------------------------- //
// 
// pushes radio buttons / checkboxes offscreen and allows for custom controls
function hideFormElements(type,class) {
	// first get an array of all form elements and their associated labels in the given form
	allObjs = document.getElementsByTagName('input');
	// create an array to place all the valid objects into
	validObjs = new Array();
	
	
	// remove blank elements
	var i = 0;
	var j = 0;
	while(i<allObjs.length) {
		// only use valid input elements (correct type and class)
		if(allObjs[i].type == type && allObjs[i].className == class) {
			allObjs[i].style.position = 'absolute';
			allObjs[i].style.left = '-9999px';
			if(allObjs[i].addEventListener) allObjs[i].addEventListener("change",changeLabels,false);		
			else if(allObjs[i].attachEvent) allObjs[i].attachEvent("onchange",changeLabels);
			else allObjs[i].onchange = changeLabels;
			// add the element to the validInputs array to be used later in changeLabels()
			validObjs[j] = allObjs[i];
			j++;
		}
		// increment
		i++;		
	}
}

function changeLabels() {	
	// loop through the validObjs array (from hideFormElements function)
	for(var i=0; i<validObjs.length; i++) {
		// find the assiciated label
		var label = document.getElementById(validObjs[i].id + '_label');
		// change the background color depending on whether the form element is checked
		if(validObjs[i].checked == true) label.style.backgroundColor = '#007ab9';
		else label.style.backgroundColor = '#eee';
	}
}



