var feature=new Object ();

// when mapping total lines to fxo and fxs, fxo first, than fxs.
// Eg: if we have 3 lines, 1 fxo, 2 fxs, then we enable fxo0, fxs1, fxs2
function feature_line_is_enabled(line)
{
	if (line<feature.fxo_total+feature.fxs_total)
		return 1;
	return 0;
}
function feature_fxo_is_enabled(fxo)
{
	if (fxo<feature.fxo_total)
		return 1;
	return 0;
}
function feature_fxs_is_enabled(fxs)
{
	if (feature.fxo_total<=fxs && feature.fxo_total+feature.fxs_total>fxs)
		return 1;
	return 0;
}

// check if this feature is enabled
function feature_is_enabled(e)
{
	var s=e.getAttribute("feature");
	if (typeof s == "string" && s.length > 0) {
		if (s=="threedns_enable" && feature.threedns_enable==0) {
			return 0;
		} else if (s=="digitmaps_enable" && feature.digitmaps_enable==0) {
			return 0;
		} else if (s=="digitmapu_enable" && feature.digitmaps_enable==1) {
			return 0;
		} else if (s=="sip_sameserverset_enable" && feature.sip_sameserverset_enable==0) {
			return 0;
		} else if (s=="sip_perportserverset_enable" && feature.sip_sameserverset_enable==1) {
			return 0;
		} else if (/^line(\d)$/.test(s)) {	// line0..line3
			if (!feature_line_is_enabled(RegExp.$1))
				return 0;
		} else if (/^fxo(\d)$/.test(s)) {	// fxo0..fxo3
			if (!feature_fxo_is_enabled(RegExp.$1))
				return 0;
		} else if (/^fxs(\d)$/.test(s)) {	// fxs0..fxs3
			if (!feature_fxs_is_enabled(RegExp.$1))
				return 0;
		}
	}
	return 1;
}

// check if user is allowed to access an element
function ac_is_allowed(e)
{
	var s=e.getAttribute("ac");
	if (typeof s == "string" && s.length > 0) {
		if (s == "admin" && ac!="admin") {
			return 0;
		}
		if (s == "user" && ac!="admin" && ac!="user") {
			return 0;
		}
	}
	return 1;
}

/* hidden items of disabled or disallowed features */
function feature_init()
{
	var elist, i, n;

	elist = (document.all)? document.all : document.body.getElementsByTagName("*");
	for (i=0; i<elist.length; i++) {
		if (feature_is_enabled(elist[i]) && ac_is_allowed(elist[i])) {
			if (elist[i].className == "data_invisible")
				elist[i].className = "data_visible";
		} else {
			//alert("name="+elist[i].name + ", id="+elist[i].id);
			elist[i].className = "data_invisible";
		}
	}
}
function id_visible(id)
{
	var e=document.getElementById(id);
	if (e == null)
		alert("ERROR: "+id+"  NOT DEFINED?");
	if (feature_is_enabled(e) && ac_is_allowed(e))
		if (e.className == "data_invisible")
			e.className="data_visible";
}
function id_invisible(id)
{
	var e=document.getElementById(id);
	if (e == null)
		alert("ERROR: "+id+"  NOT DEFINED?");
	e.className="data_invisible";
}

