function PortCheck(I){
	d =parseInt(I.value, 10);
	if ( !( d<65536 && d>=0) ) {
		alert('Port value is out of range [0 - 65535]');
		I.value = I.defaultValue;
	}
}

function IPCheck(I){
	d =parseInt(I.value, 10);
	if ( !(d<256 && d>=0) ) {
		alert('IP value is out of range [0 - 255]');
		I.value = I.defaultValue;
	}
}

function netMaskCheck(I,last){
	d =parseInt(I.value, 10);
	if( !(d==0 || d==128 || d==192 || d==224 || d==240 || d==248 || d==252 || d==254 || (!last && d==255) )) {
		alert('Incorrect netmask value');
		I.value = I.defaultValue;
	}
}


function ToSCheck(I)
{
  d = parseInt(I.value,10);
  if( d > 63 || d < 0 ){
     alert('Type of service/DSCP is out of range [0 - 63]');
	I.value = I.defaultValue;
  }
}

function gpi_check(){
	f=document.forms["settings"];
        x=(f.B352.selectedIndex!=3)?"none":"";

	set_display('control_gpi',x);
	set_display('gpi_polarity',x);
}

/* set form element visibility via CSS */
function set_display(id,display)
{
	var c=parent.frames['cfg'].document;
	obj=c.getElementById(id);
	if (obj) obj.style.display=display;
}


/* switch between VBR and CBR in MPEG modes */
function select_bitrate() {
	var f=document.forms["settings"];
	var fmt=f.B486.selectedIndex;		/* audio format: MP3 <6 */
	var is_mp3=(fmt<6);

	if (is_mp3) {
		set_display('mp3_bitrate',(f.B504.selectedIndex==0)?"none":"");	/* disable bitrate if VBR selected */
		set_display('mp3_quality',(f.B504.selectedIndex==0)?"":"none");	/* enable quality if VBR selected */
	} else {
		/* MP3 not selected */
		set_display('mp3_bitrate',"none");
		set_display('mp3_quality',"none");
	}
}

/* generate list of bitrates depending on MPEG type */
function generate_bitrate_select() {
	var high_bitrates={
		4	:	'32 kbps',
		5	:	'40 kbps',
		6	:	'48 kbps',
		7	:	'56 kbps',
		8	:	'64 kbps',
		10	:	'80 kbps',
		12	:	'96 kbps',
		14	:	'112 kbps',
		16	:	'128 kbps',
		20	:	'160 kbps',
		24	:	'192 kbps',
		28	:	'224 kbps',
		32	:	'256 kbps',
		40	:	'320 kbps'
	};
	var low_bitrates={
		1	:	'8 kbps',
		2	:	'16 kbps',
		3	:	'24 kbps',
		4	:	'32 kbps',
		5	:	'40 kbps',
		6	:	'48 kbps',
		7	:	'56 kbps',
		8	:	'64 kbps',
		10	:	'80 kbps',
		12	:	'96 kbps',
		14	:	'112 kbps',
		16	:	'128 kbps',
		18	:	'144 kbps',
		20	:	'160 kbps'
	};
	var f=document.forms["settings"];
	var fmt=f.B486.selectedIndex;
	var select=f.B505;
	var selected_value=parseInt(select.options[select.selectedIndex].value,10);

	if (fmt<=2) { /* MPEG 1: list high bitrates */
		bitrates=high_bitrates;
	}
	else {	/* if non MPEG format is selected doesn't really matter what is in the list */
		/* MPEG 2: list low bitrates */
		bitrates=low_bitrates;
	}
	/* clear the list */
	select.options.length=0;

	/* repopulate the list */
	for(index in bitrates) {
		select.options[select.options.length] = new Option(bitrates[index], index);
	}

	/* re-select the original entry by value*/
	select.selectedIndex=select.options.length-1;			/* maximum if nothing found */
	for(index=0;index<select.options.length;index++) {			/* search for the nearest higher */
		if (selected_value <= parseInt(select.options[index].value,10)) {
			select.selectedIndex=index;
			break;
		}
	}
}

function show_trigger_level(){
	f=document.forms["settings"];
        x=(f.B352.selectedIndex!=1)?"none":"";
	set_display('trigger_level',x);
	set_display('pre_trigger',x);
	set_display('post_trigger',x);
}



function show_mp3_settings()
{
	var f=document.forms["settings"];
	var fmt=f.B486.selectedIndex;		/* audio format: MP3 <6 */
	var show=(fmt<6)?"":"none";

	set_display('mp3_br_mode',show);
	set_display('mp3_crc',show);
	set_display('mp3_reservoir',show);
	set_display('mp3_chan_ext',show);
	set_display('mp3_copyright',show);
	set_display('mp3_str_type',show);
	set_display('mp3_emphasis',show);
	select_bitrate();		/* hide/show bitrate, quality */
}

function translate_hw_type(element)
{
	var hw_list = [];

	hw_list["1"] = "Legacy 1";
	hw_list["2"] = "Legacy 2";
	hw_list["3"] = "Legacy 3";
	hw_list["4"] = "Legacy 4";
	hw_list["5"] = "Legacy 5";
	hw_list["6"] = "Legacy 6";
	hw_list["7"] = "Annuncicom 100";
	hw_list["8"] = "Instreamer 100";
	hw_list["9"] = "Exstreamer Digital";
	hw_list["13"] = "IPAM 100";
	hw_list["14"] = "Exstreamer 100";
	hw_list["15"] = "Exstreamer 200";
	hw_list["16"] = "IPAM Evaluation kit";
	hw_list["17"] = "Annuncicom 1000";
	hw_list["18"] = "Bosch IP Audio";
	hw_list["19"] = "Annuncicom 200";
	hw_list["20"] = "Exstreamer 110";
	hw_list["21"] = "Exstreamer 1000";
	hw_list["22"] = "Barionet 200";
	hw_list["25"] = "Annuncicom PS16";
	hw_list["26"] = "Barionet 50";
	hw_list["32"] = "Annuncicom 155";
	hw_list["33"] = "Annuncicom VME";
	hw_list["34"] = "Exstreamer 120";
	hw_list["35"] = "Exstreamer 500";
	hw_list["36"] = "Exstreamer P5";
	hw_list["37"] = "Exstreamer 105";
	hw_list["38"] = "Exstreamer 205";
	hw_list["39"] = "IPAM 101";
	hw_list["40"] = "IPAM 102";
	hw_list["41"] = "Annuncicom 60";
	hw_list["42"] = "Annuncicom PS1";
	hw_list["43"] = "Instreamer";
	hw_list["44"] = "Annuncicom 50 OEM";
	hw_list["45"] = "IPAM 301";
	hw_list["46"] = "IPAM 302";
	hw_list["93"] = "Barionet";


	e=document.getElementById(element);
	num=e.innerHTML;
	text = hw_list[num];

	if (text!="") e.innerHTML=text;
	else e.innerHTML=num;

	if (e.style.display=="none")	e.style.display="inline";
}



function translate_ipam_type(element)
{
	var ipam_list = [];

	ipam_list["0"] = "generic";
	ipam_list["1"] = "IPAM 101";
	ipam_list["2"] = "IPAM 102";
	ipam_list["3"] = "IPAM 301";
	ipam_list["4"] = "IPAM 302";


	e=document.getElementById(element);
	num=e.innerHTML;
	text = ipam_list[num];

	if (text!="") e.innerHTML=text;
	else e.innerHTML=num;

	if (e.style.display=="none")	e.style.display="inline";
}


