var subnet_mask = new Array(0, 128, 192, 224, 240, 248, 252, 254, 255);
function addr_obj(addr, e_msg, allow_zero, is_network){	
	this.addr = addr;
	this.e_msg = e_msg;
	this.allow_zero = allow_zero;	
	this.is_network = is_network;
}
function varible_obj(var_value, e_msg, min, max, is_even){	
	this.var_value = var_value;
	this.e_msg = e_msg;
	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 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 = "#F0F0F0";
 }
 } 
}
function change_wan_html(which_wan){
 var html_file;
 
 switch(which_wan){
	case 0 :
	html_file = "wan_dhcp.htm";
	break;
	case 1 :	
	html_file = "wan_poe.htm";
	break;
	case 2 :
	html_file = "wan_pptp.htm";
	break;
	case 3 :
	html_file = "wan_l2tp.htm";
	break;
	case 4 :
	html_file = "wan_bigpond.htm";
	break;
	}
	
	location.href = html_file;
}
function check_address(my_obj){	
	var count_zero = 0;
	var ip = my_obj.addr;
	
	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){
	for(var i = 0; i < ip.length; i++){
	if (!check_ip_range(i, my_obj)){
	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_domain(ip, mask, gateway){
	var temp_ip = ip.addr;
	var temp_mask = mask.addr;
	var temp_gateway = gateway.addr;
	
	for (var i = 0; i < temp_ip.length - 1; i++){
	if ((temp_ip[i] & temp_mask[i]) != (temp_gateway[i] & temp_mask[i])){	
	return false;	// when not in the same subnet mask, return false	
	}
	}
	return true;
}
function check_ip_order(start_ip, end_ip){
	var temp_start_ip = start_ip.addr;
	var temp_end_ip = end_ip.addr;
	
	
	for (var i = 0; i < temp_start_ip.length; i++){
	if (parseInt(temp_start_ip[i]) > parseInt(temp_end_ip[i])){
	return false;
	}
	}
	return true;
}
function check_ip_range(order, my_obj){	
	var which_ip = (my_obj.addr[order]).split(" ");	
	var start, end;
	
	if (isNaN(which_ip) || which_ip == "" || which_ip.length > 1 || (which_ip[0].length > 1 && which_ip[0].substring(0,1) == "0")){	// if the address is invalid
	alert(my_obj.e_msg[FIRST_IP_ERROR + order]);
	return false;
	}
	
	start = 0;	// the checking range will be 0 ~ 255
	end = 255;
	
	if ((order == 0) || (order == 3 && !my_obj.is_network)){	// if the 1st address or the 4th address is not a network address	
	start = 1;	// the checking range will be 1 ~ 254	
	end = 254;	
	}
	
	if (parseInt(which_ip) < start || parseInt(which_ip) > end){
	alert(my_obj.e_msg[FIRST_RANGE_ERROR + order]);
	return false;
	}
	
	return true;
}
function check_hex(data){	
	data = data.toUpperCase();
	
	if (!(data >= 'A' && data <= 'F') && !(data >= '0' && data <= '9')){	
	return false;
	}	
	return true;
}
function check_lan_setting(ip, mask, gateway){	
	
	if (!check_address(ip)){
	return false;	// when ip is invalid
	}else if (!check_mask(mask)){
	return false; // when subnet mask is not in the subnet mask range
	}else if (!check_address(gateway)){
	return false;	// when gateway is invalid
	}else if (!check_domain(ip, mask, gateway)){	// check if the ip and the gateway are in the same subnet mask or not	
	alert(msg[MSG7]);
	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_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 mask = parseInt(temp_mask[i]);
	var in_range = false;
	var j = 0;
	
	if (isNaN(which_ip) || which_ip == "" || which_ip.length > 1 || (which_ip[0].length > 1 && which_ip[0].substring(0,1) == "0")){	// if the address is invalid
	alert(my_mask.e_msg[FIRST_IP_ERROR + i]);
	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 (!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;
	}
	}	
	
	if (i == 3 && parseInt(mask) == 255){	// when the last mask address is 255
	alert(my_mask.e_msg[INVALID_IP]);
	return false;
	}	
	}
	}else{
	alert(my_mask.e_msg[INVALID_IP]);
	return false;
	}
	
	return true;
}
function check_pwd(pwd1, pwd2){
	if (get_by_id(pwd1).value != get_by_id(pwd2).value){
	alert(msg[MSG1]);
	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_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[MSG6]);
	return false;
	}
	return true; 
}
function check_varible(obj){
	var temp_obj = (obj.var_value).split(" ");
	
	if (temp_obj == "" || temp_obj.length > 1){	
	alert(obj.e_msg[EMPTY_VARIBLE_ERROR]);
	return false;
	}else if (isNaN(obj.var_value)){
	alert(obj.e_msg[INVALID_VARIBLE_ERROR]);
	return false;
	}else if (parseInt(obj.var_value) < obj.min || parseInt(obj.var_value) > obj.max){
	alert(obj.e_msg[VARIBLE_RANGE_ERROR]);
	return false;
	}else if (obj.is_even && (parseInt(obj.var_value) % 2 != 0)){
	alert(obj.e_msg[EVEN_NUMBER_ERROR]);
	return false;
	}
	return true;
}
function check_wep_key(){ 
	var length = get_length();	
	var wep_def_key = get_by_name("wep_def_key");
	var wep_key_type = get_by_id("wep_key_type").value;
	var key_len_msg;
	
	for (var i = 1; i < 5; i++){	
	var key = get_by_id("key" + i).value;
	
	if (wep_def_key[i-1].checked){
	if (key == ''){
	alert(msg[MSG10]);
	return false;
	}
	}
	
	key_len_msg = get_key_len_msg(i);
	
 if (key != ''){
 if (key.length < length){
 alert(show_key_len_error(key_len_msg, length));
 return false;
 }else{
 if (wep_key_type == "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(illegal_key_error[i-1]);
	return false;
	}
	}
	}
 }
 } 
	} 
	
	return true;
}
function disable_dhcp(){
	var dhcp = get_by_name("dhcp");
	
	get_by_id("start_ip").disabled = dhcp[1].checked;
	get_by_id("end_ip").disabled = dhcp[1].checked;
}
function disable_static_address(){
	var wan_type = get_by_name("wan_type");
	
	get_by_id("ip").disabled = wan_type[0].checked;
	get_by_id("mask").disabled = wan_type[0].checked;
	get_by_id("gateway").disabled = wan_type[0].checked;
}
function disable_ip_address(){
	var wan_type = get_by_name("wan_type");
	
	get_by_id("ip").disabled = wan_type[0].checked;
}
	
	
function exit_wizard(){
 if (confirm(msg[MSG0])){
 window.close();
 }
}
function get_by_id(id){
	with(document){
	return getElementById(id);
	}
}
function get_by_name(name){
	with(document){
	return getElementsByName(name);
	}
}
function get_key_len_msg(which_key){
	switch(which_key){
 case 1 :
 return key1_len_error; 
 case 2 :
 return key2_len_error; 
 case 3 :
 return key3_len_error; 
 case 4 :
 return key4_len_error; 
	}
}
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 = parseInt(wep_key_len.value);
	
	if (wep_key_type.value == "0"){
	length *= 2;
	}
	return length;
}
function get_row_data(obj, index){
	
 try {
 return obj.cells[index].childNodes[0].data;
 }catch(e) {
 return ("");
 }
}
function send_submit(which_form){
	get_by_id(which_form).submit();
}
function show_key_len_error(key_len_msg, length){
	switch(length){
	case 5 :
	return key_len_msg[0];
	case 13 :
	return key_len_msg[1];
	case 10 :
	return key_len_msg[2];
	case 26:
	return key_len_msg[3];	
	}
}
function show_words(word){	
	with(document){
	return write(word);
	}
}
function show_wizard(name){
	window.open(name,"Wizard","width=470,height=370")
}
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 enable_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 = false;	
	}
	}	
	
	if (select_objs != null){	
	for (var i = 0; i < select_objs.length; i++){	
	select_objs[i].disabled = false;	
	}
	}
}
function MM_preloadImages() { //v3.0
 var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
 var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
 if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
function MM_swapImgRestore() { //v3.0
 var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_findObj(n, d) { //v4.01
 var p,i,x; if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
 d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
 if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
 for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
 if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function MM_swapImage() { //v3.0
 var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
 if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
