﻿function validate_required(field,alerttxt) {
	with (field) {
		if (value==null||value==""||value==" "||value=="select an affiliation...") {
			alert(alerttxt);
			return false;
		} else {
			return true;
		}
	}
}

function validate_form(thisform) {
	with (thisform) {
		/* personal information */
		if (document.getElementById('fname')) {
			if (validate_required(fname,"Please enter your first name.")==false) {
				fname.focus();
				return false;
			}
		}

		if (document.getElementById('lname')) {
			if (validate_required(lname,"Please enter your last name.")==false) {
				lname.focus();
				return false;
			}
		}
		if (document.getElementById('email')) {
			if (validate_required(email,"Please enter your MIT email.")==false) {
				email.focus();
				return false;
			}
		}		
		if (document.getElementById('phone')) {
			if (validate_required(phone,"Please enter your phone number.")==false) {
				phone.focus();
				return false;
			}
		}		
		if (document.getElementById('group')) {
			if (validate_required(group,"Please enter the name of your group.")==false) {
				group.focus();
				return false;
			}
		}		
		if (document.getElementById('affiliation')) {
			if (validate_required(affiliation,"Please select your MIT affiliation.")==false) {
				affiliation.focus();
				return false;
			}
		}		

		if (document.getElementById('address')) {
			if (validate_required(address,"Please enter your MIT address.")==false) {
				address.focus();
				return false;
			}
		}		
		if (document.getElementById('ptitle')) {
			if (validate_required(ptitle,"Please enter the title of your project.")==false) {
				ptitle.focus();
				return false;
			}
		}
		if (document.getElementById('plocation')) {
			if (validate_required(plocation,"Please enter the location of your project.")==false) {
				plocation.focus();
				return false;
			}
		}		

		if (document.getElementById('advisor')) {
			if (validate_required(advisor,"Please enter the name of your advisor.")==false) {
				advisor.focus();
				return false;
			}
		}		
		if (document.getElementById('pstart')) {
			if (validate_required(pstart,"Please enter the start date for your project.")==false) {
				pstart.focus();
				return false;
			}
		}		
		if (document.getElementById('pend')) {
			if (validate_required(pend,"Please enter the end date for your project.")==false) {
				pend.focus();
				return false;
			}
		}		
		if (document.getElementById('pdesc')) {
			if (validate_required(pdesc,"Please enter a description for your project.")==false) {
				pdesc.focus();
				return false;
			}
		}	
		if (document.getElementById('pcomm')) {
			if (validate_required(pcomm,"Please let us know how your project will involve the MIT community.")==false) {
				pcomm.focus();
				return false;
			}
		}

		// check "itemized" table
		var rowCount = document.getElementById("itemized").getElementsByTagName("tr").length + 5;

		// get values from all "amount fields"
		for (var i = 1;i < rowCount;i++) {
			var type = 'p'+i+'_expense';
			var total = 'p'+i+'_amount';
			var request = 'p'+i+'_amount-request';
			var cash_source = 'p'+i+'_cash-source';
			var cash_amount = 'p'+i+'_cash-amount';
			var inkind_source = 'p'+i+'_inkind-source';
			var inkind_amount = 'p'+i+'_inkind-amount';
			if (document.getElementById(type)) {
				var type_o = document.getElementById(type);
				var total_o = document.getElementById(total);
				var request_o = document.getElementById(request);
				var cash_source_o = document.getElementById(cash_source);
				var cash_amount_o = document.getElementById(cash_amount);
				var inkind_source_o = document.getElementById(inkind_source);
				var inkind_amount_o = document.getElementById(inkind_amount);
				/*
				// if any field is filled, make sure type, total, and request are filled				
				if (type_o.value || total_o.value || request_o.value || cash_source_o.value || cash_amount_o.value || inkind_source_o.value || inkind_amount_o.value) {
					if (!type_o.value || !total_o.value) {
						alert('Please make sure the values for "project expenses" are entered on line '+i+' or delete all values from this line.');
						if (!type_o.value) {
							type_o.focus();
						} else if (!total_o.value) {
							total_o.focus();
						} else {
							request_o.focus();
						}
						return false;
					}
				}
				*/
				// make sure cash and inkind have both fields filled out if one is filled out
				if (cash_source_o.value || cash_amount_o.value) {
					if (!cash_source_o.value || !cash_amount_o.value) {
						alert('Please make sure both the source and amount for "cash contributions" are entered on line '+i+' or delete all values from "cash contributions" on this line.');
						if (!cash_source_o.value) {
							cash_source_o.focus();
						} else {
							cash_amount_o.focus();
						}
						return false;						
					}
				}
				if (inkind_source_o.value || inkind_amount_o.value) {
					if (!inkind_source_o.value || !inkind_amount_o.value) {
						alert('Please make sure both the source and amount for "in-kind contributions" are entered on line '+i+' or delete all values from "in-kind contributions" on this line.');
						if (!inkind_source_o.value) {
							inkind_source_o.focus();
						} else {
							inkind_amount_o.focus();
						}
						return false;						
					}
				}				
				/*
				// check amounts
				if (total_o.value) {
					var contribs = 0;
					var total_request = parseFloat(total_o.value);
					if (request_o.value) {
						contribs = contribs + parseFloat(request_o.value);
					}
					if (cash_amount_o.value) {
						contribs = contribs + parseFloat(cash_amount_o.value);
					}
					if (inkind_amount_o.value) {
						contribs = contribs + parseFloat(inkind_amount_o.value);
					}
					contribs = Math.round(contribs*100)/100;
					total_request = Math.round(total_request*100)/100;		
					if (contribs != total_request) {
						alert('Please make sure the total for your requested amount and contributions equal your expenses on line '+i+'.');
						request_o.focus();
						return false;
					}
				}
				*/
			} // if
		} // for
		
		// check totals
		var total_o = document.getElementById('t_amount');
		var request_o = document.getElementById('t_amount-request');
		var other_o = document.getElementById('t_contribution');		
		if (total_o.value) {
			var total_cost =  Math.round(parseFloat(total_o.value)*100)/100;
			var requested_amt =  Math.round(parseFloat(request_o.value)*100)/100;
			var other_amt =  Math.round(parseFloat(other_o.value)*100)/100;
				
			if ( (other_amt + requested_amt) != total_cost) {
				alert('Please make sure the total amount ($'+requested_amt+') requested from the Council for the Arts and the total amount requested from other sources ($'+other_amt+') equals the total cost ($'+total_cost+').');
				return false;
			} 
		}		
		
		// validate checkboxes
		for (var i = 1;i < 6;i++) {
			var req = 'check_req'+i;
			var box = document.getElementById(req);
			if (box) {
				if (!box.checked) {
					alert('Please read and check off all items in the checklist.');
					box.focus();
					return false;
				}
			}
		}
		
		
	} // with
} // validate_form