function GetXmlHttpObject()
{
var xmlHttp=null;
try
  {
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
return xmlHttp;
}
function stateChanged() 
{ 
  if (xmlHttp.readyState==4)
  { 
	window.location.reload();
  }
}
//checks form element is not empty
function validateName() {
    var error = "";

    if (document.commentform.t_name.value.length == 0) {
        error = "Please enter your name.\n"
        document.commentform.t_name.style.background = '#FFFF99' ; 
    } else {
		document.commentform.t_name.style.background = 'White' ;
    }
    return error;  
}

//checks form element is not empty
function validateSubject() {
    var error = "";
 
    if (document.commentform.t_subject.value.length == 0) {
        error = "The subject field cannot be empty.\n"
		document.commentform.t_subject.style.background = '#FFFF99' ;
    } else {
		document.commentform.t_subject.style.background = 'White' ;
    }
    return error;  
}
//replace /n w br
function swapBR(s)
{
	return s.replace(/\n/g,"[br/]").replace(/\r/g,"");
}
//removes whitespace
function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
}
//valid email??
function validateEmail() {
    var error="";
    var tfld = trim(document.commentform.t_email.value);    // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
   
    if (document.commentform.t_email.value == "") {
		document.commentform.t_email.style.background = '#FFFF99' ;
        error = "You didn't enter an email address.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
		document.commentform.t_email.style.background = '#FFFF99' ;
        error = "Please enter a valid email address.\n";
    } else if (document.commentform.t_email.value.match(illegalChars)) {
		document.commentform.t_email.style.background = '#FFFF99' ;
        error = "The email address contains illegal characters.\n";
    } else {
		document.commentform.t_email.style.background = 'White' ;
    }
    return error;
}

//tests the validity of a form
function valid()
{
var reason = "";

  reason += validateName();
  reason += validateSubject();
  reason += validateEmail();
  
    if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  } else {
	tmpStr = document.commentform.ta_comment.value;
	tmpStr = swapBR(tmpStr);
	var url="makecomment.asp?t="+ document.commentform.t_title.value + "&n=" + document.commentform.t_name.value + "&e=" + document.commentform.t_email.value + "&s=" + document.commentform.t_subject.value + "&c=" + tmpStr ;
	xmlHttp=GetXmlHttpObject();
    if (xmlHttp==null)
    {
       alert ("Your browser does not support AJAX!");
       return;
    } 
	xmlHttp.onreadystatechange=stateChanged; 
	xmlHttp.open("GET",url,true);
    xmlHttp.send(null);
  }
}
