// Get the absolute position of a DOM object
function absXPos(obj)
{
  var xpl = obj.offsetLeft;
  while(obj.offsetParent != undefined && obj.offsetParent.tagName != 'BODY'){
    obj = obj.offsetParent;
    xpl += obj.offsetLeft;
  }
  if(obj.offsetParent != undefined)
    xpl += obj.offsetParent.offsetLeft; // Grab the left offset of the body
  return xpl;
}

function absYPos(obj)
{
  var yp = obj.offsetTop;
  
  while(obj.offsetParent != undefined && obj.offsetParent.tagName != 'BODY'){
    obj = obj.offsetParent;
    yp += obj.offsetTop;
  }
  if(obj.offsetParent != undefined)
    yp += obj.offsetParent.offsetTop;
  return yp;
}

// Get the size of a DOM object
function ElementWidth(obj)
{
  if(obj.style.width)
    return obj.style.width;
  if(obj.offsetWidth != 0)
    return obj.offsetWidth;
  return 10;
}

function ElementHeight(obj)
{
  if(obj.style.height)
    return obj.style.height;
  if(obj.offsetHeight != 0)
    return obj.offsetHeight;
  return 10;
}

// Visibility Functions
function hide(divid){
  var div=document.getElementById(divid);
  div.style.visibility="hidden";
}

function show(divid){
  var div=document.getElementById(divid);
  div.style.visibility="visible";
}

function showOver(divid, overwhat){
  var div = document.getElementById(divid);
  var bdiv = document.getElementById(overwhat);
  
  div.style.visibility="visible";
  div.style.position = "absolute";
  var xp = ElementWidth(bdiv)/2;
  var yp = ElementHeight(bdiv)/2;
  var nxp = absXPos(bdiv) + xp;
  var nyp = absYPos(bdiv) + yp;
  div.style.left = nxp+"px";
  div.style.top = nyp+"px";
}

function setTimeFld(tmid,tmval)
{
  var flds = tmval.split(':');
  var hr = parseInt(flds[0]);
  if(hr > 12){
    document.getElementById(tmid+'_suffix').value="PM";
    hr -= 12;
  }else if(hr == 12){
    document.getElementById(tmid+'_suffix').value="PM";
  }else{
    document.getElementById(tmid+'_suffix').value="AM";
  }
  document.getElementById(tmid).value = hr+':'+flds[1];
}

// Validation functions

function HasText(sText){
  if(sText.length == 0)
    return false;
  var BlankChars = " \n\t";

  for(i = 0;i<sText.length;i++){
    Char = sText.charAt(i); 
    if(BlankChars.indexOf(Char) == -1)
      return true;
  }
  return false;
}

function IsNumeric(sText){
  var ValidChars = "0123456789.";
  var IsNumber=true;
  var Char;
  
  
  for (i = 0; i < sText.length && IsNumber == true; i++){ 
    Char = sText.charAt(i); 
    if (ValidChars.indexOf(Char) == -1){
      IsNumber = false;
    }
  }
  return IsNumber;
}

function isDate(value){
  var fields = value.split("/");
  if(fields.length != 3)
    return false;
  if(!IsNumeric(fields[0]) || fields[0] < 1 || fields[0] > 12)
    return false;
  if(!IsNumeric(fields[1]) || fields[1] < 1 || fields[1] > 31)
    return false;
  if(!isYear(fields[2]))
    return false;
  
  if(value == null || value.length==0)
    return false;
  return true;
}

function isTime(value){
  var fields = value.split(":");
  if(fields.length != 2)
    return false;
  var fields = value.split(":");
  if(!IsNumeric(fields[0]) || fields[0] < 0 || fields[0] > 24)
    return false;
  if(!IsNumeric(fields[1]) || fields[1] < 0 || fields[1] > 59)
    return false;
  if(value == null || value.length==0)
    return false;
  return true;
  
}

function isYear(value){
  if(value == null || value.length!=4)
    return false;
  if(value > 2020 || value < 2005)
    return false;
  return true;
}

// Communication
function encode(string) {
  return string.replace('&','%26');
}

function initXMLHttpClient() {
  var xmlhttp;
  try {
    // Mozilla / Safari / IE7
    xmlhttp = new XMLHttpRequest();
  } catch (e) {
    // IE
    var XMLHTTP_IDS = new Array('MSXML2.XMLHTTP.5.0',
				'MSXML2.XMLHTTP.4.0',
				'MSXML2.XMLHTTP.3.0',
				'MSXML2.XMLHTTP',
				'Microsoft.XMLHTTP' );
    var success = false;
    for (var i=0;i < XMLHTTP_IDS.length && !success; i++) {
      try {
	xmlhttp = new ActiveXObject(XMLHTTP_IDS[i]);
	success = true;
      } catch (e) {}
    }
    if (!success) {
      throw new Error('Unable to create XMLHttpRequest.');
    }
  }
  return xmlhttp;
}


// A message box
function msgBox(elemid, message){
  // pop up a message box positioned at the center of elemid
  var msgbox = document.getElementById("messageBox");
  msgbox.innerHTML='<div class="message">'+message+
        '</div><p><div class="bbar"><a class="AButton" onclick="hide('+
        "'messageBox'"+');">Ok</a></div>';
  showOver("messageBox", elemid);
}
function insertWebcam(width, height)
{
// Set the BaseURL to the URL of your camera
var BaseURL = "http://windturbine.ceas.wmich.edu/";

// DisplayWidth & DisplayHeight specifies the displayed width & height of the image.
// You may change these numbers, the effect will be a stretched or a shrunk image
var DisplayWidth = width;
var DisplayHeight = height;

// This is the path to the image generating file inside the camera itself
var File = "axis-cgi/mjpg/video.cgi?resolution="+width+"x"+height;

// No changes required below this point
var output = "";
if ((navigator.appName == "Microsoft Internet Explorer") &&
   (navigator.platform != "MacPPC") && (navigator.platform != "Mac68k"))
{
  // If Internet Explorer under Windows then use ActiveX 
  output  = '<OBJECT ID="Player" width='
  output += DisplayWidth;
  output += ' height=';
  output += DisplayHeight;
  output += ' CLASSID="CLSID:DE625294-70E6-45ED-B895-CFFA13AEB044" ';
  output += 'CODEBASE="';
  output += BaseURL;
  output += 'activex/AMC.cab#version=4,1,5,5">';
  output += '<PARAM NAME="MediaURL" VALUE="';
  output += BaseURL;
  output += File + '">';
  output += '<param name="MediaType" value="mjpeg-unicast">';
  output += '<param name="ShowStatusBar" value="0">';
  output += '<param name="ShowToolbar" value="0">';
  output += '<param name="AutoStart" value="1">';
  output += '<param name="StretchToFit" value="1">';
  output += '<BR><B>Axis Media Control</B><BR>';
  output += 'The AXIS Media Control, which enables you ';
  output += 'to view live image streams in Microsoft Internet';
  output += ' Explorer, could not be registered on your computer.';
  output += '<BR></OBJECT>';
} else {
  // If not IE for Windows use the browser itself to display
  theDate = new Date();
  output  = '<IMG SRC="';
  output += BaseURL;
  output += File;
  output += '&dummy=' + theDate.getTime().toString(10);
  output += '" HEIGHT="';
  output += DisplayHeight;
  output += '" WIDTH="';
  output += DisplayWidth;
  output += '" ALT="Camera Image">';
}
document.write(output);
document.Player.ToolbarConfiguration = "play,+snapshot,+fullscreen"

// Remove the // below to use the code for Motion Detection. 
  // document.Player.UIMode = "MDConfig";
  // document.Player.MotionConfigURL = "/axis-cgi/operator/param.cgi?ImageSource=0"
  // document.Player.MotionDataURL = "/axis-cgi/motion/motiondata.cgi";

}