function showCam(user) {
  window.open('showcam.php?user='+user,'Webcam'+user,'toolbar=no,status=no,menubar=no,width=328,height=310,resizeable=1');
}

function showMessageBox(item, typ) {
  window.open('messagebox.php?item='+item+'&typ='+typ,'Messagebox'+item,'scrollbars=yes,toolbar=no,status=no,menubar=no,width=520,height=300,resizeable=1');
}

function showSnapshot(user) {
  window.open('snapshot.php?user='+user,'','toolbar=no,status=no,menubar=no,width=520,height=240,resizeable=1');
}

function showClient() {
  window.open('webclient/','','toolbar=no,status=no,menubar=no,width=620,height=360,resizeable=1');
}

var sending = false;
var http_request = false;
var status = 0;

function createReq() {
  http_request = false;

  if (window.XMLHttpRequest) { // Mozilla, Safari,...
    http_request = new XMLHttpRequest();
    if (http_request.overrideMimeType) {
      http_request.overrideMimeType('text/plain;charset=iso-8859-1');
    }
  } else if (window.ActiveXObject) { // IE
    try {
      http_request = new ActiveXObject("Msxml2.XMLHTTP");
  http_request.setRequestHeader("Content-Type", "text/html; charset=iso-8859-1");
    } catch (e) {
      try {
        http_request = new ActiveXObject("Microsoft.XMLHTTP");
  http_request.setRequestHeader("Content-Type", "text/html; charset=iso-8859-1");
      } catch (e) {}
    }
  }
  if (!http_request) {

    return false;
  }
}

// Div-Layer bei versch. Browsern mit Zugriff auf style
function div_ref_style (id) {
  if      (document.layers)         return document.layers[id];
  else if (document.all)            return document.all[id].style;
  else if (document.getElementById) return document.getElementById(id).style;
  else                              return null;
}
// Div-Layer bei versch. Browsern
function div_ref (id) {
  if      (document.layers)         return document.layers[id];         // NC 4
  else if (document.all)            return document.all[id];            // IE 4
  else if (document.getElementById) return document.getElementById(id); // DOM
  else                              return null;
}

function makeFav(user, fav)
{
  if (sending==false)
  {
    sending = true;
    setTimeout('sending = false;', 10000);

    document.getElementById("favcode"+user).innerHTML = '<img src="img/icon_loading.gif" class="icon">';

    createReq();
    http_request.open('GET', 'ajax_makefav.php?user='+user+'&fav='+fav, true);
    http_request.setRequestHeader('Content-Type', 'text/xml');
    http_request.onreadystatechange = done_makeFav;
    http_request.send(null);

  }
}

function done_makeFav()
{
  if(http_request.readyState == 4)
  {
    var response = http_request.responseText;

    var erg = response.split('|');

    sending = false;

    if (erg[0]=="1")
    {
      document.getElementById("favcode"+erg[1]).innerHTML = erg[2];
    }

  }
}


function addToGal(id, isingal)
{
  if (sending==false)
  {
    sending = true;
    setTimeout('sending = false;', 10000);

    document.getElementById("addtogal"+id).innerHTML = '<img src="img/icon_loading.gif" class="icon">';

    createReq();
    http_request.open('GET', 'ajax_addtogal.php?id='+id+'&isingal='+isingal, true);
    http_request.setRequestHeader('Content-Type', 'text/xml');
    http_request.onreadystatechange = done_addToGal;
    http_request.send(null);

  }
}

function done_addToGal()
{
  if(http_request.readyState == 4)
  {
    var response = http_request.responseText;

    var erg = response.split('|');

    sending = false;

    if (erg[0]=="1")
    {
      document.getElementById("addtogal"+erg[1]).innerHTML = erg[2];
    }

  }
}

function closeGalPic()
{
  div_ref_style("galPicOuter").visibility = 'hidden';
  div_ref_style("darkBackgroundLayer").visibility = 'hidden';
}

function openGalPic(id, user)
{
  div_ref_style("darkBackgroundLayer").visibility = 'visible';
  div_ref_style("galPicOuter").visibility = 'visible';
  div_ref_style("galPicInner").textAlign = 'center';
  div_ref_style("galPicInner").backgroundColor = 'white';
  div_ref_style("galPicInner").padding = '10px';
  div_ref("galPicInner").innerHTML = '<img src="img/loading.gif">';
  
  createReq();
  http_request.open('GET', 'ajax_viewgal.php?id='+id+'&user='+user, true);
  http_request.setRequestHeader('Content-Type', 'text/xml');
  http_request.onreadystatechange = done_openGalPic;
  http_request.send(null);
}

function done_openGalPic()
{
  if(http_request.readyState == 4)
  {
    var response = http_request.responseText;
    sending = false;

    div_ref_style("galPicInner").textAlign = 'left';
    div_ref_style("galPicInner").backgroundColor = 'transparent';
    div_ref_style("galPicInner").padding = '5px';
    div_ref("galPicInner").innerHTML = response;
    

    Behaviour.apply();

  }
}


function updateCamList(camcount)
{
  if (sending==false)
  {
    sending = true;
    setTimeout('sending = false;', 10000);

    createReq();
    http_request.open('GET', 'ajax_camlist.php?c='+camcount, true);
    http_request.setRequestHeader('Content-Type', 'text/xml');
    http_request.onreadystatechange = done_updateCamList;
    http_request.send(null);

  }
}

function done_updateCamList()
{
  if(http_request.readyState == 4)
  {
    var response = http_request.responseText;

    var erg = response.split('|');

    sending = false;

    if (erg[0]=="1")
    {
      document.getElementById("camsshown").innerHTML = erg[1];
      document.getElementById("camsonline").innerHTML = erg[2];
      document.getElementById("camlist").innerHTML = erg[3];
    }

  }
}

function reloadInfo(count)
{
  updateCamList(count);
  setTimeout("reloadInfo("+count+")", 30000);
}

function searchLive()
{
  var s_username = document.forms["search"]["f_username"].value;
  var s_sex0 = document.forms["search"]["f_sex0"].checked;
  var s_sex1 = document.forms["search"]["f_sex1"].checked;
  var s_sex2 = document.forms["search"]["f_sex2"].checked;
  var s_pic1 = document.forms["search"]["s_pic1"].checked;
  var s_pic2 = document.forms["search"]["s_pic2"].checked;
  document.getElementById("findmore").href =
    "search.php?action=search&f_username="+s_username+"&f_sex0="+s_sex0+"&f_sex1="+s_sex1+"&f_sex2="+s_sex2+"&f_pic1="+s_pic1+"&f_pic2="+s_pic2;
    
  if (s_username.length>=0)
  {
    createReq();
    http_request.open('GET', "ajax_search.php?username="+s_username+"&sex0="+s_sex0+"&sex1="+s_sex1+"&sex2="+s_sex2+"&pic1="+s_pic1+"&pic2="+s_pic2, true);
    http_request.setRequestHeader('Content-Type', 'text/xml');
    http_request.onreadystatechange = done_searchLive;
    http_request.send(null);
  }

}

function done_searchLive()
{
  if(http_request.readyState == 4)
  {
    var response = http_request.responseText;
    var erg = response.split('|');

    sending = false;

    if (erg[0]=="1")
    {
      document.getElementById("search_hits").innerHTML = erg[1]+" Treffer";
      if (erg[1]>10)
        document.getElementById("findmore").style.visibility = "visible"
      else
        document.getElementById("findmore").style.visibility = "hidden";
        
      document.getElementById("search_results").innerHTML = "";
      
      for (var i = 2; i < erg.length-1; i++)
      {
        var userdata = erg[i].split("%");
        document.getElementById("search_results").innerHTML +=
          '<div style="height:40px;width:300px;"><a href="javascript:showCam('+userdata[0]+')" style="float:left;"><img src="gettinycampic.php?user='+userdata[0]+'" class="tinycampic" style="margin-right:3px;"></a>'+
          userdata[1]+"<br>"+
          '<span class="smalltext">'+userdata[2]+"</span></div>";
      }

    }

  }
}







