/*
  Writen by John Tang 
  poll.js requires json.js and cookie.js to be included in the parent page as well.
*/

var gHTTP = null;
gReadyToProcess =  true;

function getHttpObject() {

   xmlhttp = false;
   
   // branch for native XMLHttpRequest object
   if(window.XMLHttpRequest) {   
   	  try {
	     xmlhttp = new XMLHttpRequest();
      } catch(e) {
	     xmlhttp = false;
      }
      
   // branch for IE/Windows ActiveX version
   } else if(window.ActiveXObject) {
      try {
         xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
      } catch(e) {
         try {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
         } catch(e) {
            xmlhttp = false;
         }
	  }
   }
   
   return xmlhttp;
}

function getPoll(gURL) {
	if(!gReadyToProcess) return;
	
	gReadyToProcess =  false;
	try {
		delete gHTTP;
	} catch (err) {
	    //don't do any thing.
	}
    gHTTP = getHttpObject();
	gHTTP.onreadystatechange = pollResponseHandler;
    gHTTP.open("GET", gURL, true);
	gHTTP.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
	gHTTP.send(null);
}



function pollResponseHandler() {
	var rc;

	if (gHTTP.readyState == 4) {
        if (gHTTP.responseText != "") {		
            rc = gHTTP.responseText;
			gHTTP = null;
            if (rc.indexOf("Error") > -1) {
                alert(rc);
                
            } else if(rc.indexOf("Failure") > -1) {
                alert("The validation code is incorrect or expired.  Please retype the code or refresh the screen.");
            
            } else if(rc.length > 0) {
				createOrDisplayPoll(rc);
            }
            
        }
		gHTTP = null;
        gReadyToProcess = true;
    } 
}


function createOrDisplayPoll(jsonStr) {
    var obj;
	var cookie;
	var cookieName;
	

	
	try {
		//obj = jsonStr.parseJSON();
		obj = eval("(" + jsonStr + ")");
        cookieName = "poll_" + obj.id;
		cookie = readCookie(cookieName);
		if(cookie != null && (typeof(cookie) != "undefined")) {
		    displayPoll(obj);
		} else {
			//setCookie(cookieName,"1", 3);
			createPoll(obj);
		}
	} catch (e) {
	    //alert(e.name + ": " + e.message);	
	}
}



function createPoll(obj) {
	var html;
	var i;

    html = "<form action=\"/onsetproxy/machines/onset/onsetfeature/pollcapvarsity.php\" method=\"post\" name=\"pollForm\" target=\"pollWindow\" onsubmit=\"return checkVote(this);\">";
	html += "<input type=\"hidden\" id=\"pollid\" name=\"pollid\" value=\"" + obj.id + "\" />";
    html += "<input type=\"hidden\" id=\"captcha\" name=\"captcha\" value=\"" + obj.captcha + "\" />";
    if (document.domain.indexOf('www.') < 0) html += "<input type=\"hidden\" id=\"dn\" name=\"dn\" value=\"" + document.domain + "\" />";

	html += "<table>\n";
	html += "<tr>\n";
    html += "   <td class=\"pollquestion\">" + obj.question + "</td>\n";
	html += "</tr>\n";
	
	for(i=0; i<12; i++) {
	   if(obj.answers[i] != "") html += createPollRow(i, obj.answers[i]);
	}

	html += "<tr>\n";
    html += "<td align=\"center\">\n";
    html += "<table border=\"0\" cellpadding=\"2\" cellspacing=\"2\" width=\"100%\">\n";
    html += "<tbody><tr>\n";
    html += "<td class=\"pollquestion\" colspan=\"3\">\n";
    html += "<strong>Enter The Code To Vote</strong>\n";
    html += "</td>\n";
    html += "</tr>\n";
    html += "<tr>\n";
    html += "<td valign=\"middle\"><input id=\"guess\" name=\"guess\" size=\"8\" maxlength=\"5\" type=\"text\"></td>\n";
    html += "<td valign=\"middle\">&nbsp;</td>\n";
    html += "<td valign=\"middle\"><img src=\"http://onset.freedom.com/onsetfeature/captcha-img.php?captcha=" + obj.captcha + "\"></td>\n";
    html += "</tr>\n";
    html += "<tr>\n";
    html += "<td colspan=\"3\">\n";
    html += "<input value=\"Vote!\" id=\"voteButton\" name=\"voteButton\" type=\"submit\">\n";
    html += "</td>\n";
    html += "</tr>\n";
    html += "</tbody></table>\n";
    html += "</td>\n";
    html += "</tr>\n";
	html += "</table>\n";
    html == "</form>";
	
    el(gDivName).innerHTML = html;
}



function createPollRow(i, answer) {
	var html;
	var idx = i + 1;
	
	html  = "<tr>\n";
    html +=   "<td class=\"pollanswers\"> <input id=\"vote\" name=\"vote\" value=\"" + idx + "\" type=\"radio\"> " + answer + "</td>\n";
	html += "</tr>";
	
	return html;

}





function displayPoll(obj) {
	var html = "<table>\n";
	var i;
	
	html += "<tr>\n";
    html += "   <td class=\"pollquestion\">" + obj.question + "</td>\n";
	html += "</tr>\n";
	
	for(i=0; i<12; i++) {
	   if(obj.answers[i] != "") html += displayPollRow(obj.answers[i], obj.votes[i], obj.votepcts[i]);
	}
	html += "<tr>\n";
    html += "   <td class=\"polltotal\">Total Votes: " + obj.totalvotes + "</td>\n";
	html += "</tr>\n";
	html += "</table>";

    el(gDivName).innerHTML = html;
}



function displayPollRow(answer, vote, pct) {
	var html;
	
	html  = "<tr>\n";
    html += "  <td class=\"pollanswers\">\n";
    html += answer + "  <table width=\"" + pct + "%\">\n";
    html +=   "<tbody>\n";
    html +=     "<tr>\n";
    html +=       "<td class=\"pollbars\" title=\"" + vote + " votes ("  +  pct + "%)\">\n";
    html += pct + "%";
    html +=       "</td>\n";
    html +=     "</tr>\n";
    html +=   "</tbody>\n";
	html += "</table>\n";
    html += "</td>";
	html += "</tr>";
	
	return html;

}


function el(str) {
    return document.getElementById(str);	
}


function checkVote(f) {

	var radiogroup = f.vote;
	var guess = f.guess.value;

	if(guess == "") {
		alert("Enter The Code To Vote");
		return false;
	}

	for(var c = 0 ; c < radiogroup.length ; c++) {
		if(radiogroup[c].checked) {
			var votenum = radiogroup[c].value;
			//document.forms.pollForm.voteButton.disabled = 'true';
			pollWindowVal = window.open("","pollWindow","width=350,height=250,scrollbars=yes,resizable=yes");

			if (pollWindowVal.opener == null)
				pollWindowVal.opener = window; 

			pollWindowVal.opener.name = "opener";
 			return true;
		}
	}

	alert("You must choose an answer.");
	return false;
}
	
