var timestatist;
var timest;

function checkform(f) {
  var errMSG = "";
  // цикл ниже перебирает все элементы в объекте f, 
  // переданном в качестве параметра
  // функции, в данном случае - наша форма.            
  for (var i = 0; i<f.elements.length; i++) 
    // если текущий элемент имеет атрибут required
    // т.е. обязательный для заполнения
    if (null!=f.elements[i].getAttribute("required")) 
       // проверяем, заполнен ли он в форме
//            alert(f.elements[i].name);       
        if (isEmpty(f.elements[i].value)) // пустой
            errMSG += "  " + f.elements[i].name + "\n"; // формируем сообщение
                                                       // об ошибке, перечисляя 
                                                       // незаполненные поля
        // если сообщение об ошибке не пусто,
        // выводим его, и возвращаем false     
        if ("" != errMSG) {
            alert("Не заполнены обязательные поля:\n");
            return false;
        }
}

function isEmpty(str) {
   for (var i = 0; i < str.length; i++)
      if (" " != str.charAt(i))
          return false;
      return true;
}

function getXmlHttp(){
  var xmlhttp;
  try {
    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
  } catch (e) {
    try {
      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (E) {
      xmlhttp = false;
    }
  }
  if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
    xmlhttp = new XMLHttpRequest();
  }
  return xmlhttp;
}


function testiruemcameru(id, url)
{
  document.getElementById('rec_status'+id).innerHTML = 'Test';
 var xmlhttp = getXmlHttp();
 var current_date = new Date();
 xmlhttp.open('GET', 'js/cam_2_send_comand.php', true);
 
    xmlhttp.onreadystatechange = function() {
   if (xmlhttp.readyState == 4)
   {
    if(xmlhttp.status == 200)
    {
    document.getElementById('rec_status'+id).innerHTML = xmlhttp.responseText;
    setTimeout('document.getElementById(\'rec_status'+id+'\').innerHTML = \'\'', 1000);
    }
   }
  } 
 xmlhttp.send(null);
// tessst.innerHTML = current_date; rec_status

}

function select_cam_func(id_cam)
{
  document.getElementById('cam_func').innerHTML = 'Загрузка...';
  var xmlhttp = getXmlHttp();   
  var current_date = new Date();
  var id = document.getElementById('id_camm').options[document.getElementById('id_camm').options.selectedIndex].value;
   xmlhttp.open('GET', 'cat_funca.php?id='+id+'&id_cam='+id_cam, true);
   xmlhttp.onreadystatechange = function() {
   if (xmlhttp.readyState == 4)
   {
    if(xmlhttp.status == 200)
    {
    document.getElementById('cam_func').innerHTML = xmlhttp.responseText;
    }
   }
  }

//  var myAjax = new Ajax.Request('cat_func.php', {method: 'get', parameters: 'id='+id, onComplete: shownewcatfunc});
   xmlhttp.send(null);
}

function shownewcatfunc(originalRequest)
{
  document.getElementById('cam_func').innerHTML = originalRequest.responseText;
}



function about (id)
{ if(document.getElementById('about'+id).style.display == '')
 {
 document.getElementById('about'+id).style.display = 'none';
 }
 else
 { document.getElementById('about'+id).style.display = '';
 }
}

function add_user_zakaz ()
{
 document.getElementById('in').value = 'edit_members';
 document.getElementById('peredacha').value = '1';
 document.zakaz.submit();
}
function add_user_tovar ()
{
 document.getElementById('in').value = 'edit_tovar';
 document.getElementById('peredacha').value = '1';
 document.zakaz.submit();
}

function top_menu_on (id)
{
 document.getElementById('top_1'+id).src = 'images/top_1_a.jpg';
 document.getElementById('top_pix'+id).background = 'images/top_pix_a.jpg';
 document.getElementById('top_2'+id).src = 'images/top_2_a.jpg';

}
function top_menu_off (id)
{
 document.getElementById('top_1'+id).src = 'images/top_1.jpg';
 document.getElementById('top_pix'+id).background = 'images/top_pix.jpg';
 document.getElementById('top_2'+id).src = 'images/top_2.jpg';

}
function new_demo_setting ()
{
   var xmlhttp = getXmlHttp();
   var current_date = new Date();
   xmlhttp.open('GET', 'saveData.php?V_CBR='+document.getElementById('V_CBR').options[document.getElementById('V_CBR').options.selectedIndex].value+'&V_Rs='+document.getElementById('V_Rs').options[document.getElementById('V_Rs').options.selectedIndex].value+'&date='+current_date, true);
   xmlhttp.send(null);

//document.getElementById('region').options[document.getElementById('region').options.selectedIndex].value          background
}


function ch_status_pult(id)
{ if(document.getElementById('pultstatus'+id).value == '0')
 {
 document.getElementById('pultstatus'+id).value = '1';
 document.getElementById('pult'+id).style.display = '';
 }
 else
 {
 document.getElementById('pultstatus'+id).value = '0';
 document.getElementById('pult'+id).style.display = 'none';
 }
}

function scrollfoot(me)
{
  for(i=1;i<=150;i++)
  {
  document.getElementById('scrollfoot').scrollLeft = eval(''+document.getElementById('scrollfoot').scrollLeft+' '+me+' 1');
    }
}

function img_on(id, id_div, pix, action, actionn)
{
  document.getElementById(id_div).innerHTML = "<img onclick=\"img_off('"+id+"', '"+id_div+"', '"+pix+"', '"+action+"', '"+actionn+"');\" border=\"0\" src=\"images/pult/"+pix+"_3.jpg\" id=\""+pix+"\" width=\"142\" height=\"34\" >";
  setAction(action, id);
}
function img_off(id, id_div, pix, action, actionn)
{
  document.getElementById(id_div).innerHTML = "<img onclick=\"img_on('"+id+"', '"+id_div+"', '"+pix+"', '"+action+"', '"+actionn+"');\" border=\"0\" src=\"images/pult/"+pix+"_1.jpg\" id=\""+pix+"\" width=\"142\" height=\"34\" onMouseDown=\"img_pult('"+pix+"', '"+pix+"', 3)\" onMouseUp=\"img_pult('"+pix+"', '"+pix+"', 2)\" onMouseOver=\"img_pult('"+pix+"', '"+pix+"', 2)\" onMouseOut=\"img_pult('"+pix+"', '"+pix+"', 1)\">";
  setAction(action, id);
}


function img_pult(id, pix, metod)
{
if(document.getElementById(id)!=undefined)
{
document.getElementById(id).src = 'images/pult/'+pix+'_'+metod+'.jpg';
}
else
{
alert("Отсутствует - "+id);   
}
}

function opr_wh()
{var oknoWidth=800; var oknoHeight=640;
if (self.innerWidth)
    oknoWidth = self.innerWidth;
else if (document.documentElement && document.documentElement.clientWidth)
    oknoWidth = document.documentElement.clientWidth;
else if (document.body)
    oknoWidth = document.body.clientWidth;

    if (self.innerHeight)
    oknoHeight = self.innerHeight;
else if (document.documentElement && document.documentElement.clientHeight)
    oknoHeight = document.documentElement.clientHeight;
else if (document.body)
    oknoHeight = document.body.clientHeight;
var begushaya = document.getElementById('begushaya');
if(begushaya!=undefined)
{
begushaya.style.width = eval(oknoWidth+'-317');
}
var scrollfoot = document.getElementById('scrollfoot');
if(scrollfoot!=undefined)
{
scrollfoot.style.width = eval(oknoWidth+'-490');
}
}

function gettimest(id)
{ var xmlhttp = getXmlHttp();
 var current_date = new Date();
 xmlhttp.open('GET', 'cam_statist.php?id='+id+'&date='+current_date, true);
 xmlhttp.send(null);
// tessst.innerHTML = current_date;
 if(document.getElementById('butplaystatus'+id).value == '1')
 {
 timestatist = setTimeout('gettimest('+id+')', 10000);
 }
}



function clickbut(id, time, ii)
{
 if(document.getElementById('butplaystatus'+id).value == '1')
 {
 clearTimeout(timestatist);
 clearTimeout(timest);
 document.getElementById('butplaystatus'+id).value = '0';
 document.getElementById('start'+id).src = 'images/pult/start_'+ii+'.jpg';
 document.getElementById('startt'+id).src = 'images/pult/start_3.jpg';
 document.getElementById('Player'+id).Stop();

   var xmlhttp = getXmlHttp();
   var current_date = new Date();
   xmlhttp.open('GET', 'cam_statist.php?id='+id+'&date='+current_date, true);
   xmlhttp.send(null);

 }
 else
 {
 timestatist = setTimeout('gettimest('+id+')', 10000);
 document.getElementById('butplaystatus'+id).value = '1';
 document.getElementById('start'+id).src = 'images/pult/stop_'+ii+'.jpg';
 document.getElementById('startt'+id).src = 'images/pult/stop_3.jpg';
 document.getElementById('Player'+id).Play();
   var xmlhttp = getXmlHttp();
   var current_date = new Date();
   xmlhttp.open('GET', 'cam_statist.php?id='+id+'&date='+current_date, true);
   xmlhttp.send(null);

 if(time!=0)
  {
  timest = setTimeout('timestop('+id+', '+ii+')', time);
  }
 }
}

function timestop(id, ii)
{
 clearTimeout(timestatist);
 document.getElementById('butplaystatus'+id).value = '0';
 document.getElementById('start'+id).src = 'images/pult/start_'+ii+'.jpg';
 document.getElementById('startt'+id).src = 'images/pult/start_3.jpg';
 document.getElementById('Player'+id).Stop();
   var xmlhttp = getXmlHttp();
   var current_date = new Date();
   xmlhttp.open('GET', 'cam_statist.php?id='+id+'&date='+current_date, true);
   xmlhttp.send(null);
//   alert("Stop");

}

function Scale(id, size, s)
{ var newwidth = document.getElementById('Player'+id).width;
 var newheight = document.getElementById('Player'+id).height;
 if(size=='1')
 {
  if(document.getElementById('Player'+id).height<=560)
  {   
 newwidth = eval(''+newwidth+'+100');
 newheight = eval('('+newwidth+'*3)/4');
  } 
 }
 if(size=='0')
 {  if(document.getElementById('Player'+id).height>=200)
  {
 newwidth = eval(''+newwidth+'-100');
 newheight = eval('('+newwidth+'*3)/4');
  }
 }
 document.getElementById('Player'+id).width = newwidth;
 document.getElementById('Player'+id).height = newheight;
// document.getElementById('Player'+id).SetDisplayResolution(newwidth, newheight)
 if(s==1)
 {   var xmlhttp = getXmlHttp();
   var current_date = new Date();
   xmlhttp.open('GET', 'up_cam.php?id='+id+'&w='+newwidth+'+&h='+newheight+'+&date='+current_date, true);
   xmlhttp.send(null);
 }
}

function full(id)
{
  if (document.getElementById('ifull'+id).value==1)
  {
   document.body.scrollTop = 0;
   document.body.scrollLeft = 0;
//   document.body.scroll = "no";
   document.getElementById('Player'+id).FullScreen(1);
   document.getElementById('ifull'+id).value = 0;
  }
  else
  {
   document.getElementById('Player'+id).FullScreen(0);
   document.body.scroll = "yes";
   document.getElementById('ifull'+id).value = 1;
  }

}

function onLoad()
{
  var PTZTmp = "0";
  if (PTZTmp == 1){
    PTZProtocol = PROTOCOL_PELCO_D;
  }else{
    PTZProtocol = PROTOCOL_PELCO_P;
  }
  var BAUDRATETep="9600";
  if (BAUDRATETep == 2400){
    BAUDRATE = 2400;
  }else{
    BAUDRATE = 9600;
  }
  var address;
  var addressTmp=1;
  if (addressTmp != 0){
    address = addressTmp;
  }else{
    address = 0;
  }
  //address = parseInt(address);
  if ( address <= 0xf){
   address = "0" + address.toString(16);
  }
  AddrP = address.toString(16);
  AddrD = address.toString(16);
  //Set pan direction
  pan_dir = 0;
  if (pan_dir){
  swap("TiltUpP", "TiltDownP");
  swap("PanLeftP", "PanRightP");
  swap("UpD", "DownD");
  swap("LeftD", "RightD");
  }
}


function getcam (id, time, rotat, ii, start) {
   var xmlhttp = getXmlHttp();
   var current_date = new Date();
   xmlhttp.open('GET', 'getcam.php?id='+id+'&date='+current_date, true);
   xmlhttp.onreadystatechange = function() {
   if (xmlhttp.readyState == 4)
   {
    if(xmlhttp.status == 200)
    {
    document.getElementById('Player'+id).Source='http://'+xmlhttp.responseText+'/test.cmp';
    if(start==1)
    { 
    document.getElementById('Player'+id).Play();
    if(time!=0)
     {
      timest = setTimeout('timestop('+id+', '+ii+')', time);
     }
    timestatist = setTimeout('gettimest('+id+')', 10000);
    xmlhttp.open('GET', 'cam_statist.php?id='+id+'&tip=1&date='+current_date, true);
    xmlhttp.send(null);
    }
 if(rotat!=0)
  {
 document.getElementById('rever'+id).value=1
 document.getElementById('Player'+id).RotateImage(1)
  }


    }
   }
 }
xmlhttp.send(null);
}

function add_fav (id) {
   var xmlhttp = getXmlHttp();
   var current_date = new Date();
   xmlhttp.open('GET', 'add_fav.php?id='+id+'&date='+current_date, true);
   xmlhttp.onreadystatechange = function() {
   if (xmlhttp.readyState == 4)
   {
    if(xmlhttp.status == 200)
    {
    document.getElementById('fav'+id).innerHTML = xmlhttp.responseText+'<font color="green">Камера в избранном</font>';
    }
   }
 }
xmlhttp.send(null);
}


function reversal(id){
if(document.getElementById('rever'+id).value==0)
 {
 document.getElementById('rever'+id).value=1
 document.getElementById('Player'+id).RotateImage(1)
 }
 else
 {
 document.getElementById('rever'+id).value=0
 document.getElementById('Player'+id).RotateImage(0)
 }
}



