// Functions_Plus
//====================================================================

//<<====================================================================
//___Dynamic Select Boxes ____________________________________________
// Use the below functions to chain multiple select boxes dynamically.
// To use the 3 lines below to the same file the form is at,
// and rename the two select box ids to match the form.
// _____
// window.onload = function() {
//     dynamicSelect("selectbox_1", "dynamic_selectbox_2");
// }
//____________________________________________________________________

function dynamicSelect(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
        refreshDynamicSelectOptions(sel1, sel2, clonedOptions);
        // 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
    while (sel2.options.length) {
        sel2.remove(0);
    }
    // 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[sel1.selectedIndex].value + ")( |$)");
    // 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
        // Or if first select box is at "select"
        if (clonedOptions[i].className.match(pattern1) || clonedOptions[i].className.match(pattern2) ||
            (sel1.options[sel1.selectedIndex].value == "select")) {
            // Clone the option from the hidden option pool and append it to the dynamic select box
            sel2.appendChild(clonedOptions[i].cloneNode(true));
        }
    }
}

//>>====================================================================

// GENERIC FUNCTIONS 

		/* This generic function updates the style used for various elements on the page */
		function setStyleByClass(p_element,p_class,p_parameter,p_value){
			var elements;
			
			if(p_element == '*') {
				// '*' not supported by IE/Win 5.5 and below
				elements = (ie) ? document.all : document.getElementsByTagName('*');
			} else {
				elements = document.getElementsByTagName(p_element);
			}

			for(var cntElementLength = 0; cntElementLength < elements.length; cntElementLength++){
				var node = elements.item(cntElementLength);

				for(var cntAttributeLength = 0; cntAttributeLength < node.attributes.length; cntAttributeLength++) {
					if(node.attributes.item(cntAttributeLength).nodeName == 'class') {
						if(node.attributes.item(cntAttributeLength).nodeValue == p_class) {
							eval('node.style.' + p_parameter + " = '" + p_value + "'");
						} // End is class value condition
					} // End is class name condition
				} // End cntAttributeLength Loop
			} // End cntElementLength Loop
		}
		// End setStyleByClass()


		/* Generic function used to toggle a value between true and false */
		function fToggle(p_key) {
			if (p_key == false) {
				p_key = true;
			} else {
				p_key = false;
			}

			return p_key;
		}
		// End fToggle


		//== ADD LOAD EVENTS by 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();
			}
		  }
		}

		
		//addLoadEvent(nameOfSomeFunctionToRunOnPageLoad);
		//addLoadEvent(function() {
		  /* more code to run on page load */ 
		//});


//>> Generic function used to remove space characters from a string 
function removeSpaces(string) {
    var tstring = "";
    string = '' + string;
    splitstring = string.split(" ");
    for(i = 0; i < splitstring.length; i++)
        tstring += splitstring[i];
    return tstring;
}


//== This function resets the cache for the page
function rc() {
	str_href = document.location.href;
	strpos = str_href.indexOf('?');

	if (strpos > 0 )
	{
		document.location.href = str_href + '&reset_cache=1';
	} else {
		document.location.href = str_href + '?reset_cache=1';
	}
} //== End Function reset cache




