/**************************************************************************************************************************************************
	
	Purpose:
		Handle checking/unchecking options in custom drop down menus
	
	Info:
	 Options will be inside a div, like this:
	 <div optname="smp_search_mode">
	 	<img src="check.gif" style="visibility: hidden" /><a href="" onclick="optionClick(this)" optvalue="exact phrase">exact phrase</a>
	 	<img src="check.gif" style="visibility: hidden" /><a href="" onclick="optionClick(this)" optvalue="all words">exact phrase</a>
	 </div>
	 
	 Field value specified in the optvalue attribute of the link will be stored in a hidden form field, 
	 named after the "optname" attribute in the parent div
	 
	 Parameters:
	 	optRef - ref. to the anchor that was clicked
	 	divId - id of the div that the anchor is in (optional)
	 	optValue - value of the option that you want to set (optional)
		toggle - if set to true, it will toggle the opt that was clicked on (setting value) or off (making value eq to nothing)
	
**************************************************************************************************************************************************/

//optionClick(this,false,false,true);

function optionClick(optRef, divId, optValue, toggle) {
	if (optRef) {
		// grab ref. to parent div....
		optDiv = optRef.parentNode;
		if (optDiv) {
			// grab name of field and value
			fieldName 	= optDiv.getAttribute('field_name');
			fieldValue 	= optRef.getAttribute('opt_value');

			// attempt to grab the form field...might need to create it
			fieldRef 	= document.getElementById(fieldName); 

			// check to see if the field already exists to store hidden value
			if (!fieldRef) {
				fieldRef = document.createElement('input');
				fieldRef.setAttribute('id', fieldName);
				fieldRef.setAttribute('name', fieldName);
				fieldRef.setAttribute('type', 'hidden');
				
				// add to DOM
				optDiv.appendChild(fieldRef);
			}
			
			// store value in form field
			if (toggle) {
				if (fieldRef.value == fieldValue && fieldValue.length > 0) {
					fieldRef.value = '';
					imgVis = 'hidden';
				} else {
					fieldRef.value = fieldValue;
					imgVis = 'visible';
				}
			} else {
				fieldRef.value = fieldValue;
				imgVis = 'visible';
			}
			
			// grab all anchors inside the div
			opts = optDiv.getElementsByTagName('A');
			for(x=0;x<opts.length;x++) {
				opt = opts[x];
				// find image tag inside the anchor...
				imgs = opt.getElementsByTagName('IMG');
				// if option we are on is the one that was clicked
				if (opt == optRef) {
					// show the check mark image
					imgs[0].style.visibility = imgVis;
				} else {
					imgs[0].style.visibility = 'hidden';
				}
			}
			
		}
	
	} else {
	
		// if here, then we are setting the option without clicking on it...
		// grab div ref.
		optDiv = document.getElementById(divId);
		// grab all anchors inside this div
		opts = optDiv.getElementsByTagName('A');
		// loop through anchors, until we find the one with the matching value
		for(x=0;x<opts.length;x++) {
			opt = opts[x];
			if (opt.getAttribute('opt_value') == optValue) optionClick(opt);
		}		
		
	}
}