function getF(F){
  var A=[];
  if(F){
    var f=(typeof F=="object")? F : document.forms[F];
    for(var i=0; i<f.elements.length; i++) A[A.length] = f.elements[i];
  }
  else
  for(var j=0;j<document.forms.length;j++)
    for(i=0;i<document.forms[j].elements.length;i++)
      A[A.length]=document.forms[j].elements[i];
  return A;
}


function getFBy(p,v,F){
  var A=(F) ? getF(F) : getF();
  for(var i=0; i<A.length; i++)
    if(A[i].p == v) return a[i];
  return false;
}


var formInValid=0;
var fieldIndex=0;

function process(F){return ValidateForm(F)}

function ValidateForm(F){
  var A = getF(F);
  var i = 0;
  formInValid = 0;
  while(i<A.length && formInValid==0 ){
    A[i].style.borderColor='';
    checkfield(A[i++]);
  }
  if(formInValid != 0) return false;
  else return processKeys(F);
}

function invalidate(o){
  formInValid = 1;
  o.focus();
  o.style.borderColor = 'red';
}  

function checkfield(o){
  if(o.getAttribute){
    var req = o.getAttribute("required");
    if(req != null && o.value != null){
      switch(req){
        case 'string': if(o.value.search(/^\w+( \w+)?$/) == -1) invalidate(o); break;
        case 'email': if(o.value.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) == -1) invalidate(o); break;
        case 'invoer': if(o.value=='') invalidate(o); break;
      }
    }
  }
}

function stripEmpty(s){
  if(/\n$/.test(s)){ //test op eindstandige line-brakes
    s = s.slice(0,s.length-2);
    return stripEmpty(s);
  }
  else return s;
}

function processKeys(F){
  var A = getF(F);
  var f = F.elements['invoer'];
  if(f){
    var cmt = stripEmpty(f.value);
    for(var i=0; i<A.length; i++){
      var a = A[i];
      if(a.getAttribute){
        var kv = a.getAttribute("keyvalue");
        if(kv != null){
          if( a.type=="invoer" && a.value!="") cmt += ("\n" + a.name + "=" + a.value);
          else if( a.type=="checkbox" && a.checked==true) cmt += ("\n" + a.name + "=" + a.value);
        }
      }
    }
    f.value = cmt;
  }
  return true;
}


 

(function(){
	fMsg = {
	  author : 'Fill out your name in this field. :: Example: &quot;John Doe&quot;',
	  email : 'Fill out your email adress in this field.  Example: johndoe&#64;hotmail&#46;com.  Read our anti-spam policy in the privacy statement',
	  url : 'Fill out the url of your homepage in this field (optional).  If you fill out this field, your name will link to your website instead of your e-mail',
	  location : 'fill out the \'city\' and \'country\' ( + \'state\' ) where you come from. :: For example: Leuven, Belgium',
	  invoer : 'your entry : required field',
	  post : 'submit my entry'
	};

	var Ar = [];
		Ar.push('<form id="form" method="post" action="',MT.CGIPath,'mt-comments.cgi" name="form" onsubmit="javascript:return process(this);">');
		Ar.push(  '<table border=0 cellspacing=0 cellpadding=0 width="350"><tr align="left" height="0"><td width="50"></td><td width="200"><input type="hidden" name="static" value="1"><input type="hidden" name="entry_id" value="', MT.EntryID, '"></td><td width="100"></td></tr>');
		Ar.push(    '<tr align="left" height="25"><td><label for="author">Name:</label></td><td><input id="author" class="blur" size="30" name="author" memo required="string"  title="', fMsg.author ,'"></td><td><font size="1"> (required!)</font></td></tr>');
		Ar.push(    '<tr align="left" height="25"><td><label for="email">Email:</label></td><td><input id="email" class="blur" size="30" name="email" memo required="email"  title="', fMsg.email,'"></td><td><font size="1"> (required!)</font></td></tr>');
		Ar.push(    '<tr align="left" height="25"><td><label for="url">URL:</label></td><td><input id="url" class="blur" size="30" name="url" memo  title="', fMsg.url ,'"></td></tr>');
		Ar.push(    '<tr align="left" height="25"><td><label for="location">Location:</label></td><td colspan="2"><input class="blur" size="30" style="width:100%" id="location" name="location" memo keyvalue title="', fMsg.location,'" /></td></tr>');
		Ar.push(    '<tr align="left" height="25"><td><label for="text">Message:</label></td><td><font size="1"> (required!)</font></td></tr>');
		Ar.push(    '<tr align="left"><td colspan="3"><textarea id="invoer" name="invoer" class="blur" rows="10" cols="60" required="text" title="', fMsg.invoer,'"></textarea></td></tr>');
		Ar.push(    '<tr align="left" height="25"><td><br><input class="button" value="POST" type="submit" id="post" name="post" title="', fMsg.post,'"></td></tr>');
		Ar.push(  '</table>');
		Ar.push('</form>');

	document.write(Ar.join('\n'));
	
		Ar = null;
})();

