teltarif = {
    n: [ ["dummy","", ""],
	 ["Dt. Telekom T-Net (analog)","telekom",1000],
	 ["Dt. Telekom ISDN", "telekom-callplus-isdn",1000],
	 ["T-DSL 1000","telekom-dsl1000",1000],
	 ["T-DSL 2000","telekom-dsl2000",2000],
	 ["T-DSL 3000","telekom-dsl3000",3000],
	 ["T-DSL 6000","telekom-dsl6000",6000] ],

    c: [ [1,2],
	 [3,4,5,6],
	 [3,4,5,6] ],

    isNumeric: function(s) {
        return s.search("^[0-9]+$") == 0;
    },

    initMenu: function() {
	var i;

	document.abfrage.anschluss.options[0] = new Option("keiner", "-1");
	document.abfrage.anschluss.length = 1;
	document.abfrage.anschluss2.options[0] = new Option("keiner", "-1");
	document.abfrage.anschluss2.length = 1;

	for(i = 0; i < this.c[0].length; i++) {
	    document.abfrage.anschluss.options[i+1] = new Option(this.n[this.c[0][i]][0], this.c[0][i]);
	}
    },

    changeMenu: function(cm) {
	var i, v;

	if(cm == 0 && document.abfrage.anschluss.selectedIndex >= 0) {
	    v = document.abfrage.anschluss.options[document.abfrage.anschluss.selectedIndex].value;
	    if(v > -1) {
		document.abfrage.anschluss2.options[0] = new Option("keiner", "-1");
		for (i = 0; i < this.c[v].length; i++) {
		    document.abfrage.anschluss2.options[i+1] = new Option(this.n[this.c[v][i]][0], this.c[v][i]);
		}
		document.abfrage.anschluss2.length = i + 1;
	    } else {
		document.abfrage.anschluss2.options[0] = new Option("keiner", "-1");
		document.abfrage.anschluss2.length = 1;
		document.abfrage.downstream.value = 1000;
		if(document.abfrage.downstream.type == "select-one") {
		    document.abfrage.downstream.selectedIndex = 1;
		}
	    }
	} else if(cm == 1  && document.abfrage.anschluss2.selectedIndex >= 0) {
	    v = document.abfrage.anschluss2.options[document.abfrage.anschluss2.selectedIndex].value;
	    if(v > -1) {
		downstream = this.n[v][2];
		document.abfrage.downstream.value = downstream;
		if(document.abfrage.downstream.type == "select-one") {
		    var index = -1;
		    switch (downstream) {
			case  768 : index = 0; break;
			case 1000 : index = 1; break;
			case 1536 : index = 2; break;
			case 2000 : index = 3; break;
			case 3000 : index = 4; break;
		    }
		    if (index > -1) {
			document.abfrage.downstream.selectedIndex = index;
		    }
		}
	    }
	}
    },

    finishMenu: function() {
	var i = document.abfrage.anschluss.selectedIndex;
	var j = document.abfrage.anschluss2.selectedIndex;
	if(i > 0 && j > -1) {
	    var x = document.abfrage.anschluss.options[i].value;
	    var y = document.abfrage.anschluss2.options[j].value;
	    if (x > -1 && y > -1 && this.n[x][1] != "")
		document.abfrage.anschluss.options[i].value = this.n[x][1] + "," + this.n[y][1];
	    else if (y > -1)
		document.abfrage.anschluss.options[i].value = this.n[y][1];
	    else if (x > -1)
		document.abfrage.anschluss.options[i].value = this.n[x][1];
	    if (document.abfrage.anschlusstyp.type == "hidden")
	        document.abfrage.anschlusstyp.value = "fest";
	} else {
	    document.abfrage.anschluss.options[i].value = "";
	    if (document.abfrage.anschlusstyp.type == "hidden")
		document.abfrage.anschlusstyp.value = "frei";

	}
	document.abfrage.anschluss2.length = 0;
    },

    changeProfil: function() {
	var index = document.abfrage.dslprofil.selectedIndex;
	var dauer, mb;

	if(index >= -1) {
	    switch(index) {
		case 0: dauer = 25;
			mb = 1150;
			break;

		case 1: dauer = 60;
			mb = 2250;
			break;

		case 2: dauer = 80;
			mb = 4500;
			break;

		case 3: dauer = "unbegrenzt";
			mb = 50000;
			break;

		case 4: dauer = 300;
			mb = 5500;
			break;
	    }
	    document.abfrage.dauer.value = dauer;
	    document.abfrage.mb.value = mb;
	}
    },

    finishProfil: function() {
	if(document.abfrage.dauer.value != "unbegrenzt" &&
	   (document.abfrage.dauer.value == "" ||
	    ! this.isNumeric(document.abfrage.dauer.value) ||
	    document.abfrage.dauer.value <= 0)) {
	    alert("Bitte füllen Sie das Feld \"Onlinestunden monatlich\" in den Nutzungsdetails korrekt aus!");
	    document.abfrage.dauer.focus();
	    return false;
	}
	if(document.abfrage.mb.value != "unbegrenzt" &&
	   (document.abfrage.mb.value == "" ||
	    ! this.isNumeric(document.abfrage.mb.value) ||
	    document.abfrage.mb.value <= 0)) {
	    alert("Bitte füllen Sie das Feld \"Megabyte monatlich\" in den Nutzungsdetails korrekt aus!");
	    document.abfrage.mb.focus();
	    return false;
	}

	return true;
    },

    finishForm: function() {
	if(document.abfrage.vonnr.value != "" && ! this.isNumeric(document.abfrage.vonnr.value)) {
	    alert("Bitte füllen Sie das Feld \"Ortsvorwahl\" korrekt aus!");
	    document.abfrage.vonnr.focus();
	    return false;
	}
	if(document.abfrage.downstream.value == "" || ! this.isNumeric(document.abfrage.downstream.value) ||
	   document.abfrage.downstream.value <= 0 || document.abfrage.downstream.value > 100000) {
	    alert("Bitte füllen Sie das Feld \"Minimaler Downstream\" korrekt aus!");
	    document.abfrage.downstream.focus();
	    return false;
	}
	if(! this.finishProfil())
	    return false;
	this.finishMenu();
	return true;

    }
};

teltarif.initMenu();

