// ------------------------------------------------------
//Title: checkObject.js
//Author: Pia Klein, PEKAN Solution
//copyright2003, Cologne
//You are free to use this script, but please ...
// leave it unchanged and leave this header as it is

//THANK you and goed luck

//needed along with this: 
//  checkWhatYouLikeForm.js 
//  to define the object new ValidForm()
//  and its methods and properties 
//-------------------------------------------------------
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//-------------DON'T CHANGE ANYTHING HERE---------------!
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!



function ValidForm(){
	this.elements=new Array();
	function elementIndex(index,alertText){
		i=this.elements.length;
		this.elements[i]=new Array;
		this.elements[i].index=index;
		this.elements[i].alertText=alertText;
		return i;
		//return this.elements[i].index;
	}
	this.elementIndex=elementIndex;	
	
	
	
	
	
	function dropDown(index){
		i=this.elements[index].length;
		
		this.elements[index][i]=new Array();
		
		this.elements[index][i].type="dropDown";
	}
	this.dropDown = dropDown;
	
	
	function checkNRadio(index,groupMembers){
		i=this.elements[index].length;
		
		this.elements[index][i]=new Array();
		
		this.elements[index][i].type="checkNRadio";
		this.elements[index][i].groupMembers=groupMembers;
	}
	this.checkNRadio = checkNRadio;
	
	function maxLength(index,elementLength){
		i=this.elements[index].length;
		this.elements[index][i]=new Array();
		
		this.elements[index][i].type="maxLength";
		this.elements[index][i].maxLength=elementLength;
	}
	this.maxLength = maxLength;


	function minLength(index,elementLength){
		i=this.elements[index].length;
		this.elements[index][i]=new Array();
		
		this.elements[index][i].type="minLength";
		this.elements[index][i].minLength=elementLength;
	}
	this.minLength = minLength;
	
	function allowedChars(index,allowedChars,charStart,charEnd){
		i=this.elements[index].length;
		
		this.elements[index][i]=new Array();
		
		this.elements[index][i].type="allowedChars";
		this.elements[index][i].allowedChars=allowedChars;
		this.elements[index][i].charStart=charStart;
		this.elements[index][i].charEnd=charEnd;
	}
	this.allowedChars = allowedChars;

	function prohibitedChars(index,prohibitedChars,charStart,charEnd){
		i=this.elements[index].length;
		
		this.elements[index][i]=new Array();
		
		this.elements[index][i].type="prohibitedChars";
		this.elements[index][i].prohibitedChars=prohibitedChars;
		this.elements[index][i].charStart=charStart;
		this.elements[index][i].charEnd=charEnd;
	}
	this.prohibitedChars = prohibitedChars;

	function mandatoryChars(index,mandatoryChars,sequence){
		i=this.elements[index].length;
		
		this.elements[index][i]=new Array();
		
		this.elements[index][i].type="mandatoryChars";
		this.elements[index][i].mandatoryChars=mandatoryChars;
		this.elements[index][i].sequence=sequence;
	}
	this.mandatoryChars = mandatoryChars;

}



function validateForm(form,validForm){
	validElement=true;


	for(i=0;i<validForm.elements.length;i++){
		currentElement=form.elements[validForm.elements[i].index];

		for(j=0;j<validForm.elements[i].length;j++){
		
			properties=validForm.elements[i][j];
			alertText=validForm.elements[i].alertText;
			//alert(i+", "+nawForm.elements[i].index+", "+nawForm.elements[i].alertText)

			if(properties.type=="dropDown"){
				validElement=dropDown();	
			}
			
			if(properties.type=="checkNRadio"){
				validElement=checkNRadio(form,properties.groupMembers);	
			}
			
			if(properties.type=="maxLength"){
				validElement=maxLength(currentElement.value.length,properties.maxLength);	
			}
			
			if(properties.type=="minLength"){
				validElement=minLength(currentElement.value.length,properties.minLength);	
			}			
			
			if(properties.type=="allowedChars"){
				validElement=allowedChars(properties.allowedChars,properties.charStart,properties.charEnd);	
			}
			
			if(properties.type=="prohibitedChars"){
				validElement=prohibitedChars(properties.prohibitedChars,properties.charStart,properties.charEnd);	
			}			
			
			if(properties.type=="mandatoryChars"){
				validElement=mandatoryChars(properties.mandatoryChars,properties.sequence);	
			}			
		if(!validElement){
			break;
		}
		
		}		
		if(!validElement){
			break;
		}
	}
	
	if(!validElement){
		alert(alertText);
		currentElement.focus();
		return validElement; //returns true or false to form (true will cause the form to be submitted)
	}

return validElement;
}

function dropDown(){
	if(currentElement.selectedIndex==0){
		validElement=false;			
	}
	return validElement;
}

function checkNRadio(form,groupMembers){
	
	validElement=false;
	for(cRi=0;cRi<groupMembers.split(",").length;cRi++){
		currentCheckNRadio=form.elements[groupMembers.split(",")[cRi]];
		if(currentCheckNRadio.checked){
				validElement=true;
		}
	}
	return validElement;
}
function maxLength(elementLength,maxLength){
	if(elementLength>maxLength){
		validElement=false;			
	}
	return validElement;
}

function minLength(elementLength,minLength){
	if(elementLength<minLength){
		validElement=false;			
	}
	return validElement;
}

function allowedChars(allowedChars,charStart,charEnd){
	if(charEnd<0){
		charEnd=currentElement.value.length;
	}
	

	for(aCi=charStart;aCi<charEnd;aCi++){
		if(allowedChars.indexOf(currentElement.value.charAt(aCi))==-1){
			validElement=false;
		}
	}				

	return validElement;
}

function prohibitedChars(prohibitedChars,charStart,charEnd){
	if(charEnd<0){
		charEnd=currentElement.value.length;
	}
	

	for(aCi=charStart;aCi<charEnd;aCi++){
		if(prohibitedChars.indexOf(currentElement.value.charAt(aCi))!=-1){
			validElement=false;
		}
	}				

	return validElement;
}

function mandatoryChars(mandatoryChars,sequence){
	if(sequence=="mandatory" && currentElement.value.length!=0){
		validElement=false;
		mCj=0;
		for(mCi=0;mCi<mandatoryChars.length;mCi++){
			for(mCj;mCj<currentElement.value.length;mCj++){
				if(currentElement.value.charAt(mCj)==mandatoryChars.charAt(mCi)){
					validElement=true;
					break;
				}
				else{
					validElement=false;
				}
			}	
		}
	}
					
	if(sequence=="indifferent" && currentElement.value.length!=0){
		for(mCi=0;mCi<mandatoryChars.length;mCi++){
			if(validElement){
				validElement=false;
				for(mCj=0;mCj<currentElement.value.length;mCj++){
					if(currentElement.value.charAt(mCj)==mandatoryChars.charAt(mCi)){
						validElement=true;
						break;
					}
					else{
						validElement=false;
					}
				}
			}	
		}
	}
	return validElement;
}





