/* &L(0,"*",1); */
var xmlhttp=false;
var rx;
var array;
var mode=0;
var statold=0;
var refCfg, refChk;
var htype=&LState(1,"%u",28);;
var plist,fconf;

//input gain array.
var gainar=new Array(-3,-1.5,0,1.5,3,4.5,6,7.5,9,10.5,12,13.5,15,16.5,18,19.5);

/*@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 @*/

//these function are taken from applications/abclapp/newstuff
//and must  use those function when using new web ui.

/* hide HTML element */
function hide(elm){
    var x = document.getElementById(elm);
    if(x) x.style.display = 'none';
}

/* show HTML element */
function show(elm){
    var x = document.getElementById(elm);
    if (x) x.style.display = "";
}

/* set innerHTML of a HTML element */
function set_txt(elm,val){
    var x= document.getElementById(elm);
    if(x) x.innerHTML = val;
}

function cal_time(ts)
{
	if (ts.length<=2) return "not available";
	else 
		return ts.substr(6,2)+':'+ts.substr(8,2)+'h, ' + ts.substr(4,2)+'/'+ts.substr(2,2)+'/20' +ts.substr(0,2) + ' GMT';
}

function updateConfig()
{
	if (array[1]=="[NO_VAR]"||array[1]=="") {
		//console.log("received empty cfg, initially setting to 1000");
		window.clearInterval(refCfg);
		refCfg=window.setInterval ("loadDoc('reflect_data_c.html')",1000);
		set_txt('mo',"loading ..");
		hide('butr');
		hide('vltr');
		hide('ratr');
		hide('intr');
		hide('igtr');
		hide('rstr');
		hide('iptr');
		hide('cfgtr');
	} else {
		var text, error=0;
		//console.log("received cfg, resetting to 25000");
		window.clearInterval(refCfg);
		refCfg=window.setInterval ("loadDoc('reflect_data_c.html')",25000);
		mode=(array[1]=="encoder")?1:2;
		show('iptr');
		show('cfgtr');
		if(array[1]=="encoder"){
			show('intr');
			show('igtr');
			show('rstr');
			hide('butr');
			hide('vltr');
			hide('ratr');
		} else {
			hide('intr');
			hide('igtr');
			hide('rstr');
			show('butr');
			show('vltr');
			show('ratr');
		}
		// error flag
		if (mode==1) { if(htype==1||htype==9||htype==14||htype==15||htype==20) error=1; }
		if (mode==2) { if(htype==8) error=1; }
		var color; /* see BUG 44 WEBUI, error must be red */
		if (error) { text="ERROR, incompatible device"; color="red";}
		else  { text=array[1]; color="#6D929B"; }
		document.getElementById('mo').innerHTML=text;
		document.getElementById('mo').style.color=color;
		text=(mode==2)?text=array[2]+" dB":text="---";
		document.getElementById('vl').innerHTML=text;
		/* Quality fields is not required.
		if (mode==1) {
			switch (array[8]) {
				case '40':  text="MPEG 22.05 kHz mono quality 2"; break;
				case '64':  text="MPEG 44.1 kHz mono quality 0"; break;
				case '80':  text="MPEG 44.1 kHz stereo quality 3"; break;
				case '140': text="MPEG 44.1 kHz stereo quality 7"; break;
				case '192': text="MPEG 48 kHz stereo quality 7"; break;
			}
		} else text="---";
		document.getElementById('ql').innerHTML=text;
		*/
		text=(mode==1)?array[3]:"---";
		document.getElementById('in').innerHTML=text;
		text=(mode==1)?gainar[array[4]]+" dB":"---";
		document.getElementById('ig').innerHTML=text;
		text=(mode==2)?array[5]+" ms":"---";
		document.getElementById('bu').innerHTML=text;
		document.getElementById('ip').innerHTML=array[6]+":"+array[7];
		if (mode==2) {
			text=(array[9]=="1")?"signal stream loss":"mirror encoder input 1";
		} else text="---";
		document.getElementById('ra').innerHTML=text;
		if (mode==1) {
			text=(array[10]=="1")?"alarm when not streaming":"unused";
		} else text="---";
		document.getElementById('rs').innerHTML=text;
		//set configuration time
		set_txt('cfg',cal_time(array[11]));
	}
}

function ioDisplay(idio,st) {
	var i,c;
	for (x=0,n=1;x<8;++x,++n) {
		i=st.charAt(x);
		switch(i){
			case '0': c="#bbbbbb"; break;
			case '1': c="#00ff00"; break;
			case '-': c="#ffffff"; break;
		}
		document.getElementById(idio+n).style.backgroundColor=c;
	}
}

function updateStatus() {
	if (array[1]=="[NO_VAR]"||array[1]=="") {
		document.getElementById('st').innerHTML="loading ...";
	} else {
		len=array.length;
		 /* can be received a truncated string, updating only the received fields. */
		switch (len) {
			case 11:
				fconf=array[10];
			case 10:
				plist=array[9];
			case 9:
				document.getElementById('al').innerHTML=array[8]+" ms";
			/* PASS THROUGH */
			case 8: document.getElementById('rf').innerHTML=array[7];
			/* PASS THROUGH */
			case 7: document.getElementById('df').innerHTML=array[6];
			/* PASS THROUGH */
			case 6: document.getElementById('lf').innerHTML=array[5];
			/* PASS THROUGH\ */
			case 5: document.getElementById('br').innerHTML=array[4]+" kbits/sec";
			/* PASS THROUGH */
			case 4:
				ioDisplay("i",array[2]);
				ioDisplay("r",array[3]);
			/* PASS THROUGH */
			case 3:
			case 2:
			{
				var status,st,style;
				st=array[1];
				switch(st) {
					default:
					case '0': status="Starting ..."; break;
					case '1': status="Resolving URL's ..."; break;
					case '2': status="Detecting available configurator server"; break;
					case '3': status="Connected to configurator"; break;
					case '4': status="Configuration updated"; break;
					case '5': status="No Configuration server available, retrying .."; break;
					case '6': status="Can't resolve reflector IP, check DNS and Geteway"; break;
					case '7': status="No Incoming Stream"; break;
					case '8': status="Receiving Stream"; break;
					case '9': status="Sending Stream"; break;
					case '10': status="No Reflector service available"; break;
					case '11': status="Stop playing from USB"; break;
					case '12': status="Not sending stream, no Reflector servcie available"; break;
				}
				if (st=='7'||st=='10') {
					if (plist=="1") status+=" / playing from USB"; else status+=" / no USB backup";
				}
				document.getElementById('st').innerHTML=status;
				if (st!=statold) {
					if (st==8) style="block"; else style="none";
					document.getElementById('tr1').style.display=style;
					document.getElementById('tr2').style.display=style;
					document.getElementById('tr3').style.display=style;
					document.getElementById('tr4').style.display=style;
					document.getElementById('tr5').style.display=style;
					statold=st;
				}
			}
		}
		if (fconf=="1") {
			//console.log("fconf=1, setting to 100");
			window.clearInterval(refCfg);
			refCfg=window.setInterval ("loadDoc('reflect_data_c.html')",500);
		}
	}
}

function setup() {
	hide('tr1');
	hide('tr2');
	hide('tr3');
	hide('tr4');
	hide('tr5');
}

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 && xmlhttp.readyState==4) {
				rx=xmlhttp.responseText;
				//console.log(rx);
				array=rx.split(":");
				if (array[0]=="C") updateConfig(); else updateStatus();
			}
		}
		xmlhttp.send();
	}
}

if (!xmlhttp) {
	try {
		xmlhttp=new XMLHttpRequest();
	} catch (e) {
		xmlhttp=false;
	}
}

// leav as last, always ask config on refresh
loadDoc('reflect_data_c.html');
window.setInterval ("loadDoc('reflect_data_s.html')",980);
