//var validip=/^\d{1,3}\.\d{1,3}(\.\d{1,3}){0,2}\s*$/ //match valid ip address- up to 999.999.999.999
//var valid_domain=/^[\w-\.]+(\.[a-z]{2,4}|\d+){0,1}\s*$/i

var validip=/^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.)){1,3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)?$/
var valid_domain=/^[\w\.-]+$/
var valid_file=/^([a-zA-Z0-9#_\-]+)$/i

function isValidIP(ip){
return validip.test(ip)
}

function isValidDomain(domain){
if (/[a-z]+/i.test(domain)) //if domain isn't a IP domain (contains alpha chars)
return valid_domain.test(domain)
else //else, test for valid IP domain
return validip.test(domain)
}

function isValidFile(file){
return valid_file.test(file)
}

/*
function validateip(field){
	var acceptinput=true
	var iptextarea=field
	var ips=iptextarea.value.split("\n") //Store each line of textarea as array element
	for (var i=0; i<ips.length; i++){
		if (!validip.test(ips[i])){ //if this line is not a valid IP
			acceptinput=false
			errormsg+="- "+ips[i]+"\n"
		}
	}
	if (!acceptinput)
		alert(errormsg)
	else if (iptextarea.value=="")
		alert("TEXTAREA is blank! Please entire a list of IP addresses to ban, each on its own line.")
	else{
		return true
	}
	return false
}
*/

function useripban(input, reqtype){
var errormsg="The following IPs are invalid. Please correct them and press \"submit\" again:\n\n"
var acceptinput=true
var ips=input.value.split(/\r*\n/) //Store each line of textarea as array element
for (var i=0; i<ips.length; i++){
if (!isValidIP(ips[i])){ //if this line is not a valid IP
acceptinput=false
errormsg+="- "+ips[i]+"\n"
}
} //END loop
if (!acceptinput)
alert(errormsg)
else
getindividual(reqtype)
}

function referrer_hotlink_ban(input, reqtype){
var errormsg="The following DOMAINS/ Site IPS are invalid. Please correct them and press \"submit\" again:\n\n"
var acceptinput=true
var domains=input.value.split(/\r*\n/) //Store each line of textarea as array element
for (var i=0; i<domains.length; i++){
if (!isValidDomain(domains[i])){ //if this line is not a valid domain
acceptinput=false
errormsg+="- "+domains[i]+"\n"
}
} //END loop
if (reqtype=="hotlinkban"){ //if this is a "hot link ban" request
var input_filetype=document.getElementById("filetype_list")
removewhitespace(input_filetype)
var filetypes=input_filetype.value.split(/\r*\n/)
for (var i=0; i<filetypes.length; i++){
if (!isValidFile(filetypes[i])){ //if this line is not a valid file type
acceptinput=false
errormsg+="- "+filetypes[i]+"\n"
}
} //END loop
}
if (!acceptinput)
alert(errormsg)
else
getindividual(reqtype)
}


function processForm(form, reqtype){
document.getElementById('output-'+reqtype).innerHTML=''
var inputfield=(reqtype=="userban")? form.ip_deny_list : (reqtype=="referrerban")? form.referer_deny_list : form.filetype_ip_allow_list
removewhitespace(inputfield)
if (reqtype=="userban")
useripban(inputfield, reqtype)
else
referrer_hotlink_ban(inputfield, reqtype)
}


function clearText(thefield){
if (thefield.defaultValue==thefield.value)
thefield.select()
}

function removewhitespace(textarea){ //remove white spaces and line breaks from a textarea value
var list=textarea.value.split(/\r*\n/)
for (var i=0; i<list.length; i++)
list[i]=list[i].replace(/^\s+/g, '').replace(/\s+$/g, ''); //remove leading/ trailing white spaces
textareafinal=list.join("\n").replace(/\s{2,}/g, '\n').replace(/^\s+/g, '').replace(/\s+$/g, '') //replace multiple new lines, spaces at start, spaces at end
textarea.value=textareafinal
}

