function UI(){
	//nothing here
}

UI.prototype.lastStatus="";

UI.prototype.getElement=function(id){
	//cross browser wrapper to return an element from the document
	var elm;
	if (document.getElementById)
	  {
	    // browser implements part of W3C DOM HTML - Gecko, Internet Explorer 5+, Opera 5+
	    elm = document.getElementById(id);
	  }
	  else if (document.all)
	  {
	    // Internet Explorer 4 or Opera with IE user agent
	    elm = document.all[id];
	  }
	  else if (document.layers)
	  {
	    // Navigator 4
	    elm = document.layers[id];
  }
	return elm;
}

UI.prototype.getComboValue=function(id){
	//gets a value from a combo box
	var elm = this.getElement(id);
	if (!elm) return '';
	return elm.options[elm.options.selectedIndex].value;
}

UI.prototype.hideRow=function (id){
	var elmtr=this.getElement(id);
	if(elmtr){
		if (elmtr.style){
			elmtr.style.display='none';
		}else if(elmtr.visibility){
			elmtr.visibility = "hide";
		}
	}
}

UI.prototype.showRow=function(id){
	var elmtr=this.getElement(id);
	if (elmtr){
		if (elmtr.style){
			elmtr.style.display='';
		}else if(elmtr.visibility){
				elmtr.visibility = "show";
		}
	}
}

UI.prototype.update=function(elmName){
	if (elmName){
		//write the updated value to the cookie
		this.updateSetting(elmName);
	} else {
 		//do full update (used only once at load time)
		this.doUpdate("fromformat");
	 	this.doUpdate("toformat");
 		this.doUpdate("saveOnExit");
 		this.doUpdate("sortBy");
 		this.doUpdate("listCleaning");
 		this.doUpdate("expiry");
 		this.doUpdate("prefix");
 		this.doUpdate("bpcat");
 		this.doUpdate("denyonly");
 		this.doUpdate("singleAsRanges");
 		this.doUpdate("biIniSection");
 		this.doUpdate("logging");
 		this.doUpdate("ciscoaclnum");
 		this.doUpdate("ciscodirection");
		this.doUpdate("debugLevel");
	}
 	this.updateDisplay();
}

UI.prototype.doUpdate=function(whatid){
	//if a cookie value exists, update the ui
	//else update the cookie val fro the ui
	if (!this.updateElement(whatid)) this.updateSetting(whatid);
}

UI.prototype.updateDisplay=function(){
	//now start hiding stuff based on toFormat
	var fmt=this.getComboValue("toformat");
	//here we get the toformat value

	if ('raza blackice'.indexOf(fmt)!=-1) this.showRow("trExpiry");
		else this.hideRow("trExpiry");
	if ("bearshare shorewall plainrange sygate plainrangecs iptables ciscoacl ciscoios".indexOf(fmt)==-1) this.showRow("trPrefix");
		else this.hideRow("trPrefix");
	if (fmt=='blockpost') this.showRow("trBPCat");
		else this.hideRow("trBPCat");
	if ("blackice ciscoios donk kpf kp4 raza zonealarm".indexOf(fmt)==-1) this.showRow("trDenyOnly");
		else this.hideRow("trDenyOnly");
	if ("pg plainrange plainrangecs".indexOf(fmt)!=-1) this.showRow("trSingleAsRanges");
		else this.hideRow("trSingleAsRanges");
	if (fmt=='blackice') this.showRow("trBIIniSect");
			else this.hideRow("trBIIniSect");
	if ("kpf kp4 smoothie".indexOf(fmt)!=-1) this.showRow("trLogging");
				else this.hideRow("trLogging");
	if ("zonealarm".indexOf(fmt)==-1) this.showRow("trSort");
				else this.hideRow("trSort");
	if ('ciscoacl'.indexOf(fmt)!=-1){
		this.showRow("trCiscoACLNum");
		this.showRow("trCiscoDirection");
	}else{
		this.hideRow("trCiscoACLNum");
		this.hideRow("trCiscoDirection");
	}
}

UI.prototype.updateElement=function(elmName){
	//updates the ui from a cookie. used at load time only.
	if(!settings[elmName]) return false; //nothing to update
	var elm=this.getElement(elmName);
	switch (elm.tagName){
		case "INPUT":{
			elm.value=settings[elmName];
			break;
		}
		case "SELECT":{
			elm.options.selectedIndex=settings[elmName];
			break;
		}
		default:{
			break;
		}
	}
}

UI.prototype.updateSetting=function(elmid){
	//updates the cookie
	//used at loadtime and by doc elements only
	var elm=this.getElement(elmid);
	if(elm==null) return;
	//alert(elmid+" : "+elm);
	switch (elm.tagName){
	case "INPUT":{
		settings[elmid]=elm.value;
		break;
	}
	case "SELECT":{
		settings[elmid]=elm.options.selectedIndex;
		break;
	}
	default:{
		break;
	}
	}
}

UI.prototype.writeToItem=function(text,id){
	var x,text2;
	if (document.getElementById)
	{
		x = document.getElementById(id);
		x.innerHTML = '';
		x.innerHTML = text;
	}
	else if (document.all)
	{
		//this is broken for ie5.01, why??
		//is it document.all.innerHtml ??
		document.all.id.innerHTML = text;
	}
	else if (document.layers)
	{
		x = document.layers[id];
		text2 = '<P CLASS="testclass">' + text + '</P>';
		x.document.open();
		x.document.write(text2);
		x.document.close();
	}
}

UI.prototype.lastStatusTime=new Date();

UI.prototype.updateStatus=function(what){
	//if the status is blank, write it anyway
	if (this.lastStatus=='') this.setFinalStatus(what);
	//record the status for debugging
	this.lastStatus=what;
	//debug log
	if (settings.debugLevel>0) {
		var sdbg=this.getElement('sdbg');
		if (sdbg) sdbg.value+=what+'\n';
	}
	//now check that the last status update was <2s ago (speed up for m$ie, no gratuitous updates)
	timeNow = new Date()
	var diff = timeNow.getTime() - this.lastStatusTime.getTime()
	if (diff<2000) return false; //every 2000 milliseconds we update else get out
	this.lastStatusTime=timeNow;
	this.writeToItem(what,"sStatus");
	window.status=what;
}

UI.prototype.setFinalStatus=function(what){
	if (settings.debugLevel>0) {
		var sdbg=this.getElement('sdbg');
		if (sdbg) sdbg.value+=what+'\n';
	}
	//just write it
	this.lastStatus=what;
	this.writeToItem(what,"sStatus");
	window.status=what;
}

UI.prototype.debug=function(what,eol){
	if(!settings.debugLevel) return; //if the debugLevel variable doesn't exist then get out
	if(settings.debugLevel==2){
		if(eol==null) eol=true; //by default always append a linefeed (set eol to false to prevent linefeed)
		var sdbg=this.getElement('sdbg');
		if (sdbg) sdbg.value+=what+(eol?'\n':'');
	}
}

//==============================
//  UI Helper functions
//==============================

//IE specific "copy to clipboard" functionality
function clipcopy(){
	if(tabPane) tabPane.setSelectedIndex(2);
    bResult = window.clipboardData.setData("Text",ui.getElement("sto").value);
    ui.updateStatus("Output data copied to clipboard.");
}

//IE specific copy button
function renderCopyButton(){
	if(window.clipboardData) document.write('<input type=button value="Copy Output" onClick="clipcopy();">&nbsp;&nbsp;');
}

//render functions for output list and format specific options
function renderSourceList(){
	document.write('<tr><td style="width:200px;">Source Format</td><td>');
	document.write('<select id=fromformat style="width:200px;" onchange="ui.update(this.id);">');
	//document.write('<option value="htaccess">Apache .htaccess');
	document.write('<option value="arin">ARIN raw whois results');
	document.write('<option value="bearshare">BearShare hostiles.txt');
	document.write('<option value="blackice">BlackICE Defender firewall.ini');
	document.write('<option value="blockpost">BlockPost plugin for OutPost v1');
	document.write('<option value="blockp2">BlockPost plugin for OutPost v2');
	document.write('<option value="bbl">Bob\'s Block List (BBL) Format');
	document.write('<option value="ciscoios">Cisco IOS Firewall ACL');
	document.write('<option value="dshield">DShield.org block list');
	document.write('<option value="donk" selected>eDonkey (eMule,cDonkey)');
	document.write('<option value="flowpoint">Flowpoint Routers');
	document.write('<option value="gnu">Gnucleus GnuBlocked.net');
	document.write('<option value="iptocountry">ip-to-country.csv');
	document.write('<option value="morph">Morpheus blacklist');
	document.write('<option value="pg">PeerGuardian plain text');
	document.write('<option value="plainrange">Plain IP ranges');
	document.write('<option value="pw">Protowall plain text');
	document.write('<option value="raza">Shareaza xml security digest');
	document.write('<option value="shorewall">Shoreline Firewall');
	document.write('<option value="sygate">Sygate Advanced Rules');
	document.write('<option value="trusty">TrustyFiles');
	document.write('<option value="zonealarm">ZoneAlarm v4 xml');
	document.write('</select></td><td></td></tr>');
}

function renderOutputList(){
	document.write('<tr><td>Output Format</td><td>');
	document.write('<select name=toformat id=toformat style="width:200px;" onchange="ui.update(this.id);">');
	//document.write('<option value="htaccess">Apache .htaccess');
	document.write('<option value="ares">Ares Galaxy');
	//document.write('<option value="azureus">Azureus Bittorrent Client');
	document.write('<option value="bearshare">BearShare hostiles.txt');
	document.write('<option value="blackice">BlackICE Defender firewall.ini');
	document.write('<option value="blockpost">BlockPost plugin for OutPost v1');
	document.write('<option value="blockp2">BlockPost plugin for OutPost v2');
	document.write('<option value="bbl">Bob\'s Block List (BBL) Format');
	document.write('<option value="shorewall">CIDR Notation');
	document.write('<option value="cidrcmts">CIDR Notation with #comments');
	document.write('<option value="ciscoacl">Cisco Access Control List (ACL)');
	document.write('<option value="ciscoios">Cisco IOS Firewall Deny List');
	document.write('<option value="donk">eDonkey (eMule,cDonkey)');
	document.write('<option value="flowpoint">Flowpoint Routers');
	document.write('<option value="gnu">Gnucleus GnuBlocked.net');
	document.write('<option value="hosts">HOSTS file');
	document.write('<option value="morph">Morpheus blacklist');
	document.write('<option value="ipchains">ipchains');
	document.write('<option value="ipfw">ipfw for *BSD/OSX');
	document.write('<option value="iptables">iptables bash script');
	document.write('<option value="kplusplus">Kazaa K++');
	document.write('<option value="kpf">Kerio Personal Firewall v2');
	document.write('<option value="kp4">Kerio Personal Firewall v4');
	document.write('<option value="kwr5">Kerio WinRoute Firewall v5.1.x');
	document.write('<option value="pg">PeerGuardian plain text');
	//document.write('<option value="pgipdbsql">PeerGuardian IPDB SQL Dump');
	document.write('<option value="phex">Phex/MacPhex');
	document.write('<option value="plainrange">Plain IP ranges');
	document.write('<option value="plainrangecs">Plain IP ranges - Comma Seperated');
	document.write('<option value="pw">Protowall plain text');
	document.write('<option value="raza" selected>Shareaza xml security digest');
	document.write('<option value="shorewall">Shoreline Firewall');
	document.write('<option value="smoothie">Smoothwall Express 2.0');
	document.write('<option value="snort">Snort IDS');
	document.write('<option value="sygate">Sygate Advanced Rules');
	document.write('<option value="trusty">TrustyFiles');
	document.write('<option value="zonealarm">ZoneAlarm v4 xml');
	document.write('</select></td><td></td></tr>');
}

function renderOtherOptions(){
	document.write('<tr><td>Save settings on exit?</td><td><select id=saveOnExit style="width:200px;" onchange="ui.updateSetting(this.id);"><option value="yes">Yes<option value="no" selected>No</select></td><td></td></tr>');
	document.write('<tr id=trSort><td>Output Sorting</td><td><select id=sortBy style="width:200px;" onchange="ui.updateSetting(this.id);"><option value="none" selected>No Sorting<option value="IP">By IP Address<option value="Comment">By Comment<option value="RuleType">By Rule Type (Permit/Deny)</select></td><td></td></tr>');
	document.write('<tr><td>List Cleaning</td><td><select id=listCleaning style="width:200px;" onchange="ui.updateSetting(this.id);"><option value="none" selected>None<option value="exactonly">Remove exact duplicates only<option value="mergeoverlaps">Merge overlapping ranges<option value="mergeadjacent">Merge adjacent ranges</select></td><td></td></tr>');
	document.write('<tr id=trDenyOnly><td>Include only deny filters in output?</td><td><select id=denyonly style="width:200px;" onchange="ui.updateSetting(this.id);"><option value="yes" selected>Yes<option value="no">No</select></td><td></td></tr>');
	document.write('<tr id=trExpiry><td>Rule Expiry </td><td><select name=expiry id=expiry style="width:200px;" onchange="ui.updateSetting(this.id);"><option value="0" selected>Never<option value="10">After 10 days<option value="20">After 20 days<option value="30">After 30 days<option value="60">After 60 days</select></td><td></td></tr>');
	document.write('<tr id=trPrefix><td>Comment Prefix</td><td><input type=text name=prefix id=prefix value="" style="width:200px;" onchange="ui.updateSetting(this.id);"></td></tr>');
	document.write('<tr id=trBPCat><td>Category for Blockpost v1:</td><td><input type=text name=bpcat id=bpcat value="P2P Dangerous IPs" style="width:200px;" onchange="ui.updateSetting(this.id);"></td></tr>');
	document.write('<tr id=trSingleAsRanges><td>Display single IPs as IP ranges?</td><td><select id=singleAsRanges style="width:200px;" onchange="ui.updateSetting(this.id);"><option value="yes" selected>Yes<option value="no">No</select></td><td></td></tr>');
	document.write('<tr id=trBIIniSect><td>BlackICE - firewall.ini section</td><td><select name=biIniSection id=biIniSection style="width:200px;" onchange="ui.updateSetting(this.id);"><option value="ipaccept" selected>[MANUAL IP ACCEPT]<option value="low">TCP/UDP ports below 1024<option value="high">TCP/UDP ports 1024 and above</select></td><td><a href="/instructions.html#blackice"><img src="/img/icon_help.gif" border=0 alt="Further information on converting to BlackICE firewall.ini format"></a></td></tr>');
	document.write('<tr id=trLogging><td>Enable Logging?</td><td><select id=logging style="width:200px;" onchange="ui.updateSetting(this.id);"><option value="yes" selected>Yes<option value="no">No</select></td><td><!--<a href="/instructions.html#kpf"><img src="/img/icon_help.gif" border=0 alt="Further information on the Kerio Personal Firewall persfw.conf format"></a>--></td></tr>');
	document.write('<tr id=trCiscoACLNum><td>Cisco access list number</td><td><input type=text name=ciscoaclnum id=ciscoaclnum value="101" style="width:200px;" onchange="ui.updateSetting(this.id);"></td></tr>');
	document.write('<tr id=trCiscoDirection><td>Cisco access list direction</td><td><select id=ciscodirection style="width:200px;" onchange="ui.updateSetting(this.id);"><option value="in" selected>In<option value="out">Out</select></td></tr>');
}

