//&L(0,"*",21);
function DoorOpenCodeCheck(I){
  var s = I.value + '';
  if(!s.length == 0){
    var d = parseInt(I.value);
    var num=/^\d+$/
    if (!num.test(s)){
      alert("Please enter only numbers.")
	  I.value = I.defaultValue;
    } else if (d <= 99 && d >= 10) {
       alert('Door open code very short. Consider using a longer code for security reasons.');
    } else if (d <= 9){
	   alert('Door open code too short. Leave this field empty to disable this functionality.');
	   I.value = I.defaultValue;
	}
  }
}
function AudioBufferCheck(I){
  var s = I.value + '';
  if(!s.length == 0){
    var d = parseInt(I.value);
    var num=/^\d+$/
    if (!num.test(s)){
      alert("Please enter only numbers.")
	  I.value = I.defaultValue;
    } else if (d > 2000){
	   alert('Buffer level too big; max. 2000.');
	   I.value = I.defaultValue;
	}
  }
}
function PortCheck(I){
  var s = I.value + '';
  if(!s.length == 0){
    var d = parseInt(I.value);
    var num=/^\d+$/
    if (!num.test(s)){
      alert("Please enter only numbers.")
	  I.value = I.defaultValue;
    } else if (d > 65535){
	   alert('Port value too big; max. 65535');
	   I.value = I.defaultValue;
	}
  }
}

function CallOnLevelCheck(I){
  var s = I.value + '';
  if(!s.length == 0){
    var d = parseInt(I.value);
    var num=/^\d+$/
    if (!num.test(s)){
      alert("Please enter only numbers.")
	  I.value = I.defaultValue;
    } else if (d > 32767){
	   alert('Audio level too high; max. 32767.');
	   I.value = I.defaultValue;
	}
  }
}
function hwType(ht,p2p) 
{
//	if (p2p) ht = 0xff;
	if (ht != 25)
	{
		get_elem('col8').disabled = true;
		get_elem('col9').disabled = true;
		get_elem('col10').disabled = true;
		get_elem('col11').disabled = true;
		get_elem('col12').disabled = true;
		get_elem('col13').disabled = true;
		get_elem('col14').disabled = true;
		get_elem('col15').disabled = true;
	}
	
	switch(ht)
	{
//	case 0xff:
	case 14:
		get_elem('col0').disabled = true;
		get_elem('col1').disabled = true;
	case 7:
	case 19:
		get_elem('col2').disabled = true;
		get_elem('col3').disabled = true;
		get_elem('col4').disabled = true;
		get_elem('col5').disabled = true;
		get_elem('col6').disabled = true;
		get_elem('col7').disabled = true;
	break;
	case 41:
		get_elem('col1').disabled = true;
		get_elem('col2').disabled = true;
		get_elem('col3').disabled = true;
		get_elem('col4').disabled = true;
		get_elem('col5').disabled = true;
		get_elem('col6').disabled = true;
		get_elem('col7').disabled = true;
	break;
	default:
	}
}
function tln(sz$) { document.write("<tr><td width=200>"+sz$+"</td>"); }
function tel(sz$) { document.write("</tr>"); }
function ioState(desc,val,num_inp,iotype,sup_val) 
{
	/* 
	   desc                 - description to be printed
	   val                  - byte with the first bit of the input states
	   num_inp		- number of available inputs
	   iotype: inp 		- normal input
	           out 		- normal output
	     	   sup_inp 	- supervised input (ann 155)
	           x8_out	- X8 output
	   sup_val: 		- byte with the second bit of the supervised input states
	*/
	
	var result = 1; 	// get the input bit
	var result2 =0;		// get the sup_input bit
	
	tln(desc);
	for (i=7;i>=0;i--)
	{
		
		if (i > num_inp - 1){
			c$="<img src=\"ioX.gif\">";
		}else{
			result = val&(1<<i);
			if (result > 0) {
				switch (iotype){
					case "inp":
						c$='<img src=\"i1.gif\">';
						break;
					case "x8_inp":
					case "out":
						c$='<img src=\"o1.gif\">';
						break;
					case "sup_inp":
						result2 = sup_val&(1<<i);
						if (result2 > 0){
							c$='<img src=\"i3.gif\">';
						}else{
							c$='<img src=\"i1.gif\">';
						}
						break;
				}
			}else{
				switch (iotype){
					case "inp":
						c$='<img src=\"i0.gif\">';
						break;
					case "x8_inp":
					case "out":
						c$='<img src=\"o0.gif\">';
						break;
					case "sup_inp":
						result2 = sup_val&(1<<i);
						if (result2 > 0){
							c$='<img src=\"i2.gif\">';
						}else{
							c$='<img src=\"i0.gif\">';
						}
						break;
				}
			}
		}
		document.write("<td  align=center class=\"val\" >");
		document.write(c$);
		document.write("</td>");
	}
	tel();
}
function ioEnum(s$)
{
	tln(s$);
	for (i=7;i>=0;i--) document.write("<td align=center class=\"val\" ><b>"+i+"</td>");
	tel();
}
function x8state(x8,iconf,istate,ostate)
{
	if (!x8) {
		document.write("<tr><td width=200>X8 status:</td><td colspan=9><b>X8 not detected</b></td></tr>");
	} else
	{
		var c$;
		ioEnum("X8 I/O");
		tln("Pin configuration")
		for (i=7;i>=0;i--)
		{
			iconf&(1<<i) ? c$ = "<img src=\"out.png\"/>" : c$="<img src=\"in.png\"/>";
			document.write("<td align=center>");
			document.write(c$);
			document.write("</td>");
		}
		tel();
		ioState("Input register", istate,8,"x8_inp",0);
		ioState("Output register", ostate,8,"out",0);
	}
}
function ist(val)
{
	tln("Pin configuration");
}
function enableP2P()
{	
	var e;
	get_elem('ip_pbx').value = "";
	hide('ip_pbx_row');
	hide('ip_pbx2_row');
	hide('passwd_row');
	get_elem('sip_port').disabled = false;
	get_elem('rtp_port').disabled = false;
	return true;
}

function disableP2P(){ 
	show('ip_pbx_row');
	show('ip_pbx2_row');
	show('passwd_row');
	get_elem('sip_port').disabled = false;
	get_elem('rtp_port').disabled = false;
	return true;
}

function enableCoL() { get_elem('level').disabled = false; }
function disableCoL() { get_elem('level').disabled = true; }
function def_hideshow() {
	get_elem("prot").style.display = "block";
	get_elem("call").style.display = "none";
	get_elem("audi").style.display = "none";
	get_elem("drel").style.display = "none";
	get_elem("x8ex").style.display = "none";
	get_elem("apic").style.display = "none";
	get_elem("audr").style.display = "none";
	get_elem("bgms").style.display = "none";
	get_elem("sdev").style.display = "none";
	get_elem("sx8e").style.display = "none";
}
function ts(tid,tim) {
	if (get_elem(tid).style.display == 'none') {
		get_elem(tim).src = 'minus.png';
		get_elem(tid).style.display = 'block';
	} else {
		get_elem(tim).src = 'plus.png';
		get_elem(tid).style.display = 'none';
	}
}

function showHideCoL(show){
	if (show == 0){
		get_elem('levelext').style.display="none";
		get_elem('levelth').style.display="none"
		get_elem('levelclose').style.display="none"
	}else{
		get_elem('levelext').style.display="";
		get_elem('levelth').style.display=""
		get_elem('levelclose').style.display=""
	}
}

function showHideX8Opts(x8available){
	trArray = document.getElementsByTagName('tr');
	for (i=0;i < trArray.length;i++){
		if (trArray[i].getAttribute('cox8input')){
			if (x8available > 0){
				// X8 detected
				trArray[i].style.display = "";
			}else{
				// X8 not detected
				trArray[i].style.display = "none";
			}
		}
	}
}

function ShowSelectedProfileOnly(theProfileMode){
	SetCodecSelect();
	var theRowShowMode = '';
	var i=0;
	var trArray = document.getElementsByTagName('tr');
	for (i=0;i < trArray.length;i++){
		if (trArray[i].getAttribute('showinmode')){
			theRowShowMode = trArray[i].getAttribute('showinmode');
			if (theRowShowMode.indexOf(theProfileMode) < 0){
				trArray[i].style.display = "none";
			}else{
				trArray[i].style.display = "";
			}
		}
	}
}

function SetCodecSelect(theProfileMode){
	var codecElement;
	var g722_fdx;
	var g722_dec;
	var g722_selected = false;
	var g722_enable = 0;
	var selectedCodec=&LSetup(1,"%u",511,B);;
	var availableCodecs=&LState(1,"%u",45);;
	if (get_elem('audiocodec')){
		codecElement=get_elem('audiocodec');
	}else{
		return;
	}
	g722_fdx = availableCodecs & 4096; // get bit 12
	g722_dec = availableCodecs & 2048; // get bit 11
	
	if (selectedCodec == 12) g722_selected=true;
	// if device supports fdx 722, then enable it ...
	if (g722_fdx > 0) g722_enable=1;	
	// We may need g722 only on decoding devices, and in decoding profile (paging station) ...
	if (g722_dec > 0 && theProfileMode == 2) g722_enable=1;

	if (g722_enable > 0){
		// insert the g722 option
		codecElement.options[codecElement.options.length]=new Option('G.722 / 16 kHz',12,false,g722_selected);
	}
}
