/*
	Global variables
*/
/* This one references and outside script used to target IE6; not ideal, but it does work */
var currentBrowser = BrowserDetect.browser + " " + BrowserDetect.version;
var selectedOption;

/*
	Simple script that allows for multiple onload events
		Note: This site only needs one onload event; it never hurts to have this just in case.
*/
function addLoadEvent(func) {
	var oldonload = window.onload;
	
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
      
		if (oldonload) {
			oldonload();
		}
      
		func();
    }
  }
}

/*
	Creates the "pop-up" effect for step 2 of the form
*/
function openStep2 (currentBrowser) {
	document.getElementById("formStep2Container").style.display = "block";
	
	/*
		Necessary piece of code to hide select inputs from first form; otherwise they would appear above step 2
			** This problem is cause by IE6 treating <select>s as windowed elements
	*/
	if (currentBrowser == "Explorer 6") {
		var y = document.getElementsByTagName('select');
		
		for (i=0; i<y.length; i++) {
			if (y[i].className != 'step1Select') continue; 
			
			y[i].style.display = "none";
		}
	}
}

/*
	Allows user to close the form; in case they change their mind
*/
function closeStep2 (currentBrowser) {
	document.getElementById("formStep2Container").style.display = "none";	
	
	/*
		Reverses the hiding code for <select> inputs from step 1
	*/
	if (currentBrowser == "Explorer 6") {
		var y = document.getElementsByTagName('select');
		
		for (i=0; i<y.length; i++) {
			if (y[i].className != 'step1Select') continue; 
			
			y[i].style.display = "block";
		}
	}
}

/*
	Creates 'Learn More' links and adds expanding functionality to course description
*/
function createExpandLinks () {
	var x = document.getElementsByTagName('span');
	
	for (var i=0; i<x.length; i++) {
		if (x[i].className != 'expandInfo') continue;
		
		x[i].style.display = 'none';
		
		var jumpLink = x[i].parentNode.getElementsByTagName('a')[0].name;
		
		var moreLink = document.createElement('a');
		
		moreLink.href = '#'+jumpLink;
		moreLink.className = "moreInfo";
		moreLink.innerHTML = 'Read More';
		moreLink.relatedTag = x[i];
		moreLink.onclick = openClose;
		
		x[i].parentNode.appendChild(moreLink);
	}
}

/*
	Triggers the expand/contract functionality for course descriptions
*/
function openClose() {
	var currentDisplay = this.relatedTag.style.display;
	var newDisplay = (currentDisplay == 'none') ? 'inline' : 'none';
	this.relatedTag.style.display = newDisplay;
	
	/*
		Reverses jump link highlighting and resets page appearance
	*/
	if (this.href != '#jumpLinkChoice' && this.innerHTML == "Keep Searching") {
		this.href = '#jumpLinkChoice';
		this.parentNode.getElementsByTagName('a')[0].style.fontWeight = "normal";
	}
	
	(this.innerHTML != 'Keep Searching') ? this.innerHTML = 'Keep Searching' : this.innerHTML = 'Read More';	
}

/*
	Highlights the header of the selected jump link
*/
function highlightSelected(selectedLink) {
	var x = document.getElementsByTagName('span');
	for (i=0; i<x.length; i++) {
		if (x[i].className != 'programHeader') continue;
			
		x[i].getElementsByTagName('a')[0].style.fontWeight = "normal";
	}
		
	var pageName = document.location;
	var jumpLink = selectedLink.split("#");
	document.getElementById(jumpLink[1]).style.fontWeight= "bold";
}

/*
	Gets step 1 of the form ready to pass its program of interest value to step 2
*/
function selectPass (step1) {
	selectedOption = step1.value;
}

/*
	Catches the program of interest value from step 1 and pre-selects the same field in step 2
*/
function selectCatch () {
	var x = document.getElementsByTagName('option');
	
	// Tracking Pixel on form 2
	document.getElementById('trackingPixel').innerHTML="<img src='http://leadback.advertising.com/adcedge/lb?site=695501&srvc=1&betr=lendingtreeedu_cs=1&betq=7196=396082' width='1' height='1' border='0'>";
	for (i in x) {
		if (x[i] == undefined || x[i].className != 'step2') continue;

		/*
			Toggles display of additional nursing question of step 2 initialization
		*/
		if (selectedOption == "BSNU" || selectedOption == "BSHI" || selectedOption == "MSNUE" || selectedOption == "MSNUL" || selectedOption == "RNMSE" || selectedOption == "RNMSL" || selectedOption == "MBAHM") {
			document.getElementById("nursingQuestionTable").style.display = "block";
			document.getElementById("nursingQuestion1").setAttribute("dtmk:validation", "{required:true,message:'Please indicate your license status.'}");
		} else {
			document.getElementById("nursingQuestionTable").style.display = "none";	
			document.getElementById("nursingQuestion1").removeAttribute("dtmk:validation", "{required:true,message:'Please indicate your license status.'}");
		}

		x[i].selected = false;
		if (x[i].value == selectedOption) {
			x[i].selected = 'selected';
			return;
		}
	}
}

/*
	Toggles display of additional nursing question when step 2 program of interest is changed
*/
function showHideNursing (selectedOption) {
	if (selectedOption == "BSNU" || selectedOption == "BSHI" || selectedOption == "MSNUE" || selectedOption == "MSNUL" || selectedOption == "RNMSE" || selectedOption == "RNMSL" || selectedOption == "MBAHM") {
		document.getElementById("nursingQuestionTable").style.display = "block";
		document.getElementById("nursingQuestion1").setAttribute("dtmk:validation", "{required:true,message:'Please indicate your license status.'}");
	} else {
		document.getElementById("nursingQuestionTable").style.display = "none";	
		document.getElementById("nursingQuestion1").removeAttribute("dtmk:validation", "{required:true,message:'Please indicate your license status.'}");
	}
}

function autoTab (originalField, destinationField) {
	if (originalField.value.length == originalField.getAttribute("maxlength")) {
		destinationField.focus()
	}
}

//Functions to be called when the page loads
	addLoadEvent(createExpandLinks);