function makeSplitSafe(text) {
	text = text.replace(/\;/g, '__SEMICOLON__');
	text = text.replace(/\:/g, '__COLON__');
	
	return text;
}

function updateCriticSelections(select) {
	//if any other selects in that category have this same priority, overwrite
	var category = false;
	if (select.id.match(/^ballot_(\d+)_\d+$/)) {
		category = RegExp.$1;
	}
	var priority = select.value;

	var selects = document.getElementsByTagName('select');
	for (var i=0; i < selects.length; i++) {
		var this_select = selects[i];
		
		if (this_select.id.match(/^ballot_(\d+)_\d+$/)) {
			var this_category = RegExp.$1;
			var this_priority = this_select.value;
			
			if (this_category == category && this_select.id != select.id && this_priority == priority) {
				this_select.selectedIndex = 0;
			}
		}
	}
}

function updateInput(element) {
	var type = 'name';
	if (element.id.match(/^nomination_email_/)) {
		type = 'email';
	}
	
	element.style.color = 'black';
	
	if (element.value == '(enter name here)' || element.value == '(enter link here)') {
		var new_value = '';
		element.value = new_value;
	}
}

function validateBallot(user_type) {
	//is user's name populated?
	if (!document.getElementById('username').value) {
		alert('You must provide your name. You are not some sort of spy.');
		return false;
	}
	
	//is user's email populated, and valid?
	if (!document.getElementById('useremail').value) {
		alert('You must provide your email address.');
		return false;
	}
	
	if (!document.getElementById('useremail').value.match(/^\S+\@\S+.\S+/)) {
		alert('You must provide a valid email address.');
		return false;
	}
	
	//assemble selections
	var votes = new Array();
	
	if (user_type == 'plebe') {
		var inputs = document.getElementsByTagName('input');
		for (var i=0; i < inputs.length; i++) {
			var this_input = inputs[i];
			
			if (this_input.id.match(/^ballot_(\d+)_(\d+)$/) && this_input.checked) {
				var cat_id = RegExp.$1;
				var ballot_id = RegExp.$2;
				
				if (typeof(votes[cat_id]) == 'undefined') {
					votes[cat_id] = new Array();
				}
				votes[cat_id][0] = ballot_id;
			}
		}
	} else if (user_type == 'critic') {
		var inputs = document.getElementsByTagName('select');
		for (var i=0; i < inputs.length; i++) {
			var this_input = inputs[i];
			
			if (this_input.id.match(/^ballot_(\d+)_(\d+)$/) && this_input.value != '-') {
				var cat_id = RegExp.$1;
				var ballot_id = RegExp.$2;
				
				if (typeof(votes[cat_id]) == 'undefined') {
					votes[cat_id] = new Array();
				}
				
				var index = this_input.value - 1;
				votes[cat_id][index] = ballot_id;
			}
		}
	}

	var votes_str = '';
	for (var this_cat_id in votes) {
		var choices = votes[this_cat_id];
		
		votes_str += this_cat_id + ':';
		
		for (var i=0; i < choices.length; i++) {
			var this_choice = choices[i];
			
			 votes_str += this_choice + ',';
		}
		
		votes_str = votes_str.replace(/,$/, ';');
	}
	votes_str = votes_str.replace(/;$/, '');

//	x_sajax_saveBallot(document.getElementById('username').value, document.getElementById('useremail').value, votes_str, user_type, validateBallot_cb);
}

function validateBallot_cb (return_val) {
	if (return_val == 'ok') {
		alert("Thanks! Your vote has been saved.");
//		alert("How does it feel to be a tastemaker?");
//		alert("Pretty good, I bet.");
	} else if (return_val == 'repeat') {
		alert("Sorry, it looks like you've voted already, you scamp.");
	} else if (return_val == 'critic_extra') {
		alert("Sorry, it looks like you've entered more than two options in one category. Please amend your vote, you rascal.");
	} else {
		alert("Sorry, some freaky sort of error has occurred. Would you be so kind as to let us know via email?");
	}
}
















function validateForm() {
	//is user's name populated?
	if (!document.getElementById('username').value) {
		alert('You must provide your name. You are not some sort of spy.');
		return false;
	}
	
	//is user's email populated, and valid?
	if (!document.getElementById('useremail').value) {
		alert('You must provide your email address.');
		return false;
	}
	
	if (!document.getElementById('useremail').value.match(/^\S+\@\S+.\S+/)) {
		alert('You must provide a valid email address.');
		return false;
	}
	
	//build list of nominations
	var noms = new Object();
	
	var inputs = document.getElementsByTagName('input');
	var noms_exist = false;
	
	for (var i=0; i < inputs.length; i++) {
		var this_input = inputs[i];
		if (this_input.id.match(/^nomination_(\w+)_(\d+)_(\d+)$/) && this_input.value && !this_input.value.match(/^\(enter \w+ here\)$/)) {
			var nom_type = RegExp.$1;
			var cat_id = RegExp.$2;
			var col_num = RegExp.$3;
			
			if (typeof(noms[cat_id]) == 'undefined') {
				noms[cat_id] = new Object();
			}
			
			if (typeof(noms[cat_id][col_num]) == 'undefined') {
				noms[cat_id][col_num] = new Object();
			}
			
			
			noms[cat_id][col_num][nom_type] = this_input.value;
			noms[cat_id][col_num]['id'] = cat_id;
			
			noms_exist = true;
		}
	}
	
	//are there any nominations at all?
	if (!noms_exist) {
		alert("You must make some nominations, otherwise this doesn't really make any sense.");
		return false;
	}
	
	//is each name/link pair complete
	var bad_boys = new Object();
	var bad_boy_count = 0;
	
	for (var this_id in noms) {
		var this_cols = noms[this_id];
		
		for (var this_col in this_cols) {
			var this_nom = noms[this_id][this_col];
			
			if (typeof(this_nom['name']) == 'undefined') {
				if (typeof(bad_boys[this_id]) == 'undefined') {
					bad_boys[this_id] = new Object();
				}
			
				bad_boys[this_id][this_col] = 'name';
				++bad_boy_count;
				
				document.getElementById('nomination_name_' + this_id + '_' + this_col).className += ' nomination_input_error';
			} else {
				document.getElementById('nomination_name_' + this_id + '_' + this_col).className = ' nomination_input';
			}
			
			if (typeof(this_nom['link']) == 'undefined') {
				if (typeof(bad_boys[this_id]) == 'undefined') {
					bad_boys[this_id] = new Object();
				}
			
				bad_boys[this_id][this_col] = 'link';
				++bad_boy_count;
				
				document.getElementById('nomination_link_' + this_id + '_' + this_col).className += ' nomination_input_error';
			} else {
				document.getElementById('nomination_link_' + this_id + '_' + this_col).className = ' nomination_input';			
			}
		}
	}
	
	if (bad_boy_count > 0) {
		var msg = "Every nomination must contain both a name and a web link. Please complete the following categories:\n\n";
		
		for (var cat_id in bad_boys) {
			var this_cols = bad_boys[cat_id];
			
			for (var col_num in this_cols) {
				var this_bad_boy = bad_boys[cat_id][col_num];
							
				var descr = cats[cat_id]['desc'];
				var type = cats[cat_id]['type'];
				
				msg += '* a ' + bad_boys[cat_id][col_num] + ' for "' + descr + '" in column ' + col_num + "\n\n";

			}
		}
		
		alert(msg);
		return false;
	}

	//build string version of nominations
	var noms_str = '';
	for (var cat_id in noms) {
		for (var col_num in noms[cat_id]) {
			var this_link = noms[cat_id][col_num]['link'];
			var this_name = noms[cat_id][col_num]['name'];
			noms_str += makeSplitSafe(cat_id) + '::' + makeSplitSafe(this_name) + '::' + makeSplitSafe(this_link) + ';;';
		}
	}
	
	noms_str = noms_str.replace(/;;$/, '');
	
//	x_sajax_saveNominations(document.getElementById('username').value, document.getElementById('useremail').value, noms_str, validateForm_cb);
}

function validateForm_cb (return_val) {
	if (return_val == 'ok') {
		alert("Thanks! Your nominations have been saved.");
//		alert("How does it feel to be a tastemaker?");
//		alert("Pretty good, I bet.");
	} else {
		alert("Sorry, some freaky sort of error has occurred. Would you be so kind as to let us know via email?");
	}
}