var pollid = 0; var name = ''; var vcount = 0;
var job = ''; var state = ''; var ajax = '';
var iArr = new Array(); var tArr = new Array();
var iCook=''; var tCook='';  var voted=0;

function loadMain() {
	name =$F('poll_name');
	vcount = parseInt($F('poll_votecount'));
	pollid = parseInt($F('poll_id'));
	$('preview').style.display = 'none';
	$('mainpage').style.display = 'block';
	voted = (getCookie('voted') == 'yes')?1:0;
	loadSelectedProducts();
}

function loadPreview() {
	$('preview').style.display = 'block';
	$('mainpage').style.display = 'none';
	getPollCookie();
	$("previewtitle").innerHTML = '';
	$("list").innerHTML ='';
	if(iCook != '') {
		for(a=0; a<iArr.length; a++) {
			$("list").innerHTML += '<div class="subsection clearfix vote checked"><div class="left"><h1>Nominate</h1><a class="checkbox checked"></a></div><div class="right"><h1>Article '+(a+1)+'</h1><h3><a href="/pr/product.jsp?id='+iArr[a]+'">'+tArr[a]+'</a></h3></div></div>';
			$('previewtitle').innerHTML = '<p>Below is the list of articles you chose to nominate. You can edit the list by clicking on the Edit button above.  Please make all selections before voting. Click on the Vote Now button below to cast your vote. </p>';
		}
	}
	else {$("list").innerHTML = '<p><b>You have not nominated any article.</b><p/>'}
	if(getCookie('voted') == 'yes' ) {
		$('previewtitle').innerHTML = '<p>You have nominated following articles:</p>';
		$('submitmessage').innerHTML = '<p><b><font style="font-size:16px;">Thank you for your vote!</font> We will publish the results in a few weeks.</b></p>';
		$('caption').innerHTML = 'You have already voted.';
		if(getCookie('occupation') != '') {$(getCookie('occupation')).selected = true;}
		if(getCookie('state') != '') {$(getCookie('state')).selected = true;}
		$('occupation').disabled = true;
		$('statelist').disabled = true;
	}
}
function getSelectedProducts(ele) {
	if(getCookie('voted') == 'yes') {alert("Your choices have already been recorded. Please only vote once.");return;}
	getPollCookie();
	updatedIds = '';    updatedTitles = '';
	if(ele.className == 'checkbox checked') {  //unchecked
		$('article'+ele.id).className = 'subsection clearfix vote';
		ele.className = 'checkbox';
		for(i=0; i<iArr.length; i++) {if(iArr[i] != ele.id) updatedIds += iArr[i]+',';}
		for(j=0; j<tArr.length; j++) {if(tArr[j] != ele.name) updatedTitles += tArr[j]+'|';}
		iCook = (updatedIds != '')?updatedIds.substring(0, updatedIds.length-1):'';
		tCook = (updatedTitles != '')?updatedTitles.substring(0, updatedTitles.length-1):'';
	}
	else { //checked
		if(iArr.length >= vcount ) {alert('You have nominated '+vcount+' articles. If you wish to vote for this article, please update your previous selections.');return;}
		iCook += iCook == '' ? ele.id : ','+ele.id;
		tCook += tCook == '' ? ele.name : '|'+ele.name;
		$('article'+ele.id).className = 'subsection clearfix vote checked';
		ele.className = 'checkbox checked';
	}
	iArr =iCook.split(','); 
	$('articlecount').innerHTML = (iCook != '')?(vcount-iArr.length):vcount;
	setPollCookie('ids', iCook);
	setPollCookie('titles', tCook);
}

function loadSelectedProducts() {
	getPollCookie();
	voted = (getCookie('voted') == 'yes')?1:0;
	for(j=0; j<iArr.length; j++) {
		if(voted && $('article'+iArr[j])!=null) $('article'+iArr[j]).className = 'subsection clearfix vote disabled';
		else if($('article'+iArr[j])!=null) $('article'+iArr[j]).className = 'subsection clearfix vote checked';
		if($(iArr[j])!=null)$(iArr[j]).className = 'checkbox checked';
	}
	if($('articlecount')!=null && !voted) $('articlecount').innerHTML = vcount-iArr.length;
}

function submitVote() {
	getPollCookie();
	if(iCook== '') {alert("Please select an article.");return;}
	url='/poll/xml/pollxml.jsp?action=visitor&POLLID='+escape(pollid)+'&SESSIONID='+escape(getCookie('JSESSIONID'))+'&USERCLASSIFICATIONID='+escape(job)+'&STATE='+escape(state)+'&ms=' + new Date().getTime();
	ajax = new Ajax.Request(url, {
		method: 'get',
		contentType:'application/xml',
		onSuccess: function(transport) {getVisitorID( transport.responseXML );},
		onFailure: function() {alert('There was an error. \nPlease reload the page.')}
	});
}

function getVisitorID(xmldata){
	var visitorid_tag =  xmldata.getElementsByTagName('visitorid');
	visitorid= visitorid_tag[0].firstChild!=null? visitorid_tag[0].firstChild.nodeValue: '';
	i=0;
	getPollCookie();
	url='/poll/xml/pollxml.jsp?action=save&pvid='+escape(visitorid)+'&pid='+escape(iCook)+'&ms=' + new Date().getTime();
	ajax = new Ajax.Request(url, {
		method: 'get',
		contentType:'application/xml',
		onSuccess: function(transport) {lastStep();},
		onFailure: function(transport) {alert('There was an error. \nPlease reload the page.');pe.stop();}
	});
}
function lastStep(){
	setPollCookie('voted', 'yes');
	setPollCookie('occupation',job);
	setPollCookie('state',state);
	$('occupation').disabled = true;
	$('statelist').disabled = true;
	$('voteButton').style.display = 'none';
	$('submitmessage').innerHTML = '<p><b><font style="font-size:16px;">Thank you for your vote!</font> We will publish the results in a few weeks.</b></p>';
	$('caption').innerHTML = 'You have already voted.';
}
function saveOccupation(element) {job = element.value;}
function saveState(element) {state = element.value;}
function setPollCookie(c_name,value){document.cookie=c_name+ "=" +escape(value)+";expires="+ new Date($F('poll_enddate')).toGMTString();}
function getPollCookie(){ iCook = getCookie('ids'); tCook = getCookie('titles'); iArr = iCook!=''?iCook.split(','):new Array(); tArr = tCook!=''?tCook.split('|'):new Array();}