//----------------------------
//AJAX Polling Question for Vendor News Letter
//----------------------------
//creates a cookie
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}
//reads a cookie
function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}
//deletes a cookie
function eraseCookie(name) {
	createCookie(name,"",-1);
}
//creates a XMLHTTP object
function getXMLObj(bAsXML) {
	var req = false;
	if(window.XMLHttpRequest) {
		try {
		  req = new XMLHttpRequest();
      if (bAsXML)
        req.setMimeType('text/xml');
		} catch(e) {
		  req = false;
		}
	} else if(window.ActiveXObject) {
	  try {
		  req = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			try {
			  req = new ActiveXObject("Microsoft.XMLHTTP");
			} catch(e) {
				req = false;
			}
	  }
	}
	return req;
}
//posts the data to the proxy page 
function getPage(url, cntID, params) {
  var rnd = Math.random();
  var xmlhttp = getXMLObj();
  var elem = document.getElementById(cntID);
  if (xmlhttp) {
	xmlhttp.open("POST",url,true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.setRequestHeader("Content-length", params.length);
	xmlhttp.setRequestHeader("Connection", "close");
    xmlhttp.onreadystatechange=function() {
      if (xmlhttp.readyState==4) {
        elem.innerHTML = xmlhttp.responseText;
      }
    }
    xmlhttp.send(params)
  } else {
    alert("Page could not be found.");
  }
}
//updates the poll data and displays the results
function updatePoll() {
	var currentDate = new Date()
	var poll_id = document.pollquestion.poll_id.value;
	var poll_content_div = document.pollquestion.poll_content_div.value;
	var polloption = '';
	var isOptionChecked=false;
	for (var i=0; i<document.pollquestion.poll_option.length; i++)  { 
	if (document.pollquestion.poll_option[i].checked)  {
	polloption = document.pollquestion.poll_option[i].value;
	isOptionChecked=true;
	} 
	} 
	if (isOptionChecked) {
	getPage("http://www.alanet.org/HTMail/vnews/pollingquestion.asp","pollquestion","poll_id="+ poll_id +"&poll_source=ALAMeansBusiness&poll_option="+ polloption +"&poll_action=updatepoll");
	createCookie("ALApoll_id_"+ poll_id,currentDate,730);
	}
}

//determines whether to display the form or show the results
function checkPollCookie(poll_id, poll_content_div)
{
	var allowvote = false;
	var pollcookie = readCookie("ALApoll_id_"+ poll_id);
	if ((pollcookie == "") || (null == pollcookie)) {
		getPage("http://www.alanet.org/HTMail/vnews/pollingquestion.asp",poll_content_div,"poll_id="+ poll_id +"&poll_content_div="+ poll_content_div +"&poll_action=showform");
	} else {
		getPage("http://www.alanet.org/HTMail/vnews/pollingquestion.asp",poll_content_div,"poll_id="+ poll_id +"&poll_content_div="+ poll_content_div +"&poll_action=showresults");
	}
}
