function validateEmail(){
this.set(strip(this.get(), " \r\n\t"));
return this.setMessage(checkEmail(this.get()));
}
function validateEmailList(){
var i, j, mssg, tokens = tokenize(this.get(), " ,;|\n\r\t")
var out = "";
var duplicate = false;
var emailSet = new Array()
for(i = 0; i < tokens.length; i++){
if(tokens[i] > ""){
var message = checkEmail(tokens[i]);
if(message > "")
return this.setMessage('"' + tokens[i] + '" is not valid--' + message);
if(this.allowDuplicates || !emailSet[tokens[i]]){
emailSet[tokens[i]] = true;
out += ", " + tokens[i];
}
}
}
this.set(out.substring(2));
return this.setMessage(out > "" ? "" : "This is a required field");
}
function validateDomain(){
this.set(strip(this.get(), " \r\n\t"));
return this.setMessage(checkDomain(this.get(), false) ? "" : this.get() + " is not a valid domain or IP address");
}
function validateDomainList(){
var i, j, mssg, tokens = tokenize(this.get(), " ,;|\n\r\t")
var out = "";
var duplicate = false;
var domainSet = new Array()
for(i = 0; i < tokens.length; i++){
if(tokens[i] > ""){
if(!checkDomain(tokens[i], false))
return this.setMessage('"' + tokens[i] + '" is not a valid domain name or IP address');
if(this.allowDuplicates || !domainSet[tokens[i]]){
domainSet[tokens[i]] = true;
out += ", " + tokens[i];
}
}
}
this.set(out.substring(2));
return out > "";
}

var defaultProtocol = "http://";
function validateURL(){
var url = strip(this.get(), " \r\n\t");
if(url.toLowerCase().indexOf("mailto:") == 0){
var message = checkEmail(url.substring(7));
return message == "" ? true : this.setMessage("Bad mailto link: " + message);
}
if(url.toLowerCase().indexOf("javascript:") == 0)
return this.allowScripts ? true : this.setMessage("JavaScript is not allowed.");
var protocolEnd = url.indexOf("//") + 2;
var protocol = defaultProtocol;
if(this.allowRelative && url.charAt(0) == "/") return true;
if(protocolEnd < 2 || protocolEnd > 8){
url = protocol + url;
protocolEnd = protocol.length;
}else
protocol = url.substring(0, protocolEnd);



var domainEnd = url.indexOf("/", protocolEnd);
if(domainEnd == -1) domainEnd = url.length;
var domain = url.substring(protocolEnd, domainEnd);
if(!checkDomain(domain, false)) return this.setMessage("Invalid domain: " + domain);

this.set(url);
return true;
}


function checkEmail(e){
var atSign, before, after, dot;
var validChars = ALPHANUMERIC + ".-_@:'";
atSign = e.indexOf("@");
if(atSign == -1) return "Email addresses must contain an @";
before = e.substring(0, atSign);
after = e.substring(atSign + 1, e.length);
if(before.length == 0) return "@ cannot be the first character in an email address.";
if(after.length == 0) return "@ cannot be the last character in an email address.";
if(after.indexOf("@") != -1) return "Email address cannot contain two @ characters.";
if(!checkDomain(after, false)) return "Invalid email domain: " + after;
if(e != extract(e, validChars)) return "Email address contains invalid characters.";
return "";
}

var MAX_PORT = (1 << 16) - 1
var MAX_IP_SECTION = (1 << 8) - 1
function checkDomain(domain, requireSubDomain){
var minSections = requireSubDomain ? 3 : 2;
var isIP = true, sections = new Array(), curSection = 0;;
var hasPort = false, port;
var i, c;

sections[curSection] = "";
for(i = 0; i < domain.length; i++){
c = domain.charAt(i)
if(c == '.'){
if(hasPort) return false;
sections[++curSection] = "";
}else if(ALPHA.indexOf(c) != -1 || c == '-'){
if(hasPort) return false;
isIP = false;
sections[curSection] += c;
}else if(NUMERIC.indexOf(c) != -1){
if(hasPort)
port += c;
else
sections[curSection] += c;
}else if(c == ":"){
if(hasPort) return false;
hasPort = true;
port = "";
}else
return false;
}
if(isIP){
if(curSection != 3) return false;
for(i = 0; i <= curSection; i++)
if(sections[i] == "" || parseInt(sections[i]) > MAX_IP_SECTION) return false;
}else{
if(curSection + 1 < minSections) return false
for(i = 0; i <= curSection; i++)
if(sections[i].length < ((i == curSection) ? 2 : 1) || sections[i].length > ((i == curSection) ? 7 : 63)) return false
}
if(hasPort && (port == "" || parseInt(port) > MAX_PORT)) return false;
return true;
}
function tokenize(str, separators){
var tokens = new Array(), curToken = 0, i;
tokens[curToken] = "";
for(i = 0; i < str.length; i++)
if(separators.indexOf(str.charAt(i)) != -1){
if(tokens[curToken] != "") tokens[++curToken] = "";
}else
tokens[curToken] += str.charAt(i);
return tokens;
}
function validateIPList() {
var ips = this.get();
var ipList = ips.split('\n');
var error = null;
for (i = 0; i < ipList.length; i++) {
ip = ipList[i].replace(/^\s+|\s+$/g, '');
if (ip > "") {
var slash = ip.indexOf("/");
var mask = "";
if (slash != -1) {
mask = extract(ip.substring(slash + 1), NUMERIC);
ip = ip.substring(0, slash);
}
var isIPv6 = false;
if (ip.indexOf(":") != -1) {
var isIPv6 = true;
ip = extract(ip, NUMERIC + "ABCDEFabcdef:");
var parts = ip.split(":");
var foundAbbrv = false;
for (j in parts) {
if (parts[j].length > 4) {
error = "Each part of a IPv6 address may contain, at most, 4 hexidecimal digits."; break;
} else if (parts[j].length == 0) {
if (j == 0 && parts[1] > ""){
error = "An IPv6 address may not begin with a single colon."; break;
} else if (j == parts.length - 1 && parts[j - 1] > ""){
error = "An IPv6 address may not end with a single colon."; break;
} else if (foundAbbrv) {
error = "An IPv6 address may contain only one :: shortcut syntax."; break;
} else if (j > 0) {
foundAbbrv = true;
}
}
}
if (error == null && (foundAbbrv == false && parts.length < 8 || parts.length > 8)) {
error = "An IPv6 address must have 8 parts.";
}
} else {
ip = extract(ip, NUMERIC + ".");
var parts = ip.split(".");
if (parts.length != 4) {
error = "An IPv4 address must have exactly 4 parts.";
} else {
for (j in parts) {
if (parts[j] <= "") {
error = "An IPv4 address part may not be empty."; break;
}
var num = parseInt(parts[j], 10);
if (num > 255) {
error = "An IPv4 address part may not be greater than 255."; break;
}
parts[j] = "" + num;
}
ip = parts.join(".");
}
}
if (this.allowMask != false) {
var max = isIPv6 ? 128 : 32;
var num = max;
if (mask > "") {
num = parseInt(mask, 10);
if (error == null && num > max) {
error = isIPv6 ? "An IPv6 mask may not be greater than 128" : "The mask for a IPv4 address by not be greater than 32.";
}
}
ip += "/" + num;
}
}
if (ip > "") {
ipList[i] = ip;
} else {
ipList.splice(i--, 1);
}
if (error != null) break;
}
this.set(ipList.join("\n"));
return this.setMessage(error);
}
var _SR_;
if(_SR_ != null) _SR_.notify("validatehttp.js");
