function add_word_sub(new_word,new_perl,extra) {
	if (document.gen_input.sel_words.value == "") {
		document.gen_input.sel_words.value = new_word + "\r";
	} else {
    	document.gen_input.sel_words.value = document.gen_input.sel_words.value + new_word + "\r";
	}

	if (extra) {
		if (document.gen_input.perl_new_words.value == "") {
			document.gen_input.perl_new_words.value = new_perl;
		} else {
		    document.gen_input.perl_new_words.value = document.gen_input.perl_new_words.value + ";" + new_perl;
		}
	} else {
		if (document.gen_input.perl_words.value == "") {
			document.gen_input.perl_words.value = new_perl;
		} else {
		    document.gen_input.perl_words.value = document.gen_input.perl_words.value + ";" + new_perl;
		}
	}

	document.gen_input.perl_word_cnt.value++;
}

function remove_word_sub(new_word,new_perl) {
    var search_word = new_word;
	var pos_word = document.gen_input.sel_words.value.indexOf(search_word);
	var len_word = search_word.length + 1;
    var search_perl = new_perl;
	var pos_perl = document.gen_input.perl_words.value.indexOf(search_perl);
	var len_perl = search_perl.length + 1;
	
	document.gen_input.sel_words.value = document.gen_input.sel_words.value.substring(0,pos_word) +
	  document.gen_input.sel_words.value.substring(pos_word + len_word);

	document.gen_input.perl_words.value = document.gen_input.perl_words.value.substring(0,pos_perl) +
	  document.gen_input.perl_words.value.substring(pos_perl + len_perl);	

	document.gen_input.perl_word_cnt.value--;
}

function add_word(select) {
	var new_word = select.options[select.options.selectedIndex].text;
    var new_perl = select.options[select.options.selectedIndex].value ;
  	
	if (document.gen_input.sel_words.value.indexOf(new_word) != -1) {
		remove_word_sub(new_word,new_perl);
	} else {
    	add_word_sub(new_word, new_perl,0);
	}
	
	select.options.selectedIndex=-1;
}

function clear_word(button) {
	document.gen_input.sel_words.value = "";
	document.gen_input.perl_words.value = "";
	document.gen_input.perl_new_words.value = "";
	document.gen_input.perl_word_cnt.value = 0;
}

function get_checked(radio) {
	var idx;

	for (idx=0;radio[idx];idx++) {
		if (radio[idx].checked == true) return idx;
	}

	return -1;
}

function add_noun() {
	var new_word = "+ " + document.substantiv_form.noun_sg.value;
	var new_perl = "noun:";
	
	if (document.substantiv_form.noun_sg.value == "") {
    	alert("Singular missing!");
	    document.substantiv_form.noun_sg.focus();
 		return;
	}
	if (document.substantiv_form.noun_pl.value == "") {
    	alert("Plural missing!");
	    document.substantiv_form.noun_pl.focus();
 		return;
	}
	
	new_perl += document.substantiv_form.noun_sg.value + ",";
	new_perl += document.substantiv_form.noun_pl.value;
	
	add_word_sub(new_word,new_perl,1);
}

function add_adj() {
	var new_word = "+ " + document.adjektiv_form.adj.value;
	var new_perl = "adj:";
	
	if (document.adjektiv_form.adj.value == "") {
    	alert("Adjectiv missing!");
	    document.adjektiv_form.adj.focus();	
 		return;
	}
	if (document.adjektiv_form.adv.value == "") {
    	alert("Adverb missing!");
	    document.adjektiv_form.adv.focus();	
 		return;
	}
	
	new_perl += document.adjektiv_form.adj.value + ",";
	new_perl += document.adjektiv_form.adv.value;

	add_word_sub(new_word,new_perl,1);
}

function add_verb() {
	var new_word = "+ " + document.verb_form.verb_inf.value;
	var new_perl = "verb:";
	
	if (document.verb_form.verb_inf.value == "") {
    	alert("Infinitive missing!");
	    document.verb_form.verb.focus();
 		return;
	}
	if (document.verb_form.verb_s.value == "") {
    	alert("3rd.person missing!");
	    document.verb_form.verb_s.focus();
 		return;
	}
	if (document.verb_form.verb_ing.value == "") {
    	alert("Continuous missing!");
	    document.verb_form.verb_ing.focus();
 		return;
	}
	if (document.verb_form.verb_ed.value == "") {
    	alert("Past tense missing!");
	    document.verb_form.verb_ed.focus();	
 		return;
	}
	
	new_perl += document.verb_form.verb_inf.value + ",";
	new_perl += document.verb_form.verb_s.value + ",";
	new_perl += document.verb_form.verb_ing.value + ",";
	new_perl += document.verb_form.verb_ed.value;
	
	add_word_sub(new_word,new_perl,1);
}

function all_words_chg() {
	if (document.gen_input.all_words.checked == true) {
		document.gen_input.random.value = 100;
		document.gen_input.random.disabled = true;
	} else {
		document.gen_input.random.disabled = false;		
	}
}

function chk_form () {
	var chkZ = 1;
	var count, word_cnt, probability;
	
	for (i = 0; i < document.gen_input.count.value.length; ++i)
	if (document.gen_input.count.value.charAt(i) < "0" ||
    	document.gen_input.count.value.charAt(i) > "9")
    	chkZ = -1;
	if (chkZ == -1) {
    	alert("Number of sentences not a number!");
	    document.gen_input.count.focus();
	    return false;
	}
	
	count=parseInt(document.gen_input.count.value);

	if ((count<1) || (count>99)) {
    	alert("Number of sentences must be in the range 1 to 99!");
	    document.gen_input.count.focus();
	    return false;
	}
	
	word_cnt=parseInt(document.gen_input.perl_word_cnt.value);
	
	probability = (word_cnt / count) * 1.6;
	if (probability > 0.8) probability = 0.8;
	document.gen_input.perl_probability.value=probability;
	
	gentext = window.open('','gentext','scrollbars=yes,toolbar=no,location=no,directories=no,status=no,menubar=no,resizable=yes,width=580,height=420,top=240,left=190');
	gentext.focus();
	
	return true;
}

function chk_brwser() {
	var brwser = navigator.userAgent;
	var mac = (brwser.search(/Mac/)>=0);
	var safari = (brwser.search(/Safari/)>=0);
	
	if ((mac) && (!safari)) {
		alert("On Apple computers this site works best with Safari!");
	}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function MM_showHideLayers() { //v9.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) 
  with (document) if (getElementById && ((obj=getElementById(args[i]))!=null)) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
    obj.visibility=v; }
}

