// JavaScript Document
// AJAX support
// by kubix

function xmlhttpPost(strURL,id,star) {
    var xmlHttpReq = false;
    var self = this;
    
    if (window.XMLHttpRequest) { // Mozilla/Safari
        self.xmlHttpReq = new XMLHttpRequest();
    } else if (window.ActiveXObject) { // IE
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }

    self.xmlHttpReq.open('POST', "/" + strURL, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
            updatepage(self.xmlHttpReq.responseText,id,strURL,star);
        }
    }
    
    if (strURL == "write_comment.fcgi") {
      self.xmlHttpReq.send(getquerystring());
    } else if (strURL == "send_link.cgi") {
      self.xmlHttpReq.send(getquerystring2());    
    } else if (strURL == "make_star.cgi") {      
      obj = eval("document.getElementById('mygame')");
      obj.innerHTML = "Refreshing container...";
      qstr = 'id=' + escape(id) + '&star=' + escape(star);  
      self.xmlHttpReq.send(qstr);

    } else {
      qstr = 'id=' + escape(id) + '&star=' + escape(star);
      self.xmlHttpReq.send(qstr);
    }
}

function getquerystring() {
    var form = document.comment;
    var name = form.name.value;
    var text = form.text.value;
    var id = form.id.value;
    if (text != "") {
      qstr = 'name=' + escape(name) + '&text=' + escape(text) + '&id=' + id;  // NOTE: no '?' before querystring
      text = "";    
    } else {
      qstr = 'id=' + id;  // NOTE: no '?' before querystring
    }
    return qstr;
}

function getquerystring2() {
    var form = document.forms['send'];
    var name = form.name.value;
    var email = form.email.value;
    var id = form.id.value;
    var email_to = form.email_to.value;
    var mess1 = form.mess1.value;    
    var mess2 = form.mess2.value;    
    if (email_to != "") {
      qstr = 'name=' + escape(name) + '&email=' + escape(email) + '&id=' + id + '&email_to=' + escape(email_to)  + '&mess1=' + escape(mess1)  + '&mess2=' + escape(mess2);  // NOTE: no '?' before querystring
    }
    return qstr;
}


function updatepage(str,id,strURL,star){
  var obj;
  if (strURL == "make_vote.cgi") {
    obj = eval("document.getElementById('rating" + id + "')");
    obj.innerHTML = str;
    doing = 0;

    obj_v = eval("document.getElementById('pom_vote" + id + "')");
    reRate(obj_v.value,id,1);    
    alert("Thanx for voting ! Rate: " + obj_v.value);
          
  } else if (strURL == "make_star.cgi") {
    obj = eval("document.getElementById('mygame')");
    obj.innerHTML = str;
    if ((id != "") && (star != "")) { updateRate(id,star) };
    
  } else if ((strURL == "select_video.cgi") && (!star)) {
    obj = document.getElementById("rss");
    obj.innerHTML = str;
    
  } else if ((strURL == "select_video.cgi") && (star)) {
    obj = document.getElementById("rss_ep");
    obj.innerHTML = "<br><h1>Video TV</h1>";
    obj.innerHTML = obj.innerHTML + str;
    obj.style.display = "block";
    obj.style.top = "1600px";
    obj.style.left = "400px";
    
    //obj = document.getElementById("rekVideo");
    //obj.innerHTML = "TU bude<br>VIDEO reklama<br>TU bude<br>VIDEO reklama<br>TU bude<br>VIDEO reklama<br>TU bude<br>VIDEO reklama<br>"
    
  } else if (strURL == "write_comment.fcgi") {
    document.getElementById("result").innerHTML = str;
    document.getElementById("comment").text.value = "";
    
  } else if (strURL == "send_link.cgi") {
    obj = document.getElementById("send_link");
    obj.innerHTML = str;
    
  } else if (strURL == "select_img.cgi") {
    obj = document.getElementById("img_preview");
    obj.innerHTML = str;

  } else if (strURL == "report_broken.cgi") {
    alert('Broken link sent. Thanx for reporting :)')
    
  } else if (strURL == "search_result.cgi") {
    obj = document.getElementById("search_result");
    obj.innerHTML = str; 
    obj.style.display = "block";
    
  } else if (strURL == "check_url.cgi") {
    obj = eval("document.getElementById('check_url')");
    obj.innerHTML = str;
  } 
  
  
}

function updateRate(id,cis,what) {
  var obj = eval("document.getElementById('fav" + id + "')");
  var obj_2 = eval("document.getElementById('favx" + id + "')");

  if (cis == 1) {//
      if ((what != "") && (what == 'update')) { xmlhttpPost('make_star.cgi',id,1) };
      obj.innerHTML = '<a href="javascript:void(0);" onClick="updateRate(' + id + ',0,\'update\');">Remove from your "My favorite games"</a>';
      obj_2.value = 1; 
  } else {
      if ((what != "") && (what == 'update')) { xmlhttpPost('make_star.cgi',id,0) };
      obj.innerHTML = '<a href="javascript:void(0);" onClick="updateRate(' + id + ',1,\'update\');">Add to your favorite games</a>';
      obj_2.value = 0;
  }
}

function updateVotes(id,vote) {
  xmlhttpPost('make_vote.cgi',id,vote);
}

var doing = 0;

function nullClasses(id,cis,objX) {
  var obj;
  //pom = Div(cis,1)+1;
  pom = Math.round(cis)+1;

  //if (pom > 5) { pom = 5; };
  pom = 1;  
  obj = eval("document.getElementById('rat" + id + "_" + pom + "')");
  if (pom == 1 ) {
    obj.className = "one-star";
    obj.style.width = "15px";
  } else if (pom == 2) {
    obj.className = "two-stars";
    obj.style.width = "30px";
  } else if (pom == 3) {
    obj.className = "three-stars";
    obj.style.width = "45px";
  } else if (pom == 4) {
    obj.className = "four-stars";
    obj.style.width = "60px";    
  } else if (pom == 5) {
    obj.className = "five-stars";
    obj.style.width = "75px";    
  }
  doing = 1;
}

function Mod(a, b) {
    return a - Math.floor(a / b) * b;
}

function Div(a, b) {
    return Math.floor(a / b);
}

function reRate(cis,id,ww) {
  var obj;
  pom = Math.round(cis)+1;
  //pom = Div(cis,1);
  //if (pom > 5) { pom = 5; };  
  pom = 1;
  
  obj = eval("document.getElementById('rat" + id + "_" + pom + "')");

  if (cis > 0 && cis <= 0.5) {
    obj.className = "on";
    obj.style.width = "7px";  
  } else if (cis > 0.5 && cis <= 1) {
    obj.className = "on";
    obj.style.width = "15px";  
  } else if (cis > 1 && cis <= 1.5) {
    obj.className = "on";
    obj.style.width = "23px";  
  } else if (cis > 1.5 && cis <= 2) {
    obj.className = "on";
    obj.style.width = "30px";  
  } else if (cis > 2 && cis <= 2.5) {
    obj.className = "on";
    obj.style.width = "37px";  
  } else if (cis > 2.5 && cis <= 3) {
    obj.className = "on";
    obj.style.width = "45px";  
  } else if (cis > 3 && cis <= 3.5) {
    obj.className = "on";
    obj.style.width = "53px";  
  } else if (cis > 3.5 && cis <= 4) {
    obj.className = "on";
    obj.style.width = "60px";  
  } else if (cis > 4 && cis <= 4.5) {
    obj.className = "on";
    obj.style.width = "67px";  
  } else if (cis > 4.5 && cis <= 5) {
    obj.className = "on";
    obj.style.width = "75px";  
  }
  
  var obj_x = eval("document.getElementById('rate_online" + id + "')");
  if (obj_x !=  null && ww) {
    var obj_v = eval("document.getElementById('pom_vote" + id + "')");
    var obj_vc = eval("document.getElementById('pom_vote_count" + id + "')");
    obj_x.innerHTML = "Rate: " + obj_v.value + " / " + obj_vc.value + " people";
  }
  
  doing = 0;
}

function addBookmark(url,title) {
  if (window.sidebar)
  {
    window.sidebar.addPanel(title, url,"");
  }
  else if( document.all )
  {
    window.external.AddFavorite( url, title);
  }
  else
  {
    return true;
  }
}


