window.onload = onLoadStuff;

window.searchTextDefault = "Type Fund Name here";

var currentPage = "news";
var currentDonationPage = "fundPage";
var thefkey = 0;

function alphaObject()
{
	this.inputText = "";
	this.current = "";
	this.cameFrom = "";
	this.queryLength = 4;
}
var alphaObject = new alphaObject(); //object that holds all typequery information

var curAlt = "listAltCol1"; //alternating list color class for giftcart
var curAltError = "listAltError1"; //alternating list color class for process errors

var giftArray = new Array();
function giftObject(fundName, fundCode, isEndowed, origin)
{
	this.fundName = fundName; //contains the shortname of the fund
	this.fundCode = fundCode; //contains the code for the fund (i.e. A700)
	if(isEndowed) this.isEndowed = isEndowed;
	else this.isEndowed = 0;
	if(origin) this.origin = origin;
	else this.origin = "";
	this.amount = 0;
	this.creditTo = "Donor"; //options are donor, spouse, or both
	this.deptCode = 0;
	this.accountNumber = 0;
}

function elementObject(fieldName, fieldValue, fieldType, paySourceNum)
{
	this.fieldName = fieldName;
	this.fieldValue = fieldValue;
	this.fieldType = fieldType;
	this.paySourceNum = paySourceNum;
}

/*jQuery(document).ready(function() {
	
});*/

jQuery(document).ready(function() {

	jQuery('#bmMinMaxLink').click(function(){
		jQuery('#browserMessageContents').slideToggle('slow');
		return false;
	});
	
	//jQuery('a.helpLink').cluetip({splitTitle: '|'});
	/*jQuery('#helpLink').cluetip({
		splitTitle: '|',
		showTitle: false	
	});*/
	
});

function onLoadStuff()
{
	dm = new domMan();
	
	//PREP THE TYPEQUERYBOX SO IT DOESNT HOLD REMEMBERED INFORMATION
	switchClass('typeQueryInput', 'greyText');
	document.getElementById('contentSpan').numTextHolder = 0;
	document.getElementById('typeQueryInput').value = searchTextDefault;
	document.getElementById('typeQueryInput').curGift = new giftObject("", 0);
	if (document.getElementById('fundPage')) {
		//If a company is being passed in to match gifts with
		//var sid = document.getElementById("sid").value;
		
		initScenarioFunds(); //handle if a scenario was passed in
		if(document.getElementById("donorCompany").value != "")
		{
			
			document.getElementById('matchRadioYes').checked = true;
			document.getElementById('matchRadioNo').checked = false; // I put this here to insure matchRadioNo is set to false because it is checked by default.
			unHide("matchTextHolder");
			
			document.getElementById("donorCompany").disabled = true;
			
				//1 for someone entering in a matching gift
				//2 for someone being pushed from a matching gift site
				//3 [matchGift=3] for donorCompany being pushed via URL but NO ONLINE MATCHING GIFT FORM, therefore contact the HR department
				// Example:  http://www.givingto.msu.edu/labs/giftCart_v29/index.cfm?match=BDO%20Seidman,%20LLP&matchGift=3
				//4 [matchGift=4] for donorCompany being pushed via URL but NO MATCHING GIFT PROGRAM, therefore  treat this like a normal gift
			var matchGift = document.getElementById('matchGift').value;
			switch (matchGift) 
				{ 
	   				case "3" : 
					document.getElementById('matchRadioYes').value = "3";
					break;
	   				case "4" : 
					document.getElementById('matchRadioYes').value = "4";
					break;
	   				default: 
					document.getElementById('matchRadioYes').value = "2";
				}
		}
		
		if (document.getElementById("gradRadioYes").checked == true) {
			unHide("gradTextHolder");
		}
		
		if (document.getElementById("memoryRadioYes").checked == true) {
			prepMemoryTextHolder();
		}
		
		addGift('DIG');
		
		//document.getElementById("typeQueryInput").onkeyup = KeyCheck; 
		//document.getElementById("typeQueryInput").onkeypress = KeyCheck;
		//document.getElementById("typeQueryInput").onkeypress = function() { alert('hey'); }
		document.getElementById("typeQueryInput").lastFocused = -1;
			
	}
	
	document.onmousedown = mouseDownAction;
}

function toggleDisplay(id)
{
	elPath = document.getElementById(id);
	if (elPath.style.display != "block") {
		/*alert("not eq to block");*/
		elPath.style.display = "block";
	}
	else 
	{
		elPath.style.display = "none";
		/*alert("eq to block");*/
	}
		
}

function hide(id)
{
	elPath = document.getElementById(id);
	elPath.style.display = "none";
}

function unHide(id)
{
	elPath = document.getElementById(id);
	elPath.style.display = "block";
}

function createOverlay(idName)
{
	
	dm.addEl("div", "body", "theOverlay", ["class:overlay"]);
	dm.addEl("div", "body", "overlayContentHolder", ["class:overlayContentHolder"]);
	dm.addEl("div", "overlayContentHolder", idName, ["class:overlayContent"]);
	//dm.addEl("a", "overlayContentHolder", "closeOverlayLink", ["href:javascript:removeOverlay()", "text:Close"]);
}

function removeOverlay()
{
	dm.remElById("theOverlay");
	dm.remElById("overlayContentHolder");
}

function switchClass(whichId, whatClass)
{
	if( document.getElementById(whichId) )
	{
		document.getElementById(whichId).setAttribute("class", whatClass);
		document.getElementById(whichId).setAttribute("className", whatClass);
	}
}

function switchDonationPage(whichPage)
{
	//things to do when leaving the fundPage
	//alert("switching");
	var proceed = pageCheck(currentDonationPage);
	if(proceed == true) //only proceeds if all conditions are met and it actually needs to go to another page
	{
		if(currentDonationPage == "fundPage") preparePaymentPage();	
		
		document.getElementById(whichPage).setAttribute("class", "currentPage");
		document.getElementById(whichPage).setAttribute("className", "currentPage");
		
		document.getElementById(currentDonationPage).setAttribute("class", "somePage");
		document.getElementById(currentDonationPage).setAttribute("className", "somePage");
		
		window.currentDonationPage = whichPage;
	}
}

function pageCheck(curPage)
{
	var proceed = true;
	var i;
	var errors = new Array();

	/*FUND PAGE CHECKS*/
	////////////////////
	if(curPage == "fundPage")
	{
		if(giftArray.length >= 1) //check to make sure something is in the giftcart
		{
			i = 0;
			while(i < giftArray.length)
			{
				if(giftArray[i].amount == 0) //check to make sure a donation is worth something
				{
					errors.push("One or more of the selected gifts has a donation amount of $0.");
					proceed = false;
					break;
				}
				i++;
			}//end while
		}//end if
		else 
		{
			errors.push("You have no items in the gift cart.");
			proceed = false;
		}//end else
	}//end of fundPage if
	
	i = 0;
	if(document.getElementById("pageErrors")) dm.remElById("pageErrors");
	if(errors.length >= 1) 
	{
		proceed = false;
		dm.addElement('div', 'pageErrorsList', 'pageErrors')
	}
	while(i < errors.length)
	{
		dm.addElement('li', 'pageErrors', ["pageError"+i], ['class:giftErrorText', 'fadeIn:20', 'text:'+errors[i] ])
		i++;
	}
	
	
	/*PAYMENT PAGE CHECKS*/
	if(curPage == "paymentPage")
	{
		getRequired();
		
		proceed = false;
	}
	/*end of PAYMENT PAGE CHECKS*/
	
	return proceed;
}

//checks if a string is just numbers
function IsNumeric(sText)
{
	var ValidChars = "0123456789.";
	var IsNumber=true;
	var Char;

	for (i = 0; i < sText.length && IsNumber == true; i++) 
	{ 
		Char = sText.charAt(i); 
		if (ValidChars.indexOf(Char) == -1) 
		{
			IsNumber = false;
		}
	}
	if(sText.length == 0) IsNumber = false;
	//alert(IsNumber);
	return IsNumber;
 }

function selectOther()
{
	selectLoc = document.getElementById("gradConferred");
	curOptValue = selectLoc.options[selectLoc.selectedIndex].value;
	curOptId = selectLoc.options[selectLoc.selectedIndex].id;
	
	if(curOptId == "otherConferred") //if the other option is chosen
	{
		if(!document.getElementById("otherInput")) //if the other input isnt already there make it
		{
			var onblur = "document.getElementById('otherConferred').value = this.value";
			dm.addEl("input", "gradConferredHolder", "otherInput", ["type:text", "size:25", "maxlength:50", "onblur:"+onblur]);	
			selectLoc.options[selectLoc.selectedIndex].value = "blank"; //initialize the other box to nothing
		}	
	}
	else if(document.getElementById("otherInput")) //if something else is chosen and the other input box is there get rid of it
	{
		dm.remElById("otherInput");
	}
		
}

function prepMemoryTextHolder()
{
//sDumper(document.forms[0].giftAssociatedCode[0].value, document.forms[0].giftAssociatedCode[1].value, document.forms[0].giftAssociatedCode[2].value);
	unHide('memoryTextHolder'); 
	//document.getElementById('IMO').checked = true; 
	document.forms[0].giftAssociatedCode[0].checked = true; 
	inMemFields('IMO')
}

function inMemFields(whatType)
{
	dm.remInById("memoryDynamic");
	
	if(whatType=="IMO") dm.addEl("label", "memoryDynamic", "dMN1Label", ["class:autoWidth", "text:Name of person being remembered: *"]);
	else if(whatType=="IHO") dm.addEl("label", "memoryDynamic", "dMN1Label", ["class:autoWidth", "text:Name of person being honored: *"]);
	else if(whatType=="IMP") dm.addEl("label", "memoryDynamic", "dMN1Label", ["class:autoWidth", "text:Name of pet: *"]);
	dm.addEl("input", "memoryDynamic", "giftAssociatedName1", ["name:giftAssociatedName1", "size:20", "maxlength:50"]);
	dm.addEl("br", "memoryDynamic");
	if(whatType=="IMP") 
	{
		dm.addEl("label", "memoryDynamic", "dMC1Label", ["class:autoWidth", "text:Type of pet: "]);
		dm.addEl("input", "memoryDynamic", "giftAssociatedCode1", ["name:giftAssociatedCode1", "size:20", "maxlength:50"]);
		dm.addEl("br", "memoryDynamic");
	}
	
	dm.addEl("br", "memoryDynamic");
	
	if(whatType=="IMO") dm.addEl("h3", "memoryDynamic", "ackHeader", ["text:Name and Address of the person to be acknowledged (i.e. next of kin)"]);
	else if(whatType="IHO") dm.addEl("h3", "memoryDynamic", "ackHeader", ["text:Name and Address of the person to be acknowledged"]);
	else if(whatType="IMP") dm.addEl("h3", "memoryDynamic", "ackHeader", ["text:Name and Address of pet owner"]);
	dm.addEl("label", "memoryDynamic", "dMNLabel", ["text:Name: *"]);
	dm.addEl("input", "memoryDynamic", "giftAssociatedName", ["name:giftAssociatedName", "size:20", "maxlength:50"]);
	dm.addEl("br", "memoryDynamic");
	
	dm.addEl("label", "memoryDynamic", "dMALabel", ["text:Address:  "]);
	dm.addEl("input", "memoryDynamic", "giftAssociatedAddress", ["name:giftAssociatedAddress", "size:40", "maxlength:60"]);
	dm.addEl("br", "memoryDynamic");
	
	dm.addEl("label", "memoryDynamic", "dMCLabel", ["text:City: "]);
	dm.addEl("input", "memoryDynamic", "giftAssociatedCity", ["name:giftAssociatedCity", "size:15", "maxlength:30"]);
	dm.addEl("br", "memoryDynamic");
	
	dm.addEl("label", "memoryDynamic", "dMSLabel", ["text:State: "]);
	dm.addEl("input", "memoryDynamic", "giftAssociatedState", ["name:giftAssociatedState", "size:4", "maxlength:3"]);
	dm.addEl("br", "memoryDynamic");
	
	dm.addEl("label", "memoryDynamic", "dMZLabel", ["text:Zip: "]);
	dm.addEl("input", "memoryDynamic", "giftAssociatedZipCode", ["name:giftAssociatedZipCode", "size:11", "maxlength:10"]);
	dm.addEl("br", "memoryDynamic");
}

/*--- =========================== ---*/
function inMemFields_BACKUP(whatType)
{
	dm.remInById("memoryDynamic");
	
	if(whatType=="IMO") dm.addEl("label", "memoryDynamic", "dMN1Label", ["class:autoWidth", "text:Name of person being remembered: *"]);
	else if(whatType=="IHO") dm.addEl("label", "memoryDynamic", "dMN1Label", ["class:autoWidth", "text:Name of person being honored: *"]);
	else if(whatType=="IMP") dm.addEl("label", "memoryDynamic", "dMN1Label", ["class:autoWidth", "text:Name of pet: *"]);
	dm.addEl("input", "memoryDynamic", "giftAssociatedName1", ["name:giftAssociatedName1", "size:20", "maxlength:50"]);
	dm.addEl("br", "memoryDynamic");
	if(whatType=="IMP") 
	{
		dm.addEl("label", "memoryDynamic", "dMC1Label", ["class:autoWidth", "text:Type of pet: "]);
		dm.addEl("input", "memoryDynamic", "giftAssociatedCode1", ["name:giftAssociatedCode1", "size:20", "maxlength:50"]);
		dm.addEl("br", "memoryDynamic");
	}
	
	dm.addEl("br", "memoryDynamic");
	
	if(whatType=="IMO") dm.addEl("h3", "memoryDynamic", "ackHeader", ["text:Name and Address of the person to be acknowledged (i.e. next of kin)"]);
	else if(whatType="IHO") dm.addEl("h3", "memoryDynamic", "ackHeader", ["text:Name and Address of the person to be acknowledged"]);
	else if(whatType="IMP") dm.addEl("h3", "memoryDynamic", "ackHeader", ["text:Name and Address of pet owner"]);
	dm.addEl("label", "memoryDynamic", "dMNLabel", ["text:Name: *"]);
	dm.addEl("input", "memoryDynamic", "giftAssociatedName", ["name:giftAssociatedName", "size:20", "maxlength:50"]);
	dm.addEl("br", "memoryDynamic");
	
	dm.addEl("label", "memoryDynamic", "dMALabel", ["text:Address: *"]);
	dm.addEl("input", "memoryDynamic", "giftAssociatedAddress", ["name:giftAssociatedAddress", "size:40", "maxlength:60"]);
	dm.addEl("br", "memoryDynamic");
	
	dm.addEl("label", "memoryDynamic", "dMCLabel", ["text:City: *"]);
	dm.addEl("input", "memoryDynamic", "giftAssociatedCity", ["name:giftAssociatedCity", "size:15", "maxlength:30"]);
	dm.addEl("br", "memoryDynamic");
	
	dm.addEl("label", "memoryDynamic", "dMSLabel", ["text:State: *"]);
	dm.addEl("input", "memoryDynamic", "giftAssociatedState", ["name:giftAssociatedState", "size:4", "maxlength:3"]);
	dm.addEl("br", "memoryDynamic");
	
	dm.addEl("label", "memoryDynamic", "dMZLabel", ["text:Zip: *"]);
	dm.addEl("input", "memoryDynamic", "giftAssociatedZipCode", ["name:giftAssociatedZipCode", "size:11", "maxlength:10"]);
	dm.addEl("br", "memoryDynamic");
}


