// Submit the serach form if the given value is not empty, and reset the other select boxes
function resetOthersSubmitForm(form,value,inst,field,cert) {

	if (value == "") return false;
	
	if (!inst) form.institute.options[0].selected = true;
	if (!field) form.fieldofstudy.options[0].selected = true;
	if (!cert) form.certificatetypes.options[0].selected = true;
	
	form.submit();
}

// Submit the given form if the given value is not empty
function submitForm(form,value) {
					
	if (value != "") {
		form.submit();
	}
}

// this function ensures a form value is not empty or "none" and then posts the form.
function ensure_value_post(form, value, name) {

	if (value == "" || value == "none") {
		alert("Please select a " + name + " before proceeding.");
	}
	else {
		form.submit();
	}
}

// This function returns true if the given parameter is a positive float, false otherwise
function isPositiveFloat(value) {
	
	var periodCount = 0;
    inputStr = value.toString(); // in case not a string already
	
	if (inputStr.length == 0) return false;
	
	var lastChar = inputStr.charAt(inputStr.length-1);
	
	if (lastChar == ".") return false;
	
    for (var i = 0; i < inputStr.length; i++) {
        var oneChar = inputStr.charAt(i);
		if (oneChar == ".") {
			periodCount++;
		}
        if ((oneChar < "0" || oneChar > "9") && oneChar != ".") {
			return false;
        }
		else if (periodCount > 1) {
			return false;
		}
    } 
	// if you get here it is a positive float.
	return true;
}

// This function sets the value of a form field, checks the given value as a positive float,
// and then submits the form.
// ( used for adding new program costs )
function changeValueCheckPost(form, control, value, theFloat) {

	if (isPositiveFloat(theFloat)) {
		control.value = value;
		form.submit();
	}
	else {
		alert("The tuition dollar amount must be a positive number");
	}
}

// This function sets the value of a form field and then submits the form.
// ( used for adding new programs )
function changeValuePost(form, control, value) {

	control.value = value;
	form.submit();
}

// This function opens a new window to display a preview of a program profile.
function showPreview(url) {
	
	window.open(url,"myPopUpWin","toolbar=no,location=no,directories=no,status=yes,scrollbars=yes,resizable=yes,width=800,height=600");
	
}

// This function resets the 3 select boxes on the search form in case someone uses the
// browser back buttons.
function resetSearchForm() {

	document.searchForm.institute.selectedIndex=0;
	document.searchForm.fieldofstudy.selectedIndex=0;
	document.searchForm.certificatetypes.selectedIndex=0;
}

// for featured search results
function set_program_redirect(selectbox,id_value) {

	// Program redirect (if selectedindex is 0, do nothing), if it's one go to the profile
	// page, if it's greater go to the mini profile page
	if (selectbox.selectedIndex == 1) {
		window.location = "./profile.cfm?artid=" + id_value;
	}
	else if (selectbox.selectedIndex > 1) {
		window.location = "./program_view.cfm?id=" + id_value;
	}
}	

// popup new window with overview showing
function overview_popup (instid)
{
		var newwin, page, myHeight, myWidth, info;
		
		if (navigator.appName == 'Netscape') {
			page = "./overview.cfm?instid="+instid;
		}
		else {
			page = "./overview.cfm?instid="+instid;
		}
		
		myHeight = 400;
		myWidth = 500;
		info = "toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no,width="+myWidth+",height="+myHeight;

		newwin = window.open(page,"ControlWindow",info);
		newwin.open (page,"ControlWindow",info);
		if (newwin.opener == null) newwin.opener = self;
		newwin.focus();
}


// popup new window for Virtual Tour
function vtWindow (url,toolToggle,scrollToggle)
{
	window.open(url,"myPopUpWin","toolbar="+toolToggle+",location=no," + 
		"directories=no,status=yes,scrollbars="+scrollToggle+",resizable=yes," + 
		"width=600,height=600");
}

// close the popup window and redirect the main window to the new
// location.  If there is no main window, open a new one.
function close_window_redirect_main(url) {

	var newwin, myHeight, myWidth, info;
	
	myHeight = 500;
	myWidth = 825;
	info = "toolbar=yes,location=yes,directories=yes,status=yes,menubar=yes,scrollbars=yes,resizable=yes,copyhistory=no,width="+myWidth+",height="+myHeight;

	if (top.opener == null || top.opener == top || !top.opener || top.opener.closed) {
	
		// open new window
		newwin = window.open(url,"NewWin",info);
		newwin.open (url,"NewWin",info);
		if (newwin.opener == null) newwin.opener = self;
		newwin.focus();
		top.window.close();
	}
	else {
		// use current parent window	
		top.opener.location = url;
		top.opener.focus();
		top.close();
	}

}

// used on the search results page to select/deselect all checkboxes
function selectAll(theForm) {

	var isChecked = true;

	// go through each form element
	for(var i=0; i<theForm.elements.length; i++) {
	
		// if this form element is a checkbox and
		// has the name 'requests', see if the checkbox
		// is not checked, then set the variable and
		// break out of this loop
		if (theForm.elements[i].type == "checkbox"
			&& theForm.elements[i].name == "requests") {
			
			if (!theForm.elements[i].checked) {
			
				isChecked = false;
				break;
			}	
		}
	}
	
	// loop through each form element and check/uncheck all the checkboxes
	// named 'requests' (if any checkbox was unchecked then check them all,
	// otherwise uncheck them all)
	for(var i=0; i<theForm.elements.length; i++) {
	
		// if this form element is a checkbox and
		// has the name 'requests', see if the checkbox
		// is not checked, then set the variable and
		// break out of this loop
		if (theForm.elements[i].type == "checkbox"
			&& theForm.elements[i].name == "requests") {
			
			if (isChecked) {
			
				theForm.elements[i].checked = false;
			}	
			else {
				
				theForm.elements[i].checked = true;
			}
		}
	}
}