// GENERAL SCRIPT -----------------------------------------------------------------

// IE-only - for building HTML body of popup objects.
var useIEPopup = false;//(is_ie5_5up && is_win);
var hideAllDropDownsOnMenuOpen = (is_ie6 && is_win);
var stubHTML_preInclude = '<html><head>';
var stubHTML_preContent = '</head><body scroll="auto" topmargin="0" leftmargin="0" '
							+ 'bottommargin="0" rightmargin="0" '
							+ 'style="border: 0; background: transparent none;">';
var stubHTML_postContent = '</body></html>';

//dx:custom-drop-down ----------------------------------------------------------------------------------

var customDropdowns = new Object();

// If using IE popups, make sure they're closed before unloading the page.
if (useIEPopup) {
	bodyOnUnload.push(
		function() {
			for (var i in customDropdowns) {
				hideCustomDropdownOptions(customDropdowns[i]);
			}
		}
	);
}

function setupCustomDropdown(divDropdown, options, settings) {
	customDropdowns[divDropdown.id] = divDropdown;
	
	divDropdown.options = options;
	divDropdown.hasFormField = settings.hasFormField;
	divDropdown.FormFieldName = settings.name;
	divDropdown.optionClass = settings.optionClass;
	divDropdown.optionHighlightClass = settings.optionHighlightClass;
	divDropdown.selectedValue = settings.value;
	divDropdown.dropDir = settings.dropDir;
	
	// Either the main document object, or the IE popup document object (IE 5.5+).
	divDropdown.optionDoc = document;
	
	divDropdown.overDiv = false;
	divDropdown.overDropdown = false;
	
	// Give drop DIV its own link to the customDropdowns data structure. This is necessary 
	// because in IE, when popup appears, the drop div will live in there and will need 
	// a reference to the opening page's data structure.
	getObjectById(divDropdown.id + '_DropBox').customDropdowns = customDropdowns;
	
	// DIV events.
	
	divDropdown.onchangeCode = settings.onchange;
	divDropdown.onchange = dxCustomDropdown_onchange;
	divDropdown.onclick = dxCustomDropdown_onclick;
	divDropdown.onmouseover = dxCustomDropdown_onmouseover;
	divDropdown.onmouseout = dxCustomDropdown_onmouseout;
	
	// Child object events.
	
	divDropdown.onDropBoxMouseOver = dxCustomDropdown_onDropBoxMouseOver;
	divDropdown.onDropBoxMouseOut = dxCustomDropdown_onDropBoxMouseOut;
	divDropdown.onOptionClick = dxCustomDropdown_onOptionClick;
	divDropdown.onOptionMouseOver = dxCustomDropdown_onOptionMouseOver;
	divDropdown.onOptionMouseOut =  dxCustomDropdown_onOptionMouseOut;
	
	// Utilities.
	
	divDropdown.getOptionByID = dxCustomDropdown_getOptionByID;
}

function dxCustomDropdown_onchange(newValue) {
	eval(this.onchangeCode);
}

function dxCustomDropdown_onclick() {
	clearTimeout(this.timer);
	showCustomDropdownOptions(this);
}

function dxCustomDropdown_onmouseover() {
	this.overDiv = true;
	clearTimeout(this.timer);
}

function dxCustomDropdown_onmouseout() {
	this.overDiv = false;
	if (!this.overDropdown) 
		this.timer = setTimeout("hideCustomDropdownOptions(document.getElementById('" + this.id + "'));", 500);
}

// Child object events.

function dxCustomDropdown_onDropBoxMouseOver() {
	this.overDropdown = true;
	clearTimeout(this.timer);
}

function dxCustomDropdown_onDropBoxMouseOut() {
	this.overDropdown = false;
	if (!this.overDiv)
		this.timer = setTimeout("hideCustomDropdownOptions(document.getElementById('" + this.id + "'));", 500);
}

function dxCustomDropdown_onOptionClick(OptionID) {
	var option = this.getOptionByID(OptionID);
	if (option) {
		if (this.selectedValue != option.value) {
			// User has chosen a new value.
			
			// Swap icon and caption.
			document.getElementById(this.id + '_CaptionCell').innerHTML = 
				(option.icon ? '<img src="' + option.icon + '" class="topIcon"/>' : '') + 
				'<a href="#" onclick="document.getElementById(\'' + this.id + '\').onclick(); return false;">'
				+ option.caption
				+ '</a>';
			
			// Swap value and possibly form field.
			this.selectedValue = option.value;
			if (this.hasFormField) {
				var formField = document.getElementById(this.FormFieldName);
				formField.value = option.value;
				formField.isDirty = true;
			}
			
			// Fire event.
			this.onchange(option.value);
		}
		
		// Close menu.
		hideCustomDropdownOptions(this);
	}
}

function dxCustomDropdown_onOptionMouseOver(OptionID) {
	var option = this.getOptionByID(OptionID);
	var optionControl = this.optionDoc.getElementById(OptionID);
	
	if (option && optionControl) {
		if (this.optionHighlightClass) {
			optionControl.className = this.optionHighlightClass;
		}
	}
}

function dxCustomDropdown_onOptionMouseOut(OptionID) {
	var option = this.getOptionByID(OptionID);
	var optionControl = this.optionDoc.getElementById(OptionID);
	
	if (option && optionControl) {
		if (this.optionClass) {
			optionControl.className = this.optionClass;
		}
	}
}

// Utilities.

function dxCustomDropdown_getOptionByID(OptionID) {
	for (var i in this.options) {
		if (this.options[i].id == OptionID) return this.options[i];
	}
	return null;
}




function showCustomDropdownOptions(divDropdown) {
	var optionBox = document.getElementById(divDropdown.id + '_DropBox');
	var rect = getRect(divDropdown);
	var optionRect;
	
	if (useIEPopup) {
		if (!divDropdown.popup) {
			// Create IE popup object and initialize.
			
			divDropdown.popup = window.createPopup();
			var popDoc = divDropdown.popup.document;
			popDoc.write(stubHTML_preInclude);
			
			// Add stylesheets from the current document.
			for (var i in document.styleSheets) {
				if (document.styleSheets[i].href) {
					popDoc.write(
						'<link rel="stylesheet" type="text/css" href="'
						+ escape(document.styleSheets[i].href) + '">');
				} else {
					popDoc.write(
						'<style type="text/css">\n' + document.styleSheets[i].cssText
						+ '\n</style>');
				}
			}
			
			popDoc.write(stubHTML_preContent);
			popDoc.write(optionBox.outerHTML);
			popDoc.write(stubHTML_postContent);
			popDoc.close();
			showObject(null, popDoc.all[optionBox.id]);
			
			// Link popup event handlers to parent page event handlers.
			popDoc.all[optionBox.id].customDropdowns = customDropdowns;
			
			// Link other references.
			divDropdown.optionDoc = popDoc;
		}
		var remoteOptionBox = divDropdown.popup.document.getElementById(optionBox.id);
		remoteOptionBox.style.width = rect.right - rect.left;
		
		divDropdown.popup.show(0, 0, 0, 0, document.body);
		optionRect = getRect(remoteOptionBox);
		
		if (divDropdown.dropDir == 'down') {
			divDropdown.popup.show(0, rect.bottom - rect.top, rect.right - rect.left, 
				optionRect.bottom - optionRect.top, divDropdown);
		} else {
			divDropdown.popup.show(0, 0 - (optionRect.bottom - optionRect.top),
				rect.right - rect.left, optionRect.bottom - optionRect.top, divDropdown);
		}
		
	} else {
		if (divDropdown.dropDir == 'down') {
			optionBox.style.left = rect.left;
			optionBox.style.top = rect.bottom;
			optionBox.style.width = rect.right - rect.left;
			
			showObject(null, optionBox);
			
		} else {
			optionBox.style.left = rect.left;
			optionBox.style.width = 0;
			
			showObject(null, optionBox);
			
			optionRect = getRect(optionBox);
			optionBox.style.top = rect.top - (optionRect.bottom - optionRect.top);
			optionBox.style.width = rect.right - rect.left;
		}
	}
}

function hideCustomDropdownOptions(divDropdown) {
	if (useIEPopup) {
		if (divDropdown.popup) {
			divDropdown.popup.hide();
		}
	} else {
		var optionBox = document.getElementById(divDropdown.id + '_DropBox');
		hideObject(null, optionBox);
	}
}

//dx:multi-section -------------------------------------------------------------------------------------------------------------------------

function dxMultiSection() {
	this.sections = new Array();
	this.currentSection = 0;
	
	this.showSection = dxMultiSection_showSection;
	this.previousSection = dxMultiSection_previousSection;
	this.nextSection = dxMultiSection_nextSection;
	this.refresh = dxMultiSection_refresh;
	this.showAll = dxMultiSection_showAll;
	this.printPage = dxMultiSection_printPage;
}

function dxMultiSection_showSection(sectionIndex) {
	this.currentSection = sectionIndex;
	if(this.currentSection < 0) 
		this.currentSection = this.sections.length - 1;
	if(this.currentSection >= this.sections.length) 
		this.currentSection = 0;
	this.refresh();
}

function dxMultiSection_previousSection() {
	this.currentSection --;
	if(this.currentSection < 0) 
		this.currentSection = this.sections.length - 1;
	this.refresh();
}

function dxMultiSection_nextSection () {
	this.currentSection ++;
	if(this.currentSection >= this.sections.length) 
		this.currentSection = 0;
	this.refresh();
}

function dxMultiSection_refresh () {
	for(var index = 0; index < this.sections.length; index ++) {
		if(index == this.currentSection)
			this.sections[index].style.display = "inline";
		else
			this.sections[index].style.display = "none";
	}
}

function dxMultiSection_showAll () {
	for(var index = 0; index < this.sections.length; index ++) {
		this.sections[index].style.display = "inline";
	}
}

function dxMultiSection_printPage () {
	this.showAll();
	if(window.print)
		window.print();
}

// Highlights the drop-down arrow image when you mouse over a dxFakeDropdown field
	function dxFakeDropdown_onmouseover() {
		dxFakeDropdown_changeArrowImage(this, root + '/images/dropdownMouseOver.gif');
	}
	
	// Removes the highlight from the drop-down arrow image when you mouse out of a dxFakeDropdown field
	function dxFakeDropdown_onmouseout() {
		dxFakeDropdown_changeArrowImage(this, root + '/images/dropdownIdle.gif');
	}
	
	function dxFakeDropdown_changeArrowImage(currentDropDown, newSourcePath) {
		var imageElements = currentDropDown.getElementsByTagName('img');
		
		for (var i = 0; i < imageElements.length; i++) {
			if (imageElements[i].id == 'FakeDropDownArrow') {
				imageElements[i].src = newSourcePath;
				return;
			}
		}
	}

