//<!--
// Module to open/close the network
// used in macchk.html in lang(ja/en) directory
var seconds;
var status;
var macaddr;
var httpObj;
var cginame;
var timeout;

// create object of XMLHttpRequest
function createXMLHttpRequest() {
  try { return new ActiveXObject("Msxml2.XMLHTTP");    } catch(e) {}
  try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {}
  try { return new XMLHttpRequest();                   } catch(e) {}
  return null;
}

// send AJAX request for status(=open/close)
// timer is started at open-request and stopped at close-request.
function ajaxRequest(status){
  if((httpObj=createXMLHttpRequest())!=null){
    try{  
      httpObj.open('post', cginame ,false);
      httpObj.setRequestHeader('content-type',
	'application/x-www-form-urlencoded,charset=utf-8');
      httpObj.send('status='+status+'&macaddr='+macaddr);
      if(status=='open') startTimer();
      else stopTimer();
    }catch(e){ }
  }
}

// send 'open' AJAX request
function openNet(){
  document.checkform.open.disabled=true;
  document.checkform.close.disabled=false;
  getMacAddr();
  document.regform.macaddr.value=macaddr;
  ajaxRequest('open');
}

// send 'close' AJAX request
function closeNet(){
  document.checkform.open.disabled=false;
  document.checkform.close.disabled=true;
  ajaxRequest('close');
}

// start timer
function startTimer(){
  seconds=timeout;
  setTimeout('onTimeout()', 1000);
}

// stop timer
function stopTimer(){
  seconds=0;
}

// decrement counter and show the value on page
// if the counter reachs zero, close-request is sent automatically
function onTimeout(){
  document.checkform.timer.value=seconds;
  seconds--;
  if(seconds<0) closeNet();
  else setTimeout('onTimeout()', 1000);
}

// get the checked mac address in the list 
function getMacAddr(){
  if(document.checkform.macaddr==undefined) macaddr='';
  else if(document.checkform.macaddr.length==undefined)
    macaddr=document.checkform.macaddr.value;
  else{
    for(var i=0; i<document.checkform.macaddr.length; i++){
      if(document.checkform.macaddr[i].checked){
        macaddr=document.checkform.macaddr[i].value;
      }
    }
  }
}

// set parameters at starting of page(<body onload=..>)
function setParameters(cginameArg, timeoutArg){
  cginame=cginameArg;
  timeout=timeoutArg;
}
//-->
