function contestAudioVote(csi_id, label) {
	document.getElementById('contestAudioVoteForm').style.display = 'block';
	document.getElementById('naming').innerHTML = '<h3>You are about to vote for: ' + label + '</h3>';
	lastAudio = csi_id;
	document.getElementsByName('csi_id')[0].value = csi_id;
}

function contestAudioCancel() {
	document.getElementById('contestAudioVoteForm').style.display = 'none';
	
}

function contestAudioVoteSuccess() {
	csi_id				= document.getElementsByName('csi_id')[0].value;
	flag_share_info		= document.getElementsByName('flag_share_info')[0].value;
	
	$.post("{$_SERVER['REQUEST_URI']}",
		{
			reaction: 'vote', 
			csi_id: csi_id, 
			flag_share_info: flag_share_info
		},
		contestAudioVoteCallback,
		'text');
}

function contestAudioVoteCallback(data, textStatus) {
	// if not successful, show error message
	if( data.indexOf('Error') >= 0 ) {
		alert(data);
	} else { // else hide all voting links
		confirm('Thanks for your participation, your vote has been saved successfully!','');		
	}
		
	contestAudioCancel();
	
	ballots=document.getElementsByName('ballots');	
	for(var i=0;i<ballots.length;i++){
		ballots[i].innerHTML = 'You have already voted.';
		ballots[i].onclick = null;
	}
}


function contestCheckRequiredFields() {
	var length	= contestRequiredFields.length;
	var missing = new Array();
	
	for( i=0; i < length; i++ ) {
		elemId		= contestRequiredFields[i];
		elemType	= contestRequiredFieldTypes[i];
		
		if( elemId == '' ) {
			continue;
		}
		
		switch( elemType ) {
			case 'checkBoxes':
				countChecked	= 0;
				$('.contestCheckBoxesInput').each(function() {
					// check it is within the group we are interested in (in case there is more than one)
					if( this.id.indexOf(elemId) < 0 ) {
						return;
					}
					if( this.checked ) {
						countChecked++;
					}
				});
				if( countChecked < 1 ) {
					$('label[for="'+elemId+'"]').attr('class','error');
					missing[missing.length] = $('label[for="'+elemId+'"]').text();
				}
				break;
				
			case 'photoUpload':
			elem	= $('#'+elemId)[0];
			elem2	= $('#'+elemId+'_caption')[0];
			if( elem == null || elem2 == null ) {
				continue;
			}
			if( elem.value == '' ) {
				$('label[for="'+elemId+'"]').attr('class','error');
				missing[missing.length] = elem.title;
			}
			if( elem2.value == '' ) {
				$('label[for="'+elemId+'_caption"]').attr('class','error');
				missing[missing.length] = 'Caption for '+elem.title;
			}
			break;
				
			default:
				elem	= $('#'+elemId)[0];
				if( elem == null ) {
					continue;
				}
				if( elem.value == '' ) {
					$('label[for="'+elemId+'"]').attr('class','error');
					missing[missing.length] = elem.title;
				}
		}
	}
	if( missing.length > 0 ) {
		alert("You missed filling out the following required fields:\n- "+missing.join("\n- "));
		return false;
	}
	return true;
}
