﻿//variables wi and wf are used in regular expressions
var wi='';//word initial constraint
var wf='';//word final constraint
var multigraphs=new Array;

function set_phonotactic_constraint(position){
	if(position == 'wi') {
		wi='\^';
		wf='';
	}
	if(position == 'wf') {
		wf='\$';
		wi='';
	}
	if(position == '') {
		wi='';
		wf='';
	}
}
function run_multigraph() {
	//this function is used to select for multiple letters in a sequence, such as consonant blends 'br' or 'sp' etc.
	//this function uses regex to match for words. the matching string can be in full gpc format such as: 'p_p,l_l' or simple gpc format such as: 'p,l'
	choose_the_arrays();//chooses a group of words
	//alert(multigraphs[0]);
	construct_source_array_names(multigraphs);//the  current_GPC should be the first gpc of the multigraph
		//eg. if we want the word initial consonants 'br' we will select the array of words which have a 'b' in each word
		//we now have an array of words which all contain at least the first letter of the multigraph
	//current_GPC=items.split(',');
	//alert(aSourceArrays);
	matched_gpc_words.length=0;
	var multigpc=wi + multigraphs.join('\\,') + wf;
	var regMultiGraph=new RegExp(multigpc,"i");
	//alert(regMultiGraph+'\n'+items)
	for(ca in current_array){
		for(i in aSourceArrays){
			//alert(current_array[ca][aSourceArrays[0]]);
			//alert(gpc_words);
			var gpc_words=current_array[ca][aSourceArrays[i]];
			for(s in gpc_words){
				var reject=0;
				if(regMultiGraph.test(gpc_words[s])==false){
					reject=1;
					//alert(regMultiGraph.test(gpc_words[s])+': '+gpc_words[s]);
				}
				if(reject==0){
					word_exists=0;
					for(i in matched_gpc_words){
						if(matched_gpc_words[i]==gpc_words[s]){
							word_exists=1;
						}
					}
					if(word_exists==0){
						matched_gpc_words.push(gpc_words[s]);
					}
				}
			}
		}
	}
	processedWords.length=0;
	checkcss();
	displayList(matched_gpc_words);
}
function add_multi_gpcs(items){
	//alert('items: '+items+'\n'+document.getElementById(items).checked);
	var found=0;
	current_GPC.length=0;
	if(/,/.test(items)==true) {
		multigraphs=items.split(',');
	} else {
		multigraphs.push(items);
	}
	for(var i=0; i < multigraphs.length; i++){
		//addStringItemsToArray(known_GPCs,multigraphs[1]);
		//addStringItemsToArray(current_GPC,multigraphs[1]);
		addUniqueItemsToArray(known_GPCs,multigraphs);
		addUniqueItemsToArray(current_GPC,multigraphs);
	}
	//alert(known_GPCs+'\n'+current_GPC);
	run_multigraph();//send the gpc items as a string to the next function to use as regex
}
function run_multigraph2() {
	var blends=document.getElementsByName('blends');
	for(var i=0; i<blends.length; i++){
		if(blends[i].checked == true){
			run_multigraph(blends[i].value)
		}
	}
}
