// Maian Support v2.0
// Javascript/Ajax functions
// Written by David Ian Bennett
// http://www.maianscriptworld.co.uk

var xmlHttp = createXmlHttpRequestObject();

function createXmlHttpRequestObject() {
  var xmlHttp;
  if (window.ActiveXObject) {
    try {
      xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch(e) {
      xmlHttp = false;
    }
  } else {
    try {
      xmlHttp = new XMLHttpRequest();
    }
    catch(e) {
      var xmlHttpVersions = new Array('MSXML2.XMLHTTP.6.0',
                                      'MSXML2.XMLHTTP.5.0',
                                      'MSXML2.XMLHTTP.4.0',
                                      'MSXML2.XMLHTTP.3.0',
                                      'MSXML2.XMLHTTP',
                                      'Microsoft.XMLHTTP');
      for (var i=0; i<xmlHttpVersions.length && !xmlHttp; i++) {
        try {
          xmlHttp = new ActiveXObject(xmlHttpVersions[i]);
        }
        catch (e) {}
      }                                
      xmlHttp = false;
    }
  }

  if (!xmlHttp) {
    alert('Error Creating XMLHttpRequest Object');
  } else {
    return xmlHttp;
  }
}

// New password..
function sendNewPassword() {
  email = document.getElementById('email').value;
  if (email) {
    if (xmlHttp.readyState==4 || xmlHttp.readyState==0) {
      try {
        xmlHttp.open("GET","index.php?np="+email,true);
        xmlHttp.setRequestHeader('Content-Type', 
                                 'application/x-www-form-urlencoded; charset=UTF-8');
        xmlHttp.onreadystatechange = handleServerResponse;
        document.getElementById('forgot').style.display = 'block';
        document.getElementById('forgot').innerHTML = '<img src="templates/images/loading.gif" alt="" title="" /> ';
        xmlHttp.send(null);
      }
      catch (e) {
        alert("Server Error Connection:"+e.toString());
      }
    } else {
      setTimeout('sendNewPassword()',1000);
    }
  }
}

// Apply vote..
function addVote(id,vote) {
  if (id>0) {
    if (xmlHttp.readyState==4 || xmlHttp.readyState==0) {
      try {
        xmlHttp.open("GET","index.php?v="+id+"&vote="+vote,true);
        xmlHttp.setRequestHeader('Content-Type', 
                                 'application/x-www-form-urlencoded; charset=UTF-8');
        xmlHttp.onreadystatechange = handleServerResponse;
        document.getElementById('loading').innerHTML = '<img src="templates/images/loading.gif" alt="" title="" /> ';
        xmlHttp.send(null);
      }
      catch (e) {
        alert("Server Error Connection:"+e.toString());
      }
    } else {
      setTimeout('addVote()',1000);
    }
  }
}

function xmlResponseHeaders(msg,xml) {
  switch (msg) {
    case 'email-error':
    case 'email-no-account':
    case 'newpass':
    
    if (msg=='email-error' || msg=='email-no-account') {
      text   = xml.getElementsByTagName('text')[0].firstChild.data;
      document.getElementById('forgot').innerHTML = '<span class="newPassError">'+text+'</span>';
    }
    if (msg=='newpass') {
      text   = xml.getElementsByTagName('text')[0].firstChild.data;
      document.getElementById('forgot').innerHTML = '<span class="newPassSuccess">'+text+'</span>';
    }
    
    break;
    case 'vote-added':
    text   = xml.getElementsByTagName('text')[0].firstChild.data;
    vote1  = xml.getElementsByTagName('votey')[0].firstChild.data;
    vote2  = xml.getElementsByTagName('voten')[0].firstChild.data;
    no     = xml.getElementsByTagName('no')[0].firstChild.data;
    yes    = xml.getElementsByTagName('yes')[0].firstChild.data;
    document.getElementById('loading').innerHTML = '';
    document.getElementById('voting').innerHTML  = '<span class="voting" id="voting">'+
                                                   ' <span class="voted">'+text+'</span>'+
                                                   ' <span class="yes">'+yes+'</span> ('+vote1+'%)'+
                                                   ' <span class="no">'+no+'</span> ('+vote2+'%)'+
                                                   '</span>';
    break;
  }
  return false;
}

function handleServerResponse() {
  if (xmlHttp.readyState==4) {
    if (xmlHttp.status==200) {
      try {
        var xmlResponse  = xmlHttp.responseXML;
        var rootNodeName = xmlResponse.documentElement.nodeName;
        if (!xmlResponse || !xmlResponse.documentElement) {
          throw("Invalid XML Structure:\n"+xmlHttp.responseText);
        } else if (rootNodeName=='parsererror') {
          throw("Invalid XML Structure:\n"+xmlHttp.responseText);
        } else {
          xmlDoc      = xmlResponse.documentElement;
          msg         = xmlDoc.getElementsByTagName('message')[0].firstChild.data;
          xmlResponseHeaders(msg,xmlDoc);
        }
      }
      catch(e) {
        alert('Error reading response: '+e.toString());
      }
    } else {
      alert('There was a problem accessing the server:'+xmlHttp.statusText);
    }
  }
}

