var sTargetURL = "/Home.do";

// define image variables and preload
	var ismShareefOn = new Image();
	ismShareefOn.src = "/jsp/images/IsmShareef.gif";
	var ismShareefOff = new Image();
	ismShareefOff.src = "/jsp/images/sitebanner-fade.gif";

	var jilaniOn = new Image();
	jilaniOn.src = "/jsp/images/jilani-selected.gif";
	var jilaniOff = new Image();
	jilaniOff.src = "/jsp/images/jilani.gif";



function doRedirect()
{
    //  The JavaScript 1.0 version of this function supports
    //  browsers that don't have the ability to replace this
    //  file in the browser history.  As a result we want to
    //  make sure that we briefly delay so when the visitor
    //  is moving back through their browser-history they
    //  will have time to press the back-button on this page
    //  before the script moves them to the redirection
    //  target page again.
    //
    //  Notice that we have the timeout the same amount as in
    //  the meta-refresh version.
    //
    setTimeout( "window.location.href = sTargetURL", 4*1000 );
    
    //  The 2*1000 equals 2 seconds.
}

function loadQuestion(id)
{
  location.href = "/RetrieveQuestion.do?questionId=" + id
}

function loadQuestionToApprove(id)
{
  location.href = "/RetrievePendingQuestion.do?questionId=" + id
}

function loadQuestionToResubmit(id)
{
  location.href = "/RetrieveRejectedQuestion.do?questionId=" + id
}

function rejectQuestion(id)
{
  location.href = "/RejectQuestion.do?questionId=" + id
}

function loadUser(id)
{
  location.href = "/FindUserById.do?userId=" + id
}

function loadEvent(id, role, roleEventAdmin)
{
  // if at least event admin, forward to update event page
  if(role >= roleEventAdmin)
    location.href = "/FindEventById.do?eventId=" + id;
  else // readonly for others
    launchPopup("/FindEventById.do?eventId=" + id);
}

function loadEventFromCalendarView(id, role, roleEventAdmin)
{
  // if at least event admin, forward to update event page
  if(role >= roleEventAdmin)
    location.href = "/FindEventById.do?eventId=" + id ;
  else // readonly for others
    launchPopup("/FindEventById.do?eventId=" + id + "&return=calendar");
}

function clearSearchFields()
{
  elems = document.getElementsByTagName("input");
  for(i=0; i< elems.length; i++)
  {
    if(elems[i].name == 'subject')
    {
    	alert(elems[i].value);
    	elems[i].value = '';
    	alert(elems[i].value);
    }
  }
}

function setHelpLabel(label, text)
{
  document.forms[1].helplabel.value = label;
  document.forms[1].helptext.value = text;
  helpWinWidth = 300;
  helpWinHeight = 300;
  helpWinTop = (screen.height/2)-(helpWinHeight/2);
  helpWinLeft = (screen.width/2)-(helpWinWidth/2);
  window.open("/jsp/help-popup.html","helpWin","toolbar=no,menubar=no,location=no,scrollbars=no,resizeable=no,status=no,width=" + helpWinWidth + ",height=" + helpWinHeight + ",top=" + helpWinTop + ",left=" + helpWinLeft);
}

function launchHelpPage(anchor)
{
  helpWinWidth = 400;
  helpWinHeight = 400;
  helpWinTop = (screen.height/2)-(helpWinHeight/2);
  helpWinLeft = (screen.width/2)-(helpWinWidth/2);
  window.open("/jsp/HelpManual.html" + "#" + anchor,"helpWin","toolbar=no,menubar=no,location=no,scrollbars=no,resizeable=no,status=no,width=" + helpWinWidth + ",height=" + helpWinHeight + ",top=" + helpWinTop + ",left=" + helpWinLeft);
}

function launchPopup(location)
{
  popupWinWidth = 820;
  popupWinHeight = 600;
  popupWinTop = (screen.height/2)-(popupWinHeight/2);
  popupWinLeft = (screen.width/2)-(popupWinWidth/2);
  window.open(location,"popup","toolbar=no,menubar=no,location=no,scrollbars=yes,resizeable=no,status=no,width=" + popupWinWidth + ",height=" + popupWinHeight + ",top=" + popupWinTop + ",left=" + popupWinLeft);
}

function launchAlerts(anchor)
{
  helpWinWidth = 400;
  helpWinHeight = 400;
  helpWinTop = (screen.height/2)-(helpWinHeight/2);
  helpWinLeft = (screen.width/2)-(helpWinWidth/2);
  window.open("/jsp/Alerts.html" + "#" + anchor,"helpWin","toolbar=no,menubar=no,location=no,scrollbars=no,resizeable=no,status=no,width=" + helpWinWidth + ",height=" + helpWinHeight + ",top=" + helpWinTop + ",left=" + helpWinLeft);
}

// define the rollon() and rolloff() functions
function rollon(id)
{
	var elem = document.getElementById(id);
	elem.src = eval(id + "On.src");
}
function rolloff(id)
{
	var elem = document.getElementById(id);	
	elem.src = eval(id + "Off.src");	
}

function validateTimeEntries()
{
  var valid1 = IsValidTime(document.forms[0].eventStartTime.value);
  if (valid1)
  {
    var valid2;
    if (document.forms[0].eventEndTime.value == '')
      valid2= true;
    else
      valid2 = IsValidTime(document.forms[0].eventEndTime.value);
	return valid2
  }
  else 
    return false;
}

// check if user-entered time is in valid format
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("Please enter a valid time using the format (<hour>:<minute> am/pm). Note that a space is required before am/pm.");
    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 true;
}

function showDiv(id)
{
  var elem = document.getElementById(id);
  elem.style.display = 'block';
}

function hideDiv(id)
{
  var elem = document.getElementById(id);
  elem.style.display = 'none';
}
