function chooseGetFnc(inputText)
{
	//WHAT WAY TO SEARCH
	/*var i = 0;
	var whichWayIndex = 1;
	while( i < document.radioForm.searchType.length )
	{
		if(document.radioForm.searchType[i].checked == true) 
		{
			whichWayIndex = i;
			i = document.radioForm.searchType.length;
		}
		i++;
	}
	var whichWay = document.radioForm.searchType[whichWayIndex].id;*/
	//alert(whichWay);
	
	whichWay = "searchTypeWithin";
	if(whichWay == "searchTypeBegins" || whichWay == "searchTypeWithin")
	{
		getInputResponse(inputText);	
	}
}

function getInputResponse(inputText)
{	
	window["alphaObject"].cameFrom = "getInputResponse";
	
	inputText = inputText.toLowerCase();

	if(inputText.length >= 4) //first check length
	{	
		if (inputText.charAt(inputText.length - 1) == " " || inputText.length == 4) {
			dm.addEl("img", "spinnyTypeQuery", "spinnyTypeQueryImg", ["src:images/spinny.gif"]);
		     //jQuery("#typeQueryInput").animate( { bgColor:"#FFFFFF" } , 1000 );
			//DWRUtil.useLoadingMessage();
			DWREngine._execute(_ajaxConfig._cfscriptLocation, null, 'getInputResponse', inputText, showInputResponse);
		}
	}
	
	if(inputText.length == 0)
	{
		dm.remElById("textHolder");
	}
	
}

function getQuickPickResponse(inputText)
{
	var selectPath = document.getElementById("fundSelect");
	var selectValue;
	var i = 0;
	while( i <= selectPath.options.length )
	{
		selectValue = selectPath.options[i].value;
		if(selectValue == inputText) 
		{
			selectPath.selectedIndex = i;
			break;
		}
		i++;	
	}
	var selectIndex = selectPath.selectedIndex;
	var optionText = selectPath.options[selectIndex].text;
	
	//check for auto-adds (usually special interest)
	if(inputText.substring(0,4) == "auto")
	{
		var fundCode = inputText.substring(4);
		var fundName = optionText;
		var isEndowed = 0; //need to do something about this later
		var isGood = true;
		origin = "CP";
		if( giftExists(fundCode, fundName) == 1 )
		{
				isGood = false;
				var erMess = "You have already added this fund to the gift cart.";
				disGiftMsg(origin, erMess, 1);
		}
		if(isGood == true) addToCart(fundName, fundCode, isEndowed, origin);
	}
	else if(selectValue == 0) //check for headers with value 0
	{
		//do nothing!
	}
	else //otherwise, continue to bring up the priorities
	{
		inputText = inputText.toLowerCase();
		window["alphaObject"].cameFrom = "getQuickPickResponse";
		
		DWRUtil.useLoadingMessage();
		DWREngine._execute(_ajaxConfig._cfscriptLocation, null, 'getQuickPickResponse', inputText, showQuickPickResponse);	
	}
}

function showInputResponse(r)
{
	dm.remElById("spinnyTypeQueryImg");
	createDropDown(r);
}

function showQuickPickResponse(r)
{
	createDropDownPri(r);
}

//USED FOR SEARCH CONFIRMATION
function getFundValidation(inputText)
{
	//DWRUtil.useLoadingMessage();
	DWREngine._execute(_ajaxConfig._cfscriptLocation, null, 'getFundValidation', inputText, showFundValidation);	
}

function showFundValidation(r)
{
	/*Validates whether or not the fund is a real fund and if it isnt, sets its fund code to the general code of 0*/
	if(r.isgood == "false" && window["alphaObject"].cameFrom != "getQuickPickResponse")
	{
		document.getElementById("typeQueryInput").curGift.fundCode = 0000;
		document.getElementById("typeQueryInput").curGift.fundName = document.getElementById("typeQueryInput").value;
	}
	addGift('fundSearch');

}


function addHelpPopup(whatParent, whatName, whatLinkText, whatDescripText)
{
	dm.addEl("div", whatParent, whatName+"helpHolder", ["class:helpHolderRel"]);
	
	
	/*??????? Should need an ID for the element  otherwise IE8 will throws an error ????????*/
	dm.addEl("a", whatName+"helpHolder", "endowedWhatis", ["href:#", "text:"+whatLinkText, "onmouseover:unHide('"+whatName+"Help')", "onmouseout:hide('"+whatName+"Help')"] );
	
	dm.addEl("div", whatName+"helpHolder", whatName+"Help", ["class:help", "text:"+whatDescripText]);	
}

function createFundPopup(h3Text, h2Text, fundArray)
{
	var fundNameAr = new Array();
	var fundCodeAr = new Array();
	createOverlay("overlayFundPick");
	hide("fundSelect");	  
	dm.addEl("h3", "overlayFundPick", "h3_fundheader", ["text:"+h3Text]);
	dm.addEl("h2", "overlayFundPick", "h2_fundheader", ["text:"+h2Text]);
	dm.addEl("br", "overlayFundPick");
	dm.addEl("p", "overlayFundPick", "p_instructions", ["text:Please select the funds you would like to give to and click the button 'Add Selected Funds' to move these items to your gift cart."]);
	dm.addEl("br", "p_instructions");
	//dm.addE1("div", "p_instructions", "endowedhelpHolder", ["class:helpHolderRel"]);
	//dm.addEl("a", "endowedhelpHolder", "", ["text:What is", "href:#"]);
	var endowedHelpText = "The E symbol means a fund is endowed.  The principal, usually the original gift of a donor, is never spent.  A part of the investment income from MSU's investment of the principal (original gift) supports a program or programs at MSU.  The remaining investment income is returned to principal and retained as a hedge against inflation and to provide a reserve that can be drawn on when investment returns are low or negative.";
	addHelpPopup("p_instructions", "endowed", "What is", endowedHelpText);
	
	dm.addEl("img", "endowedhelpHolder", "img_endowedMeaning", ["src:images/endowedSymbolCart.gif"]);
	
	dm.addEl("ul", "overlayFundPick", "ul_fundcbHolder");
	var i = 0;
	while (i < fundArray.length)
	{
		if(window["alphaObject"].cameFrom == "getQuickPickResponse") isGood = 0;
		
		if(	isGood != -1 )
		{
			
			//BELOW IS A GOOD EXAMPLE OF CREATING A LINK WITH A VARIABLE IN IT
			
			//THE LINK //dm.addEl("a",textHolder, "inputResponseLink"+i, ["href:javascript:changeInputText(\'"+typeQueryInput+"\', \'"+window["alphaObject"][window["alphaObject"].current][i][0]+"\', \'"+window["alphaObject"][window["alphaObject"].current][i][1]+"\' )", "class:dropLinks"] );
			var fundName = fundArray[i].fundname;
			var fundCode =  fundArray[i].fundcode;
			var isEndowed =  fundArray[i].isendowed; //1 for yes, 0 for no
			
			//alert(fundName);
			fundNameAr.push(fundName);
			fundCodeAr.push(fundCode);

			dm.addEl("li", "ul_fundcbHolder", "li_"+fundCode);
			//dm.addEl("span", "li_"+fundCode, "amountBoxHolder"+fundCode);
			//dm.addEl("label", "amountBoxHolder"+fundCode, "label_amount"+fundCode, ["text:$"]);
			//dm.addEl("input", "amountBoxHolder"+fundCode, "amountBox"+fundCode, ["type:text", "name:amountBox_funds", "class:amountBox", "size: 4", "maxlength: 10"]);
			
			dm.addEl("input", "li_"+fundCode, "cb_"+fundCode, ["type:checkbox", "name:cb_funds"]);
			//var cb = document.getElementById("cb_"+fundCode);
			//cb.onclick = unHide("amountBoxHolder"+fundCode);
			
			dm.addEl("label", "li_"+fundCode, "label_"+fundCode, ["text:"+fundName, "for:cb_"+fundCode]);
			if(isEndowed == 1) dm.addEl("img", "li_"+fundCode, "img_endSymbol_"+fundCode, ["src:images/endowedSymbolCart.gif", "title:This is an endowment fund", "alt:Endowment Fund"]);
			
			var cbPath = document.getElementById("cb_"+fundCode);
			cbPath.fundName = fundName;
			cbPath.fundCode = fundCode;
			if(isEndowed == 1) cbPath.isEndowed = 1;
			else cbPath.isEndowed = 0;
		}
		i++;
	}
	
	dm.addEl("br", "overlayFundPick");
	dm.addEl("a", "overlayFundPick", "a_cancelBtn", ["href:#", "onclick:removeOverlay(); unHide('fundSelect'); return false;", "class:corTR btn", "text:No Thanks", "style:width:70px"]);
	
	/*dm.addEl("span", "a_cancelBtn", "span_noThanks", ["text:No Thanks"]);*/
	/*dm.addEl("img", "a_cancelBtn", "img_cancelBtn", ["alt:Cancel", "title:Cancel", "src:images/xCart.gif"]);*/
	dm.addEl("a", "overlayFundPick", "a_fundsSelected", ["text:Add Selected Funds", "href:#", "onclick:addGift('CP'); unHide('fundSelect'); return false;", "class:btn", "style:width:35%;"]);
	/*dm.addEl("br", "overlayFundPick");*/
	/*dm.addEl("a", "overlayFundPick", "a_nothanks", ["text:No thanks, search for more funds", "href:#", "onclick:removeOverlay(); unHide('fundSelect'); return false;", "class:btn"]);*/
	//dm.addOptionGroup( "priSelect", "priOption", fundCodeAr, fundNameAr, []); //THIS WAS USED BEFORE ENDOWMENT SPECIFICATION AND SHOULD BE USED FOR MOST SELECTS

	//dm.addAtts("priOption0", ['class:blueBG']);
	//window["alphaObject"].cameFrom = "";
}

function createDropDownPri(r)
{ 	
	//ADD THE OPTIONS FOR THE PRIORTIY
		
		var selectPath = document.getElementById("fundSelect");
		var selectIndex = selectPath.selectedIndex;
		var optionText = selectPath.options[selectIndex].text;
		var curCollege = optionText;
		var fundArray = r.inputresponsearray;
		
		createFundPopup(curCollege, "College Priorities", fundArray); //consolidated to this more reusable function(can now be used with scenarios)
		/*
		dm.addEl("h3", "overlayFundPick", "h3_fundheader", ["text:"+curCollege]);
		dm.addEl("h2", "overlayFundPick", "h2_fundheader", ["text:College Priority Funds"]);
		dm.addEl("br", "overlayFundPick");
		dm.addEl("p", "overlayFundPick", "p_instructions", ["text:Please select the funds you would like to give to and click the button 'Add Selected Funds' to move these items to your gift cart."]);
		dm.addEl("br", "p_instructions");
		//dm.addEl("a", "p_instructions", "a_endowedMeaning", ["text:What is", "href:#"]);
		var endowedHelpText = "The E symbol means a fund is endowed.  The principal, usually the original gift of a donor, is never spent.  A part of the investment income from MSU's investment of the principal (original gift) supports a program or programs at MSU.  The remaining investment income is returned to principal and retained as a hedge against inflation and to provide a reserve that can be drawn on when investment returns are low or negative.";
		addHelpPopup("p_instructions", "endowed", "What is", endowedHelpText);
		dm.addEl("img", "endowedhelpHolder", "img_endowedMeaning", ["src:images/endowedSymbolCart.gif"]);
		
		dm.addEl("ul", "overlayFundPick", "ul_fundcbHolder");
		var i = 0;
		while (i < r.inputresponsearray.length)
		{
			if(window["alphaObject"].cameFrom == "getQuickPickResponse") isGood = 0;
			
			if(	isGood != -1 )
			{
				
				//BELOW IS A GOOD EXAMPLE OF CREATING A LINK WITH A VARIABLE IN IT
				
				//THE LINK //dm.addEl("a",textHolder, "inputResponseLink"+i, ["href:javascript:changeInputText(\'"+typeQueryInput+"\', \'"+window["alphaObject"][window["alphaObject"].current][i][0]+"\', \'"+window["alphaObject"][window["alphaObject"].current][i][1]+"\' )", "class:dropLinks"] );
				var fundName = r.inputresponsearray[i][0];
				var fundCode =  r.inputresponsearray[i][1];
				var isEndowed =  r.inputresponsearray[i][2]; //1 for yes, 0 for no
				
				//alert(fundName);
				fundNameAr.push(fundName);
				fundCodeAr.push(fundCode);

				dm.addEl("li", "ul_fundcbHolder", "li_"+fundCode);
				//dm.addEl("span", "li_"+fundCode, "amountBoxHolder"+fundCode);
				//dm.addEl("label", "amountBoxHolder"+fundCode, "label_amount"+fundCode, ["text:$"]);
				//dm.addEl("input", "amountBoxHolder"+fundCode, "amountBox"+fundCode, ["type:text", "name:amountBox_funds", "class:amountBox", "size: 4", "maxlength: 10"]);
				
				dm.addEl("input", "li_"+fundCode, "cb_"+fundCode, ["type:checkbox", "name:cb_funds"]);
				//var cb = document.getElementById("cb_"+fundCode);
				//cb.onclick = unHide("amountBoxHolder"+fundCode);
				
				dm.addEl("label", "li_"+fundCode, "label_"+fundCode, ["text:"+fundName, "for:cb_"+fundCode]);
				if(isEndowed == 1) dm.addEl("img", "li_"+fundCode, "img_endSymbol_"+fundCode, ["src:images/endowedSymbolCart.gif", "title:This is an endowment fund", "alt:Endowment Fund"]);
				
				var cbPath = document.getElementById("cb_"+fundCode);
				cbPath.fundName = fundName;
				cbPath.fundCode = fundCode;
				if(isEndowed == 1) cbPath.isEndowed = 1;
				else cbPath.isEndowed = 0;
			}
			i++;
		}
		dm.addEl("a", "overlayFundPick", "a_cancelBtn", ["href:#", "onclick:removeOverlay(); unHide('fundSelect'); return false;", "class:corTR"]);
		dm.addEl("img", "a_cancelBtn", "img_cancelBtn", ["alt:Cancel", "title:Cancel", "src:images/xCart.gif"]);
		dm.addEl("a", "overlayFundPick", "a_fundsSelected", ["text:Add Selected Funds", "href:#", "onclick:addGift('CP'); unHide('fundSelect'); return false;", "class:btn"]);
		//dm.addOptionGroup( "priSelect", "priOption", fundCodeAr, fundNameAr, []); //THIS WAS USED BEFORE ENDOWMENT SPECIFICATION AND SHOULD BE USED FOR MOST SELECTS

		//dm.addAtts("priOption0", ['class:blueBG']);
		//window["alphaObject"].cameFrom = "";
		*/
}


/*
function readPriSelections()
{ 
	var cbPath = document.getElementsByName("cb_funds");
	//alert(cbPath.length);
	for (i = 0; i < cbPath.length; i++) {
		if (cbPath[i].checked) {
			addToCart(cbPath[i].fundName, cbPath[i].fundCode, cbPath[i].isEndowed, "CP");
		}
	}
	removeOverlay();
}*/

function createDropDown(r)
{
		var contentSpan = "contentSpan";
		var textHolder = "textHolder";
		var typeQueryInput = "typeQueryInput";
		document.getElementById("typeQueryInput").lastFocused = -1; //reset the last focused item to default value
		
	if(document.getElementById(textHolder))
	{
		dm.remElById(textHolder);
	}
	
	contentSpanPath = document.getElementById(contentSpan);
	if(contentSpanPath.style.display == "none") contentSpanPath.style.display = "block";
	dm.addEl( "div", contentSpan, textHolder, ["class:contentSpan"] );

	var isEverGood = false;
	var i = 0;
	while (i < r.inputresponsearray.length)
	{
		var curShortName = r.inputresponsearray[i][0];
		var curFundCode = r.inputresponsearray[i][1];
		var curIsEndowed = r.inputresponsearray[i][2]; //1 for yes, 0 for no
		var curObjectCode = r.inputresponsearray[i][3];
		var curAccountNumber = r.inputresponsearray[i][4];
		isEverGood = true;
		//BELOW IS A GOOD EXAMPLE OF CREATING A LINK WITH A VARIABLE IN IT
		//added \" to escape a single quote in a fund name
		
		var altClass;
		if(i % 2 == 0) altClass="evenTQColor";
		else altClass="oddTQColor";
		
		if(curIsEndowed == 1) dm.addEl("a","textHolder", "inputResponseLink"+i, ["href:#", "onclick:changeInputText('typeQueryInput', \""+curShortName+"\", \'"+curFundCode+"\', \'"+curIsEndowed+"\', \'"+curObjectCode+"\', \'"+curAccountNumber+"\' ); return false;", "class:dropLinks endowedImg "+altClass] );
		else dm.addEl("a","textHolder", "inputResponseLink"+i, ["href:#", "onclick:changeInputText('typeQueryInput', \""+curShortName+"\", \'"+curFundCode+"\', \'"+curIsEndowed+"\', \'"+curObjectCode+"\', \'"+curAccountNumber+"\' ); return false;", "class:dropLinks "+altClass] );
		
		
		//add the endowedImg class if its endowed, otherwise, just add the div
		//if(curIsEndowed == 1) dm.addEl( "span", "inputResponseLink"+i, "inputResponseShell"+i, ["class:inputResponseShell endowedImg", "text:"+divText] );
		//else 
		
		dm.addEl( "span", "inputResponseLink"+i, "inputResponseShell"+i, ["class:inputResponseShell ", "text:"+curShortName] );
		
		//attach the fund name, code, and endowed to each shell
		var irs = document.getElementById("inputResponseShell"+i);
		irs.fundName = curShortName;
		irs.fundCode = curFundCode;
		irs.isEndowed = curIsEndowed;
		irs.objectCode = curObjectCode;
		irs.accountNumber = curAccountNumber;
		i++;
	}
}

//document.getElementById("typeQueryInput").onkeyup = KeyCheck;       
function KeyCheck(e)
{
	//alert('key press');
	//get locations
	var tqEl = document.getElementById("typeQueryInput");
	
	//get key pressed
	var KeyID = (window.event) ? event.keyCode : e.keyCode;
	
	//which key pressed
	switch(KeyID)
	{
		case 40: //arrow down
	      	//alert("pressed down key");
			//if(tqEl.lastFocused) tqEl.lastFocused = 0;
			tqEl.lastFocused = tqEl.lastFocused + 1;
			
			//sudo focus the keyed over element
			if (document.getElementById("inputResponseLink" + (tqEl.lastFocused - 1))) {
				var sudoFocusElPrev = document.getElementById("inputResponseLink" + (tqEl.lastFocused - 1));
				sudoFocusElPrev.className = sudoFocusElPrev.oldClass;
			}
			
			if (document.getElementById("inputResponseLink" + (tqEl.lastFocused))) {
				var sudoFocusEl = document.getElementById("inputResponseLink" + tqEl.lastFocused);
				sudoFocusEl.oldClass = sudoFocusEl.className;
				sudoFocusEl.className += " inrsH";
				
				tqEl.value = document.getElementById("inputResponseShell" + tqEl.lastFocused).childNodes[0].nodeValue;
			}
			break;
		
		case 38:
			//alert("pressed up key");
			//if(tqEl.lastFocused) tqEl.lastFocused = 0;
			tqEl.lastFocused = tqEl.lastFocused - 1;
			
			//sudo focus the keyed over element
			if (document.getElementById("inputResponseLink" + (tqEl.lastFocused + 1))) {
				var sudoFocusElPrev = document.getElementById("inputResponseLink" + (tqEl.lastFocused + 1));
				sudoFocusElPrev.className = sudoFocusElPrev.oldClass;
			}
			
			if (document.getElementById("inputResponseLink" + (tqEl.lastFocused))) {
				var sudoFocusEl = document.getElementById("inputResponseLink" + tqEl.lastFocused);
				sudoFocusEl.oldClass = sudoFocusEl.className;
				sudoFocusEl.className += " inrsH";
				
				tqEl.value = document.getElementById("inputResponseShell" + tqEl.lastFocused).childNodes[0].nodeValue;
			}
			break;
		
		case 13:
			if(document.getElementById("inputResponseLink" + (tqEl.lastFocused) )) {
			var sudoFocusEl = document.getElementById("inputResponseShell" + (tqEl.lastFocused));
			changeInputText('typeQueryInput', sudoFocusEl.fundName, sudoFocusEl.fundCode, sudoFocusEl.isEndowed, sudoFocusEl.objectCode, sudoFocusEl.accountNumber );
			}
			break;
		
		
		
      
	  
	  default:
	  	chooseGetFnc(document.getElementById("typeQueryInput").value);
   }

}

//for removing the the typeQuery dropdown when something else on the page is clicked
function mouseDownAction(e)
{
	var targ;
	if (!e)
	  {
	  	var e = window.event;
	  }
	if (e.target)
	  {
	 	 targ = e.target;
	  }
	else if (e.srcElement)
	  {
	  	targ = e.srcElement;
	  }
	if (targ.nodeType == 3) // defeat Safari bug
	  {
	 	 targ = targ.parentNode;
	  }
	
	if (document.getElementById("textHolder") && targ.id != "textHolder" && targ.id != "contentSpan" && targ.id != "typeQueryInput") {
		setTimeout("dm.remElById(\'textHolder\'); hide(\'contentSpan\');", 500);
		//make the timeout longer than 100ms, because it needs time to register the click...
		//with a small timeframe, mouse clicks sometimes won't be picked up
	}
}

function modifyQueryInput(whichId, whatClass, whatText)
{
	//change which default text to be displayed in the inputbox
	var valueVar = document.getElementById(whichId).value;
	if( valueVar == searchTextDefault || valueVar == "")
	{
		//switchClass(whichId, whatClass);
		document.getElementById(whichId).value = whatText;
	}
}

function changeSearch(howToSearch)
{
	if( howToSearch == "begins" || howToSearch == "contains" )
	{
		dm.remElById( "descripArrow" );
        dm.remElById( "descripThen" );
	}
	else if( howToSearch == "description" && !document.getElementById("descripArrow") )
	{
		dm.addEl( "img", "descripBtn", "descripArrow", ["src:images/descripArrow.gif", "class:imgoffset", "alt:Search", "onclick:getInputResponseDes()" ] );
		dm.addEl( "span", "descripBtn", "descripThen", ["text: then ", "class:giftSubText"] );
	}
}

	//--------------GET ZIP FROM WEB SERVICE AND RETURN VALUES TO FIELDS------------//////
	function getZip(arg1)	{
		// send data to CF
		document.getElementById("donorCity").value = "Searching...";
		document.getElementById("donorState").value = "..";
		DWRUtil.useLoadingMessage();
		DWREngine._execute(_ajaxConfig._cfscriptLocation, null, 'getZip', arg1, showZipResult);
	}
	
	// call back function
	function showZipResult (r) {
		//sDumper(r);
		// set city and state
		document.getElementById("donorCity").value = r.city;
		document.getElementById("donorState").value = r.state;
		if(document.getElementById("donorCity").value != "Searching...") document.getElementById("donorCity").focus();
	}
