// Handles submission of Lottery Board (http://www.troppolotto.com)

var LotterySelections = new Array();
function iSort(a,b){
	return(a-b)
}

function ClearAll(){
	//remove all selections from array
	LotterySelections.splice(0,LotterySelections.length+1);
	//remove form fields
	for(i=0;i<7;i++){
		if(i/2==Math.floor(i/2)){
			document.getElementById('Selected_'+i).className = '';
		} else {
			document.getElementById('Selected_'+i).className = 'even ';
		}
		document.forms.LotteryBox['Selected_'+i].value='';
		document.getElementById('Selected_'+(i)).innerHTML = '?';
	}
	//remove selected buttons
	for(i=1;i<51;i++){
		document.getElementById('Selection_'+i).className='';
	}
	BlinkPlayAllButton();
}

function BlinkPlayAllButton(){
	var Prefix='';
}

/*
function ValidateLotteryBox(){
	ErrorMessages = '';
	for(i=0;i<7;i++){
		if(document.forms.LotteryBox['Selected_'+i].value.length==0){
			if(!ErrorMessages.length) ErrorMessages += ErrorMessage;
		}
	}
	if(ErrorMessages.length){
		alert(ErrorMessages);
		return false;
	} else {
		return true;
	}
}
*/

function quickPick(CurrentPick) {
	if(typeof CurrentPick == 'undefined'){
		ClearAll();
		CurrentPick = 0;
	}
	if(CurrentPick<7){
		newnum = GetRandomNumber();
		SelectNumber(newnum);
		CurrentPick++;
		setTimeout('quickPick('+CurrentPick+')',200);
	}
}

function GetRandomNumber(){
	newnum = Math.ceil(Math.random() * 50);
	bNewNumber = 1;
	if(LotterySelections.length){
		for(i=0;i<LotterySelections.length;i++){
			if(LotterySelections[i] == newnum){
				bNewNumber = 0;
			}
		}
		if(!bNewNumber){
			newnum = GetRandomNumber();
		}
	}
	return newnum;
}

function SelectNumber(thisNumber){
	bNewSelection = 1;
	for(i=0;i<LotterySelections.length;i++){
		if(LotterySelections[i] == thisNumber){
			LotterySelections.splice(i,1);
			document.getElementById('Selection_'+thisNumber).className='';
			LotterySelections = LotterySelections.sort(iSort);
			LotterySelections = LotterySelections.slice(0,LotterySelections.length);
			bNewSelection = 0;
		}
	}

	if(bNewSelection){
		if(LotterySelections.length>=7){
			// we are not doing this anymore
			//deactivateBoard();
		} else {
			LotterySelections.push(thisNumber);
			document.getElementById('Selection_'+thisNumber).className='selected';
			if (LotterySelections.length==7){
				// we are not doing this anymore
				//deactivateBoard();
			}
		}
	}

	for(i=0;i<7;i++){
		if(i/2==Math.floor(i/2)){
			newclass = '';
		} else {
			newclass = 'even ';
		}
		sInput = document.forms.LotteryBox['Selected_'+i];
		sItem = document.getElementById('Selected_'+i);
		if(LotterySelections[i]==null || LotterySelections[i] == undefined){
			sInput.value='';
			sItem.innerHTML = '?';
			sItem.className =newclass;
		} else {
			document.forms.LotteryBox['Selected_'+i].value = LotterySelections[i];
			sItem.innerHTML = LotterySelections[i];
			sItem.className = newclass+'selected';
		}
	}

	// if 7 chosen, move to the next page
	if( LotterySelections.length >= 7 ){
		if( wOverlay == 'new' ){
			var LotterySelectionsCSV = LotterySelections.join();
			jQuery('#new_upicks').val(LotterySelectionsCSV);
			jQuery('#rtn_upicks').val(LotterySelectionsCSV);
			jQuery('#new_overlay').overlay({api:true,closeOnClick:false,expose:"#111"}).load();
		}
		else if( wOverlay == 'rtn' ){
			var LotterySelectionsCSV = LotterySelections.join();
			jQuery('#new_upicks').val(LotterySelectionsCSV);
			jQuery('#rtn_upicks').val(LotterySelectionsCSV);
			jQuery('#rtn_overlay').overlay({api:true,closeOnClick:false,expose:"#111"}).load();
		}
		else if( wOverlay == 'sess' ){
			setTimeout('document.LotteryBox.submit()',800);
		}
	}
	//BlinkPlayAllButton();
}
