// JavaScript Document
function validate_data() {

	this.hasErrors = 0;
	this.setFocus = false;
	this.defaultType = "Error";
	this.errors = new Array();
	this.types = new Array();
	this.availabletypes = new Array();
	this.inputElements = new Array();
	this.displayElements  = new Array();

	this.PATTERN_CC = "^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6(?:011|5[0-9][0-9])[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\d{3})\d{11})$";
	this.PATTERN_CC_MSC = "^5[1-5][0-9]{14}$";
	this.PATTERN_CC_VSA = "^4[0-9]{12}(?:[0-9]{3})?$";
	this.PATTERN_CC_AME = "^3[47][0-9]{13}$";
	this.PATTERN_CC_DISC = "^6(?:011|5[0-9]{2})[0-9]{12}$";
	this.PATTERN_EMAIL = "^[a-zA-Z_0-9-'\+~]+(\.[a-zA-Z_0-9-'\+~]+)*@([a-zA-Z_0-9-\.]+)+[\.][a-zA-Z]{2,7}$";
	
	this.addError = function(error_returned,type,attachid) {
			this.hasErrors += 1;
			this.addType(type);	
			this.errors[this.errors.length]=error_returned;	
			this.displayElements[this.errors.length-1]=attachid;
			this.types[this.errors.length-1]=type;
	}
	
	
	this.addType = function(type) {
		var exists = false;
		for (var i=0;i<this.types.length;i++) {
			if (type==this.types[i]) {
				exists = true;
			}
		}
		if (!exists) {
			this.availabletypes.push(type);
		}
		
	}
	this.getError = function(type) {
		var temp = new Array();
		for (var i=0;i<this.errors.length;i++) {
			if (type!==null && type!==undefined){
				if (this.types[i]==type) {
					temp.push(this.errors[i]);
				} 
			} else {
					temp.push(this.errors[i]);
			}
		}
		return temp;
	}
	this.getErrorID = function(type) {
		var temp = new Array();
		for (var i=0;i<this.errors.length;i++) {
			if (type!==null && type!==undefined){
				if (this.types[i]==type) {
					temp.push(i);
				} 
			} else {
					temp.push(i);
			}
		}
		return temp;
	}
	
	
	//validation check ******************************/
	
	function luhn(num) {
		num = (num + '').replace(/\D+/g, '').split('').reverse();
		if (!num.length)
			return false;
		var total = 0, i;
		for (i = 0; i < num.length; i++) {
			num[i] = parseInt(num[i])
			total += i % 2 ? 2 * num[i] - (num[i] > 4 ? 9 : 0) : num[i];
		}
		return (total % 10) == 0;
	}
	
	function isObject(obj) {
		if(obj.value!==undefined) {
			return true;
		}
		return false;
	}
	
	this.matchPattern = function(obj,pattern,error_returned,type,attachid) {
		//default values
		error_returned = (error_returned==null) ? "" : error_returned;
		type = (type==null) ? this.defaultType : type;
		attachid = (attachid==null) ? "" :attachid;
		

		var string = (isObject(obj)) ? obj.value : obj;
		var regExp = new RegExp(pattern);
		var results = regExp.test(string);
		
		if (!results) {
			this.addError(error_returned,type,attachid);
			if (isObject(obj)) {			
				this.inputElements[this.inputElements.length]=obj
			}
		}
		return results;
	}
	
	this.isTheSame = function(obj,string2,error_returned,type,attachid) {
		var string = (isObject(obj)) ? obj.value : obj;
		if (string!==string2) {
				this.addError(error_returned,type,attachid);
				this.addInput(obj);
				return false;
		}
		return true;
	}
	
	this.isDifferent = function(obj,string2,error_returned,type,attachid) {
		var string = (isObject(obj)) ? obj.value : obj;
		if (string==string2) {
				this.addError(error_returned,type,attachid);
				this.addInput(obj);
				return false;
		}
		return true;
	}
	
	this.isOneOf = function(obj,array,error_returned,type,attachid) {
		var string = (isObject(obj)) ? obj.value : obj;
		for (var i=0;i>array.length;i++) {
			if (string==array[i]) {
				return true;
			}
		}
		this.addError(error_returned,type,attachid);
		this.addInput(obj);
		return false;
	}
	this.isChecked = function(obj,error_returned,type,attachid) {
		if (obj.length==undefined) {
			if (obj.checked) {
				return true;
			}
		} else {
			for (var i=0;i<obj.length;i++) {
				if (obj[i].checked) {
					return true;
				}
			}
		}
		this.addError(error_returned,type,attachid);
		if (obj.length!==undefined) {
			this.addInput(obj[0]);
		}else {
			this.addInput(obj);
		}

		return false;
		
	}
	
	this.isCreditCard = function(obj,cardtype,error_returned,type,attachid) {
		//default values
		error_returned = (error_returned==null) ? "" : error_returned;
		type = (type==null) ? this.defaultType : type;
		attachid = (attachid==null) ? "" :attachid;
		
		var string = (isObject(obj)) ? obj.value : obj;
		
		var pattern=this.PATTERN_CC;
		switch(cardtype) {
			case "Visa":
				pattern=this.PATTERN_CC_VSA;
				break;
			case "MasterCard":
				pattern=this.PATTERN_CC_MSC;
				break;
			case "Discovery":
				pattern=this.PATTERN_CC_DISC;
				break;
			case "AMEX":
				pattern=this.PATTERN_CC_AME;
				break;
		}
	
		if (this.matchPattern(obj,pattern,error_returned,type,attachid)) {
			if (!luhn(string)) {
				return false;
				this.addError(error_returned,type,attachid);
				this.addInput(obj);
			}
				return true;
		}
		return false;
	
		
	}
	this.lenBetween = function(obj,gt,lt,error_returned,type,attachid) {
		//default values
		error_returned = (error_returned==null) ? "" : error_returned;
		type = (type==null) ? this.defaultType : type;
		attachid = (attachid==null) ? "" :attachid;
		
		var string = (isObject(obj)) ? obj.value : obj;
		string = string.toString().replace(/^\s+|\s+$/g,"");;
		if (!(string.length >= gt && string.length <= lt)) {
			this.addError(error_returned,type,attachid);
			this.addInput(obj);
			return false;
		}
		return true;
		
	}
	
	this.isDate  = function(obj,m,d,y,error_returned,type,attachid) {
		//default values
		error_returned = (error_returned==null) ? "" : error_returned;
		type = (type==null) ? this.defaultType : type;
		attachid = (attachid==null) ? "" :attachid;
		
		m = m - 1; // javascript month range : 0- 11
		var tempDate = new Date(y,m,d);
		var tempYear = (tempDate.getYear() < 1000) ? tempDate.getYear() + 1900 : tempDate.getYear();

		if ( !((tempYear == y) && (m == tempDate.getMonth()) && (d == tempDate.getDate())) ) {	
			this.addError(error_returned,type,attachid);
			this.addInput(obj);
			return false;
		}
		return true;
	}
	
	this.addInput = function(obj) {
		if (isObject(obj)) {
			this.inputElements[this.inputElements.length]=obj;
		}
	}
	
	function isArray(obj) {
  	 if (obj.toString() == "Array")
      return false;
   	else
      return true;
	}
	
	this.isNotEmpty = function (obj,error_returned,type,attachid) {
		error_returned = (error_returned==null) ? "" : error_returned;
		type = (type==null) ? this.defaultType : type;
		attachid = (attachid==null) ? "" :attachid;
		
		if(obj==null || obj.length==0 || obj.value==null || obj.value.length==0) {
			this.addError(error_returned,type,attachid);
			this.addInput(obj);
			return false;
		}
		return true;
	}
	
	
	// display error functions **********************/
	
	this.showErrors = function(type) {
		for (var i=0;i<this.errors.length;i++) {
			if (this.displayElements[i]!==null && this.displayElements[i]!=="") {

				var elem = document.getElementById(this.displayElements[i]);
				if (elem!==null) {

					if (type!==null && type!==undefined){
																
						if ( this.types[i]==type) {
							elem.innerHTML += this.errors[i];
						}
					}
					else {
						elem.innerHTML += this.errors[i];
					}
				}
			}
			
		}
		
	}
	this.showErrorsAlert = function(type) {
		var msg="";
		for (var i=0;i<this.errors.length;i++) {
			if (type!==null && type!==undefined){
				if ( this.types[i]==type) {
					msg+=this.errors[i] +"\n";
				}
					
			}else {
				msg+=this.errors[i] +"\n";
			}
		}
		if (msg.length>0) {
			alert(msg);
		}
		
	}
	
	
}

