
// allow field type

var INTEGER = 1;
var DOUBLE = 2;
var STRING = 3;
var DATE = 4;
var SELECT = 5;
var RADIO = 6;
var CHECK = 7;
var EMAIL = 8;
var CUSTOMED = 9;

// declaring the object.

// customed validator
function CustomType(customedMsg,customMethod) {		
	this.dataType = CUSTOMED;	
	this.customedMsg = customedMsg;	
	this.customMethod = customMethod;	
}

// check box validator
function CheckType(formElement, prettyName, customedMsg) {	
	this.formElement = formElement;
	this.dataType = CHECK;	
	this.mandatory = true;		
	this.prettyName = prettyName;
	this.customedMsg = customedMsg;	
}

// select box validator
function SelectType(formElement, prettyName, customedMsg) {	
	this.formElement = formElement;
	this.dataType = SELECT;	
	this.mandatory = true;		
	this.prettyName = prettyName;
	this.customedMsg = customedMsg;	
}

// radio validator
function RadioType(formElement, prettyName, customedMsg) {	
	this.formElement = formElement;
	this.dataType = RADIO;	
	this.mandatory = true;		
	this.prettyName = prettyName;
	this.customedMsg = customedMsg;	
}

//string validator
function StringType(formElement, mandatory, charsNot, prettyName, customedMsg) {	
	this.formElement = formElement;
	this.dataType = STRING;	
	this.mandatory = mandatory;		
	this.prettyName = prettyName;
	this.customedMsg = customedMsg;	
	this.charsNot = charsNot;
}

//date validator
function DateType(formElement, mandatory, prettyName, customedMsg) {	
	this.formElement = formElement;
	this.dataType = DATE;	
	this.mandatory = mandatory;		
	this.prettyName = prettyName;
	this.customedMsg = customedMsg;	
}

//integer validator
function IntegerType(formElement, mandatory, prettyName, customedMsg) {	
	this.formElement = formElement;
	this.dataType = INTEGER;
	this.mandatory = mandatory;			
	this.prettyName = prettyName;
	this.customedMsg = customedMsg;
}

// double validator
function DoubleType(formElement, mandatory, decimalPlace, prettyName, customedMsg) {	
	this.formElement = formElement;
	this.dataType = DOUBLE;
	this.mandatory = mandatory;		
	this.decimalPlace = decimalPlace;
	this.prettyName = prettyName;
	this.customedMsg = customedMsg;
}

// has form object with specific name

function hasFormElement(formElement,elementType,elementName) {
	
	var hasElement=false ;
	
	for (  i=0 ; i < formElement.elements.length ; i++ ) {			
		
		if (( formElement.elements[i].type == elementType ) && 
			( formElement.elements[i].name == elementName ) ) {
			hasElement=true ;
			break;
		}			
	}		
	
	return hasElement;
	
}

function verify(validatorArray) {

	var errorMesg = "";
	
	var arrayLength = validatorArray.length;

	for(i=0;i<validatorArray.length;i++) {
	
		var validator = validatorArray[i];			
				 
		//////////////////////////////////
		// validate integer
		if (validator.dataType == INTEGER) {
			
			if (validator.mandatory) {			
				if (!verifyNotBlank(validator.formElement)) {
					
					if (validator.prettyName != "")	 {
						errorMesg+=validator.prettyName+" is required.\n";	
					}else{					
						errorMesg+=validator.customedMsg+"\n";					
					}
				}		
			} 
			
			if (!verifyInteger(validator.formElement)) {

				if (validator.prettyName != "")	 {
					errorMesg+=validator.prettyName+" must be an integer.\n"	;
				}else{
					errorMesg+=validator.customedMsg+"\n";
				}				
			}
			
			
		//////////////////////////////////
		// validate double			
		} else if (validator.dataType == DOUBLE) {
			
			if (validator.mandatory) {			
				if (!verifyNotBlank(validator.formElement)) {
					
					if (validator.prettyName != "")	 {
						errorMesg+=validator.prettyName+" is required.\n";	
					}else{					
						errorMesg+=validator.customedMsg+"\n";					
					}					
				}		
			} 
			
			if (!verifyDouble(validator.formElement,validator.decimalPlace)) {
				
				if (validator.prettyName != "")	 {
					errorMesg+=validator.prettyName+" must be floating number with "+validator.decimalPlace+" decimal places.\n";	
				}else{					
					errorMesg+=validator.customedMsg+"\n";					
				}				
			}		
				
		//////////////////////////////////
		// validate string			
		} else if (validator.dataType == STRING) {			
			var isValidated = false;	
			
			if (validator.mandatory) {			
				
				if (!verifyNotBlank(validator.formElement)) {
					isValidated = true;					
					if (validator.prettyName != "")	 {
						errorMesg+=validator.prettyName+" is required.\n";	
					}else{					
						errorMesg+=validator.customedMsg+"\n";					
					}	
				}		
			} 

			var stringValue = validator.formElement.value;
			
			if (!isValidated) {
				
				if (validator.charsNot != "") {							
					for(n=0;n<stringValue.length;n++){												
						if (validator.charsNot.indexOf(stringValue.substring(n,n+1))>-1) {							
							errorMesg+=validator.prettyName+" can not contain \""+validator.charsNot+"\"\n";
							break;	
						}						
					}					
				}
			}
			
		
		//////////////////////////////////
		// validate date			
		} else if (validator.dataType == DATE) {
			
			
		//////////////////////////////////
		// validate select			
		} else if (validator.dataType == SELECT) {
			
			if (validator.mandatory) {			
				if (!verifySelect(validator.formElement)) {
					
					if (validator.prettyName != "")	 {
						errorMesg+=validator.prettyName+" is required.\n";	
					}else{					
						errorMesg+=validator.customedMsg+"\n";					
					}					
				}		
			} 			

		//////////////////////////////////
		// validate radio			
		} else if (validator.dataType == RADIO) {

			if (validator.mandatory) {			
				if (!verifyRadio(validator.formElement)) {
					if (validator.prettyName != "")	 {
						errorMesg+=validator.prettyName+" is required.\n";	
					}else{					
						errorMesg+=validator.customedMsg+"\n";					
					}	
				}		
			}			
			
		//////////////////////////////////
		// validate check			
		} else if (validator.dataType == CHECK) {

			if (validator.mandatory) {			
				if (!verifyCheck(validator.formElement)) {
					if (validator.prettyName != "")	 {
						errorMesg+=validator.prettyName+" is required.\n";	
					}else{					
						errorMesg+=validator.customedMsg+"\n";					
					}	
				}		
			}				
			
		//////////////////////////////////
		// validate email			
		} else if (validator.dataType == EMAIL) {
			
			
		//////////////////////////////////
		// validate custom			
		} else if (validator.dataType == CUSTOMED) {			
			
			var result = eval(validator.customMethod);
			
			if (!result) {
				errorMesg+=validator.customedMsg+"\n";
			}
			
		
					
		} else {
			
			
			
		}		
		
	}
	
	if (errorMesg != "") {
		
		errorMesg = "The submission is not successful.                \n"+
					"---------------------------------------\n"+
					"Please correct the following errors:-\n\n"+errorMesg;
		
		alert(errorMesg+"");

	}
	
	
	if (errorMesg != "") {
		return false;	
	} else {
		return true;
	}
	

}	  


///////////////////////////////////////////////////////////////////////////////////
// respective type validator


function verifyInteger(aTextField){
	var textValue = aTextField.value;
		
	if (isNaN(textValue)) {	
		return false;
	} else {
		if (textValue.indexOf(".") > -1) {
			return false
		} else {
			return true;
		}
	}	
}

function verifyDouble(textField,decimalPlace){
	
	var textValue = textField.value;	
	
	var theLen = textValue.length; 
	var dotPos = textValue.indexOf("."); 	
	
	/*This is to cater to no dot value.
	*/
	if (dotPos < 0) {
		dotPos = theLen-1;
	}	
	var thePos = theLen - (dotPos+1);
	var isNum = (isNaN(textValue)) ? false:true; 	
	var isAt = (thePos <= decimalPlace) ? true:false; 	
	var isOne = (textValue.indexOf(".",dotPos+1) == -1) ? true:false; 	
	
	if((isNum) && (isAt) && (isOne)) { 
		return true;	
	} else { 
		return false;	
	} 
	
	
}

function verifyNotBlank(textField) {

	var textValue = textField.value;
	
	if (textValue == "") {
		return false;	
	} else {
		return true;
	}	
	
}

function verifySelect(textField) {

	var textValue = textField.value;
	
	if (textValue == "") {
		return false;	
	} else {
		return true;
	}
	
}


function verifyRadio(radioField) {
	
	for (c=0;c<radioField.length;c++){		
		if(radioField[c].checked){
			return true;
		}		
	}	
	return false;
}

function verifyCheck(checkField) {

	if (checkField.length == null) {
		if (checkField.checked) {
			return true;	
		}		
	} else {
		
		for (c=0;c<checkField.length;c++){		
			if(checkField[c].checked){
				return true;
			}		
		}	
		
	}
	return false;
}

