//&L(0,"*",1);
var xmlhttp=false;
var c="";
/*@cc_on @*/

/*@if (@_jscript_version >= 5)
	try {
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
			xmlhttp = false;
		}
	}
@else
	xmlhttp = false;
@end @*/

if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
	try {
		xmlhttp = new XMLHttpRequest();
	} catch (e) {
		xmlhttp = false;
	}
}

function loadDoc(docname) {
	if (xmlhttp) {
		xmlhttp.open("GET",docname,true);
		xmlhttp.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
		xmlhttp.onreadystatechange=function() {
			if (xmlhttp.readyState==4) {
				c = xmlhttp.responseText;
			}
		}
		xmlhttp.send(null)
	}
}

/* get setup info here */
var htype = &LState(1,"%u",28);;
var inp   = &LSetup(1,"%u",510,B);;
var aft   = &LSetup(1,"%u",511,B);;
var srt   = &LSetup(1,"%u",512,B);;
var qsr   = &LSetup(1,"%u",513,B);;
var wmode = &LSetup(1,"%u",536,B);;
var r1    = &LSetup(1,"%u",530,B);;
var r2    = &LSetup(1,"%u",531,B);;
var r3    = &LSetup(1,"%u",532,B);;
var r4    = &LSetup(1,"%u",533,B);;
var r5    = &LSetup(1,"%u",514,B);;
var r6    = &LSetup(1,"%u",515,B);;
var r7    = &LSetup(1,"%u",516,B);;
var r8    = &LSetup(1,"%u",517,B);;
var r9    = &LSetup(1,"%u",518,B);;	
var kal   = &LSetup(1,"%u",529,B);;
var corr  = &LSetup(1,"%u",519,B);;
if (corr>0) corr-=100;

/* hardware detection, safe defaults */
var deconly=0;
var enconly=0;
var relays=4;
var inputs=4;
/*
 * this function allows to avoid the htype check several times 
 * here and there.
 */
function hw_detection ()
{
	// check dev type
	if (htype==9||htype==14||htype==15||htype==20||htype==34||htype==36||htype==37||htype==38||htype==42||htype==44)
		deconly=1;
	if (htype==8)
		enconly=1;
	
	// check inputs
	if (htype==8||htype==14||htype==15||htype==20||htype==34||htype==36||htype==37||htype==38||htype==43)
		inputs=0;
	else
	if (htype==41||htype==44)
		inputs=1;
	else
	if (htype==7||htype==32||htype==19||htype==42)
		inputs=2;
	else
	if (htype==21||htype==35)
		inputs=4;
	else
	if (htype==17||htype==33)
		inputs=8;
		
	// check relays
	if (htype==8||htype==14||htype==15||htype==36||htype==37||htype==38||htype==41||htype==43||htype==44)
		relays=0;
	else
	if (htype==20||htype==34||htype==7||htype==19||htype==42)
		relays=1;
	else
	if (htype==21||htype==35)
		relays=4
	else
	if (htype==17||htype==33)
		relays=8
		
}

hw_detection();

function afterLoad ()
{
	if (deconly)
	{
		document.getElementById('audioinfo').rows[0].style.display='none';
		document.getElementById('levels').rows[0].style.display='none';
		document.getElementById('levels').rows[1].style.display='none';
	}
	else
	if (enconly)
	{
		document.getElementById('levels').rows[2].style.display='none';
		document.getElementById('levels').rows[3].style.display='none';
	}
	
	/* on load setup for Annuncicom 1000 / VME */
	if (relays==8) {
		document.getElementById('li5').style.display='block';
		document.getElementById('li6').style.display='block';
		document.getElementById('li7').style.display='block';
		document.getElementById('li8').style.display='block';
		document.getElementById('lr5').style.display='block';
		document.getElementById('lr6').style.display='block';
		document.getElementById('lr7').style.display='block';
		document.getElementById('lr8').style.display='block';
		document.getElementById('lr9').style.display='block';
		document.getElementById('rc1').style.display='';
		document.getElementById('rc2').style.display='';
		document.getElementById('rc3').style.display='';
		document.getElementById('rc4').style.display='';
		document.getElementById('rc5').style.display='';
		document.getElementById('rc6').style.display='';
		document.getElementById('rc7').style.display='';
		document.getElementById('rc8').style.display='';
		if (htype==33)
			document.getElementById('rrf').style.display='none';
		else
			document.getElementById('rrf').style.display='';
	}
	else
	{
		if (relays) {
			switch (relays)
			{
			default:
			case 4:
				document.getElementById('lr2').style.display='block';
				document.getElementById('lr3').style.display='block';
				document.getElementById('lr4').style.display='block';
				document.getElementById('rc2').style.display='block';
				document.getElementById('rc3').style.display='block';
				document.getElementById('rc4').style.display='block';
			case 1:
				document.getElementById('lr1').style.display='block';
				document.getElementById('rc1').style.display='block';
			break;
			}
		}	
	}
	
	window.setInterval("get_Status()",1000);
}
	
function get_Status()
{
	var plo,talk,buffl,confl,levl,levr,levlo,levro,ldbl,ldbr,ldblo,ldbro,IR,IL,RL,rhost,rport;

	loadDoc('/stl_data.html');

	var array=c.split(":");
	if (array.length<19) return; /* incomplete string */

	smode=array[0]; // *
	plo  =array[1];
	talk =array[2]; // *
	buffl=array[3]; // *
	confl=array[4]; // *
	levl =array[5];
	levr =array[6];
	levlo=array[7];
	levro=array[8];
	ldbl =array[9];
	ldbr =array[10];
	ldblo=array[11];
	ldbro=array[12];
	IR   =array[13];
	IL   =array[14];
	RL   =array[15];
	rhost=array[16];
	rport=array[17];
	
	switch(wmode){
	case 0: sm.innerHTML="Studio Encoder"; break;
	case 1: sm.innerHTML="Transmitter Decoder"; break;
	case 2: sm.innerHTML="Studio Encoder / Decoder"; break;
	case 3: sm.innerHTML="Transmitter Encoder / Decoder"; break;
	}
	switch(smode){
	case '0': stm.innerHTML="Send always"; break;
	case '1': stm.innerHTML="Send on input1"; break;
	case '2': stm.innerHTML="Send on input2"; break;
	case '3': stm.innerHTML="Send on level"; break;
	case '4': stm.innerHTML="Respond"; break;
	case '5': stm.innerHTML="Never"; break;
	}
	switch(kal){
	case   0: ka.innerHTML="Passive"; break;
	case 255: ka.innerHTML="Disabled"; break;
	default : ka.innerHTML="Active, " + (kal*10)+" msec"; break;
	}
	switch(confl){
	case '0': {cs.innerHTML="No keep-alive info"; cs.style.backgroundColor = "gray";} break;
	case '1': {cs.innerHTML="Established TO "  +rhost+":"+rport; cs.style.backgroundColor = "lime";} break;
	case '2': {cs.innerHTML="Established FROM "+rhost+":"+rport; cs.style.backgroundColor = "lime";} break;
	case '3': {cs.innerHTML="Only Stream FROM "+rhost+":"+rport; cs.style.backgroundColor = "yellow";} break;
	}
	if(buffl == 0){is.innerHTML = "OFF"; is.style.backgroundColor = "gray";}
	if(buffl == 1){is.innerHTML = "ON";  is.style.backgroundColor = "lime";}
	if(talk  == 0){os.innerHTML = "OFF"; os.style.backgroundColor = "gray";}
	if(talk  == 1){os.innerHTML = "ON";  os.style.backgroundColor = "lime";}
	switch(inp){
	case 0:
	case 1: ai.innerHTML="Line Stereo"; break;
	case 129: ai.innerHTML="Line Mono"; break;
	case 2: ai.innerHTML="MIC"; break;
	case 4: ai.innerHTML="SPDIF optical"; break;
	case 5: ai.innerHTML="SPDIF coaxial"; break;	
	case 7: ai.innerHTML="Analog Line, external ADC"; break;
	case 9: ai.innerHTML="AES-EBU"; break;
	}
	s = "";
	if(plo == 127) {
		switch(aft){
			case 0 : s="G.711 &micro;-law mono"; break;
			case 1 : s="G.711 A-law mono"; break;
			case 2 : s="PCM 16bit mono big endian"; break;
			case 3 : s="PCM 16bit mono little endian"; break;
			case 4 : s="PCM 16bit stereo big endian"; break;
			case 5 : s="PCM 16bit stereo little endian"; break;
			case 6 : s="MPEG mono"; break;
			case 7 : s="MPEG stereo"; break;
		}
		switch(srt){
			case 0 : s+=" 8 kHz"; break;
			case 1 : s+=" 12 kHz"; break;
			case 2 : s+=" 16 kHz"; break;
			case 3 : s+=" 22.05 kHz"; break;
			case 4 : s+=" 24 kHz"; break;
			case 5 : s+=" 32 kHz"; break;
			case 6 : s+=" 44.1 kHz"; break;
			case 7 : s+=" 48 kHz"; break;
		}
		if (aft>5) s+= (" ql "+qsr);
	}
	else
	{
		if(plo== -1) s = "USB fallback";
		if(plo==0  ) s = "[pl 0] &micro;-law mono, 8000 Hz";
		if(plo==8  ) s = "[pl 8] A-law mono, 8000 Hz";
		if(plo==10 ) s = "[pl 10] PCM MSB stereo, 44100 Hz";
		if(plo==11 ) s = "[pl 11] PCM MSB mono, 44100 Hz";
		if(plo==14 ) s = "[pl 14] MPEG audio";
		if(plo==96 ) s = "[pl 96] PCM MSB mono, 8000 Hz";
		if(plo==97 ) s = "[pl 97] &micro;-law mono, 24000 Hz";
		if(plo==98 ) s = "[pl 98] A-law mono, 24000 Hz";
		if(plo==99 ) s = "[pl 99] PCM MSB mono, 24000 Hz";
		if(plo==100) s = "[pl 100] &micro;-law mono, 32000 Hz";
		if(plo==101) s = "[pl 101] A-law mono, 32000 Hz";
		if(plo==102) s = "[pl 102] PCM MSB mono, 32000 Hz";
		if(plo==103) s = "[pl 103] PCM MSB stereo, 48000 Hz";
		if(plo==104) s = "[pl 104] PCM LSB mono, 8000 Hz";
		if(plo==105) s = "[pl 105] PCM LSB mono, 24000 Hz";
		if(plo==106) s = "[pl 106] PCM LSB mono, 32000 Hz";
		if(plo==107) s = "[pl 107] PCM LSB stereo, 44100 Hz";
		if(plo==108) s = "[pl 108] PCM LSB stereo, 48000 Hz";
		if(plo==109) s = "[pl 109] &micro;-law mono, 12000 Hz";
		if(plo==110) s = "[pl 110] A-law mono, 12000 Hz";
		if(plo==111) s = "[pl 111] PCM MSB mono, 12000 Hz";
		if(plo==112) s = "[pl 112] PCM LSB mono, 12000 Hz";
	}
	af.innerHTML = s;

	ldblo=parseInt(ldblo)+parseInt(corr);
	ldbro=parseInt(ldbro)+parseInt(corr);

	if(ldbl  < -99) ldbl  = -99;
	if(ldbr  < -99) ldbr  = -99;
	if(ldblo < -99) ldblo = -99;
	if(ldbro < -99) ldbro = -99;

	il.innerHTML = ldbl +" dBFS"; get_db_bar(ldbl, 2);
	ir.innerHTML = ldbr +" dBFS"; get_db_bar(ldbr, 3);
	ol.innerHTML = ldblo+" dBFS"; get_db_bar(ldblo, 0);
	or.innerHTML = ldbro+" dBFS"; get_db_bar(ldbro, 1);

	if (relays) {
		LR1.innerHTML = get_Relay_txt(r1);
		if (relays>1) {
			LR2.innerHTML = get_Relay_txt(r2);
			LR3.innerHTML = get_Relay_txt(r3);
			LR4.innerHTML = get_Relay_txt(r4);
			if (relays==8) {
				LR5.innerHTML = get_Relay_txt(r5);
				LR6.innerHTML = get_Relay_txt(r6);
				LR7.innerHTML = get_Relay_txt(r7);
				LR8.innerHTML = get_Relay_txt(r8);
				if (htype==17) // 9
					LRF.innerHTML = get_Relay_txt(r9);
			}
		}
	}

	if(IR.substring(0,1) == "1") ri1.style.backgroundColor = "lime"; else ri1.style.backgroundColor = "gray";
	if(IR.substring(1,2) == "1") ri2.style.backgroundColor = "lime"; else ri2.style.backgroundColor = "gray";
	if(IR.substring(2,3) == "1") ri3.style.backgroundColor = "lime"; else ri3.style.backgroundColor = "gray";
	if(IR.substring(3,4) == "1") ri4.style.backgroundColor = "lime"; else ri4.style.backgroundColor = "gray";
	if(IR.substring(4,5) == "1") ri5.style.backgroundColor = "lime"; else ri5.style.backgroundColor = "gray";
	if(IR.substring(5,6) == "1") ri6.style.backgroundColor = "lime"; else ri6.style.backgroundColor = "gray";
	if(IR.substring(6,7) == "1") ri7.style.backgroundColor = "lime"; else ri7.style.backgroundColor = "gray";
	if(IR.substring(7,8) == "1") ri8.style.backgroundColor = "lime"; else ri8.style.backgroundColor = "gray";
	
	if(inputs) {
		switch (inputs)
		{
			case 8:
			if(IL.substring(4,5)=="1") li5.style.backgroundColor="lime"; else li5.style.backgroundColor="gray";
			if(IL.substring(5,6)=="1") li6.style.backgroundColor="lime"; else li6.style.backgroundColor="gray";
			if(IL.substring(6,7)=="1") li7.style.backgroundColor="lime"; else li7.style.backgroundColor="gray";
			if(IL.substring(7,8)=="1") li8.style.backgroundColor="lime"; else li8.style.backgroundColor="gray";
			default:
			case 4:
			if(IL.substring(2,3)=="1") li3.style.backgroundColor="lime"; else li3.style.backgroundColor="gray";
			if(IL.substring(3,4)=="1") li4.style.backgroundColor="lime"; else li4.style.backgroundColor="gray";
			case 2:
			if(IL.substring(1,2)=="1") li2.style.backgroundColor="lime"; else li2.style.backgroundColor="gray";
			case 1:
			if(IL.substring(0,1)=="1") li1.style.backgroundColor="lime"; else li1.style.backgroundColor="gray";
			break;
		}
	}
	if(relays) {
		switch (relays) {
			case 8:
			if(RL.substring(4,5) == "1") lr5.style.backgroundColor = "lime"; else lr5.style.backgroundColor = "gray";
			if(RL.substring(5,6) == "1") lr6.style.backgroundColor = "lime"; else lr6.style.backgroundColor = "gray"; 
			if(RL.substring(6,7) == "1") lr7.style.backgroundColor = "lime"; else lr7.style.backgroundColor = "gray";
			if(RL.substring(7,8) == "1") lr8.style.backgroundColor = "lime"; else lr8.style.backgroundColor = "gray";
			if (htype==17) {
				if(RL.substring(8,9) == "1") lr9.style.backgroundColor = "lime"; else lr9.style.backgroundColor = "gray";
			}
			default:
			case 4:
			if(RL.substring(1,2) == "1") lr2.style.backgroundColor = "lime"; else lr2.style.backgroundColor = "gray";
			if(RL.substring(2,3) == "1") lr3.style.backgroundColor = "lime"; else lr3.style.backgroundColor = "gray";
			if(RL.substring(3,4) == "1") lr4.style.backgroundColor = "lime"; else lr4.style.backgroundColor = "gray";
			case 1:
			if(RL.substring(0,1) == "1") lr1.style.backgroundColor = "lime"; else lr1.style.backgroundColor = "gray";
			break;
		}
	}
}

function get_Relay_txt(r)
{
	if(r == 0) return "ON while remote Input ON";
	if(r == 1) return "ON while connection ON";
	if(r == 5) return "ON while connection OFF";
	if(r == 8) return "ON while IN audio ON";
	if(r == 6) return "ON while IN audio OFF";
	if(r == 7) return "PULSE ON with IN audio change";
	if(r == 3) return "ON while incoming stream ON";
	if(r == 4) return "ON while incoming stream OFF";
	if(r == 2) return "always OFF";
	if(r == 9) return "ON while OUT audio ON";
	if(r == 10) return "ON while OUT audio OFF";
	return "wrong setting!";
}
function get_db_bar(v, right)
{
	switch(right) {
	case 0:
		o1=document.getElementById('lolb1');
		o2=document.getElementById('lolb2');
		o3=document.getElementById('lolb3');
		o4=document.getElementById('lolb4');
		o5=document.getElementById('lolb5');
		o6=document.getElementById('lolb6');
		o7=document.getElementById('lolb7');
		o8=document.getElementById('lolb8');
		o9=document.getElementById('lolb9');
		o10=document.getElementById('lolb10');
		break;
	case 1:
		o1=document.getElementById('rolb1');
		o2=document.getElementById('rolb2');
		o3=document.getElementById('rolb3');
		o4=document.getElementById('rolb4');
		o5=document.getElementById('rolb5');
		o6=document.getElementById('rolb6');
		o7=document.getElementById('rolb7');
		o8=document.getElementById('rolb8');
		o9=document.getElementById('rolb9');
		o10=document.getElementById('rolb10');
		break;
	case 2:
		o1=document.getElementById('lilb1');
		o2=document.getElementById('lilb2');
		o3=document.getElementById('lilb3');
		o4=document.getElementById('lilb4');
		o5=document.getElementById('lilb5');
		o6=document.getElementById('lilb6');
		o7=document.getElementById('lilb7');
		o8=document.getElementById('lilb8');
		o9=document.getElementById('lilb9');
		o10=document.getElementById('lilb10');
		break;
	case 3:
		o1=document.getElementById('rilb1');
		o2=document.getElementById('rilb2');
		o3=document.getElementById('rilb3');
		o4=document.getElementById('rilb4');
		o5=document.getElementById('rilb5');
		o6=document.getElementById('rilb6');
		o7=document.getElementById('rilb7');
		o8=document.getElementById('rilb8');
		o9=document.getElementById('rilb9');
		o10=document.getElementById('rilb10');
		break;
	}
	if(v > -58) o1.style.backgroundColor = "lime"; else o1.style.backgroundColor = "#e0e0e0";
	if(v > -46) o2.style.backgroundColor = "lime"; else o2.style.backgroundColor = "#e0e0e0";
	if(v > -34) o3.style.backgroundColor = "lime"; else o3.style.backgroundColor = "#e0e0e0";
	if(v > -28) o4.style.backgroundColor = "lime"; else o4.style.backgroundColor = "#e0e0e0";
	if(v > -22) o5.style.backgroundColor = "lime"; else o5.style.backgroundColor = "#e0e0e0";
	if(v > -16) o6.style.backgroundColor = "lime"; else o6.style.backgroundColor = "#e0e0e0";
	if(v > -13) o7.style.backgroundColor = "gold"; else o7.style.backgroundColor = "#e0e0e0";
	if(v > -10) o8.style.backgroundColor = "gold"; else o8.style.backgroundColor = "#e0e0e0";
	if(v > -7)  o9.style.backgroundColor = "gold"; else o9.style.backgroundColor = "#e0e0e0";
	if(v > -4) o10.style.backgroundColor = "red"; else o10.style.backgroundColor = "#e0e0e0";
}
//+ Jonas Raoni Soares Silva //@ http://jsfromhell.com [v1.0]
String.prototype.repeat = function(l){return new Array(l+1).join(this);};
