function setMainHeight() {
	var main = document.getElementById('main');
	var content_top = document.getElementById('content_top');
	var content_mid = document.getElementById('content_mid');
	var content_bot = document.getElementById('content_bot');
	content_mid.style.height = (main.offsetHeight - content_top.offsetHeight - content_bot.offsetHeight) + 'px';
	//var sidebar_height = document.getElementById('sidebar').offsetHeight;
	var content_height = document.getElementById('content_mid').offsetHeight;
	content_height += 10; // add in top and bottom borders
	
}

function myShow(section, myTime) {
	var section_to_change = $('#' + section);
	section_to_change.slideToggle(myTime);
}

function myShowIcons(section, callingSection, myTime){
	var section_to_change = $('#' + section);
	section_to_change.toggle(0);
	var old_section = $('#' + callingSection);
	old_section.toggle(0);
	
}

function myShowSelect() {
	if ($('#highSchoolID')[0].selectedIndex == 1) {
		$('#high_school_details').toggle(400);
		$('#highSchoolID').attr('disabled', true);
	}
}

function myShowDependent(section,dependent,type,dependent_value) {
	var section_to_change = $('#' + section);
	var dependent_on = $('#' + dependent);

	if (type == 'text') {	
		if (dependent_on.val() == dependent_value) {
			section_to_change.toggle(200);
		} else {
			section_to_change.hide(200);
		}
	} else if (type == 'radio') {
		if (dependent_on.attr('checked') == true) {
			section_to_change.toggle(200);
		} else {
			section_to_change.hide(200);
		}
	} else{
		
	}
	
}

function myDisable(field) {
	var field_to_change = $('#' + field);
	if (field_to_change.attr('disabled') == true) {
		field_to_change.attr('disabled', false);
	} else {
		field_to_change.attr('disabled', true);
	}
}

function updateSeasonLabels(season) {
	var seasonLabel = $('#season' + season).val();
	$('.season' + season).val(seasonLabel);
}

function moveO(name,w){
	var sel=document.getElementsByName(name)[0];
	var opt=sel.options[sel.selectedIndex];
	if(w=='up') {
		var prev = opt.previousSibling;
		while (prev&&prev.nodeType != 1) {
			prev=prev.previousSibling;
		}
		prev?sel.insertBefore(opt,prev):sel.appendChild(opt);
	} else {
		var next = opt.nextSibling;
		while (next&&next.nodeType != 1) {
			next=next.nextSibling;
		}
		if (!next) {
			sel.insertBefore(opt,sel.options[0]);
		} else {
			var nextnext = next.nextSibling;
			while (next&&next.nodeType != 1) {
				next=next.nextSibling;
			}
			nextnext?sel.insertBefore(opt,nextnext):sel.appendChild(opt);
		}
	}
}

function copyDiv(source, max) {
	var content = $('#' + source + '_content').html();
	var count = $('#' + source + '_source').children().size();
	if (count < max) {
	var new_id = source + '_' + eval(count + 1);
	$('#' + source + '_source').append('<div class="module_mid_form_bord" style="display:none;" id="' + new_id + '">' + content + '</div>');
	$('#' + new_id).find('input:text').each(function(i) {
	$(this).val('');
	});
	$('#' + new_id).find('select').each(function() {
	$(this)[0].selectedIndex = -1;
	});
	$('#' + new_id).show(200);
	} else {
	alert('You have reached the maximum allowed for this entry.');
	}
	}

function nextPage(page_name) {
	var next_page;
	switch (page_name) {
		case 'general':
			next_page = 'academic';
			break;
		case 'academic':
			next_page = 'athletic';
			break;
		case 'athletic':
			next_page = 'clubs';
			break;
		case 'clubs':
			next_page = 'stats';
			break;
		case 'stats':
			next_page = 'personal';
			break;
		case 'personal':
			next_page = 'preferences';
			break;
		case 'preferences':
			next_page = 'files';
			break;
	}
	location.href='sr_' + next_page + '.html';
}

function lastPage(page_name) {
	var next_page;
	switch (page_name) {
		case 'academic':
			next_page = 'general';
			break;
		case 'athletic':
			next_page = 'academic';
			break;
		case 'clubs':
			next_page = 'athletic';
			break;
		case 'stats':
			next_page = 'clubs';
			break;
		case 'personal':
			next_page = 'stats';
			break;
		case 'preferences':
			next_page = 'personal';
			break;
		case 'files':
			next_page = 'preferences';
			break;
	}
	location.href='sr_' + next_page + '.html';
}

function saveAndGoto(subaction){
	document.forms[0].goto.value=subaction;
	document.forms[0].submit();
}

function trim(str) {
	return ltrim(rtrim(str));
}
 
function ltrim(str) {
	var chars = "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str) {
	var chars = "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

var allowOther = false;
/**
 * Function for the onchange method of the state select list to go get the 
 * highschools for the highschool select list. State select list must have
 * an id (or htmlId) of "highSchoolStateSelect". The high school select list 
 * must have an id (or htmlId) of "highSchoolSelect".
 * @param inAllowOther (optional) Whether to put the "Other" option in High School drop down.
 * @return void
 */
function getHighSchools(inAllowOther) {
	if(typeof inAllowOther != "undefined") {
		allowOther = inAllowOther;
	}
	if(typeof $("#addHighSchool") != "undefined") {
		if($("#highSchoolStateSelect").val() == "") {
			$("#addHighSchool").hide("fast");
		} else {
			$("#addHighSchool").show("fast");
		}
	}
    $("#highSchoolSelect").fadeTo("fast", 0.33, _hsCallbackStart);
}

var _hsCallbackStart = function() {
	var stateIdFromSelect = $("#highSchoolStateSelect").val();
    var url = "/fasttrack/highSchool/Ajax.do";
	$.getJSON(url, { method: "ajaxGetHighSchoolsForState", stateId: stateIdFromSelect }, _hsCallbackFinish);	
}

/**
 * Callback function passed to the getJSON jquery function. The callback gets run 
 * when the ajax call finishes.
 */
var _hsCallbackFinish = function(data) {
	if("OK" == data.code) {
		// empty target select
		$("#highSchoolSelect").empty();
		// if the state selected is the special include all high schools from all states, then don't add the "Select High School" option.
		// -2 is defined in com.adivi.ncsa.fasttrack.common.MiscellaneousDAO.INCLUDE_ALL_STATES_ID
		if($("#highSchoolStateSelect").val() != -2) {
			$("#highSchoolSelect").append("<option value=\"\" >Select High School</option>");
		}
		if(allowOther) {
			$("#highSchoolSelect").append("<option value=\"-100\" >Other</option>");
		}
		$.each(data.highSchools, function(i,item){
			$("#highSchoolSelect").append("<option value=\"" + item.generic.ID + "\">" + item.generic.name + "</option>");
		});
	}
	// else, probably should show "no highschools found" msg
	else {
		alert("Unable to retrieve high schools. Re-select the chosen state to try again. If the error persists, please contact technical support.");
	}
    $("#highSchoolSelect").fadeTo("slow", 1.00);
};

/**
 * Function for the onchange method of a college select list to go get the 
 * coaches for the coach select list. College select list must have
 * an id (or htmlId) of "coachCollegeSelect". The coach select list 
 * must have an id (or htmlId) of "coachSelect".
 * @param inAllowOther (optional) Whether to put the "Other" option in College drop down.
 * @return void
 */
function getCoaches() {	
	$("#coachSelect").fadeTo("fast", 0.33, _coachCallbackStart);
}

var _coachCallbackStart = function() {
var collegeIdFromSelect = $("#coachCollegeSelect").val();	
var url = "/fasttrack/coach/Ajax.do";
$.getJSON(url, { method: "ajaxGetCoachforCollege", collegeId: collegeIdFromSelect }, _coachCallbackFinish);	
}

/**
* Callback function passed to the getJSON jquery function. The callback gets run 
* when the ajax call finishes.
*/
var _coachCallbackFinish = function(data) {	
if("OK" == data.code) {
	// empty target select
	$("#coachSelect").empty();
	if($("#coachSelect").val() != -2) {
		$("#coachSelect").append("<option value=\"\" >Select Coach</option>");
	}
	$.each(data.coaches, function(i,item){			
		$("#coachSelect").append("<option value=\"" + item.generic.ID + "\">" + item.generic.name + "</option>");
	});
}
else {
	alert("Unable to retrieve coaches. Re-select the chosen college to try again. If the error persists, please contact technical support.");
}
$("#coachSelect").fadeTo("slow", 1.00);
};

/**
 * An attempt to enforce maxlength on a textarea. Problem is, struts won't all the "maxlength" attribute on &lt;html:textarea&gt;.
 */
//$(document).ready(function() {
//	$("textarea[@maxlength]").keypress(function(event) {
//		var key = event.which;
//		
//		//all keys including return.
//		if(key >= 33 || key == 13) {
//			var maxLength = $(this).attr("maxlength");
//			var length = this.value.length;
//			if(length >= maxLength) {
//				event.preventDefault();
//			}  
//		}  
//	});  
//});

