//create onDomReady Event
window.onDomReady = DomReady;

//Setup the event
function DomReady(fn)
{
	//W3C
	if(document.addEventListener)
	{
		document.addEventListener("DOMContentLoaded", fn, false);
	}
	//IE
	else
	{
		document.onreadystatechange = function(){readyState(fn)}
	}
}

//IE execute function
function readyState(fn)
{
	/* dom is ready for interaction
	   But this doesn't seem to work on first IE6 page load.
	   if(document.readyState == "interactive")
	{
		fn();
	}

	   So we'll just go with old standby. */
	window.onload = fn;
}

window.onDomReady(init);

// wait for DOM to load, then initialize event handlers in functions
function init() {

	// Unhide the additional pages list
	additionalPages = document.getElementById("additional-pages");
	if (additionalPages) {
		document.getElementById("more-help").innerHTML = "";
		additionalPages.className = "nohover show";
		mouseOverToggle("active-element", "arrow-additional-pages");
		mouseClickToggle("active-element", "answer-additional-pages", "showhide-additional-pages", "arrow-additional-pages");
	}
	
	// Setup account lookup form for ajax
	lookupKADform = document.getElementById("lookupKAD");
	if (lookupKADform) {
		// Intercept the form submit
		lookupKADform.onsubmit = function () {
			return false;
		}

		DNDlookupField = document.getElementById("DNDlookup");
		DNDlookupField.value = "";

		// Run the ajax funtion when submit button is clicked
		document.getElementById("lookupButton").onclick = function() {
			getKAD(DNDlookupField.value);
		} 
	}
	
	// Display IT bookmarks on sidebar (blog pages only)
	bookmarks = document.getElementById("bookmarks");
	if (bookmarks) {
		bookmarks.className = bookmarks.className.replace("hide", "show");
		getITbookmarks();
	}
	
	// Dynamically set sidebar/content border for the length of the longest element
	if ( typeof setColumnBorder == 'function' ) {
		setColumnBorder();
	}

	// Set up quota lookup page for ajax
	lookupQuotaForm = document.getElementById("share-qtree-lookup");
	resetForm = document.getElementById("reset-form")

	if ( lookupQuotaForm ) {
		// Intercept the form submit.
		lookupQuotaForm.onsubmit =  function () {
			return false;
		}

		resetForm.onsubmit =  function () {
                        return false;
                }

		// Remove the share lookup button
		shareButton = document.getElementById("share-button");
		lookupQuotaForm.removeChild(shareButton);

		// Create directory drop-down menu element.
		dirSelect = document.createElement('select');
		dirSelect.setAttribute("id", "dir-dd");
		dirSelect.setAttribute("name", "dir");
		qtreeOption = document.createElement('option');
		qtreeOption.setAttribute("value", "");
		qtreeOption.innerHTML =  "= Select a directory =";
		dirSelect.appendChild(qtreeOption);
		lookupQuotaForm.appendChild(dirSelect);

		KADresult = document.getElementById("KADresult");
		quotaResult = document.getElementById("quotaResult");
		DNDlookup = document.getElementById("DNDlookup");
		resetButton = document.getElementById("reset-button");
		shareDD = document.getElementById("share-dd");
		dirDD = document.getElementById("dir-dd");

		dirDD.className = "hide";

		shareDD.selectedIndex = 0;

		// Run ajax when item is selected from form.

		resetButton.onclick = function () {

			if ( KADresult ) {
				KADresult.className = "empty";
	                        KADresult.innerHTML = '';
			}

			if ( quotaResult ) {
				quotaResult.className = "empty";
				quotaResult.innerHTML = '';
			}

			DNDlookup.value = '';
			shareDD.selectedIndex = 0;
			if ( dirDD.length > 1 ) {
				for ( i = dirDD.length; i > 0; i-- ) {
					dirDD.remove(i);
				}
			}
			dirDD.className = "hide";
		}

		shareDD.onchange = function () {
			shareVol = shareDD.options[shareDD.selectedIndex].value;
			getQuota(shareVol, "");
		}

		dirDD.onchange = function () {
                        shareVol = shareDD.options[shareDD.selectedIndex].value;
			dirVol = dirDD.options[dirDD.selectedIndex].value;
                        getQuota(shareVol, dirVol);
                }
	}
}
