var longRef = "document.forms.form";
var searchStr = 'Search This Site';
	
addEvent(window, "load", cleanSearch, false);
addEvent(window, "load", function() {enableDropdown('utilities')}, false);
addEvent(window, "load", function() {enableDropdown('nav')}, false);

var searchStr = 'Search This Site';

function cleanSASearch() 
{
	document.search.query.onfocus = function() {if (this.value == searchStr) this.value = ''};
	document.search.query.onblur = function() {if (this.value == '') this.value = searchStr};
}


function displayOrHideFormSection(formSection)
{
	if (document.getElementById(formSection).style.display == 'block')
	{
		hideFormSection(formSection);
	}
	else
	{
		displayFormSection(formSection);
	}
}
	
	

function displayFormSection(formSection)
{
	document.getElementById(formSection).style.display = 'block';
}


function hideFormSection(formSection)
{
	document.getElementById(formSection).style.display = 'none';
}



function mainToggle(formSection1, formSection2)
{
	if (document.getElementById(formSection1).style.display == 'block')
	{
		hideFormSection(formSection1);
	}
	else
	{
		displayFormSection(formSection1);
		hideFormSection(formSection2);
	}
	
	
}

function otherToggle(formSection1, formSection2)
{
	if (document.getElementById(formSection1).style.display == 'block')
	{
		hideFormSection(formSection1);
	}
	else
	{
		displayFormSection(formSection1);
		hideFormSection(formSection2);
	}
}


function displayShowChangeBackground (displayId, changeId, backgroundImage)
{	
	if (document.getElementById(displayId).style.display == 'block')
	{
		//hideFormSection(displayId);
		var imageUrl = "url(" + "../../images/" + backgroundImage + ".on.gif)";
		document.getElementById(changeId).style.backgroundImage = imageUrl;
	}
	else
	{
		//displayFormSection(displayId);
		var imageUrl = "url(" + "../../images/" + backgroundImage + ".off.gif)";
		document.getElementById(changeId).style.backgroundImage = imageUrl;
	}

}
	
	
function changeBtn(formSection, img_id)
{
	if (document.getElementById(img_id).src == 'http://www.nyu.edu/epubs/preview/advisement/web/images/btn.expand.gif')
	{
	    document.getElementById(img_id).src = '../images/btn.collapse.gif';
	}
	else
	{
	    document.getElementById(img_id).src = '../images/btn.expand.gif';
	}
}


var ids=new Array();
var isExpand = 0;

function QAinit()
{
	if(document.getElementById)
	{	
		var tids=document.getElementsByTagName('div');
		for(i=0;i<tids.length;i++)
			if(tids[i].className=="questionBox")
				ids[ids.length]=tids[i];
		for(i=0;i<ids.length;i++)
			ids[i].onmouseup=setstate;
	}
}

function setstate()
{
	for(i=0;i<ids.length;i++)
	{
		if(ids[i]==this)
		{
			var d=this.parentNode.getElementsByTagName('div')[1];
			if(d.style.display=="block")
			{
				d.style.display="none";
				isExpand = 0;
			}
			else 
			{
				d.style.display="block";
				isExpand = 1;
			}
		}
	}
}

function expandall()
{
	if(document.getElementById)
	{
		for(i=0;i<ids.length;i++)
			ids[i].parentNode.getElementsByTagName('div')[1].style.display="block";
	}

	isExpand = 1;
	document.getElementById('ecBtn').src = '/epubs/preview/advisement/web/images/btn.collapse.gif';
}

function collapseall()
{
	if(document.getElementById)
	{
		for(i=0;i<ids.length;i++)
			ids[i].parentNode.getElementsByTagName('div')[1].style.display="none";
	}
	isExpand = 0;
	document.getElementById('ecBtn').src = '/epubs/preview/advisement/web/images/btn.expand.gif';
}

function expandCollapseAll()
{
	if (isExpand == 0)
		expandall();
	else
		collapseall();
}
window.onload=QAinit;


/* 
	Email Form Verification
*/
function isBlank(string)
{
	for (var i = 0; i < string.length; i++) {
		var c = string.charAt(i);
		if ((c != ' ') && (c != '\n') && (c != '\t')) return false;
	}
	return true;
}

/**
	Receives a string of required elements
	split by commas and a reference to the form and test
	to see if they are filled out.
*/
function verifyForm(required, whichForm)
{
	var msg			= "";
	var error		= false;
	var errorMsg	= "";
	
	//	Split string of required fields into an array
	var reqList = required.split(/\s*,\s*/);
	
	//	Test for empty required fields and list name if empty
	for (var i = 0; i < reqList.length; i++) {
		var whichElement = eval("whichForm." + reqList[i]);
		if ((whichElement.value == null) || (whichElement.value == "") || isBlank(whichElement.value))
			errorMsg += "- " + whichElement.title + "\n";
	}
	
	//	If everything is ok, return true and submit the form already...
	if (errorMsg == "") return true;
	
	//	Shucks, we have to create an error message
	msg += "Please correct these error(s) and re-submit.\n";
	msg += "Some required fields are missing:\n" + errorMsg;
	
	//	Show error message
	alert(msg);
	
	//	Prevent form from submitting
	return false;
}

