
/**
 * Library of Poll-token related functions
 */
 
function newsys_poll_popup(sURL, sWindow, nWidth, nHeight)
{
   var hWindow;
   var sParams = "";

   // Standard modal arguments

   sParams += "resizable=yes";
   sParams += ",scrollbars=yes";
   sParams += ",location=no";
   sParams += ",toolbar=no";
   sParams += ",statusbar=no";
   sParams += ",width="+nWidth;
   sParams += ",innerWidth="+nWidth;
   sParams += ",height="+nHeight;
   sParams += ",innerHeight="+nHeight;
   sParams += ",dependent=yes";

   // Try to center the window (if we can)

   if (window.screen) {

      var nCenterWidth = (screen.availWidth)/2 - nWidth/2;
      var nCenterHeight = (screen.availHeight)/2 - nHeight/2;

      sParams += ",left="+nCenterWidth;
      sParams += ",screenX="+nCenterWidth;
      sParams += ",top="+nCenterHeight;
      sParams += ",screenY="+nCenterHeight;
 
   }
   
   hWindow = window.open(sURL, sWindow, sParams);
}

function newsys_poll_getcookie(c_name)
{
    if (document.cookie.length>0)
    {
        c_start=document.cookie.indexOf(c_name + "=")
        if (c_start!=-1)
        {
            c_start=c_start + c_name.length+1
            c_end=document.cookie.indexOf(";",c_start)
            if (c_end==-1) c_end=document.cookie.length
            {
                return unescape(document.cookie.substring(c_start,c_end))
            }
        }
        return null
	}
}

function newsys_poll_http_client()
{
    var xmlhttp=false; //Clear our fetching variable
        try {
            xmlhttp = new ActiveXObject('Msxml2.XMLHTTP'); //Try the first kind of active x object.
        } catch (e) {
            try {
                xmlhttp = new ActiveXObject('Microsoft.XMLHTTP'); //Try the second kind of active x object
            } catch (E) {
                xmlhttp = false;
            }
        }
    if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
        xmlhttp = new XMLHttpRequest(); //If we were able to get a working active x object, start an XMLHttpRequest
    }

    return xmlhttp;
}


function newsys_poll_vote(id,qid,duedate,sElementId)
{
	var xmlhttp = newsys_poll_http_client();
    var pollid='newsys_poll_'+id;
    var newsysPollID = newsys_poll_getcookie(pollid);

    if (newsysPollID!=null) {

        var file = '/shared-content/newsys/poll/poll-vote.php?pollid=';
        file = file + id +'&questid='+qid+'&vote=0';
        xmlhttp.open('GET', file, true);

        xmlhttp.onreadystatechange=function() {
            if (xmlhttp.readyState==4) {
                if (xmlhttp.status==200) {
                    var content = xmlhttp.responseText;
                    document.getElementById(sElementId).innerHTML = content;
                }
            }
        }
        xmlhttp.send(null);
        return;


    } else {
        var file = '/shared-content/newsys/poll/poll-vote.php?pollid=';
        file = file + id +'&questid='+qid+'&vote=1';
        xmlhttp.open('GET', file, true);

        xmlhttp.onreadystatechange=function() {
            if (xmlhttp.readyState==4) {
                if (xmlhttp.status==200) {
                    var content = xmlhttp.responseText;
                    document.getElementById(sElementId).innerHTML = content;
                }
            }
        }
        var cookieName = 'newsys_poll_' + id;
        document.cookie=cookieName+ '=' +id+'; expires='+duedate ;
        xmlhttp.send(null);
        return;
    }
}


