var outofmemory=0;

function randomizeSelectedVerbs() {
	document.conjugator.answer.value="";
	document.conjugator.answer.focus(); 
	pronoun=document.conjugator.pronouns.selectedIndex;
    	verb=Math.floor(Math.random()*selectedinfinitive.length)
  	document.conjugator.verb.value=selectedinfinitive[verb];
	while (!infinitive[verb]) {
		verb=Math.floor(Math.random()*infinitive.length);
	}

	if (document.conjugator.pronouns.selectedIndex==0) {
		pronoun=Math.floor(Math.random()*20);

	}
	
	if (document.conjugator.pronouns.selectedIndex==1 || approveVosotros=="no") {
		pronoun=Math.floor(Math.random()*20);
		while (pronoun==8 || pronoun==19) {
			pronoun=Math.floor(Math.random()*20);
		}
	}
	
	if (pronoun==2 || pronoun==12 || pronoun==14 || pronoun==16) {
		solution=selectedyo[verb];
		document.conjugator.pronoun.value="yo";
		}
	if (pronoun==3 || pronoun==13 || pronoun==15 || pronoun==17) {
		solution=selectedtu[verb];
		document.conjugator.pronoun.value="tú";
		}
	if (pronoun==0 || pronoun==4) {
		solution=selectedella[verb];
		document.conjugator.pronoun.value="él";
		}
	if (pronoun==5) {
		solution=selectedella[verb];
		document.conjugator.pronoun.value="ella";
		}
	if (pronoun==6) {
		solution=selectedella[verb];
		document.conjugator.pronoun.value="usted";
		}
	if (pronoun==7 || pronoun==18) {
		solution=selectednosotros[verb];
		document.conjugator.pronoun.value="nosotros";
		}
	if (pronoun==8 || pronoun==19) {
		solution=selectedvosotros[verb];
		document.conjugator.pronoun.value="vosotros";
		}
	if (pronoun==1 || pronoun==9) {
		solution=selectedellas[verb];
		document.conjugator.pronoun.value="ellas";
		}
	if (pronoun==10) {
		solution=selectedellas[verb];
		document.conjugator.pronoun.value="ellos";
		}
	if (pronoun==11) {
		solution=selectedellas[verb];
		document.conjugator.pronoun.value="ustedes";
		}
	if (alltenses == "yes") {
		whichTenses();
		solution=solution[tenseNumber];
	}
	if (solution == "none") {
		if (document.conjugator.tenselist[0].selected) {
			numTenses = tenses.length;
		}
		else {
			numTenses = 0;
			for (i=1;i<=tenses.length;i++) {
				if (document.conjugator.tenselist[i].selected) {
					numTenses++;		
				}
			}
		}
		var numOptions = selectedyo.length * numTenses;
		for (i=0;i<selectedyo.length;i++) {
			for(h=0;h<selectedyo[i].length;h++) {
				if (selectedyo[i][h] == "none") {
					outofmemory++;	
				} 
			}
		}
		if (outofmemory == numOptions) {
			outofmemory = 0;
			document.conjugator.verb.value="";
			document.conjugator.pronoun.value="";
			document.conjugator.tense.value="";
			alert("The verb and tense combination that you selected is not available for this exercise. Please change your selections.");
			changeSettings();

		}
		else {
			outofmemory = 0;
			randomizeSelectedVerbs() ;
		}	
		
	}
	return;
}	

function randomizeSelectedCommands() {        

	document.conjugator.answer.value="";        
	document.conjugator.answer.focus();         
	pronoun=document.conjugator.pronouns.selectedIndex;        
	verb=Math.floor(Math.random()*selectedinfinitive.length)        
	document.conjugator.verb.value=selectedinfinitive[verb];                

	if (document.conjugator.pronouns.selectedIndex==0) {                
		pronoun=Math.floor(Math.random()*types.length);                
		               	               
	}              
	
	if (document.conjugator.pronouns.selectedIndex>0){   
		pronoun=document.conjugator.pronouns.selectedIndex-1;             
		                    
		}        
	solution=selectedcommand[verb][pronoun];     
	document.conjugator.pronoun.value=types[pronoun];  

}

function randomizeAllCommands() {        
	document.conjugator.answer.value="";        
	document.conjugator.answer.focus();         
	pronoun=document.conjugator.pronouns.selectedIndex;        
	verb=Math.floor(Math.random()*infinitive.length);        
	document.conjugator.verb.value=infinitive[verb];                
	
	if (document.conjugator.pronouns.selectedIndex==0) {                
		pronoun=Math.floor(Math.random()*types.length);                
		               	               
	}              
	
	if (document.conjugator.pronouns.selectedIndex>0){   
		pronoun=document.conjugator.pronouns.selectedIndex-1;             
		                    
		}        
	solution=commandConjugations[verb][pronoun];     
	document.conjugator.pronoun.value=types[pronoun];  
	return;

}



function randomizeAllVerbs() {
       	document.conjugator.answer.value="";
	document.conjugator.answer.focus();
    	pronoun=document.conjugator.pronouns.selectedIndex;
    	verb=Math.floor(Math.random()*infinitive.length);
	while (!infinitive[verb]) {
		verb=Math.floor(Math.random()*infinitive.length);
	}
	document.conjugator.verb.value=infinitive[verb];
	
	if (document.conjugator.pronouns.selectedIndex==0) {
		pronoun=Math.floor(Math.random()*20);

	}
	
	
	if (document.conjugator.pronouns.selectedIndex==1 || approveVosotros=="no") {
		pronoun=Math.floor(Math.random()*20);
		while (pronoun==8 || pronoun==19) {
			pronoun=Math.floor(Math.random()*20);
		}
	}
	
	if (pronoun==2 || pronoun==12 || pronoun==14 || pronoun==16) {
		solution=yo[verb];
		document.conjugator.pronoun.value="yo";
		}	
	if (pronoun==3 || pronoun==13 || pronoun==15 || pronoun==17) {
		solution=tu[verb];
		document.conjugator.pronoun.value="tú";
		}
	if (pronoun==0 || pronoun==4) {
		solution=ella[verb];
		document.conjugator.pronoun.value="él";
		}
	if (pronoun==5) {
		solution=ella[verb];
		document.conjugator.pronoun.value="ella";
		}
	if (pronoun==6) {
		solution=ella[verb];
		document.conjugator.pronoun.value="usted";
		}
	if (pronoun==7 || pronoun==18) {
		solution=nosotros[verb];
		document.conjugator.pronoun.value="nosotros";
		}
	if (pronoun==8 || pronoun==19) {
		solution=vosotros[verb];
		document.conjugator.pronoun.value="vosotros";
		}
	if (pronoun==1 || pronoun==9) {
		solution=ellas[verb];
		document.conjugator.pronoun.value="ellas";
		}
	if (pronoun==10) {
		solution=ellas[verb];
		document.conjugator.pronoun.value="ellos";
		}
	if (pronoun==11) {
		solution=ellas[verb];
		document.conjugator.pronoun.value="ustedes";
		}
	if (alltenses=="yes") {
		whichTenses();
		solution=solution[tenseNumber];
	}
	if (solution == "none") {randomizeAllVerbs();}
	return;

}	



function spanishConjugation() {
	if (start==0) {
		alert("You must first press START to begin the practice.");
		}
	else {
		conjugations++;
		if (commands=="yes") {
			spanishConjugationCommands();
			return
		}
		
		if (allverbs==0) {
			if (alltenses=="yes") {
				alert("yo: "+yo[verb][tenseNumber]+"\n tú: "+tu[verb][tenseNumber]+"\n ella: "+ella[verb][tenseNumber]+"\n nosotros: "+nosotros[verb][tenseNumber]+"\n vosotros: "+vosotros[verb][tenseNumber]+"\n ellos: "+ellas[verb][tenseNumber]);
			}
				
			else {
				alert("yo: "+yo[verb]+"\n tú: "+tu[verb]+"\n ella: "+ella[verb]+"\n nosotros: "+nosotros[verb]+"\n vosotros: "+vosotros[verb]+"\n ellos: "+ellas[verb]);
			}
			document.conjugator.answer.focus();
		}
		else {
			if (alltenses=="yes") {
				alert("yo: " + selectedyo[verb][tenseNumber]+ "\n tú: "+selectedtu[verb][tenseNumber]+"\n ella: "+selectedella[verb][tenseNumber]+"\n nosotros: "+selectednosotros[verb][tenseNumber]+"\n vosotros: "+selectedvosotros[verb][tenseNumber]+"\n ellos: "+selectedellas[verb][tenseNumber]);

			}

			else {
				alert("yo: " + selectedyo[verb]+ "\n tú: "+selectedtu[verb]+"\n ella: "+selectedella[verb]+"\n nosotros: "+selectednosotros[verb]+"\n vosotros: "+selectedvosotros[verb]+"\n ellos: "+selectedellas[verb]);
			}
			document.conjugator.answer.focus();
		}
	}
	
}

function spanishConjugationCommands() {        

	if (start==0) {                
		alert("You must first press START to begin the practice.");                
	}        

	else {  
		var str="";              
		if (allverbs==0) {     
			for (i=0;i<types.length;i++) {
				str += types[i] + ": "+commandConjugations[verb][i] + "\n";
			}           
			alert(str);
	               	document.conjugator.answer.focus();       
	         }     
			           

		else {                
			for (i=0;i<types.length;i++) {
				str += types[i] + ": "+selectedcommand[verb][i] + "\n";
			}           
			alert(str);
	               	document.conjugator.answer.focus();       
	         }           
	}
}        	


function spanishAccent() {
	warning();
	text=document.conjugator.answer.value;
	
	
	if (text.charAt(text.length-1)=="" || text.charAt(text.length-1)==" ") {
		return
	}
		


	if (text.charAt(text.length-1)=="a" || text.charAt(text.length-1)=="e" || text.charAt(text.length-1)=="i" || text.charAt(text.length-1)=="o" || text.charAt(text.length-1)=="u") {
		if (text.charAt(text.length-1)=="a") {
		text=text.substring(0,text.length-1);
		document.conjugator.answer.value=text+"á";
		document.conjugator.answer.focus();
		return
		}
		if (text.charAt(text.length-1)=="e") {
		text=text.substring(0,text.length-1);
		document.conjugator.answer.value=text+"é";
		document.conjugator.answer.focus();
		return
		}

		if (text.charAt(text.length-1)=="i") {
		text=text.substring(0,text.length-1);
		document.conjugator.answer.value=text+"í";
		document.conjugator.answer.focus();
		return
		}

		if (text.charAt(text.length-1)=="o") {
		text=text.substring(0,text.length-1);
		document.conjugator.answer.value=text+"ó";
		document.conjugator.answer.focus();
		return
		}

		if (text.charAt(text.length-1)=="u") {
		text=text.substring(0,text.length-1);
		document.conjugator.answer.value=text+"ú";
		document.conjugator.answer.focus();
		return
		}
		
	}
	
	else {
		var wrongLetter2=text.charAt(text.length-1);
		var wrongLetter=wrongLetter2.toUpperCase();
		alert("OOPS!\n You tried to put an accent on the letter \""+wrongLetter+"\".");
		document.conjugator.answer.focus();
	}

}	
