//Parameter's functions, Anzeige der richtigem Einstellung des Config Strings
function screen(key,conf){
	if (key=="screen" && conf[key]==1)
		document.getElementById(key).checked=true;
	else if (key=="screen" && conf[key]==0)
		document.getElementById(key).checked=false;
}

function cd(key,conf){
	if (key=="cd" && conf[key]==1)
		document.getElementById(key).checked=true;
	else if (key=="cd" && conf[key]==0)
		document.getElementById(key).checked=false;
}

function pres(key,conf){
	if (key=="pres" && conf[key]==1)
		document.getElementById(key).checked=true;
	else if (key=="pres" && conf[key]==0)
		document.getElementById(key).checked=false;
}

function ec(key,conf){
	if (key=="ec" && conf[key]==1)
		document.getElementById(key).checked=true;
	else if (key=="ec" && conf[key]==0)
		document.getElementById(key).checked=false;
}

function ectl(key,conf){
	if (key=="ectl")
		document.getElementById(key).value=conf[key];
}

function dialplans(key,conf){
	if (key=="dnumplan" || key=="onumplan" /*|| key=="rnumplan" || key=="cpnnumplan"*/){
		if (conf[key]==4)
		conf[key]=3
		document.getElementById(key).options[conf[key]].selected=true;
	}
}

function bearer_cap(key,conf){
	if (key=="bearer_cap") {
		for (i = 0; i < document.config.bearer_cap.length; i++){
    		if (document.config.bearer_cap.options[i].value == conf[key] )
			conf[key]=i;
		}
		document.getElementById(key).options[conf[key]].selected=true;
	}
}

function ie_on_sip(key,conf){
		if (key=="ie_on_sip" && conf[key]==1)
			document.getElementById(key).checked=true;
		else if (key=="ie_on_sip" && conf[key]==0)
			document.getElementById(key).checked=false;

}

function wait_for_cancel(key,conf){

		if (key=="wait_for_cancel" && conf[key]==1)
			document.getElementById(key).checked=true;
		else if (key=="wait_for_cancel" && conf[key]==0)
			document.getElementById(key).checked=false;

}

function t38(key,conf){
		if (key=="t38" && conf[key]==1)
			document.getElementById(key).checked=true;
		else if (key=="t38" && conf[key]==0)
			document.getElementById(key).checked=false;
}

function analog_oad_setting(key,conf){

		if (key=="analog_oad_setting") {
			for (i = 0; i < document.config.analog_oad_setting.length; i++){
    			if (document.config.analog_oad_setting.options[i].value == conf[key]){
				document.getElementById(key).options[i].selected=true;
				document.getElementById(key+'_manual').style.display="none";
				}
			}
			if (document.getElementById(key).options[document.getElementById(key).selectedIndex].value!=conf[key]){
				document.getElementById(key).options[document.config.analog_oad_setting.length-1].selected=true;
				document.getElementById(key+'_manual').style.display="";
				document.getElementById(key+'_manual').value=conf[key];
			}
		}

}

function analog_display_setting(key,conf){

		if (key=="analog_display_setting") {
			for (i = 0; i < document.config.analog_display_setting.length; i++){
    			if (document.config.analog_display_setting.options[i].value == conf[key]){
				document.getElementById(key).options[i].selected=true;
				document.getElementById(key+'_manual').style.display="none";
				}
			}
			if (document.getElementById(key).options[document.getElementById(key).selectedIndex].value!=conf[key]){
				document.getElementById(key).options[document.config.analog_display_setting.length-1].selected=true;
				document.getElementById(key+'_manual').style.display="";
				document.getElementById(key+'_manual').value=conf[key];
			}
		}
}

function isdn_oad2_setting(key,conf){

		if (key=="isdn_oad2_setting") {
			for (i = 0; i < document.config.isdn_oad2_setting.length; i++){
    			if (document.config.isdn_oad2_setting.options[i].value == conf[key]){
				document.getElementById(key).options[i].selected=true;
				document.getElementById(key+'_manual').style.display="none";
				}
			}
			if (document.getElementById(key).options[document.getElementById(key).selectedIndex].value!=conf[key]){
				document.getElementById(key).options[document.config.isdn_oad2_setting.length-1].selected=true;
				document.getElementById(key+'_manual').style.display="";
				document.getElementById(key+'_manual').value=conf[key];
			}
		}
}

function isdn_oad_setting(key,conf){

		if (key=="isdn_oad_setting") {
			for (i = 0; i < document.config.isdn_oad_setting.length; i++){
    			if (document.config.isdn_oad_setting.options[i].value == conf[key]){
				document.getElementById(key).options[i].selected=true;
				document.getElementById(key+'_manual').style.display="none";
				}
			}
			if (document.getElementById(key).options[document.getElementById(key).selectedIndex].value!=conf[key]){
				document.getElementById(key).options[document.config.isdn_oad_setting.length-1].selected=true;
				document.getElementById(key+'_manual').style.display="";
				document.getElementById(key+'_manual').value=conf[key];
			}
		}
}

function isdn_qsigname_setting(key,conf){

		if (key=="isdn_qsigname_setting") {
			for (i = 0; i < document.config.isdn_qsigname_setting.length; i++){
    			if (document.config.isdn_qsigname_setting.options[i].value == conf[key]){
				document.getElementById(key).options[i].selected=true;
				document.getElementById(key+'_manual').style.display="none";
				}
			}
			if (document.getElementById(key).options[document.getElementById(key).selectedIndex].value!=conf[key]){
				document.getElementById(key).options[document.config.isdn_qsigname_setting.length-1].selected=true;
				document.getElementById(key+'_manual').style.display="";
				document.getElementById(key+'_manual').value=conf[key];
			}
		}
}

function isdn_redirected_nr_setting(key,conf){

		if (key=="isdn_redirected_nr_setting") {
			for (i = 0; i < document.config.isdn_redirected_nr_setting.length; i++){
    			if (document.config.isdn_redirected_nr_setting.options[i].value == conf[key]){
				document.getElementById(key).options[i].selected=true;
				document.getElementById(key+'_manual').style.display="none";
				}
			}
			if (document.getElementById(key).options[document.getElementById(key).selectedIndex].value!=conf[key]){
				document.getElementById(key).options[document.config.isdn_redirected_nr_setting.length-1].selected=true;
				document.getElementById(key+'_manual').style.display="";
				document.getElementById(key+'_manual').value=conf[key];
			}
		}
}

function sip_from_user_setting(key,conf){

		if (key=="sip_from_user_setting") {
			for (i = 0; i < document.config.sip_from_user_setting.length; i++){
    			if (document.config.sip_from_user_setting.options[i].value == conf[key])
				document.getElementById(key).options[i].selected=true;
			}
			if (document.getElementById(key).options[document.getElementById(key).selectedIndex].value!=conf[key]){
				document.getElementById(key).options[document.config.sip_from_user_setting.length-1].selected=true;
				document.getElementById(key+'_manual').style.display="";
				document.getElementById(key+'_manual').value=conf[key];
			}
		}
}

function sip_from_display_setting(key,conf){

		if (key=="sip_from_display_setting") {
			for (i = 0; i < document.config.sip_from_display_setting.length; i++){
    			if (document.config.sip_from_display_setting.options[i].value == conf[key])
				document.getElementById(key).options[i].selected=true;
			}
			if (document.getElementById(key).options[document.getElementById(key).selectedIndex].value!=conf[key]){
				document.getElementById(key).options[document.config.sip_from_display_setting.length-1].selected=true;
				document.getElementById(key+'_manual').style.display="";
				document.getElementById(key+'_manual').value=conf[key];
			}
		}
}

function from_pstn_src_setting(key,conf){

		if (key=="from_pstn_src_setting") {
			for (i = 0; i < document.config.from_pstn_src_setting_analog.length; i++){

    			if (document.config.from_pstn_src_setting_analog.options[i].value == conf[key])
				document.config.from_pstn_src_setting_analog.options[i].selected=true;
			}
			for (i = 0; i < document.config.from_pstn_src_setting_isdn.length; i++){
    			if (document.config.from_pstn_src_setting_isdn.options[i].value == conf[key])
				document.config.from_pstn_src_setting_isdn.options[i].selected=true;
			}
		}

}

function from_sip_src_setting(key,conf){

		if (key=="from_sip_src_setting") {
			for (i = 0; i < document.config.from_sip_src_setting.length; i++){
    			if (document.config.from_sip_src_setting.options[i].value == conf[key])
				document.getElementById(key).options[i].selected=true;
			}
		}

}

function dtmfmode(key,conf){

		if (key=="dtmfmode") {
			for (i = 0; i < document.config.dtmfmode.length; i++){
    			if (document.config.dtmfmode.options[i].value == conf[key])
				//conf[sip.options[sip.selectedIndex].value][key]=i;
				document.getElementById(key).options[i].selected=true;
			}
			//document.getElementById(key).value=conf[sip.options[sip.selectedIndex].value][key];
		}

}

function s2i_cpt(key,conf){

		if (key=="s2i_cpt") {
			for (i = 0; i < document.config.s2i_cpt.length; i++){
    			if (document.config.s2i_cpt.options[i].value == conf[key])
				//conf[sip.options[sip.selectedIndex].value][key]=i;
				document.getElementById(key).options[i].selected=true;
			}
			//document.getElementById(key).value=conf[sip.options[sip.selectedIndex].value][key];
		}

}

function i2s_cpt(key,conf){

		if (key=="i2s_cpt") {
			for (i = 0; i < document.config.i2s_cpt.length; i++){
    			if (document.config.i2s_cpt.options[i].value == conf[key])
				//conf[sip.options[sip.selectedIndex].value][key]=i;
				document.getElementById(key).options[i].selected=true;
			}
			//document.getElementById(key).value=conf[sip.options[sip.selectedIndex].value][key];
		}

}

function codecs(key,conf){
	if (key=="codecs") {
		var sel = document.getElementById("sel");
		var sel1 = document.getElementById("sel1");

		while (sel.length>0){
			sel.remove(sel.length-1);
		}
		while (sel1.length>0){
			sel1.remove(sel1.length-1);
		}
		var option1 = new Option(); option1.text='pcma'; option1.value='pcma'; sel1.options[sel1.length]=option1;
		var option2 = new Option(); option2.text='pcmu'; option2.value='pcmu'; sel1.options[sel1.length]=option2;
		var option3 = new Option(); option3.text='gsm'; option3.value='gsm'; sel1.options[sel1.length]=option3;
		var option4 = new Option(); option4.text='g729'; option4.value='g729'; sel1.options[sel1.length]=option4;
		var option5 = new Option(); option5.text='g723'; option5.value='g723'; sel1.options[sel1.length]=option5;
		var option5 = new Option(); option5.text='g726-32'; option5.value='g726-32'; sel1.options[sel1.length]=option5;

			var cod= conf[key].split(":");
			for (i=0;i<cod.length;i++){
				var newOption = new Option(); // Create a new instance of ListItem
				newOption.text = cod[i];
				newOption.value = cod[i];
				sel.options[sel.length] = newOption; //Append the item in Target Listbox
			}
			var index;
			for (i=0;i<cod.length;i++){
				for (j=0;j<sel1.options.length;j++){
					if (cod[i]==sel1.options[j].value) index=j;
				}
				sel1.remove(index);
			}
	}
}
//END parameters functions

//Der Feld für Kommentare wird auf 100 Zeichen begrenzt


function resize(){
	if(window.sizeToContent) window.sizeToContent();
}
function closeW(){
	this.window.close();
}

//Funktion für die Bewegung von Items von einem Listbox in den Anderen.
function move(Orig, Dest){
	var varFromBox = document.getElementById(Orig);
	var varToBox = document.getElementById(Dest);
	if ((varFromBox != null) && (varToBox != null))
	{
		if(varFromBox.length < 1)
		{
			alert('There are no items in the source ListBox');
			return false;
		}
		if(varFromBox.options.selectedIndex == -1) // when no Item is selected the index will be -1

		{
			alert('Please select an Item to move');
			return false;
		}
		while ( varFromBox.options.selectedIndex >= 0 )
		{
			var newOption = new Option(); // Create a new instance of ListItem

			newOption.text = varFromBox.options[varFromBox.options.selectedIndex].text;
			newOption.value = varFromBox.options[varFromBox.options.selectedIndex].value;
			varToBox.options[varToBox.length] = newOption; //Append the item in Target Listbox

			varFromBox.remove(varFromBox.options.selectedIndex); //Remove the item from Source Listbox

		}
	}
	return false;
}

function selectall(){
	selectBox = document.getElementById('sel');
	for (var i = 0; i < selectBox.options.length; i++) {
		selectBox.options[i].selected = true;
	}
}

function up() {
	obj=document.getElementById('sel');
	for (var i = 0; i < obj.options.length; i++)
		if (obj.options[ i ].selected){
			if (i>0) change(obj,i,i-1);
		}

}

function down() {
	obj=document.getElementById('sel');
	for (var i = obj.options.length-1; i >= 0; i--){
		if (obj.options[ i ].selected){
			if ( i<obj.length-1) change(obj,i,i+1);
		}
	}
}

function change(obj,num1,num2) {
	proVal=obj.options[num1].value;
	proTex=obj.options[num1].text;
	obj.options[num1].value=obj.options[num2].value;
	obj.options[num1].text=obj.options[num2].text;
	obj.options[num2].value=proVal;
	obj.options[num2].text=proTex;
	obj.options[num2].selected=true;
	obj.options[num1].selected=false;
}

function showdef_(conf,config,from,to){

	var c=config.split(",");
	for (i=0;i<c.length;i++){
		var v=c[i].split("=");
		if (v[0]=='codecs'){
			document.getElementById('sel').disabled=false;
			document.getElementById('sel1').disabled=false;
			document.getElementById('upB').disabled=false;
			document.getElementById('downB').disabled=false;
			document.getElementById('addB').disabled=false;
			document.getElementById('removeB').disabled=false;
		}else if (v[0]=='from_pstn_src_setting'){
			if (from=='analog' || to=='analog') document.getElementById('from_pstn_src_setting_analog').disabled=false;
			if (from=='isdn' || to=='isdn') document.getElementById('from_pstn_src_setting_isdn').disabled=false;
			var check_def=document.getElementById("def_from_pstn_src_setting_analog");
			if (check_def!=null && (from=='analog' || to=='analog')) check_def.checked=false;
			var check_def=document.getElementById("def_from_pstn_src_setting_isdn");
			if (check_def!=null && (from=='isdn' || to=='isdn')) check_def.checked=false;
		}else{
			var field=document.getElementById(v[0]);
			if (field!=null) field.disabled=false;
		}
		var check_def=document.getElementById("def_"+v[0]);
		if (check_def!=null) check_def.checked=false;
	}


	for (var key in conf['from']){
		screen(key,conf);
		pres(key,conf['from']);
		ec(key,conf['from']);
		ectl(key,conf['from']);
		dialplans(key,conf['from']);
		bearer_cap(key,conf['from']);
		cd(key,conf['from']);
		t38(key,conf,conf['from']);
		dtmfmode(key,conf['from']);
		s2i_cpt(key,conf['from']);
		i2s_cpt(key,conf['from']);
		codecs(key,conf['from']);
		ie_on_sip(key,conf['from']);
		wait_for_cancel(key,conf['from']);
		analog_oad_setting(key,conf['from']);
		analog_display_setting(key,conf['from']);
		from_pstn_src_setting(key,conf['from']);
		sip_from_user_setting(key,conf['from']);
		sip_from_display_setting(key,conf['from']);
		from_sip_src_setting(key,conf['from']);
		isdn_oad_setting(key,conf['from']);
		isdn_oad2_setting(key,conf['from']);
		isdn_qsigname_setting(key,conf['from']);
		isdn_redirected_nr_setting(key,conf['from']);

	}
	for (var key in conf['to']){
		screen(key,conf['to']);
		pres(key,conf['to']);
		ec(key,conf['to']);
		ectl(key,conf['to']);
		dialplans(key,conf['to']);
		bearer_cap(key,conf['to']);
		cd(key,conf['to']);
		t38(key,conf['to']);
		dtmfmode(key,conf['to']);
		s2i_cpt(key,conf['to']);
		i2s_cpt(key,conf['to']);
		codecs(key,conf['to']);
		ie_on_sip(key,conf['to']);
		wait_for_cancel(key,conf['to']);
		analog_oad_setting(key,conf['to']);
		from_pstn_src_setting(key,conf['to']);
		sip_from_user_setting(key,conf['to']);
		sip_from_display_setting(key,conf['to']);
		from_sip_src_setting(key,conf['to']);
		isdn_oad_setting(key,conf['to']);
		isdn_oad2_setting(key,conf['to']);
		isdn_qsigname_setting(key,conf['to']);
		isdn_redirected_nr_setting(key,conf['to']);

	}

}

function hidec(){
	var config_isdn=document.getElementById('config_isdn');
	var config_sip=document.getElementById('config_sip');
	var config_analog=document.getElementById('config_analog');
	var config_additional=document.getElementById('config_additional');
	var buttons=document.getElementById('buttons');
	config_sip.style.display="none";
	config_additional.style.display="none";
	config_isdn.style.display="none";
	config_analog.style.display="none";
	buttons.style.display="none";
}

//Config String tabelle anzeigen oder ausblenden (ISDN)
function show_config_isdn(){
	var config_isdn=document.getElementById('config_isdn');
	var config_sip=document.getElementById('config_sip');
	var config_analog=document.getElementById('config_analog');
	var config_additional=document.getElementById('config_additional');
	var buttons=document.getElementById('buttons');
	if (config_isdn.style.display=="none"){
		config_isdn.style.display="";
		buttons.style.display="";
		config_sip.style.display="none";
		config_analog.style.display="none";
		config_additional.style.display="none";
	}else{
		config_isdn.style.display="none";
		buttons.style.display="none";
	}
}

//Config String tabelle anzeigen oder ausblenden (Additional)
function show_config_addit(){
	var config_isdn=document.getElementById('config_isdn');
	var config_analog=document.getElementById('config_analog');
	var config_additional=document.getElementById('config_additional');
	var config_sip=document.getElementById('config_sip');
	var buttons=document.getElementById('buttons');
	if (config_additional.style.display=="none"){
		config_additional.style.display="";
		buttons.style.display="";
		config_sip.style.display="none";
		config_isdn.style.display="none";
		config_analog.style.display="none";
		$("#addit_options").focus();
	}else{
		config_additional.style.display="none";
		buttons.style.display="none";
	}
}

//Config String tabelle anzeigen oder ausblenden (SIP)
function show_config_sip(){
	var config_sip=document.getElementById('config_sip');
	var config_isdn=document.getElementById('config_isdn');
	var config_analog=document.getElementById('config_analog');
	var config_additional=document.getElementById('config_additional');
	var buttons=document.getElementById('buttons');
	if (config_sip.style.display=="none"){
		config_sip.style.display="";
		buttons.style.display="";
		config_isdn.style.display="none";
		config_analog.style.display="none";
		config_additional.style.display="none";
	}else{
		config_sip.style.display="none";
		buttons.style.display="none";
	}
}

//Config String tabelle anzeigen oder ausblenden (ANALOG)
function show_config_analog(){
	var config_sip=document.getElementById('config_sip');
	var config_isdn=document.getElementById('config_isdn');
	var config_analog=document.getElementById('config_analog');
	var config_additional=document.getElementById('config_additional');
	var buttons=document.getElementById('buttons');
	if (config_analog.style.display=="none"){
		config_analog.style.display="";
		buttons.style.display="";
		config_sip.style.display="none";
		config_isdn.style.display="none";
		config_additional.style.display="none";
	}else{
		config_analog.style.display="none";
		buttons.style.display="none";
	}
}


function disable(){
	var ec= document.getElementById("ec");
	ec.disabled=true;
	var ectl= document.getElementById("ectl");
	ectl.disabled=true;
	var ie_on_sip= document.getElementById("ie_on_sip");
	ie_on_sip.disabled=true;
	var wait_for_cancel= document.getElementById("wait_for_cancel");
	wait_for_cancel.disabled=true;
	var dnumplan = document.getElementById("dnumplan");
	dnumplan.disabled=true;
	var onumplan= document.getElementById("onumplan");
	onumplan.disabled=true;
	var screen= document.getElementById("screen");
	screen.disabled=true;
	var pres= document.getElementById("pres");
	pres.disabled=true;
	var t38= document.getElementById("t38");
	t38.disabled=true;
	var dtmfmode= document.getElementById("dtmfmode");
	dtmfmode.disabled=true;
	var s2i_cpt= document.getElementById("s2i_cpt");
	s2i_cpt.disabled=true;
	var i2s_cpt= document.getElementById("i2s_cpt");
	i2s_cpt.disabled=true;
	var codecs= document.getElementById("sel");
	codecs.disabled=true;
	var codecs1= document.getElementById("sel1");
	codecs1.disabled=true;
	var upB= document.getElementById("upB");
	upB.disabled=true;
	var downB= document.getElementById("downB");
	downB.disabled=true;
	var addB= document.getElementById("addB");
	addB.disabled=true;
	var removeB= document.getElementById("removeB");
	removeB.disabled=true;
	var bearer_cap= document.getElementById("bearer_cap");
	bearer_cap.disabled=true;
	var cd= document.getElementById("cd");
	cd.disabled=true;
	var isdn_oad_setting= document.getElementById("isdn_oad_setting");
	isdn_oad_setting.disabled=true;
	var isdn_oad2_setting= document.getElementById("isdn_oad2_setting");
	isdn_oad2_setting.disabled=true;
	var isdn_qsigname_setting= document.getElementById("isdn_qsigname_setting");
	isdn_qsigname_setting.disabled=true;
	var isdn_redirected_nr_setting= document.getElementById("isdn_redirected_nr_setting");
	isdn_redirected_nr_setting.disabled=true;
	var from_pstn_src_setting_analog= document.getElementById("from_pstn_src_setting_analog");
	from_pstn_src_setting_analog.disabled=true;
	var from_pstn_src_setting_isdn= document.getElementById("from_pstn_src_setting_isdn");
	from_pstn_src_setting_isdn.disabled=true;
	var sip_from_user_setting= document.getElementById("sip_from_user_setting");
	sip_from_user_setting.disabled=true;
	var sip_from_display_setting= document.getElementById("sip_from_display_setting");
	sip_from_display_setting.disabled=true;
	var from_sip_src_setting= document.getElementById("from_sip_src_setting");
	from_sip_src_setting.disabled=true;
	var analog_oad_setting= document.getElementById("analog_oad_setting");
	analog_oad_setting.disabled=true;
	var analog_display_setting= document.getElementById("analog_display_setting");
	analog_display_setting.disabled=true;
}

function hide_elements(from_to){
	hidec();
	var t38_tr=document.getElementById("t38_tr");
	var dtmf_tr=document.getElementById("dtmf_tr");
	var s2i_cpt_tr=document.getElementById("s2i_cpt_tr");
	var i2s_cpt_tr=document.getElementById("i2s_cpt_tr");
	var ie_on_sip_tr=document.getElementById("ie_on_sip_tr");
	var wait_for_cancel_tr=document.getElementById("wait_for_cancel_tr");
	var codecs_tr=document.getElementById("codecs_tr");
	var from_sip_src_setting_tr=document.getElementById("from_sip_src_setting_tr");
	var sip_from_user_setting_tr=document.getElementById("sip_from_user_setting_tr");
	var sip_from_display_setting_tr=document.getElementById("sip_from_display_setting_tr");
	var caller_id_mapping_tr=document.getElementById("caller_id_mapping_tr");
	 if ( from_to=="isdn-isdn" ){
			t38_tr.style.display="none";
			dtmf_tr.style.display="none";
			s2i_cpt_tr.style.display="";
			i2s_cpt_tr.style.display="";
			codecs_tr.style.display="none";
			wait_for_cancel_tr.style.display="none";
			from_sip_src_setting_tr.style.display="none";
			sip_from_user_setting_tr.style.display="none";
			sip_from_display_setting_tr.style.display="none";
			caller_id_mapping_tr.style.display="none";
			ie_on_sip_tr.style.display="";
		}else{
			t38_tr.style.display="";
			dtmf_tr.style.display="";
			i2s_cpt_tr.style.display="";
			codecs_tr.style.display="";
			ie_on_sip_tr.style.display="";
			from_sip_src_setting_tr.style.display="";
			sip_from_user_setting_tr.style.display="";
			sip_from_display_setting_tr.style.display="";
			caller_id_mapping_tr.style.display="";
		}
}


function is_submit(form)
{
	var elem = document.getElementsByTagName("input");
	for (i=0;i<elem.length;i++){
		if (elem[i].type=="text"){
			elem[i].value= elem[i].value.replace(/\"/g,"\"");
			elem[i].value= elem[i].value.replace(/\'/g,"\"");
		}
	}
	return true;
}
