// JavaScript Document

function chk_quotes(str)
{
	re = /(\'.*\")|(\".*\')/;
	return str.search(re) == -1;
}


function chk_pd_name(dir)
{
	re = /^[0-9a-zA-Z-_.\/]+$/;
	// forbid /../ and // and ^./ and ^../  and /./  and /..$ and /.$ and ^.$
	re1 = /(\/\.\.\/|\/\/|^\.\/|^\.\.\/|\/\.\/|\/\.\.$|\/\.$|^\.$)/;
	return (dir.search(re) != -1) && (dir.search(re1) == -1);
}

function chk_db_name(db)
{
	re = /^[A-Za-z0-9-_]{1,63}$/;
	return db.search(re) != -1;
}

function chk_db_usr_name(usr)
{
	re = /^[a-zA-Z]{1}[A-Za-z0-9_\-]{0,15}$/;
	return usr.search(re) != -1;
}

function chk_pos_int(pos_int)
{
	re = /^\s*[1-9]{1}[0-9]*\s*$/;
	return pos_int.search(re) != -1;
}

function chk_sys_login(nm)
{
	re = /^[a-z]{1}[a-z0-9_.-]{0,15}$/;
	return nm.search(re) != -1;
}

function chk_mn(mail_name)
{
	re = /^[\w-][\w&\-\+]*((\.)[\w&\-\+]+)*$/;
	return mail_name.search(re) != -1;
}


function chk_resp_name(resp_name)
{
	return ((resp_name.length > 0) && (resp_name.length <= 245));
}



function chk_login(login)
{
	re = /^[a-zA-Z0-9]{1}[A-Za-z0-9_.-]{0,19}$/;
	return login.search(re) != -1;
}

function chk_realm(realm)
{
	re = /^[^\"]*$/;
	return realm.search(re) != -1;
}

function chk_dom(dom_name)
{
	if ('' == dom_name)
		return false; // check empty value

	if ('localhost.rev' == dom_name)
		return false;

	inaddr = /\.in-addr.arpa$/;
	if (dom_name.search(inaddr) != -1)
		return false;

	if (chk_ip(dom_name))	// no domain name like IP address
		return false;

	return true;    // IDN support
	/*
	nore = /\.$/;
	re = /^[A-Za-z0-9]([A-Za-z0-9-]{0,61}[A-Za-z0-9]){0,1}(\.[A-Za-z0-9]([A-Za-z0-9-]{0,61}[A-Za-z0-9]){0,1}){1,}$/;

	return (dom_name.search(nore) == -1) && dom_name.match(re);
	*/
}

function chk_subdom(dom_name)
{
	return true;    // IDN support
	/*
	re = /^[A-Za-z0-9]([A-Za-z0-9-]*[A-Za-z0-9])*$/;
	return dom_name.search(re) != -1;
	*/
}

function chk_email(email)
{
	re = /^([^\@]+){1}\@([^\@]+){1}$/;
	found = email.match(re);
	re2 = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (!found)
		return false;
	return chk_mn(found[1]) && chk_dom(found[2]) && email.match(re2);
}

function chk_url(url)
{
	re = /^((http[s]?|ftp):\/\/)?([^\/:]+)(:\d{1,5})?(\/[^\s\"\'`]*)?$/i;
	found = re.exec(url);
	if (!found)
		return false;
	return chk_dom(found[3]);
}

function chk_ip(ip)
{
	return chk_ip_address_and_mask(ip, 4, 8);
}

function chk_dom_t(hst)
{
        re = /^((<domain>|[A-Za-z0-9]([A-Za-z0-9-]*[A-Za-z0-9])*)(\.[A-Za-z0-9]([A-Za-z0-9-]*[A-Za-z0-9])*|\.<domain>)+|<domain>)$/;
        return hst.match(re);
}

function chk_ip_t(ip)
{
	re = /^<ip>$/;
	return chk_ip(ip) || ip.match(re);
}

function chk_mask(mask)
{
	re = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/;
	found = mask.match(re);
	if (!found)
		return false;
	i = found.length - 1;
	while (i && (found[i] == 0)) i--;
	if (!i)
		return true;
	if (
		(found[i] != 128) &&
		(found[i] != 192) &&
		(found[i] != 224) &&
		(found[i] != 240) &&
		(found[i] != 248) &&
		(found[i] != 252) &&
		(found[i] != 254) &&
		(found[i] != 255)
	)
		return false;
	i--;
	while (i && (found[i] == 255)) i--;

	return i == 0;
}

function ip2long(str)
{
	var num = 0;
	var re = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/;

	found = str.match(re);
	if (!found)
		return null;
	for (i = 0; ++i < found.length; ) {
		num <<= 8;
		num  |= found[i];
	}
	return num;
}

// convert ip block mask to int32 mask
function block_to_num(block)
{
	num = 0;
	for (i = 0; i < 32; i++) {
		num <<= 1;
		num |= (block > i ? 1 : 0);
	}
	return num;
}

// check ip address and mask by valid rules
function chk_ip_address_and_mask(ip, valid, valid_formats)
{
	var ip_address_mask_format = /^(\d{1,3})(\.(\d{1,3})(\.(\d{1,3})(\.\*|)|\.\*\.\*|\.\*|)|\.\*\.\*\.\*|\.\*\.\*|\.\*|)()$/;
	var ip_address_block_format = /^(\d{1,3})(\.(\d{1,3})(\.(\d{1,3})(\.(\d{1,3})|)|)|)\/(\d{1,2})$/;
	var ip_address_netmask_format = /^(\d{1,3})(\.(\d{1,3})(\.(\d{1,3})(\.(\d{1,3})|)|)|)\/(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/;
	var ip_address_format = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/;

	if (!valid)
		valid = 7;
	if (!valid_formats)
		valid_formats = 15;

	var found;
	var ip_address = null;
	var ip_mask = null;

	if (
		(valid_formats & 1) &&
		(found = ip.match(ip_address_mask_format)) &&
		(!found[1] || (found[1] >= 0 && found[1] <= 255)) &&
		(!found[3] || (found[3] >= 0 && found[3] <= 255)) &&
		(!found[5] || (found[5] >= 0 && found[5] <= 255)) &&
		(!found[7] || (found[7] >= 0 && found[7] <= 255))
	) {
		ip_address = ip2long((found[1] ? found[1] : 0) + '.' + (found[3] ? found[3] : 0) + '.' + (found[5] ? found[5] : 0) + '.' + (found[7] ? found[7] : 0));
		ip_mask = ip2long((found[1] ? 255 : 0) + '.' + (found[3] ? 255 : 0) + '.' + (found[5] ? 255 : 0) + '.' + (found[7] ? 255 : 0));
	} else if (
		(valid_formats & 2) &&
		(found = ip.match(ip_address_block_format)) &&
		(!found[1] || (found[1] >= 0 && found[1] <= 255)) &&
		(!found[3] || (found[3] >= 0 && found[3] <= 255)) &&
		(!found[5] || (found[5] >= 0 && found[5] <= 255)) &&
		(!found[7] || (found[7] >= 0 && found[7] <= 255)) &&
		(found[8] >= 0 && found[8] <= 32)
	) {
		ip_address = ip2long((found[1] ? found[1] : 0) + '.' + (found[3] ? found[3] : 0) + '.' + (found[5] ? found[5] : 0) + '.' + (found[7] ? found[7] : 0));
		ip_mask = block_to_num(found[8]);
	} else if (
		(valid_formats & 4) &&
		(found = ip.match(ip_address_netmask_format)) &&
		(!found[1] || (found[1] >= 0 && found[1] <= 223)) &&
		(!found[3] || (found[3] >= 0 && found[3] <= 255)) &&
		(!found[5] || (found[5] >= 0 && found[5] <= 255)) &&
		(!found[7] || (found[7] >= 0 && found[7] <= 255)) &&
		(found[8] >= 0 && found[8] <= 255) &&
		(found[9] >= 0 && found[9] <= 255) &&
		(found[10] >= 0 && found[10] <= 255) &&
		(found[11] >= 0 && found[11] <= 255)
	) {
		ip_address = ip2long((found[1] ? found[1] : 0) + '.' + (found[3] ? found[3] : 0) + '.' + (found[5] ? found[5] : 0) + '.' + (found[7] ? found[7] : 0));
		ip_mask = ip2long(found[8] + '.' + found[9] + '.' + found[10] + '.' + found[11]);
	} else if (
		(valid_formats & 8) &&
		(found = ip.match(ip_address_format)) &&
		(found[1] >= 0 && found[1] <= 255) &&
		(found[2] >= 0 && found[2] <= 255) &&
		(found[3] >= 0 && found[3] <= 255) &&
		(found[4] >= 0 && found[4] <= 255)
	) {
		ip_address = ip2long(found[1] + '.' + found[2] + '.' + found[3] + '.' + found[4]);
		ip_mask = block_to_num(32);
	}

	if (null == ip_address || null == ip_mask)
		return false;

	return ((valid & 1) && //network block
				((ip_address & ip_mask) == ip_address)
		) ||
		((valid & 2) && // ip address with netmask
			ip_address && ip_mask &&
				(
					((ip_address & ~ip_mask) && // and not empty lower bits
					~((ip_address & ~ip_mask) | ip_mask) // minimum network size 'may be removed - bcause added single addres networks by client request ...
				) || (ip_mask == block_to_num(32))
			) // or single ip address
		) ||
		((valid & 4) &&  //sigle ip address
			ip_address && (ip_mask == block_to_num(32)) // not empty ip address and 0xFFFFFFFF ip mask
		);
}

function chk_ip_mask(ip, mask)
{
	if (!chk_ip(ip) || !chk_mask(mask))
		return false;

	var num_ip;
	var num_mask;

	if ((num_ip = ip2long(ip)) == null)
		return false;

	if ((num_mask = ip2long(mask)) == null)
		return false;

	if (num_ip == 0)
		return false;

	// check ip/mask combination for interface:
	// 1) mask not null
	// 2) host part not null (num_ip & ~num_mask)
	// 3) host part not all 1 ~((num_ip & ~num_mask) | num_mask)
	return num_mask && (((num_ip & ~num_mask) && ~((num_ip & ~num_mask) | num_mask)) || (mask == '255.255.255.255'));
}

function chk_net_mask(net, mask)
{
	if (!chk_ip(net) || !chk_mask(mask))
		return false;

	var num_net;
	var num_mask;

	if ((num_net = ip2long(net)) == null)
		return false;

	if ((num_mask = ip2long(mask)) == null)
		return false;

	return num_mask && (num_net & num_mask) && ((num_net & num_mask) == num_net);
}



function chk_sys_passwd(username, passwd)
{
	if ((passwd.length < 5) || (passwd.length > 255))
		return false;

	if (passwd.length >= username.length) {
		if (passwd.indexOf(username, 0) != -1)
			return false;
	}

	if ((passwd.indexOf('\'') != -1) || (passwd.indexOf(' ') != -1))
		return false;

	for (i = passwd.length; i-- > 0;) {
		if (passwd.charCodeAt(i) > 127)
			return false;
	}

	return true;
}

function chk_filename(filename)
{
	re = /^[^\']*$/;
	return filename.search(re) != -1;

}

function chk_uint(uint)
{
	re = /^[0-9]+$/;
	found = uint.match(re);
	if (!found) {
		return false;
	}

	return true;
}


function chk_digit(digit)
{
  return ((digit >= "0") && (digit <= "9"))
}


function chk_empty(s)
   {
      return ((s == null) || (s.length == 0))
   }


function chk_integer (integer)
{
  var i;

  if (chk_empty(integer))
  if (chk_integer.arguments.length == 1) return 0;
  else return (chk_integer.arguments[1] == true);

  for (i = 0; i < integer.length; i++)
  {
	 var digit = integer.charAt(i);

	 if (!chk_digit(digit)) return false;
  }

  return true;
}


function chk_urlFull(url) {
	var regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
	return regexp.test(url);
	
}

