
/*
 * computeSchedulePulldown()
 *	Populate the <select> control 'document.mainform.schedule_names' with the
 *	schedule names, starting from the given option offset. Also populate
 *	any <select> controls 'document.mainform.schedule_names_N' for N=0,1,2...
 */
function computeSchedulePulldown (starting_offset)
{
	if (typeof(starting_offset) == "undefined") {
		starting_offset = 2;
	}

	var scount = starting_offset;
	
	for (var i = 0; i < ARRAYSIZE_SCHED_TABLE; i++) {
		if (data.sched_table[i].used == 1) {
			if (document.mainform.schedule_names) {
				document.mainform.schedule_names.options[scount] = new Option (data.sched_table[i].sched_name, data.sched_table[i].sched_name);
			}
			var elt, j = 0;
			while (elt = document.mainform['schedule_names_' + j]) {
				elt.options[scount] = new Option (data.sched_table[i].sched_name, data.sched_table[i].sched_name);
				j ++;
			}
			scount++;
		}
	}
}

/*
 * computeIngressFilterPulldown()
 *	Populate the <select> control 'document.mainform.ingress_filter_names' with the
 *	ingress_filter names. Also populate
 *	any <select> controls 'document.mainform.ingress_filter_names_N' for N=0,1,2...
 */
function computeIngressFilterPulldown()
{
	var scount = 2;

	for (var i = 0; i < ARRAYSIZE_INGRESS_RULES; i++) {
		if (data.ingress_rules[i].used == 1) {
			if (document.mainform.ingress_filter_names) {
				document.mainform.ingress_filter_names.options[scount] = new Option (data.ingress_rules[i].ingress_filter_name, data.ingress_rules[i].ingress_filter_name);
			}
			var elt, j = 0;
			while (elt = document.mainform['ingress_filter_names_' + j]) {
				elt.options[scount] = new Option (data.ingress_rules[i].ingress_filter_name, data.ingress_rules[i].ingress_filter_name);
				j++;
			}
			scount++;
		}
	}
}

/*
 * createObjectListFromDocumentData()
 *	Generate an array of objects from the given dataInstance and field names.
 *
 * The dataInstance is the document returned using an XML or HTML retrieval object.
 * The fieldList is an array of field names, such as 'route_addr_', 'route_mask_' and 'route_gateway_'.
 * The dataInstance contains records that are formed as <field_name_>X where X is the record number.
 * Data is encoded as, for example, route_addr_1, route_mask_1, route_gateway_1, route_addr_2, route_mask_2....
 */
function createObjectListFromDocumentData(dataInstance, fieldList)
{
	var objectList = new Array();
	if (!dataInstance || !fieldList.length) {
		/*
		 * Empty array returned
		 */
		return objectList;
	}

	/*
	 * Generate the array
	 */
	var entry = 0;
	while (true) {
		var objectEntry = new Object;

		/*
		 * Iterate through the fieldList reading the fields of the current 'entry'.
		 */
		for (var field = 0; field < fieldList.length; ++field) {
			/*
			 * Extract the field content from the current entry of the data document
			 */
			var field_content = dataInstance.getElementData(fieldList[field] + entry);
			if (field_content == null) {
				/*
				 * No more data, if not on a new record then this is an error.
				 */
				if (field == 0) {
					return objectList;
				}

				/*
				 * Consider this an empty string.
				 */
				field_content = "";
			}

			/*
			 * Store the field content into the objectEntry
			 */
			eval("objectEntry." + fieldList[field] + " = '" + field_content + "'");
		}

		/*
		 * Store the objectEntry into the objectList at the entry position
		 */
		objectList[entry] = objectEntry;

		entry++;
	}

	return objectList;
}

/*
 * createRoutingTable()
 *	Generate an array of route objects from the given dataInstance
 */
function createRoutingTable(dataInstance, excludeStatic)
{
	var fields = new Array("rt_address_", "rt_netmask_", "rt_gateway_", "rt_metric_", "rt_is_static_", "rt_link_type_", "rt_flags_");
	var routeList = createObjectListFromDocumentData(dataInstance, fields);

	/*
	 * We now need to convert some of the values in the routeList array.
	 */
	for (var entry = 0; entry < routeList.length; ++entry) {
		routeList[entry].rt_is_static_ *= 1;		/* Integer convert */

		if (excludeStatic) {
			if (routeList[entry].rt_is_static_ != 0) {
				routeList.splice(entry, 1);
				entry--;	/* Re-iterate over this element as all others are shifted down */
				continue;
			}
		}

		routeList[entry].rt_link_type_ *= 1;			/* Integer convert */
		routeList[entry].rt_flags_ *= 1;		/* Integer convert */
		routeList[entry].rt_metric_ *= 1;		/* Integer convert */

		/*
		 * Create a 'creator' string
		 */
		routeList[entry].creator = "System";
		if ((routeList[entry].rt_flags_ & 0x80) != 0) {
			routeList[entry].creator = "RIP";
		} if (routeList[entry].rt_is_static_ != 0) {
			routeList[entry].creator = "User";
		}

		/*
		 * Create a link type enum
		 */
		routeList[entry].link_name = "WAN";
		if (routeList[entry].rt_link_type_ == 2) {
			routeList[entry].link_name = "LAN";
		} else if (routeList[entry].rt_link_type_ == 3) {
			routeList[entry].link_name = "Static WAN";
		}

		/*
		 * Create some numerical equivalents
		 */
		routeList[entry].address = IPAddressToInteger(routeList[entry].rt_address_);
		routeList[entry].netmask = IPAddressToInteger(routeList[entry].rt_netmask_);
		routeList[entry].gateway = IPAddressToInteger(routeList[entry].rt_gateway_);
	}

	return routeList;
}

/*
 * createDNSList()
 *	Generate an array of DNS IP list objects from the given dataInstance.
 *
 * This is faster than messing around with the dataInstance direct.
 */
function createDNSList(dataInstance)
{
	var fields = new Array("dns_");
	var dnsList = createObjectListFromDocumentData(dataInstance, fields);
	return dnsList;
}

/*
 * createPingResponseList()
 *	Generate an array of Ping response objects from the given dataInstance.
 *
 * This is faster than messing around with the dataInstance direct.
 */
function createPingResponseList(dataInstance)
{
	var fields = new Array("reply_ip_", "reply_millis_", "reply_ttl_");
	var respList = createObjectListFromDocumentData(dataInstance, fields);
	return respList;
}

/*
 * createComputerList()
 *	Generate an array of computer list objects from the given dataInstance.
 *
 * This is faster than messing around with the dataInstance direct.
 */
function createComputerList(dataInstance)
{
	var fields = new Array("ip_address", "mac", "host_name", "seconds_remain", "is_reservation", "learned_by");
	var computerList = createObjectListFromDocumentData(dataInstance, fields);

	/*
	 * We now need to convert some of the values in the computerList array.
	 */
	for (var entry = 0; entry < computerList.length; ++entry) {
		computerList[entry].is_reservation * 1;		/* Integer convert */
		computerList[entry].learned_by * 1;			/* Integer convert */
		computerList[entry].seconds_remain * 1;		/* Integer convert */

		/*
		 * Create a 'time_remain' string representation of 'seconds_remain'
		 */
		var str = "";
		var remain = computerList[entry].seconds_remain;
		if (remain == 0xffffffff) {
			str = "Never";
		} else {
			/*
			 * Days
			 */		
			var days = remain / 86400;
			days = Math.floor(days);
			remain %= 86400;
			if (days) {
				str += days + "&nbsp;Days ";
			}

			/*
			 * Hours
			 */
			var hours = remain / 3600;
			hours = Math.floor(hours);
			remain %= 3600;
			if (hours) {
				str += hours + "&nbsp;Hours ";
			}

			/*
			 * Minutes
			 */
			var mins = remain / 60;
			mins = Math.floor(mins);
			if (mins) {
				str += mins + "&nbsp;Minutes ";
			}
		}
		computerList[entry].time_remain = str;
	}

	return computerList;
}

/*
 * computerListPopulatePulldown()
 *	Populates the 'pulldown' list selection object with required computer information from the given computerList.
 *
 * pulldown_arg is either a <select> object, or a string containing the name of a set of such
 * objects in document.mainform. For example if pulldown_arg is 'foo' then the <select> objects
 * 	document.mainform.foo_0
 * 	document.mainform.foo_1
 * 	document.mainform.foo_2
 *	etc...
 * will be populated, up to the highest index that is defined.
 *
 * NOTE: Attempts to preserve the current selection, or reverts to "-1" if the selection has dissapeared.
 * NOTE: The visible text for each entry is one of:
 * "<host name> (<computerListField>)"		-- if there is a host name associated with the computer entry
 * or
 * "<computerListField>"						-- if there is no host name
 *
 * This function associates the following attributes with each item in the pulldown:
 * mac - mac address
 * host_name - text name of the host, if available, or ""
 * ip_address - the IP address of the machine.
 * display_name - the preferred displayable name, either <host name>, if available, or <computerListField>.
 * 
 * Note that .text is available too but see the notes on visible text above as its format will change above depending
 * on whether the host name is available or not.
 */
function computerListPopulatePulldown(pulldown_arg, computerList, computerListField)
{
	/*
	 * Determine whether the pulldown_arg refers to a single pulldown object OR
	 * a string that indicates an array of them.
	 * Note that when pulldown_arg is a string, then the number of pulldown objects
	 * is determined by counting from 'pulldown_arg'XXX where XXX is 0 to <some upper value>.
	 */
	var pulldown_count = 0;
	var pulldown_objects = new Array;
	if (typeof(pulldown_arg) != 'string') {
		/*
		 * Single pulldown object
		 */
		pulldown_count = 1;
		pulldown_objects[0] = pulldown_arg;
	} else {
		/*
		 * Count the number of "pulldown_arg"+XXX form objects
		 */
		while (typeof(document.mainform[pulldown_arg + pulldown_count]) != "undefined") {
			pulldown_objects[pulldown_count] = document.mainform[pulldown_arg + pulldown_count];
			pulldown_count++;
		}
	}

	/*
	 * Re-Initialise the pulldown objects
	 */
	for (var pulldown = 0; pulldown < pulldown_count; ++pulldown) {
		/*
		 * Disable the pulldown so that it does not conflict with any user-interaction
		 */
		pulldown_objects[pulldown].disabled = true;

		/*
		 * Remember the selection value of this pulldown, if any
		 */
		var current_value_selection = null;
		try {
			var current_selected_index = pulldown_objects[pulldown].selectedIndex;
			current_value_selection = pulldown_objects[pulldown].options[current_selected_index].value;
		} catch (e) {
			current_value_selection = "-1";
		}

		/*
		 * Clear the pulldown object as we are now going to refresh the list
		 */
		pulldown_objects[pulldown].options.length = 0;

		/*
		 * Populate this pulldown with the new computer list
		 */
		var selected_index = 0;
		for (var entry = 0; entry < (computerList.length + 1); ++entry) {

			var opt = document.createElement("OPTION");

			if (entry == 0) {
				/*
				 * The first entry in the options pulldown becomes descriptive label
				 */
				opt.text = "Computer Name";
				opt.value = "-1";
			} else {
				/*
				 * Value is assigned to the given 'required field' value, i.e. the one given in computerListField
				 */
				var opt_value = eval("computerList[entry - 1]." + computerListField);

				/*
				 * Text is assigned as "<host> (<computerListField>)" or simply "<computerListField>"
				 */
				var opt_text = "";
				var display_name = "";
				if (computerList[entry - 1].host_name.length > 0) {
					display_name = computerList[entry - 1].host_name;
					opt_text = display_name + " (" + eval("computerList[entry - 1]." + computerListField) + ")";
				} else {
					opt_text = eval("computerList[entry - 1]." + computerListField);
					display_name = opt_text;
				}

				opt.value = opt_value;
				opt.text = opt_text;

				/*
				 * Store additional information with the option for fast retrieval later
				 */
				opt.setAttribute("mac", computerList[entry - 1].mac);
				opt.setAttribute("host_name", computerList[entry - 1].host_name);
				opt.setAttribute("ip_address", computerList[entry - 1].ip_address);
				opt.setAttribute("display_name", display_name);
			}

			/*
			 * Add the new option
			 */
			pulldown_objects[pulldown].options.add(opt);

			/*
			 * If the previously selected value matches this entry then we remeber the
			 * index of it so that we can restore the selected position
			 */
			if (opt.value == current_value_selection) {
				selected_index = entry;
			}
		}

		/*
		 * Re-enable the pulldown
		 */
		pulldown_objects[pulldown].disabled = false;

		/*
		 * Restore the selected position
		 */
		pulldown_objects[pulldown].selectedIndex = selected_index;

	}
}

/*
 * populateVirtualServerPulldown()
 *	Populates default virtual server entries into dropdown box
 */
function populateVirtualServerPulldown (obj)
{
	obj.options[0] = new Option ("Application Name", -1);
	obj.options[1] = new Option ("TELNET", 0);
	obj.options[2] = new Option ("HTTP",1);	
	obj.options[3] = new Option ("HTTPS", 2);
	obj.options[4] = new Option ("FTP",3);	
	obj.options[5] = new Option ("DNS", 4);
	obj.options[6] = new Option ("SMTP",5);	
	obj.options[7] = new Option ("POP3", 6);
	obj.options[8] = new Option ("H.323",7);
	obj.options[9] = new Option ("REMOTE DESKTOP",8);		
	obj.options[10] = new Option ("PPTP",9);	
	obj.options[11] = new Option ("L2TP",10);	
	obj.options[12] = new Option ("Wake-On-LAN",11);
}

/*
 * computeVirtualServerPulldown()
 *	Populates 'default_virtual_servers' and 'default_virtual_servers_NNN' controls.
 */
function computeVirtualServerPulldown()
{
	if (document.mainform.default_virtual_servers) {
		populateVirtualServerPulldown (document.mainform.default_virtual_servers);
	}
	var N = 0;
	while (typeof(document.mainform['default_virtual_servers_'+N]) != "undefined") {
		populateVirtualServerPulldown (document.mainform['default_virtual_servers_'+N]);
		N++;
	}
}


/*
 * getVirtualServerEntry()
 *	Returns associated information of selected virtual server entry from dropdown box.
 *
 * NOTE: The 'alg_assoc' names MUST match (case sentitive) the name given to the ALG, unless no association is needed.
 */
function getVirtualServerEntry (ctrl)
{
	virual_servers = new Array();
	
	virual_servers[0] = new Object;
	virual_servers[0].private_port = 23;
	virual_servers[0].protocol = 6;
	virual_servers[0].public_port = 23;
	virual_servers[0].alg_assoc = "";

	virual_servers[1] = new Object;
	virual_servers[1].private_port = 80;
	virual_servers[1].protocol = 6;
	virual_servers[1].public_port = 80;
	virual_servers[1].alg_assoc = "";
	
	virual_servers[2] = new Object;
	virual_servers[2].private_port = 443;
	virual_servers[2].protocol = 6;
	virual_servers[2].public_port = 443;
	virual_servers[2].alg_assoc = "";

	virual_servers[3] = new Object;
	virual_servers[3].private_port = 21;
	virual_servers[3].protocol = 6;
	virual_servers[3].public_port = 21;
	virual_servers[3].alg_assoc = "FTP";
	
	virual_servers[4] = new Object;
	virual_servers[4].private_port = 53;
	virual_servers[4].protocol = 17;
	virual_servers[4].public_port = 53;
	virual_servers[4].alg_assoc = "";

	virual_servers[5] = new Object;
	virual_servers[5].private_port = 25;
	virual_servers[5].protocol = 6;
	virual_servers[5].public_port = 25;
	virual_servers[5].alg_assoc = "";
	
	virual_servers[6] = new Object;
	virual_servers[6].private_port = 110;
	virual_servers[6].protocol = 6;
	virual_servers[6].public_port = 110;
	virual_servers[6].alg_assoc = "";

	virual_servers[7] = new Object;
	virual_servers[7].private_port = 1720;
	virual_servers[7].protocol = 6;
	virual_servers[7].public_port = 1720;
	virual_servers[7].alg_assoc = "H.323";

	virual_servers[8] = new Object;
	virual_servers[8].private_port = 3389;
	virual_servers[8].protocol = 6;
	virual_servers[8].public_port = 3389;
	virual_servers[8].alg_assoc = "";

	virual_servers[9] = new Object;
	virual_servers[9].private_port = 1723;
	virual_servers[9].protocol = 6;
	virual_servers[9].public_port = 1723;
	virual_servers[9].alg_assoc = "PPTP Control";
    
	virual_servers[10] = new Object;
	virual_servers[10].private_port = 1701;
	virual_servers[10].protocol = 17;
	virual_servers[10].public_port = 1701;
	virual_servers[10].alg_assoc = "";

	virual_servers[11] = new Object;
	virual_servers[11].private_port = 9;
	virual_servers[11].protocol = 17;
	virual_servers[11].public_port = 9;
	virual_servers[11].alg_assoc = "Wake-On-LAN";

	var opt = ctrl.options[ctrl.selectedIndex];
	var index = opt.value;
	virual_servers[index].name = opt.text;
	return virual_servers[index];
}

/*
 * populateGamePulldown()
 *	Populates default gaming entries into dropdown box
 */
function populateGamePulldown(obj)
{
	obj.options[0] = new Option ("Application Name", -1);
	obj.options[1] = new Option ("Age of Empires", 0);
	obj.options[2] = new Option ("Aliens vs. Predator", 2);
	obj.options[3] = new Option ("America's Army", 1);
	obj.options[4] = new Option ("Asheron's Call", 3);
	obj.options[5] = new Option ("Battlefield 1942", 4);
	obj.options[6] = new Option ("Battlefield 2", 84);
	obj.options[7] = new Option ("Battlefield: Vietnam", 5);		
	obj.options[8] = new Option ("BitTorrent", 63);
	obj.options[9] = new Option ("Black and White", 6);	
	obj.options[10] = new Option ("Call of Duty", 7);		
	obj.options[11] = new Option ("Command and Conquer Generals", 8);
	obj.options[12] = new Option ("Command and Conquer Zero Hour", 9);		
	obj.options[13] = new Option ("Counter Strike", 10);
	obj.options[14] = new Option ("Crimson Skies", 11);		
	obj.options[15] = new Option ("D-Link DVC-1000", 83);
	obj.options[16] = new Option ("Dark Reign 2", 12);
	obj.options[17] = new Option ("Delta Force", 13);
	obj.options[18] = new Option ("Diablo I and II", 14);
	obj.options[19] = new Option ("Doom 3", 15);
	obj.options[20] = new Option ("Dungeon Siege", 16);
	obj.options[21] = new Option ("eDonkey", 65);
	obj.options[22] = new Option ("eMule", 67);
	obj.options[23] = new Option ("Everquest", 17);
	obj.options[24] = new Option ("Far Cry", 18);
	obj.options[25] = new Option ("Final Fantasy XI (PC)", 20);
	obj.options[26] = new Option ("Final Fantasy XI (PS2)", 21);
	obj.options[27] = new Option ("Gamespy Arcade", 76);
	obj.options[28] = new Option ("Gamespy Tunnel", 77);
	obj.options[29] = new Option ("Ghost Recon", 19);
	obj.options[30] = new Option ("Gnutella", 64);
	obj.options[31] = new Option ("Half Life", 22);
	obj.options[32] = new Option ("Halo: Combat Evolved ", 23);
	obj.options[33] = new Option ("Heretic II", 24);
	obj.options[34] = new Option ("Hexen II", 25);
	obj.options[35] = new Option ("Jedi Knight II: Jedi Outcast ", 26);
	obj.options[36] = new Option ("Jedi Knight III: Jedi Academy ", 27);
	obj.options[37] = new Option ("KALI", 75);
	obj.options[38] = new Option ("Links", 28);
	obj.options[39] = new Option ("Medal of Honor: Games", 29);
	obj.options[40] = new Option ("MSN Game Zone", 73);
	obj.options[41] = new Option ("MSN Game Zone (DX)", 74);
	obj.options[42] = new Option ("Myth", 30);
	obj.options[43] = new Option ("Need for Speed", 31);
	obj.options[44] = new Option ("Need for Speed 3", 33);
	obj.options[45] = new Option ("Need for Speed: Hot Pursuit 2", 32);
	obj.options[46] = new Option ("Neverwinter Nights", 34);
	obj.options[47] = new Option ("PainKiller ", 35);
	obj.options[48] = new Option ("PlayStation2 ", 81);
	obj.options[49] = new Option ("Postal 2: Share the Pain ", 36);
	obj.options[50] = new Option ("Quake 2", 37);
	obj.options[51] = new Option ("Quake 3", 38);
	obj.options[52] = new Option ("Rainbow Six", 39);
	obj.options[53] = new Option ("Rainbow Six: Raven Shield ", 40);
	obj.options[54] = new Option ("Return to Castle Wolfenstein ", 41);
	obj.options[55] = new Option ("Rise of Nations", 42);
	obj.options[56] = new Option ("Roger Wilco", 78);
	obj.options[57] = new Option ("Rogue Spear", 43);
	obj.options[58] = new Option ("Serious Sam II", 44);
	obj.options[59] = new Option ("Shareaza", 66);
	obj.options[60] = new Option ("Silent Hunter II", 46);
	obj.options[61] = new Option ("Soldier of Fortune", 47);
	obj.options[62] = new Option ("Soldier of Fortune II: Double Helix", 48);
	obj.options[63] = new Option ("Splinter Cell: Pandora Tomorrow",45);
	obj.options[64] = new Option ("Star Trek: Elite Force II", 51);
	obj.options[65] = new Option ("Starcraft", 49);
	obj.options[66] = new Option ("Starsiege Tribes", 50);	
	obj.options[67] = new Option ("Steam", 72);
	obj.options[68] = new Option ("SWAT 4", 82);
	obj.options[69] = new Option ("TeamSpeak", 79);
	obj.options[70] = new Option ("Tiberian Sun", 52);
	obj.options[71] = new Option ("Tiger Woods 2K4", 53);
	obj.options[72] = new Option ("Tribes of Vengeance", 80);
	obj.options[73] = new Option ("Ubi.com", 71);
	obj.options[74] = new Option ("Ultima", 54);
	obj.options[75] = new Option ("Unreal", 55);
	obj.options[76] = new Option ("Unreal Tournament", 56);
	obj.options[77] = new Option ("Unreal Tournament 2004", 57);
	obj.options[78] = new Option ("Vietcong ", 58);
	obj.options[79] = new Option ("Warcraft II", 59);
	obj.options[80] = new Option ("Warcraft III", 60);
	obj.options[81] = new Option ("WinMX", 68);
	obj.options[82] = new Option ("Wolfenstein: Enemy Territory ", 61);
	obj.options[83] = new Option ("WON Servers", 69);
	obj.options[84] = new Option ("World of Warcraft", 62);	
	obj.options[85] = new Option ("Xbox Live", 70);
}

/*
 * computeGamePulldown()
 *	Populates default gaming entries into dropdown box
 */
function computeGamePulldown()
{
	if (document.mainform.default_games) {
		populateGamePulldown (document.mainform.default_games);
	}
	var N = 0;
	while (typeof(document.mainform['default_games_'+N]) != "undefined") {
		populateGamePulldown (document.mainform['default_games_'+N]);
		N++;
	}
}

/*
 * getGammingEntry()
 *	Returns associated information of selected game entry from dropdown box.
 */
function getGammingEntry(ctrl)
{
	games = new Array();
	
	games[0] = new Object;
	games[0].tcp_port_to_open = "2302-2400,6073"							
	games[0].udp_port_to_open = "2302-2400,6073"
	
	games[1] = new Object;
	games[1].tcp_port_to_open = "20045"
	games[1].udp_port_to_open = "1716-1718,8777,27900"
	
	games[2] = new Object;
	games[2].tcp_port_to_open = "80,2300-2400,8000-8999"
	games[2].udp_port_to_open = "80,2300-2400,8000-8999"
	
	games[3] = new Object;
	games[3].tcp_port_to_open = "9000-9013"
	games[3].udp_port_to_open = "2001,9000-9013"
		
	games[4] = new Object;
	games[4].tcp_port_to_open = ""
	games[4].udp_port_to_open = "14567,22000,23000-23009,27900,28900"
	
	games[5] = new Object;
	games[5].tcp_port_to_open = ""
	games[5].udp_port_to_open = "4755,23000,22000,27243-27245"
	
	games[6] = new Object;
	games[6].tcp_port_to_open = "2611-2612,6500,6667,27900"
	games[6].udp_port_to_open = "2611-2612,6500,6667,27900"
	
	games[7] = new Object;
	games[7].tcp_port_to_open = "28960"
	games[7].udp_port_to_open = "28960,20500,20510"
	
	games[8] = new Object;
	games[8].tcp_port_to_open = "80,6667,28910,29900,29920"
	games[8].udp_port_to_open = "4321,27900"
	
	games[9] = new Object;
	games[9].tcp_port_to_open = "80,6667,28910,29900,29920"
	games[9].udp_port_to_open = "4321,27900"
	
	games[10] = new Object;
	games[10].tcp_port_to_open = "27030-27039"
	games[10].udp_port_to_open = "1200,27000-27015"
	
	games[11] = new Object;
	games[11].tcp_port_to_open = "1121,3040,28801,28805"
	games[11].udp_port_to_open = ""
	
	games[12] = new Object;
	games[12].tcp_port_to_open = "26214"	
	games[12].udp_port_to_open = "26214"
	
	games[13] = new Object;
	games[13].tcp_port_to_open = "3100-3999"
	games[13].udp_port_to_open = "3568"

	games[14] = new Object;
	games[14].tcp_port_to_open = "6112-6119,4000"
	games[14].udp_port_to_open = "6112-6119"

	games[15] = new Object;
	games[15].tcp_port_to_open = ""
	games[15].udp_port_to_open = "27666"

	games[16] = new Object;
	games[16].tcp_port_to_open = ""
	games[16].udp_port_to_open = "6073,2302-2400"

	games[17] = new Object;
	games[17].tcp_port_to_open = "1024-6000,7000"
	games[17].udp_port_to_open = "1024-6000,7000"

	games[18] = new Object;
	games[18].tcp_port_to_open = ""
	games[18].udp_port_to_open = "49001,49002"

	games[19] = new Object;
	games[19].tcp_port_to_open = "2346-2348"
	games[19].udp_port_to_open = "2346-2348"

	games[20] = new Object;
	games[20].tcp_port_to_open = "25,80,110,443,50000-65535"
	games[20].udp_port_to_open = "50000-65535"

	games[21] = new Object;
	games[21].tcp_port_to_open = "1024-65535"
	games[21].udp_port_to_open = "50000-65535"

	games[22] = new Object;
	games[22].tcp_port_to_open = "6003, 7002"
	games[22].udp_port_to_open = "27005,27010,27011,27015"

	games[23] = new Object;
	games[23].tcp_port_to_open = ""
	games[23].udp_port_to_open = "2302,2303"

	games[24] = new Object;
	games[24].tcp_port_to_open = "28910"
	games[24].udp_port_to_open = "28910"

	games[25] = new Object;
	games[25].tcp_port_to_open = "26900"
	games[25].udp_port_to_open = "26900"

	games[26] = new Object;	
	games[26].tcp_port_to_open = ""
	games[26].udp_port_to_open = "28060,28061,28062,28070-28081"

	games[27] = new Object;	
	games[27].tcp_port_to_open = ""
	games[27].udp_port_to_open = "28060,28061,28062,28070-28081"

	games[28] = new Object;	
	games[28].tcp_port_to_open = "2300-2400,47624"
	games[28].udp_port_to_open = "2300-2400,6073"

	games[29] = new Object;	
	games[29].tcp_port_to_open = "12203-12204"
	games[29].udp_port_to_open = ""

	games[30] = new Object;	
	games[30].tcp_port_to_open = "3453"
	games[30].udp_port_to_open = "3453"
	
	games[31] = new Object;
	games[31].tcp_port_to_open = "9442"
	games[31].udp_port_to_open = "9442"

	games[32] = new Object;
	games[32].tcp_port_to_open = "8511,28900"	
	games[32].udp_port_to_open = "1230,8512,27900,61200-61230"

	games[33] = new Object;
	games[33].tcp_port_to_open = "1030"	
	games[33].udp_port_to_open = "1030"

	games[34] = new Object;
	games[34].tcp_port_to_open = ""	
	games[34].udp_port_to_open = "5120-5300,6500,27900,28900"

	games[35] = new Object;
	games[35].tcp_port_to_open = ""	
	games[35].udp_port_to_open = "3455"

	games[36] = new Object;
	games[36].tcp_port_to_open = "80"	
	games[36].udp_port_to_open = "7777-7779,27900,28900"

	games[37] = new Object;
	games[37].tcp_port_to_open = "27910"	
	games[37].udp_port_to_open = "27910"

	games[38] = new Object;
	games[38].tcp_port_to_open = "27660,27960"	
	games[38].udp_port_to_open = "27660,27960"

	games[39] = new Object;
	games[39].tcp_port_to_open = "2346"	
	games[39].udp_port_to_open = "2346"

	games[40] = new Object;
	games[40].tcp_port_to_open = ""	
	games[40].udp_port_to_open = "7777-7787,8777-8787"

	games[41] = new Object;
	games[41].tcp_port_to_open = ""	
	games[41].udp_port_to_open = "27950,27960,27965,27952"

	games[42] = new Object;
	games[42].tcp_port_to_open = ""	
	games[42].udp_port_to_open = "34987"

	games[43] = new Object;
	games[43].tcp_port_to_open = "2346"	
	games[43].udp_port_to_open = "2346"

	games[44] = new Object;
	games[44].tcp_port_to_open = "25600-25605"
	games[44].udp_port_to_open = "25600-25605"

	games[45] = new Object;
	games[45].tcp_port_to_open = "40000-43000"	
	games[45].udp_port_to_open = "44000-45001,7776,8888"

	games[46] = new Object;
	games[46].tcp_port_to_open = "3000"	
	games[46].udp_port_to_open = "3000"

	games[47] = new Object;
	games[47].tcp_port_to_open = ""	
	games[47].udp_port_to_open = "28901,28910,38900-38910,22100-23000"

	games[48] = new Object;
	games[48].tcp_port_to_open = ""	
	games[48].udp_port_to_open = "20100-20112"

	games[49] = new Object;
	games[49].tcp_port_to_open = "6112-6119,4000"	
	games[49].udp_port_to_open = "6112-6119"

	games[50] = new Object;
	games[50].tcp_port_to_open = ""	
	games[50].udp_port_to_open = "27999,28000"

	games[51] = new Object;
	games[51].tcp_port_to_open = ""	
	games[51].udp_port_to_open = "29250,29256"

	games[52] = new Object;
	games[52].tcp_port_to_open = "1140-1234,4000"	
	games[52].udp_port_to_open = "1140-1234,4000"

	games[53] = new Object;
	games[53].tcp_port_to_open = "80,443,1791-1792,13500,20801-20900,32768-65535"	
	games[53].udp_port_to_open = "80,443,1791-1792,13500,20801-20900,32768-65535"

	games[54] = new Object;
	games[54].tcp_port_to_open = "5001-5010,7775-7777,7875,8800-8900,9999"	
	games[54].udp_port_to_open = "5001-5010,7775-7777,7875,8800-8900,9999"

	games[55] = new Object;
	games[55].tcp_port_to_open = "7777,8888,27900"	
	games[55].udp_port_to_open = "7777-7781"

	games[56] = new Object;
	games[56].tcp_port_to_open = "7777-7783,8080,27900"	
	games[56].udp_port_to_open = "7777-7783,8080,27900"

	games[57] = new Object;
	games[57].tcp_port_to_open = "28902"	
	games[57].udp_port_to_open = "7777-7778,7787-7788"

	games[58] = new Object;
	games[58].tcp_port_to_open = ""	
	games[58].udp_port_to_open = "5425,15425,28900"

	games[59] = new Object;
	games[59].tcp_port_to_open = "6112-6119,4000"	
	games[59].udp_port_to_open = "6112-6119"

	games[60] = new Object;
	games[60].tcp_port_to_open = "6112-6119,4000"	
	games[60].udp_port_to_open = "6112-6119"

	games[61] = new Object;
	games[61].tcp_port_to_open = ""	
	games[61].udp_port_to_open = "27950,27960,27965,27952"

	games[62] = new Object;
	games[62].tcp_port_to_open = "3724,6112,6881-6999"
	games[62].udp_port_to_open = ""

	games[63] = new Object;
	games[63].tcp_port_to_open = "6881-6889"	
	games[63].udp_port_to_open = ""

	games[64] = new Object;
	games[64].tcp_port_to_open = "6346"	
	games[64].udp_port_to_open = "6346"

	games[65] = new Object;
	games[65].tcp_port_to_open = "4661-4662"	
	games[65].udp_port_to_open = "4665"

	games[66] = new Object;
	games[66].tcp_port_to_open = "6346"	
	games[66].udp_port_to_open = "6346"

	games[67] = new Object;
	games[67].tcp_port_to_open = "4661-4662,4711"	
	games[67].udp_port_to_open = "4672,4665"

	games[68] = new Object;
	games[68].tcp_port_to_open = "6699"	
	games[68].udp_port_to_open = "6257"

	games[69] = new Object;
	games[69].tcp_port_to_open = "27000-27999"	
	games[69].udp_port_to_open = "15001,15101,15200,15400"

	games[70] = new Object;
	games[70].tcp_port_to_open = "3074"	
	games[70].udp_port_to_open = "88,3074"

	games[71] = new Object;
	games[71].tcp_port_to_open = "40000-42999"	
	games[71].udp_port_to_open = "41005"

	games[72] = new Object;
	games[72].tcp_port_to_open = "27030-27039"	
	games[72].udp_port_to_open = "1200,27000-27015"

	games[73] = new Object;
	games[73].tcp_port_to_open = "6667"	
	games[73].udp_port_to_open = "28800-29000"

	games[74] = new Object;
	games[74].tcp_port_to_open = "2300-2400,47624"	
	games[74].udp_port_to_open = "2300-2400"

	games[75] = new Object;
	games[75].tcp_port_to_open = ""
	games[75].udp_port_to_open = "2213,6666"

	games[76] = new Object;
	games[76].tcp_port_to_open = ""	
	games[76].udp_port_to_open = "6500"

	games[77] = new Object;
	games[77].tcp_port_to_open = ""	
	games[77].udp_port_to_open = "6700"

	games[78] = new Object;
	games[78].tcp_port_to_open = "3782"	
	games[78].udp_port_to_open = "27900,28900,3782-3783"

	games[79] = new Object;
	games[79].tcp_port_to_open = ""	
	games[79].udp_port_to_open = "8767"

	games[80] = new Object;
	games[80].tcp_port_to_open = "7777,7778,28910"
	games[80].udp_port_to_open = "6500,7777,7778,27900"

	games[81] = new Object;
	games[81].tcp_port_to_open = "4658,4659"
	games[81].udp_port_to_open = "4658,4659"

	games[82] = new Object;
	games[82].tcp_port_to_open = ""
	games[82].udp_port_to_open = "10480-10483"

	games[83] = new Object;
	games[83].tcp_port_to_open = "1720,15328-15333"
	games[83].udp_port_to_open = "15328-15333"

	games[84] = new Object;
	games[84].tcp_port_to_open = "80,4711,29900,29901,29920,28910"
	games[84].udp_port_to_open = "1500-4999,16567,27900,29900,29910,27901,55123,55124,55215"

	var opt = ctrl.options[ctrl.selectedIndex];
	var index = opt.value;
	games[index].name = opt.text;
	return games[index];
}

/*
 * populateSpecialApplicationPulldown()
 *	Populates default Special Application entries into dropdown box
 */
function populateSpecialApplicationPulldown(obj)
{
	obj.options[0] = new Option ("Application Name", -1);
	obj.options[1] = new Option ("AIM Talk", 0);
	obj.options[2] = new Option ("BitTorrent", 1);
	obj.options[3] = new Option ("Calista IP phone", 2);
	obj.options[4] = new Option ("ICQ", 3);
	obj.options[5] = new Option ("MSN Messenger", 4);
	obj.options[6] = new Option ("PalTalk", 5);
}

/*
 * computeSpecialApplicationPulldown()
 *	Populates default Special Application entries into dropdown box
 */
function computeSpecialApplicationPulldown()
{
	populateSpecialApplicationPulldown (document.mainform.default_special_applications);
	var N = 0;
	while (typeof(document.mainform['default_special_applications_'+N]) != "undefined") {
		populateSpecialApplicationPulldown (document.mainform['default_special_applications_'+N]);
		N++;
	}
}

/*
 * getSpecialApplicationEntry()
 *	Returns associated information of selected special application entry from dropdown box.
 */
function getSpecialApplicationEntry(ctrl)
{
	special_application = new Array();
	
	special_application[0] = new Object;
	special_application[0].input_ports_port_range = "5190" 
	special_application[0].input_ports_protocol = 6
	special_application[0].trigger_ports_port_range = "4099"
	special_application[0].trigger_ports_protocol = 6;
	
	special_application[1] = new Object;
	special_application[1].input_ports_port_range = "6881-6889";
	special_application[1].input_ports_protocol = 6;
	special_application[1].trigger_ports_port_range = "6969";
	special_application[1].trigger_ports_protocol = 6;
	
	special_application[2] = new Object;
	special_application[2].input_ports_port_range = "3000";
	special_application[2].input_ports_protocol = 17;
	special_application[2].trigger_ports_port_range = "5190";
	special_application[2].trigger_ports_protocol = 6;
 
	special_application[3] = new Object;
	special_application[3].input_ports_port_range = "20000,20019,20039,20059";
	special_application[3].input_ports_protocol = 6;
	special_application[3].trigger_ports_port_range = "4000";
	special_application[3].trigger_ports_protocol = 17;

	special_application[4] = new Object;
	special_application[4].input_ports_port_range = "1863,5190,6891,6901";
	special_application[4].input_ports_protocol = 0;
	special_application[4].trigger_ports_port_range = "1863,5190,6891,6901";
	special_application[4].trigger_ports_protocol = 0;
	
	special_application[5] = new Object;
	special_application[5].input_ports_port_range = "2090,2091,2095";
	special_application[5].input_ports_protocol = 0;
	special_application[5].trigger_ports_port_range = "5001-5020";
	special_application[5].trigger_ports_protocol = 6;

	var opt = ctrl.options[ctrl.selectedIndex];
	var index = opt.value;
	special_application[index].entry_name = opt.text;
	return special_application[index];
}

/*
 * computeBigPondServerPulldown()
 *	Populates default BigPond server entries into dropdown box
 */
function computeBigPondServerPulldown()
{
	document.mainform.bigpond_servers.options[0] = new Option ("Select BigPond Server", -1);
	document.mainform.bigpond_servers.options[1] = new Option ("Name1", 0);
	document.mainform.bigpond_servers.options[2] = new Option ("Name2", 1);
	document.mainform.bigpond_servers.options[3] = new Option ("Name3", 2);
}

/*
 * DebugDisplayFields
 *	Dump the current wireless setting if the ctrlKey is pressed
 */
function DebugDisplayFields(when)
{
	if (!window || !window.event || !window.event.ctrlKey) {
		return;
	}	
	
	var s = "";
	s += when + ":\n";
	s += "SSID=" + data.wireless.SSID + "\n";
	s += "radio_control=" + data.wireless.radio_control + "\n";
	s += "region_id=" + data.wireless.region_id + "\n";
	s += "channel=" + data.wireless.channel + "\n";
	s += "wpa_enabled="	+ data.wireless.wpa_enabled + "\n";
	s += "wpa_psk=" + data.wireless.wpa_psk + "\n";
	s += "wpa_cipher=" + data.wireless.wpa_cipher + "\n";
	s += "wpa_rekey_time=" + data.wireless.wpa_rekey_time + "\n";
	s += "ieee8021x_enabled=" + data.wireless.ieee8021x_enabled + "\n";
	s += "wepon=" + data.wireless.wepon + "\n";
	s += "keylen=" + data.wireless.keylen + "\n";
	s += "wep_key[0]=" + data.wireless.wep_key[0] + "\n";
	s += "use_key=" + data.wireless.use_key + "\n";
	s += "auth=" + data.wireless.auth + "\n";
	alert(s);
}

/*
 * LoadActiveX
 *	Load the WCN ActiveX control
 */
function LoadActiveX(wcn_id, classid, codebase)
{
	document.getElementById("wcn_activex").innerHTML = '<object classid=\"clsid:' + classid + '\" ID=\"' + wcn_id + '\" VIEWASTEXT height=0 width=0 codebase=\"' + codebase + '\"></object>';
}

/*
 * GetIEVersion
 *	Get the major version of IE. Return -1 if it is not IE
 */
function GetIEVersion()
{
	var agt=navigator.userAgent.toLowerCase();
	var ie_ix = agt.indexOf("msie");
	if (ie_ix != -1) {
		return (agt.charAt(ie_ix + 5) - '0');
	}
	return -1;
}

/*
 * IsActiveXReady
 *	Is the ActiveX is ready to be used
 */
function IsActiveXReady(ax)
{
	return ax.ReadyState == 4;
}

function DebugAlert(msg)
{
	if (window && window.event && window.event.ctrlKey) {
		alert(msg);
	}	
}

/*
 * IsWCNSupported
 *	WCN is only supported on Windows XP/SP2 or later platforms, parse the userAgent string as much as possible to
 *	determin the os platform.
 *
 * check http://forums.devarticles.com/archive/t-1031/UserAgent for the list of userAgent formats
 * on different browsers/versions.
 */
function IsWCNSupported() 
{
	var agt = navigator.userAgent.toLowerCase();
	
	// msie 5.0 shows 'windows xp' instead of 'windows nt 5.1' and no sp version. 
	// the activex has to take care of the sp version check in this case
	if (agt.indexOf("windows xp") != -1) {
		return true;
	}
	
	// otherwise, the xp name string has to be 'windows nt 5.1;'
	var os_ix = agt.indexOf("windows nt ");
	if (os_ix == -1) {
		return false;
	}
	var major = agt.charAt(os_ix + 11);
	var minor = agt.charAt(os_ix + 13);
	// it's XP or newer version os
	if (major >= '5' && minor > '1') {
		return true;
	}
	
	// only MSIE 6.0 or later gives Service Pack version in userAgent string.
	// again, the activex has to take care of the sp version check in this case
	if (GetIEVersion >= 6) {
		// check SP version, SP2 should be shown as sv1
		var sp_ix = agt.indexOf("sv");
		if (sp_ix <= os_ix) {
			return false;
		}
		var sp_v = agt.charAt(sp_ix + 2);
		if (sp_v < '1') {
			return false;
		}
	}
	
	return true;
}

/*
 * IsActiveXSupported
 *	Whether the browser supports ActiveX
 *
 * To make things simpler, only IE4.0 or later is supported here
 */
function IsActiveXSupported()
{
	return (GetIEVersion() >= 4);
	
	/*
		Microsoft IE 4.0 or later supports ActiveX. Most of other browsers, like Netscape and Firefox,
		may have plugins to support ActiveX. However, very often Active is only enabled for a few controls
		and it needs the user to add the clsid to the allowed list, too complex for WCN.
	
	var pi = navigator.plugins;
	for (var i=0; i< pi.length; i++) {
		if (pi[i].name.toLowerCase().indexOf("activex") != -1) {
			return true;
		}
	}
	return false;
	*/
}

/*
 * LoadWCNObject
 *	Common procedure to dynamically load the ActiveX control and verify the state of the control
 */
function LoadWCNObject(wcn_id, classid, codebase) 
{
	// http://www.metalusions.com/backstage/articles/8/, for older versions of IE
	if(document.all && !document.getElementById) {
		document.getElementById = function(id) {
			return document.all[id];
		}
	}

	var wcn_obj = document.getElementById(wcn_id);
	
	if (!wcn_obj) {
		LoadActiveX(wcn_id, classid, codebase);
		wcn_obj = document.getElementById(wcn_id);
		if (!IsActiveXReady(wcn_obj)) {
			alert('Please accept and install the ActiveX, then try it again.');
			return false;
		}
	} else  {
		if (!IsActiveXReady(wcn_obj)) {
			alert('WCN ActiveX control is not available. Please check the security setting and refresh this page to install it.');
			return false;
		}
	}	
	return true;
}

/*
 * WCNValidateWSettings
 *	Check if the current wireless settings are supported by WCN
 */
function WCNValidateWSettings()
{
	if (data.wireless.wepon && data.wireless.use_key != 0) {
		alert('WCN does not support key index other than 1 now.');
		return false;
	}
	
	if (data.wireless.wpa_enabled) {
		if (data.wireless.wpa_mode >= 2) {
			alert('WCN does not support WPA2 mode now.');
			return false;
		}
		
		if (data.wireless.ieee8021x_enabled) {
			alert('WCN does not support WPA enterprise authentication now.');
			return false;
		}
	}

	// TODO: add more validation here	
	return true;
}

/*
 * WCNErrorReportHandling
 *	Error reporting helper function
 */
function WCNErrorReportHandling(wcn_obj, isSaving)
{
	if (wcn_obj.err_code == 0) {
		alert('Wireless settings saved successfully');
		return;
	}
	
	var error_msg = 'Error code: ' + wcn_obj.err_code + '; ';
	
	switch (wcn_obj.err_code) {
	case 1: // _E_OS_VERSION
		error_msg += 'This version of the operation system does not support WCN';
		break;

	case 2: // _E_IO_FILE_NAME
	case 3: // _E_IO_LOAD_CONFIG
	case 4: // _E_DECRYPTION
		if (isSaving) {
			error_msg += 'Loading the Wireless Configuration file failed.  Please run Windows Wireless Network Setup Wizard to create/re-create the configuration file';
		} else {
			error_msg += 'Loading the Wireless Configuration file failed.  Please run Windows Wireless Network Setup Wizard to create/re-create the configuration file';
		}
		break;

	case 5: // _E_PROVISION
		error_msg += 'Failed to add the wireless profile. Please make sure that the new SSID does not conflict with an existing profile';
		break;

	case 6: // _E_IO_WRITE_CONFIG
		error_msg += 'Failed to write wireless data in to configuration file. Please check the security setting';
		break;

	case 7: //_E_ENCRYPTION
		error_msg += 'Failed to encrypt wireless data.';
		break;

	case 8: // _E_EXCEPTION 
		error_msg += 'Internal exception occured';
		break;

	case 9: // _E_COM,
		error_msg += 'WCN ActiveX control is not registered. Please check the security setting and refresh this page to install it.';
		break;

	case 11: // _E_BAD_WSETTING_ENTRY,
		error_msg += 'Invalid wireless settings. Please check wireless settings';
		break;

	case 12: // _E_BAD_WPS_PROFILE,
		error_msg += 'Cannot create the wirless profile XML file';
		break;

	case 13: // _E_UNSUPPORTED_WSETTING,
		error_msg += 'WPA security mode is not enabled. Please check WPA security settings';
		break;

	case 14: // _E_DOM_PROCESSING,
		error_msg += 'MSXML2 DOM parser failed to parse the string in XML format';
		break;

	case 10: // _E_BAD_WSETTING_XML,
	case 15: // _E_OTHER,
	default:
		error_msg += 'Unexpected error';
		break;
	}
	
	alert(error_msg);
}

function calcDayList(x)
{
	var days = new Array ("Sun","Mon","Tue","Wed","Thu","Fri","Sat");
	if (x==127) return "Every Day" + " ";
	s = "";
	for (var i=0; i<7; i++) if (x&(1<<i)) s += days[i] + " ";
	return s;
}

function calcTimeFrame(i)
{
	if (data.sched_table[i].start_time==0 && data.sched_table[i].end_time==86400) return "("+"All Day"+")";
	var m = Math.floor (data.sched_table[i].start_time/60);
	var h = Math.floor (m/60);
	m = m - h*60;
	var sh;
	var sm = m * 1;
	var sampm;
	if ( h < 12) {
		if (h == 0) {
			sh = 12;
		} else {
			sh = h;
		}
		sampm = "AM";
	} else {
		if (h != 12) {
			sh = h - 12;
		} else {
			sh = h;
		}
		sampm = "PM";
	}

	m = Math.floor (data.sched_table[i].end_time/60);
	h = Math.floor (m/60);
	m = m - h*60;
	var eh;
	var em = m * 1;
	var eampm;
	if ( h < 12) {
		if (h == 0) {
			eh = 12;
		} else {
			eh = h;
		}
		eampm = "AM";
	} else {
		if (h != 12) {
			eh = h - 12;
		} else {
			eh = h;
		}
		eampm = "PM";
	}
	
	if (sh < 10) sh = "0"+sh;
	if (sm < 10) sm = "0"+sm;
	if (eh < 10) eh = "0"+eh;
	if (em < 10) em = "0"+em;
	return '(' + sh+':'+sm+' '+sampm+' - '+eh+':'+em+' '+eampm + ')';
}

function UpdateFilterDescription()
{
	for (var i = 0; i < ARRAYSIZE_INGRESS_RULES; i++) {
		if (data.ingress_rules[i].used == 1) {
			if (document.mainform.ingress_filter_names.options[document.mainform.ingress_filter_names.selectedIndex].text == data.ingress_rules[i].ingress_filter_name) {
				if (data.ingress_rules[i].action == 0) {
					action = 'Allow';
				} else {
					action = 'Deny';
				}
				document.mainform.filtext.value = action + '  ' + getAllIpRanges(i);
				return;
			}
		}
	}
	if (document.mainform.ingress_filter_names.selectedIndex == 0) {
		document.mainform.filtext.value = "Everyone allowed";
	} else if (document.mainform.ingress_filter_names.selectedIndex == 1) {
		document.mainform.filtext.value = "No one allowed";
	} else {
		document.mainform.filtext.value = "";
	}
}

function UpdateScheduleDescription()
{
	for (var i = 0; i < ARRAYSIZE_SCHED_TABLE; i++) {
		if (data.sched_table[i].used == 1) {
			if (document.mainform.schedule_names.options[document.mainform.schedule_names.selectedIndex].text == data.sched_table[i].sched_name) {
				document.mainform.schtext.value = calcDayList(data.sched_table[i].weekdays) + calcTimeFrame(i);
				return;
			}
		}
	}
	if (document.mainform.schedule_names.selectedIndex == 0) {
		document.mainform.schtext.value = "Always";
	} else if (document.mainform.schedule_names.selectedIndex == 1) {
		document.mainform.schtext.value = "Never";
	} else {
		document.mainform.schtext.value = "";
	}
}

/*
 * Return IP range in string format.  For example, 
 * (0.0.0.0, 255.255.255.255) returns '*'
 * (10.10.0.1, 10.10.0.1)  returns '10.10.0.1'
 * (10.10.0.1, 10.10.0.10) returns '10.10.0.1-10.10.0.10'
 */
function getIp(start, end)
{
	if ((start == '0.0.0.0') && (end == '255.255.255.255')) {
		return '*';
	}
	if (start == end) {
		return start;
	}
	return start+'-'+end;
}

/*
 * Get IP ranges in ingress filter rule in string format, comma separated.
 */
function getAllIpRanges(i)
{
	var str = '', firstItem=1;
	for (var j = 0; j < ARRAYSIZE_INGRESS_RULES_ITEM_IP_RANGE_TABLE; j++) {
		if (data.ingress_rules[i].ip_range_table[j].enabled) {
			if (firstItem == 1) {
				firstItem = 0; 
			} else {
				str += ',';
			}
			str += getIp(data.ingress_rules[i].ip_range_table[j].ip_start, data.ingress_rules[i].ip_range_table[j].ip_end);
		}
	}
	return str;
}

/* 
 * isNumberInRange
 * 	Return 1 if val is within ranges in input_string 
 */
function isNumberInRange(val, input_string)
{
	input_string = new String (input_string);
	var s = input_string;
	/* check individual port numbers */
	var got = s.match (/\d+/g);
	if (got) {
		for (var i=0; i<got.length; i++) {
			var n = parseInt(got[i],10);
			if (val == n) {
				return 1;
			}			
		}
	}
		
	/* check port ranges */
	got = s.match (/\d+\s*-\s*\d+/g);
	if (got) {
		for (var i=0; i<got.length; i++) {
			var got2 = got[i].match (/(\d+)\s*-\s*(\d+)/);
			if (got2) {
				var start = parseInt(got2[1]);
				var end = parseInt(got2[2]);
				if ((start <= val) && (val <= end)) {
					return 1;
				}				
			}
		}
	}
	return 0;
}

/*
 * isNumberOverlapped
 *	Return 1 if number in string1 overlapped with string2.
 */
function isStringOverlapped(string1, string2)
{
	/* check individual port numbers */
	input_string = new String (string1);
	var s = input_string;
	var got = s.match (/\d+/g);		
	if (got) {
		for (var j=0; j<got.length; j++) {
			var n = parseInt(got[j],10);
			if (isNumberInRange(n, string2)) {
				return 1;
			}	
		}
	}
	return 0;
}
	
/*
 * isPortOverlapped
 *	Returns 1 if port range in string1 is overlapped with string2.
 *  Protocol must match.  BOTH=0, TCP=6, UDP=17.
 */
function isPortOverlapped(protocol1, string1, protocol2, string2)
{
	/* check only if both protocols are the same or one of them is both (0) */
	if ((protocol1 == protocol2) || (protocol1 == 0) || (protocol2 == 0)) {
		/* check individual port numbers in string 1 against string2, and vice versa */
		if (isStringOverlapped(string1, string2) || isStringOverlapped(string2, string1)) {
			return 1;
		}		
	}
	return 0;
}

/*
 * isProtocolOverlapped
 *	Returns 1 if protocol1 is overlapped with protocol2.
 *  BOTH(0) includes TCP(6) and UDP(17).
 *  ANY(0) includes ICMP(1), TCP(6), and UDP(17).
 */
function isProtocolOverlapped(protocol1, protocol2)
{
	// check only if both protocols are the same or one of them is 0
	if ((protocol1 == protocol2) || (protocol1 == 0) || (protocol2 == 0)) {
		return 1;
	}		
	return 0;
}

/*
 * isProtocolICMP
 *	Returns 1 if protocol is ICMP(1) or ANY (0)
 */
function isProtocolICMP(protocol)
{
	if ((protocol == 1) || (protocol == 0)) {
		return 1;
	}		
	return 0;
}

/*
 * isNumDuplicated
 *	Returns 1 if any number in string1 is overlapped with another.
 *  Examples are "10, 5-15" or "1,2,2".
 */
function isNumDuplicated(string1, error_string)
{
	var num_array = string1.split(",");

	for (var loop = 0; loop < num_array.length; loop++) {
		for (var i = 0; i < num_array.length; i++) {
			if (i != loop) {
				if (isStringOverlapped(num_array[loop], num_array[i]) 
						|| isStringOverlapped(num_array[i], num_array[loop])) {
					alert(error_string + ' [' + string1 + '] ' + 'should not contain duplicated numbers');
					return 1;
				}
			}
		}		
	}
	return 0;
}

/* 
 * isIpInRange
 * 	Return 1 if ip_str is within range of (ip_range_start, ip_range_end)
 *
 * Examples are:
 *	isIpInRange ("192.168.0.1", "192.0.0.0", "192.255.255.255") returns 1
 *	isIpInRange ("191.168.0.1", "192.0.0.0", "192.255.255.255") returns 0
 */
function isIpInRange(ip_str, ip_range_start_str, ip_range_end_str)
{
	var ip_val = IPAddressToUnsignedInteger(ip_str);
	var ip_start = IPAddressToUnsignedInteger(ip_range_start_str);
	var ip_end = IPAddressToUnsignedInteger(ip_range_end_str);

	if ((ip_start <= ip_val) && (ip_val <= ip_end)) {
		return 1;
	}
	return 0
}

/*
 * isIpRangeOverlapped
 *	Returns 1 if IP ranges are overlapped.
 *
 * Examples are:
 *	isIpRangeOverlapped ("10.0.0.0", "10.255.255.255", "10.2.2.0", "10.2.2.255") return 1
 *	isIpRangeOverlapped ("10.0.0.0", "10.255.255.255", "12.0.0.0", "12.0.0.255") return 0
 */
function isIpRangeOverlapped(ip1_start, ip1_end, ip2_start, ip2_end)
{
	if (isIpInRange (ip1_start, ip2_start, ip2_end) 
			|| isIpInRange (ip1_end, ip2_start, ip2_end) 
			|| isIpInRange (ip2_start, ip1_start, ip1_end) 
			|| isIpInRange (ip2_end, ip1_start, ip1_end)) {
		return 1;
	}
	return 0;
}

/*
 * isNumInRange
 *	Returns 1 if num is in range of (start, end)
 *
 * Examples are
 *	isNumInRange(10, 0, 20) return 1
 *	isNumInRange(30, 0, 20) return 0
 */
function isNumInRange(num, start, end)
{
	if ((start <= num) && (num <= end)) {
		return 1;
	}
	return 0;
}

/*
 * isRangeOverlapped
 *	Returns 1 if IP ranges are overlapped.
 *
 * Examples are
 *	isRangeOverlapped(10, 20, 15, 20) return 1 since (10,20) and (15,20) are overlapped
 *	isRangeOverlapped(10, 20, 21, 40) return 0 since (10,20) and (21,40) are not overlapped.
 */
function isRangeOverlapped(start1, end1, start2, end2)
{
	if (isNumInRange (start1, start2, end2) 
			|| isNumInRange (end1, start2, end2)
			|| isNumInRange (start2, start1, end1)
			|| isNumInRange (end2, start1, end1)) {
		return 1;
	}
	return 0;
}


/*
 * Translate various English strings used by the back-end into a localized equivalent.
 * This is for the few cases where the back end stores text that will be displayed to
 * the user, but also wants to interpret that text.
 */
function translate(back_end_english)
{
	// Schedules
	if (back_end_english == 'Always') {
		return 'Always';
	}
	if (back_end_english == 'Never') {
		return 'Never';
	}
	
	// Filters
	if (back_end_english == 'Allow All') {
		return 'Allow All';
	}
	if (back_end_english == 'Deny All') {
		return 'Deny All';
	}
	
	// Printer status
	if (back_end_english == 'Queued') {
		return 'Queued';
	}
	if (back_end_english == 'Starting') {
		return 'Starting';
	}
	if (back_end_english == 'Closed') {
		return 'Closed';
	}
	if (back_end_english == 'Idle') {
		return 'Idle';
	}
	if (back_end_english == 'Ready') {
		return 'Ready';
	}
	if (back_end_english == 'Offline') {
		return 'Offline';
	}
	if (back_end_english == 'Unplugged') {
		return 'Unplugged';
	}
	if (back_end_english == 'Printing') {
		return 'Printing';
	}
	if (back_end_english == 'Paper Error') {
		return 'Paper Error';
	}

	// Default
	return back_end_english;
}

function gateway_js_loaded() { return true; }
