var cl_head = "";

function cl_getXMLHttp() {
	var xmlhttp, alerted;
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
// JScript gives us Conditional compilation, we can cope with old IE versions.
	try {
		xmlhttp=new ActiveXObject("Msxml2.XMLHTTP")
	} catch (e) {
		try {
    		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
    	} catch (E) {
   			alert("You must have Microsofts XML parsers available")
  		}
	}
@else
	alert("You must have JScript version 5 or above.")
	xmlhttp=false
	alerted=true
@end @*/
	if (!xmlhttp && !alerted) {
	 	// Non ECMAScript Ed. 3 will error here (IE<5 ok), nothing I can
	 	// realistically do about it, blame the w3c or ECMA for not
	  	// having a working versioning capability in  <SCRIPT> or
	 	// ECMAScript.
    	try {
    	    xmlhttp = new XMLHttpRequest();
    	}
    	catch (e) {
        	alert("You need a browser which supports an XMLHttpRequest Object.\nMozilla build 0.9.5 has this Object and IE5 and above.");
    	}
	}
	return xmlhttp;
}

function leverArea(name, orientation, autoRefresh, showGoButton, buttonLabel) {
// methods
	this.add = cl_add;
	this.drawLevers = cl_drawLevers;
	this.go = cl_go;

// members
	this.name = name;
	this.orientation = orientation;
	this.autoRefresh = autoRefresh;
	this.showGoButton = showGoButton;
	this.buttonLabel = buttonLabel;
	this.leverList = new Array();
}

function cl_add(lever) {
	this.leverList[this.leverList.length] = lever;
}

function cl_drawLevers() {
 
        var request = cl_getXMLHttp();
	    var leverArea = this;
        var levername = this.name;
 
        request.open("GET", "/marvelit/leversGen.jsp?area=" + this.name + "&orientation=" + this.orientation + "&fromcl=1&ids=" + this.leverList.join(), true);
 
        request.onreadystatechange = function() {
			if (request.readyState == 4 && request.status == 200) {
				if (request.responseText){
					document.getElementById(levername).innerHTML = cl_head + request.responseText + (leverArea.showGoButton ? "<input type=button name=go_button value=\"" + leverArea.buttonLabel + "\" onclick=\"submit()\">" : ""); 
				}
			}
        };
        request.send(null);
}

function cl_go(lever, val) {
    var request = cl_getXMLHttp();
    var leverArea = this;

    request.open("GET", "/marvelit/levers.jsp?" + escape(lever) + "=" + escape(val), true);
    request.onreadystatechange = function() { 
			if (request.readyState == 4 && request.status == 200) {
				if (leverArea.autoRefresh) {
					window.location.reload();
				} else {
					leverArea.drawLevers();
				}
			}
        };
    request.send(null);
}