//copyright NetAdapt 2002
//checkform 2.0

/**********************************************************************************
Fieldtype-object
	- defines different behaviours of different formelements
**********************************************************************************/

function fieldType(name, arrayField, selectAllowed){

	this.name = name;
	this.isArray = arrayField;
	this.selectAllowed = selectAllowed;

}

/**********************************************************************************
Fieldtypes array
**********************************************************************************/
fieldTypes = new Array();
fieldTypes[0] = new fieldType("text", 0, 1);
fieldTypes[1] = new fieldType("select", 0, 0);
fieldTypes[2] = new fieldType("radio", 1, 0);
fieldTypes[3] = new fieldType("checkboxlist", 1, 0);


/********************************************************************************
checkObject:

	Defines checkfunctions.
	fieldType needs to be predefined in fieldTypes array
	errormessages default to errorMessage when no custom message is given
********************************************************************************/
function checkObject(checkName, fieldType, functionName, errorMessage){

	this.name = checkName;
	for(i=0; i<fieldTypes.length; i++){
		if(fieldTypes[i].name == fieldType){
		
			this.fieldType = fieldTypes[i]; 
			
		}	
	}

	this.msg = errorMessage;
	this.getCheck = eval(functionName);
	this.fieldRef = 0;
}
/******************************************************************************
Utility functions checkNum and checkChar:

	function check each character in a string against a given range
	from the GoodChars-array.

******************************************************************************/

GoodChars = new Array();
GoodChars[0]="1";
GoodChars[1]="2";
GoodChars[2]="3";
GoodChars[3]="4";
GoodChars[4]="5";
GoodChars[5]="6";
GoodChars[6]="7";
GoodChars[7]="8";
GoodChars[8]="9";
GoodChars[9]="0";
GoodChars[10]="a";
GoodChars[11]="b";
GoodChars[12]="c";
GoodChars[13]="d";
GoodChars[14]="e";
GoodChars[15]="f";
GoodChars[16]="g";
GoodChars[17]="h";
GoodChars[18]="i";
GoodChars[19]="j";
GoodChars[20]="k";
GoodChars[21]="l";
GoodChars[22]="m";
GoodChars[23]="n";
GoodChars[24]="o";
GoodChars[25]="p";
GoodChars[26]="q";
GoodChars[27]="r";
GoodChars[28]="s";
GoodChars[29]="t";
GoodChars[30]="u";
GoodChars[31]="v";
GoodChars[32]="w";
GoodChars[33]="x";
GoodChars[34]="y";
GoodChars[35]="z";
GoodChars[36]="A";
GoodChars[37]="B";
GoodChars[38]="C";
GoodChars[39]="D";
GoodChars[40]="E";
GoodChars[41]="F";
GoodChars[42]="G";
GoodChars[43]="H";
GoodChars[44]="J";
GoodChars[45]="I";
GoodChars[46]="K";
GoodChars[47]="L";
GoodChars[48]="M";
GoodChars[50]="N";
GoodChars[51]="O";
GoodChars[52]="P";
GoodChars[53]="Q";
GoodChars[54]="R";
GoodChars[55]="S";
GoodChars[56]="T";
GoodChars[57]="U";
GoodChars[58]="V";
GoodChars[59]="W";
GoodChars[60]="X";
GoodChars[61]="Y";
GoodChars[62]="Z";
GoodChars[63]="_";
GoodChars[64]="-";
GoodChars[65]=".";

function CheckChar(CharX, startChars, endChars){

if(!startChars || startChars == ""){
	var startChars = 0;
	}
if(!endChars || endChars == ""){
	var endChars = GoodChars.length - 1;
	}
	for(j=startChars;j<endChars+1;j++){
		if(CharX == GoodChars[j]){
			return true;
		}
	}
return false;
}

function checkNum(valueToCheck){

if(valueToCheck.length > 0){
	returnValue = true;
		for(x=0;x<valueToCheck.length;x++){
			if(!CheckChar(valueToCheck.charAt(x), 0, 9)){
				returnValue = false;
			}
		}

}else{

	returnValue = false;

}

return returnValue;
}

/******************************************************************************
Checks:

Here you can add your own formchecks if you want to and
create checkType objects in the checkTypes array.
******************************************************************************/
//pre-defined checkfunctions
function defaultCheck(){

	return (this.fieldRef.value != "" && this.fieldRef.value != null && this.fieldRef.value);

}

function intCheck(){

	return (this.fieldRef.value == parseInt(this.fieldRef.value));
}

function checkEmail(){

	adres = this.fieldRef.value;
	
	var A = adres.indexOf("@");
	var B = adres.lastIndexOf(".");
	var C = adres.indexOf(" ");
	var D = adres.length;


	for (x=0;x<D;x++){
		if(x != A){
			if(!CheckChar(adres.charAt(x))){
				return false;
			}
		}
	}
	
	if((A>0) && (B>0) && (B>A+1) && (C<0) && (D>B+2)){
	return true;
	}
	
	return false;
}

function numCheck(){

	return checkNum(this.fieldRef.value);

}

function checkTel_nl(){

	if(this.fieldRef.value.indexOf("-") == 3 || this.fieldRef.value.indexOf("-") == 4){
	
		this.fieldRef.value = this.fieldRef.value.substring(0,this.fieldRef.value.indexOf("-")) + this.fieldRef.value.substring(this.fieldRef.value.indexOf("-")+1, this.fieldRef.value.length);
	
	}

	return (this.fieldRef.value.length == 10 && checkNum(this.fieldRef.value));

}

function checkValuta(){

	if(this.fieldRef.value > 0){
	
		tempVal = this.fieldRef.value * 100;
		if(parseInt(tempVal)){
		
			this.fieldRef.value = Math.round(tempVal)/100;
			return true
		
		}else{
		
			return false;
		
		}
	
	}else{
	
		return false;
	
	}

}

function checkPostcode(){

postcode = this.fieldRef.value;

var D = postcode.length;

	if(D > 7 || (D>6&&postcode.indexOf(" ")!=4) || D<6){

		return false;
	
	}else{
		
		if(D == 7 && postcode.indexOf(" ") == 4){
		
			postcode = postcode.substring(0,4) + postcode.substring(5,7);
			this.fieldRef.value = postcode;
		
		}
	}

	for(x=0;x<4;x++){
		if(!CheckChar(postcode.charAt(x), 0, 9)){
			return false;
			}
		}
		
	for(x=4;x<6;x++){
		if(!CheckChar(postcode.charAt(x), 10, 62)){
			return false;
			}
		}
	return true;
}

function checkDate(){

datevalue = this.fieldRef.value;

var D = datevalue.length;
var dag = parseInt(datevalue.charAt(0)	+ datevalue.charAt(1), 10);
var maand = parseInt(datevalue.charAt(3)	+ datevalue.charAt(4), 10);
var jaar = parseInt(datevalue.charAt(6)	+ datevalue.charAt(7) + datevalue.charAt(8)	+ datevalue.charAt(9), 10);

	if(D != 10){
		return false;
	}		
	
	if(datevalue.charAt(2) != "-" || datevalue.charAt(2) != "-"){ //streepjes op de juiste plaats?
		return false;
	}
	
	if(dag && maand && jaar){		//zijn er wel cijfers ingevoerd?
		if(dag < 1 || dag > 31 || maand > 12 || maand < 1 || jaar < 0){		//vallen de waarden binnen de range waarin datums moeten vallen?
			return false;
		}else{
			if((maand == 4 || maand == 6 || maand == 9 || maand == 11) && dag > 30){	//check of maanden met 30 dagen niet een 31 dag krijgen toegewezen
				return false;
			}
			if(maand == 2 && dag > 29){		//februari mag zelfs maximaal 29 dagen hebben
				return false;
			}else{
				if(!(((jaar % 4 == 0) && (jaar % 100 != 0)) || (jaar % 400 == 0)) && maand == 2 && dag > 28){	//maar als het geen schrikkeljaar is, dan mag februari zelfs maar 28 dagen hebben
					return false;
				}
			}
		}
	}else{
		return false;
	}
	return true;
}

function checkTime(){

timevalue = this.fieldRef.value;

var D = timevalue.length;
	if(D == 5){
		var x = 2;
		var uur = parseInt(timevalue.charAt(0)	+ timevalue.charAt(1), 10);
		var minuten = parseInt(timevalue.charAt(3)	+ timevalue.charAt(4), 10);
	}else{
		if(D == 4){
			var x = 1;
			var uur = parseInt(timevalue.charAt(0), 10);
			var minuten = parseInt(timevalue.charAt(2)	+ timevalue.charAt(3), 10);
		}else{
			return false;
		}
	}
	
	if(timevalue.charAt(x) != ":"){ //dubbele punt op de juiste plaats?
		return false;
	}

	if((uur || (timevalue.charAt(0)==0 && D==4) || (timevalue.charAt(0)==0 && timevalue.charAt(1)==0 && D==5)) && (minuten || (D==4 && timevalue.charAt(2)==0 && timevalue.charAt(3)==0) || (D==5 && timevalue.charAt(3)==0 && timevalue.charAt(4)==0))){		//zijn er wel cijfers ingevoerd?
		if(uur > 23 || minuten > 59){		//vallen de waarden binnen de range waarin een tijdsaanduiding moeten vallen?
			return false;
		}
	}else{
		return false;
	}
	return true;}

function checkRadio(fieldRef){

	optionsNum = fieldRef.length;

	for(x=0; x<optionsNum; x++){
		if(fieldRef[x].checked){
			return true;
		}
	}
	return false;
}

function checkRadio(){

	optionsNum = this.fieldRef.length;

	for(x=0; x<optionsNum; x++){
		if(this.fieldRef[x].checked){
			return true;
		}
	}
	return false;
}

function checkSelect(){

	if(this.fieldRef.options[this.fieldRef.selectedIndex].value == "" || this.fieldRef.options[this.fieldRef.selectedIndex].value == null){
	
		return false;
	
	}else{
	
		return true;
	
	}

}

function checkboxlistCheck(){
	var valueChecked = false;
	
		for(j=0;j<this.fieldRef.length; j++){
			if(this.fieldRef[j].value && this.fieldRef[j].value != "" && this.fieldRef[j].value != null && this.fieldRef[j].checked){
				valueChecked = true;
			}
		}
		
	return valueChecked;
}

function checkFloat(){


	return (this.fieldRef.value > 0);

}

//Checktypes-array: create objects from functions

checkTypes = new Array();
checkTypes[0] = new checkObject("default", "text", "defaultCheck", "U bent vergeten een verplicht veld in te vullen!"); 
checkTypes[1] = new checkObject("int", "text", "intCheck", "Dit is geen heel cijfer!");
checkTypes[2] = new checkObject("email", "text", "checkEmail", "Dit emailadres lijkt niet te kloppen.");
checkTypes[3] = new checkObject("num", "text", "numCheck", "U hoort hier alleen cijfers in te vullen.");
checkTypes[4] = new checkObject("telnl", "text", "checkTel_nl", "Dit is geen Nederlands telefoonnummer.");
checkTypes[5] = new checkObject("valuta", "text", "checkValuta", "Dit lijkt geen valide bedrag.");
checkTypes[6] = new checkObject("postcode", "text", "checkPostcode", "Dit lijkt geen valide nederlandse postcode.");
checkTypes[7] = new checkObject("datum1", "text", "checkDate", "De ingevulde waarde is geen geldige datum volgens notatie dd-mm-jjjj");
checkTypes[8] = new checkObject("time1", "text", "checkTime", "De ingevulde waarde is geen geldige tijdsaanduiding volgens notatie hh:mm");
checkTypes[9] = new checkObject("radio", "radio", "checkRadio", "U moet een keuze maken.");
checkTypes[10] = new checkObject("select", "select", "checkSelect", "U moet een keuze maken.");
checkTypes[11] = new checkObject("checkboxlist", "checkboxlist", "checkboxlistCheck", "U moet een keuze maken.");
checkTypes[12] = new checkObject("float", "text", "checkFloat", "Dit is geen geldig getal.");

/****************************************************************************
Form & Field functions

Defining objects in order to check a specified field.

****************************************************************************/


//repondToError: alerts errorMessage (given or custom) and focusses on field

function respondToError(){ 
	if(!this.fieldRef.disabled){
		if(this.checkType.fieldType.isArray){
			this.fieldRef[0].focus();
		}else{
			this.fieldRef.focus();
		}
		if(this.checkType.fieldType.selectAllowed){
			this.fieldRef.select();
		}
	}
		count = 0;
		alert(this.errorMsg);
}

//evaluates checks

function checkIt(){

	if((this.fieldRef.value == "" || this.fieldRef.value == null || !this.fieldRef.value) && this.defaultValue && this.defaultValue != "" && this.defaultValue != null){
	
		if(this.defaultValue == "null"){
		
			return true;
		
		}else{
		
			this.fieldRef.value = this.defaultValue;
			return true;
		
		}
	
	}else{
	
		this.checkType.fieldRef = this.fieldRef;
		
		return this.checkType.getCheck();
	
	}
	
}


//actual check definition. Defines the form where the field is in, the field itself, the check that should be used and if there's a defaultvalue to be user when the fieldvalue is empty.

function checkField(fieldName, checkType, formName, defaultValue, errorMsg, layerName, N4){

	if(checkType == "" || checkType == null || !checkType){
	
		checkType = "default";
	
	}

	for(i=0; i<checkTypes.length; i++){
	
		if(checkTypes[i].name == checkType){
		
			this.checkType = checkTypes[i];
		
		}
	
	}
	
	formID = (formName == "" || !formName)?0:formName;
	
	if(N4 && layerName && layerName != "" && layerName != null){
		alert(document.layers[0].name);
		this.formRef = document.layers[layerName].document.forms[formID];
	}else{
		this.formRef = document.forms[formID];
	}
	
	this.fieldRef = this.formRef.elements[fieldName];
	if(!errorMsg || errorMsg == "" || errorMsg == null){
		this.errorMsg = this.checkType.msg;
	}else{
		this.errorMsg = errorMsg;
	}
	this.respondToError = respondToError;
	this.checkIt = checkIt;
	this.defaultValue = defaultValue;
}

/**********************************************************************************

Submit functions

**********************************************************************************/

function checkForm(formRef, eventValue, altSubmit, actionFile, targetWindow){

doSubmit = true;

if(fieldsToCheck.length > 0){
	for(i=0; i<fieldsToCheck.length; i++){
	
		if(fieldsToCheck[i].formRef == formRef){
			if(!fieldsToCheck[i].checkIt()){
				fieldsToCheck[i].respondToError();
				doSubmit = false;

				break;
			
			}
		
		}
	
	}
}
	
	if(doSubmit){
	
		if(actionFile){
		
			formRef.action = actionFile;
		
		}
		
		if(targetWindow){
		
			formRef.target = targetWindow;
		
		}
	
		if(eventValue){
		
			formRef.event.value = eventValue;
		
		}
		
		if(altSubmit){
		
			return true;
		
		}else{
		
			formRef.submit();
		
		}
	
	}else{
	
		return false;
	
	}

}

function eventSubmit(formRef, eventValue){

	formRef.event.value = eventValue;
	formRef.submit();

}

//array with all fields that need to be checked. Defined in document.
fieldsToCheck = new Array();



