//var expiresecs = Date.parse(Date()) + 960000;
var expiresecs = Date.parse(Date()) + 1920000;
//var DHTML = (document.GetElementById || document.all || document.layers);
function getObj(name)
{
  if (document.getElementById)
  {
    if ( document.getElementById(name) )
    {
      this.obj = document.getElementById(name);
      this.style = document.getElementById(name).style;
    }
    else
    {
      this.obj = false;
    }
  }
  else if (document.all)
  {
    this.obj = document.all[name];
    this.style = document.all[name].style;
  }
  else if (document.layers)
  {
    this.obj = document.layers[name];
    this.style = document.layers[name];
  }
}

function hitPing()
{
  if ( !handlesXmlHttp())
  {
    window.open("ping.php","pingwin","resizble=no, scrollbars=no,width=300,height=400")
    //document.problemset_worksheet.submit();
    return;
  }
  var xmlhttp=false;
  /*@cc_on @*/
  /*@if (@_jscript_version >= 5)
  // JScript gives us Conditional compilation, we can cope with old IE versions.
  // and security blocked creation of the objects.
   try {
    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
   } catch (e) {
    try {
     xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (E) {
     xmlhttp = false;
    }
   }
  @end @*/
  if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
    xmlhttp = new XMLHttpRequest();
  }
  xmlhttp.open("GET", "ping.php", true);
  xmlhttp.onreadystatechange=function() {
    if (xmlhttp.readyState==4)
    {
    }
  }
  xmlhttp.send(null);
}
 
function loadCurTime(secs)
{
  var nowsecs = Date.parse(Date());
//alert(nowsecs + " Now\n" + expiresecs + " Expire");
  if (expiresecs-nowsecs<120000)
  {
    if (confirm("You will be logged out in two minutes after a period of inactivity.  Please press 'Cancel' to log out now, or press 'OK' to continue accessing the site."))
    {
      nowsecs = Date.parse(Date());
      if (expiresecs>nowsecs)
      {
        expiresecs = Date.parse(Date()) + 960000;
//alert(nowsecs + " Now\n" + expiresecs + " Expire");
//        location.replace(location.href);
        hitPing();
      }
      else
      {
//alert(nowsecs + " Now\n" + expiresecs + " Expire");
        location.replace("/logout.php");
      }
    }
    else
    {
      location.replace("/logout.php?src=automated");
    }
  }

  var timeDiv = new getObj('curtime');
  var tmpDate = new Date(secs);
  var str = 'Current server time is: ';
  str = str + tmpDate.getFullYear() + '-';
  if ( tmpDate.getMonth()<9 )
    str = str + '0';
  str = str + (tmpDate.getMonth() +1) + '-';
  if ( tmpDate.getDate()<10 )
    str = str + '0';
  str = str + tmpDate.getDate() + ' ';
  if ( tmpDate.getHours()<10 )
    str = str + '0';
  str = str + tmpDate.getHours() + ':';
  if ( tmpDate.getMinutes()<10 )
    str = str + '0';
  str = str + tmpDate.getMinutes();
  timeDiv.obj.innerHTML=str;

  var remaindersecs = (60-tmpDate.getSeconds())*1000;

  if (remaindersecs<1000)
    remaindersecs=60000;
  secs = secs + remaindersecs;
  self.setTimeout("loadCurTime(" + secs + ")",remaindersecs);

}


function handlesXmlHttp()
{
  var ua = navigator.userAgent.toLowerCase();

  if (ua.indexOf("safari") > -1 )
  {
//return false;
    var vindex = ua.indexOf("applewebkit/");
    var vindex2 = ua.indexOf(".",vindex);
    var vindex3 = ua.indexOf(" ",vindex);
    if (vindex2==-1 || vindex3<vindex2)
    {
      vindex2 = vindex3;
    }
    var v = ua.substring(vindex+12,vindex2);
//alert(v);
    return (v>=124);
  }
  return true;
}


function openHelpWin(hid)
{

  window.open('viewhelp.php?hid=' + hid,'HELPWIN','resizable=0,scrollbars=0,height=335,width=420');

}

// Return true if value is a number
function isNumber(value)
{
  if (value=="") return false;
  var d = parseInt(value);
  if (!isNaN(d)) return true; else return false;		
}

// Return true if value is a float
function isFloat(value)
{
  if (value=="") return false;
  var d = parseFloat(value);
  if (!isNaN(d)) return true; else return false;
}

function trim(str)
{
  str = str.replace(/^\s*|\s*$/g,"");
  return str;
}

function isTime(value)
{
  var sepindex1 = value.indexOf(":");
  var val = value.substr(0,sepindex1);
  if (!isNumber(val) || val < 0 || val > 23)
  {
    return false;
  }
  val = value.substring(sepindex1+1);
  if (!isNumber(val) || val < 0 || val > 59)
  {
    return false;
  }

  return true;
}

function returnDate(dateVal, timeVal)
{
  if (!isDate(dateVal) || !isTime(timeVal))
  {
    return false;
  }

  var sepindex1 = dateVal.indexOf("/");
  var mm = dateVal.substring(0,sepindex1);
  var sepindex2 = dateVal.indexOf("/",sepindex1+1);
  var dd = dateVal.substring(sepindex1+1,sepindex2);
  var yy = dateVal.substring(sepindex2+1);

  sepindex1 = timeVal.indexOf(":");
  var hh = timeVal.substr(0,sepindex1);
  var nn = timeVal.substring(sepindex1+1);

  if (yy=='08')
  {
    yy = 2008;
  }
  else if (yy=='09')
  {
    yy = 2009;
  }
  else if (yy < 100)
  {
    yy = 2000 + parseInt(yy);
  }

  mm = mm - 1;

  var dt = new Date();
  dt.setYear(yy);
  dt.setMonth(mm);
  dt.setDate(dd);
  dt.setHours(hh);
  dt.setMinutes(nn);
  dt.setSeconds(0);
  dt.setMilliseconds(0);

  return dt;

}

function isDate(value)
{
  var sepindex1 = value.indexOf("/");
  var mm = value.substring(0,sepindex1);
  var sepindex2 = value.indexOf("/",sepindex1+1);
  var dd = value.substring(sepindex1+1,sepindex2);
  var yy = value.substring(sepindex2+1);
  if (!isNumber(dd) || !isNumber(mm) || !isNumber(yy))
  {
    return false;
  }

  // Bug where parseInt((08|09)) == 0
  if (yy=='08')
  {
    yy = 2008;
  }
  else if (yy=='09')
  {
    yy = 2009;
  }
  else if (yy < 100)
  {
    yy = 2000 + parseInt(yy);
  }

  var maxdd = 31;
  if (mm == 4 || mm==6 || mm==9 || mm==11)
  {
    maxdd = 30;
  }
  else if (mm == 2)
  {
    maxdd = 28;
    if (yy % 4 == 0) 
    {
      maxdd = 29;
    }
    if (yy % 100 == 0)
    {
      maxdd = 28;
    }
    if (yy % 400 == 0)
    {
      maxdd = 29;
    }
  }

  if (yy < 1970 || yy > 2037)
  {
    return false;
  }
  if (mm < 1 || mm > 12)
  {
    return false;
  }
  if (dd < 1 || dd > maxdd)
  {
    return false;
  }
  return true;
}


function validateElem(name,nicename,elemtype,minval,maxval)
{
  var elem = new getObj(name);
  if (!elem.obj)
  {
    return "Object not found: " + name;
  }
//alert('meep!');
  var val = 0;
  var str = "";

//alert( "starting elem " + name + "," + nicename + "," + elemtype + "," + minval + "," + maxval);
  if (elemtype=='email')
  {
    val = elem.obj.value;

    // 2949: Reverting to simple email validation in javascript, moving more thorough check to php
    //// 2949 partial fix, still hangs on \w{60+}@\w\. on safari
    ////if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w+)+$/.test(val)))
    //if (!(/^\w+([\.-]\w+)*@\w+([\.-]\w+)*(\.\w+)+$/.test(val)))
    ////if (true)
    //{
    //  return "Not a valid e-mail address: " + val;
    //}
    var ix = val.indexOf('@');
    if (ix < 1)
    {
      return "Not a valid e-mail address: " + val;
    }
    var ix = val.indexOf('.',ix);
    if (ix == -1)
    {
      return "Not a valid e-mail address: " + val;
    }
    val = val.length;
    str = "Length of " + nicename + " must be ";
  }
  else if (elemtype=='password')
  {
    val = elem.obj.value;
    if (!val.match(/[abcdefghijklmnopqrstuvwxyz]/))
    {
      return nicename + " must contain at least one upper-case, one lower-case, and one number, and have between 6 and 32 characters";
    }
    else if (!val.match(/[ABCDEFGHIJKLMNOPQRSTUVWXYZ]/))
    {
      return nicename + " must contain at least one upper-case, one lower-case, and one number, and have between 6 and 32 characters";
    }
    else if (!val.match(/[0123456789]/))
    {
      return nicename + " must contain at least one upper-case, one lower-case, and one number, and have between 6 and 32 characters";
    }
    else if (val.length < 6)
    {
      return nicename + " must contain at least one upper-case, one lower-case, and one number, and have between 6 and 32 characters";
    }
    else
    {
      return "";
    }
  }
  else if (elemtype=='txt')
  {
    val = elem.obj.value;
    val = val.replace(/^\s*|\s*$/g,"");
    val = val.length;
    //str = "Length of " + nicename + " must be ";
    str = nicename + " is required";
  }
  else if (elemtype=='num')
  {
    val = elem.obj.value;
    str = "Value of " + nicename + " must be ";
    if (!isFloat(val))
      return "Value of " + nicename + " should be a number";
  }
  else if (elemtype=='dropdown')
  {
//return 'we have a dropdown: ' + nicename;
    val = elem.obj.options[elem.obj.selectedIndex].value;
//alert(val);
    if (val <= 0)
    {
      //return  "Please select a " + nicename;
      return "Please make a selection for the dropdown " + nicename;
    }
    else
    {
      return "";
    }
  }
  else if (elemtype=='date')
  {
    val = elem.obj.value;
    if (!isDate(val))
    {
      return nicename + " must be of format mm/dd/yy and be between 1970 and 2037";
    }
    else
    {
      return '';
    }
  }
  else if (elemtype=='time')
  {
    val = elem.obj.value;
    if (!isTime(val))
    {
      return nicename + " must be of format hh:mm";
    }
    else
    {
      return '';
    }
  }
  else
  {
    return "Unknown type for " + nicename + ": " + elemtype;
  }
  if (val < minval && minval != -9998)
  {
    if (elemtype=='txt')
    {
      return str;
    }
    else
    {
      //return str + "at least " + minval;
      return str + "must be between " + minval + " and " + maxval;
    }
  }
  
  if (val > maxval && maxval != 9998)
  {
    if (elemtype=='txt')
    {
      return str + " and must be less than " + maxval + " characters long";
    }
    else
    {
      //return str + "at most " + maxval;
      return str + "must be between " + minval + " and " + maxval;
    }
  }
  return "";


}

function updateSes(curPage)
{
  if ( !handlesXmlHttp() )
  {
    window.open("ping.php","Kinetic Books Homework","resizble=no, scrollbars=no,width=300,height=400")
    //alert("The browser you are using cannot automatically keep your session alive.  Please click on a link or save or submit your progress to avoid being logged out.");
    return;
  }
  var xmlhttp=false;
  /*@cc_on @*/
  /*@if (@_jscript_version >= 5)
  // JScript gives us Conditional compilation, we can cope with old IE versions.
  // and security blocked creation of the objects.
   try {
    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
   } catch (e) {
    try {
     xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (E) {
     xmlhttp = false;
    }
   }
  @end @*/
  if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
    xmlhttp = new XMLHttpRequest();
  }
  xmlhttp.open("GET", "inc/seshead.inc.php?xmlhttp=true&curpage=" + curPage, true);
  xmlhttp.onreadystatechange=function() {
    if (xmlhttp.readyState==4)
    {
      var str = xmlhttp.responseText;
      alert(str);
    }
  }
  xmlhttp.send(null);
}

function noenter() 
{
  return !(window.event && (window.event.keyCode == 13 || window.event.keyCode == 3)); 
}
