function trim(s) {
		while (s.substring(0,1) == ' ') {
		  s = s.substring(1,s.length);
		}
		while (s.substring(s.length-1,s.length) == ' ') {
		  s = s.substring(0,s.length-1);
		}
		return s;
	}

function validater(){ //validates against empty required fields and field lengths
	var frm = document.registration;
	var intErr = 0;

	if(trim(frm.name_first.value)==''){
		alert('Please provide a first name.');
		frm.name_first.focus();
		intErr = 1;
		return false;
		}

	if(trim(frm.name_last.value)==''){
		alert('Please provide a last name.');
		frm.name_last.focus();
		intErr = 1;
		return false;
		}

	/*if(trim(frm.suffix.value)==''){
		alert('Please provide an suffix.');
		frm.suffix.focus();
		intErr = 1;
		return false;
		}

	if(trim(frm.speciality.value)==''){
		alert('Please select a speciality.');
		frm.speciality.focus();
		intErr = 1;
		return false;
		}


	if(trim(frm.address.value)==''){
		alert('Please provide an address.');
		frm.address.focus();
		intErr = 1;
		return false;
		}

	if(trim(frm.city.value)==''){
		alert('Please provide a city.');
		frm.city.focus();
		intErr = 1;
		return false;
		}

	if(trim(frm.state.value)=='' && trim(frm.country.value)=='United-States'){
		alert('Please select a state.');
		frm.state.focus();
		intErr = 1;
		return false;
		}

	if(trim(frm.zipcode.value)==''){
		alert('Please provide a zipcode.');
		frm.zipcode.focus();
		intErr = 1;
		return false;
		}

	if(trim(frm.phone.value)==''){
		alert('Please provide a phone number.');
		frm.phone.focus();
		intErr = 1;
		return false;
		}*/

	if(trim(frm.email_address.value)==''){
		alert('Please provide an Email Address.');
		frm.email_address.focus();
		intErr = 1;
		return false;
		}

	if(trim(frm.password.value)==''){
		alert('Please provide a Password.');
		frm.password.focus();
		intErr = 1;
		return false;
		}
	
	/*
	if(trim(frm.program_discovery.value)==''){
		alert('Please tell us how you heard about us.');
		frm.program_discovery.focus();
		intErr = 1;
		return false;
		}*/
	
	var emailFilter=/^.+@.+\..{2,3}$/;
	if (!(emailFilter.test(frm.email_address.value))) { 
		alert('Please provide a valid email address.  This address will remain confidential.');
		frm.email_address.focus();
		intErr = 1;
		return false;
		}

	var illegalChars= /[\(\)\<\>\,\;\:\\\/\"\[\]]/
	if (frm.email_address.value.match(illegalChars)) {
		alert('The email address contains invalid characters.  Please provide a valid email address.');
		frm.email_address.focus();
		intErr = 1;
		return false;
		}

	if(intErr < 1){return true;} else {return false;}
}

function validateFileType() {
	var frm = document.getElementById('application_upload_form');
	if (!frm) return true;

	if (frm.file_type.value == '') {
		alert('Please select a file type!');
		frm.file_type.focus();
		return false;
	}

	return true;
}

function toggleCommentsDiv(imgObj) {
	if (!imgObj) return;

	var tdObj = imgObj.parentNode;
	if (!tdObj) return;

	var divObjs = tdObj.getElementsByTagName('div');
	if (!divObjs) return;
	if (!divObjs.length) return;

	var divObj = divObjs[0];

	// toggle the divObj
	if (divObj.style.visibility == 'hidden') {
		divObj.style.visibility = 'visible';
		divObj.style.display = 'block';
		imgObj.src = 'images/collapse.png';
	} else {
		divObj.style.visibility = 'hidden';
		divObj.style.display = 'none';
		imgObj.src = 'images/expand.png';
	}
}

function saveReviewButton(submitButton) {
	if (submitButton.name == 'saveAndFinish') {
		return confirmSubmitReview('save');
	} else {
		return confirmSubmitReview('submit');
	}
}

function confirmSubmitReview(actionValue) {
	var frm = document.getElementById('scoreApplicationForm');
	if (!frm) return true;

	frm.action.value = actionValue;

	if (frm.action.value == 'submit') {
		var validateMsg = validateReview(frm);
		if (validateMsg.length > 0) {
			alert('Your review has the following errors:\n\n' + validateMsg.join('\n'));
			return false;
		}

		var message = 'This action will submit your review.\n\nYou will not be able to modify this\nreview after this point.\n\nProceed?'
		if (confirm(message))
			return true;
	} else {
		return true;
	}

	return false;
}

function validateReview(frm) {
	var validateMsg = new Array();

	var scores = [
		'scientific_merit_score',
		'significance_score',
		'investigators_score',
		'final_recommendation_score',
		'fair_market_value_score'
	];
	for (var idx in scores) {
		var scoreEle = frm[scores[idx]];
		if (!scoreEle) continue;

		var scoreEleOk = false;
		for (var eleIdx = 0; !scoreEleOk && eleIdx < scoreEle.length; eleIdx++)
			if (scoreEle[eleIdx].checked) scoreEleOk = true;

		if (!scoreEleOk) {
			switch(scores[idx]) {
				case 'scientific_merit_score':
					validateMsg.push('Scientific Merit not scored.');
					break;
				case 'significance_score':
					validateMsg.push('Significance not scored.');
					break;
				case 'investigators_score':
					validateMsg.push('Investigators not scored.');
					break;
				case 'final_recommendation_score':
					validateMsg.push('Final Recommendation not provided.');
					break;
				case 'fair_market_value_score':
					validateMsg.push('Fair Market Value not scored.');
					break;
			}
		}

		// if this is the final recommendation and the comments are blank
		// and this is either "additional info req" or "conditional approval"...
		if (scores[idx] == 'final_recommendation_score' &&
				frm['final_recommendation_comments'] &&
				frm['final_recommendation_comments'].value == '') {
			for (var eleIdx = 0; eleIdx < scoreEle.length; eleIdx++) {
				if (scoreEle[eleIdx].checked) {
					switch (scoreEle[eleIdx].value) {
						case 'additional_info_req':
							validateMsg.push('"Additional Information Required" suggested but no comments provided\n\tfor your Final Recommentation.');
							break;
						case 'conditional_approval':
							validateMsg.push('"Conditionally Approved" suggested but no comments provided\n\tfor your Final Recommentation.');
							break;
					}
				}
			}
		}
	}

	return validateMsg;
}

function confirmSubmitApplication() {
	return confirm('This action will submit your application\nfor review.\n\nYou will not be able to modify this\napplication after this point.\n\nProceed?');
}

function confirmDeleteApplication() {
	return confirm('This action will delete your application!!\n\nAll of your uploaded files will be deleted\nand your application record will be removed\nfrom the database.\n\nThis action is 100% non-reversible.\n\nProceed?');
}

function confirmRemoveFile(filename) {
	return confirm('This action will remove the following file:\n\n' + filename + '\n\nAre you sure?');
}

function validateFocusArea() {
	var frm = document.getElementById('change_focus_area');
	if (!frm) return true;

	if (frm.application_type_id.value == '') {
		alert('Please select a focus area!');
		return false;
	}

	return true;
}

function toggleCommentAuthorDiv(commentLink) {
	var parentDiv = commentLink.parentNode;
	if (parentDiv.tagName.toLowerCase() != 'div') {
		return false;
	}

	var anchorShow = document.getElementById(parentDiv.id + '_anchorShow');
	var anchorHide = document.getElementById(parentDiv.id + '_anchorHide');
	var divAuthor = document.getElementById(parentDiv.id + '_divAuthor');

	if (!(anchorShow && anchorHide && divAuthor)) return;

	if (isHidden(divAuthor)) {
		showElement(divAuthor); //, 'inline');
		showElement(anchorHide);
		hideElement(anchorShow);
	} else {
		hideElement(divAuthor);
		hideElement(anchorHide);
		showElement(anchorShow);
	}

	return false;
}

function isHidden(ele) {
	// if 'display' is blank and the offset width/height are not undefined
	if (ele.style.display == '' && ele.offsetWidth != undefined && ele.offsetHeight != undefined) {
		// if the offset width/height are not equal to zero...
		if (ele.offsetWidth == 0 || ele.offsetHeight == 0) {
			return true;
		}
	}

	// if the display is not equal to blank, then
	if (ele.style.display != 'none') {
		return false;
	} else {
		return true;
	}
}

function showElement(ele, displayType) {
	ele.style.visibility = 'visible';
	if (!displayType) {
		if (ele.tagName.toLowerCase() == 'div')
			displayType = 'block';
		else if (ele.tagName.toLowerCase() == 'a')
			displayType = 'inline';
	}

	ele.style.display = displayType;
}

function hideElement(ele) {
	ele.style.visiblity = 'hidden';
	ele.style.display = 'none';
}
