// <!-- javascript development begun by Doug Rintoul, Nov 10, 2003 -->

var MNumber  ; 
var MGender  ; 
var MPronoun ; 
var MPerson  ; 
var SNumber  ; 
var SGender  ; 
var SPronoun ; 
var SPerson  ; 
var VTime    ; 
var Adjective;
var NPhrase  ;
var Num  ; 
var Gender  ; 
var Pronoun ; 
var Person  ; 
var Main ;
var Form;
var HeadNoun;

function getButton(Button) {
	for (i=0; i< Button.length; i ++ ){
		if(Button[i].checked)
			return Button[i].value;
	} 
}

// gets the Tooltip from 
function getToolTip(word) {
	for (i=0; i< tt.length; i ++) {
		if(word == Lemma) {
			return tt[i];
		}
	}
	return false;
}

function refreshVariables(){
	MNumber  = getButton(document.StorySelectors.MainNumber) ;
	MGender  = getButton(document.StorySelectors.MainGender) ;
	MPronoun = getButton(document.StorySelectors.MainPronoun);
	MPerson  = getButton(document.StorySelectors.MainPerson) ;
	SNumber  = getButton(document.StorySelectors.SecNumber)  ;
	SGender  = getButton(document.StorySelectors.SecGender)  ;
	SPronoun = getButton(document.StorySelectors.SecPronoun) ;
	SPerson  = getButton(document.StorySelectors.SecPerson)  ;
	VTime    = getButton(document.StorySelectors.Tense) ;
}

// Verbs = lemma.VMoodTenseVoicePersonNumber  getVerb(1,'βλέπω',1,0,0)
function getVerb(Main,Lemma,vNum,vPerson,Cap){
	refreshVariables();
	if(Main == '1') {
		Num = MNumber; 
		Person = MPerson;
	} else {
		Num = SNumber; 
		Person = SPerson;
	}
	// MTV variable code still needs to be tested
	// MTV = 3 letters+1 number
	// var a=new Array(MTV.split("")); // creates new array and splits the MTV variable into an array called 'a'
	// var a= new Array; // creates new array called 'a'
	// a=MTV.split(""); // split MTV into array 'a'
	// switch(a[3]) {  // check for value of last array element, which is a number
	// 	case '0': a[1]=VTime; MTV=a.join();  // unrestricted tense values can be changed by tense selector
	// 	case '1': MTV=a.join(); // restricted = leave tense value as it is in variable
	// }
	// MTV=a.join();  // rejoins the MTV variable making it available for selecting vocabulary

	// value of '0' in vNum and vPerson allows unrestricted modification
	if(vNum == '1') Num = "s"; // restrict to singular form
	if(vNum == '2') Num = "p"; // restrict to plural form
	if(vPerson == '1') Person = '1'; // restrict to 1st person
	if(vPerson == '2') Person = '2'; // restrict to 2nd person
	if(vPerson == '3') Person = '3'; // restrict to 3rd person
	switch (VTime) {
		case 'p' : Form = word[Lemma + ".vipa" + Person + Num ]; break; // present
		case 'a' : Form = word[Lemma + ".viaa" + Person + Num ]; break; // aorist
		case 'f' : Form = word[Lemma + ".vifa" + Person + Num ]; break; // future
		case 'i' : Form = word[Lemma + ".viia" + Person + Num ]; break; // imperfect
		case 'x' : Form = word[Lemma + ".vixa" + Person + Num ]; break; // perfect
	} 
	// moveable nu:  if(nu == '1') Form = Form.slice(0,-1);

	if(Cap == "1") Form = Form.charAt(0).toUpperCase() + Form.slice(1) ; // capitalization check
	return '<a title="' + tt[Lemma] + '">' + Form + '</a>' ;
}

// contains: Main,Article,Modifier,Lemma,Masc,MPlural,Fem,FPlural,Case,Cap 
// sample data: getPNoun(0,1,'ἄνθρωπος','Μᾶρκος',3,4,3,4,'n',0)
function getPNoun(Main,Article,Modifier,Lemma,Masc,MPlural,Fem,FPlural,Case,Cap) { 
	refreshVariables();
	if(Main == '1') {
		Person = MPerson;
		Gender = MGender;
		Num = MNumber;
		Pronoun = MPronoun;
	} else {
		Person = SPerson;
		Gender = SGender;
		Num = SNumber;
		Pronoun = SPronoun;
	} 
	if(MPlural == 0) Num = 's';
	if(Article == "1") { // Article is required
		Form = word["ὁ.d" + Case + Gender + Num];
		if(Cap =="1") Form = Form.charAt(0).toUpperCase() + Form.slice(1) ; 
		Article = '<a title="' + tt["ὁ.d" + Case + Gender + Num] + '">'+ Form + ' </a>'; 
	} else {
		Article = ""; // article not required
	}
	if(Modifier != 0) { 
		if(Gender == "f"){
			if(Modifier == "ἄνθρωπος") // man
				Modifier = "γυνή"; // woman
			if(Modifier == "ἀδελφός") // brother
				Modifier = "ἀδελφή"; // sister
			if(Modifier == "πατήρ") // father
				Modifier = "μήτηρ"; // mother
			if(Modifier == "δοῦλος") // masc. slave
				Modifier = "δούλη"; // fem. slave
		}
		if(Gender == "m"){
			if(Modifier == "γυνή") // woman
				Modifier = "ἄνθρωπος"; // man
			if(Modifier == "ἀδελφή")  // sister
				Modifier = "ἀδελφός"; // brother
			if(Modifier == "μήτηρ")  // mother
				Modifier = "πατήρ"; // father
			if(Modifier == "δούλη") // fem. slave
				Modifier = "δοῦλος"; // masc. slave
		//if(Gender == "n");
		//	if(Modifier == "ἄνθρωπος") // man
		//		Modifier = "!child!"; // child
		}

		MForm = word[Modifier  + ".n" + Case + Gender + Num]; //modifier is a noun
		Modifier = '<a title="' + tt[Modifier] +'">' +  MForm + ' </a>';
		if(MForm == null) {
			MForm = word[Modifier  + ".an" + Case + Gender + Num]; //modifier is an adjective
			Modifier = '<a title="' + tt[Modifier] +'">' +  MForm + ' </a>';
		}
		if(Cap == '1' && Article == '0') {
			MForm = MForm.charAt(0).toUpperCase() + MForm.slice(1);
			Modifier = '<a title="' + tt[Modifier] +'">' +  MForm + ' </a>';
		}
	} else { // Modifier = 0
		Modifier = '';
	}

	switch (Gender + Num){
		case "ms" : HeadNoun = '<a title="' + tt[getMascActors(Masc)] + '">' + word[getMascActors(Masc) + ".n" + Case + "ms"] + '</a>'; 
		break;
		case "fs" : HeadNoun = '<a title="' + tt[getFemActors(Fem)] + '">' + word[getFemActors(Fem)   + ".n" + Case + "fs"] + '</a>'; 
		break; 
		case "mp" :
			if(MPlural >= '1'){
				HeadNoun = '<a title="' + tt[getMascActors(Masc)] + '">' + word[getMascActors(Masc) + ".n" + Case + "ms"] + '</a>' + " καὶ " + '<a title="' + tt[getMascActors(MPlural)] + '">' + word[getMascActors(MPlural) + ".n" + Case + "ms"] + '</a>' ;
			}
		break;
		case "fp" :
			if(MPlural >= '1'){
				HeadNoun = '<a title="' + tt[getFemActors(Fem)] + '">' + word[getFemActors(Fem)   + ".n" + Case + "fs"] + '</a>' + " καὶ " + '<a title="' + tt[getFemActors(FPlural)] + '">' + word[getFemActors(FPlural)  + ".n" + Case + "fs"] + '</a>' ;
			}
		break;
	}
	if(Pronoun == 'p'  && Case != "v") { 
		Article = ''; Modifier ='';
		Form = word["αὐτός.rp" + Case + Gender + Num]
		if(Cap == "1") Form = Form.charAt(0).toUpperCase() + Form.slice(1); // capitalize
		HeadNoun = '<a title="' + tt["αὐτός"] + '">' + Form + '</a>';
	}
	if(Person == "1" && Case != "v") {
		Article = ''; Modifier ='';
		Form = word["ἐγώ.rp" + Case + Gender + Num];
		if(Cap == "1") Form = Form.charAt(0).toUpperCase() + Form.slice(1); // capitalize
		HeadNoun = '<a title="first person: I, we">' + Form + '</a>';
	}
	if(Person == "2" &&  Case != "v") {
		Article = ''; Modifier ='';
		Form = word["σου.rp" + Case + Gender + Num];
		if(Cap == "1") Form = Form.charAt(0).toUpperCase() + Form.slice(1); // capitalize
		HeadNoun = '<a title="second person: you">' + Form + '</a>';
	}
	return Article + Modifier + HeadNoun;
}

// Main012,Article01,'Modifier','Lemma',Case'ngdav',Tag'ms',Plural012,Cap01,Order01
// order = 1 means noun comes first
// order = 0 means noun goes second, adjective comes first
// common nouns  getCNoun(0,0,'','ἄνθρωπος','a','ms',0,0,0)
function getCNoun(Main,Article,Modifier,Lemma,Case,Tag,Plural,Cap,Order){  
	refreshVariables();
	if(Main == '0') {    // for nouns which should not react to main and secondary actor selections
		Pronoun = 'n' ;
		Person = '0' ;
	}
	if(Main == '1') {  // for main actors
		Person = MPerson;
		Gender = MGender;
		Num = MNumber;
		if(Plural == 1) Num = 's'; // override Number selector
		if(Plural == 2) Num = 'p'; // override Number selector
		Tag = Gender + Num;
		Pronoun = MPronoun;
	}
	if(Main == '2')	{  // for secondary actors
		Person = SPerson;
		Gender = SGender;
		Num = SNumber;
		if(Plural == 1) Num = 's'; // override Number selector 
		if(Plural == 2) Num = 'p'; // override Number selector
		Tag = Gender + Num;
		Pronoun = SPronoun;
	} 
	if(Article == "1") {
		AForm = word["ὁ.d" + Case + Tag]
		if(Cap =="1") AForm = AForm.charAt(0).toUpperCase() + AForm.slice(1); // capitalize
		Article = '<a title="' + tt["ὁ.d" + Case + Tag] + '">'+ AForm + ' </a>'; 
	} else { //article='0'
		Article = "";
	}
	if(Modifier != 0) {  // modifier must be tagged like a noun (not .annms but .nnms) modifiers can be nouns or adjectives
		MForm = word[Modifier  + ".n" + Case + Tag ];
		if(MForm == null) { 
			MForm = word[Modifier  + ".an" + Case + Tag ];
		}
		if(Cap == '1' && Order == '0' && Article =='0') { // if there is no article AND modifier comes before noun AND must be capitalized
			MForm =  MForm.charAt(0).toUpperCase() + MForm.slice(1); // capitalize
			Modifier = '<a title="' + tt[Modifier] +'">' +  MForm + '</a>';
		}
	} else {
		Modifier = '';
	}
	if(Gender == "f"){
		if(Lemma == "ἄνθρωπος") // man
			Lemma = "γυνή"; 		// woman
		if(Lemma == "ἀδελφός")  // brother
			Lemma = "ἀδελφή"; 	// sister
		if(Lemma == "πατήρ")  	// father
			Lemma = "μήτηρ"; 	// mother
		//if(SGender == "n");
		//if(Lemma == "ἄνθρωπος") // man
		//Lemma = "!child!"; 		// child
	}
	if(Gender == "m"){
		if(Modifier == "γυνή") // woman
			Modifier = "ἄνθρωπος"; 	// man
		if(Modifier == "ἀδελφή")  // sister
			Modifier = "ἀδελφός"; // brother
		if(Modifier == "μήτηρ")  // mother
			Modifier = "πατήρ"; 	// father
	}

	NForm =  word[Lemma + ".n" + Case + Tag];
	if(Cap == "1" && Order == '1' ) { // if there is no article AND no modifier AND noun must be capitalized
		NForm = NForm.charAt(0).toUpperCase() + NForm.slice(1) ; // capitalize
	}
	HeadNoun = '<a title="' + tt[Lemma] + '">' + NForm + '</a>';

	if(Pronoun == "p") {
		Article = ''; Modifier ='';
		PForm = word["αὐτός.rp" + Case + Tag];
		if(Cap == "1") PForm = PForm.charAt(0).toUpperCase() +PForm.slice(1); // αὐτός
		HeadNoun = '<a title="' + tt["αὐτός"] + '">' + PForm + '</a>';
	}
	if(Person == "1") {
		Article = ''; Modifier ='';
		Form = word["ἐγώ.rp" + Case + Tag];
		if(Cap == "1") Form = Form.charAt(0).toUpperCase() + Form.slice(1);
		HeadNoun = '<a title="first person: I, we">' + Form + '</a>';
	}
	if(Person == "2") {
		Article = ''; Modifier ='';
		Form = word["σου.rp" + Case + Tag];
		if(Cap == "1") Form =  Form.charAt(0).toUpperCase() + Form.slice(1);
		HeadNoun = '<a title="second person: you">' + Form + '</a> ';
	}
	if(Order == "0") {
		return Article + Modifier + ' '  + HeadNoun;
		} else {
		return Article + HeadNoun + ' ' + Modifier;
	}
}

// Main Actor Adjectives: Lemma,Case,Tag,Plural,Cap
function getAdj(Main,Lemma,Case,Tag,Plural,Cap){ 
refreshVariables();
	if(Main == '0') {    // for adjectives which should not react to main and secondary actor selections
		Pronoun = '' ;
		Person = '0' ;
	}
	if(Main == '1') {  // for main actors
		Person = MPerson;
		Gender = MGender;
		if(Plural == 1) MNumber = 's'; // override Number selector
		if(Plural == 2) MNumber = 'p'; // override Number selector
		Tag = MGender + MNumber;
		Pronoun = MPronoun;
	}
	if(Main == '2')	{  // for secondary actors
		Person = SPerson;
		Genderx = SGender;
		if(Plural == 1) SNumber = 's'; // override Number selector
		if(Plural == 2) SNumber = 'p'; // override Number selector
		Tag = SGender + SNumber;
		Pronoun = SPronoun;
	}
	Form = word[Lemma + ".an" + Case + Tag];
	if(Cap == "1") Form = Form.charAt(0).toUpperCase() + Form.slice(1); // capitalize 1st letter
	return '<a title="' + tt[Lemma] + '">' + Form + '</a> '; 
}

// words not handled elsewhere for now. contains Lemma,tagging info, Cap
function getMWord(Lemma,Tag,Plural,Cap){
refreshVariables();
	if(Plural == '0') MNumber = "s"; // reset MNumber to Singular if flag is set to "0"
	Form = word[Lemma + "." + Tag + MNumber];
	if(Cap == "1") Form = Form.charAt(0).toUpperCase() + Form.slice(1); // capitalize 1st letter
	return Form;
}

function getWord(Lemma,Tag,Plural,Cap){
refreshVariables();
	if(Plural == '1') SNumber = "s"; // reset SNumber to Singular if flag is set to "1"
	if(Plural == '2') SNumber = "p"; // reset SNumber to Plural if flag is set to "2"
	//Form = word[Lemma + "." + Tag + SNumber];
	if(Tag.charAt(0) == "b") Form = word[Lemma + "." + Tag]; // matches adverbs
	if(Tag.charAt(0) == "c") Form = word[Lemma + "." + Tag]; // matches conjunctions
	if(Tag.charAt(0) == "p") Form = word[Lemma + "." + Tag]; // matches prepostions
	if(Tag.substring(0,2) == "vn") Form = word[Lemma + "." + Tag]; // matches infinite verbs
	if(Tag.substring(0,2) == "vd") Form = word[Lemma + "." + Tag + SNumber]; // matches imperative verbs
	if(Cap == "1") Form = Form.charAt(0).toUpperCase() + Form.slice(1); // capitalize 1st letter
	return '<a title="' + tt[Lemma] + '">' + Form + '</a>' ;
}

function resetButtons(){
	for (var i in document.StorySelectors.elements){
		if(document.StorySelectors.elements[i].defaultChecked==true){
			document.StorySelectors[i].checked=true
		}
	}
}
