var tablename = "";
var root = "";
function isPostcode(code){
	var result;
	result = code.match(/^[a-zA-Z][0-9][a-zA-Z][ ]?[0-9][a-zA-Z][0-9]$/);
	if ( result )
		return true;
	else{
		result = code.match(/(^\d{5}$)|(^\d{5}-\d{4}$)/);
		if ( result )
			return true;
	}
	return false;
}
function isDate(code){
	var result;
	result = code.match(/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/);
	if ( result )
		return true;
	return false; 
}
function isPhone(code){
	var result;
	result = code.match(/^[0-9]{3}-[0-9]{3}-[0-9]{4}$/);
	if ( result )
		return true;
	return false; 
}
function isExtension(code){
	var result;
	result = code.match(/^[0-9]+$/);
	if ( result )
		return true;
	return false; 
}
function isNumber(code){
	var result;
	result = code.match(/^[0-9]+$/);
	if ( result )
		return true;
	return false; 
}
function isPhoneWithExtension(code){
	var result;
	result = code.match(/^[0-9]{3}-[0-9]{3}-[0-9]{4} (x[0-9])?$/);
	if ( result )
		return true;
	return false;
}
function isEmail(emailStr) {
var emailPat=/^(.+)@(.+)$/
var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
var validChars="\[^\\s" + specialChars + "\]"
var quotedUser="(\"[^\"]*\")"
var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
var atom=validChars + '+'
var word="(" + atom + "|" + quotedUser + ")"
var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")

var matchArray=emailStr.match(emailPat)
if (matchArray==null) {
	alert("E-mail address seems incorrect (check @ and .'s)")
	return false
}
var user=matchArray[1]
var domain=matchArray[2]

if (user.match(userPat)==null) {
    alert("The username doesn't seem to be valid.")
    return false
}

var IPArray=domain.match(ipDomainPat)
if (IPArray!=null) {
	  for (var i=1;i<=4;i++) {
	    if (IPArray[i]>255) {
	        alert("Destination IP address is invalid!")
		return false
	    }
    }
    return true
}

var domainArray=domain.match(domainPat)
if (domainArray==null) {
	alert("The domain name doesn't seem to be valid.")
    return false
}

var atomPat=new RegExp(atom,"g")
var domArr=domain.match(atomPat)
var len=domArr.length
if (domArr[domArr.length-1].length<2 || 
    domArr[domArr.length-1].length>3) {
   alert("The address must end in a three-letter domain, or two letter country.")
   return false
}

if (len<2) {
   var errStr="This address is missing a hostname!"
   alert(errStr)
   return false
}

return true;
}
function urlencode(instr){
	var rstr = instr;
	rstr = rstr.replace("%", "%25");
	rstr = rstr.replace("!", "%21");
	rstr = rstr.replace("*", "%2A");
	rstr = rstr.replace("'", "%27");
	rstr = rstr.replace("(", "%28");
	rstr = rstr.replace(")", "%29");
	rstr = rstr.replace(";", "%3B");
	rstr = rstr.replace(":", "%3A");
	rstr = rstr.replace("@", "%40");
	rstr = rstr.replace("&", "%26");
	rstr = rstr.replace("=", "%3D");
	rstr = rstr.replace("+", "%2B");
	rstr = rstr.replace("$", "%24");
	rstr = rstr.replace(",", "%2C");
	rstr = rstr.replace("/", "%2F");
	rstr = rstr.replace("?", "%3F");
	rstr = rstr.replace("#", "%23");
	rstr = rstr.replace("[", "%5B");
	rstr = rstr.replace("]", "%5D");
	return rstr;
}
function $(id) { return document.getElementById(id); }
function createXMLHttpRequest() {
  try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {}
  try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {}
  try { return new XMLHttpRequest(); } catch(e) {}
  alert("XMLHttpRequest not supported");
  return null;
}
function updatefieldByCheckbox(id, obj, fname){
	var str;
	if ( obj.checked == true )
		str = "Y";
	else
		str = "N";
	updatefieldCommon(id, str, fname);
}
function updatefield(id, obj, fname){
	updatefieldCommon(id, obj.value, fname);
}
function updatefieldUppercase(id, obj, fname){
	updatefieldCommon(id, obj.value.toUpperCase(), fname);
}
function updatefieldCommon(id, vobj, fname){
	var url;
	var xhr = createXMLHttpRequest();
	xhr.onreadystatechange = function() {
		if (xhr.readyState==4) { // Request is finished
			$("status").style.visibility = "hidden";
			if (xhr.status==200 || xhr.status == 0) {
			} else {
				alert("Message returned, but with error status.");
			}
		}
	}
	url = root + "common/update.asp?t=" + tablename;
	url += "&id=" + id;
	url += "&fn=" + fname;
	url += "&fv=" + urlencode(vobj);
	xhr.open("GET", url, true);
	xhr.send(null);
	$("status").style.visibility = "visible";
}
function updateDiv(urlbase, divname){
	var url;
	var xhr = createXMLHttpRequest();
	xhr.onreadystatechange = function() {
		if (xhr.readyState==4) { // Request is finished
			$("status").style.visibility = "hidden";
			if (xhr.status==200 || xhr.status == 0) {
				$(divname).innerHTML = xhr.responseText;
			} else {
				alert("Message returned, but with error status.");
			}
		}
	}
	url = root + urlbase;
	xhr.open("GET", url, true);
	xhr.send(null);
	$("status").style.visibility = "visible";
}

