// JavaScript Document
function select_init (sel) {
	if (sel.options[0].text != "Not sure... How about we skip this one?") {
		sel.options[0].text = "Not sure... How about we skip this one?" ;
	}
}

function append_to_list (el) {
//these variables should be set by the programmer
	var dynamicSelect = document.vegvote.fave ;
//these variables are determined by input -- needs validation
	var state = el.name ;
	var selected = el.selectedIndex ;
/*
	When the select_init function is called by the onfocus event, the change in the text property causes IE6 to
	trigger the onchange event, which calls this function.  This appears to be an error in IE6, as onchange event
	handler (as documented in several places, including the W3 consortium) requires that the *value* be changed in
	order for an event to be triggered.  IE6 returns -1 for the variable "selected" when this erroneous trigger
	occurs.
*/
	if (selected != -1) {
		var favoriteName = el.options[selected].text ;
		var favoriteId = el.options[selected].value ;
	//start doing something
		for (i=0; i < dynamicSelect.options.length; i++) {
			if (dynamicSelect.options[i].id == state && favoriteId != 'null') {
				dynamicSelect.options[i].value = favoriteId ;
				dynamicSelect.options[i].text = state + ': ' + favoriteName ;
				dynamicSelect.options[i].disabled = false;
				break ;
			} else if (dynamicSelect.options[i].id == state) {
				dynamicSelect.options[i].value = 'null' ;
				dynamicSelect.options[i].text = state + ': none selected' ;
				dynamicSelect.options[i].disabled = true;			
				break ;
			}
		}
	}
}

function validate (form) {
	var base_fields = new Array(form.DC, form.MD, form.VA) ;
	var filled_fields = 0 ;
	for (i = 0; i < base_fields.length ; i++) {
		if (base_fields[i].value != 'null') {
			filled_fields++ ;
		}
	}

	if (filled_fields < 1) {
//no vote error	
		alert('You must cast a vote in at least one region.') ;
		return false ;
	} else if (filled_fields > 1 && (form.fave.value == null || form.fave.value == "" || form.fave.value == 'null') ) {
//error: voted for more than one region but didn't indicate an overall favorite
		alert('Please indicate which of your favorites is your overall favorite.') ;
		return false ;
	} else if (typeof(form.email.value) != 'string') {
//error: did not check yes or no for contest
		var boo = false ;
		for (i = 0; i < form.email.length ; i++ ) {
			if (form.email[i].checked) {
				boo = true ;
			}
		}
		if (boo == false) {
			alert('Please indicate whether or not you\'d like to be entered for a chance to win prizes.') ;	
			return false ;
		}
	}
	if (filled_fields != 3) {
		var answer = confirm("You have not voted for all three regions.  If you submit the form now, you will not be able to come back later to vote for the remaining regions.  Continue?") ;
		if (answer == true) {
			return true ;
		} else {
			return false ;
		}
	} else {
		return true ;
	}
}

function emailage (e) {
	var boo = true ;
	if (e.type == 'keypress') {
		var unicode = e.keyCode ? e.keyCode : e.charCode ;
		boo = (unicode == 32 ? true : false) ;
	}
	
	if (boo == true) {
		document.getElementById('emailage').innerHTML = '<label for="email">Email address</label><input type="text" name="email" id="email" maxlength="50" />' ;
		document.getElementById('email').focus() ;
	}
}