var SERVER_PATH = "http://web.ideal-house.org";

function launch(url) {
  var w = 480, h = 340;

  if (document.all) {
    /* the following is only available after onLoad */
    w = document.body.clientWidth;
    h = document.body.clientHeight;
  }
  else if (document.layers) {
    w = window.innerWidth;
    h = window.innerHeight;
  }

  var popW = 465, popH = 570;
  var leftPos = (w-popW)/2, topPos = (h-popH)/2;

  self.name = "opener";
  remote = open(url, 'helpWin', "resizable,scrollbars,status,width=" + popW + ",height="+popH+",left="+leftPos+",top="+topPos+"");
}

function openPopUp(thePage, theName, theWidth, theHeight)
{
  var someFeatures = 'scrollbars=yes,toolbar=0,location=no,directories=0,status=0,menubar=0,resizable=1,width=' + theWidth + ',height=' + theHeight;
  var aPopUpWin = window.open(thePage, theName, someFeatures);

  if (navigator.appName == "Netscape") {
    aPopUpWin.focus();
  }
}

function showStatus(msg)
{
  window.status = msg
  return true
}

function DrawTB1(UseAudio, ImagePath, CamNbr, MediaRecording, HideMicSettings) // NOTE: HideMicSettings = "yes" for 207, otherwise it should be "no" (or undefined)
{
  if (document.Player) {
    if (UseAudio == "yes") {
      if (HideMicSettings == "yes") {
        document.Player.ToolbarConfiguration = "+play,+snapshot,+fullscreen,+mute,+volume";
      } else {
        remaining_audio_init_timeout = 5000;
        doAudioCheck1(ImagePath, CamNbr, MediaRecording);
      }
    }
    else if (ImagePath.indexOf("mpeg4") != -1 && MediaRecording != 0) {
      document.Player.ToolbarConfiguration = "+play,+snapshot,+rec,+fullscreen";
      document.Player.EnableRecording = MediaRecording;
    }
    else {
      document.Player.ToolbarConfiguration = "+play,+snapshot,+fullscreen";
    }
    if (UseAudio == "yes") {
       document.Player.Play();
    }
  }
}

function doAudioCheck1(ImagePath, CamNbr, MediaRecording)
{
  if (!document.AudioComponent) {
    if (remaining_audio_init_timeout > 0) {
      setTimeout('doAudioCheck1()', 250);
      remaining_audio_init_timeout -= 250;
    }
    else if (ImagePath.indexOf("mpeg4") != -1 && MediaRecording != 0) {
      document.Player.ToolbarConfiguration = "+play,+snapshot,+rec,+fullscreen";
      document.Player.EnableRecording = MediaRecording;
    }
    else {
      document.Player.ToolbarConfiguration = "+play,+snapshot,+fullscreen";
    }
  }
  else {
    initAudio1(ImagePath, CamNbr, MediaRecording);
  }
}

function initAudio1(imagepath, camnbr, mediarecording)
{
  document.Player.AudioConfigURL = "http://web.ideal-house.org/axis-cgi/audio/getparam.cgi?camera=" + camnbr;
  document.Player.AudioTransmitURL = "http://web.ideal-house.org/axis-cgi/audio/transmit.cgi?camera=" + camnbr;
  if (imagepath.indexOf("mjpg") != -1) {
    document.Player.AudioReceiveURL = "http://web.ideal-house.org/axis-cgi/audio/receive.cgi?camera=" + camnbr;
  }
  if (imagepath.indexOf("mpeg4") != -1 && mediarecording != 0) {
    document.Player.ToolbarConfiguration = "+play,+snapshot,+rec,+fullscreen,+audiocontrol";
    document.Player.EnableRecording = mediarecording;
  }
  else {
    document.Player.ToolbarConfiguration = "+play,+snapshot,+fullscreen,+audiocontrol";
  }
}

function InstallFilter1(prodName, ID, CLSID_filter, ver_filter, cab, installText1, text, installText2, installText3, installText4)
{
  document.write('<object ID="'+ ID + '" height="0" width="0" CLASSID="CLSID:' + CLSID_filter + '" CODEBASE="http://web.ideal-house.org/activex/' + cab + '#Version=' + ver_filter + '">');
  document.write('<br><b>' + ID + '</b>&nbsp;' + installText1 + '&nbsp;' + text + '&nbsp;' + installText2 + '&nbsp;' + installText3 + '<br>' +ID + '&nbsp;' + installText4 + '<br>');
  document.write('</OBJECT>');
}

function InstallDecoder1(prodName, ID, CLSID_AMC, cab, ver_AMC, authorized, notAuthorizedText, authorizedText, installDecoderText1, installDecoderText2)
{
    document.write('<OBJECT ID="'+ ID + '" height="0" width="0" CLASSID="CLSID:' + CLSID_AMC + '" CODEBASE="' + cab + '#version=' + ver_AMC + '" STANDBY="Loading Axis Media Control components..."');
    if(authorized != "yes")
    {
      document.write('<br><p><b>' + notAuthorizedText + '</b><br>');
    } else {
      document.write('<br><p><a href="javascript:launch(\'')
      document.write('http://web.ideal-house.org/incl/license.shtml\')">' + authorizedText + '</a><br>')
    }
    document.write('<br>' + installDecoderText1);
    if(authorized == "yes")
    {
      document.write('<br>' + installDecoderText2);
    }
    document.write('<br></OBJECT>');

}


function DrawAMC1(prodName, ID, height, width, imagepath, CLSID_AMC, cab, ver_AMC, ShowAMCToolbar, ptzgui, useWithCam, camnbr, UseRel, ShowRelCross, UseMotion, UseAudio, rtspPort, external, installText1, text, installText2, installText3, installText4, mediaRecording, HideMicSettings) // NOTE: HideMicSettings = "yes" for 207, otherwise it should be "no" (or undefined)
{
  var rtspAddress = "rtsp://web.ideal-house.org" //+ location.hostname

  if (imagepath.indexOf("mpeg4") != -1) {
    var mpeg4 = "yes";
  }
  if (imagepath.indexOf("mjpg") != -1) {
    var mjpg = "yes";
  }
  if (imagepath.indexOf("mpeg2") != -1) {
    var mpeg2 = "yes";
  }
  if (imagepath.indexOf("sdp") != -1) {
    var multicast = "yes";
  }

  document.write('<OBJECT ID="Player" height="' + height + '" width="' + width + '" border="0" CLASSID="CLSID:' + CLSID_AMC + '" CODEBASE="http://web.ideal-house.org/activex/' + cab + '#version=' + ver_AMC + '" STANDBY="Loading Axis Media Control components...">');
  if(mpeg4 == "yes" && external == "no") {
    document.write('<param name="MediaURL" value="' + rtspAddress + ':' + rtspPort + '' + imagepath + '">');
  } else {
    document.write('<param name="MediaURL" value="http://web.ideal-house.org' + imagepath + '">');
  }
  if ((mpeg4 == "yes") && (multicast != "yes")) {
    document.write('<param name="MediaType" value="auto-sense">');
  } else if (multicast == "yes") {
    document.write('<param name="MediaType" value="multicast">');
  }
  if (mjpg == "yes") {
    document.write('<param name="MediaType" value="mjpeg-unicast">');
  }
  if (mpeg2 == "yes") {
    document.write('<param name="MediaType" value="mpeg2-unicast">');
  }
  if (HideMicSettings == "yes") {  // HideMicSettings defines 207 - No other relation between HideMicSettings and Volume=70
    document.write('<param name="Volume" value="70">');
  }
  document.write('<param name="ShowStatusBar"');
  if (ShowAMCToolbar == "yes") {
    document.write('value="1">');
  } else {
    document.write('value="0">');
  }
  document.write('<param name="ShowToolbar"');
  if (ShowAMCToolbar == "yes") {
    document.write('value="1">');
  } else {
    document.write('value="0">');
  }

  if (UseAudio == "yes") {
    document.write('<param name="AutoStart" value="0">');
  }
  else {
    document.write('<param name="AutoStart" value="1">');
  }

  document.write('<param name="StretchToFit" value="1">');

  if ((ptzgui == "yes") && (useWithCam == "yes")) {
    document.write('<param name="PTZControlURL" value="http://web.ideal-house.org/axis-cgi/com/ptz.cgi?camera=' + camnbr + '">');
    document.write('<param name="UIMode"');
    if (UseRel == "no") {
      document.write('value="ptz-absolute">');
    } else if (ShowRelCross == "yes") {
      document.write('value="ptz-relative">');
    } else {
      document.write('value="ptz-relative-no-cross">');
    }
  }

  document.write('<br><b>' + ID + '</b>&nbsp;' + installText1 + '&nbsp;' + text + '&nbsp;' + installText2 + '&nbsp;' + installText3 + '<br>' + ID + '&nbsp;' + installText4);
   document.write('<br><br></OBJECT>');


  if (document.Player.ShowToolbar) { // This line is essential when installing AMC or else volume and mute buttons will show until reload
    if (ShowAMCToolbar == "yes") {
      if (HideMicSettings == "yes") {
        document.Player.ToolbarConfiguration = "+play,+snapshot,+fullscreen,+mute,+volume";
      } else if (mpeg4 == "yes" && mediaRecording != 0) {
        document.Player.ToolbarConfiguration = "+play,+snapshot,+rec,+fullscreen";
        document.Player.EnableRecording = mediaRecording;
      }
      else {
        document.Player.ToolbarConfiguration = "+play,+snapshot,+fullscreen";
      }
    }
  }
  if (UseMotion == "yes") {
    document.Player.UIMode = "MDConfig";
    document.Player.MotionConfigURL = "http://web.ideal-house.org/axis-cgi/operator/param.cgi?ImageSource=" + [camnbr - 1]
    document.Player.MotionDataURL = "http://web.ideal-house.org/axis-cgi/motion/motiondata.cgi";
  }
  document.Player.EnableContextMenu = "1";
}

function CSS ()
{
  if ((navigator.appVersion.indexOf("Mac") != -1) && (navigator.appName.indexOf("Netscape") != -1)) {
    document.write('<link rel="stylesheet" href="' + SERVER_PATH + '/css/mac_ns.css" type="text/css">');

  } else if ((navigator.appVersion.indexOf("Mac") != -1) && (navigator.appName.indexOf("Microsoft Internet Explorer") != -1)) {
    document.write('<link rel="stylesheet" href="' + SERVER_PATH + '/css/mac_ie.css" type="text/css">');

  } else if ((navigator.appVersion.indexOf("Win") != -1) && (navigator.appName.indexOf("Netscape") != -1)) {
    document.write('<link rel="stylesheet" href="' + SERVER_PATH + '/css/win_ns.css" type="text/css">');

  } else if ((navigator.appVersion.indexOf("Win") != -1) && (navigator.appName.indexOf("Microsoft Internet Explorer") != -1)) {
    document.write('<link rel="stylesheet" href="' + SERVER_PATH + '/css/win_ie.css" type="text/css">');

  } else if (navigator.appName.indexOf("Netscape") != -1) {
    // Unix or other system
    document.write('<link rel="stylesheet" href="' + SERVER_PATH + '/css/win_ns.css" type="text/css">');
  } else {
    document.write('<link rel="stylesheet" href="' + SERVER_PATH + '/css/other.css" type="text/css">');
  }

  // Wrapper for old browsers which can't handle getElementById
  if(!document.getElementById) {
     document.getElementById = function(element)
     {
       return eval("document.all." + element);
     }
  }
}

CSS ();

function SubmitForm()
{
  document.WizardForm.submit();
}

//END GLOBAL JAVASCRIPTS


if ((navigator.appName == "Microsoft Internet Explorer") &&
    (navigator.platform != "MacPPC") &&
    (navigator.platform != "Mac68k")) {
  var browser = "IE"
} else {
  var browser = "Netscape"
}

function drawBoxHeader(title)
{
  var args = drawBoxHeader.arguments;
  var fill = (args.length >= 2 ? args[1] : false);

  document.write('' +
'<td' + (fill ? ' width="100%"' : '') + '>' +
'  <table width="100%" height="46" cellpadding=0 cellspacing=0 border=0>' +
'    <tr>' +
'      <td colspan=2 width=5 background="' + SERVER_PATH + '/pics/gray_corner_lt_5x50px.gif"><img src="' + SERVER_PATH + '/pics/blank.gif" width=5 height=5 border=0 alt=""></td>' +
'      <td valign="top" align="center" width="100%" background="' + SERVER_PATH + '/pics/gray_t_5x50px.gif" height="5" class="funcText" nowrap>' + title + '</td>' +
'      <td colspan=2 width=5  background="' + SERVER_PATH + '/pics/gray_corner_rt_5x50px.gif"><img src="' + SERVER_PATH + '/pics/blank.gif" width=5 height=5 border=0 alt=""></td>' +
'    </tr>' +
'    <tr>' +
'      <td width=1 class="lineBg"><img src="' + SERVER_PATH + '/pics/blank.gif" width=1 height=5 alt=""></td>' +
'      <td width=4><img src="' + SERVER_PATH + '/pics/blank.gif" width=4 height=5 alt=""></td>' +
'      <td valign="middle" align="center" width="100%">' +
'        <table cellpadding=0 cellspacing=0 border=0>' +
'          <tr>');
}

function drawBoxFooter()
{
  document.write('' +
'          </tr>' +
'        </table>' +
'      </td>' +
'      <td width=4><img src="' + SERVER_PATH + '/pics/blank.gif" width=4 height=5 alt=""></td>' +
'      <td width=1 class="lineBg"><img src="' + SERVER_PATH + '/pics/blank.gif" width=1 height=5 alt=""></td>' +
'    </tr>' +
'    <tr>' +
'      <td colspan=2 width=5 height=5><img src="' + SERVER_PATH + '/pics/line_corner_lb_5x5px.gif" width=5 height=5 border=0 alt=""></td>' +
'      <td width="100%" height=5 background="' + SERVER_PATH + '/pics/line_b_100x5px.gif"><img src="' + SERVER_PATH + '/pics/blank.gif" width=1 height=5 alt=""></td>' +
'      <td colspan=2 width=5 height=5><img src="' + SERVER_PATH + '/pics/line_corner_rb_5x5px.gif" width=5 height=5 border=0 alt=""></td>' +
'    </tr>' +
'  </table>' +
'</td>');
}

function drawBoxButton(pic, title)
{
  var args = drawBoxButton.arguments;
  var url =      (args.length >= 3 ? args[2] : null);
  var name =     (args.length >= 4 ? args[3] : null);
  var pressed =  (args.length >= 5 ? args[4] : null);
  var on_click = (args.length >= 6 ? args[5] : null);
  var width =    (args.length >= 7 ? args[6] : 27);
  var height =   (args.length >= 8 ? args[7] : 27);

  if (url) {
    if (name) {
      document.write('<td valign="middle" align="left">' +
                     '<a href="' + url + '" target="Temp"' +
                     (on_click ? ' onClick="' + on_click+ '"' : '') +
                     ' onMouseDown="movepic(\'' + name + '\', \'' + pressed + '\')"' +
                     ' onMouseUp="movepic(\'' + name + '\', \'' + pic + '\')"' +
                     '>' +
                     '<img name="' + name + '" src="' + pic + '" ' +
                     'width=' + width + ' height=' + height + ' border=0 ' +
                     (title ? ' title="' + title + '" alt="' + title + '"' : '') +
                     '>' +
                     '</a>' +
                     '</td>');
    } else {
      document.write('<td valign="middle" align="left">' +
                     '<a href="' + url + '" target="_self"' +
                     (on_click ? ' onClick="' + on_click+ '"' : '') +
                     '>' +
                     '<img src="' + pic + '" ' +
                     'width=' + width + ' height=' + height + ' border=0 ' +
                     (title ? ' title="' + title + '" alt="' + title + '"' : '') +
                     '>' +
                     '</a>' +
                     '</td>');
    }
  } else {
    document.write('<td valign="middle" align="left">' +
                   '<img src="' + pic + '" ' +
                   'width=' + width + ' height=' + height + ' border=0' +
                   (title ? ' title="' + title + '" alt="' + title + '"' : '') +
                   '>' +
                   '</td>');
  }
}

function drawBoxSpacer()
{
  document.write('<td width=5><img src="' + SERVER_PATH + '/pics/blank.gif" width=5 height=5 alt=""></td>');
}

function extractVideoNbr(theParamPos)
{
  var i = 0;
  var start, stop;
  var len;
  var colonCount = 0;

  while (theParamPos.charAt(i++) != '~' && i < theParamPos.length);
  if (i >= theParamPos.length) {
    return -1;
  }
  while (colonCount < 6 && i < theParamPos.length) {
    if (theParamPos.charAt(i++) == ':') {
      colonCount++;
      if (colonCount == 4)
        start = i;
      if (colonCount == 5)
        len = i-2-start+1;
    }
  }
  if (i < theParamPos.length && len > 0)
    return parseInt(theParamPos.substr(start,len));
  else
   return -1;
}

function extractPresetNbr(name)
{
  var nbr = name.slice(20, -4)
  return parseInt(nbr)
}

function selectPresLink(elem) {
    var i=1;
    while (true) {
        var curElem = document.getElementById("presLink" + i++);
        if (!curElem) {
            break;
        }
        if (curElem == elem) {
            curElem.style.fontWeight = "bold";
        } else {
            curElem.style.fontWeight = "normal";
        }
    }
}

function listVideoSources()
{
  var formInt = document.listFormInt
  var formExt = document.listFormExt
  var presetForm = document.listFormPreset;
  var form = document.WizardForm
  var imageType = '/mjpg/';
  var imageSource = 'video.mjpg';

  if ((1 > 1) ||
      ext_sources ||
      presetForm.elements.length > 0
     ) {
//    document.write('<select name="videoList" onChange="selectView()" >');

    if ((1 == 1) &&
        !ext_sources &&
        (presetForm.elements.length > 0)
       ) {
//      document.write('<option class="gray">&nbsp;----------------</option>');
    } else {
      for (i = 0; i < 1; i++) {
        var camera_nice_nbr = i + 1;
        document.write('<a href="javascript:void(0)" onclick="selectView("' + imageType + '' + imageSource + '")">');
//        document.write('<option value="' + imageType + '' + imageSource + '"');
        if (imageType + '' + imageSource == '/mjpg/video.mjpg' ||
            '/jpg/image.jpg' == '/mjpg/video.mjpg') {
          document.write('<b>');
        }

        document.write(formInt['root_ImageSource_I' + i + '_Name'].value);
        if (imageType + '' + imageSource == '/mjpg/video.mjpg' ||
            '/jpg/image.jpg' == '/mjpg/video.mjpg') {
          document.write('</b>');
        }
        document.write('</a>&nbsp;&nbsp;');
      }
    }

    // ***** PTZ presets *****
    var curpreset = unescape('');
    var cntr = 0;
    for (i = 0; i < 1; i++) {
      if (presetForm.elements.length >= 2) {
        for (var j = 0; j < presetForm.elements.length; j++) {
          var tmpArray = presetForm.elements[j].name.split("_");
          if (tmpArray[tmpArray.length - 1] == "Label") {
	    var posStr = presetForm.elements[j-1].value;
	    var label = escape(presetForm[j].value);
	    var videoNbr = extractVideoNbr(posStr);
	    if (videoNbr-1 == i) {
	      //document.write('<option value="' + '/mjpg/video.mjpg' + '&camera=' + videoNbr + '&gotopresetname=' + label + '"');
	      cntr++;
	      document.write('<a href="javascript:void(0)" id="presLink' + cntr +'" style="font-weight:normal" onclick="selectPresLink(this);selectView(\'/mjpg/video.mjpg' + '&camera=' + videoNbr + '&gotopresetname=' + label + '\')"');
	      if (label == curpreset) {
//	        document.write(' selected');
            document.write('style="font-weight:bold"');
          }
	      document.write('>' + formInt['root_ImageSource_I' + [i] + '_Name'].value + '');
	      document.write('&nbsp;-&nbsp;' + unescape(label));
          document.write('</a>&nbsp;&nbsp;');
	    }
          }
        }
      }
    }

var viewAppletIE = "no";
var viewAppletOther = "no";
// Don't show external sources while using Java applet as Default Viewer. An unsigned applet doesn't have permission to access remote hosts.
if (((browser == "IE") && (viewAppletIE == "no")) || ((browser != "IE") && (viewAppletOther == "no"))) {
    if (formExt.elements.length > 0) {
      for (i = 0; i < formExt.elements.length; i += 3) {
        var aTempString = formExt.elements[i].name
        var videoNr = aTempString.substring(20, aTempString.lastIndexOf('_'))
        var external = escape(formExt['root_ExternalVideo_E' + videoNr + '_ImagePath'].value);

//          document.write('<option value="' + external + '"');
          document.write('<a href="javascript:void(0)" onclick="selectView("' + external + '")">');
          if (formExt['root_ExternalVideo_E' + videoNr + '_ImagePath'].value == '/mjpg/video.mjpg') {
//            document.write(' selected');
              document.write('<b>');
          }
          document.write(formExt['root_ExternalVideo_E' + videoNr + '_Name'].value);
          if (formExt['root_ExternalVideo_E' + videoNr + '_ImagePath'].value == '/mjpg/video.mjpg') {
              document.write('</b>');
          }
          document.write('</a>&nbsp;&nbsp;');
      }
    }
}

//    document.write('</select>');
//} else {
//  document.write(formInt.root_ImageSource_I0_Name.value);
  }
}

function zoom(size)
{
  var url = document.URL;

  if (url.indexOf("?") == -1) {
    url += "?size=" + size
  } else if (url.indexOf("size=") == -1) {
    url += "&size=" + size
  } else {
    var searchStr = "size=1"
    var replaceStr = "size=" + size
    var re = new RegExp(searchStr , "g")
    url = url.replace(re, replaceStr)
  }

  document.location = url;
}

var aNewImagePath;

function reloadPage()
{
  document.location = aNewImagePath;
}

function selectView(val)
{
  var form = document.WizardForm;
  var source = val;//form.videoList.options[form.videoList.selectedIndex].value;
  var params = source.split('&');
  var newCamera = null;
  var presetName = null;

  if ((params[1] != null) && ("no" == "no")) {
    newCamera = (params[1].split('='))[1];
    presetName = unescape((params[2].split('='))[1]);
    changeView(params[0], newCamera, presetName);
  } else {
    changeView(source, newCamera, presetName);
  }
}

function changeView(imagePath)
{
  var args = changeView.arguments;
  var newCamera  = (args.length >= 2 ? args[1] : null);
  var presetName = (args.length >= 3 ? args[2] : null);
  var newSize    = (args.length >= 4 ? args[3] : null);
  var other      = (args.length >= 5 ? args[4] : null);

  var reload = false;
  // the whole Live View design is built on page reload >-(
  // must reload initial sequence mode view to toggle buttons etc
  reload = (other != null && other.search("seq=yes"));
  var delay = 0;

  reload |= (imagePath != '/mjpg/video.mjpg');
  if (presetName != null) {
    var prevCamera = '1';
    // set new image path
    if (prevCamera == 'quad') {
      imagePath = imagePath.replace(/quad/, newCamera);
      reload = true;
    } else if (prevCamera != newCamera) {
      imagePath = imagePath.replace(prevCamera, newCamera);
      reload = true;
    }
    gotoPreset(newCamera, presetName);
    if (reload) {
      // avoid request being suspended due to page reload
      delay = 100;
      /* append preset parameters so that preset postion is selected in drop
       * down list after reload */
      imagePath += '&gotopresetname=' + escape(presetName);
    }
  }
  if (reload) {
    var size = newSize;

    if (size == null) {
      size = 1;
    }
    if (size != null) {
      size = '&size=' + size;
    }
    aNewImagePath = SERVER_PATH + '/view/view.shtml?imagePath=' + SERVER_PATH + imagePath + size;
    if (other != null)
      aNewImagePath += other;
    setTimeout("reloadPage()", delay);
  }
  else if (other != null) {
    //selectSource(newCamera, presetName);    temp decision
  }
}

function gotoPreset(camera, presetName)
{
  var form = document.gotopresetForm;

  form.camera.value = camera;
  form.gotoserverpresetname.value = presetName;
  form.submit();
}

function selectSource(newCamera, presetName) {
  var options = document.WizardForm.videoList.options;
  var i = 0;
  var option;

  while ((option = options[i++]) != null) {
    if (presetName != null) {
      var presetStart = option.value.search('gotopresetname');
      if (presetStart != -1) {
        var preset = option.value.substring(presetStart).split('=');
        if (unescape(preset[1]) == presetName) {
          option.selected = true;
          return;
        }
      }
    } else {
      var videoNbr = option.value.match(/\d/);
      if (videoNbr == null && newCamera == 1 || videoNbr == newCamera) {
        option.selected = true;
        return;
      }
    }
  }
}

var seqNext = null;

function doSequence(size) {
  var formSeq = document.listFormSeq;
  var formExt = document.listFormExt;
  var presetForm = document.listFormPreset;
  var seqSources = new Array();
  var seqNumbers = new Array();
  var seqTimes = new Array();
  var cameraNbr;
  var presetName = null;
  var imagePath;
  var next;

  for (var i = 0; i < formSeq['root_Sequence_S0_NbrOfSources'].value; i++) {
    seqSources[i] = formSeq['root_Sequence_S0_Source_S' + i + '_Type'].value;
    seqNumbers[i] = formSeq['root_Sequence_S0_Source_S' + i + '_Number'].value;
    seqTimes[i] = formSeq['root_Sequence_S0_Source_S' + i + '_Time'].value;
  }

  if (seqNext != null) {
    next = seqNext;
  } else {
    next = 0;
  }

  imagePath = "";
  if (seqSources[next] == 'Ext') {
    imagePath = formExt['root_ExternalVideo_E' + seqNumbers[next] + '_ImagePath'].value;
  }
  else if (seqSources[next] == 'Preset') {
    cameraNbr = extractVideoNbr(presetForm['root_PTZ_PresetPos_P' + seqNumbers[next] + '_Pos'].value);
    presetName = presetForm['root_PTZ_PresetPos_P' + seqNumbers[next] + '_Label'].value;
  }
  else if (seqSources[next] == 'Quad') {
    cameraNbr = 'quad';
  } else {
    cameraNbr = parseInt(seqNumbers[next]) + 1;
  }

  if (imagePath == "") {
      imagePath = '/mjpg/' + cameraNbr + '/video.mjpg';
  }

  var nextNext;
  if (formSeq['root_Sequence_S0_RandomEnabled'].value == "yes")
    nextNext = Math.floor(Math.random() * formSeq['root_Sequence_S0_NbrOfSources'].value);
  else
    nextNext = next + 1;
  if (nextNext >= formSeq['root_Sequence_S0_NbrOfSources'].value)
    nextNext = 0;

  seqNext = nextNext;
    changeView(imagePath, cameraNbr, presetName, size, '&seq=yes&next=' + nextNext + '&sequencetime=' + seqTimes[next]);

    t1 = setTimeout("doSequence(size)", 1000 * seqTimes[next]);
}


function snapshot(imagepath)
{
  var no = document.WizardForm.amount.value++
  var picturepath = imagepath.replace(/video/, "image")
  if (imagepath.indexOf("mpeg4") != -1)
  {
    picturepath = picturepath.replace(/rtsp/, "http")
    picturepath = picturepath.replace(/mpeg4/g, "jpg")
    picturepath = picturepath.replace(/media.amp/g, "image.jpg")
  } else {
    picturepath = picturepath.replace(/mjpg/g, "jpg")
  }
  var page = SERVER_PATH + '/view/snapshot.shtml?picturepath=' + picturepath
  var time = new Date()
  var timestamp = time.getTime()

  page += '&timestamp=' + timestamp + '&width=' + 704
  openPopUp(page, 'Take_snapshot' + [no] + '', 704 + 45, 576 + 75 )
}

var updateId;
var countId;

function ctlReq(form)
{
  switch(form.control.value)
  {
    case "request":
      handleCtlReq(form);
      break;
    case "drop":
      handleCtlDrop(form);
      break;
  }
}

function handleCtlReq(form)
{
  window.clearTimeout(updateId);
  form.submit();
  // restart timeout to be sure update will be done in time.
  updateId = window.setTimeout("updateStatus()", 2000);
}

function handleCtlDrop(form)
{
  window.clearInterval(countId);
  window.clearTimeout(updateId);
  form.submit();
  // restart - but wait a while to be sure the above form is sent.
  updateId = window.setTimeout("ctlStart()", 500);
}

function ctlStart()
{
  document.periodicForm.submit();
  // delay to allow response to arrive
  updateId = window.setTimeout("updateStatus()", 2000);
  startCountDown();
}

function ctlRepeat()
{
  document.periodicForm.submit();
  // delay to allow response to arrive
  updateId = window.setTimeout("updateStatus()", 1000);
}

function ctlStop()
{
  // reset label + action
  document.ctlreqForm.label.value="Request control";
  document.ctlreqForm.control.value="request";
  // stop timers
  window.clearInterval(countId);
  window.clearTimeout(updateId);
}

function updateStatus()
{
  var form = document.ctlstatusform;
  var msgDoc;

  if (parent.frames[1] && parent.frames[1].frames[1]) {   // parent.frames[1] doesn't exist
    msgDoc = parent.frames[1].frames[1].document;
  }
  else {
    // wait another 500 ms on the status response
    updateId = window.setTimeout("updateStatus()", 500);
    return;
  }

  if (msgDoc.anchors.length > 0)
  {
    var pos       = msgDoc.anchors[0].name;
    var secs      = msgDoc.anchors[1].name;
    var minPeriod = msgDoc.anchors[2].name;

    if (secs == "-1")
    {
      form.tleft.value = "Unlimited";
    }
    else
    {
      form.tleft.value = secs;
    }

    if (pos == "0")
    {
      document.ctlreqForm.label.value = "Request control";
      document.ctlreqForm.control.value="request";
      form.pos.value = "";
      if (secs == "0")
      {
        form.status.value = "No entry in queue with higher priority than yours.";
      }
      else
      {
        form.status.value = "Queue contains entry(s) with higher priority than yours.";
      }
    }
    else
    {
      form.pos.value = pos;
      document.ctlreqForm.control.value = "drop";
      if (pos == "1")
      {
        form.status.value = "You are possessing the control.";
        document.ctlreqForm.label.value = "Release control";
      }
      else
      {
        form.status.value = "You are in queue, please wait for your turn.";
        document.ctlreqForm.label.value = "Leave queue";
      }
    }

    if (minPeriod == 0)
    {
      updateId = window.setTimeout("ctlRepeat()", 15 * 1000);
    }
    else
    {
      updateId = window.setTimeout("ctlRepeat()", minPeriod * 1000);
    }
  }
  else if (msgDoc.title != "")
  {
    // some error has occurred
    form.status.value = msgDoc.title;
    form.pos.value = "";
    form.tleft.value = "";
    ctlStop();
  }
  else
  {
    // wait another 500 ms on the status response
    updateId = window.setTimeout("updateStatus()", 500);
  }
}

function startCountDown()
{
  countId = window.setInterval("countDown()", 1000);
}

function countDown()
{
  var form = document.ctlstatusform;
  if (form.tleft.value != "")
  {
    if (form.tleft.value > 0)
    {
      form.tleft.value--;
    }
  }
}
function movepic(img_name, img_src)
{
  document[img_name].src = img_src;
}

function auto(Path)
{
//  parent.frames[1].location = Path
    location = Path;
}

function AbsoluteOrRelative(form)
{
  var url = document.URL;
  if (form.AbsOrRel.selectedIndex == 0)
    var relative = "no";
  else
    var relative = "yes_no_cross";
  if (url.indexOf("?") == -1) {
    url += "?relative=" + relative;
  } else if (url.indexOf("relative=") == -1) {
    url += "&relative=" + relative;
  } else {
    var searchStr = "relative=";
    var replaceStr = "relative=" + relative;
    var re = new RegExp(searchStr , "g");
    url = url.replace(re, replaceStr);
  }
  document.location = url;
}

function video(imagepath)
{
  var resolution = "704x576"
  var width = 704
  var height = 576

  if (imagepath.indexOf("resolution=") != -1) {
    var resStart = imagepath.indexOf("resolution=")
    var resStop = imagepath.indexOf("&", resStart)
    if (resStop == -1) resStop = imagepath.length
    resolution = imagepath.substring(resStart + 11, resStop)
    width = parseInt(resolution.substring(0, resolution.indexOf('x')))
    height = parseInt(resolution.slice((resolution.indexOf('x') + 1)))
  }

  if ((imagepath.indexOf("rotation=90") != -1) || (imagepath.indexOf("rotation=270") != -1)) {
    var aTempWidth = width
    width = height
    height = aTempWidth
    resolution = width + 'x' + height
  }

  if ('1' != '1') {
    width = width * 1
    height = height * 1
  }
  var width_height = 'width="' + width + '" height="' + height + '"';
  var use_activex = 0;
  var use_java = 0;
  var use_spush = 0;
  var use_flash = 0;
  var use_still = 0;
  var viewer = "still";
  var use_quicktime = 0;
  if ((navigator.appName == "Microsoft Internet Explorer") && (navigator.platform != "MacPPC") && (navigator.platform != "Mac68k")) {
    viewer = "activex";
  } else {
    viewer = "spush";
  }

  if (viewer.indexOf("activex") != -1) {
    use_activex = 1;
  }
  if (viewer.indexOf("spush") != -1) {
    use_spush = 1;
  }
  if (viewer.indexOf("flash") != -1) {
    use_flash = 1;
  }
  if (viewer.indexOf("quicktime") != -1) {
    use_quicktime = 1;
  }
  if (imagepath.indexOf("mpeg4") != -1) {
    if ((navigator.appName == "Microsoft Internet Explorer") && (navigator.platform != "MacPPC") && (navigator.platform != "Mac68k")) {
        use_quicktime = 0;
        use_activex = 1;
    } else {
      use_quicktime = 1;
      use_spush = 0;
      use_still = 0;
    }
  } else {
    if ((navigator.appName == "Microsoft Internet Explorer") && (navigator.platform != "MacPPC") && (navigator.platform != "Mac68k")) {
        use_quicktime = 0;
        use_activex = 1;
    } else {
      use_quicktime = 0;
      use_spush = 1;
      use_still = 0;
    }
  }
  if (viewer.indexOf("java") != -1) {
    if (imagepath.indexOf("mpeg4") == -1) {
      use_java = 1;
      use_activex = 0;
      use_spush = 0;
      use_flash = 0;
      use_still = 0;
      use_quicktime = 0;
    }
  }
  if ((viewer.indexOf("still") != -1) || !(use_activex || use_spush || use_flash || use_java || use_quicktime)) {
    if (imagepath.indexOf("mpeg4") == -1) {
      use_still = 1;
      use_quicktime = 0;
      use_spush = 0;
      use_activex = 0;
    } else {
      if ((navigator.appName == "Microsoft Internet Explorer") && (navigator.platform != "MacPPC") && (navigator.platform != "Mac68k")) {
        use_activex = 1;
      } else {
        use_quicktime = 1;
      }
    }
  }
  if (navigator.userAgent.indexOf("Safari") != -1) {
    if (use_spush) {
      use_java = 1;
      use_spush = 0;
    }
  }

  if (use_activex) {
    height = height + 54
    var notAuthorizedText = "The installation of the MPEG-4 Decoder has been disabled. Contact the Administrator of this AXIS 214.";
    var authorizedText = "Click here to install or upgrade the MPEG-4 Decoder.";
    var installDecoderText1 = "<b>MPEG-4 Decoder</b>, which enables streaming video in Microsoft Internet Explorer, has not been installed or could not be registered on this computer.";

    var installDecoderText2 = "To <a href=\"javascript:launch('http://web.ideal-house.org/incl/license.shtml')\">install or upgrade</a> the MPEG-4 Decoder, you must have Administration rights on this computer and you must answer Yes <br>when asked if you wish to allow the installation. AXIS 214 can also be configured to show still images.";

    var installText1 = "which enables streaming";
    var videoText = "video";
    var audioText = "audio";
    var installText2 = "in Microsoft Internet Explorer, has not been installed or could not be registered on this computer.";
    var installText3 = "To install or upgrade the";
    var installText4 = ", you must have Administration rights on this computer and you must answer Yes <br>when asked if you wish to allow the installation. AXIS 214 can also be configured to show still images.";
    DrawAMC1("AXIS 214", "AXIS Media Control", height, width, imagepath, "DE625294-70E6-45ED-B895-CFFA13AEB044", "AMC.cab", "3,32,19,0", "yes", "yes", "yes", "1", "no", "yes", "no", "yes", "554", "no", installText1, videoText, installText2, installText3, installText4, "0");

    if (imagepath.indexOf("mpeg4") != -1) {
      InstallDecoder1("AXIS 214", "MPEG-4 Decoder", "32c11e38-e587-4be9-9abb-d69158c21ce5", "NotFound.cab", "1,2,53,50725", "no", notAuthorizedText, authorizedText, installDecoderText1, installDecoderText2);
      InstallFilter1("AXIS 214", "MPEG RTP Reader", "67B1A88A-B5D2-48B1-BF93-EB74D6FCB077", "1,4,13,0", "AMC.cab", installText1, videoText, installText2, installText3, installText4);
      InstallFilter1("AXIS 214", "Image Notify Component", "0173EEF5-1FDE-479C-9F24-34C3CB0B3243", "1,2,0,0", "AMC.cab", installText1, videoText, installText2, installText3, installText4);
    }

    InstallFilter1("AXIS 214", "AudioComponent", "C111A91F-D4EC-4D22-8D27-C3BCB0389F43", "1,11,5,0", "AMC.cab", installText1, audioText, installText2, installText3, installText4);

    document.write("<br>");
  }

  if (use_spush) {
      var output = '<A HREF="http://web.ideal-house.org/axis-cgi/com/ptz.cgi?imagewidth=704&imageheight=576&camera=1&center=" TARGET="Temp">';
      output += '<img SRC="' + imagepath + '"'+ width_height ;
      var view_PressReloadTxt = "Press Reload if no image is displayed"
      output += ' ALT="' + view_PressReloadTxt + '" BORDER="0" ISMAP></A>';
    output += '<br>';
    document.write(output);
  }

  if (use_quicktime) {
    var DisplayWidth = width;
    var DisplayHeight = height  + 16;
    if (imagepath.indexOf("://") == -1)
      var MediaURL = "rtsp://" + location.hostname + imagepath
    else
      var MediaURL = imagepath
    var output = "";
    output  = '<OBJECT CLASSID="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" width=' + DisplayWidth + ' height=' + DisplayHeight + ' CODEBASE="http://www.apple.com/qtactivex/qtplugin.cab">';
    output += '<param name="src" value="AxisMoviePoster.mov">';
    output += '<param name="autoplay" value="true">';
    output += '<param name="controller" value="true">';
    output += '<param name="qtsrc" value="' + MediaURL + '">';
    output += '<embed src="AxisMoviePoster.mov" width=' + DisplayWidth + ' height=' + DisplayHeight + ' qtsrc="' + MediaURL + '" autoplay="true" controller="true" target="myself" PLUGINSPAGE="http://www.apple.com/quicktime/download/"></embed>';
    output += '</OBJECT>';
    document.write(output);
  }

  if (use_flash) {
    var view_NeedFlashPluginTxt = "You need a Shockwave Flash plugin, get it from:"
    document.write('<EMBED src="/axis-cgi/mjpg/video.swf?resolution=' + resolution +'&camera=1" ' +
    'quality=high bgcolor=#000000 ' + width_height +
    ' TYPE="application/x-shockwave-flash" swLiveConnect=TRUE' +
    ' PLUGINSPAGE="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash">' +
    '</EMBED>' +
    '<NOEMBED>' + view_NeedFlashPluginTxt +
    '<a href="http://www.macromedia.com/shockwave/download/">Macromedia</a>.' +
    '</NOEMBED><br>');
  }

  if (use_java) {
    var playerWidth = 704
    if (("yes" == "yes") || ("yes" == "yes")) {
      if (("yes" == "yes") && ("yes" == "yes")) {
        if (playerWidth < 431)
          playerWidth = 431
      } else {
        if (playerWidth < 288)
          playerWidth = 288
      }
    }
      var playerHeight = 576 + 85
    if (imagepath.indexOf("http") != -1) {
      var addrEnd = imagepath.indexOf("/", 8)
      var addr = imagepath.substring(0, addrEnd)
    } else {
      var addr = ""
    }
    document.write('<APPLET archive="ama.jar" codeBase="/java" code="AxisMediaApplet" width="' + playerWidth + '" height="' + playerHeight + '">');
    document.write('<PARAM NAME="code" VALUE="AxisMediaApplet">');
    document.write('<PARAM NAME="archive" VALUE="ama.jar">');
    document.write('<PARAM NAME="codebase" VALUE="/java">');
    document.write('<PARAM NAME="cache_archive" VALUE="ama.jar, ama-audio.jar, ama-ptz.jar">');
    document.write('<PARAM NAME="cache_version" VALUE="0.12.0.0, 0.12.0.0, 0.12.0.0">');
    document.write('<PARAM NAME="type" VALUE="application/x-java-applet;version=1.4">');
    document.write('<PARAM NAME="ama_url" VALUE="' + addr + '/axis-cgi/mjpg/video.cgi?camera=1&resolution=704x576">');
    document.write('<PARAM NAME="ama_audio" VALUE="yes">');
    if ("no" == "no")
      document.write('<PARAM NAME="ama_ptz" VALUE="absolute">');
    else
      document.write('<PARAM NAME="ama_ptz" VALUE="relative">');
    document.write('<PARAM NAME="ama_ptz-cross" VALUE="yes">');
    document.write("</APPLET><br>");
  }


  if (use_still) {
    var picturepath = imagepath.replace(/video/, "image")
    picturepath = picturepath.replace(/mjpg/g, "jpg")
    picturepath = picturepath.replace(/rtsp/, "http")
    picturepath = picturepath.replace(/mpeg4/g, "jpg")
    picturepath = picturepath.replace(/media.amp/g, "image.jpg")

    document.write('<img SRC="' + picturepath + '" border=0 ' + width_height +'><br>');
  }
}

function goto_camera(cam)
{
  cam = '';

  var imagePath;
    imagePath = '/mjpg/' + cam + 'video.mjpg';
  changeView(imagePath);
}

