﻿var TimeToFade = 1000.0;

function fade(eid)
{
  var element = document.getElementById(eid);
  if(element == null)
    return;
   
  if(element.FadeState == null)
  {
    if(element.style.opacity == null
        || element.style.opacity == ''
        || element.style.opacity == '1')
    {
      element.FadeState = 2;
    }
    else
    {
      element.FadeState = -2;
      
    }
  }
   
  if(element.FadeState == 1 || element.FadeState == -1)
  {
    element.FadeState = element.FadeState == 1 ? -1 : 1;
    element.FadeTimeLeft = TimeToFade - element.FadeTimeLeft;
  }
  else
  {
    element.FadeState = element.FadeState == 2 ? -1 : 1;
    element.FadeTimeLeft = TimeToFade;
    setTimeout("animateFade(" + new Date().getTime() + ",'" + eid + "')", 33);
  }  
  
}


function animateFade(lastTick, eid)
{  
  var curTick = new Date().getTime();
  var elapsedTicks = curTick - lastTick;
 
  var element = document.getElementById(eid);
 
  if(element.FadeTimeLeft <= elapsedTicks)
  {
    element.style.opacity = element.FadeState == 1 ? '1' : '0';
    
//    if (element.style.opacity == '0') 
//        document.getElementById('ctl00_cphBodyTop_ctl00_lblSpacer').style.visibility = 'hidden';
    
    element.style.filter = 'alpha(opacity = '
        + (element.FadeState == 1 ? '100' : '0') + ')';
    element.FadeState = element.FadeState == 1 ? 2 : -2;
    return;
  }
 
  element.FadeTimeLeft -= elapsedTicks;
  var newOpVal = element.FadeTimeLeft/TimeToFade;
  if(element.FadeState == 1)
    newOpVal = 1 - newOpVal;

  element.style.opacity = newOpVal;
  element.style.filter = 'alpha(opacity = ' + (newOpVal*100) + ')';
 
  setTimeout("animateFade(" + curTick + ",'" + eid + "')", 33);
}


function hideItForAll(pnlid)
{
    setTimeout("fade('" + pnlid + "')",5000);
}


function pressEnterKey(e, buttonid)
{ 
      var evt = e ? e : window.event;

      var bt = document.getElementById(buttonid);

      if (bt){ 

          if (evt.keyCode == 13){ 

                bt.click(); 

                return false; 

          } 

      } 

}


function trimAllSides(sString) 
{ 
    while (sString.substring(0,1) == ' ') 
    { 
        sString = sString.substring(1, sString.length); 
    } 
    
    while (sString.substring(sString.length-1, sString.length) == ' ') 
    { 
        sString = sString.substring(0,sString.length-1); 
    }
     
    return sString;
}


function clearSrchMessage(lblid, elemid) 
{
    var lblmsg = document.getElementById(lblid);
    var sender = document.getElementById(elemid);

    if (lblmsg != null && sender != null) {
        lblmsg.innerHTML = "&nbsp;";
    }
}


function stopSrchExec(lblid, txtid) 
{
    var lblmsg = document.getElementById(lblid);
    var txtbox = document.getElementById(txtid);

    if (lblmsg != null && txtbox != null) {
        if (trimAllSides(txtbox.value).length === 0) {
            lblmsg.innerHTML = "Keywords are required.";
            return false;
        }
        else {
            lblmsg.innerHTML = "&nbsp;";
            return true;
        }
    }
}


function Time (s) {var a = s.split(':'); this.date = new Date(1970, 0, 0, a[0] ? a[0] : 0, a[1] ? a[1] : 0, a[2] ? a[2] : 0)}


function CalcTimeDifference(HHStart, MMStart, SSStart, FFStart, HHEnd, MMEnd, SSEnd, FFEnd, Duration, ErrID)
{
    var frStart, frEnd, frOut, secOut, minsOut, hoursOut, timeSecStart, timeSecEnd;
    
    var iHHStart = document.getElementById(HHStart).value;
    var iMMStart = document.getElementById(MMStart).value;
    var iSSStart = document.getElementById(SSStart).value;
    var iFFStart = document.getElementById(FFStart).value;
    var iHHEnd = document.getElementById(HHEnd).value;
    var iMMEnd = document.getElementById(MMEnd).value;
    var iSSEnd = document.getElementById(SSEnd).value;
    var iFFEnd = document.getElementById(FFEnd).value;
    
    
    if ( trimAllSides(iHHStart)!="" && trimAllSides(iMMStart)!="" && trimAllSides(iSSStart)!="" && trimAllSides(iFFStart)!="" && trimAllSides(iHHEnd)!="" && trimAllSides(iMMEnd)!="" && trimAllSides(iSSEnd)!="" && trimAllSides(iFFEnd)!="" )
    {
        if ( isNaN(iHHStart)==false && isNaN(iMMStart)==false && isNaN(iSSStart)==false && isNaN(iFFStart)==false && isNaN(iHHEnd)==false && isNaN(iMMEnd)==false && isNaN(iSSEnd)==false && isNaN(iFFEnd)==false )
        {
            if (iHHStart <= 0)
            { 
                iHHStart = iHHStart * (-1);
                document.getElementById(HHStart).value = iHHStart;
            }
            
            if (iMMStart <= 0)
            { 
                iMMStart = iMMStart * (-1);
                document.getElementById(MMStart).value = iMMStart;
            }
            
            if (iSSStart <= 0)
            { 
                iSSStart = iSSStart * (-1);
                document.getElementById(SSStart).value = iSSStart;
            }
            
            if (iFFStart <= 0)
            { 
                iFFStart = iFFStart * (-1);
                document.getElementById(FFStart).value = iFFStart;
            }
            
            if (iHHEnd <= 0)
            { 
                iHHEnd = iHHEnd * (-1);
                document.getElementById(HHEnd).value = iHHEnd;
            }
            
            if (iMMEnd <= 0)
            { 
                iMMEnd = iMMEnd * (-1);
                document.getElementById(MMEnd).value = iMMEnd;
            }
            
            if (iSSEnd <= 0)
            { 
                iSSEnd = iSSEnd * (-1);
                document.getElementById(SSEnd).value = iSSEnd;
            }
            
            if (iFFEnd <= 0)
            { 
                iFFEnd = iFFEnd * (-1);
                document.getElementById(FFEnd).value = iFFEnd;
            }
            
            
            
//            if ( (iMMStart*1) < (60*1) && (iSSStart*1) < (60*1) && (iFFStart*1) < (60*1) && (iMMEnd*1) < (60*1) && (SSEnd*1) < (60*1) && (FFEnd*1) < (60*1) )
//            {
                                    
                Time.prototype.valueOf = function () {return this.date.getTime()}

                Time.prototype.toString = function () {return this.date.toTimeString()}

                timeSecEnd = (new Time (iHHEnd+":"+iMMEnd+":"+iSSEnd)) / 1000;
                timeSecStart = (new Time (iHHStart+":"+iMMStart+":"+iSSStart)) / 1000;
 
                if ((iFFEnd*1)>=(iFFStart*1))
                {
                    frOut = iFFEnd - iFFStart;
                }
                else
                {
                    timeSecEnd = timeSecEnd - 1;
                    iFFEnd = (iFFEnd*1) + (60*1);
                    frOut = iFFEnd - iFFStart;
                }
 
                secOut = timeSecEnd - timeSecStart;
                
                if ( secOut>=0 )
                {
                    hoursOut = Math.floor(secOut / 3600);
                    secOut = secOut - (hoursOut * 3600);

                    
                    minsOut = Math.floor(secOut / 60);
                    secOut = secOut - (minsOut * 60);
                    
                    if (hoursOut<10)
                        hoursOut = "0" + hoursOut;
                        
                    if (minsOut<10)
                        minsOut = "0" + minsOut;
                        
                    if (secOut<10)
                        secOut = "0" + secOut;
                        
                    if (frOut<10)
                        frOut = "0" + frOut;

                                
                    document.getElementById(Duration).value = hoursOut+":"+minsOut+":"+secOut+":"+frOut;
                    document.getElementById(ErrID).innerHTML = "";
                }
                else
                {
                    document.getElementById(Duration).value = "";
                    document.getElementById(ErrID).innerHTML = "Error: End Time Code cannot be less than Start Time Code.";
                }
                
//            }
//            else
//            {
//                document.getElementById(Duration).value = "";
//                document.getElementById(ErrID).innerHTML = "Error: Minutes, seconds, and frames must be less than 60.";
//            }
          
        }
        else
        {
            document.getElementById(Duration).value = "";
            document.getElementById(ErrID).innerHTML = "Error: Time Codes must be positive integer values.";
        }
    }
    else
    {
        document.getElementById(Duration).value = "";
    }
}


function ShowDiv(objid,hideid)
{
    var objDiv;
    var index = objid.substr(objid.length-2, 2);
    var objName = objid.substring(0, objid.length-2);
    
    objDiv = document.getElementById(objid);
    
    while (objDiv != null)
    {
        if (objDiv.style.visibility == "hidden")
        {
            objDiv.style.visibility = "visible";
            break;
        }
        else
        {                                
            index = index*1+1;
            objDiv = document.getElementById(objName + index);
        }
    }
    
    index = index*1+1;
    objDiv = document.getElementById(objName + index);
    
    if (objDiv == null)
        document.getElementById(hideid).style.visibility = "hidden";
    
    return false;
}
