function clean(text) {
	var result = text.replace(/[\W]/g,"")
	return result
}

function getConnectType() {
	var regx = /^(http|https)\:.*$/
	var thisPageURL = document.URL
	var connType = thisPageURL.match(regx)
	return connType[1]
}

function AppStatus() {
	var status = ""
	var vhost = location.hostname.split(".")

	if(vhost.length > 0) {
		vhost = vhost[0]
	} else {
		vhost = ""
	}

	var re = new RegExp("^(.+)(dev|stage)$")
	var m = re.exec(vhost)

	if(m != null && m.constructor.toString().indexOf("Array") != -1) {
		if(m[2] != "") {
			status = m[2]
		}
 	} else {
		status = "prod"
	}
	return status
}

function getVhostName() {
	var returnVal = ""
	var thisVhostName =  location.hostname.split(".")

	if(thisVhostName.length > 0) {
		thisVhostName = thisVhostName[0]
	} else {
		thisVhostName = ""
	}
	var re = new RegExp("^(.+)(dev|stage)$")
	var m = re.exec(thisVhostName)
	if (m != null) {
		returnVal = m[1]
	} else {
		returnVal = thisVhostName
	}
	return returnVal
}

function displayCaptchaURL(urlSuffix) {
	var vhost = "" 
	var ssl = ""
	var aps = AppStatus()
	if(aps == "dev") {
		vhost = getVhostName()+"dev"
	} else if (aps == "stage") {	
		vhost = getVhostName()+"stage"
	} else if(aps == "prod") {
		vhost = getVhostName()
	}

	document.open()
	document.write("<img src=\""+getConnectType()+"://"+vhost+".uchicago.edu"+urlSuffix+"?"+Math.random()+"\" alt=\"CAPTCHA\" border=\"0\"/>")
	document.close()
}
 
function displayFormURL(urlSuffix,id,thisClass) {
    var vhost = ""
    var aps = AppStatus()
	if (id == undefined) { id = "" }
	if (thisClass == undefined) { thisClass = "" }
    if(aps == "dev") {
		vhost = getVhostName()+"dev"
    } else if (aps == "stage") {
		vhost = getVhostName()+"stage"
    } else if(aps == "prod") {
		vhost = getVhostName()
	}
	
    document.open()
    document.write("<form action=\""+getConnectType()+"://"+vhost+".uchicago.edu"+urlSuffix+"\" method=\"post\" id=\""+clean(id)+"\" class=\""+clean(thisClass)+"\">")
    document.close()
}

function check4Errors(id,thisClass) {
	if (id == undefined) { id = "" }
	if (thisClass == undefined) { thisClass = "" }
	var get=(""+location.search).substring(1).split("&")
	for (var i in get) {
		var temp=get[i].split("=")
		if(temp[0] == "captcha" && temp[1] == "error") {
			document.open()
			document.write("<div id=\""+clean(id)+"\" class=\""+clean(thisClass)+"\">The entered text did not match the text in the image.</div>")
			document.close()
		}
	}
}