function checkTab(theInput) {
	if ((theInput.value) &&
	    (/\t/.test(theInput.value))) return true;
	else return false;
}
function checkBreaks(theInput) {
	if (theInput.value) {
		theInput.value = theInput.value.replace('\n', ' ');
		theInput.value = theInput.value.replace('\r', ' ');
		theInput.value = theInput.value.replace('<', '&lt;');
		theInput.value = theInput.value.replace('>', '&gt;');
	}
}
function checkBadText(theInput) {
	if ((theInput.value) &&
	    (/\W/.test(theInput.value))) return true;
	else return false;
}
function checkBadNum(theInput) {
	if ((theInput.value) &&
	    (/\D/.test(theInput.value))) return true;
	else return false;
}
function addTerm(theForm) {
	if (!theForm.elements['term_title'].value ||
	    !theForm.elements['term_url'].value) {
		alert('You must enter a term title and a term URL before we can add it to the list.');
		return;
	}
	if (checkTab(theForm.elements['term_title']) ||
	    checkTab(theForm.elements['term_url'])   ||
	    checkTab(theForm.elements['term_description'])) {
		alert('One or more of the title, url, or description contains tabs, and this is not allowed.');
		return;
	}
	checkBreaks(theForm.elements['term_title']);
	checkBreaks(theForm.elements['term_url']);
	checkBreaks(theForm.elements['term_description']);
	var theString = theForm.elements['term_title'].value + '\t' +
	                theForm.elements['term_url'].value;
	if (theForm.elements['term_description'].value) {
		theString += '\t' + theForm.elements['term_description'].value;
	}
	if (theForm.elements['term_list'].value) {
		theForm.elements['term_list'].value = theString + '\n' + theForm.elements['term_list'].value;
	}
	else {
		theForm.elements['term_list'].value = theString;
	}
	theForm.elements['term_title'].value=
	theForm.elements['term_url'].value=
	theForm.elements['term_description'].value='';
}
function checkForm(theForm) {
	if (!document.getElementById) return true;
	var textInputs = ['prefix', 'link_class', 'link_target', 'ignore_class', 'allow_class'];
	var numInputs  = ['max_match'];
	for (var i = 0; i < textInputs.length; i++) {
		var theInput = theForm.elements[textInputs[i]];
		if ((theInput.value) && (checkBadText(theInput))) {
			alert('Only letters, numbers, and underscores allowed in ' + textInputs[i] + '.');
			theInput.focus();
			return false;
		}
	}
	for (var i = 0; i < numInputs.length; i++) {
		var theInput = theForm.elements[numInputs[i]];
		if ((theInput.value) && (checkBadNum(theInput))) {
			alert('Only numbers allowed in ' + numInputs[i] + '.');
			theInput.focus();
			return false;
		}
	}
	if (!theForm.elements['prefix'].value) {
		alert('Prefix is required.');
		return false;
	}
	if (!theForm.elements['term_list'].value) {
		alert('The term list is required.');
		return false;
	}
	return getResults(theForm);
}
function getResults(theForm) {
	if (typeof(oWR_req) == 'undefined') return true;
	document.getElementById('results').value = "Updating. Please wait...";
	var sURL = '/cgi-bin/xref/make_xref.pl';
	var aParams = ['prefix', 'link_class', 'link_target', 'ignore_class', 'allow_class', 
	               'max_match', 'scan_now', 'link_all', 'no_case', 'term_list'];
	var sParams = 'from_ajax=1&n=' + Math.random();
	for (var i = 0; i < aParams.length; i++) {
		var theElement = theForm.elements[aParams[i]];
		if (((theElement.type == 'text') || (theElement.type == 'textarea')) &&
		    theElement.value) {
			sParams += '&' + aParams[i] + '=';
			sParams += encodeURIComponent(theElement.value);
		}
		else if ((theElement.type == 'checkbox') && (theElement.checked)) {
			sParams += '&' + aParams[i] + '=1';
		}
	}
	oWR_req.open('POST', sURL, true);
	oWR_req.onreadystatechange = applyResults;
	oWR_req.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
	oWR_req.send(sParams);
	return false;
}
function applyResults() {
	if ((oWR_req.readyState != 4) || (oWR_req.status != 200)) return;
	var nStatus = oWR_req.responseText;
	if (/^0/.test(nStatus)) {
		var sMessage = nStatus.replace('0\n', '');
		alert(sMessage);
	}
	else {
		var sResults = nStatus.replace('1\n', '');
		document.getElementById('results').value = sResults;
		alert('Results have been updated.');
	}
}
function getHTTPObject() { 
	if (window.ActiveXObject) {
		try { 
			return new ActiveXObject("Msxml2.XMLHTTP"); 
		} 
		catch (e) { 
			try { 
				return new ActiveXObject("Microsoft.XMLHTTP"); 
			} 
			catch (e) {} 
		} 
	}
	else {
		if (typeof XMLHttpRequest != 'undefined') return new XMLHttpRequest(); 
	}
	return false; 
}
function highlight(theBoxName) {
	if (document.getElementById) {
		var theBox = document.getElementById(theBoxName);
		if ((theBox) &&
		    (theBox.select)) theBox.select();
	}
}
oWR_req = getHTTPObject();
