// ajax bits for this script from linuxjournal.com
// rest PL Collett 2007

var xhr = getXMLHttpRequest();
var displayarea = 'votes'; // default name for results area

// the cross-browser creation function
function getXMLHttpRequest () {
try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch(e) {};
try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {}
try { return new XMLHttpRequest(); } catch(e) {};
    return null;
}

// the callback function for the url
function parseHttpResponse() {

var errstatus = 0;
var votetxt = 'votes so far: ';
var voteresult = [ ];
var ix = document.getElementById(displayarea);
    //alert("entered parseHttpResponse displayarea=" + displayarea);
    //alert("entered parseHttpResponse");

    // some docs might not want to display a result
    if (ix != null) {
	ix.innerHtml = "Processing...";
    }

    if (xhr.readyState == 4) {
        //alert("readystate == 4");
        if (xhr.status == 200) {
            //alert(xhr.responseText);
	    voteresult = xhr.responseText.split("\n");
            for (i=0 ; i<voteresult.length; i++)
            {
	    	rsltbits = voteresult[i].split(" ");
                if (rsltbits[0] == 'votes')
                {
                    votetxt = votetxt+rsltbits[1]+' '+rsltbits[2]+', \n';
                }
                if (rsltbits[0] == 'already')
                {
                    votetxt = 'Sorry you have already voted\n';
                }
            }
	    if (ix != null) {
		ix.innerHTML = votetxt;
            } else {
            	//alert("ix is NULL!!");
		errstatus = 4;
	    }
        }
        else
        {
            //alert("xhr.status == " + xhr.status);
	    errstatus = 5;
        }
    }
}

//**************************** interface from HTML ***************
function votefor(vt,trg,disp) {

// set up named area for results
displayarea = disp;

// open doesnt actually send request - third param true=asynchronous

// note only change when deployed ****************************************
//xhr.open("GET", "http://localhost/cgi-bin/vote.pl?subj="+vt+"&vote="+trg, true);
//xhr.open("GET", "http://192.168.0.2/cgi-bin/vote.pl?subj="+vt+"&vote="+trg, true);
xhr.open("GET", "http://www.dorsetshire.com/cgi-bin/vote.pl?subj="+vt+"&vote="+trg, true);
// note only change when deployed ****************************************

// set up callback function
xhr.onreadystatechange = parseHttpResponse;

// actually sends request
xhr.send(null);

}

