

function addDays(myDate,days) {
    return new Date(myDate.getTime() + days*24*60*60*1000);
}

function dateDiff(date1,date2)
{
	//Set 1 day in milliseconds
	var one_day=1000*60*60*24;
	//Calculate difference btw the two dates, and convert to days
	return Math.ceil((date2.getTime()-date1.getTime())/(one_day));
}

function IsValidTime(timeStr) 
{
	// Checks if time is in HH:MM:SS AM/PM format.
	// The seconds and AM/PM are optional.
	
	var timePat = /^(\d{1,2}):(\d{2})(:(\d{2}))?(\s?(AM|am|PM|pm))?$/;
	
	var matchArray = timeStr.match(timePat);
	if (matchArray == null) {
		alert("Time is not in a valid format.");
		return false;
	}
	hour = matchArray[1];
	minute = matchArray[2];
	second = matchArray[4];
	ampm = matchArray[6];
	
	if (second=="") { second = null; }
	if (ampm=="") { ampm = null; }
	
	if (hour < 0  || hour > 23) {
		alert("Hour must be between 1 and 12. (or 0 and 23 for military time)");
		return false;
	}
	if (hour <= 12 && ampm == null) {
		if (confirm("Please indicate which time format you are using.  OK = Standard Time, CANCEL = Military Time")) {
			alert("You must specify AM or PM.");
			return false;
	    }
	}
	if  (hour > 12 && ampm != null) {
		alert("You can't specify AM or PM for military time.");
		return false;
	}
	if (minute<0 || minute > 59) {
		alert ("Minute must be between 0 and 59.");
		return false;
	}
	if (second != null && (second < 0 || second > 59)) {
		alert ("Second must be between 0 and 59.");
		return false;
	}
	return false;
}

function getCookieVal (offset) {  
  var endstr = document.cookie.indexOf (";", offset);  
  if (endstr == -1)    
    endstr = document.cookie.length;  
  return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie (name) {
  var uid = "<%= session.getAttribute('Login.studentID') %>";
  name = uid + name;
  var arg = name + "=";  
  var alen = arg.length;  
  var clen = document.cookie.length;  
  var i = 0;

  while (i < clen) {    
    var j = i + alen;    
    if (document.cookie.substring(i, j) == arg)      
      return getCookieVal (j);    
    i = document.cookie.indexOf(" ", i) + 1; 
    if (i == 0) break;   
  }  
  return null;
}

function saveBox(b, val) {
  var uid = "<%= session.getAttribute('Login.studentID') %>";
  var cookieStr = "";
  var expDays = 3650;
  var exp = new Date(); 
  exp.setTime(exp.getTime() + (expDays*24*60*60*1000));

  if(navigator.appName != "Microsoft Internet Explorer")
  	cookieStr = uid + b + "=" + val + "; expires=" + exp.toGMTString();
  else
    cookieStr = uid + b + "=" + val + "; expires=" + exp.toGMTString();
  document.cookie = cookieStr;
}

function loadMenuBox(b) {
  if (GetCookie(b) == "show") {
    menuinit[b][1]="show";
	 alert(b + " show");
  }
  else {
    menuinit[b][1]="not show";
	 alert(b + " not show");
  }
}

function textCounter2(field, countfield, maxlimit) 
{
	if (field.value.length > maxlimit) // if too long...trim it!
		field.value = field.value.substring(0, maxlimit);
	else	// otherwise, update 'characters left' counter
		countfield.value = maxlimit - field.value.length;
}


var layerShow = false;
var layerStill = false;
var	layerFixedX = -1;			// x position (-1 if to appear below control)
var	layerFixedY = -1;			// y position (-1 if to appear below control)
var objLayer;


function miniLayer(ctl,layer)
{
	objLayer = layer;
	reallocate(ctl);
	//document.getElementById(objLayer).style.display='block';
	expandcontent('searchLayer');
	layerShow = true;
}

function displayLayer(ctl,layer)
{
	objLayer = layer;
	centerLayer(ctl,200,100);
	document.getElementById(objLayer).style.display='block';
	//expandcontent(layer);
	layerShow = true;
}

function centerLayer(ctl,w,h)
{
	var layerCrossobj = document.getElementById(objLayer).style;
	
	var LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
	var TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
	
	layerCrossobj.left = LeftPosition;
	layerCrossobj.top = TopPosition;
}

function reallocate(ctl)
{
	var layerCrossobj = document.getElementById(objLayer).style;
	var	leftpos = 0;
	var	toppos = 0;
	var aTag = ctl;

	do {
		aTag = aTag.offsetParent;
		leftpos	+= aTag.offsetLeft;
		toppos += aTag.offsetTop;
	} while(aTag.tagName!="BODY");
	
	layerCrossobj.left = layerFixedX==-1 ? ctl.offsetLeft	+ leftpos + "px": layerFixedX;
	layerCrossobj.top = layerFixedY==-1 ?	ctl.offsetTop +	toppos + ctl.offsetHeight +	2 + "px": layerFixedY;
}

function layerHide () {
	document.getElementById(objLayer).style.display='none';
	layerShow = false;
}

function trim(strText)
{ 
    // this will get rid of leading spaces 
    while (strText.substring(0,1) == ' ') 
        strText = strText.substring(1, strText.length);

    // this will get rid of trailing spaces 
    while (strText.substring(strText.length-1,strText.length) == ' ')
        strText = strText.substring(0, strText.length-1);

   return strText;
}

function popUp(strURL,strType,strHeight,strWidth) {
	var strOptions="";
	if (strType=="console") strOptions="resizable,scrollbars=yes,height="+strHeight+",width="+strWidth;
	if (strType=="fixed") strOptions="status,scrollbars=yes,height="+strHeight+",width="+strWidth;
	if (strType=="elastic") strOptions="toolbar,menubar,scrollbars=yes,resizable,location,height="+strHeight+",width="+strWidth;
	window.open(strURL, 'newWin', strOptions);
}


document.onclick = function miniHide () {
		try
		{
			if (objLayer!="equipLayer")
			{
				if (!layerShow && !layerStill)
					document.getElementById(objLayer).style.display='none';
				layerShow = false;	
			}	
		}
		catch(e){}
				
		
}

function getElementbyClass(classname){
	ccollect=new Array()
	var inc=0
	var alltags=document.all? document.all : document.getElementsByTagName("*")
	for (i=0; i<alltags.length; i++){
	if (alltags[i].className==classname)
		ccollect[inc++]=alltags[i]
	}
}


function get_cookie(Name) { 
	var search = Name + "="
	var returnvalue = "";
	if (document.cookie.length > 0) {
		offset = document.cookie.indexOf(search)
		if (offset != -1) { 
			offset += search.length
			end = document.cookie.indexOf(";", offset);
			if (end == -1) end = document.cookie.length;
			returnvalue=unescape(document.cookie.substring(offset, end))
		}
	}
	return returnvalue;
}

var interval = "";
var timeout = "";

function startInterval()
{
	document.getElementById('msgAlert').style.color='white';
	interval = window.setInterval(blinkFont,300);
} 

function stopInterval()
{
	window.clearInterval (interval);
	interval="";
}

function blinkFont() 
{
	stopInterval();
	startAgain();
}

function startAgain()
{
	document.getElementById('msgAlert').style.color='red';
	timeout = window.setTimeout(startInterval, 300);
}

