var xmlHttp;
var article;

function createXMLHttpRequest()
{
    if (window.ActiveXObject)
    {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else if (window.XMLHttpRequest)
    {
        xmlHttp = new XMLHttpRequest();
    }
}

function doAjaxStuff(type, url, queryString)
{
    createXMLHttpRequest();
    xmlHttp.onreadystatechange = handleStateChange;
    if (type == "POST")
    {
        xmlHttp.open(type, url, true);
        xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    }
    else
    {
        url = url + "?" + queryString + "&timestamp=" + new Date().getTime();
        xmlHttp.open(type, url, true);
        queryString = null;
    }

    var body = document.getElementsByTagName("body")[0];
    var pageDimensions = getPageDimensions();
    var viewportSize = getViewportSize();

    if (viewportSize[1] > pageDimensions[1])
    {
        pageDimensions[1] = viewportSize[1];
    }

    var dropSheet = document.createElement("div");

    dropSheet.setAttribute("id", "dropSheet");
    dropSheet.style.position = "absolute";
    dropSheet.style.left = "0";
    dropSheet.style.top = "0";
    dropSheet.style.width = pageDimensions[0] + "px";
    dropSheet.style.height = pageDimensions[1] + "px";
    body.appendChild(dropSheet);

    dialog = document.createElement("div");
    dialog.setAttribute("id", "dialog");
    dialog.className = "customDialog";
    dialog.style.visibility = "hidden";
    dialog.style.position = "absolute";

    var dialogMessage = document.createElement("p");
    dialogMessage.appendChild(document.createTextNode("Please wait...submitting your rating..."));
    dialog.appendChild(dialogMessage);

    body.appendChild(dialog);

    var scrollingPosition = getScrollingPosition();

    dialog.style.left = scrollingPosition[0] + parseInt(viewportSize[0] / 2) - parseInt(dialog.offsetWidth / 2) + "px";
    dialog.style.top = scrollingPosition[1] + parseInt(viewportSize[1] / 2) - parseInt(dialog.offsetHeight / 2) + "px";
    dialog.style.visibility = "visible";

    // document.getElementById("rating").innerHTML = "Please wait...submitting your rating...";
    setTimeout("xmlHttp.send('"+queryString+"')", 2000);
}

function handleStateChange()
{
    if(xmlHttp.readyState == 4)
    {
        if(xmlHttp.status == 200)
        {
            parseResults();
        }
    }
}

function submitRating(score, page)
{
    var rating      = encodeURIComponent(score);
    article         = encodeURIComponent(page);
    var url         = "/articles/rating.php";
    var queryString = "rating=" + rating + "&article=" + article;
    doAjaxStuff("GET", url, queryString);
    return false;
}

function parseResults()
{
    var cookieName = "sites";
    var results    = xmlHttp.responseXML;
    var response   = results.getElementsByTagName("xmlresponse");
    var code       = response[0].getElementsByTagName("resultcode")[0].firstChild.nodeValue;

    if (code.indexOf('success') != -1)
    {
        document.getElementById("dialog").innerHTML = "<p>Thank you for rating this article.</p>";
        setTimeout("closeDialog(dialog)", 2000);
        document.getElementById("rating").innerHTML = "Thank you for rating this article.";
        setCookie(article, "voted", 180);
    }
    else
    {
        document.getElementById("dialog").innerHTML = "<p>There was an error while contacting the server.</p>";
        setTimeout("closeDialog(dialog)", 2000);
        document.getElementById("rating").innerHTML = "There was an error while contacting the server.";
    }
}

function prepareRating()
{
    if (!document.getElementsByTagName) return false;
    if (!document.getElementById) return false;
    if (document.getElementById("rating"))
    {
        var url  = document.getElementById("article").getAttribute("value");
        rateform = document.getElementById("rating");
        ratings  = rateform.getElementsByTagName("input");
        for (var i = 0; i < ratings.length; i++)
        {
            if (ratings[i].getAttribute("type") == "radio")
            {
                var score = ratings[i].getAttribute("value");
                ratings[i].onclick = function()
                {
                    submitRating(score, url);
                }
            }
        }
    }
}

function getScrollingPosition()
{
  //array for X and Y scroll position
  var position = [0, 0];

  //if the window.pageYOffset property is supported
  if (typeof window.pageYOffset != 'undefined')
  {
    //store position values
    position = [
        window.pageXOffset,
        window.pageYOffset
    ];
  }

  //if the documentElement.scrollTop property is supported
  //and the value is greater than zero
  if (typeof document.documentElement.scrollTop != 'undefined'
      && document.documentElement.scrollTop > 0)
  {
    //store position values
    position = [
        document.documentElement.scrollLeft,
        document.documentElement.scrollTop
    ];
  }

  //if the body.scrollTop property is supported
  else if(typeof document.body.scrollTop != 'undefined')
  {
    //store position values
    position = [
        document.body.scrollLeft,
        document.body.scrollTop
    ];
  }

  //return the array
  return position;
}

function closeDialog(dialog)
{
  var dropSheet = document.getElementById("dropSheet");

  dropSheet.parentNode.removeChild(dropSheet);
  dialog.parentNode.removeChild(dialog);

  return true;
}

function getViewportSize()
{
  var size = [0,0];

  if (typeof window.innerWidth != 'undefined')
  {
    size = [
        window.innerWidth,
        window.innerHeight
    ];
  }
  else if (typeof document.documentElement != 'undefined'
      && typeof document.documentElement.clientWidth != 'undefined'
      && document.documentElement.clientWidth != 0)
  {
    size = [
        document.documentElement.clientWidth,
        document.documentElement.clientHeight
    ];
  }
  else
  {
    size = [
        document.getElementsByTagName('body')[0].clientWidth,
        document.getElementsByTagName('body')[0].clientHeight
    ];
  }

  return size;
}

function getPageDimensions()
{
  var body = document.getElementsByTagName("body")[0];
  var bodyOffsetWidth = 0;
  var bodyOffsetHeight = 0;
  var bodyScrollWidth = 0;
  var bodyScrollHeight = 0;
  var pageDimensions = [0, 0];

  if (typeof document.documentElement != "undefined" &&
      typeof document.documentElement.scrollWidth != "undefined")
  {
    pageDimensions[0] = document.documentElement.scrollWidth;
    pageDimensions[1] = document.documentElement.scrollHeight;
  }

  bodyOffsetWidth = body.offsetWidth;
  bodyOffsetHeight = body.offsetHeight;
  bodyScrollWidth = body.scrollWidth;
  bodyScrollHeight = body.scrollHeight;

  if (bodyOffsetWidth > pageDimensions[0])
  {
    pageDimensions[0] = bodyOffsetWidth;
  }

  if (bodyOffsetHeight > pageDimensions[1])
  {
    pageDimensions[1] = bodyOffsetHeight;
  }

  if (bodyScrollWidth > pageDimensions[0])
  {
    pageDimensions[0] = bodyScrollWidth;
  }

  if (bodyScrollHeight > pageDimensions[1])
  {
    pageDimensions[1] = bodyScrollHeight;
  }

  return pageDimensions;
}

addLoadEvent(prepareRating);
