// JavaScript Document
// compiled by Daniel Armendariz
// adapted for use on the MIT-EMS website

/* 
get cross-browser XMLHttpRequest
*/
function ajax_getXMLHttpRequest() {
	var xmlHttp;
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	} catch (e) {
		// Internet Explorer
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}

	return xmlHttp;
}

/*
make and AJAX request to requestURL and then call the functoin "func" with the responseText.
*/
function ajax_do(requestURL, func) {
	var xmlhttp = ajax_getXMLHttpRequest();
	xmlhttp.open('GET', requestURL, true);
	xmlhttp.onreadystatechange = function() {
	    if (xmlhttp.readyState == 4) {
	    	func(xmlhttp.responseText);
	    }
	}
	xmlhttp.send(null);
}
function ajax_replace(requestURL, elementId) {
	ajax_do(requestURL, function(responseText) {
		var element = document.getElementById(elementId);
	    element.innerHTML = responseText;
	});
}
function ajax_append(requestURL, elementId) {
	ajax_do(requestURL, function(responseText) {
		document.getElementById(elementId).innerHTML += responseText;
	});
}

/*
redirects the current window to the specified URL
*/
function gotoURL(url) {
	window.location = url;
}

/*	
changes the font of a select menu when the selection is changed pased on the values in classMap.
*/
function colorSelect(select, classMap) {
	var text = select.options[select.selectedIndex].text;
	var className = classMap[text];
	if(className == null) {
		select.className = 'normal';
	} else {
		select.className = className;
	}
}

function sAll(myType, idArray, show) {
	for (var i=0; i < idArray.length; i++) {
		var id = idArray[i];
		var EID = myType + "_" + id;
		if(show) {
			showId(EID)
		} else {
			hideId(EID)
		}
	}
}
function sAll_ajax(myType, idArray, show, remoteRequestURL) {
	for (var i=0; i < idArray.length; i++) {
		var id = idArray[i];
		var EID = myType + "_" + id;
		if(show) {
			showId_ajax(EID, remoteRequestURL+'arg1='+id)
		} else {
			hideId(EID)
		}
	}
}

function sw(EID) {
	if(document.getElementById(EID).style.display == 'block') {
		hideId(EID);
	} else {
		showId(EID);
	}
}
function sw_ajax(EID, remoteRequestURL) {
	if(document.getElementById(EID).style.display == 'block') {
		hideId(EID);
	} else {
		showId_ajax(EID, remoteRequestURL);
	}
}

function hideId(EID) {
	document.getElementById(EID).style.display = 'none';
	document.getElementById(EID+'Is').style.display = 'inline';
	document.getElementById(EID+'Ih').style.display = 'none';
}
function showId(EID) {
	document.getElementById(EID).style.display = 'block';
	document.getElementById(EID+'Is').style.display = 'none';
	document.getElementById(EID+'Ih').style.display = 'inline';
}

function showId_ajax(EID, remoteRequestURL) {
	showId(EID);
	if(!document.getElementById(EID).innerHTML) {
		ajax_replace(remoteRequestURL, EID);
	}
}

////////////////////////////////
////// Check Form script ///////
////////////////////////////////

/* Comment and citation for 'Check Form script' code
   except for validateDates */
/* $Id: java.js,v 1.1.1.1 2004/08/09 04:20:48 nei Exp $ */
/*
Copyright (c) 2001, 2002 by Martin Tsachev. All rights reserved.
mailto:shaggy@members.evolt.org
http://members.evolt.org/shaggy/

Redistribution and use in source and binary forms,
with or without modification, are permitted provided
that the conditions available at
http://www.opensource.org/licenses/bsd-license.html
are met.
*/

function validateMatch(var1, var2, msg) {
	return validateFalse(var1, msg, var1.value != var2.value);
}

function validateTime(field1, msg) {
	if(!validateFalse(field1, "Time must be in the format HH:MM:SS, in 24-hour format. HH from 0 to 23, MM and SS from 0 to 59", !field1.value)) {
		return false;
	}

	cHour = field1.value.substr(0,2);
	cMinute = field1.value.substr(3,2);
	
	return validateFalse(field1, "Time must be in the format HH:MM, in 24-hour format. HH from 0 to 23, MM from 0 to 59.", !(!isNaN(cHour) && !isNaN(cMinute) && cHour >= 0 && cHour <= 23 && cMinute >= 0 && cMinute <= 59));
}

function validateMilTime(field1,required, msg){
	if(!required) {
		if(!field1.value) {
			return true;
		}
	}

	if (field1.value.length != 4){
		alert(msg);
		return false;
	}
	
	cHour = field1.value.substr(0,2);
	cMin = field1.value.substr(2,2);

	if (!isNaN(cHour) && !isNaN(cMin) && cHour >= 0 && cHour < 24 && cMin >= 0 && cMin < 60) {
		return true
	} else {
		alert(msg);
		return false;
	}

}


function validateDay(myDay, myMonth, myYear, msg) {
	return_me = true;
	switch(myMonth) {
		case 1: if (myDay > 31) { return_me = false; }
		break
		case 2: if ((myDay > 29 && myYear%4 == 0) || (myDay > 28 && myYear%4 != 0)) { return_me = false; }
		break
		case 3: if (myDay > 31) { return_me = false; }
		break
		case 4: if (myDay > 30) { return_me = false; }
		break
		case 5: if (myDay > 31) { return_me = false; }
		break
		case 6: if (myDay > 30) { return_me = false; }
		break
		case 7: if (myDay > 31) { return_me = false; }
		break
		case 8: if (myDay > 31) { return_me = false; }
		break
		case 9: if (myDay > 31) { return_me = false; }
		break
		case 10: if (myDay > 31) { return_me = false; }
		break
		case 11: if (myDay > 30) { return_me = false; }
		break
		case 12: if (myDay > 31) { return_me = false; }
		break
		default: return_me = false;
	}
	if(!return_me) {
		alert(msg);
		return false;
	}
	return true;
}

function validateDate(field1, required, msg2, msg3) {
	if(!required) {
		if(!field1.value || field1.equals('Expired') || field1.equals('Incomplete')) {
			return true;
		}
	}

	endYear = field1.value.substr(0,4);
	endMonth = field1.value.substr(5,2) - 1;
	endDay = field1.value.substr(8,2);

	if (!isNaN(endYear) && !isNaN(endMonth) && !isNaN(endDay) && endMonth >= 0 && endMonth <= 11 && endDay >= 0 && endDay <= 31) {
		return validateDay(endDay, endMonth+1, endYear, msg3);
	} else {
		alert(msg2);
		return false;
	}
	return false;
}

function validateDates(field1,field2) {
	if (!field2.value) {
		alert("The End date MUST be filled out!");
		return false;
	}

	endYear = field2.value.substr(0,4);
	endMonth = field2.value.substr(5,2) - 1;
	endDay = field2.value.substr(8,2);

    if (!isNaN(endYear) && !isNaN(endMonth) && !isNaN(endDay) && endMonth >= 0 && endMonth <= 11 && endDay >= 0 && endDay <= 31) {
      endDate = new Date(endYear, endMonth, endDay);
    } else {
      	alert("The End date must be in the format YYYY-MM-DD.");
    	return false;
  	}

	if(!validateDay(endDay, endMonth+1, endYear, "Please enter a valid day for the month you entered in the End date.")) {
		return false;
	}

	if(!field1.value) {
		alert("The Begin date MUST be filled out!");
		return false;
	}

	startYear = field1.value.substr(0,4);
	startMonth = field1.value.substr(5,2) - 1;
	startDay = field1.value.substr(8,2);

	if (!isNaN(startYear) && !isNaN(startMonth) && !isNaN(startDay) && startMonth >= 0 && startMonth <= 11 && startDay >= 0 && startDay <= 31) {
  		startDate = new Date(startYear, startMonth, startDay);
   	} else {
   		alert("The Begin date must be in the format YYYY-MM-DD.");
   		return false;
  	}

	if(!validateDay(startDay, startMonth+1, startYear, "Please enter a valid day for the month you entered in the Begin date.")) {
		return false;
	}

  	if (startDate > endDate) {
		alert("The Begin date must be earlier than the End date");
		return false;
	}

	return true;

}

function validateNumber(field, msg, minLength, maxLength) { 
	if (minLength < 0) { minLength = 0 } 
	if (!maxLength) { maxLength = 255 } 

	return validateFalse(field, msg, ((minLength > 0) && (parseInt(field.value) != field.value)) || field.value.length < minLength || field.value.length > maxLength);
}

function validateNumberRange(field, msg, min) { return validateNumberRange(field, msg, min, null); }
function validateNumberRange(field, msg, min, max) {
	var value = parseInt(field.value);
	
	return validateFalse(field, msg, (value != field.value) || value < min || (max != null && value > max));
}

function validateEmail(email, msg, required) { 
	if (!email.value && !required) {
		return true; 
	} 

 	var re_mail = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z])+$/; 
	return validateFalse(email, msg, !re_mail.test(email.value));
}

function validateString(field, msg, min, max) { 
	if (min < 0) { min = 1 } 
	if (!max) { max = 65535 } 
	
	return validateFalse(field, msg, (min > 0 && !field.value) || field.value.length < min || field.value.max > max);
}

function validatePhone(field, msg, required) {
	if(!field.value && !required) {
		return true;
	}

	area = field.value.substr(0, 3);
	prefix = field.value.substr(4, 3);
	suffix = field.value.substr(8, 4);

	return validateFalse(field, msg, isNaN(area) || isNaN(prefix) || isNaN(suffix) || field.value.length != 12);
}

function validateTextArea(field, msg, min, max) { 
	if (!min) { min = 1 } 
	if (!max) { max = 65535 } 
	
	return validateFalse(field, msg, !field.value || field.value.length < min || field.value.max > max || field.value.indexOf('{link}') < 0);
}

function validateSelected(field, msg, min, max) { 
    if (!min) { min = 1 } 
    if (!max) { max = 65535 } 
    
    return validateFalse(field, msg, !field.value || field.value.length < min || field.value.max > max || field.value.indexOf('{link}') < 0);
}

function validateRadioButtons(buttonGroup, msg) {
    var isChecked = false;
    for (var i=0; i < buttonGroup.length; i++) {
        if (buttonGroup[i].checked) {
            isChecked = true;
        }
    }
    
    return validateFalse(buttonGroup[0], msg, !isChecked);
}

function validateFalse(field, msg, test) {
	if(test) {
		alert(msg);
		field.focus(); 
		field.select(); 
		return false; 
	}

	return true;
}

////////////////////////////////
///// Pop-up window script /////
////////////////////////////////

var NewWin=0;
function NewWindow(URLStr, width, height)
{
  var left = (screen.width - width) / 2;
  var top = (screen.height - height) / 2;
  if(NewWin)
  {
    if(!NewWin.closed) NewWin.close();
  }
  NewWin = open(URLStr, 'popUpWindowMITEMS', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbar=no,resizable=no,copyhistory=yes,width='+width+',height='+height+',left='+left+', top='+top+',screenX='+left+',screenY='+top+'');
}

////////////////////////////////
// Global and subglobal menus //
////////////////////////////////

// Code from: Dreamweaver MX

var time = 3000;
var numofitems = 6;

//menu constructor
function menu(allitems,thisitem,startstate){ 
  callname= "gl"+thisitem;
  divname="subglobal"+thisitem;  
  this.numberofmenuitems = numofitems;
  this.caller = document.getElementById(callname);
  this.thediv = document.getElementById(divname);
  this.thediv.style.visibility = startstate;
}

//menu methods
function ehandler(event,theobj){
  for (var i=1; i<= theobj.numberofmenuitems; i++){
    var shutdiv =eval( "menuitem"+i+".thediv");
    shutdiv.style.visibility="hidden";
  }
  theobj.thediv.style.visibility="visible";
}
				
function closesubnav(event){
  for (var i=1; i<= numofitems; i++){
     var shutdiv =eval('menuitem'+i+'.thediv');
     shutdiv.style.visibility='hidden';
  }
}
