function get_by_id(id){
	return document.getElementById(id);
}

function get_by_name(name){
	return document.getElementsByName(name);
}

function send_submit(which_form){
	get_by_id(which_form).submit();
}

function ShowWizard(name){
        window.open(name,"Wizard","width=450,height=370")
}

function ShowPage(name){
				window.open(name,"Showpage","width=1000,height=500,resizable=1,scrollbars=1")
}

function validateNum(str)
{
  for (var i=0; i<str.length; i++) {
   	if ( !(str.charAt(i) >='0' && str.charAt(i) <= '9')) {
		alert("Invalid value. It should be in decimal number (0-9).");
		return false;
  	}
  }
  return true;
}

function do_reboot()
{
        if(confirm("Reboot DAP-1360?")){
                alert("This feature has been disabled on this emulator.");
        }
}
	
var subnet_mask = new Array(0, 128, 192, 224, 240, 248, 252, 254, 255);
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 FOURTH_RANGE_ERROR1 = 10;
var RADIUS_SERVER_PORT_ERROR = 11;
var RADIUS_SERVER_SECRET_ERROR = 12;

var ip_addr_msg = new Array("The IP address is an invalid address.",
						       "The IP address can not be zero.",
						       "The 1st address of IP address must be an integer.",
						       "The 2nd address of IP address must be an integer.",
						       "The 3rd address of IP address must be an integer.",
						       "The 4th address of IP address must be an integer.",
						       "The 1st range of IP address must be between 1 to 254.",
						       "The 2nd range of IP address must be between 0 to 255.",
						       "The 3rd range of IP address must be between 0 to 255.",
						       "The 4th range of IP address must be between 1 to 254.",
						       "The 4th range of IP address must be between 0 to 255."
						      );

var subnet_mask_msg = new Array("The Subnet Mask is an invalid address.",
						           "The Subnet Mask can not be zero.",
						           "The 1st address of Subnet Mask must be an integer.",
						    	   "The 2nd address of Subnet Mask must be an integer.",
						    	   "The 3rd address of Subnet Mask must be an integer.",
						    	   "The 4th address of Subnet Mask must be an integer.",
						    	   "The 1st range of Subnet Mask must be 128, 192, 224, 240, 248, 252, 254, 255.",
						    	   "The 2nd range of Subnet Mask must be 0, 128, 192, 224, 240, 248, 252, 254, 255.",
						    	   "The 3rd range of Subnet Mask must be 0, 128, 192, 224, 240, 248, 252, 254, 255.",
						    	   "The 4th range of Subnet Mask must be 0, 128, 192, 224, 240, 248, 252, 254."
						          );

var gateway_msg = new Array("The Gateway address is an invalid address.",
						       "The Gateway address can not be zero.",
						       "The 1st address of Gateway address must be an integer.",
						       "The 2nd address of Gateway address must be an integer.",
						       "The 3rd address of Gateway address must be an integer.",
						       "The 4th address of Gateway address must be an integer.",
						       "The 1st range of Gateway address must be between 1 to 254.",
						       "The 2nd range of Gateway address must be between 0 to 255.",
						       "The 3rd range of Gateway address must be between 0 to 255.",
						       "The 4th range of Gateway address must be between 1 to 254.",
						       "The 4th range of Gateway address must be between 0 to 255."
						       );

var server_msg = new Array("The server address is an invalid address.",
						       "The server address can not be zero.",
						       "The 1st address of server address must be an integer.",
						       "The 2nd address of server address must be an integer.",
						       "The 3rd address of server address must be an integer.",
						       "The 4th address of server address must be an integer.",
						       "The 1st range of server address must be between 1 to 254.",
						       "The 2nd range of server address must be between 0 to 255.",
						       "The 3rd range of server address must be between 0 to 255.",
						       "The 4th range of server address must be between 1 to 254.",
						       "The 4th range of server address must be between 0 to 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 check_current_range(order, my_obj, checking_ip, mask){
	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")){
		alert(my_obj.e_msg[FIRST_IP_ERROR + order]);
		return false;
	}
	
	if (order == 0){
		start = 1;	
	}else{
		start = 0;				
	}
	
	if (mask[order] != 255){				
		if (parseInt(checking_ip[order]) >= 0 && parseInt(checking_ip[order]) <= 255){	
			end = (~mask[order]+256);				
			start = mask[order] & checking_ip[order];	
			end += start;
		
			if (end > 255){
				end = 255;
			}
		}else{
			end = 255;
		}
	}else{
		end = 255;
	}
	
	if (order == 3){
		if ((mask[0] == 255) && (mask[1] == 255) && (mask[2] == 255)){
			start += 1;
			end -= 1;
		}else{		
			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)){
				start += 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 + " ~ " + end + ".");		
		return false;
	}
	
	return true;
}

function check_ip_range(order, my_obj, mask){
	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")){
		alert(my_obj.e_msg[FIRST_IP_ERROR + order]);
		return false;
	}
	
	if (order == 0){
		start = 1;	
	}else{
		start = 0;				
	}
		
	if (mask[order] != 255){		
		if (parseInt(which_ip) >= 0 && parseInt(which_ip) <= 255){	
			end = (~mask[order]+256);				
			start = mask[order] & which_ip;	
			end += start;
		
			if (end > 255){
				end = 255;
			}
		}else{
			end = 255;
		}
	}else{
		end = 255;
	}
	
	
	if (order == 3){
		if ((mask[0] == 255) && (mask[1] == 255) && (mask[2] == 255)){
			start += 1;
			end -= 1;
		}else{								
			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)){
				start += 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 + " ~ " + end + ".");		
		return false;
	}
	
	return true;
}

function check_IP(ip_address, msg_title){
	var ip_split;
	var re=/^(\d{1,3}\.){3}\d{1,3}$/gi;
	var chkflag=true;
	var ErrMsg="The "+msg_title+" is an invalid address!"
	if(ip_address.search(re)==-1){
		chkflag=false;
	}else{
		ip_split=ip_address.split(".");
		for(i=0;i<4;i++){
			if(ip_split[i]>255){
				chkflag=false;
				break;
			}
		}
	}
	if(!chkflag)
	alert(ErrMsg);
	return chkflag;
} 

function check_address(my_obj, mask_obj, ip_obj){
	var count_zero = 0;
	var count_bcast = 0;	
	var ip = my_obj.addr;
	var mask;
	
	if (my_obj.addr.length == 4){
		for(var i = 0; i < ip.length; i++){
			if (ip[i] == "0"){
				count_zero++;			
			}				
		}

		if (!my_obj.allow_zero && count_zero == 4){
			alert(my_obj.e_msg[ZERO_IP]);
			return false;
		}else if (count_zero != 4){
			count_zero = 0;
				
			if (check_address.arguments.length >= 2 && 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_current_range(i, my_obj, ip_obj.addr, mask)){
						return false;
					}
				}else{					
					if (!check_ip_range(i, my_obj, mask)){
						return false;
					}
				}
			}		
							
			for (var i = 0; i < 4; i++){
				if (((~mask[i] + 256) & ip[i]) == 0){
					count_zero++;						
				}
								
				if ((mask[i] | ip[i]) == 255){
					count_bcast++;
				}
			}
		
			if ((count_zero == 4 && !my_obj.is_network) || (count_bcast == 4)){
				alert(my_obj.e_msg[INVALID_IP]);			
				return false;
			}													
		}
	}else{
		alert(my_obj.e_msg[INVALID_IP]);
		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 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")){
				alert(my_mask.e_msg[FIRST_IP_ERROR + i]);
				return false;
			}

			if (i == 0){
				j = 1;
			}

			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){
				if (parseInt(temp_mask[i-1]) != 255){
					alert(my_mask.e_msg[INVALID_IP]);
					return false;
				}
			}

			if (i == 3 && parseInt(mask) == 255){
				alert(my_mask.e_msg[FOURTH_RANGE_ERROR]);
				return false;
			}
		}
	}else{
		alert(my_mask.e_msg[INVALID_IP]);
		return false;
	}

	return true;
}

var app_port_id = new Array(
									"21","21","1",
						      "80","80","1",
						      "443","443","1",
									"53","53","2",
						      "25","25","1",
						      "110","110","1",
						      "23","23","1",
						      "500","500","2",
						      "1723","1723","1",
						      "1503","1503","1"
						      );

function pin_checksum(PIN)
{
	var accum = 0;
	var digit;
	
	PIN *= 10;
	accum += 3 * (parseInt(PIN / 10000000) % 10); 	
	accum += 1 * (parseInt(PIN / 1000000) % 10);
	accum += 3 * (parseInt(PIN / 100000) % 10);
	accum += 1 * (parseInt(PIN / 10000) % 10); 
	accum += 3 * (parseInt(PIN / 1000) % 10); 
	accum += 1 * (parseInt(PIN / 100) % 10); 
	accum += 3 * (parseInt(PIN / 10) % 10);

	digit = (accum % 10);
	return (10 - digit) % 10;
}

/*                                                                                                 
 * set_form_default_values                                                                         
 *	Save a form's current values to a custom attribute.                                            
 */                                                                                                
function set_form_default_values(form_id)                                                          
{                                                                                                  
	var df = document.forms[form_id];                                                                
	if (!df) {                                                                                       
		return;                                                                                        
	}                                                                                                
	for (var i = 0, k = df.elements.length; i < k; i++) {                                            
		var obj = df.elements[i];                                                                      
		if (obj.getAttribute('modified') == 'ignore') {                                                
			continue;                                                                                    
		}                                                                                              
		var name = obj.tagName.toLowerCase();                                                          
		if (name == 'input') {                                                                         
			var type = obj.type.toLowerCase();                                                           
			if ((type == 'text') || (type == 'textarea') || (type == 'password') || (type == 'hidden')) {
				obj.setAttribute('default', obj.value);                                                    
				/* Workaround for FF error when calling focus() from an input text element. */             
				if (type == 'text') {                                                                      
					obj.setAttribute('autocomplete', 'off');                                                 
				}                                                                                          
			} else if ((type == 'checkbox') || (type == 'radio')) {                                      
				obj.setAttribute('default', obj.checked);                                                  
			}                                                                                            
		} else if (name == 'select') {                                                                 
			var opt = obj.options;                                                                       
			for (var j = 0; j < opt.length; j++) {                                                       
				opt[j].setAttribute('default', opt[j].selected);                                           
			}                                                                                            
		}                                                                                              
	}                                                                                                
	df.setAttribute('saved', "true");                                                                
}

/*
 * is_form_modified
 *	Check if a form's current values differ from saved values in custom attribute.
 *	Function skips elements with attribute: 'modified'= 'ignore'. 
 */
function is_form_modified(form_id)
{
	var df = document.forms[form_id];
	if (!df) {
		return false;
	}
	if (df.getAttribute('modified') == "true") {
		return true;
	}
	if (df.getAttribute('saved') != "true") {
		return false;
	}

	for (var i = 0, k = df.elements.length; i < k; i++) {
		var obj = df.elements[i];
		if (obj.getAttribute('modified') == 'ignore') {
			continue;
		}
		var name = obj.tagName.toLowerCase();
		if (name == 'input') {
			var type = obj.type.toLowerCase();
			if (((type == 'text') || (type == 'textarea') || (type == 'password') || (type == 'hidden')) &&
					!are_values_equal(obj.getAttribute('default'), obj.value)) {
				return true;
			} else if (((type == 'checkbox') || (type == 'radio')) && !are_values_equal(obj.getAttribute('default'), obj.checked)) {
				return true;
			}
		} else if (name == 'select') {
			var opt = obj.options;
			for (var j = 0; j < opt.length; j++) {
				if (!are_values_equal(opt[j].getAttribute('default'), opt[j].selected)) {
					return true;
				}
			}
		}
	}
	return false;
}

/*
 * are_values_equal()
 *	Compare values of types boolean, string and number. The types may be different.
 *	Returns true if values are equal.
 */
function are_values_equal(val1, val2)
{
	/* Make sure we can handle these values. */
	switch (typeof(val1)) {
	case 'boolean':
	case 'string':
	case 'number':
		break;
	default:
		// alert("are_values_equal does not handle the type '" + typeof(val1) + "' of val1 '" + val1 + "'.");
		return false;
	}

	switch (typeof(val2)) {
	case 'boolean':
		switch (typeof(val1)) {
		case 'boolean':
			return (val1 == val2);
		case 'string':
			if (val2) {
				return (val1 == "1" || val1.toLowerCase() == "true" || val1.toLowerCase() == "on");
			} else {
				return (val1 == "0" || val1.toLowerCase() == "false" || val1.toLowerCase() == "off");
			}
			break;
		case 'number':
			return (val1 == val2 * 1);
		}
		break;
	case 'string':
		switch (typeof(val1)) {
		case 'boolean':
			if (val1) {
				return (val2 == "1" || val2.toLowerCase() == "true" || val2.toLowerCase() == "on");
			} else {
				return (val2 == "0" || val2.toLowerCase() == "false" || val2.toLowerCase() == "off");
			}
			break;
		case 'string':
			if (val2 == "1" || val2.toLowerCase() == "true" || val2.toLowerCase() == "on") {
				return (val1 == "1" || val1.toLowerCase() == "true" || val1.toLowerCase() == "on");
			}
			if (val2 == "0" || val2.toLowerCase() == "false" || val2.toLowerCase() == "off") {
				return (val1 == "0" || val1.toLowerCase() == "false" || val1.toLowerCase() == "off");
			}
			return (val2 == val1);
		case 'number':
			if (val2 == "1" || val2.toLowerCase() == "true" || val2.toLowerCase() == "on") {
				return (val1 == 1);
			}
			if (val2 == "0" || val2.toLowerCase() == "false" || val2.toLowerCase() == "off") {
				return (val1 === 0);
			}
			return (val2 == val1 + "");
		}
		break;
	case 'number':
		switch (typeof(val1)) {
		case 'boolean':
			return (val1 * 1 == val2);
		case 'string':
			if (val1 == "1" || val1.toLowerCase() == "true" || val1.toLowerCase() == "on") {
				return (val2 == 1);
			}
			if (val1 == "0" || val1.toLowerCase() == "false" || val1.toLowerCase() == "off") {
				return (val2 === 0);
			}
			return (val1 == val2 + "");
		case 'number':
			return (val2 == val1);
		}
		break;
	default:
		return false;
	}
}

/*
 * reset_form()
 *	Reset a form with previously saved default values.
 *	Function skips elements with attribute: 'modified'= 'ignore'. 
 */
function reset_form(form_id)
{
	var df = document.forms[form_id];
	if (!df) {
		return;
	}
	if (df.getAttribute('saved') != "true") {
		return;
	}

	for (var i = 0, k = df.elements.length; i < k; i++) {
		var obj = df.elements[i];
		if (obj.getAttribute('modified') == 'ignore') {
			continue;
		}
		var name = obj.tagName.toLowerCase();
		var value;
		if (name == 'input') {
			var type = obj.type.toLowerCase();
			if ((type == 'text') || (type == 'textarea') || (type == 'password') || (type == 'hidden')) {
				obj.value = obj.getAttribute('default');
			} else if ((type == 'checkbox') || (type == 'radio')) {
				value = obj.getAttribute('default');
				switch (typeof(value)) {
				case 'boolean':
					obj.checked = value;
					break;
				case 'string':
					if (value == "1" || value.toLowerCase() == "true" || value.toLowerCase() == "on") {
						obj.checked = true;
					}
					if (value == "0" || value.toLowerCase() == "false" || value.toLowerCase() == "off") {
						obj.checked = false;
					}
					break;
				}
			}
		} else if (name == 'select') {
			var opt = obj.options;
			for (var j = 0; j < opt.length; j++) {
				value = obj[j].getAttribute('default');
				switch (typeof(value)) {
				case 'boolean':
					obj[j].selected = value;
					break;
				case 'string':
					if (value == "1" || value.toLowerCase() == "true" || value.toLowerCase() == "on") {
						obj[j].selected = true;
					}
					if (value == "0" || value.toLowerCase() == "false" || value.toLowerCase() == "off") {
						obj[j].selected = false;
					}
					break;
				}
			}
		}
	}
}

/*
 * is_number
 *	Return true if a value represents a number, else return false.
 */
function is_number(value)
{
	var str = value + "";
	return str.match(/^-?\d*\.?\d+$/) ? true : false;
}

var days = new Array(	"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" );				     						          						      
var days_in_month = ['31', '28', '31', '30', '31', '30', '31', '31', '30', '31', '30', '31'];