/*
# class login
# validated login form
*/
GI.FormValidator = function(){
	
	/*
	# stores an array of objects containing field id
	# and type of validateion to perform
	*/
	this.required = new Array();
	
	/*
	# stores an array of validation types available
	*/
	this.validTypes = ['string','email'];
	
	/*
	# validation errors are stored here 
	*/
	this.errors = new Array();
	
}

/*
# add a required field
*/
GI.FormValidator.prototype.addRequired = function(strID,strType,strMsg){
	
	/*
	#  check for null values
	*/
	if(strID==null)return false;
	
	/*
	#  populate defaults
	*/
	if(strType==null)strType='string';
	if(strMsg==null)strMsg='Please complete all required fields to continue';
	
	/*
	#  check type exists
	*/
	if(!this.isValidType(strType))return false;
	
	/*
	#  add to the required array
	*/
	this.required.push({id:strID,type:strType,msg:strMsg});
	
	/*
	# no problems, return true
	*/
	return true;
	
}

/*
# drop a required field
*/
GI.FormValidator.prototype.dropRequired = function(strID){
	
	/*
	#  check for null values
	*/
	if(strID==null)return false;
	
	/*
	# find and splice out the matching element
	*/
	for(a=0;a<this.required.length;a++)
	if(this.required[a].id==strID)this.required.splice(a,1);
	
	/*
	# no problems, return true
	*/
	return true;
	
}

/*
# check the field type
*/
GI.FormValidator.prototype.isValidType = function(strType){
	
	/*
	#  check for null values
	*/
	if(strType==null)return false;
	
	/*
	#  check that the type exists in our types array
	*/
	var hasFound=0;
	for(a=0;a<this.validTypes.length;a++)if(this.validTypes[a]==strType)hasFound=1;	
	
	/*
	#  return the results
	*/
	return hasFound<1?false:true;
}

/*
# function to check each individual submission
*/
GI.FormValidator.prototype.isValidSubmission = function(objRequired){
	
	/*
	#  check for null values
	*/
	if(objRequired==null)return false;
	
	/*
	#  collect the dom element
	*/
	var domElement = $(objRequired.id);
	
	/*
	#  use a switch to validate fot correct type
	*/
	switch(objRequired.type){
	
		case 'string':
			/*
			#  validate a string
			*/
			if(domElement.value.length<1){
				this.errors.push(objRequired);
				return false;
			}
		break;
		
		case 'email':
			/*
			#  validate an email
			*/
			var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
			if(reg.test(domElement.value) == false ){
				this.errors.push(objRequired);
				return false;
			}
		break;
		
		case 'url':
			/*
			#  validate an url
			*/
			var reg = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;
			if(reg.test(domElement.value) == false ){
				this.errors.push(objRequired);
				return false;
			}
		break;
		
	}
	
	/*
	#  no problems, return true
	*/
	return true;
}

/*
# function to call from submit
*/
GI.FormValidator.prototype.validate = function(){
	//var c= '';
	//for(a=0;a<this.required.length;a++)c+=this.required[a].id+'\r\n';
	//alert(c);
	/*
	# clear the array
	*/
	this.errors = new Array();
	
	/*
	#  check each requirement
	*/
	for(a=0;a<this.required.length;a++)this.isValidSubmission(this.required[a]);
	
	/*
	#  if no errors return truw
	*/
	if(this.errors.length<1)return true;
	
	/*
	#  else return our error function
	*/
	return this.onError();
	
}

/*
# default validateion error
# for this purpose we can just return the first error
*/
GI.FormValidator.prototype.onError = function(){
	
	/*
	# inform the user
	*/
	alert(this.errors[0].msg);
	
	/*
	# focus on the error
	*/
	$(this.errors[0].id).focus();
	
	/*
	# stop the submission
	*/
	return false;
}
