/** 
* JavaScript für Modul Centrex/Telefon-Tastenbelegungen 
* @author Oliver Hallmann
* @since 17.12.2007
*/


/* Sucht Funktionsparameter zum Schluessel "key" und gibt diese zurueck */
function centrex_getFunction(key) {
	for (var i = 0; i < key_fc.length; i++) {
		if (key_fc[i]['id']==key) {
			return key_fc[i];
		}
	}
	return false;
}

function centrex_feld_test(feld,error) {
	if (feld.value=='') {
		alert('Eine Eingabe im Feld '+error+' wird benötigt');
		feld.focus();
		return false;
	}
	return true;
}

function centrex_submit() {
	fe=document.getElementById('fedit');
	ok=true;
	switch (fe.key_function.value) {
		case '31':
			ok=!centrex_feld_test(fe.line_sip_uri,'SIP-Uri')?false:ok;
			ok=!centrex_feld_test(fe.line_realm,'Realm')?false:ok;
			ok=!centrex_feld_test(fe.line_user_id,'User-ID')?false:ok;
			ok=!centrex_feld_test(fe.line_password,'Password')?false:ok;
			break;
	}
	if (ok) {
		btn=fe.btnSubmit;
		btn.disabled=true;
		btn.innerHTML='bitte warten...';
		fe.submit();
	}
}


function centrex_edit_function(id) {
	/* Quelle */
	f=document.getElementById(id);
	/* Ziel */
	fe=document.getElementById('fedit');
	/* key_fc und key_fc_shift sind global/array mit den funktionen */
	fe.txtTaste.value=(f.key_def_index.value>=1000?'Shift ':'') + f.key.value;
	fe.primary_id.value=f.primary_id.value;
	/* Select für Funktionsauswahl bauen */
	fe.key_function.options.length=0;
	if (f.key_def_index.value<1000) {
		for (var i = 0; i < key_fc.length; i++) {
			fe.key_function.options[i]=new Option(key_fc[i]['bezeichnung'],key_fc[i]['id']);
			if (key_fc[i]['id'] == f.key_function.value) {
				fe.key_function.options[i].selected=true;
			}
		}
	} else {
		/* Auswahl für Shift-Tasten */
		for (var i = 0; i < key_fc_shift.length; i++) {
			fe.key_function.options[i]=new Option(key_fc_shift[i]['bezeichnung'],key_fc_shift[i]['id']);
			if (key_fc_shift[i]['id'] == f.key_function.value) {
				fe.key_function.options[i].selected=true;
			}
		}
	}
	fe.key_label.value=f.key_label.value;
	fe.dial_code.value=f.dial_code.value;
	for (var i = 0; i < fe.type.length; ++i) {
		if (fe.type.options[i].value==f.type.value) fe.type.options[i].selected=true;
	}
	fe.line_primary.checked=(f.line_primary.value=='Y');
	fe.line_ring.checked=(f.line_ring.value=='Y');
	for (var i = 0; i < fe.line_hunt_ranking.length; ++i) {
		if (fe.line_hunt_ranking.options[i].value==f.line_hunt_ranking.value) fe.line_hunt_ranking.options[i].selected=true;
	}
	fe.line_sip_uri.value=f.line_sip_uri.value;
	fe.line_realm.value=f.line_realm.value;
	fe.line_user_id.value=f.line_user_id.value;
	fe.line_password.value=f.line_password.value;
	
	for (var i = 0; i < fe.line_shared_type.length; ++i) {
		if (fe.line_shared_type.options[i].value==f.line_shared_type.value) fe.line_shared_type.options[i].selected=true;
	}
	
	fe.line_intrusion_allowed.checked=(f.line_intrusion_allowed.value=='Y');
	fe.line_hold_active.checked=(f.line_hold_active.value=='Y');
	fe.key_def_index.value=f.key_def_index.value;
	centrex_update_edit_area();
	/* einblenden */
	document.getElementById('edit').style.display='block';
}

function centrex_update_edit_area(clear) {
	fe=document.getElementById('fedit');
	if (clear) {
		fe.key_label.value='';
		fe.dial_code.value='';
	}
	if (fe.key_label.value=='') {
		fe.key_label.value=fe.key_function.options[fe.key_function.selectedIndex].text;
	}
	
	/* Parameterfelder ein/ausblenden, je nach Funktions-Typ */
	func=centrex_getFunction(fe.key_function.options[fe.key_function.selectedIndex].value);
	dc=document.getElementById('id_dial_code');
	
	dc.style.display=func['dial_code']=='Y'?'':'none';
	document.getElementById('id_buttons').style.display=func['id']==30?'':'none';
	document.getElementById('id_type').style.display=func['id']==6?'':'none';
	line=func['id']==31?'':'none';
	document.getElementById('id_line1').style.display=line;
	document.getElementById('id_line2').style.display=line;
	document.getElementById('id_line3').style.display=line;
	document.getElementById('id_line4').style.display=line;
	document.getElementById('id_line5').style.display=line;
	document.getElementById('id_line6').style.display=line;
	document.getElementById('id_line7').style.display=line;
	document.getElementById('id_line8').style.display=line;
	document.getElementById('id_line9').style.display=line;
	document.getElementById('id_line10').style.display=line;
	
}

function centrex_btn(txt) {
	fe=document.getElementById('fedit');
	fe.dial_code.value=fe.dial_code.value+txt;
}


/* *************************** */

function centrex_update_edit_area_aastra(clear) {
	fe=document.getElementById('fedit');
	if (clear) {
		fe.key_label.value='';
		fe.dial_code.value='';
	}
	if (fe.key_label.value=='') {
		fe.key_label.value=fe.key_function.options[fe.key_function.selectedIndex].text;
	}
	
	/* Parameterfelder ein/ausblenden, je nach Funktions-Typ */
	func=centrex_getFunction(fe.key_function.options[fe.key_function.selectedIndex].value);
	dc=document.getElementById('id_dial_code');
	
	dc.readOnly=(func['dial_code']!='Y');
	if (func['id']!=4 && func['id']!=1 && dc.value=='') {
		dc.value=func['value'];
	}
	if (func['id']==1 && dc.value=='') {
		dc.value='95';
	}
	/*
	document.getElementById('id_buttons').style.display=func['id']==30?'':'none';
	document.getElementById('id_type').style.display=func['id']==6?'':'none';
	line=func['id']==31?'':'none';
	document.getElementById('id_line1').style.display=line;
	document.getElementById('id_line2').style.display=line;
	document.getElementById('id_line3').style.display=line;
	document.getElementById('id_line4').style.display=line;
	document.getElementById('id_line5').style.display=line;
	document.getElementById('id_line6').style.display=line;
	document.getElementById('id_line7').style.display=line;
	document.getElementById('id_line8').style.display=line;
	document.getElementById('id_line9').style.display=line;
	document.getElementById('id_line10').style.display=line;
	*/
}

function centrex_edit_function_aastra(id) {
	/* Quelle */
	f=document.getElementById(id);
	/* Ziel */
	fe=document.getElementById('fedit');
	/* key_fc und key_fc_shift sind global/array mit den funktionen */
	
	fe.txtTaste.value=(f.key_def_index.value>=1000?'Softkey ':'Top-Softkey ') + f.key.value;
	fe.primary_id.value=f.primary_id.value;
	/* Select für Funktionsauswahl bauen */
	fe.key_function.options.length=0;
	if (f.key_def_index.value<1000) {
		for (var i = 0; i < key_fc.length; i++) {
			fe.key_function.options[i]=new Option(key_fc[i]['bezeichnung'],key_fc[i]['id']);
			if (key_fc[i]['id'] == f.key_function.value) {
				fe.key_function.options[i].selected=true;
			}
		}
	} else {
		/* Auswahl für Shift-Tasten */
		for (var i = 0; i < key_fc_shift.length; i++) {
			fe.key_function.options[i]=new Option(key_fc_shift[i]['bezeichnung'],key_fc_shift[i]['id']);
			if (key_fc_shift[i]['id'] == f.key_function.value) {
				fe.key_function.options[i].selected=true;
			}
		}
	}
	fe.key_label.value=f.key_label.value;
	fe.dial_code.value=f.dial_code.value;
	/*
	for (var i = 0; i < fe.type.length; ++i) {
		if (fe.type.options[i].value==f.type.value) fe.type.options[i].selected=true;
	}
	fe.line_primary.checked=(f.line_primary.value=='Y');
	fe.line_ring.checked=(f.line_ring.value=='Y');
	for (var i = 0; i < fe.line_hunt_ranking.length; ++i) {
		if (fe.line_hunt_ranking.options[i].value==f.line_hunt_ranking.value) fe.line_hunt_ranking.options[i].selected=true;
	}
	fe.line_sip_uri.value=f.line_sip_uri.value;
	fe.line_realm.value=f.line_realm.value;
	fe.line_user_id.value=f.line_user_id.value;
	fe.line_password.value=f.line_password.value;
	
	for (var i = 0; i < fe.line_shared_type.length; ++i) {
		if (fe.line_shared_type.options[i].value==f.line_shared_type.value) fe.line_shared_type.options[i].selected=true;
	}
	
	fe.line_intrusion_allowed.checked=(f.line_intrusion_allowed.value=='Y');
	fe.line_hold_active.checked=(f.line_hold_active.value=='Y');
	*/
	fe.key_def_index.value=f.key_def_index.value;
	centrex_update_edit_area_aastra();
	/* einblenden */
	document.getElementById('edit').style.display='block';
}

function toggleTR(el,id,tag) {
	if (!tag) {tag='hidetab';}
	tag='#'+tag+id;
	if ($(tag).css('display') != 'none') {
		$(el).html('+');
	} else {
		$(el).html('-');
	}
	$(tag).toggle();
}

function set_wia_form() {
	p=$('#product_id').val();
	$('#fastpath').hide();
	$('#fastpath2').hide();
	$('#upstream').hide();
	$('#stoerung_onkz_row').hide();
	$('#splitter').hide();
	$('#row_pw_vertragsnummer').hide();
	if ( produkte[p] && produkte[p]['fastpath']=='Y') {
		if ($('#sa_typ').val()=='providerwechsel') {
			$('#fastpath2').show();
		} else {
			$('#fastpath').show();
		}
	}
	if ( produkte[p] && produkte[p]['upstream']=='Y') {
		$('#upstream').show();
	}
	if ($("input[name='typ']").val()=='ADSL Shared' && $('#sa_typ').val()=='neu_bekannt') {
		$('#splitter').show();
	} else {
		$("input[name='splitter_firma']").val('');
		$("input[name='splitter_strasse']").val('');
		$("input[name='splitter_hausnummer']").val('');
		$("input[name='splitter_plz']").val('');
		$("input[name='splitter_ort']").val('');
		$('#splitter').hide();
	}
	if ($('#anrede').val()=='Firma') {
		$('#lblVorname').html('Vor-/Nachname:');
		$('#lblNachname').html('Firma:');
	} else {
		$('#lblVorname').html('Vorname:');
		$('#lblNachname').html('Nachname:');
	}
	if ($('#sa_typ')) {
		switch ($('#sa_typ').val()) {
			case 'neu_unbekannt':
				$('#onkz').val('');
				$('#rufnummer').val('');
				$('#sa_typ_row').hide();
				$('#stoerung_onkz_row').show();
				break;
			case 'providerwechsel':
				$('#stoerung_onkz_row').hide();
				break;
			default:
				$('#sa_typ_row').show();
				$('#stoerung_onkz_row').hide();
				break;
		}
	}
	if ($("#f1>input[name='typ']").val()=='SDSL') {
		$('#stoerung_onkz_row').show();
		$('#sa_typ_row').hide();
		$('#onkz').val('');
		$('#rufnummer').val('');
	};
	if (	$("#f1>input[name='typ']").val()=='ADSL Stand Alone' &&
				$('#sa_typ').val()=='providerwechsel')
	{
		$('#row_pw_vertragsnummer').show();
	}
}

function mk_confirm(o,txt) {
	if (confirm(txt)) {
		return true;
	} else {
		return false;
	}
}

function openMonitor(url) {
	w=window.open(url,'monitor','location=no,menubar=no,resizeable=yes,scrollbars=yes,status=no,toolbar=no');
	w.moveTo(0,0);
	w.resizeTo(screen.width,screen.height);
	w.focus();
}
