/* Copyright Luke Bakken 2006 */

if (!window.Node)
{
       /* IE, how I hate thee... let me count the ways... */
       var Node = {
               ELEMENT_NODE: 1,
               ATTRIBUTE_NODE: 2,
               TEXT_NODE: 3,
               COMMENT_NODE: 8,
               DOCUMENT_NODE: 9,
               DOCUMENT_FRAGMENT_NODE: 11
       }
}

var allowedHTML = {
       /*
        * Allowed HTML tags and attributes for each tag.
        * The object is constructed like this to function as
        * a hash table for fast lookup.
        */
       p: 1,
       b: 1,
       a: { href: 1 },
       i: 1,
       em: 1,
       strong: 1
}

function validateAttributes(node, nodeName)
{
       /*
        * Input:
        * node - the current DOM element node whose attributes we're checking.
        * nodeName - the name of the node
        *
        * Output:
        * none, the node is modified by reference
        */
       var cAttrs = node.attributes;
       for (var i = 0; i < cAttrs.length; ++i)
       {
               var attr = cAttrs[i];
               var attrName = attr.nodeName.toLowerCase();
               if (!(allowedHTML[nodeName][attrName]))
               {
                       node.removeAttributeNode(cAttrs[i]);
               }
       }
}

function validateComment(root)
{
       /*
        * Check comment for allowed HTML
        * strip invalid HTML and invalid attributes
        *
        * Input:
        * root - root node of comment as xml document
        *
        * Output:
        * returns true if the comment is OK, false if not
        */
       var cNodes = root.childNodes;
       var rv = true;
       for (var i = 0; i < cNodes.length; ++i)
       {
               var node = cNodes[i];
               if (node.nodeType == Node.ELEMENT_NODE)
               {
                       var nodeName = node.nodeName.toLowerCase();
                       if (allowedHTML[nodeName])
                       {
                               validateAttributes(node, nodeName);
                       }
                       else
                       {
                               alert("Forbidden HTML tag <" + nodeName + "> in your comment");
                               rv = false;
                               break;
                       }
                       if (node.hasChildNodes())
                       {
                               rv = validateComment(node);
                               if (rv == false) {
                                       break;
                               }
                       }
               }
       }
       return rv;
}

function checkCommentHTML(commentStr)
{
       /*
        * Input:
        * commentStr - reference to the comment text.
        *
        * Output:
        * validated comment xml as a string or false if the comment is not valid
        */
       if (commentStr.length == 0)
       {
               alert("Comment cannot be blank." );
               return false;
       }
       else
       {
               /* we need a root node, span is a good choice */
               var commentAsXML = "<span>" + commentStr + "</span>";

               if (document.implementation.createDocument)
               {
                       // Mozilla browser of some sort
                       var commentParser = new DOMParser();
                       var doc = commentParser.parseFromString(commentAsXML, "text/xml");
                       var rootNode = doc.documentElement;
                       if ((rootNode.tagName == "parserError") ||
                               (rootNode.namespaceURI ==
"http://www.mozilla.org/newlayout/xml/parsererror.xml"))
                       {
                               var errTextNode = rootNode.firstChild;
                               if (errTextNode.nodeType == Node.TEXT_NODE)
                               {
                                       alert(errTextNode.data);
                               }
                               return false;
                       }
                       else
                       {
                               if (validateComment(rootNode))
                               {
                                       return (new XMLSerializer()).serializeToString(doc);
                               }
                               else
                               {
                                       return false;
                               }
                       }
               }
               else if (window.ActiveXObject)
               {
                       // Internet Explorer, create a new XML document using ActiveX
                       // and use loadXML as a DOM parser.
                       var doc = new ActiveXObject("Microsoft.XMLDOM");
                       doc.async = "false";
                       doc.loadXML(commentAsXML);
                       if (doc.parseError.reason != "")
                       {
                               alert(doc.parseError.reason);
                               return false;
                       }
                       else
                       {
                               if (validateComment(doc.documentElement))
                               {
                                       return doc.xml;
                               }
                               else
                               {
                                       return false;
                               }
                       }
               }
               else
               {
                       alert("What browser are you using???");
               }
       }
}

function showComment(comment)
{
       /*
        * Input:
        * comment - comment as validated xml
        *
        * Output:
        * new browser window that shows comment
        */
       var winComment = window.open("", "Comment", "width=350,height=350");
       winComment.document.write("<html><body>" + comment + "</body></html>");
       winComment.document.close();
}


/* Original simple validation */
//checks for empty fields, only requiring name and comment for now
function isEmpty(strfield1, strfield2, strfield3) {
strfield1 = document.commentform.username.value 
strfield2 = document.commentform.comment.value
strfield3 = document.commentform.useremail.value
  //username field
    if (strfield1 == "" || strfield1 == null || strfield1.charAt(0) == ' ')
    {
    alert("You left the \"Name\" field blank.")
    return false;
    }
  //comment field 
    if (strfield2 == "" || strfield2 == null || strfield2.charAt(0) == ' ')
    {
    alert("You left the \"Comment\" field blank.")
    return false;
    }
  //comment field 
    if (strfield3 == "" || strfield3 == null || strfield3.charAt(0) == ' ')
    {
    alert("You left the \"Email\" field blank.")
    return false;
    }
return true;
}

//function to check valid email address
function isValidEmail(strEmail){
  validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i;
  strEmail = document.commentform.useremail.value;

  // if text found, search email for syntax
	if (strEmail.search(validRegExp) == -1) 
	{
	alert('You entered an invalid e-mail address.');
	return false;
	} 
		return true; 
}


//function that performs all form validation functions
function validate(form){
 if (isEmpty(form)){ 
  if (isValidEmail(form.useremail)){
   if (checkCommentHTML(document.getElementById('comment').value)){
    return true;
   }
  }
 }
return false;
}
