
var poll_response;
var voter_response;
var WEB_ROOT = '';

function sendPoll() {
	//fetchResults(); return;
	var frm = document.forms['pollForm'];
	var pid = frm.poll_id.value;
	//var poll_answer = getCheckedValue( document.forms['pollForm'].poll_answer );
	var name = frm.voter_name.value;
	var email = frm.real_email.value;
	var zip = frm.zip.value;
	var sharefb = document.getElementById( 'sharefb' );
	var _data = 'poll_id=' + pid + // + '&poll_answer=' + poll_answer +
		'&voter_name=' + name + '&real_email=' + email + '&zip=' + zip;

	if ( null != sharefb ) {
		_data = _data + '&sharefb=on';
	}

	// Get the value of each answer. Since multiple questions
	// are allowed, this means that more than one radio button group
	// may be present. Cycle over the form and get the unique groups
	var uniqueRadioGroups = new Array();
	for ( var i = 0; i < frm.elements.length; i++ ) {
		if ( frm.elements[i].type == 'radio' && frm.elements[i].name.indexOf( 'poll_answer' ) != -1 ) {
			if ( !in_array( frm.elements[i].name, uniqueRadioGroups ) ) {
				uniqueRadioGroups.push( frm.elements[i].name.toString() );
			}
		}
	}
	for ( var i = 0; i < uniqueRadioGroups.length; i++ ) {
		if ( getCheckedValue( frm.elements[ uniqueRadioGroups[i] ] ) == '' ) {
			alert( 'Please select an answer' );
			hideVoterInfoBoxEnablePollForm();
			return false;
		}
		_data = _data + '&' + uniqueRadioGroups[i] + '=' + getCheckedValue( frm.elements[ uniqueRadioGroups[i] ] );
	}
	
	$.ajax({
		type: 'POST',
		url: WEB_ROOT + '/ajax/process-poll',
		data: _data,
		success: function( msg ) {
				poll_response = msg;
				
				if ( msg.indexOf( 'status=success' ) != -1 ) {
					if ( msg.indexOf( 'status=voted' ) != -1 ) {
						alert( 'This computer has already voted. If you are a new voter, please vote from another computer.' );
						enableForm();
						return false;
					}
					$('#getVoterInfoBox').fadeTo('slow', 0);
					setTimeout( 'fetchResults()', 1000 );
				} else {
					var resp_sep = poll_response.split( '&' );
					var err_msg = '';
					for ( var i = 0; i < resp_sep.length; i++ ) {
						if ( resp_sep[i].indexOf( 'msg=' ) != -1 ) {
							var tmp = resp_sep[i].split( '=' );
							err_msg = tmp[1];
						}
					}
					
					if ( err_msg.length <= 0 ) {
						alert( poll_response );
					} else {
						alert( 'Received response: \n' + eugenesReplace( '+', ' ', err_msg ) );
						
					}
				}
			},
		error: function( xml, stat, err ) {
				alert( 'SP: An error occurred while sending the request. Please check your Internet connectivity.' );
			}
	});
	
	return false;
}

function fetchResults() {
	var pid = document.forms['pollForm'].poll_id.value;
	var sharefb = document.getElementById( 'sharefb' );
	$.ajax({
		type: 'POST',
		url: WEB_ROOT + '/ajax/get-results',
		data: 'poll_id=' + pid +
			( sharefb != null && sharefb.checked ? '&sharefb=on' : '' ),
		success: function( msg ) {
			//alert(msg); return;
			$( '#pollContainer' ).html( msg );
			$('#pollContainer').show();

			/*if ( null != sharefb ) {
				if ( sharefb.checked ) {
					FB.Connect.streamPublish( "", {}, {}, null, 'Share this with your friends' );
				}
			}*/
				
			/*if ( elem != null ) {
				document.body.removeChild( elem );
				elem = null;
			}*/
		},
		error: function( xml, stat, err ) {
				alert( 'FR: An error occurred while sending the request. Please check your Internet connectivity.' );
			}
	});
	
	return false;
}

function disablePollFormShowVoterForm() {
	var frm = document.forms['pollForm'];

	// Check if there are any options that aren't checked
	// To do this, we need to find out the unique radio
	// button groups
	var uniqueRadioGroups = new Array();
	for ( var i = 0; i < frm.elements.length; i++ ) {
		if ( frm.elements[i].type == 'radio' && frm.elements[i].name.indexOf( 'poll_answer' ) != -1 ) {
			if ( !in_array( frm.elements[i].name, uniqueRadioGroups ) ) {
				uniqueRadioGroups.push( frm.elements[i].name.toString() );
			}
		}
	}
	for ( var i = 0; i < uniqueRadioGroups.length; i++ ) {
		if ( getCheckedValue( frm.elements[ uniqueRadioGroups[i] ] ) == '' ) {
			alert( 'Please select an answer' );
			return false;
		}
	}

	frm.fakesubmit.disabled = true;
	
	for ( var i = 0; i < frm.elements.length; i++ ) {
		if ( frm.elements[i].name.indexOf('poll_answer') != -1 ) {
			frm.elements[i].disabled = true;
		}
	}
	$('#getVoterInfoBox').vCenter();
	document.getElementById( 'getVoterInfoBox' ).style.opacity = 0;
	document.getElementById( 'getVoterInfoBox' ).style.filter = 'alpha(opacity=0)';
	document.getElementById( 'getVoterInfoBox' ).style.display = "block";
	document.getElementById( 'getVoterInfoBox' ).style.zIndex = "20000";
	$('#getVoterInfoBox').fadeTo('slow', 1);
}

function hideVoterInfoBoxEnablePollForm() {
	$('#getVoterInfoBox').fadeTo('slow', 0);
	enableForm( document.forms['pollForm'] );
	setTimeout( 'document.getElementById(\'getVoterInfoBox\').style.display = \'none\'', 1000 );
}

function enableForm() {
	document.forms['pollForm'].fakesubmit.disabled = false;
	for ( var i = 0; i < document.forms['pollForm'].elements.length; i++ ) {
		if ( document.forms['pollForm'].elements[i].name.indexOf('poll_answer') != -1 ) {
			document.forms['pollForm'].elements[i].disabled = false;
		}
	}
}
// return the value of the radio button that is checked
// return an empty string if none are checked, or
// there are no radio buttons
function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

function eugenesReplace( srch, repl, string ) {
	var newString = string;
	while ( newString.indexOf( srch ) != -1 ) {
		newString = newString.replace( srch, repl );
	}
	
	return newString;
}

/*function ejSendVoterSuccess() {
	window.scrollTo(0,0);
	fetchResults();
	$('#ejDonateContainerRight').show();
}*/

function ejGetCookie( c_name ) {
	if (document.cookie.length>0) {
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start != -1) {
			c_start=c_start + c_name.length+1;
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) c_end=document.cookie.length;
			
			return unescape(document.cookie.substring(c_start,c_end));
		}
	}
	return "";
}

function ejShowPollResults() {
	if ( ejGetCookie( 'voted' ) != "" ) {
		// show the results
		$('#ejSeeResultsContainer').hide();
		$('#ejPollResultsContainer').show();
	} else {
		alert( 'Please cast your vote to see the poll results.' );
	}
}

