function isBlank(s){
	// Returns true if a string contains only non-whitespace characters.
	if((s == null) || (s == "") || (s.length == 0)) 
		return true;
			
    for(var i = 0; i < s.length; i++) {
        var c = s.charAt(i);
        if ((c != ' ') && (c != '\n') && (c != '\t')) return false;
    }
    return true;
}  // isBlank(s)

function selectionMade() {
	var f = document.frmVote;
	if(!f) { return false; }	
	for(var i = 0; i < f.optVote.length; i++){
		if(f.optVote[i].checked == true) {
			return true;	
		}
	}
	alert('Vote selection is required.');
	return false;
}  // selectionMade()

function noPreviousVote() {
	var f = document.frmVote;
	var hasNotVotedBefore = true;
	
	if(f){
		if(f.Database){
			var cookieName = f.Database.value;
			if(!isBlank(document.cookie) && !isBlank(cookieName)) {
				var cookieExists = document.cookie.indexOf(cookieName);
				if(cookieExists != -1) {
					alert('Sorry. You may only vote once.');
					hasNotVotedBefore = false;
				}
			}
		}	
	}
	return hasNotVotedBefore;
}  // noPreviousVote()

function votePeriodOk(){
	var f = document.frmVote;
	var periodOk = true;

	var dateNow = new Date();
	var expDate = f.EndDate.value;

	if(!isBlank(expDate) && (expDate.length == 10)) {
			var m = parseInt(expDate.substring(0,2));
			var d = parseInt(expDate.substring(3,5));
			var y = parseInt(expDate.substring(6,10));

			expDate = new Date(y,m - 1,d,23,59,59);
			
			if(dateNow.getTime() > expDate.getTime()) {
				if(expDate.toLocaleDateString)
					{ alert('Sorry this poll expired on:\n' + expDate.toLocaleDateString()); }
				else { alert('Sorry this poll expired on:\n' + expDate.toString()); }
				periodOk = false;
			}
	}
	else
		periodOk = false;
		
	return periodOk;		
}  // votePeriodOk()

function disableSubmit() {
	if(document.frmVote) {
		if(document.frmVote.btnSubmit) 
			document.frmVote.btnSubmit.disabled = true;
	}
} // disableSubmit()

function validateForm(){   
	var doSubmit = false;
	
	if(votePeriodOk()) {
		if(noPreviousVote()) {
			if(selectionMade()){
				doSubmit = true;
			}
		}
		else { disableSubmit();	}
	}
	else { 	disableSubmit(); }
	
	if(doSubmit){
		// Disable error raising - posting error here should be considered trivial & not stop submission
		window.onerror = function() { return true; }   
		// Must pause a bit before disabling the submit button to avoid run time JavaScript error
		window.setTimeout("disableSubmit()", 100);
	}
	return doSubmit;
}  // validateForm()

function castVote()
{
	if(validateForm())
		document.frmVote.submit();
} // castVote()
