/* *************************
     Variable Declarations
    _______________________  */


	var agent;
	
	// Valid args ( Horiz | Vert )
	var typeOfNavigationLayout = 'Horiz';
	
	// Valid args ( liTag | aTag )
	var setClassForActiveNav = 'liTag';
	
	// for fLinkChecker();
	var aDocLINKS, findPromo, linkText, theCode, ePromoCodeCode;  
	
	// related to fLinkChecker();
	var theCodeValue, aCookieArray, eCookie;
	
	// for all ajax interactions
	var ajaxResponse;
	
	// used to build links in the nav, as well as save the page
	var directory, pageID;
	
	var bigHairyBalls = false;






/* *************************
     Constructor Functions
    _______________________  */


function navElement (uID, sectionName, fileName, title, enabled, parentArray, level, sortOrder, url) {

	this.length = 10;
	this.uID = uID;
	this.sectionName = sectionName;
	this.fileName = fileName;
	this.title = title;
	this.enabled = enabled;
	this.parentArray = parentArray;
	this.level = level;
	this.sortOrder = sortOrder;
	this.url = url;
}







/* *************************
     Executing scripts
    _______________________  */


// We need to know specifically what version of IE we are dealing with to serve the correct stylesheets.
 
usrPlatform = navigator.platform;
userPlatform = usrPlatform.substr(0);

if (usrPlatform == 'Win32') {

	agent = navigator.userAgent.indexOf('MSIE') != -1 ? agent = 'ie' : agent = navigator.userAgent.indexOf('Gecko') != -1 ? agent = 'gecko': false;

     if (agent == 'ie') {
		browser = navigator.userAgent;
          browser = browser.split(';');
          browser = browser[1];
          browser = browser.substr(5);

          if (browser == 6 || browser == 5.5) {
			document.write('<link rel="stylesheet" href="/_Lib/css/ie_styles.css" type="text/css" />');
		}
		
		if (browser == 7) {
			document.write('<link rel="stylesheet" href="/_Lib/css/ie7_styles.css" type="text/css" />');
		}
	}
}







/* *************************
     Function Declarations
    _______________________  */


function findEventElement(e) {
	
	/* This function works with most of the scripts of the site. It simply
	   finds what element you have fired an event on, called currentE, what
	   the parent is, called parentE, and the element you are going to
	   called "relatedE".
	
	   It uses the ? operator to test for DOM2 compliant event properties.
	   If they return false, we set elements to IE event properties.
	
	   In addition, this script addresses the Safari bug on which mouseovers
	   fire on text nodes, not A nodes.  Thanks to Quirksmode for that one.
	*/
	
	if (!e) var e = window.event;
	
	//e.target ? currentE = e.target : currentE = e.srcElement;
	e.srcElement ? currentE = e.srcElement : currentE = e.target;
	
	if (currentE.nodeType == 3) {
		currentE = currentE.parentNode;
	}
	
	parentE = currentE.parentNode;
	grandparentE = parentE.parentNode;
	e.relatedTarget ? relatedE = e.relatedTarget : relatedE = e.toElement;	
}

function fLinkChecker() {
	
	//  This function is the Opt-in Counter for the Campaign Tracking Tool.
	
	aDocLINKS = document.links;
	findPromo = new RegExp("PromoCode=\\w+");

	for (var x = 0; x < aDocLINKS.length; x++) {
		
		linkText = aDocLINKS[x].href;
		theCode = findPromo.exec(linkText);
		
		if (theCode) {
			ePromoCodeCode = theCode[0].slice(10);
			readCookie(ePromoCodeCode);
		}
	}
}

function hideElement (e) {

	findEventElement(e);
	
	if (relatedE != topItem) {
	
		decendantList = topItem.getElementsByTagName(relatedE.tagName);
		decendant = false;
			
		for (var x = 0; x < decendantList.length; x++) {
			
			if (decendantList[x] == relatedE) {
				decendant = true;
			}
		}

		if (decendant == false) {
			
			// This is the instant out if relatedE is not my top level nav item or a decendant of it.
				
			topItem.removeChild(topItem.lastChild);
			topItem.className = oldClass;
			topItem.firstChild.onmouseover = setListener;
			topItem.removeEventListener ? topItem.removeEventListener('mouseover', showElement, false) : topItem.detachEvent('onmouseover', showElement);
			topItem.removeEventListener ? topItem.removeEventListener('mouseout', hideElement, false) : topItem.detachEvent('onmouseout', hideElement);
				
				
		} else {
		
			
			while (currentE.nodeName == 'A') {
						
				currentE = currentE.parentNode;
			}
						
			while (relatedE.nodeName == 'A') {
						
				relatedE = relatedE.parentNode;
			}
	
			
			if (relatedE != currentE) {
	
	
				var relationship;
				
				for (var x = 0; x < 2; x++) {
						
						
					x == 0 ? useElement = currentE : useElement = relatedE;
						
					useElement == currentE ? checkElement = relatedE : checkElement = currentE;
							
					checkNodeList = useElement.getElementsByTagName(checkElement.nodeName);
								
					for (var y = 0; y < checkNodeList.length; y++) {
							
						if (checkNodeList[y] == checkElement) {
								
							x == 0 ? relationship = 'child' : relationship = 'parent';
						}
					}
				}
				
				if (!relationship) {
					
					for (var x = 0; x < 2; x++) {
						
						x == 0 ? useElement = currentE : useElement = relatedE;
						
						useElement == currentE ? checkElement = relatedE : checkElement = currentE;
						
						useElementParent = useElement.parentNode;
						
						checkNodeList = useElementParent.getElementsByTagName(checkElement.nodeName);
						
						for (var y = 0; y < checkNodeList.length; y++) {
							
							if (checkNodeList[y] == checkElement) {
								
								checkElementParent = checkElement.parentNode;
								
								for (var z = 0; z < checkElementParent.childNodes.length; z++) {
									
									if (checkElementParent.childNodes[z] == useElement) {
										
										relationship = 'sibling';	
										
									}
								}
							}
						}
						
						if (!relationship) {
							
							relationship = 'uncle';
						}
					}
				}
				
				// This next IF/ELSE IF statement will do the removal of the nodes
				
				if (relationship == 'parent' || relationship == 'sibling') {
					
					if (currentE.lastChild.nodeName == 'UL') {
						
						currentE.removeChild(currentE.lastChild);	
						
					}
					
				} else if (relationship == 'uncle') {
					
					var relatedEParent = relatedE.parentNode;
					
					for (var x = 0; x < relatedEParent.childNodes.length; x++) {
						
						if (relatedEParent.childNodes[x] != relatedE && relatedEParent.childNodes[x].lastChild.nodeName == 'UL') {
							
							relatedEParent.childNodes[x].removeChild(relatedEParent.childNodes[x].lastChild);
							
						}
					}
				}
			}
		} 
	}
}

function initializeArrays () {
	
	aSectionArray = new Array();
	aSectionArray.splice (aSectionArray.length, 0, 0);
	
	for (var x = 0; x < aPages.length; x++) {
		
		if (aPages[x].sectionName) {
			
			aSectionArray.splice (aSectionArray.length, 0, aPages[x].uID);
		}
	}
	
	for (var x = 0; x < aSectionArray.length; x++) {
	
		writeArray (aSectionArray[x]);
	}
	
}

function makeRequest (url, parameters, alertString) {

	http_request = false;
	
	// Check for support of XMLHttpRequest. If found, DOM compliant browsers like Mozilla, Safari, etc. will use this branch
	
	if (window.XMLHttpRequest) { 
	
		http_request = new XMLHttpRequest();
		
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/xml');
		}
		
	// If not found, we are dealing with the proprietary IE DOM
	
	} else if (window.ActiveXObject) { 
		
		try {
			
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
			
		} catch(e) {
			
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch(e) {}
		}
	}
	
	
	if (!http_request) {
		alert('FAILED, Cannot create XMLHTTP instance');
		return false;
	}
	
	http_request.open('GET', url + parameters, false);
	http_request.send(null);
	
	if (http_request.readyState == 4) {
		
		if (http_request.status == 200) {

			ajaxResponse = http_request.responseText;

			if (ajaxResponse != 'hide') {
				
				if (alertString) {
					
					if (alertString == 'default') {
						
						alert('Your edits have been saved. Click OK to continue.');
						
					} else {
						
						alert(alertString);
					}
				}
			}
			
			return ajaxResponse;
			
		} else {
			
			alert('There was a problem with the request. \n Request Status: ' + http_request.status);
		}
	}
}

function readCookie(theCode) {
	
	theCodeValue = theCode + "=";
	
	aCookieArray = document.cookie.split(';');
	
	for (var i = 0; i < aCookieArray.length; i++) {
		
		eCookie = aCookieArray[i];
	  
		while (eCookie.charAt(0) == ' ') {
			
			eCookie = eCookie.substring(1,eCookie.length);
			
		}
		
		if (eCookie.indexOf(theCodeValue) == 0) {
			
			return true;
			
		}
	}
	
	script = "/_Lib/other/processLinkCounter.php";
	sendPromoCode = "?promoCode=" + theCode;
	makeRequest(script,sendPromoCode);
	return true;
}

function setDirectory() {


	fullURL = window.location.href;
	
	/*
	
	First we are checking to see if we can match to onScreenEditing. If we can,
	we are on the back end of the system and will execute the if part of this function
	
	*/
	
	if (fullURL.match(/^\S+onScreenEditing.php\S*$/)) {
		
		thisPageID = fullURL.match(/\d+$/);
	  
		/*
		
		This if/else branch is for when we log into the back end system. The first page we see is
		onScreenEditing.php. There are no variables at the end of the URI.
		
		Therefore - because we have matched to onScreenEditing and know we are on the back end - 
		we also know that if we match with a digit, that we are not on the entry page. We could be
		on the home page (onScreenEditing.php?pageID=1), but we have navigated back to it.
		
		*/
		
		if (thisPageID) {
			
			pageID = thisPageID;
			
			for (var x = 0; x < aPages.length; x++) {
			
				if (thisPageID == aPages[x].uID) {
				
					currentPage = aPages[x];
				
					while (currentPage.level != 0) {
					
						for (var y = 0; y < aPages.length; y++) {
						
							if (aPages[y].uID == currentPage.parentArray) {
							
								currentPage = aPages[y];
							}
						}
					}
				}
			}
				
			directory = currentPage.uID;
			
		} else {
			
			directory = 1;
			pageID = 1;
			
		}
		  
	} else {
		
		directory = fullURL.split("/");
		
		if (directory[3] == '') {
			
			directory = 'index.html';
		} else {
			
			directory = directory[3];
			
		}
	}
}

function setListener (e) {
	
	/*
	
	This simple function is the key to all the drop downs working.
	
	It works with all browsers and brances for DOM compliancy
	
	*/


	findEventElement(e);
	
	currentE.onmouseover = '';
	
	if (currentE.nodeName == 'A') {
		currentE = currentE.parentNode;
	}

	topItem = currentE;
	
	topItem.className == 'navFocus' ? oldClass = 'navFocus' : oldClass = topItem.className;
	
	topItem.addEventListener ? topItem.addEventListener('mouseover', showElement, false) : topItem.attachEvent('onmouseover', showElement);
	topItem.addEventListener ? topItem.addEventListener('mouseout', hideElement, false) : topItem.attachEvent('onmouseout', hideElement);

}

function writeArray (targetArray) {
	
	newNavArray = new Array();
	
	for (var y = 0; y < aPages.length; y++) {
		
		if (aPages[y].parentArray == targetArray) {
			
			newNavArray[aPages[y].sortOrder] = aPages[y];

		}
	}

	if (bigHairyBalls == false) {
			
		for (var g = 0; g < newNavArray.length; g++) {
				
			if (!newNavArray[g]) {
					
				newNavArray.splice (g, 1);
				g--;
			} 
		}
	}

	window['anav' + targetArray + 'Array'] = newNavArray;
	array = newNavArray;
	
}

function showElement (e) {
	
	topItem.className = 'dropDown';
	
	findEventElement(e);
	
	  if (currentE.nodeName == 'A') {
	
		if (window['a' + parentE.id + 'Array']) {
		
			if (parentE.lastChild.nodeName != 'UL') {
		
				writeSubNav(window['a' + parentE.id + 'Array'], parentE, 'yes');
			}
		}
	} 
}

function writeNav () {
	
	setDirectory();
	
	eNav = document.getElementById('topNav');
	
	while (eNav.hasChildNodes()) {
		
		eNav.removeChild(eNav.firstChild);
		
	}
		
	writeSubNav (anav0Array, eNav);
}

function writeSubNav (array, element, newUL) {

	if (newUL) {
		
		/*
		
		This area is for adding a new UL and will trigger when any drop down needs to be written.
		
		Because this function is called by the event listener, we also take into account when we
		might be writing the top nav.
		
		*/
	
		eUL = document.createElement('UL');
		
		for (var x = 0; x < aPages.length; x++) {
		
			if ('nav' + aPages[x].uID == element.id) {
			
				pageLevel = aPages[x].level;
				
			}
		}
		
		
		if (agent == 'ie' && browser >= 7) {
			
			if (pageLevel == 0) {
				
				eUL.style.left = '0px';
				eUL.className = 'firstDrop';
				
			} else {
				
				eUL.style.left = (element.offsetWidth - 3) + 'px';
				eUL.style.top = '4px';
			}
			
		} else {
		
			if (pageLevel == 0) {
				
				eUL.style.left = element.offsetLeft + 'px';
				eUL.className = 'firstDrop';
				
			} else {
				
				eUL.style.left = (element.offsetWidth - 3) + 'px';
				eUL.style.top = (currentE.offsetTop - 2) + 'px';
			}
		}
	}
	
	
	for (var x = 0; x < array.length; x++) {
		
		eNewLI = document.createElement('LI');
		eNewLI.id = 'nav' + array[x].uID;
		
		if (array[x].enabled == 'no') {
				
			eNewLI.className = 'disabled';
		}
		
		eNewA = document.createElement('A');
		
		if (bigHairyBalls == true) {
			
			eNewA.href = '?pageID=' + array[x].uID;
			
		} else {
			
			eNewA.href = array[x].url;
			eNewA.href = '?pageID=' + array[x].uID;
		}
		
		if (window['anav' + array[x].uID + 'Array']) {
			
			eNewA.onclick = function () {
				
				return false;
			};
			
		}
			
			
		if (array == anav0Array) {
			
			/*
			
			This conditional area is for setting the event listener only on the top level items
			IF there is a corresponding sub nav array.
			
			It also is for setting focus on the tab.
			
			*/
		
			if (window['anav' + array[x].uID + 'Array']) {
				eNewA.onmouseover = setListener;
			}
			
			
			// branch for checking directory agains page id on back end and directory against file or section names on front end
			
			if (window.bigHairyBalls == true) {
				
				if (array[x].uID == directory) {
					
					setClassForActiveNav == 'liTag' ? tempElement = eNewLI : tempElement = eNewA;
					tempElement.className = 'navFocus';
					
					if (	array[x].enabled == 'no') {
						
						tempElement.className = 'navFocusDisabled';	
					}
				}
				
			} else {
				
				if (array[x].fileName == directory || array[x].sectionName == directory) {
					
					setClassForActiveNav == 'liTag' ? eNewLI.className = 'navFocus' : eNewA.className = 'navFocus';
				}
			
			}
		} else {
			
			if (array[x].sectionName) {
				
				agent == 'ie' ? eNewLI.className = 'section' : eNewA.className = 'section';
			}
		}
		
		eNewAText = document.createTextNode(array[x].title);
		eNewA.appendChild(eNewAText);
		eNewLI.appendChild(eNewA);

		if (newUL) {
			eUL.appendChild(eNewLI);
			
		} else {
	
			element.appendChild(eNewLI);
		
		}
	}
	
	if (newUL) {
	
		element.appendChild(eUL);
	
	}
}

function pasteListenerDEPRECATED() {
	document.body.addEventListener ? document.body.addEventListener('paste', pasteClean, false) : document.body.attachEvent('onpaste', pasteClean);
}

function pasteCleanDEPRECATED() {

	// Safari, WebKit, FireFox, etc.
	if (event.clipboardData) {
		event.returnValue = false;
		eClip = event.clipboardData;
		alert('Safari, WebKit, FireFox, etc.');
	// IE
	} else {
		window.event.returnValue = false;
		eClip = window.clipboardData;
		alert('Internet Explorer');
	}

	textString = eClip.getData('Text');
	textString = textString.replace(/\n/g,"</p><p>");

	ePara = document.createElement('P');
	eText = document.createTextNode(textString);
	mainDiv = document.getElementById("mainContent");
	ePara.appendChild(eText);
	mainDiv.appendChild(ePara);



	//event.clipboardData ? event.returnValue = eClip : window.event.returnValue = eClip;


	/*
	for (var x=0, len=eClip.childNodes.length; x<len; x++) {

	someVar = 'Wes Was HERE';
	window.clipboardData.setData('Text', someVar);
	
	event.clipboardData ? event.returnValue = true : window.event.returnValue = true;
	}
	*/



/* DEPRECATED
	window.event ? window.event.returnValue = false : event.returnValue = false;
	//event.clipboardData ? fooBar = event.clipboardData.getData("Text") : fooBar = window.clipboardData.getData("Text");
	event.clipboardData ? fooBar = event.clipboardData.getData("Text") : fooBar = window.clipboardData.getData("Text");
	//eDiv.innerHTML = fooBar;
	//event.clipboardData ? fooBar = event.clipboardData.getData("URL") : fooBar = window.clipboardData.getData("URL");
alert(fooBar);
return false;
	eDiv = document.getElementById('mainContent');
	eDiv.lastChild.innerHTML = fooBar;
	document.body.appendChild(eDiv);
*/

}


function removeAstericks() {

	aInput = document.getElementsByTagName('input');
	for (x = 0; x < aInput.length; x++) {
	    if (aInput[x].type == 'password') {
		aInput[x].type = 'text';
	    }
	}

}
