
//JC Validation Functions

//[Trim Functions]
String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}
//[/Trim Functions]

function getEl(elId) {
	return document.getElementById(elId);
}


var fields = [];	//global

function vldtField(sId, sNm, bReq, sType, iMin, iMax) {
	
	//Constants
	var vldt_Chars_Alpha		= 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
	var vldt_Chars_Numeric		= '0123456789';

	
	//Constructor
	this.id=		sId;
	this.name=		sNm;
	this.required= 	bReq == null  ? 'false'		: bReq;
	this.type=		sType == null ? 'general' 	: sType;
	/*
	this.minChars=	((iMin==null)  || (iMin=="undefined"))  ? 0			: iMin;
	this.maxChars=	((iMax==null)  || (iMax=="undefined"))	? 1500		: iMax;
	*/
	//i'm gonna allow minChars & maxChars to stay null
	this.minChars=	iMin;
	this.maxChars=	iMax;
	
	/*it's kinda unsafe to do this at THIS point, as the DOM may not be loaded, but
		there's probably a good, safe place to call initValidation() from (e.g.,
		script tags at the botttom of the page...)  this line is not totally necessary,
		as this can be defined in the html
		*/
	//getEl(this.id).maxlength=parseInt(this.maxChars,10);
	
	
	this.checkChars=function(acceptable, testVal) {
		for (var j=0; j<testVal.length; j++) {
			if (acceptable.indexOf(testVal.substr(j,1))==-1) {
				return false;
			}
		}
	return true;
	}
	
	this.validate=function(chkZeroLen) {
		var currVal;
		
		//Late-binding to the control  
		if (getEl(this.id)==null) {
			alert('Error validating - Unbound vldtField Object:\nNo element with id "'+this.id+'" exists.');
			return false;
		} else {
			currVal=getEl(this.id).value.trim();
			//clear out spaces in txt box:
			getEl(this.id).value=currVal;
		}
		
		if (this.required) {
			chkZeroLen = true;	
		}
		//am i empty?  if i'm not required, that's quite all right
		if (((currVal.length==0) && (this.required)) && chkZeroLen) {
			return false;
		} else if ((currVal.length==0) && (!chkZeroLen)) {
			//bail, can't validate empty text
			return true;
		}
		

		//is my length ok???
		if (this.minChars) {  
			//we have a minimum length restriction
			if (currVal.length < this.minChars) {
				return false;	
			}
		}
			
		if (this.maxChars) {
			//we have a maximum length restriction
			if (currVal.length > this.maxChars) {
				return false;
			}
		}
		
		
		//type-specific validation
		switch (this.type) {
			case 'number':
				goodChars = vldt_Chars_Numeric;
				if (!this.checkChars(goodChars, currVal))
					return false;
				break;
			case 'general':
				//test and return false if invalid, otherwise function will continue
					//on and eventually return true
				
				if (currVal.length == 0){
					return false;
				}
				/*
				if (currVal.indexOf("z")!=-1)
					return false; 	//test, invalidate on presence of 'z'
				*/
			break;
			case 'date':
				
				field = getEl(this.id);
				
				
				var dateDelim = "/";
				goodChars = vldt_Chars_Numeric + "/";
				if (!this.checkChars(goodChars, currVal)) {
					return false;
				}
				
				if (currVal.indexOf("/")==-1) {
					//no valid delimiter
					if (currVal.length != 8){
						return false;
					}else{
						var tmpVal = currVal.substr(0,2);
						tmpVal += "/";
						tmpVal += currVal.substr(2,2);
						tmpVal += "/";
						tmpVal += currVal.substr(4,4);
						currVal = tmpVal;
						field.value = currVal;
					}
				}
				
				var dateParts = currVal.split(dateDelim);
				
				if (dateParts.length != 3) {
					//invalid form
					return false;
				}
				
				var m, d, y;
				m = dateParts[0];  d = dateParts[1];  y = dateParts[2]; //YYYY-MM-DD or YYYY/MM/DD
				iM = parseInt(m,10);
				iD = parseInt(d,10);
				iY = parseInt(y,10);
				
				if (m.length != 2 || iM > 12) {
					return false;
				}
				
				if (d.length != 2 || iD > 31) {
					return false;
				}
				
				if ((y.length < 4) || (y.length > 4)) {
					return false;
				}
			
			break;
			
			case 'currency':
				var maxIntPortion = 7;
				var maxDecPortion = 2;
				var intPortion = null;
				var decPortion = null;
				
				goodChars = vldt_Chars_Numeric + ".,";
				if (!this.checkChars(goodChars, currVal)) {
					return false;	
				}
				
				if (parseInt(currVal,10) == 0) return false; //0 amount
				
				if (currVal.indexOf(".")!=-1) {	//we have a decimal!
					var parts = currVal.split(".");
					if (parts.length > 2) {	//we have TOO MANY decimals!
						return false;	
					}
					
					intPortion = parts[0];
					decPortion = parts[1];
				} else {
					intPortion = currVal;	
				}
				
				if (intPortion.length > 7) {
					return false;	
				}
				
				if ((decPortion) && (decPortion.length > 2)) {
					//truncate the decimals
					getEl(this.id).value=intPortion + "." + decPortion.substring(0,2);
				}
				
			break;
			
			case 'email':
				if (currVal.indexOf("@") == -1) return false;	
				
				var emailParts = currVal.split("@"); 
				if (emailParts.length != 2) return false; //invalid form
				
				if (emailParts[1].indexOf(".")==-1) return false; //no domain present
			break;
			
			case 'cardexp':
				var dateDelim = "/";
				goodChars = vldt_Chars_Numeric + "/";
				if (!this.checkChars(goodChars, currVal)) {
					return false;
				}
				
				if ((currVal.length == 4 && currVal.indexOf("/") == -1) || (currVal.length == 5 && currVal.indexOf("/") != -1)) {
					break;
				}else{
					return false;
				}
				
				var dateParts = currVal.split(dateDelim);
				
				if (dateParts.length != 2) {
					//invalid form
					//return false;
				}
				
				var m,y;
				//m = dateParts[0];  y = dateParts[1];
				//iM = parseInt(m,10);
				
							
				if (m.length != 2 || iM > 12) {
					//return false;
				}
				
				
				if (y.length != 2) {
					//return false;
				}	
			break;
		
			case 'fullname':
				if (currVal.indexOf(" ")==-1) return false;
			break;
		}
	//////////////////i'm good!
	return true;
	}

}


function initValidation(group, subgroup) {
	
	if (group == null) group = "default";
	if (subgroup == null) subgroup = "default";
	
	group 	= group.toLowerCase();
	subgroup= subgroup.toLowerCase(); 
	
	switch (group) {
		case "default":
			switch (subgroup) {
				case "default":
					fields = new Array(
									new vldtField("nameF",				 				"First Name",				true,	"general",		null,		null),
									new vldtField("nameL",				 				"Last Name",				true,	"general",		null,		null),
									new vldtField("email",	 							"E-Mail",					true,	"email",		null,		null),
									new vldtField("phone1", 							"Area Code",				true,	"number",		null,		null),
									new vldtField("phone2", 							"Phone Number 1",				true,	"number",		null,		null),
									new vldtField("phone3", 							"Phone Number 2",				true,	"number",		null,		null)
							);
					break;
				}
		break;
	}
}


function checkField(fldId) {
	var i=0;
	var currField;
	while (i<fields.length) {
		if (fields[i].id==fldId) {
			currField=fields[i];
			if (!currField.validate(true)) {
				//alert('invalid field');
			//FIELD IS INCORRECT, WHAT DO YOU WANNA DO?
				//getEl(currField.id).style.borderColor="rgb(255,0,0)";
				getEl(currField.id).style.backgroundColor="rgb(255,94,94)";
				//alert(currField.id + " is invalid.");
			} else {
				//getEl(currField.id).style.borderColor="rgb(0,0,0)";
				getEl(currField.id).style.backgroundColor="rgb(255,255,255)";
			}
		}	
		i++;
	}
}

function checkAllFields() {

	var invalidFields=[];
	
	for (var i=0; i<fields.length; i++) {
	//let's get an array of the invalid fields
		currField=fields[i];
		if (!currField.validate(true)) {
			invalidFields[invalidFields.length]=currField;
			//getEl(currField.id).style.borderColor="rgb(255,0,0)";
			getEl(currField.id).style.backgroundColor="rgb(255,94,94)";
		} else {
			//getEl(currField.id).style.borderColor="rgb(0,0,0)";
			getEl(currField.id).style.backgroundColor="rgb(255,255,255)";
		}
	}
	
	if (invalidFields.length==0) {
		//CONTINUE ON, WE'RE OK
		return true;
	} else { //uh-oh, got invalid...
		
		var msg;
		msg	= "The following fields are required and/or must be valid:\n\n";
		i=0;
		do {
			msg += "\t" + invalidFields[i].name + "\n";
			i++;
		} while (i < invalidFields.length);
		
		alert(msg);
		return false;
	}
}