// this function helps us to check if a straing starts with a specified substring or nots
if(!String.prototype.startsWith){

    String.prototype.startsWith = function (str) {
        return !this.indexOf(str);
    }
}
var abtValidator = {
	init: function (settings) {
		this.settings = settings;
		this.form = document.getElementById(this.settings["formId"]);
		formInputs = this.form.getElementsByTagName("input");

		// change color of inputs on focus
		for(i=0;i<formInputs.length;i++)
		{
			if(formInputs[i].getAttribute("type") != "submit") {
				input = formInputs[i];
				input.style.background = "#EDEDED";
				input.onblur = function () {
					this.style.background = "#EDEDED";
				}
				input.onfocus = function () {
					this.style.background = "#FFFFFF";
				}
			}
		};

			var error = abtValidator.validate();
			if(error == "" || error == null){
				return true;
			} else {
				abtValidator.printError(error);
				return false;
			}

	},
	validate: function () {
		error = '';
		validationTypes = new Array("isRequired", "isEmail", "isNumeric","isAlphaNum","isAlpha","isSymbol");
		for(n=0; n<validationTypes.length; n++) {

                    this.form = document.getElementById(this.settings["formId"]);
                    formInputs = this.form.getElementsByTagName("input");
                    error = abtValidatorInternal(formInputs);
					if(error== "" || error == null){
					selectInputs = this.form.getElementsByTagName("select");
					error = abtValidatorInternal(selectInputs);
					}
					if(error== "" || error == null){
					textareaInputs = this.form.getElementsByTagName("textarea");
					error = abtValidatorInternal(textareaInputs);
					}

		}
		return error;
	},
	printError: function (error) {
		alert(error);
		return false;
	}
};

// returns true if the string is not empty
function isRequired(str){
	if(str == null || str.length == 0){
		return true;
	}
	else{
		return false;
	}
	//return (str == null) || (str.length == 0);
}
// returns true if the string is a valid email
function isEmail(str){
	if(isRequired(str)) return false;
	var re = /^[^\s()<>@,;:\/]+@\w[\w\.-]+\.[a-z]{2,}$/i
	return re.test(str);
}
// returns true if the string only contains characters 0-9 and is not null
function isNumeric(str){
	if(isRequired(str)) return false;
	var re = /[\D]/g
	if (re.test(str)) return false;
	return true;
}

// returns true if the string only contains characters A-Z or a-z
function isAlpha(str){
	if(isRequired(str)) return false;
	var re = /[a-zA-Z]/
	if (re.test(str)) return true;
	return false;
}

// returns true if the string only contains characters 0-9 A-Z or a-z
function isAlphaNum(str){
	if(isRequired(str)) return false;
	var re = /[0-9a-zA-Z]/
	if (re.test(str)) return true;
	return false;
}

// returns true if the string only contains characters OTHER THAN 0-9 A-Z or a-z
function isSymbol(str){
	if(isRequired(str)) return false;
	var re = /[^0-9a-zA-Z]/
	if (re.test(str)) return true;
	return false;
}



// checks if the string is of a specified minimum length or not
function minLength(str,len){
	if(isRequired(str)) return false;
	if(str.length < len){
            return false;
        }
        else{
            return true;
        }
}

// checks if the string is within a specified maximum length or not
function maxLength(str,len){
	if(isRequired(str)) return false;
	if(str.length > len){
            return false;
        }
        else{
            return true;
        }
}


// checks if the string is exactly equal to the specified length or not
function exactLength(str,len){
	if(isRequired(str)) return false;
	if(str.length == len){
            return true;
        }
        else{
            return false;
        }
}

function abtValidatorInternal(formInputs){

		// change color of inputs on focus
	for(i=0;i<formInputs.length;i++){

		classes = formInputs[i].className;

		if(classes == "" || classes== null){
			valid = true;
		}
		var parts = classes.split(" ");

		for(var cCount=0; cCount< parts.length; cCount++){

			if(parts[cCount] == "isRequired"){
				var valu = (formInputs[i].value);
				valid = !isRequired(valu);
				errorMsg = "Please don't leave this field blank";
			}
			else if(parts[cCount] == "isEmail"){
				var valu = (formInputs[i].value);
				valid = isEmail(valu);
				errorMsg = "Please enter a valid email id";
			}
			else if(parts[cCount] == "isNumeric"){
			valid = isNumeric(formInputs[i].value);
			errorMsg = "Please enter a valid number.";
			}
			else if(parts[cCount] == "isAlpha"){
			valid = isAlpha(formInputs[i].value);
			errorMsg = "This field can only contain alphabets and numbers.";
			}
			else if(parts[cCount] == "isAlphaNum" ){
			valid = isAlphaNum(formInputs[i].value);
			errorMsg = "This field can only contain alphabets and numbers.";
			}
			else if(parts[cCount] == "isSymbol"){
			valid = isSymbol(formInputs[i].value);
			errorMsg = "This field cannot contain alphabets and numbers.";
			}
                        else if(parts[cCount].startsWith("minLength")){
                        innerParts = parts[cCount].split("_");
                        valid = minLength(formInputs[i].value,innerParts[1]);
			errorMsg = "Minimum "+innerParts[1]+" characters required";
                        }
                        else if(parts[cCount].startsWith("maxLength")){
                        innerParts = parts[cCount].split("_");
                        valid = maxLength(formInputs[i].value,innerParts[1]);
			errorMsg = "More than "+innerParts[1]+" characters not allowed";
                        }
                        else if(parts[cCount].startsWith("exactLength")){
                        innerParts = parts[cCount].split("_");
                        valid = exactLength(formInputs[i].value,innerParts[1]);
			errorMsg = "This field should have exactly "+innerParts[1]+" characters";
                        }
			else{
				valid = true;
			}
			if(!valid) {
				formInputs[i].style.background = "#FFFF99";
				formInputs[i].style.border = "1px solid #CF3339";
				return errorMsg;

			} else {
				formInputs[i].style.background = "#EDEDED";
				formInputs[i].style.border = '1px solid';
			}
		}

    };

}
