// form validation code
// (c) 2005 dave thompson, deepmedia solutions ltd
// unauthorised use is prohibited by law

// commence object definition

//var checkObject=new Object();

function checkObject(objName_, niceName_, fieldType_) {
	this.objName=objName_;
	this.niceName=niceName_;
	this.fieldType=fieldType_;
}

function checkObject_toString2() {
	return "Item:"+this.objName;
}

checkObject.prototype.toString=checkObject_toString2;

// constants
var TYPE_NORMAL=0;
var TYPE_EMAIL=1;

// end object definition

var objs=new Array();

function pushOn(fieldName, niceName, fieldType) {
	var newObj=new checkObject(fieldName, niceName, fieldType);
	objs.push(newObj);
}

function checkItem(w, objRef) {
	w=w.replace(/^\s*|\s*$/g,"");
	if(objRef.fieldType==TYPE_EMAIL) {
		return ((w.indexOf(".") > 2) && (w.indexOf("@") > 0));
	}else if(objRef.fieldType==TYPE_NORMAL) {
		return !((w=="")||(w=="{nosel}"));
	}
}
	
function checkForm() {
	var errors="";
	var errorCount=0;
	var w="";
	
	for(i=0;i<objs.length;i++) {
		if(document.getElementById(objs[i].objName)!=null) {
			w=document.getElementById(objs[i].objName).value;
			if(!checkItem(w,objs[i])) {
				if(errorCount<5) {
					if(objs[i].fieldType==TYPE_EMAIL) {
						errors+=">> The '"+objs[i].niceName+"' field does not contain a valid e-mail address.\n";
					}else if(objs[i].fieldType==TYPE_NORMAL) {
						errors+=">> The '"+objs[i].niceName+"' field does not contain valid information.\n";
					}
					//if (objs[i].niceName.charAt(0)=='!') {
						//errors+=">> "+objs[i].niceName.substr(1)+"\n";
					//}else{
						//errors+=">> '"+objs[i].niceName+"' field has been left empty.\n";
					//}
				}
				
				errorCount++;
			}
		}
	}

	if(errorCount>0) {
		var finalString="The following errors were detected:\n\n"+errors;
		if(errorCount>5) {
			finalString+="\n\n... and "+(errorCount-5)+" more.";
		}
		alert(finalString);
	}
	
	return (errorCount==0);
}
