var pollHandler = function() {

		var root = this;
			root.parent = document.getElementById('pollContents');
		// stores raw markup string(s) for various elements
			root.markup = {
				small :{
					Q:'	\
						<img src="/iwp/images/enf08/bg_poll_small.jpg" class="bgImg" width="394" height="232" alt="" border="0">\
						<h3><b>Reader Poll</b> </h3>\
						<h4 swfHandler="sifrtxt:guy"><!--poll title--></h4><br /><br />\
						<div id="chooseanswer"><a href="javascript:showPollOverlay();"><img src="/iwp/images/enf08/btn_choose_your_answer.jpg" width="160" height="29" alt="" border="0"></a></div>\
						<div id="posth4"><span class="list_item">»</span>\<a href="javascript:doURL(\'/Content.do?id=/Consumer_Home2/Enf_Tools_Resources/Enfamil_Polls\');">See All Polls</a></div>\
						<div id="pollOverlay">\
						<div id="pollOverlayTop"><img src="/iwp/images/enf08/poll_overlay_top.png" width="354" height="12" alt="" border="0" /></div><div id="pollOverlayMain">\
						<ul>\
							<li><img src="/iwp/images/enf08/poll-smlg-q-radioOff.png" class="radioImg" value="<!--value a-->" /><span class="a"><!--label a--></span></li>\
							<li><img src="/iwp/images/enf08/poll-smlg-q-radioOff.png" class="radioImg" value="<!--value b-->" /><span class="b"><!--label b--></span></li>\
							<li><img src="/iwp/images/enf08/poll-smlg-q-radioOff.png" class="radioImg" value="<!--value c-->" /><span class="c"><!--label c--></span></li>\
							<li><img src="/iwp/images/enf08/poll-smlg-q-radioOff.png" class="radioImg" value="<!--value d-->" /><span class="d"><!--label d--></span></li>\
							<li style="DISPLAY:none;"><img src="/iwp/images/enf08/poll-smlg-q-radioOff.png" class="radioImg" value="<!--value e-->" /><span class="e"><!--label e--></span></li>\
						</ul>\
						<a class="pollSubmit" id="the_submit_button"><img src="/iwp/images/enf08/poll-smlg-q-seeResults.png" alt="See Results" /></a>\
						</div><div id="pollOverlayBottom"><img src="/iwp/images/enf08/poll_overlay_bot.png" width="354" height="17" alt="" border="0" /></div>\
						</div>',
					A:'	<img src="/iwp/images/enf08/poll-sm-a-bg.jpg" class="bgImg" />\
						<h3>Results: <!--poll title--></h3>\
						<div class="info">\
							<h4><!--follow hdr--></h4>\
							<p><!--follow cnt--></p>\
							<a class="learnMore" href="javascript:doURL(\'<!--follow url-->\')"><img src="/iwp/images/enf08/poll-smlg-a-learnMore.png" alt="Learn More"></a>\
						</div>\
						<div class="outcome">\
							<ul class="visual">\
								<li class="a" style="padding-top: <!--padding a-->px;"><!--percent a-->%</li>\
								<li class="b" style="padding-top: <!--padding b-->px;"><!--percent b-->%</li>\
								<li class="c" style="padding-top: <!--padding c-->px;"><!--percent c-->%</li>\
								<li class="d" style="padding-top: <!--padding d-->px;"><!--percent d-->%</li>\
								<li class="e" style="DISPLAY:none;" style="padding-top: <!--padding e-->px;"><!--percent e-->%</li>\
							</ul>\
							<ul class="verbal">\
								<li class="a"><b><!--percent a-->%</b><span> <!--description a--></span><div class="clear"><!--clear--></div></li>\
								<li class="b"><b><!--percent b-->%</b><span> <!--description b--></span><div class="clear"><!--clear--></div></li>\
								<li class="c"><b><!--percent c-->%</b><span> <!--description c--></span><div class="clear"><!--clear--></div></li>\
								<li class="d"><b><!--percent d-->%</b><span> <!--description d--></span><div class="clear"><!--clear--></div></li>\
								<li class="e" style="DISPLAY:none;"><b><!--percent e-->%</b><span> <!--description e--></span><div class="clear"><!--clear--></div></li>\
							</ul>\
						</div>'
				},
				big   :{
					Q:'	<img src="/iwp/images/enf08/poll-lg-q-bg.png" class="bgImg" />\
						<h3><b>Reader Poll</b></h3>\
						<h4><!--poll title--></h4>\
						<ul>\
							<li class="a"><img src="/iwp/images/enf08/poll-smlg-q-radioOff.png" class="radioImg" value="<!--value a-->" /><span><!--label a--></span></li>\
							<li class="b"><img src="/iwp/images/enf08/poll-smlg-q-radioOff.png" class="radioImg" value="<!--value b-->" /><span><!--label b--></span></li>\
							<li class="c"><img src="/iwp/images/enf08/poll-smlg-q-radioOff.png" class="radioImg" value="<!--value c-->" /><span><!--label c--></span></li>\
							<li class="d"><img src="/iwp/images/enf08/poll-smlg-q-radioOff.png" class="radioImg" value="<!--value d-->" /><span><!--label d--></span></li>\
							<li class="e" style="DISPLAY:none;"><img src="/iwp/images/enf08/poll-smlg-q-radioOff.png" class="radioImg" value="<!--value e-->" /><span><!--label e--></span></li>\
						</ul>\
						<a class="pollSubmit" id="the_submit_button"><img src="/iwp/images/enf08/poll-smlg-q-seeResults.png" alt="See Results" /></a>\
						<div id="pollOverlay"></div>',
						
					A:'	<img src="/iwp/images/enf08/poll-lg-a-bg.png" class="bgImg" />\
						<h3><b><!--my month--> Results:</b> <!--poll title--></h3>\
						<div class="info">\
							<h4><!--follow hdr--></h4>\
							<p><!--follow cnt--></p>\
							<a class="learnMore" href="javascript:doURL(\'<!--follow url-->\')"><img src="/iwp/images/enf08/poll-smlg-a-learnMore.png" alt="Learn More"></a>\
						</div>\
						<div class="outcome">\
							<ul class="visual">\
								<li class="a" style="padding-top: <!--padding a-->px;"><!--percent a-->%</li>\
								<li class="b" style="padding-top: <!--padding b-->px;"><!--percent b-->%</li>\
								<li class="c" style="padding-top: <!--padding c-->px;"><!--percent c-->%</li>\
								<li class="d" style="padding-top: <!--padding d-->px;"><!--percent d-->%</li>\
								<li class="e" style="DISPLAY:none;" style="padding-top: <!--padding e-->px;"><!--percent e-->%</li>\
							</ul>\
							<ul class="verbal">\
								<li class="a"><b><!--percent a-->%</b><span><!--description a--></span></li>\
								<li class="b"><b><!--percent b-->%</b><span><!--description b--></span></li>\
								<li class="c"><b><!--percent c-->%</b><span><!--description c--></span></li>\
								<li class="d"><b><!--percent d-->%</b><span><!--description d--></span></li>\
								<li class="e" style="DISPLAY:none;"><b><!--percent e-->%</b><span><!--description e--></span></li>\
							</ul>\
						</div>'
				}
			}
		// if poll exists, then handle it.
			if (root.parent) {
			// clear remaining markup and normailize for replacement
			// sets request for poll info from server
				root.getPoll = function(month, baseUrl) {
					
					//var pollUrl = baseUrl || '/iwp/js/enf08/pollData/pollQuestion' + month + '.js'; 
					var pollUrl = baseUrl || encodeURL('/enfamil/ajaxPoll.do');
					
					if (pollUrl.indexOf("http") < 0) {
					    pollUrl = window.location.protocol+"//"+window.location.host+pollUrl;
					}
					if (month) {
					    if (pollUrl.indexOf("?") < 0) { 
					        pollUrl += '?timing=' + month;
					    } else {
					        pollUrl += '&timing=' + month;
					    } 
					}
					
					//alert(pollUrl);
					ajax.get({url:pollUrl, call:root.setPoll});
				}
			// recieves response from poll info from server
				root.setPoll = function(response) {
				// clear things out
					root.parent.innerHTML = '';
					root.parent.normalize();
					root.pollSize  = root.parent.className;
					root.pollType  = null;
					root.submitUrl = null;
					root.formValue = null;
					
				// process response
					if (typeof(response) == 'string') { eval('response = ' + response) }
					//jie:
					if (! response.answered) {
					    root.pollType         = 'Q';
					} else {
                        root.pollType         = 'A';
					}
					
					//jie:
					//alert(response.answers.length);
					root.numElements = response.answers.length;	
					
					//root.numElements = (root.pollType == "Q") ? response.Choices.length : response.Results.length;	
					
				
					
					
					root.questionId       = response.questionNo || null;
					//root.submitUrl        = response.SubmitURL  || null;
					root.submitUrl        = encodeURL("/enfamil/processAjaxPoll.do");
					var markup            = root.markup[root.pollSize][root.pollType]
				// set up the parent div
					var pollDiv           = document.createElement('div');
						pollDiv.className = root.pollType;
						pollDiv.id        = 'currentPoll';
					//Jie: if no error, process markup html
					if (response.msg == "") {
					// process markup by response attributes
						markup            = root.markupProc(markup, response);
					// insert processed markup, normalize and append to DOM
						pollDiv.innerHTML = markup;
					} else {
					    pollDiv.innerHTML = response.msg;
					}
						pollDiv.normalize();
						pollDiv           = root.parent.appendChild(pollDiv);
						root.parent.normalize();
					// if this is the question, add the necessary submission events
						if (root.pollType == 'Q') {
							root.setEvents(pollDiv, response);
						}
					// re-set image methods
						window.images.setImgMethods();
						//Jie: causing non secure /secure popup on IE
						if (root.pollType == 'Q') {
						    swf.setAll(9);
						}
				}
			// processes markup prior to insertion
				root.markupProc = function(markup, response) {
				
				
				if(root.numElements >= 5){ // If 5 elements, show the 5th one.
					markup = markup.replace('style="DISPLAY:none;"', '');
					markup = markup.replace('DISPLAY:none', '');
				}
//				debug(root.pollType + root.numElements);
                    switch(root.pollType) {
					//switch(response.Type) {
						case 'Q':
						// set title
						    //jie:
						    markup = markup.split('<!--poll title-->').join(response.questionText);
							//markup = markup.split('<!--poll title-->').join(response.Question);
						// set labels
							markup = markup.split('<!--label a-->').join(response.answers[0].answerText);
							markup = markup.split('<!--label b-->').join(response.answers[1].answerText);
							markup = markup.split('<!--label c-->').join(response.answers[2].answerText);
							markup = markup.split('<!--label d-->').join(response.answers[3].answerText);
							if(root.numElements >= 5) 	markup = markup.split('<!--label e-->').join(response.answers[4].answerText); 
						// set values
							markup = markup.split('<!--value a-->').join(response.answers[0].answerNo);
							markup = markup.split('<!--value b-->').join(response.answers[1].answerNo);
							markup = markup.split('<!--value c-->').join(response.answers[2].answerNo);
							markup = markup.split('<!--value d-->').join(response.answers[3].answerNo);
							if(root.numElements >= 5)	markup = markup.split('<!--value e-->').join(response.answers[4].answerNo);
						break;
						case 'A':
						// set title
						    markup = markup.split('<!--my month-->').join(response.timingStr);
							markup = markup.split('<!--poll title-->').join(response.questionText);
						// set follow up content
							markup = markup.split('<!--follow hdr-->').join(response.followupTitle);
							markup = markup.split('<!--follow cnt-->').join(response.followupText);
							markup = markup.split('<!--follow lnk-->').join(response.followupLinkText);
							markup = markup.split('<!--follow url-->').join(response.followupURL);
						// set response titles
							markup = markup.split('<!--description a-->').join(response.answers[0].answerText);
							markup = markup.split('<!--description b-->').join(response.answers[1].answerText);
							markup = markup.split('<!--description c-->').join(response.answers[2].answerText);
							markup = markup.split('<!--description d-->').join(response.answers[3].answerText);
							if(root.numElements >= 5)		markup = markup.split('<!--description e-->').join(response.answers[4].answerText);
							
						// set percentage values
							markup = markup.split('<!--percent a-->').join(response.answers[0].answerPercent);
							markup = markup.split('<!--percent b-->').join(response.answers[1].answerPercent);
							markup = markup.split('<!--percent c-->').join(response.answers[2].answerPercent);
							markup = markup.split('<!--percent d-->').join(response.answers[3].answerPercent);
							if(root.numElements >= 5)		markup = markup.split('<!--percent e-->').join(response.answers[4].answerPercent);

						// set percentage padding
							var maxPadding = (root.pollSize == 'small') ? 37 : 60;
							markup = markup.split('<!--padding a-->').join((parseInt(response.answers[0].answerPercent)/100) * maxPadding);
							markup = markup.split('<!--padding b-->').join((parseInt(response.answers[1].answerPercent)/100) * maxPadding);
							markup = markup.split('<!--padding c-->').join((parseInt(response.answers[2].answerPercent)/100) * maxPadding);
							markup = markup.split('<!--padding d-->').join((parseInt(response.answers[3].answerPercent)/100) * maxPadding);
							if(root.numElements >= 5)		markup = markup.split('<!--padding e-->').join((parseInt(response.answers[4].answerPercent)/100) * maxPadding);
						break;
					}
					return markup;
				}
			// adds support for poll submission to questions
				root.setEvents = function(pollDiv, response) {
					var submitBtn = document.getElementById('the_submit_button');//document.getElementById('currentPoll').getElementsByTagName('a')[0];
						submitBtn.onclick = function() {
						//alert(root.formValue);
							if (root.formValue && root.submitUrl) {
								var submitUrl  = root.submitUrl;
									submitUrl += '?surveyNo='+response.surveyNo+'&questionNo=' + response.questionNo + '&answerSetNo='+response.answers[0].answerSetNo +'&answerNo=' + root.formValue;
								root.getPoll(null, submitUrl);
								//hidePollOverlay(); // ADDED BY GUY TO HIDE OVERLAY
							}
						}
					var allRadios = document.getElementById('currentPoll').getElementsByTagName('ul')[0].getElementsByTagName('img');
						for (var i = 0; i < allRadios.length; i++) {
							var thisRadio = allRadios[i];
								thisRadio.onclick = function() {
									for (var ii = 0; ii < allRadios.length; ii++) {
										var thisBtn = allRadios[ii];
										if (thisBtn == this) {
											thisBtn.setSrc('/iwp/images/enf08/poll-smlg-q-radioOn.png');
											root.formValue = thisBtn.getAttribute('value');
										} else {
											thisBtn.setSrc('/iwp/images/enf08/poll-smlg-q-radioOff.png');
										}
									}
								}
						}
				}
				
				
				
				
				
			// initialize
			
				root.getPoll();
			}
			
			
	}


	function showPollOverlay(){
		document.getElementById('pollOverlay').style.display = "block";
	}
	function hidePollOverlay(){
		document.getElementById('pollOverlay').style.display = "none";
	}
