/*******************************************************************/
/*  THIS FUNCTION AUTOPOPULATES THE DEPARTMENT COMBO BOX BASED ON  */
/*  THE ITEM SELECTED IN THE UNIT COMBO BOX                        */
/*******************************************************************/
function funBuildDepartments(num) {
	document.mg_form.Departments.selectedIndex=0;
   	for(ctr=0;ctr<Departments[num].length;ctr++)
   		document.mg_form.Departments.options[ctr]=new Option(Departments[num][ctr],Accounts[num][ctr]);
    document.mg_form.Departments.length=Departments[num].length;
}

/*******************************************************************/
/*  THIS FUNCTION VALIDATES FIELDS BEFORE FORM CAN BE SUBMITTED    */
/*******************************************************************/
function funValidateAllData(form) {
	if (checkString(form.elements["FirstName"],'First Name', false) == false) return false;
    if (checkString(form.elements["LastName"],'Last Name', false) == false) return false;
	if (checkSSN(form.elements["SSN"], true) == false) return false;
	if (checkEmail(form.elements["Email"], true) == false) return false;
	if (checkString(form.elements["StreetAddress"],'Address 1', false) == false) return false;
	if (checkString(form.elements["StreetAddress1"],'Address 2', true) == false) return false;
	if (checkString(form.elements["StreetAddress2"],'Address 3', true) == false) return false;
    if (checkString(form.elements["City"],'City', false) == false) return false;
	if (checkStateField("donor", form) == false) return false;
	if (IsZipNeeded("donor", form) == false) return false;
	if (checkUSPhone(form.elements["Phone"], true) == false) return false;
    if (checkString(form.elements["BusStreetAddress"],'Employer Address 1', true) == false) return false;
   	if (checkString(form.elements["StreetAddress1"],'Employer Address 2', true) == false) return false;
    if (checkString(form.elements["StreetAddress2"],'Employer Address 3', true) == false) return false;
    if (checkString(form.elements["BusCity"],'Employer City', true) == false) return false;
	if (checkStateField("employer", form) == false) return false;
	if (IsZipNeeded("employer", form) == false) return false;
	if (checkUSPhone(form.elements["BusTelPhone"], true) == false) return false;
	if (checkAmount(form.elements["amount"].value,form.elements["amount"]) == false) return false;
	/*if (checkHonorMemorial(form) == false) return false;*/	
	document.mg_form.appealcode.value = getCookie("appeal");	
	return true;	
}

/*
function funCheckComments() {
	document.mg_form.Comments.value = document.mg_form.Comments.value.replace("  ","");
}

/*******************************************************************/
/*  THIS FUNCTION IS USED TO SEE IF A ZIP CODE IS REQUIRED         */
/*  THE FUNCTION IS USED BY ALL ZIP CODES FIELDS IN FROM           */
/*  THE VARIABLE "FROMZIP" DETERMINES WHICH ZIP WILL BE CHECKED    */
/*******************************************************************/
function IsZipNeeded(fromzip, form) {
	var IsItValid = false
	switch (fromzip) {
		case "donor":
			if (document.mg_form.Country.value == "US") { 
				IsItValid = checkZIPCode(form.elements["ZIP"], false);}
			else  {
				IsItValid = true; }
			return IsItValid;
		case "employer":
			return checkZIPCode(form.elements["BusZIP"], true);
		case "acknowledgement":
			if (document.mg_form.Country1.value == "US") {
				IsItValid = checkZIPCode(form.elements["ZIP1"], false); }
			else  {
				IsItValid = true; }
			return IsItValid;
	}
}

/*******************************************************************/
/*  THIS FUNCTION FORMATS THE AMOUNT FIELD AFTER AMOUNT IS         */
/*  CHANGED                                                        */
/*******************************************************************/
function formatCurrency(num) {
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num)) num = "0";
	sign = (num == (num = Math.abs(num)));
	if (sign==false) return "$0.00"
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10) cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
		num = num.substring(0,num.length-(4*i+3))+','+ num.substring(num.length-(4*i+3));
	return ('$' + num + '.' + cents);
}

/*******************************************************************/
/*  THIS FUNCTION VALIDATES THE AMOUNT FIELD AFTER AMOUNT IS       */
/*  CHANGED                                                        */
/*******************************************************************/
function checkAmount(num, amtfield) {
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num)) num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10) cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
		num = num.substring(0,num.length-(4*i+3))+','+ num.substring(num.length-(4*i+3));
	if (num <= 0) {
		alert("The gift amount must be greater than $0.00. Please re-enter gift amount.");
		document.mg_form.amount.focus();		
		document.mg_form.amount.select();		
		return false }
	else return true
}

/*******************************************************************/
/*  THIS FUNCTION CHECKS THE 'IN MEMORY OF' BOX                    */
/*  AND ENABLES THE ACKNOWLEDGEMENT CONTACT FIELDS                 */
/*******************************************************************/
function ScanCheckBoxes1() {
	if (document.mg_form.checkbox1.checked) {
		document.mg_form.Memory.disabled=false;
		document.mg_form.Honor.value = "";
		document.mg_form.Honor.disabled=true;
		document.mg_form.checkbox2.checked=false;
		document.mg_form.FName.disabled=false;
		document.mg_form.LName.disabled=false;
		document.mg_form.Address1.disabled=false;
		document.mg_form.Address2.disabled=false;
		document.mg_form.Address3.disabled=false;
  		document.mg_form.City1.disabled=false;
   		document.mg_form.State1.disabled=false;
		document.mg_form.ZIP1.disabled=false;
		document.mg_form.Country1.disabled=false;
		document.mg_form.Memory.focus(); 		}
	else {
		document.mg_form.Memory.disabled=true;
		document.mg_form.FName.disabled=true;
		document.mg_form.LName.disabled=true;
		document.mg_form.Address1.disabled=true;
		document.mg_form.Address2.disabled=true;
		document.mg_form.Address3.disabled=true;		
  		document.mg_form.City1.disabled=true;
   		document.mg_form.State1.disabled=true;
		document.mg_form.Country1.disabled=true;		
		document.mg_form.ZIP1.disabled=true; }
}

/*******************************************************************/
/*  THIS FUNCTION CHECKS THE 'IN HONOR OF' BOX                     */
/*  AND ENABLES THE ACKNOWLEDGEMENT CONTACT FIELDS                 */
/*******************************************************************/
function ScanCheckBoxes2() {
	if (document.mg_form.checkbox2.checked) {
	    document.mg_form.Honor.disabled=false;
	    document.mg_form.Memory.value = "";
		document.mg_form.Memory.disabled=true;
	    document.mg_form.checkbox1.checked=false;
		document.mg_form.FName.disabled=false;
		document.mg_form.LName.disabled=false;
		document.mg_form.Address1.disabled=false;
		document.mg_form.Address2.disabled=false;
		document.mg_form.Address3.disabled=false;
  		document.mg_form.City1.disabled=false;
   		document.mg_form.State1.disabled=false;
		document.mg_form.ZIP1.disabled=false;
		document.mg_form.Country1.disabled=false;
		document.mg_form.Honor.focus(); }
	else {
		document.mg_form.Honor.disabled=true;
		document.mg_form.FName.disabled=true;
		document.mg_form.LName.disabled=true;
		document.mg_form.Address1.disabled=true;
		document.mg_form.Address2.disabled=true;
		document.mg_form.Address3.disabled=true;		
  		document.mg_form.City1.disabled=true;
   		document.mg_form.State1.disabled=true;
		document.mg_form.Country1.disabled=true;		
		document.mg_form.ZIP1.disabled=true; }
}

/*******************************************************************/
/*  THIS FUNCTION VALIDATES THE TEXTBOXES FOR THE ACKNOWLEDGMENT   */
/*  BOXES TO MAKE SURE THE REQUIRED FIELDS ARE FILLED IN           */
/*******************************************************************/
/*function checkHonorMemorial(form) {
	if (document.mg_form.checkbox1.checked)
		return (checkString(form.elements["Memory"],'In Memory Name', false) &&
				checkString(form.elements["FName"],'Acknowledgement Contact First Name',false) &&
		        checkString(form.elements["LName"],'Acknowledgment Contact Last Name', false) &&
			    checkString(form.elements["Address1"],'Acknowledgment Contact Address Line 1', false) &&
	        	checkString(form.elements["Address2"],'Acknowledgment Contact Address Line 2', true) &&
	        	checkString(form.elements["Address3"],'Acknowledgment Contact Address Line 3', true) &&				
		        checkString(form.elements["City1"],'Acknowledgment Contact City', false) &&
				checkStateField("acknowledgement", form) &&
				IsZipNeeded("acknowledgement", form))
	else {  
		if (document.mg_form.checkbox2.checked)
		    return (checkString(form.elements["Honor"],'In Honor Name', false) &&
				checkString(form.elements["FName"],'Acknowledgement Contact First Name',false) &&
        		checkString(form.elements["LName"],'Acknowledgment Contact Last Name', false) &&
			    checkString(form.elements["Address1"],'Acknowledgment Contact Address Line 1', false) &&
   		    	checkString(form.elements["Address2"],'Acknowledgment Contact Address Line 2', true) &&
	        	checkString(form.elements["Address3"],'Acknowledgment Contact Address Line 3', true) &&				
    		    checkString(form.elements["City1"], 'Acknowledgment Contact City',false) &&
				checkStateField("acknowledgement", form) &&
				IsZipNeeded("acknowledgement", form))
		else return true; }	
}
*/
/*******************************************************************/
/*  THIS FUNCTION VALIDATES THE STATE FIELD. IT ALSO CHECKS TO     */
/*  SEE IF THE COUNTRY AND STATE CONBINATION ARE VALID OTHERWISE   */
/*  IT CORRECTS IT.                                                */
/*******************************************************************/
function checkStateField(fromState, form) {
	var IsItValid = false
	switch (fromState) {
		case "donor":	
			if (document.mg_form.Country.value == "US") {
				if (document.mg_form.State.value == "") {
				    alert("Please enter your Address' State. The State field is required if the country is US. Please enter it now.");
					document.mg_form.State.focus();		
					return false; }
				else return true; }
			else {
				document.mg_form.State.options[0].selected=true;
				return true; }
		case "employer":	
			if (document.mg_form.BusCountry.value == "US") {
				if (document.mg_form.BusState.value == "") {
				    alert("Please enter your Employer's State. The State field is required if the country is US. Please enter it now.");
					document.mg_form.BusState.focus();		
					return false; }
				else return true; }
			else {
				document.mg_form.BusState.options[0].selected=true;
				return true; }
		case "acknowledgement":
			if (document.mg_form.State1.value == "") {
				if (document.mg_form.Country1.value == "US") {
				    alert("Please enter the Acknowledgement Contact's State. The State field is required if the country is US. Please enter it now.");
					document.mg_form.State1.focus();		
					return false; }
				if (document.mg_form.Country1.value == "") {
				    alert("Please enter the Acknowledgement Contact's Country. Please enter it now.");
					document.mg_form.Country1.focus();		
					return false; } }
			if (document.mg_form.Country1.value == "US") {
				document.mg_form.Country1.options[1].selected=true }
			return true; 
	}
} 

/*******************************************************************/
/*  THIS FUNCTION SEARCHES FOR THE COOKIE, AND IF IT IS FOUND IT   */
/*  RETURNS THE APPEAL CODE, IF IT IS NOT FOUND IT RETURNS '00275' */
/*******************************************************************/
function getCookie(name) {
	var appealNum = "";
	var dc = document.cookie;
	var prefix = name + "=";
	var begin = dc.indexOf("; " + prefix);	
	if (begin == -1) {
    	var begin = dc.indexOf(prefix);
	    if (begin != 0)
			return "00275";}
	else
	    begin += 2;
	var end = document.cookie.indexOf(";", begin);
	if (end == -1) {
    	end = dc.length;     	
		var foundcookie = unescape(dc.substring(begin + prefix.length, end));
	
		return (foundcookie);}
		
}

/*******************************************************************/
/*  THIS FUNCTION CHECKS THE URL TO SEE IF THERE'S AN APPEAL CODE  */
/*  IF THERE IS THEN A COOKIE IS CREATED                           */
/*******************************************************************/
function funCheckForCookie(strCookieName) {	
	var loc = document.location.href	
	if ((loc.indexOf("appeal=")+7) != 6) {				
		var fromunit =loc.substring(loc.indexOf(strCookieName+"=")+7);
		document.cookie = strCookieName + "=" + (fromunit) + "; path=/";
								
	}
}

/*******************************************************************/
/*  THIS FUNCTION CHECKS TO SEE IF A COMPANY NAME HAS BEEN ENTERED */
/*  THEN IF NOT EMPTY LAUNCHES NEW PAGE WITH HEP URL               */
/*******************************************************************/
function funHEPLookup (strLocation) {
	if (document.mg_form.HEPSearch.value.length == 0) {
   		alert("You must enter a Company Name first.") }
	else {
		if (strLocation=="MAIN")
	   		hepUrl = "http://www.matchinggifts.com/umiami/giftdb.cfm?INPUT_ORGNAME=" + document.mg_form.HEPSearch.value + "&eligible=ALL";
		else
			hepUrl = "http://www.matchinggifts.com/umiami/giftdb2.cfm?INPUT_ORGNAME=" + document.mg_form.HEPSearch.value + "&eligible=ALL";
	   	window.open(hepUrl,'HEPSearch','toolbar=no,location=no,scrollbars=yes,width=600,height=450'); }
}