// JavaScript Document
function add_word_sub(new_word, new_perl) {
	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";
	}
	document.gen_input.perl_input.value = document.gen_input.perl_input.value + " [" + new_perl + "]";
}

function replace_word_sub(new_word,new_perl,oldh,newh) {
    var search_word = new_word + " (" + oldh + ")";
	var pos_word = document.gen_input.sel_words.value.indexOf(search_word);
	var len_word = search_word.length;
    var search_perl = new_perl + "," + oldh;
	var pos_perl = document.gen_input.perl_input.value.indexOf(search_perl);
	var len_perl = search_perl.length;
	
	document.gen_input.sel_words.value = document.gen_input.sel_words.value.substring(0,pos_word) +
	  new_word + " (" + newh + ")" +
	  document.gen_input.sel_words.value.substring(pos_word + len_word);

	document.gen_input.perl_input.value = document.gen_input.perl_input.value.substring(0,pos_perl) +
	  new_perl + "," + newh +
	  document.gen_input.perl_input.value.substring(pos_perl + len_perl);	
}

function remove_word_sub(new_word,new_perl,oldh) {
    var search_word = new_word + " (" + oldh + ")";
	var pos_word = document.gen_input.sel_words.value.indexOf(search_word);
	var len_word = search_word.length + 1;
    var search_perl = " [" + new_perl + "," + oldh + "]";
	var pos_perl = document.gen_input.perl_input.value.indexOf(search_perl);
	var len_perl = search_perl.length;
	
	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_input.value = document.gen_input.perl_input.value.substring(0,pos_perl) +
	  document.gen_input.perl_input.value.substring(pos_perl + len_perl);	
}

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 + " (niedrig)") != -1) {
		replace_word_sub(new_word,new_perl,"niedrig","mittel");
	} else if (document.gen_input.sel_words.value.indexOf(new_word + " (mittel)") != -1) {
		replace_word_sub(new_word,new_perl,"mittel","hoch");
	} else if (document.gen_input.sel_words.value.indexOf(new_word + " (hoch)") != -1) {
		remove_word_sub(new_word,new_perl,"hoch");
	} else {
	    new_perl += ",niedrig";
		new_word += " (niedrig)";
    	add_word_sub(new_word, new_perl);
	}
	
	select.options.selectedIndex=-1;
}

function clear_word(button) {
	document.gen_input.sel_words.value = "";
	document.gen_input.perl_input.value = "";
}

function get_checked(radio) {
	var idx;

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

	return -1;
}

function add_subst() {
	var new_word = "+ " + document.substantiv_form.nom_sg.value;
	var new_perl = "{";
	var chkd_geschl, chkd_haeufigkeit;
	
	chkd_geschl = get_checked(document.substantiv_form.subst_geschl);
	chkd_haeufigkeit = get_checked(document.substantiv_form.subst_haeufigkeit);
	
	if (chkd_geschl < 0) {
    	alert("Geschlecht fehlt!");
	    document.substantiv_form.geschl.mask.focus();		
 		return;
	}
	if (document.substantiv_form.nom_sg.value == "") {
    	alert("Nominativ singular fehlt!");
	    document.substantiv_form.nom_sg.focus();		
 		return;
	}
	if (document.substantiv_form.nom_pl.value == "") {
    	alert("Nominativ plural fehlt!");
	    document.substantiv_form.nom_pl.focus();		
 		return;
	}
	if (document.substantiv_form.gen_sg.value == "") {
    	alert("Genetiv singular fehlt!");
	    document.substantiv_form.gen_sg.focus();		
 		return;
	}
	if (document.substantiv_form.gen_pl.value == "") {
    	alert("Genetiv plural fehlt!");
	    document.substantiv_form.gen_pl.focus();		
 		return;
	}
	if (document.substantiv_form.dat_sg.value == "") {
    	alert("Dativ singular fehlt!");
	    document.substantiv_form.dat_sg.focus();		
 		return;
	}
	if (document.substantiv_form.dat_pl.value == "") {
    	alert("Dativ plural fehlt!");
	    document.substantiv_form.dat_pl.focus();		
 		return;
	}
	if (document.substantiv_form.akk_sg.value == "") {
    	alert("Akkusativ singular fehlt!");
	    document.substantiv_form.akk_sg.focus();		
 		return;
	}
	if (document.substantiv_form.akk_pl.value == "") {
    	alert("Akkusativ plural fehlt!");
	    document.substantiv_form.akk_pl.focus();		
 		return;
	}
	
	new_perl += document.substantiv_form.nom_sg.value + "/";
	new_perl += document.substantiv_form.nom_pl.value + "/";
	new_perl += document.substantiv_form.gen_sg.value + "/";
	new_perl += document.substantiv_form.gen_pl.value + "/";
	new_perl += document.substantiv_form.dat_sg.value + "/";
	new_perl += document.substantiv_form.dat_pl.value + "/";
	new_perl += document.substantiv_form.akk_sg.value + "/";
	new_perl += document.substantiv_form.akk_pl.value + "/";
	new_perl += document.substantiv_form.subst_geschl[chkd_geschl].value + "},s,";
	new_perl += document.substantiv_form.subst_haeufigkeit[chkd_haeufigkeit].value;
	
	new_word += " (" + document.substantiv_form.subst_haeufigkeit[chkd_haeufigkeit].value + ")";
	
	add_word_sub(new_word,new_perl);
}

function add_adj() {
	var new_word = "+ " + document.adjektiv_form.adj.value;
	var new_perl = "{";
	var chkd_haeufigkeit;
	
	chkd_haeufigkeit = get_checked(document.adjektiv_form.adj_haeufigkeit);
	
	if (document.adjektiv_form.adj.value == "") {
    	alert("Adjektiv fehlt!");
	    document.adjektiv_form.adj.focus();		
 		return;
	}
	if (document.adjektiv_form.stam.value == "") {
    	alert("Stammform fehlt!");
	    document.adjektiv_form.stam.focus();		
 		return;
	}
	
	new_perl += document.adjektiv_form.stam.value + "},adj,";
	new_perl += document.adjektiv_form.adj_haeufigkeit[chkd_haeufigkeit].value;

	new_word += " (" + document.adjektiv_form.adj_haeufigkeit[chkd_haeufigkeit].value + ")";
	
	add_word_sub(new_word,new_perl);
}

function add_verb() {
	var new_word = "+ " + document.verb_form.p1_pl.value;
	var new_perl = "{";
	var chkd_haeufigkeit;
	
	chkd_haeufigkeit = get_checked(document.verb_form.verb_haeufigkeit);
	
	if (document.verb_form.p1_sg.value == "") {
    	alert("1.Person singular fehlt!");
	    document.verb_form.p1_sg.focus();		
 		return;
	}
	if (document.verb_form.p1_pl.value == "") {
    	alert("1.Person plural fehlt!");
	    document.verb_form.p1_pl.focus();		
 		return;
	}
	if (document.verb_form.p2_sg.value == "") {
    	alert("2.Person singular fehlt!");
	    document.verb_form.p2_sg.focus();		
 		return;
	}
	if (document.verb_form.p2_pl.value == "") {
    	alert("2.Person plural fehlt!");
	    document.verb_form.p2_pl.focus();		
 		return;
	}
	if (document.verb_form.p3_sg.value == "") {
    	alert("3.Person singular fehlt!");
	    document.verb_form.p3_sg.focus();		
 		return;
	}
	if (document.verb_form.p3_pl.value == "") {
    	alert("3.Person plural fehlt!");
	    document.verb_form.p3_pl.focus();		
 		return;
	}
	
	new_perl += document.verb_form.p1_sg.value + "/";
	new_perl += document.verb_form.p1_pl.value + "/";
	new_perl += document.verb_form.p2_sg.value + "/";
	new_perl += document.verb_form.p2_pl.value + "/";
	new_perl += document.verb_form.p3_sg.value + "/";
	new_perl += document.verb_form.p3_pl.value + "},vb,";
	new_perl += document.verb_form.verb_haeufigkeit[chkd_haeufigkeit].value;
	
	new_word += " (" + document.verb_form.verb_haeufigkeit[chkd_haeufigkeit].value + ")";
	
	add_word_sub(new_word,new_perl);
}

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, rand;
	
	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("Satzanzahl keine Zahl!");
	    document.gen_input.count.focus();
	    return false;
	}
	for (i = 0; i < document.gen_input.random.value.length; ++i)
	if (document.gen_input.random.value.charAt(i) < "0" ||
    	document.gen_input.random.value.charAt(i) > "9")
    	chkZ = -1;
	if (chkZ == -1) {
    	alert("Zufälligkeit keine Zahl!");
	    document.gen_input.random.focus();
	    return false;
	}
	
	count=parseInt(document.gen_input.count.value);
	rand=parseInt(document.gen_input.random.value);
	
	if (rand>100) {
    	alert("Zufälligkeit muss im Bereich 0 bis 100 liegen!");
	    document.gen_input.random.focus();
	    return false;
	}
	if ((count<1) || (count>99)) {
    	alert("Satzanzahl muss im Bereich 1 bis 99 liegen!");
	    document.gen_input.count.focus();
	    return false;
	}
	
	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("Diese Seite funktioniert auf Apple Computern am besten mit 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() { //v6.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
    obj.visibility=v; }
}

