var fout = new Array();
var foulFormName;

function setFoulForm(formName){
	fout[formName] = new Foul();
	foulFormName = formName;
}

function addTest(testString,message){
	fout[foulFormName].when(testString,message);
}

function listItemTask(formname, varname, id, task, url ) {
	if(formChanged && task!='save' && task!='addValue') {
		alert('please Save or Cancel your changes!');
		return false;
	}	else {
		check = true;
		if(fout[formname]){check = fout[formname].validate(document.forms[formname]);}
		if(check==false) return false;
		var f = document.forms[formname];
		if(!f) alert('De pagina bevat geen form `'+formname+'`!');
		if(url) f.action=url;
	  var i = document.forms[formname].id;
	  if(!i) alert('De pagina bevat geen input: id');
	  i.value = id;
	  i.name=varname;
	  submitform(formname,task);
	  return false;
	}
}
function submitform(formname,pressbutton){
	var t = document.forms[formname].task;
	if(!t) alert('De pagina bevat geen input: task');
	t.value=pressbutton;
	try {document.forms[formname].onsubmit();}
	catch(e){}
	document.forms[formname].submit();
}
function noenter() {
	return !(window.event && window.event.keyCode == 13); 
}

function entersubmit(formname,task,event){
	event = (event) ? event : ((window.event) ? event : null);
	if(event){
		var charCode = (event.charCode) ? event.charCode : ((event.which) ? event.which : event.keyCode);
		if(charCode == 13){
			return listItemTask(formname, '', '', task, '' );
		}
	}
}

var formChanged=false;
		
function checkChanged(){
	if(formChanged) {
		alert('please Save or Cancel your changes!');
		return false;
	} else {
		return true;
	}
}

function showHelp(URL,w,h,scrollbarz){
  var x=(screen.width/2)-(w/2);
  var y=(screen.height/2)-(h/2)-30;
  var helpPopup=window.open('./'+URL+'.php','help','left='+x+',top='+y+',width='+w+',height='+h+',scrollbars='+scrollbarz);
  if (helpPopup) helpPopup.focus();
}

function validate_required(field,alerttxt){
	with (field){
		if (value==null||value==''||value=='aaa')		{
			alert(alerttxt);
			return false;
		}
		else {
			return true;
		}
	}
}

var requirt = new Array();

function validate_form(thisform){
	with (thisform)	{
		for(i=0; i < requirt.length; i++){
			if (validate_required(eval(requirt[i][0]),requirt[i][1])==false) {
				eval(requirt[i][0]).focus();
				return false;
			}
		}
	}
}

function focusFirstTextInput(){
	var theForm = document.getElementById('form');
	if(theForm) {
		var inputs = theForm.getElementsByTagName('input');
		if(inputs) {
			var textFF = 'none';
			for(i=0; i < inputs.length; i++){
				if(inputs[i].type.toLowerCase() == 'text' || inputs[i].type.toLowerCase() == 'password'){
					textFF = i;
					break;
				}
			}
			if(textFF != 'none') {
				try{inputs[textFF].focus();}
				catch(er){}
			}
		}
	}
}

function checkTask(obj){
	if(!obj.task.value) return false;
	else return true;
}

function hashPW(obj){
	if(obj.pass.value && obj.challenge.value){
		for(i=0,val=''; i < obj.pass.value.length; i++) val += '*';
		if(obj.pass.value != val){
			obj.passhashed.value = hex_sha256(obj.challenge.value + obj.pass.value);
			obj.pass.value = val;
		}
	}
}

function omo(obj,onoff){
	startpos = obj.src.lastIndexOf('_')+1;
	endpos = obj.src.lastIndexOf('.');
	newstring = obj.src.substr(0,startpos);
	newstring += onoff;
	newstring += obj.src.substr(endpos);
	obj.src = newstring;
}

function checkMaxLength(obj) {
	var maxLength = obj.getAttribute('maxlength');
	var currentLength = obj.value.length;
	if (currentLength > maxLength) {
		obj.value = obj.value.substr(0,maxLength);
	}
}
