function notifyTimeout()
{ 
  var oWindowOpener = getWindowOpener();
  var oWindow = oWindowOpener;
  if (oWindow == null)
  {
    if (parent != self)
     oWindow = parent;
  }

  if ((oWindow != null))
  {
    oWindow.notifyTimeout();
  }
  else 
  { 
    window.document.getElementById("IsTimedOut").value = "Yes";
    window.location = "Login.asp";  
  }  
  // if opened in a popup, then close the popup    
  if (oWindowOpener != null)
    window.close();
}

function getWindowOpener()
{
  if (opener != null)
    return opener;
  
  if (window.dialogArguments != null)
    return window.dialogArguments;

  return null;
} 

function openWindow(sURL, sTitle, iHeight, iWidth)
{
  // if the Window is already assigned, close it, as we cannot bring it to the Front in code
  if (oModalSearchWindow!=null)
    if (!oModalSearchWindow.closed)
      oModalSearchWindow.close();
  oModalSearchWindow = openNewWindow(sURL, sTitle, iHeight, iWidth);
  return;  
}


function openNewWindow(sURL, sTitle, iHeight, iWidth)
{
  var sFeatures = 'titlebar=0,hotkeys=0,toolbar=0,scrollbars=1,location=0,status=0,resizable=1,width=' + iWidth + ',height=' + iHeight + ',dependent=1'
  if (is_nav)
    var oWindow = openWindowNetscape(sURL, sTitle, sFeatures, iHeight, iWidth)
  else
    var oWindow = openWindowIE5(sURL, sTitle, sFeatures, iHeight, iWidth);
  return oWindow;
}

function openWindowNetscape(sURL, sTitle, sFeatures, iHeight, iWidth)
{
  var oWindow = window.open(sURL, sTitle, sFeatures);
  centerWindowOnScreen(oWindow, iWidth, iHeight);
  oWindow.focus();
  return oWindow;
}

function openWindowIE5(sURL, sTitle, sFeatures, iHeight, iWidth)
{
  var x = Math.round((screen.width - iWidth) / 2);
  var y = Math.round((screen.height - iHeight) / 2);

  var sNewFeatures = sFeatures + ',center=yes,top=' + y + ',left=' + x;
  var oWindow = window.open(sURL, sTitle, sNewFeatures);
  oWindow.focus();
  
  return oWindow;
}
function centerWindowOnScreen(oWindow, iWidth, iHeight)
{
  // IE does not return window.outerWidth and window.outerHeight,
  //    the standard IE window are inaccurate due to toolbars etc.
   var x = Math.round((screen.width - iWidth) / 2);
   var y = Math.round((screen.height - iHeight) / 2);
   oWindow.moveTo(x,y);
}

function formatInputTime(sTime)
{
  var sValue = "";
  sTime = sTime.replace(/^\s*([^\s]*)\s*$/, "$1");
  if (sTime ==""){ sTime = "00:00";}
  
  // Replace any decimal places with a :, to allow for quick keypad entry
  if (sTime.indexOf(".", 0) != -1)
    sTime = sTime.replace(/[.]/, ':');

  // Replace any commas with a :, to allow for quick keypad entry
  if (sTime.indexOf(",", 0) != -1)
    sTime = sTime.replace(/[,]/, ':');
  
  // if there is no separator, then work with whole numbers
  if (sTime.indexOf(":", 0) == -1)
  {
    // 1     --> assumes 0 prefix for hour --> 01:00
    // 12    --> assumes 00 for min        --> 12:00
    // 123   --> assumes 0 prefix for hour --> 01:23
    // 1234  --> assumes : in middle       --> 12:34
   switch (sTime.length)
   {
     case 1 : sValue = sValue.concat("0", sTime, "00");
              break;
     case 2 : sValue = sValue.concat(sTime, "00");
              break;
     case 3 : sValue = sValue.concat("0", sTime);
              break;
     case 4 : sValue = sTime;
              break;
     default  : sValue = sTime;
   }
  }
  else
  {
    // :     --> 00:00
    // :1    --> 00:10
    // 1:    --> 01:00
    // :12   --> 00:12
    // 1:2   --> 01:20
    // 12:   --> 12:00
    // :123  --> INVALID
    // 1:23  --> 01:23
    // 12:3  --> 12:30
    // 123:  --> INVALID
   switch (sTime.length)
   {
     case 1 : sValue = sValue.concat("00", sTime, "00");
              break;
     case 2 : switch (sTime.indexOf(":", 0))
              {
                case 0 : sValue = sValue.concat("00", sTime, "0");
                         break;
                case 1 : sValue = sValue.concat("0", sTime, "00");
                         break;
              } 
              break;
     case 3 : switch (sTime.indexOf(":", 0))
              {
                case 0 : sValue = sValue.concat("00", sTime);
                         break;
                case 1 : sValue = sValue.concat("0", sTime, "0");
                         break;
                case 2 : sValue = sValue.concat(sTime, "00");
                         break;
              } 
              break;
     case 4 : switch (sTime.indexOf(":", 0))
              {
                case 0 : sValue = sTime;  // Invalid time, but its not our job to report it
                         break;
                case 1 : sValue = sValue.concat("0", sTime);
                         break;
                case 2 : sValue = sValue.concat(sTime, "0");
                         break;
                case 3 : sValue = sTime;
                         break;
              } 
              break;
     default  : sValue = sTime;
   }
  }

  if (sValue.length == 4)
    if (sValue.indexOf(":", 0) == -1)
      return sValue.slice(0,2) + ":" + sValue.slice(2,4);

  return sValue;  
}

function _isInteger(val) 
{
	var digits="1234567890";
	for (var i=0; i < val.length; i++) 
	{
		if (digits.indexOf(val.charAt(i))==-1) { return false; }	
	}
	return true;
}
function isDouble(val, isAllowNegative) 
{
  var digits="1234567890.";
  if (isAllowNegative)
  {
	  digits = digits + "-";
	}
	for (var i=0; i < val.length; i++) 
	{
		if (digits.indexOf(val.charAt(i))==-1) { return false; }
	}
	return true;
}
function formatTotalTime(val)
{
  if (val < 0){val = "0.00";}
  else
  {
    if (val > 24)
    {
      alert("The value entered is more than 24 hours.");
    }
    val = val.substring(0,10);
    values = val.split(".")
    var hrs = values[0];
    var fMins = values[1];
    if (fMins == null){fMins = "00";}
    if (fMins <= 0){fMins = "00";}
    fMins = fMins.substring(0,1);
    if (fMins <=0){fMins = "00";}
    else if ((fMins > 0)&&(fMins <= 2)){fMins = 25;}
    else if ((fMins > 2)&&(fMins <= 5)){fMins = 50;}
    else if ((fMins > 5)&&(fMins <= 7)){fMins = 75;}
    else if ((fMins > 7)&&(fMins <= 9)) 
    { 
      hrs =(hrs*1 + 1);
      fMins = "00";
    }
    val = hrs + "." + fMins;
  }
  return val;
}
function roundUpMin(val)
{
  values = val.split(":");
  var hrs = values[0];
  var mins = values[1];
  if (mins <= 0){ mins = "00" }
  else if ((mins > 0)&&(mins <= 15)){mins = 15;}
  else if ((mins > 15)&&(mins <= 30)){mins = 30;}
  else if ((mins > 30)&&(mins <= 45)){mins = 45;}
  else if ((mins > 45)&&(mins <= 59))
  { 
    hrs =(hrs*1 + 1);
    mins = "00";
  }
  val = hrs + ":" + mins;
  val = formatInputTime(val);
  return val;
}
function _getInt(str,i,minlength,maxlength) 
{
	for (var x=maxlength; x>=minlength; x--) 
	{
		var token=str.substring(i,i+x);
		if (token.length < minlength) { return null; }
		if (_isInteger(token)) { return token; }
	}
	return null;
}
function isDate(val,format) 
{ 
	var date=getDateFromFormat(val,format);
	if (date==0) 
	{ 
	  return false; 
	}
	else return true;
}
function validateInputTime(sValue)
{ 
  if (sValue=="")
  {
    return true;
  }
  var check = isDate(sValue, "HH:mm");
  if (!check)
  {
    alert(sValue + " is an invalid time");
    return false;
  }
  else return true;
}

function calcDuration(sStartTime, sEndTime)
{
  if (sStartTime == ""){sStartTime = "00:00";}
  if (sEndTime == ""){sEndTime = "00:00";}

  var iMins = minsDiff(sStartTime, sEndTime);

  var fHrs  = Math.floor((iMins / 60));

  var fMins = Math.floor((iMins%60));
  if(fMins <= 0) {fMins = "00";}
  else if ((fMins > 0)&&(fMins <= 15)) { fMins = 25; }
  else if ((fMins > 15)&&(fMins <= 30)) { fMins = 50; }
  else if ((fMins > 30)&&(fMins <= 45)) { fMins =75; }
  else if ((fMins >= 45)&&(fMins <= 59)) 
  { 
    fMins = "00";
    fHrs = (fHrs*1 + 1);
  }
  var fTime = fHrs + "." + fMins
  return fTime;
}

function minsDiff(dStart, dEnd)
{
  sValues = dStart.split(":");
  eValues = dEnd.split(":");
  if (eValues[0] < sValues[0] && eValues[0] != "00")
  {
    eValues[0] = (eValues[0]*1) + 24;
  }
  var startMinutes = ((sValues[0] * 60) + (sValues[1]*1)); 
  var endMinutes = ((eValues[0] * 60) + (eValues[1]*1)); 
  
  var mins = endMinutes - startMinutes;
  if (mins < 0) 
  {
    mins = 0;
  }
  return mins;
}


function calcDurationNew(sStartTime, sEndTime, interval)
{
  if (sStartTime == ""){sStartTime = "00:00";}
  if (sEndTime == ""){sEndTime = "00:00";}
  var duration = minsDiff(sStartTime, sEndTime);
  duration = eval(duration / 60);
  var timeIntrvl = eval(60.0 / interval);
  var calcDuration = eval(duration * timeIntrvl);
  var intDuration = parseInt(calcDuration);
  
  if ( Math.abs(calcDuration - intDuration) > 0.001 )
  {
    if (calcDuration >= 0)
    {
      intDuration = intDuration + 1;
    }
    else
    {
      intDuration = intDuration - 1;
    }
  }
  calcDuration = intDuration / timeIntrvl;
  return round(calcDuration);
}


function formatTotalTimeNew(duration, interval)
{
  var timeIntrvl = eval(60.0 / interval);
  var calcDuration = eval(duration * timeIntrvl);
  var intDuration = parseInt(calcDuration);
  if ( Math.abs(calcDuration - intDuration) > 0.001 )
  {
    if (calcDuration >= 0)
    {
      intDuration = intDuration + 1;
    }
    else
    {
      intDuration = intDuration - 1;
    }
  }
  calcDuration = intDuration / timeIntrvl;
  return round(calcDuration);
}

function round (Val) 
{
  var Dollar;
  if (Val >= 0)
  {
    Dollar = Math.floor(Val) ;
  }
  else
  {
    Dollar = Math.ceil(Val) ;
    if (Val > -1)
    {
      Dollar = "-" + Dollar;
    }
  }
  if (Val < .1)
  {
    Val = "0" + Math.round(Val * 100);
  }
  else
  {
    Val = "" + Math.round(Val * 100);
  }
 var Decimal = Val.substring (Val.length-2, Val.length);
 return (Dollar + "." + Decimal);
}
function setReadOnly(name, isSet)
{
  var oElement = document.getElementById(name);
  if (oElement != null)
  {
    if (isSet)
    { 
        if (oElement.type == "image")
        {
            oElement.setAttribute("disabled", true);
            oElement.src = "images/findDisabled.gif";
        }
        else if (oElement.type == "text")
        {
            oElement.setAttribute("readOnly", true);
            oElement.style.backgroundColor = "#DDDDDD";
            oElement.style.color = "#0099FF";
        }
        else if (oElement.type == "select-one")
        {
            oElement.setAttribute("readOnly", true);
            oElement.setAttribute("disabled", true);
        }
    }
    else
    {
        if (oElement.type == "image")
        {
            oElement.setAttribute("disabled", false);
            oElement.src = "images/find.gif";
        }
        else if (oElement.type == "text")
        {
            oElement.setAttribute("readOnly", false);
            oElement.style.backgroundColor = "white";
            oElement.style.color = "#000000";
        }
        else if (oElement.type == "select-one")
        {
            oElement.setAttribute("readOnly", false);
            oElement.setAttribute("disabled", false);
        }
    }
  }
}

