/**
 *
 * saveData()
 *
 */
function saveData()
{
  // Create the HTTP Object
  var ajaxRequest = getHTTPObject();

  var skipOwn;

  if(document.getElementById("skipOwnYes").checked == true)
  {
    // checkbox is ticked
    skipOwn = 'yes';
  }

  else
  {
    // checkbox is unticked
    skipOwn = 'no';
  }

 var url = "http://saddestthing.com/includes/skipOwn.php?skipOwn=";

  // Create a function that will receive data sent from the server
  ajaxRequest.onreadystatechange = function()
  {
    if(ajaxRequest.readyState == 4)
    {
      document.getElementById("skipOwnStatus").innerHTML = ajaxRequest.responseText;
    }
  }

  ajaxRequest.open("GET", url+skipOwn, true);
  ajaxRequest.send(null);

 return false; //Prevent the form from being submited
}

/**
 *
 * checkDisplayName()
 *
 */
function checkDisplayName()
{
 var ajaxRequest = getHTTPObject(); // Create the HTTP Object

 var displayName = document.getElementById("displayName").value;
 var url = "http://saddestthing.com/includes/checkDisplayName.php?displayName=";

 // Create a function that will receive data sent from the server
  ajaxRequest.onreadystatechange = function()
  {
    var dns = document.getElementById("displayNameStatus");

    if (dns)
    {
      dns.parentNode.removeChild(dns);
    }

    if (!document.getElementById("displayNameStatus"))
    {
      // Create a new div
      var dnE = document.createElement('div');

      // Set the id to "displayNameStatus"
      dnE.setAttribute('id','displayNameStatus');
    }

    else
    {
      var dnE = document.getElementById("displayNameStatus");
    }

    if (ajaxRequest.readyState == 4)
    {
      if (ajaxRequest.responseText != '')
      {
        var dnEText = document.createTextNode(ajaxRequest.responseText);

        // Set the class to "error"
        dnE.setAttribute('class','error');

        // Add the text as a child of the div
        dnE.appendChild(dnEText);

        document.getElementById("displayName").value = '';
        document.getElementById("displayName").style.border.color = '#F00';
      }

      else if (ajaxRequest.responseText == '')
      {
        var text = 'This display name is available.';
        var dnEText = document.createTextNode(text);

        // Set the class to "success"
        dnE.setAttribute('class','success');

        // Add the text as a child of the div
        dnE.appendChild(dnEText);

        document.getElementById("displayName").style.border.color = '';
      }

    }

    if (dnE)
    {
      // Add the new div to the parent of the input field (the div above)
      document.getElementById("displayName").parentNode.appendChild(dnE);
    }

  }

  /* Send the POST request */
  ajaxRequest.open("GET", url+displayName, true);
  ajaxRequest.send(null);

 return false;
}

/**
 *
 * checkThingName()
 *
 */
function checkThingName()
{
 var ajaxRequest = getHTTPObject(); // Create the HTTP Object

 var thingName = document.getElementById("thingName").value;
 var url = "http://saddestthing.com/includes/checkThingName.php?thingName=";

 // Create a function that will receive data sent from the server
  ajaxRequest.onreadystatechange = function()
  {
    var dns = document.getElementById("thingNameStatus");

    if (dns)
    {
      dns.parentNode.removeChild(dns);
    }

    if (!document.getElementById("thingNameStatus"))
    {
      // Create a new div
      var dnE = document.createElement('div');

      // Set the id to "thingNameStatus"
      dnE.setAttribute('id','thingNameStatus');
    }

    else
    {
      var dnE = document.getElementById("thingNameStatus");
    }

    if (ajaxRequest.readyState == 4)
    {
      if (ajaxRequest.responseText != '')
      {
        var dnEText = document.createTextNode(ajaxRequest.responseText);

        // Set the class to "error"
        dnE.setAttribute('class','error');

        // Add the text as a child of the div
        dnE.appendChild(dnEText);

        document.getElementById("thingName").value = '';
        document.getElementById("thingName").style.border.color = '#F00';
      }

      else if (ajaxRequest.responseText == '')
      {
        var text = 'Your title is unique and good to go.';
        var dnEText = document.createTextNode(text);

        // Set the class to "success"
        dnE.setAttribute('class','success');

        // Add the text as a child of the div
        dnE.appendChild(dnEText);

        document.getElementById("thingName").style.border.color = '';
      }

    }

    if (dnE)
    {
      // Add the new div to the parent of the input field (the div above)
      document.getElementById("thingName").parentNode.appendChild(dnE);
    }

  }

  /* Send the POST request */
  ajaxRequest.open("GET", url+thingName, true);
  ajaxRequest.send(null);

 return false;
}


/**
 *
 * toggle()
 *
 */
function toggle()
{
  if (document.getElementById("hide").style.display=='none')
  {
   document.getElementById("hide").style.display = '';
  }

  else
  {
    document.getElementById("hide").style.display = 'none';
  }
}

/**
 *
 * getHTTPObject()
 *
 */
function getHTTPObject()
{
 var xmlhttp;

 //Use IE's ActiveX items to load the file.
 if(typeof ActiveXObject != 'undefined')
 {
  try
  {
   xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
  }

  catch (e)
  {
   try
   {
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
   }
   catch (E)
   {
    xmlhttp = false;
   }
  }
 //If ActiveX is not available, use the XMLHttpRequest of Firefox/Mozilla etc. to load the document.
 } else if (XMLHttpRequest)
 {
  try
  {
   xmlhttp = new XMLHttpRequest();
  }
  catch (e)
  {
   xmlhttp = false;
  }
 }
 else
 {
  xmlhttp = false;
 }
 return xmlhttp;
}

/**
 *
 * addLoadEvent()
 *
 */
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}