var id = null;

// Main call function
function callServer(loc, respFunc) {
  xmlHttp = false;
  /*@cc_on @*/
  /*@if (@_jscript_version >= 5)
  try {
    xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
  } catch (e) {
    try {
      xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (e2) {
      xmlHttp = false;
    }
  }
  @end @*/
  if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
    xmlHttp = new XMLHttpRequest();
  }
  xmlHttp.open("GET", loc, true);
  eval("xmlHttp.onreadystatechange = " + respFunc);
  xmlHttp.send(null);
}

function mainClick() {
  window.location = "http://www.artlogs.org";
}

function formgo() {
  if (document.getElementById('add_url').value == "") { alert("Please enter a Site URL."); return 0; }
  if (document.getElementById('add_feed').value == "") { alert("Please enter a correct Feed URL (this is important.)"); return 0; }
  if (document.getElementById('add_country').value == "All countries") { alert("Please select the Artlog's country of origin."); return 0; }
  document.register.submit();
}

function getfeed() {
  document.getElementById('loader').style.display = "block";
  obj = document.getElementById('add_url');
  callServer("/getfeed.php?f=" + obj.value, getfedResponse);
}

function getfedResponse() {
  if (xmlHttp.readyState == "4") {
    document.getElementById('loader').style.display = "none";
    if (document.getElementById('add_url').value != xmlHttp.responseText) {
      document.getElementById('add_feed').value = xmlHttp.responseText;
    }
    document.getElementById('extra').style.display = "block";
  }
}

function flag(id) {
  callServer("/flag.php?id=" + id, flagResponse);
}

function thumb_up(bId) {
  callServer("/thumb.php?thumb=up&id=" + bId, thumbResponse);
  id = bId;
}

function thumb_down(bId) {
  callServer("/thumb.php?thumb=down&id=" + bId, thumbResponse);
  id = bId;
}

function thumbResponse() {
  if (xmlHttp.readyState == "4") {
    if (xmlHttp.responseText == "login") {
      alert("You must login to your Artlogs account to perform this action.");
    }
    if (xmlHttp.responseText == "up") {
      document.getElementById('b_' + id + '_up').src = "/img/thumb_up_on.gif";
      document.getElementById('b_' + id + '_down').src = "/img/thumb_down.gif"; 
      return 0;
    }
    if (xmlHttp.responseText == "down") {
      document.getElementById('b_' + id + '_up').src = "/img/thumb_up.gif";
      document.getElementById('b_' + id + '_down').src = "/img/thumb_down_on.gif";
      return 0;
    }
  }
}

function flagResponse() {
  if (xmlHttp.readyState == "4") {
    if (xmlHttp.responseText == "already") {
      alert("Someone has already flagged this photoblog, can't do it again - sorry!");
      return 0;
    }
    if (xmlHttp.responseText == "login") {
      alert("You must login to do this.");
      return 0;
    }
    if (xmlHttp.responseText == "thanks") {
      alert("Thanks for your feedback, it's been recorded!");
      return 1;
    }
  }
}

