var subnet_mask = new Array(0, 128, 192, 224, 240, 248, 252, 254, 255);
var msg = new Array("The IP Address entered is invalid",	//INVALID_IP_ADDRESS
	"The IP Address cannot be zero.",	//ZERO_IP_ADDRESS
	"IP Address",	//IP_ADDRESS_DESC
	"The Subnet Mask entered is invalid",	//INVALID_MASK_ADDRESS
	"The Subnet Mask cannot be zero",	//ZERO_MASK_ADDRESS
	"Subnet Mask",	//MASK_ADDRESS_DESC
	"The Gateway IP Address entered is invalid",	//INVALID_GATEWAY_ADDRESS
	"The Gateway IP Address cannot be zero",	//ZERO_GATEWAY_ADDRESS
	"Gateway IP Address",	//GATEWAY_ADDRESS_DESC
	"The IP Address and the Gateway IP Address are not in the same subnet",	//NOT_SAME_DOMAIN
	"The Starting IP Address entered is invalid (IP Range: 1~254)",	//INVALID_START_IP
	"Please enter another Starting IP Address",	//ZERO_START_IP
	"Starting IP Address",	//START_IP_DESC
	"The LAN IP Address and the Start IP Address are not in the same subnet",	//START_INVALID_DOMAIN
	"The Ending IP Address entered is invalid (IP Range: 1~254)",	//INVALID_END_IP
	"Please enter another Ending IP Address",	//ZERO_END_IP
	"Ending IP Address",	//END_IP_DESC
	"The LAN IP Address and the End IP Address are not in the same subnet",	//END_INVALID_DOMAIN
	"The Primary DNS Address entered is invalid",	//INVALID_DNS_ADDRESS
	"The Primary DNS Address cannot be zero",	//ZERO_DNS_ADDRESS
	"Primary DNS Address",	//DNS_ADDRESS_DESC
	"The SSID field cannot be blank",	//SSID_EMPTY_ERROR	
	"WEP cannot be disabled when the Authentication type is set to Shared Key",	//AUTH_TYPE_ERROR
	"The length of the Passphrase must be at least 8 characters",	//PSK_LENGTH_ERROR
	"The Confirmed Passphrase does not match the Passphrase",	//PSK_MATCH_ERROR
	"The Confirmed Password does not match the New Password",	//MATCH_PWD_ERROR
	"The selected WEP key field cannot be blank",	//WEP_KEY_EMPTY
	"Please enter another Key",	//WIZARD_KEY_EMPTY
	"Quit setup wizard and discard settings?",	//QUIT_WIZARD
	"The legal characters for a MAC Address are 0~9, A~F, or a~f",	//MAC_ADDRESS_ERROR	
	"The Ending IP Address must be greater than the Starting IP Address",	//IP_RANGE_ERROR
	"The Secondary DNS Address entered is invalid",	//INVALID_SEC_DNS_ADDRESS
	"The Secondary DNS Address cannot be zero",	//ZERO_SEC_DNS_ADDRESS
	"Secondary DNS Address",	//SEC_DNS_ADDRESS_DESC
	"The confirmed password does not match the new Admin password",	//ADMIN_PASS_ERROR
	"The confirmed password does not match the new User password",	//USER_PASS_ERROR
	"Please enter another Server Name",	//DDNS_SERVER_ERROR
	"Please enter another Host Name",	//DDNS_HOST_ERROR
	"Please enter another User Name",	//DDNS_USER_ERROR
	"Please enter another Password",	//DDNS_PASS_ERROR
	"Restore to Factory Default Settings?",	//RESTORE_DEFAULT
	"Are you sure you want to reboot the router?",	//REBOOT_ROUTER
	"Load settings from a saved configuration file?",	//LOAD_SETTING
	"Please select a saved configuration file to upload",	//LOAD_FILE_ERROR
	"Only the Admin account can download the settings",	//DOWNLOAD_SETTING_ERROR
	"Please enter either a Host Name or an IP Address",	//PING_IP_ERROR
	"Please enter another SMTP Server or IP Address",	//SMTP_SERVER_ERROR
	"Please enter a valid email Address",	//EMAIL_ADDRESS_ERROR
	"Are you sure that you want to delete this Virtual Server Rule?",	//DEL_SERVER_MSG
	"Are you sure that you want to delete this Application Rule",	//DEL_APPLICATION_MSG
	"Are you sure that you want to delete this Filter?",	//DEL_FILTER_MSG
	"Are you sure that you want to delete this Route?",	//DEL_ROUTE_MSG
	"Are you sure that you want to delete this MAC Address?",	//DEL_MAC_MSG
	"Are you sure that you want to delete this Keyword?",	//DEL_KEYWORD_MSG
	"Are you sure that you want to delete this Domain?",	//DEL_DOMAIN_MSG
	"Are you sure that you want to delete this Entry?",	//DEL_ENTRY_MSG
	"Are you sure that you want to delete this DHCP Reservation?",	//DEL_STATIC_DHCP_MSG
	"Please enter another name",	//NAME_ERROR
	"Please enter another Trigger Port number",	//TRIGGER_PORT_ERROR
	"Please enter a Firewall Port number",	//FIREWALL_PORT_ERROR
	"Please enter another Private Port number",	//PRIVATE_PORT_ERROR
	"Please enter a Private IP Address.",	//PRIVATE_IP_ERROR
	"Please enter another port number",	//PORT_ERROR
	"Please select a Keyword to delete",	//DEL_KEYWORD_ERROR
	"The Keyword entered already exists in the list",	//SAME_KEYWORD_ERROR
	"Please enter another Keyword",	//KEYWORD_ERROR
	"Unable to add another Keyword",	//ADD_KEYWORD_ERROR
	"Please select a Blocked Domain to delete",	//DEL_BLOCK_DOMAIN_ERROR
	"Please select a Permitted Domain to delete",	//DEL_PERMIT_DOMAIN_ERROR
	"The Domain entered already exists in the list of Blocked Domains",	//SAME_BLOCK_DOMAIN
	"Please enter another Blocked Domain",	//BLOCK_DOMAIN_ERROR
	"Unable to add another Blocked Domain",	//ADD_BLOCK_DOMAIN_ERROR
	"The Domain entered already exists in the list of Permitted Domains",	//SAME_PERMIT_DOMAIN
	"Please enter another Permitted Domain",	//PERMIT_DOMAIN_ERROR
	"Unable to add another Permitted Domain",	//ADD_PERMIT_DOMAIN_ERROR
	"Please select a Firmware file to upgrade the router to",	//FIRMWARE_UPGRADE_ERROR
	"Please enter another Domain",	//DOMAIN_ERROR
	"Unable to add another Control Domains",	//ADD_CONTROL_DOMAIN_ERROR
	"Please select a Control Domain to delete",	//DEL_CONTROL_DOMAIN_ERROR
	"Please enter at least one Control Domain",	//CONTROL_DOMAIN_ERROR
	"The RADIUS Server 1 IP Address entered is invalid",	//INVALID_RADIUS_SERVER1_IP
	"The Radius Server 1 IP Address cannot be zero",	//ZERO_RADIUS_SERVER1_IP
	"Radius Server 1 IP Address",	//RADIUS_SERVER1_IP_DESC
	"The RADIUS Server 2 IP Address entered is invalid",	//INVALID_RADIUS_SERVER2_IP
	"The Radius Server 2 IP Address cannot be zero",	//ZERO_RADIUS_SERVER2_IP
	"Radius Server 2 IP Address",	//RADIUS_SERVER2_IP_DESC
	"The IP Address entered is invalid (IP Range: 1~254)",	//INVALID_STATIC_DHCP_IP
	"Please enter another IP Address",	//ZERO_STATIC_DHCP_IP
	"Please enter another Name",	//STATIC_DHCP_NAME
	"The Server IP Address entered is invalid",	//INVALID_SERVER_IP
	"The Server IP Address cannot be zero",	//ZERO_SERVER_IP
	"Server IP Address",	//SERVER_IP_DESC
	"The Passwords entered do not match",	//MATCH_WIZARD_PWD_ERROR
	"The Source Start IP Address entered is invalid",	//INVALID_SOURCE_START_IP
	"The Source Start IP Address cannot be zero",	//ZERO_SOURCE_START_IP
	"Source Start IP Address",	//SOURCE_START_IP_DESC
	"The Source End IP Address entered is invalid",	//INVALID_SOURCE_END_IP
	"The Source End IP Address cannot be zero",	//ZERO_SOURCE_END_IP
	"Source End IP Address",	//SOURCE_END_IP_DESC
	"The Destination Start IP Address entered is invalid",	//INVALID_DEST_START_IP
	"The Destination Start IP Address cannot be zero",	//ZERO_DEST_START_IP
	"Destination Start IP Address",	//DEST_START_IP_DESC
	"The Destination End IP Address entered is invalid",	//INVALID_DEST_END_IP
	"The Destination End IP Address cannot be zero",	//ZERO_DEST_END_IP
	"Destination End IP Address",	//DEST_END_IP_DESC
	"The length of the Passphrase must be between 8 and 64 characters",	//PSK_OVER_LEN
	"Reset JumpStart?",	//RESET_JUMPSTAR
	"Are you sure that you want to delete this rule?",	//DEL_RULE_MSG
	"Are you sure that you want to delete this schedule?",	// DEL_SCHEDULE_MSG
	"Unable to add another schedule",	// ADD_SCHEDULE_ERROR
	"The Start Time entered is invalid",	// START_TIME_ERROR
	"The End Time entered is invalid",	// END_TIME_ERROR,
	"The Start Time cannot be greater than the End Time",	//	TIME_RANGE_ERROR
	"Please select a machine first",	// SELECT_MACHINE_ERROR
	"Please select an Application Name first",	// SELECT_APPLICATION_ERROR
	"Please select a Computer Name first",	// SELECT_COMPUTER_ERROR
	"Please enter another Wireless Security Password",	// SECURITY_PWD_ERROR
	"The URL/Domain entered is already in the list",	//	DUPLICATE_URL_ERROR
	"Login Name error", //LOGIN_NAME_ERROR
	"Login Password error",	//LOGIN_PASS_ERROR
	"Please enter another Public Port number",	//PUBLIC_PORT_ERROR
	"For Rule ", //RULE_NAME_MSG
	"Please enter another Start Port number",	//START_PORT_ERROR
	"Please enter another End Port number",	//END_PORT_ERROR
	"The reserved IP address already exists in the list.",	// RESERVED_IP_ERROR
	"The reserved MAC address already exists in the list.",	// RESERVED_MAC_ERROR
	"The reserved IP address conflicts with Router IP Address.",	// RESERVED_IP_CONFLICT
	"The reserved Name already exists in the list.",	// RESERVED_NAME_ERROR
	"The Retype password does not match the password.",	//MATCH_RETYPE_PWD_ERROR
	"Public Ports conflict in the list.",	//PUBLIC_PORT_CONFLICT
	"Start Ports conflict in the list.",	//START_PORT_CONFLICT
	"End Ports conflict in the list.",	//END_PORT_CONFLICT
	"Please take off 3G card from the device before you upload firmware.", //CARD_CHECK_MSG
	"SMTP Server Address/Name",	//SMTP_SRV_DESC
	"The SMTP Server Address/Name entered is invalid.",	//INVALID_SMTPSRV_IP
	"The SMTP Server Address/Name cannot be zero.",	//ZERO_SMTPSRV_IP
	"Please enter a From Email Address.",	//ZERO_SENDFROM_EMAIL
	"Please enter a To Email Address.",	//ZERO_SENDTO_EMAIL
	"Please enter another Account Name.",	//EMAIL_USER_ERROR
	"Please enter another Account Password.",	//EMAIL_PASS_ERROR
	"Please enable Email Settings first.",	//ENABLE_EMAIL_SETTINGS
	"Please enable Log Type first.",	//ENABLE_LOG_FIRST
	"The Local Domain Name can not empty or contain special characters except minus, dot, and underscore",	// LOCAL_DOMAIN_ERROR
	"End Port must be greater than Start Port",	// PORT_RANGE_ERROR
	"The reserved MAC address conflicts with Router MAC Address.",	// RESERVED_MAC_CONFLICT
	"When the length of Passphrase is 64 characters, the Passphrase must be in HEX mode(0~9, A~F or a~f).",	// PSK_MODE_ERROR
	"Duplicate Application Name in the list.",	// DUPLICATE_NAME_ERROR
	"Duplicate Mac Address in the list.",	// DUPLICATE_MAC_ERROR
	"Please choose your country",	// NON_COUNTRY_ERROR
	"Please choose your ISP",	// NON_ISP_ERROR
	"Please choose your card",	// NON_CARD_ERROR
	"Are you sure that you want to delete this static route?",	// DEL_STATIC_ROUTE	
	"The Network Address entered already exists in the static routing list.",	// SAME_STATIC_ROUTE_ERROR
	"The Schedule Name entered already exists in schedule list.",	// ADD_SCHEDULE_ERROR_SAME_NAME
	"Please enter a correct Email Address.",	//NOT_CORRECT_EMAIL_FORMAT
	"The Schedule Name field cannot be blank",	//SCHEDULE_NAME_EMPTY_ERROR
	"The Schedule Time should between 12:00 am - 11:59 pm"	//SCHEDULE_TIME_RANGE
	);
var INVALID_IP_ADDRESS = 0;
var ZERO_IP_ADDRESS = 1;
var IP_ADDRESS_DESC = 2;
var INVALID_MASK_ADDRESS = 3;
var ZERO_MASK_ADDRESS = 4;
var MASK_ADDRESS_DESC = 5;
var INVALID_GATEWAY_ADDRESS = 6;
var ZERO_GATEWAY_ADDRESS = 7;
var GATEWAY_ADDRESS_DESC = 8;
var NOT_SAME_DOMAIN = 9;
var INVALID_START_IP = 10;
var ZERO_START_IP = 11;
var START_IP_DESC = 12;
var START_INVALID_DOMAIN = 13;
var INVALID_END_IP = 14;
var ZERO_END_IP = 15;
var END_IP_DESC = 16;
var END_INVALID_DOMAIN = 17;
var INVALID_DNS_ADDRESS = 18;
var ZERO_DNS_ADDRESS = 19;
var DNS_ADDRESS_DESC = 20;
var SSID_EMPTY_ERROR = 21;
var AUTH_TYPE_ERROR = 22;
var PSK_LENGTH_ERROR = 23;
var PSK_MATCH_ERROR = 24;
var MATCH_PWD_ERROR = 25;
var WEP_KEY_EMPTY = 26;
var WIZARD_KEY_EMPTY = 27;
var QUIT_WIZARD = 28;
var MAC_ADDRESS_ERROR = 29;
var IP_RANGE_ERROR = 30;
var INVALID_SEC_DNS_ADDRESS = 31;
var ZERO_SEC_DNS_ADDRESS = 32;
var SEC_DNS_ADDRESS_DESC = 33;
var ADMIN_PASS_ERROR = 34;
var USER_PASS_ERROR = 35;
var DDNS_SERVER_ERROR = 36;
var DDNS_HOST_ERROR = 37;
var DDNS_USER_ERROR = 38;
var DDNS_PASS_ERROR = 39;
var RESTORE_DEFAULT = 40;
var REBOOT_ROUTER = 41;
var LOAD_SETTING = 42;
var LOAD_FILE_ERROR = 43;
var DOWNLOAD_SETTING_ERROR = 44;
var PING_IP_ERROR = 45;
var SMTP_SERVER_ERROR = 46;
var EMAIL_ADDRESS_ERROR = 47;
var DEL_SERVER_MSG = 48;
var DEL_APPLICATION_MSG = 49;
var DEL_FILTER_MSG = 50;
var DEL_ROUTE_MSG = 51;
var DEL_MAC_MSG = 52;
var DEL_KEYWORD_MSG = 53;
var DEL_DOMAIN_MSG = 54;
var DEL_ENTRY_MSG = 55;
var DEL_STATIC_DHCP_MSG = 56;
var NAME_ERROR = 57;
var TRIGGER_PORT_ERROR = 58;
var FIREWALL_PORT_ERROR = 59;
var PRIVATE_PORT_ERROR = 60;
var PRIVATE_IP_ERROR = 61;
var PORT_ERROR = 62;
var DEL_KEYWORD_ERROR = 63;
var SAME_KEYWORD_ERROR = 64;
var KEYWORD_ERROR = 65;
var ADD_KEYWORD_ERROR = 66;
var DEL_BLOCK_DOMAIN_ERROR = 67;
var DEL_PERMIT_DOMAIN_ERROR = 68;
var SAME_BLOCK_DOMAIN = 69;
var BLOCK_DOMAIN_ERROR = 70;
var ADD_BLOCK_DOMAIN_ERROR = 71;
var SAME_PERMIT_DOMAIN = 72;
var PERMIT_DOMAIN_ERROR = 73;
var ADD_PERMIT_DOMAIN_ERROR = 74;
var FIRMWARE_UPGRADE_ERROR = 75;
var DOMAIN_ERROR = 76;
var ADD_CONTROL_DOMAIN_ERROR = 77;
var DEL_CONTROL_DOMAIN_ERROR = 78;
var CONTROL_DOMAIN_ERROR = 79;
var INVALID_RADIUS_SERVER1_IP = 80;
var ZERO_RADIUS_SERVER1_IP = 81;
var RADIUS_SERVER1_IP_DESC = 82;
var INVALID_RADIUS_SERVER2_IP = 83;
var ZERO_RADIUS_SERVER2_IP = 84;
var RADIUS_SERVER2_IP_DESC = 85;
var INVALID_STATIC_DHCP_IP = 86;
var ZERO_STATIC_DHCP_IP = 87;
var STATIC_DHCP_NAME = 88;
var INVALID_SERVER_IP = 89;
var ZERO_SERVER_IP = 90;
var SERVER_IP_DESC = 91;
var MATCH_WIZARD_PWD_ERROR = 92;
var INVALID_SOURCE_START_IP = 93;
var ZERO_SOURCE_START_IP = 94;
var SOURCE_START_IP_DESC = 95;
var INVALID_SOURCE_END_IP = 96;
var ZERO_SOURCE_END_IP = 97;
var SOURCE_END_IP_DESC = 98;
var INVALID_DEST_START_IP = 99;
var ZERO_DEST_START_IP = 100;
var DEST_START_IP_DESC = 101;
var INVALID_DEST_END_IP = 102;
var ZERO_DEST_END_IP = 103;
var DEST_END_IP_DESC = 104;
var PSK_OVER_LEN = 105;
var RESET_JUMPSTAR = 106;
var DEL_RULE_MSG = 107;
var DEL_SCHEDULE_MSG = 108;
var ADD_SCHEDULE_ERROR = 109;
var START_TIME_ERROR = 110;
var END_TIME_ERROR = 111;
var TIME_RANGE_ERROR = 112;
var SELECT_MACHINE_ERROR = 113;
var SELECT_APPLICATION_ERROR = 114;
var SELECT_COMPUTER_ERROR = 115;
var SECURITY_PWD_ERROR = 116;
var DUPLICATE_URL_ERROR = 117;
var LOGIN_NAME_ERROR = 118;
var LOGIN_PASS_ERROR = 119;
var PUBLIC_PORT_ERROR = 120;
var RULE_NAME_MSG = 121;
var START_PORT_ERROR = 122;
var END_PORT_ERROR = 123;
var RESERVED_IP_ERROR = 124;
var RESERVED_MAC_ERROR = 125;
var RESERVED_IP_CONFLICT = 126;
var RESERVED_NAME_ERROR = 127;
var MATCH_RETYPE_PWD_ERROR = 128;
var PUBLIC_PORT_CONFLICT = 129;
var START_PORT_CONFLICT = 130;
var END_PORT_CONFLICT = 131;
var CARD_CHECK_MSG = 132;
var SMTP_SRV_DESC = 133;
var INVALID_SMTPSRV_IP = 134;
var ZERO_SMTPSRV_IP = 135;
var ZERO_SENDFROM_EMAIL = 136;
var ZERO_SENDTO_EMAIL = 137;
var EMAIL_USER_ERROR = 138;
var EMAIL_PASS_ERROR = 139;
var ENABLE_EMAIL_SETTINGS = 140;
var ENABLE_LOG_FIRST = 141;
var LOCAL_DOMAIN_ERROR = 142;
var PORT_RANGE_ERROR = 143;
var RESERVED_MAC_CONFLICT = 144;
var PSK_MODE_ERROR = 145;
var DUPLICATE_NAME_ERROR = 146;
var DUPLICATE_MAC_ERROR = 147;
var NON_COUNTRY_ERROR = 148;
var NON_ISP_ERROR = 149;
var NON_CARD_ERROR = 150;
var DEL_STATIC_ROUTE = 151;
var SAME_STATIC_ROUTE_ERROR = 152;
var ADD_SCHEDULE_ERROR_SAME_NAME = 153;
var NOT_CORRECT_EMAIL_FORMAT = 154;
var SCHEDULE_NAME_EMPTY_ERROR = 155;
var SCHEDULE_TIME_RANGE = 156;
function addr_obj(addr, e_msg, is_network){
	this.addr = addr;
	this.e_msg = e_msg;	
	this.is_network = is_network;
}
function ip4_obj(ip, obj_desc){	
	this.ip = ip;	
	this.obj_desc = obj_desc;	
}
function varible_obj(var_value, desc, min, max, is_even){	
	this.var_value = var_value;
	this.desc = desc;
	this.min = min;
	this.max = max;	
	this.is_even = is_even;	
}
function raidus_obj(ip, port, secret){
	this.ip = ip;
	this.port = port;
	this.secret = secret;
}
function check_ip_range(order, my_obj, mask, checking_ip){
	var which_ip = (my_obj.addr[order]).split(" ");
	var temp_ip = which_ip[0];
	var start = 0;
	var end = 255;
	
	if (isNaN(temp_ip) || temp_ip == "" || which_ip.length > 1){	// if the address is not an integer	
	alert(my_obj.e_msg[FIRST_IP_ERROR + order]);
	return false;
	}
	
	if ((temp_ip.charAt(0) == "0") && (temp_ip.length > 1)){	// if the address starts with 0	
	alert(my_obj.e_msg[INVALID_IP]);
	return false;
	}	
	
	if (order == 0){	// the checking range of 1st address
	start = 1;	
	}
	
	if (mask[order] != 255){	
	if (check_ip_range.arguments.length == 4 && checking_ip != null){	
	temp_ip = checking_ip[order];	
	}else{
	temp_ip = which_ip;	
	}
	
	if (parseInt(temp_ip) >= 0 && parseInt(temp_ip) <= 255){	
	end = (~mask[order]+256);	
	start = mask[order] & temp_ip;	
	end += start;
	
	if (end > 255){
	end = 255;
	}
	}	
	}
	
	if (order == 3){
	/* if the 1st, 2nd, 3rd ip address are the network address */
	if (((mask[0] | (~my_obj.addr[0]+256)) == 255) && ((mask[1] | (~my_obj.addr[1]+256)) == 255) && ((mask[2] | (~my_obj.addr[2]+256)) == 255)){
	if (!my_obj.is_network){	// if the address does not support to be the network address
	start += 1;
	}
	}
	
	/* if the 1st, 2nd, 3rd ip address are the broadcast address, the end range must be minus 1 */
	if (((mask[0] | my_obj.addr[0]) == 255) && ((mask[1] | my_obj.addr[1]) == 255) && ((mask[2] | my_obj.addr[2]) == 255)){	
	end -= 1;
	}	
	}
	
	if (parseInt(which_ip) < start || parseInt(which_ip) > end){	
	alert(my_obj.e_msg[FIRST_RANGE_ERROR + order] + " " + start + " and " + end);	
	return false;
	}
	
	return true;
}
function check_address(my_obj, mask_obj, ip_obj){
	var count_bcast = 0;	
	var ip = my_obj.addr;
	var mask;
	
	if (my_obj.addr.length == 4){	
	if (ip[0] == "0" && ip[1] == "0" && ip[2] == "0" && ip[3] == "0"){	// when ip is 0.0.0.0	
	alert(my_obj.e_msg[ZERO_IP]);
	return false;
	}else if ((ip[0] == "127") || (parseInt(ip[0]) >= 224 && parseInt(ip[0]) <= 239)){
	alert(my_obj.e_msg[FIRST_RANGE_LIMIT]);
	return false;
	}else{	// when IP is not 0.0.0.0, checking range. Otherwise no need to check	
	count_zero = 0;
	
	if (check_address.arguments.length > 1 && mask_obj != null){
	mask = mask_obj.addr;
	}else{
	mask = new Array(255,255,255,0);
	}
	
	for(var i = 0; i < ip.length; i++){	
	if (check_address.arguments.length == 3 && ip_obj != null){
	if (!check_ip_range(i, my_obj, mask, ip_obj.addr)){
	return false;
	}
	}else{	
	if (!check_ip_range(i, my_obj, mask)){
	return false;
	}
	}
	}	
	}
	}else{	// if the length of ip is not correct, show invalid ip msg
	alert(my_obj.e_msg[INVALID_IP]);
	return false;
	}
	return true;
}
function check_ip4(which_ip, ip, mask){
	var temp_ip = (which_ip.ip).split(" ");	
	var start = 0;
	var end = 255;
	
	if ((isNaN(temp_ip[0])) || (temp_ip[0] == "") || (temp_ip.length > 1)){	// if the address is not an integer	
	alert("Please enter another " + which_ip.obj_desc);
	return false;
	}
	end = (~mask[3]+256);	
	start = mask[3] & ip[3];	
	end += start;
	if (end > 255){
	end = 255;
	}
	/* if the 1st, 2nd, 3rd ip address are the network address */
	if (((mask[0] | (~ip[0]+256)) == 255) && ((mask[1] | (~ip[1]+256)) == 255) && ((mask[2] | (~ip[2]+256)) == 255)){
	start += 1;
	}
	
	/* if the 1st, 2nd, 3rd ip address are the broadcast address, the end range must be minus 1 */
	if (((mask[0] | ip[0]) == 255) && ((mask[1] | ip[1]) == 255) && ((mask[2] | ip[2]) == 255)){	
	end -= 1;
	}	
	
	if (parseInt(temp_ip[0]) < start || parseInt(temp_ip[0]) > end){
	alert("The " + which_ip.obj_desc + " entered is invalid (IP Range: " + start + "~" + end + ")");
	return false;
	}
	return true;
}
function check_mask(my_mask){
	var temp_mask = my_mask.addr;
	if (temp_mask.length == 4){
	for (var i = 0; i < temp_mask.length; i++){
	var which_ip = temp_mask[i].split(" ");
	var temp_ip = which_ip[0];
	var mask = parseInt(temp_mask[i]);
	var in_range = false;
	var j = 0;
	
	if (isNaN(temp_ip) || temp_ip == "" || which_ip.length > 1){	// if the address is not an integer	
	alert(my_mask.e_msg[FIRST_IP_ERROR + i]);
	return false;
	}
	
	if ((temp_ip.charAt(0) == "0") && (temp_ip.length > 1)){	// if the address starts with 0
	alert(my_mask.e_msg[INVALID_IP]);
	return false;
	}
	if (i == 0){	// when it's 1st address
	j = 1;	// the 1st address can't be 0
	}
	for (; j < subnet_mask.length; j++){
	if (mask == subnet_mask[j]){
	in_range = true;
	break;
	}else{
	in_range = false;
	}
	}
	
	if (i == 3 && parseInt(mask) == 255){	// when the last mask address is 255	
	in_range = false;
	}
	if (!in_range){	
	alert(my_mask.e_msg[FIRST_RANGE_ERROR + i]);
	return false;
	}
	if ((i != 0) && (mask != 0)){ // when not the 1st range and the value is not 0
	if (parseInt(temp_mask[i-1]) != 255){ // check the previous value is 255 or not
	alert(my_mask.e_msg[INVALID_IP]);
	return false;
	}
	}	
	}
	}else{
	alert(my_mask.e_msg[INVALID_IP]);
	return false;
}
	return true;
}
function check_network_address(my_obj, mask_obj){
	var count_zero = 0;
	var ip = my_obj.addr;
	var mask;
	var allow_cast = false;
	if (my_obj.addr.length == 4){
	// check the ip is "0.0.0.0" or not
	for(var i = 0; i < ip.length; i++){
	if (ip[i] == "0"){
	count_zero++;	
	}	
	}
	if (!my_obj.allow_zero && count_zero == 4){	// if the ip is not allowed to be 0.0.0.0
	alert(my_obj.e_msg[ZERO_IP]);	// but we check the ip is 0.0.0.0
	return false;
	}else if (count_zero != 4){	// when IP is not 0.0.0.0, checking range. Otherwise no need to check	
	mask = mask_obj.addr;
	for(var i = 0; i < mask.length; i++){
	if (mask[i] != "255"){
	if (ip[i] != (mask[i] & ip[i])){
	alert(my_obj.e_msg[FIRST_RANGE_ERROR + i] + (mask[i] & ip[i]));
	return false;
	}
	}
	}	
	}
	}else{	// if the length of ip is not correct, show invalid ip msg
	alert(my_obj.e_msg[INVALID_IP]);
	return false;
	}
	return true;
}
function change_filter(which_filter){
 var html_file;
 
 switch(which_filter){
	case 0 :
	html_file = "adv_filters.html";
	break;
	case 1 :
	html_file = "adv_filters_mac.html";
	break;	
	case 2 :
	html_file = "adv_filters_url.html";
	break;
	case 3 :
	html_file = "adv_filters_domain.html";
	break;
	}
	
	location.href = html_file;
}
function change_routing(which_routing){
 var html_file;
 
 switch(which_routing){
 case 0 :
 html_file = "adv_routing.html";
 break;
 case 1 :
 html_file = "adv_routing_dynamic.html";
 break;
 case 2 :
 html_file = "adv_routing_table.html";
 break;
 }
 
 location.href = html_file;
}
function check_hex(data){	
	data = data.toUpperCase();
	
	if (!(data >= 'A' && data <= 'F') && !(data >= '0' && data <= '9')){	
	return false;
	}	
	return true;
}
function check_key(){
	var length = get_length();
	var wep_type = get_by_id("wep_type");
	var wep_def_key = get_by_id("wep_def_key");
	var wep_key_type = get_by_id("wep_key_type");
	if (wep_type.selectedIndex == 1 ){
 for (var i = 1; i < 5; i++){
	var key = get_by_id("key" + i).value;
	
	if (wep_def_key[i-1].selected){
	if (key == ''){
 alert(msg[WEP_KEY_EMPTY]);
	return false;
 }
	}
	if (key != ''){
	if (key.length < length){
	alert("The length of Key" + i + " must be " + length + " characters");
	return false;
	}else{
	if (wep_key_type.value == "0"){	// check the key is hex or not
	for (var j = 0; j < key.length; j++){
	if (!check_hex(key.substring(j, j+1))){
	alert("Key" + i + " is invalid. The legal characters are 0~9, A~F, or a~f");
	return false;
	}
	}
	}
	}
	}
 }
	}
	return true;
}
function check_mac(mac){
 var temp_mac = mac.split("-");
 var error = true;
 
 if (temp_mac.length == 6){
	for (var i = 0; i < 6; i++){ 
	var temp_str = temp_mac[i];
	
	if (temp_str == ""){
	error = false;
	}else{ 
	if (!check_hex(temp_str.substring(0,1)) || !check_hex(temp_str.substring(1))){
	error = false;
	}
	}
	
	if (!error){
	break;
	}
	}
	}else{
	error = false;
	}
 return error;
}
function check_mac2(mac){
 var temp_mac = mac.split(":");
 var error = true;
 
 if (temp_mac.length == 6){
	for (var i = 0; i < 6; i++){ 
	var temp_str = temp_mac[i];
	
	if (temp_str == ""){
	error = false;
	}else{ 
	if (!check_hex(temp_str.substring(0,1)) || !check_hex(temp_str.substring(1))){
	error = false;
	}
	}
	
	if (!error){
	break;
	}
	}
	}else{
	error = false;
	}
 return error;
}
function check_varible(obj){	
	var temp_obj = (obj.var_value).split(" ");
	
	if (temp_obj == ""){
	alert(obj.desc + " cannot be left blank");	
	return false;
	}else if (temp_obj.length > 1){
	alert("Please enter another " + obj.desc + " value");
	return false;
	}else if (isNaN(obj.var_value)){
	alert(obj.desc + " must be a number");
	return false;
	}else if (parseInt(obj.var_value) < obj.min || parseInt(obj.var_value) > obj.max){
	alert("The " + obj.desc + " value range is " + obj.min + " ~ " + obj.max + "");
	return false;
	}else if (obj.is_even && (parseInt(obj.var_value) % 2 != 0)){
	alert(obj.desc + " must be an even number");
	return false;
	}
	return true;
}
function check_integer(which_value, min, max){	
	var temp_obj = (which_value).split(" ");
	
	if (temp_obj == "" || temp_obj.length > 1 || isNaN(which_value)){	
	return false;
	}else if (parseInt(which_value) < min || parseInt(which_value) > max){
	return false;
	}
	
	return true;
}
function check_domain(ip, mask, gateway){
	var temp_ip = ip.addr;
	var temp_mask = mask.addr;
	var temp_gateway = gateway.addr;
	var is_same = true;
	
	if (temp_gateway[0] == 0 && temp_gateway[1] == 0 && temp_gateway[2] == 0
	&& temp_gateway[3] == 0){
	if (gateway.allow_zero){
	return is_same;
	}
	}
	
	for (var i = 0; i < temp_ip.length - 1; i++){
	if ((temp_ip[i] & temp_mask[i]) != (temp_gateway[i] & temp_mask[i])){
	alert(gateway.e_msg3);
	is_same = false;	// when not in the same subnet mask, return false
	break;
	}
	}
	return is_same;
}
/*
function check_lan_setting(ip, mask, gateway){	
	var temp_ip_obj = new addr_obj(ip.split("."), IP_ADDRESS_DESC, false, INVALID_IP_ADDRESS, ZERO_IP_ADDRESS, -1, false);
	var temp_mask_obj = new addr_obj(mask.split("."), MASK_ADDRESS_DESC, false, INVALID_MASK_ADDRESS, ZERO_MASK_ADDRESS, -1, true);
	var temp_gateway_obj = new addr_obj(gateway.split("."), GATEWAY_ADDRESS_DESC, false, INVALID_GATEWAY_ADDRESS, ZERO_GATEWAY_ADDRESS, NOT_SAME_DOMAIN, false);
	
	if (!check_address(temp_ip_obj)){	// when ip is invalid
	return false;
	}else if (!check_address(temp_mask_obj)){	// when subnet mask is invalid
	return false;	
	}else if (!check_mask(temp_mask_obj)){	// when subnet mask is not in the subnet mask range
	return false; 
	}else if (!check_address(temp_gateway_obj)){	// when gateway is invalid
	return false;	
	}else if (!check_domain(temp_ip_obj, temp_mask_obj, temp_gateway_obj)){	// check if the ip and the gateway are in the same subnet mask or not	
	return false;
	}
	return true;
}
*/
function check_pwd(pwd1, pwd2){
	if (get_by_id(pwd1).value != get_by_id(pwd2).value){
	alert(msg[MATCH_PWD_ERROR]);
	return false;
	}
	return true;
}
function check_radius(radius){
	if (!check_address(radius.ip)){
	return false;
	}else if (!check_port(radius.port)){
 alert(radius.ip.e_msg[RADIUS_SERVER_PORT_ERROR]);
 return false;
 }else if (radius.secret == ""){
 alert(radius.ip.e_msg[RADIUS_SERVER_SECRET_ERROR]);
 return false; 
	}
	
	return true;
}
function check_ssid(id){
	if (get_by_id(id).value == ""){
	alert(msg[SSID_EMPTY_ERROR]);
	return false;
	}
	return true; 
}
function check_port(port){
 var temp_port = port.split(" ");
 
 if (isNaN(port) || port == "" || temp_port.length > 1 
 || (parseInt(port) < 1 || parseInt(port) > 65534)){
 return false;
 }
 return true;
}
function check_pf_port(port){
 var temp_port = port.split(" ");
 
 if (isNaN(port) || port == "" || temp_port.length > 1 
 || (parseInt(port) < 0 || parseInt(port) > 65535)){
 return false;
 }
 return true;
}
function change_color(table_name, row){
 var obj = get_by_id(table_name);
 for (var i = 1; i < obj.rows.length; i++){
 if (row == i){
 obj.rows[i].style.backgroundColor = "#FFFF00";
 }else{
 obj.rows[i].style.backgroundColor = "#FFFFFF";
 }
 } 
}
function enable_alive_server(){
	var auto_recon = get_by_name("wwan_auto_recon");
	
	get_by_id("ar_interval").disabled = auto_recon[1].checked;
	get_by_id("ar_server1").disabled = auto_recon[1].checked;
	get_by_id("ar_server2").disabled = auto_recon[1].checked;
}
function exit_wizard(){
 if (confirm(msg[QUIT_WIZARD])){
 window.close();
 }
}
function get_by_id(id){
	with(document){
	return getElementById(id);
	}
}
function get_by_name(name){
	with(document){
	return getElementsByName(name);
	}
}
function get_length(){
 var wep_key_len = get_by_id("wep_key_len");
 var wep_key_type = get_by_id("wep_key_type");
	var length;
	
	switch(wep_key_len.selectedIndex){ //0:64bits, 1:128bits
	case 0:
	length = 5;
	break;
	case 1:
	length = 13;
	break;
	default :
	length = 5;
	break;
	}
	
	if (wep_key_type.value == 0){
	length *= 2;
	}
	return length;
}
function openwin(url,w,h) {
	var winleft = (screen.width - w) / 2;
	var wintop = (screen.height - h) / 2;
	window.open(url,"popup",'width='+w+',height='+h+',top='+wintop+',left='+winleft+',scrollbars=yes,status=no,location=no,resizable=yes')
}	
function send_submit(which_form){
	get_by_id(which_form).submit();
}
function set_server(is_enable){
	var enable = get_by_id("enable");
	
 if (is_enable == "1"){
 enable[0].checked = true;
 }else{
 enable[1].checked = true;
 }
}
function set_protocol(which_value, obj){
 for (var i = 0; i < 3; i++){ 
 if (which_value == obj.options[i].value){
 obj.selectedIndex = i;
 break;
 }
 }
}
function set_schedule(data, index){ 
	var schd = get_by_name("schd"); 
	
 if (data[index] == "0"){
 schd[0].checked = true; 
 }else{
 schd[1].checked = true; 
 }
 
 get_by_id("hour1").selectedIndex = data[index+1];
 get_by_id("min1").selectedIndex = data[index+2];
 get_by_id("am1").selectedIndex = data[index+3];
 get_by_id("hour2").selectedIndex = data[index+4];
 get_by_id("min2").selectedIndex = data[index+5];
 get_by_id("am2").selectedIndex = data[index+6];
 get_by_id("day1").selectedIndex = data[index+7];
 get_by_id("day2").selectedIndex = data[index+8];
}
function set_mac(mac){
	var temp_mac = mac.split(":");
	for (var i = 0; i < 6; i++){
	var obj = get_by_id("mac" + (i+1));
	obj.value = temp_mac[i];
	}
}
function set_time_zone(){
	var index = get_by_id("zone_index").value;
	get_by_id("tzone").selectedIndex = parseInt(index) - 1;
}
function show_dns(type){
 if (type){
 get_by_id("dns1").value = "0.0.0.0";
 get_by_id("dns2").value = "0.0.0.0";
 }
}
function show_wizard(name){
	window.open(name,"Wizard","width=450,height=370");
}
function show_window(name){
	window.open(name,"Window","width=500,height=600,scrollbar=yes");
}
function get_row_data(obj, index){	
	
 try {
 return obj.cells[index].childNodes[0].data;
 }catch(e) {
 return ("");
 }
}
function copy_ip(index){
	if (get_by_id("ip_list" + index).selectedIndex > 0){
	get_by_id("ip" + index).value = get_by_id("ip_list" + index).options[get_by_id("ip_list" + index).selectedIndex].value;
	}else{
	alert(msg[SELECT_COMPUTER_ERROR]);
	}
}
function get_random_char(){
	var number_list = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
	var number = Math.round(Math.random()*62);
	
	return(number_list.substring(number, number + 1));
}
function generate_psk(key){
	var i = key.length;
	
	if (key.length < 8){
	for (; i < 8; i++){
	key += get_random_char();
	}
	}
	return key;
}
function create_wep_key128(passphrase, pharse_len){
 var pseed2 = "";
 var md5_str = "";
 var count;
 for(var i = 0; i < 64; i++){
 count = i % pharse_len;
 pseed2 += passphrase.substring(count, count+1);
 }
 
 md5_str = calcMD5(pseed2); 
 
 return md5_str.substring(0, 26).toUpperCase(); 
}
function check_domain_name(which_domain){
	var pass = true;
	which_domain = which_domain.toUpperCase();
	
	if(which_domain.length == 0){
	pass = false;
	}
	
	for (var i = 0; i < which_domain.length; i++){	// check the domain name are in 0 ~ 9, A ~ Z, a ~ z, -, ., _
	var temp_char = which_domain.charCodeAt(i);	// if not in the range, return error
	
	if ((temp_char >= 48 && temp_char <= 57) || (temp_char >= 65 && temp_char <= 90) || (temp_char == 45) || (temp_char == 46) || (temp_char == 95)){
	continue;
	}else{
	pass = false;
	break;
	}
	}
	
	return pass;
}
var ip_addr_msg = new Array("The IP Address entered is invalid.",	// INVALID_IP
	"The IP Address cannot be zero.",	// ZERO_IP
	"The 1st octet of IP Address must be a number.",	// FIRST_IP_ERROR
	"The 2nd octet of IP Address must be a number.",	// SECOND_IP_ERROR
	"The 3rd octet of IP Address must be a number.",	// THIRD_IP_ERROR
	"The 4th octet of IP Address must be a number.",	// FOURTH_IP_ERROR
	"The 1st octet of IP Address must be a number between",	// FIRST_RANGE_ERROR
	"The 2nd octet of IP Address must be a number between",	// SECOND_RANGE_ERROR
	"The 3rd octet of IP Address must be a number between",	// THIRD_RANGE_ERROR
	"The 4th octet of IP Address must be a number between",	// FOURTH_RANGE_ERROR	
	"The 1st octet of IP Address can not be 127, 224 ~ 239."	// FIRST_RANGE_LIMIT
	);
var pdns_addr_msg = new Array("The Primary DNS Address entered is invalid.",	// INVALID_IP
	"The Primary DNS Address cannot be zero.",	// ZERO_IP
	"The 1st octet of Primary DNS Address must be a number.",	// FIRST_IP_ERROR
	"The 2nd octet of Primary DNS Address must be a number.",	// SECOND_IP_ERROR
	"The 3rd octet of Primary DNS Address must be a number.",	// THIRD_IP_ERROR
	"The 4th octet of Primary DNS Address must be a number.",	// FOURTH_IP_ERROR
	"The 1st octet of Primary DNS Address must be a number between",	// FIRST_RANGE_ERROR
	"The 2nd octet of Primary DNS Address must be a number between",	// SECOND_RANGE_ERROR
	"The 3rd octet of Primary DNS Address must be a number between",	// THIRD_RANGE_ERROR
	"The 4th octet of Primary DNS Address must be a number between",	// FOURTH_RANGE_ERROR	
	"The 1st octet of Primary DNS Address can not be 127, 224 ~ 239."	// FIRST_RANGE_LIMIT
	);
	
var sdns_addr_msg = new Array("The Secondary DNS Address entered is invalid.",	// INVALID_IP
	"The Secondary DNS Address cannot be zero.",	// ZERO_IP
	"The 1st octet of Secondary DNS Address must be a number.",	// FIRST_IP_ERROR
	"The 2nd octet of Secondary DNS Address must be a number.",	// SECOND_IP_ERROR
	"The 3rd octet of Secondary DNS Address must be a number.",	// THIRD_IP_ERROR
	"The 4th octet of Secondary DNS Address must be a number.",	// FOURTH_IP_ERROR
	"The 1st octet of Secondary DNS Address must be a number between",	// FIRST_RANGE_ERROR
	"The 2nd octet of Secondary DNS Address must be a number between",	// SECOND_RANGE_ERROR
	"The 3rd octet of Secondary DNS Address must be a number between",	// THIRD_RANGE_ERROR
	"The 4th octet of Secondary DNS Address must be a number between",	// FOURTH_RANGE_ERROR	
	"The 1st octet of Secondary DNS Address can not be 127, 224 ~ 239."	// FIRST_RANGE_LIMIT
	);
	
var subnet_mask_msg = new Array("The Subnet Mask entered is invalid.",	// INVALID_IP
	"The Subnet Mask cannot be zero.",	// ZERO_IP
	"The 1st octet of Subnet Mask must be a number.",	// FIRST_IP_ERROR
	"The 2nd octet of Subnet Mask must be a number.",	// SECOND_IP_ERROR
	"The 3rd octet of Subnet Mask must be a number.",	// THIRD_IP_ERROR
	"The 4th octet of Subnet Mask must be a number.",	// FOURTH_IP_ERROR
	"The 1st octet of Subnet Mask must be 128, 192, 224, 240, 248, 252, 254, 255.",	// FIRST_RANGE_ERROR
	"The 2nd octet of Subnet Mask must be 0, 128, 192, 224, 240, 248, 252, 254, 255.",	// SECOND_RANGE_ERROR
	"The 3rd octet of Subnet Mask must be 0, 128, 192, 224, 240, 248, 252, 254, 255.",	// THIRD_RANGE_ERROR
	"The 4th octet of Subnet Mask must be 0, 128, 192, 224, 240, 248, 252, 254."	// FOURTH_RANGE_ERROR
	);
	
var server_ip_msg = new Array("The Server IP Address entered is invalid.",	// INVALID_IP
	"The Server IP Address cannot be zero.",	// ZERO_IP
	"The 1st octet of Server IP Address must be a number.",	// FIRST_IP_ERROR
	"The 2nd octet of Server IP Address must be a number.",	// SECOND_IP_ERROR
	"The 3rd octet of Server IP Address must be a number.",	// THIRD_IP_ERROR
	"The 4th octet of Server IP Address must be a number.",	// FOURTH_IP_ERROR
	"The 1st octet of Server IP Address must be a number between",	// FIRST_RANGE_ERROR
	"The 2nd octet of Server IP Address must be a number between",	// SECOND_RANGE_ERROR
	"The 3rd octet of Server IP Address must be a number between",	// THIRD_RANGE_ERROR
	"The 4th octet of Server IP Address must be a number between",	// FOURTH_RANGE_ERROR	
	"The 1st octet of Server IP Address can not be 127, 224 ~ 239."	// FIRST_RANGE_LIMIT
	);	
	
var radius1_msg = new Array("The RADIUS Server 1 IP Address entered is invalid.",	// INVALID_IP
	"The RADIUS Server 1 IP Address cannot be zero.",	// ZERO_IP
	"The 1st octet of RADIUS Server 1 IP Address must be a number.",	// FIRST_IP_ERROR
	"The 2nd octet of RADIUS Server 1 IP Address must be a number.",	// SECOND_IP_ERROR
	"The 3rd octet of RADIUS Server 1 IP Address must be a number.",	// THIRD_IP_ERROR
	"The 4th octet of RADIUS Server 1 IP Address must be a number.",	// FOURTH_IP_ERROR
	"The 1st octet of RADIUS Server 1 IP Address must be a number between",	// FIRST_RANGE_ERROR
	"The 2nd octet of RADIUS Server 1 IP Address must be a number between",	// SECOND_RANGE_ERROR
	"The 3rd octet of RADIUS Server 1 IP Address must be a number between",	// THIRD_RANGE_ERROR
	"The 4th octet of RADIUS Server 1 IP Address must be a number between",	// FOURTH_RANGE_ERROR	
	"The 1st octet of RADIUS Server 1 IP Address can not be 127, 224 ~ 239.",	// FIRST_RANGE_LIMIT
	"The RADIUS Server 1 Port entered is invalid",	// RADIUS_SERVER_PORT_ERROR
	"The RADIUS Server 1 Shared Secret cannot be zero"	// RADIUS_SERVER_SECRET_ERROR
	);	
	
var radius2_msg = new Array("The RADIUS Server 2 IP Address entered is invalid.",	// INVALID_IP
	"The RADIUS Server 2 IP Address cannot be zero.",	// ZERO_IP
	"The 1st octet of RADIUS Server 2 IP Address must be a number.",	// FIRST_IP_ERROR
	"The 2nd octet of RADIUS Server 2 IP Address must be a number.",	// SECOND_IP_ERROR
	"The 3rd octet of RADIUS Server 2 IP Address must be a number.",	// THIRD_IP_ERROR
	"The 4th octet of RADIUS Server 2 IP Address must be a number.",	// FOURTH_IP_ERROR
	"The 1st octet of RADIUS Server 2 IP Address must be a number between",	// FIRST_RANGE_ERROR
	"The 2nd octet of RADIUS Server 2 IP Address must be a number between",	// SECOND_RANGE_ERROR
	"The 3rd octet of RADIUS Server 2 IP Address must be a number between",	// THIRD_RANGE_ERROR
	"The 4th octet of RADIUS Server 2 IP Address must be a number between",	// FOURTH_RANGE_ERROR	
	"The 1st octet of RADIUS Server 2 IP Address can not be 127, 224 ~ 239.",	// FIRST_RANGE_LIMIT
	"The RADIUS Server 2 Port entered is invalid",	// RADIUS_SERVER_PORT_ERROR
	"The RADIUS Server 2 Shared Secret cannot be zero"	// RADIUS_SERVER_SECRET_ERROR
	);	
var network_addr_msg = new Array("The Network Address is an invalid address.",	// INVALID_IP
	"The Network Address can not be zero.",	// ZERO_IP
	"The 1st address of Network Address must be an integer.",	// FIRST_IP_ERROR
	"The 2nd address of Network Address must be an integer.",	// SECOND_IP_ERROR
	"The 3rd address of Network Address must be an integer.",	// THIRD_IP_ERROR
	"The 4th address of Network Address must be an integer.",	// FOURTH_IP_ERROR
	"The 1st range of Network Address must be ",	// FIRST_RANGE_ERROR
	"The 2nd range of Network Address must be ",	// SECOND_RANGE_ERROR
	"The 3rd range of Network Address must be ",	// THIRD_RANGE_ERROR
	"The 4th range of Network Address must be "	// FOURTH_RANGE_ERROR
	);
var network_mask_msg = new Array("The Network Mask is an invalid address.",	// INVALID_IP
	"The Network Mask can not be zero.",	// ZERO_IP
	"The 1st address of Network Mask must be an integer.",	// FIRST_IP_ERROR
	"The 2nd address of Network Mask must be an integer.",	// SECOND_IP_ERROR
	"The 3rd address of Network Mask must be an integer.",	// THIRD_IP_ERROR
	"The 4th address of Network Mask must be an integer.",	// FOURTH_IP_ERROR
	"The 1st range of Network Mask must be 128, 192, 224, 240, 248, 252, 254, 255.",	// FIRST_RANGE_ERROR
	"The 2nd range of Network Mask must be 0, 128, 192, 224, 240, 248, 252, 254, 255.",	// SECOND_RANGE_ERROR
	"The 3rd range of Network Mask must be 0, 128, 192, 224, 240, 248, 252, 254, 255.",	// THIRD_RANGE_ERROR
	"The 4th range of Network Mask must be 0, 128, 192, 224, 240, 248, 252, 254."	// FOURTH_RANGE_ERROR
	);
var gateway_msg = new Array("The Gateway address is an invalid address.",	// INVALID_IP
	"The Gateway address can not be zero.",	// ZERO_IP
	"The 1st address of Gateway address must be an integer.",	// FIRST_IP_ERROR
	"The 2nd address of Gateway address must be an integer.",	// SECOND_IP_ERROR
	"The 3rd address of Gateway address must be an integer.",	// THIRD_IP_ERROR
	"The 4th address of Gateway address must be an integer.",	// FOURTH_IP_ERROR
	"The 1st range of Gateway address must be between",	// FIRST_RANGE_ERROR
	"The 2nd range of Gateway address must be between",	// SECOND_RANGE_ERROR
	"The 3rd range of Gateway address must be between",	// THIRD_RANGE_ERROR
	"The 4th range of Gateway address must be between"	// FOURTH_RANGE_ERROR	
	);	
var INVALID_IP = 0;
var ZERO_IP = 1;
var FIRST_IP_ERROR = 2;
var SECOND_IP_ERROR = 3;
var THIRD_IP_ERROR = 4;
var FOURTH_IP_ERROR = 5;
var FIRST_RANGE_ERROR = 6;
var SECOND_RANGE_ERROR = 7;
var THIRD_RANGE_ERROR = 8;
var FOURTH_RANGE_ERROR = 9;
var FIRST_RANGE_LIMIT = 10;
var RADIUS_SERVER_PORT_ERROR = 11;	// for radius server
var RADIUS_SERVER_SECRET_ERROR = 12; // for radius server
function disable_all_items(){
	var input_objs = document.getElementsByTagName("input");
	var select_objs = document.getElementsByTagName("select");
	if (input_objs != null){
	for (var i = 0; i < input_objs.length; i++){
	input_objs[i].disabled = true;
	}
	}
	if (select_objs != null){
	for (var i = 0; i < select_objs.length; i++){
	select_objs[i].disabled = true;
	}
	}
}
function load_xml(which_one){
	var my_doc;
	
	if (window.ActiveXObject){	// code for IE
	my_doc = new ActiveXObject("Microsoft.XMLDOM");
	}else if (document.implementation && document.implementation.createDocument){	// code for Mozilla, Firefox, Opera, etc.
	my_doc = document.implementation.createDocument("","",null);
	}else{
	alert('Your browser cannot handle this script');
	}
	
	my_doc.async = false;
	
	return my_doc;
}
function get_xml_node(which_doc, which_id){
	return which_doc.getElementsByTagName(which_id)[0];
}
function get_node_value(which_node, index){
	var count_node = 0;
	
	if (which_node.childNodes.length >= index){
	for (var i = 0; i < which_node.childNodes.length; i++){
	var node = which_node.childNodes[i];
	
	if (node.nodeType != 3){ // NS6/Mozilla will treat space as an element, so we need to ingore it	
	if (count_node == index){
	return node.firstChild.nodeValue;
	}else{
	count_node++;
	}
	}
	}
	}
	
	return "";
}
function ISP_OBJ(){
	this.isp_name = null;
	this.network_number = new Array();
	this.dial_number = null;
	this.apn = null;
	this.user_name = null;
	this.password = null;
	this.support_cards = new Array();	
}
/**
 * ISP_OBJ.prototype - Prototype of ISP_OBJ Object 
 *
 * Data members:
 *
 * Methods:
 * set_isp_name()	: set isp name
 * get_isp_name() : get isp name
 * set_network_number()	: set network number
 * get_network_number() : get network number
 * set_dial_number()	: set dial number
 * get_dial_number() : get dial number
 * set_apn()	: set dial number
 * get_apn() : get apn
 *	set_user_name()	: set user name
 *	get_user_name()	:	get user name	
 *	set_password()	:	set password
 *	get_password()	:	get password
 *	set_support_cards()	:	set support cards
 *	get_support_cards()	:	get support cards
 *
 **/
ISP_OBJ.prototype = {
set_isp_name: function(isp_name){	
	this.isp_name = isp_name;
},
get_isp_name: function(){
	return this.isp_name;
},
set_network_number: function(network_number){	
	this.network_number = network_number;
},
get_network_number: function(){	
	return this.network_number;
},
set_dial_number: function(dial_number){	
	this.dial_number = dial_number;
},
get_dial_number: function(){
	return this.dial_number;
},
set_apn: function(apn){	
	this.apn = apn;
},
get_apn: function(){
	return this.apn;
},
set_user_name: function(user_name){	
	this.user_name = user_name;
},
get_user_name: function(){
	return this.user_name;
},
set_password: function(password){	
	this.password = password;
},
get_password: function(){
	return this.password;
},
set_support_cards: function(support_cards){	
	this.support_cards = support_cards;
},
get_support_cards: function(){	
	return this.support_cards;
}
}
function WAN_OBJ(){
	this.country = null;
	this.country_provider = null;
	this.isp = new Array();
}
/**
 * WAN_OBJ.prototype - Prototype of WAN_OBJ Object 
 *
 * Data members:
 *
 * Methods:
 * set_country()	: set country
 * get_country() : get country
 * set_country_provider()	: set country provider
 * get_country_provider() : get country provider
 * set_isp()	: set isp info
 * get_isp() : get isp info
 *
 **/
WAN_OBJ.prototype = {
set_country: function(country){	
	this.country = country;
},
get_country: function(){
	return this.country;
},
set_country_provider: function(country_provider){	
	this.country_provider = country_provider;
},
get_country_provider: function(){
	return this.country_provider;
},
set_isp: function(isp){	
	this.isp = isp;
},
get_isp: function(){	
	return this.isp;	
}
}
function CARD_OBJ(){
	this.card_name = null;
	this.card_code = null;
}
/**
 * CARD_OBJ.prototype - Prototype of WAN_OBJ Object 
 *
 * Data members:
 *
 * Methods:
 * set_card_name()	: set card name
 * get_card_name() : get card name
 * set_card_code()	: set card code
 * get_card_code() : get card code 
 *
 **/
CARD_OBJ.prototype = {
set_card_name: function(card_name){	
	this.card_name = card_name;
},
get_card_name: function(){
	return this.card_name;
},
set_card_code: function(card_code){	
	this.card_code = card_code;
},
get_card_code: function(){
	return this.card_code;
}
}
function set_card_info(card_root){
	var card_array = new Array();
	
	for (var i = 0; i < card_root.childNodes.length; i++){
	var card = card_root.childNodes[i];
	var card_obj = new CARD_OBJ();
	
	if (card.nodeType != 3){ // NS6/Mozilla will treat space as an element, so we need to ingore it
	card_obj.set_card_name(card.firstChild.nodeValue);
	card_obj.set_card_code(card.getAttribute("code"));
	card_array.push(card_obj);	
	}
	
	}
	
	return card_array;
}
	
function set_isp_network(network_root){
	var network_array = new Array();
	
	for (var i = 0; i < network_root.childNodes.length; i++){
	var network = network_root.childNodes[i];
	
	if (network.nodeType != 3){ // NS6/Mozilla will treat space as an element, so we need to ingore it
	network_array.push(network.firstChild.nodeValue);	
	}
	}
	
	return network_array;
}
	
function set_isp_info(isp_root){
	var isp_array = new Array();
	var count;
	
	for (var i = 0; i < isp_root.childNodes.length; i++){
	var isp = isp_root.childNodes[i];	// get "isp" element
	var temp_isp_obj = new ISP_OBJ();
	
	if (isp.nodeType != 3){ // NS6/Mozilla will treat space as an element, so we need to ingore it	
	count = 0;
	
	for (var j = 0; j < isp.childNodes.length; j++){
	var temp_node = isp.childNodes[j];
	var temp_value;
	
	if (temp_node.nodeType != 3){ // NS6/Mozilla will treat space as an element, so we need to ingore it	
	
	if (temp_node.firstChild == null){
	temp_value = "";
	}else{
	temp_value = temp_node.firstChild.nodeValue;
	}
	
	switch(count){
	case 0 :	// set isp name	
	temp_isp_obj.set_isp_name(temp_value);	
	break;
	case 1 :	// set network number
	temp_isp_obj.set_network_number(set_isp_network(temp_node));
	break;
	case 2 :	// set dial number
	temp_isp_obj.set_dial_number(temp_value);
	break;
	case 3 :	// set apn	
	temp_isp_obj.set_apn(temp_value);
	break;
	case 4 :	// set user name
	temp_isp_obj.set_user_name(temp_value);
	break;
	case 5 :	// set password
	temp_isp_obj.set_password(temp_value);
	break;
	case 6 :	// set support cards
	temp_isp_obj.set_support_cards(set_card_info(temp_node));
	break;
	}
	count++;
	}
	}
	
	isp_array.push(temp_isp_obj);
	}
	}
	
	return isp_array;
}
	
function set_wan_object(){
	var root = get_xml_node(my_xml, "root");
	var country = get_by_id("country");
	var count;
	
	for (var i = 0; i < root.childNodes.length; i++){
	var node = root.childNodes[i];	// get "wan_info" element
	var temp_wan_obj = new WAN_OBJ();
	
	if (node.nodeType != 3){	// NS6/Mozilla will treat space as an element, so we need to ingore it	
	if (node.childNodes.length > 1){
	count = 0;
	
	for (var j = 0; j < node.childNodes.length; j++){
	var temp_node = node.childNodes[j];	
	
	if (temp_node.nodeType != 3){	// NS6/Mozilla will treat space as an element, so we need to ingore it	
	if (temp_node != null){	
	switch(count){
	case 0 :	// set country
	temp_wan_obj.set_country(temp_node.firstChild.nodeValue);	
	break;
	case 1 :	// set country provider
	temp_wan_obj.set_country_provider(temp_node.firstChild.nodeValue);	
	break;
	case 2 :	// set isp
	temp_wan_obj.set_isp(set_isp_info(temp_node));
	break;
	}	
	
	count++;	
	}
	}
	}	
	
	wan_info.push(temp_wan_obj);	
	}
	}
	}
}
function show_country_list(){
	var country = get_by_id("country");
	
	for (var i = 0; i < wan_info.length; i++){
	var my_obj = wan_info[i];	
	country.options[i+1] = new Option(my_obj.get_country(), my_obj.get_country_provider());	
	}
}
function show_isp_list(){
	var country = get_by_id("country");
	var isp = get_by_id("isp");
	var support_cards = get_by_id("support_cards");
	var index = country.selectedIndex;
	var my_wan;
	var my_isp;
	
	if (index > 0){
	my_wan = wan_info[index - 1];
	my_isp = my_wan.get_isp();
	isp.options.length = 1;
	isp.selectedIndex = 0;
	support_cards.options.length = 1;
	support_cards.selectedIndex = 0;
	
	for (var i = 0; i < my_isp.length; i++){
	var obj = my_isp[i];
	var isp_name = obj.get_isp_name();
	
	isp.options[i+1] = new Option(isp_name, isp_name);
	}	
	}	
}	
function show_card_list(){
	var country = get_by_id("country").selectedIndex;
	var isp = get_by_id("isp").selectedIndex;
	var support_cards = get_by_id("support_cards");	
	var my_wan;
	var my_isp;
	var my_card;
	
	if (country > 0 && isp > 0){
	my_wan = wan_info[country - 1];
	my_isp = (my_wan.get_isp())[isp - 1];
	my_card = my_isp.get_support_cards();
	
	support_cards.options.length = 1;
	support_cards.selectedIndex = 0;	
	for (var i = 0; i < my_card.length; i++){
	var obj = my_card[i];
	support_cards.options[i+1] = new Option(obj.get_card_name(), obj.get_card_code());
	}
	}
}
/**
 *	check_ipv4_or_domain(which_data) : check is IPv4 formar or Domain
 *
 *	Parameter(s) :
 *	which_data : which data (ex:10.0.0.1 or www.domain.com)
 *
 * Return :true or false
 * 
 **/
function check_ipv4_or_domain(which_data){
	var tmp_which_data = new addr_obj(which_data.split("."), ip_addr_msg, false);
	var temp_which_ip = which_data.split(".");
	if(temp_which_ip.length < 2){
	return false;
	}
	if(temp_which_ip.length == 4 && !isNaN(temp_which_ip[0]) && !isNaN(temp_which_ip[1]) && !isNaN(temp_which_ip[2]) && !isNaN(temp_which_ip[3]) ){	// use ipv4 format
	if(!(isNaN(temp_which_ip)[0] || isNaN(temp_which_ip)[1] || isNaN(temp_which_ip)[2] || isNaN(temp_which_ip)[3])){	
 if(!check_address(tmp_which_data)){
 return false;
 }	
	}
	}else if( !isNaN(temp_which_ip[temp_which_ip.length - 1]) ){	//check the last value not number
	return false;
	}
	if(!check_domain_name(which_data)){	
	return false;
	}
	return true ;
}
/**
 *	check_mail_addr(which_data) : check mail address format
 *
 *	Parameter(s) :
 *	which_data : which data (ex:user@10.0.0.1 or user@mail.domain.com)
 *
 * Return :true or false
 * 
 **/
function check_mail_addr(which_data){
	var tmp_which_data = which_data.split("@");
	var tmp_domain_data ;
	var tmp_user_account ;
	if(tmp_which_data.length < 2){
	return false ;
	}else{
	tmp_user_account = tmp_which_data[0];	// before first '@'
	tmp_domain_data = tmp_which_data[tmp_which_data.length-1];	//after last '@'
	if(tmp_user_account.length == 0 ){
	return false ;
	}
	if(tmp_domain_data.length == 0 ){
	return false ;
	}
	if(!check_ipv4_or_domain(tmp_domain_data)){
	return false ;
	}
	}
	return true ;
}
