// AJAX function for GC:Voting System (Gustavo Campos - www.guscampos.com)

var http = false;
var count = 0;
    if(navigator.appName == "Microsoft Internet Explorer") {
      http = new ActiveXObject("Microsoft.XMLHTTP");
    } else {
      http = new XMLHttpRequest();
    }
    
	function prepare_vote(poll_id,poll_option_id) {		
		
		document.forms[''+poll_id+''].voted_option.value = poll_option_id;		
	}
	
	function do_vote(poll_id) {	

		var voted_option = document.forms[''+poll_id+''].voted_option.value;
		var sid = document.forms[''+poll_id+''].sid.value;
		/*var code = document.forms[''+poll_id+''].seccode.value;*/
		if(voted_option == '') {
			alert('Please select an option');
			return false;
		}
		
		document.forms[''+poll_id+''].disabled = true;
		document.forms[''+poll_id+''].voteBtn.value = 'Voting Now!';
		document.forms[''+poll_id+''].voteBtn.disabled = true;
		//do the ajax thing
		http.abort();
      	http.open("POST", '/vote.php?action=vote&poll_id='+poll_id+'&poll_option='+voted_option+'&sid='+sid+'&seccode=', true);
      	http.onreadystatechange=function() {
        	if(http.readyState == 4) {
				if(http.responseText == 'code') {
					/*alert('Security Code did not match, please try again' + code);*/
					document.forms[''+poll_id+''].disabled = false;
				} else {
	          		$j('#vote-form').html(http.responseText);
				}
        	}
     	}

      	http.send(null); 
	}
	
	function do_vote_sm(poll_id) {	
		
		
		if(count >= 1) {
			alert('We are processing your vote, please wait!');
			return false;
		}
		
		var voted_option = document.forms[''+poll_id+''].voted_option.value;
		var sid = document.forms[''+poll_id+''].sid.value;
		/*var code = document.forms[''+poll_id+''].seccode.value;*/
		if(voted_option == '') {
			alert('Please select an option');
			return false;
		}
		count++;
		//do the ajax thing
		http.abort();
      	http.open("POST", '/vote.php?action=vote&poll_id='+poll_id+'&poll_option='+voted_option+'&sid='+sid+'&seccode=&f=1&d=0.5', true);
      	http.onreadystatechange=function() {
        	if(http.readyState == 4) {
	          	window.location.reload();
        	}
     	}

      	http.send(null); 
	}
	
