// create object
function formValidator()
{
	// set up array to hold error messages
	this.errorList = new Array;
	// set up object methods
	this.isEmpty = isEmpty; 
	this.isNumber = isNumber; 
	this.isAlphabetic = isAlphabetic; 
	this.isAlphaNumeric = isAlphaNumeric; 
	this.isWithinRange = isWithinRange; 
	this.hasMinimumLength = hasMinimumLength;
	this.hasMinValue = hasMinValue;
	this.hasMaxValue = hasMaxValue;
	
	
	this.isEmailAddress = isEmailAddress; 
	this.isZipCode = isZipCode;
	this.isPassword = isPassword;
	this.isChecked = isChecked;  
	this.isCreditCard = isCreditCard;
	this.isCCV = isCCV;
	this.raiseError = raiseError; 
	this.numErrors = numErrors; 
	this.isPhone = isPhone; 
	this.isInteger = isInteger;
	
	//functons that will display errors
	this.displayErrors = displayErrors; 
	this.displayErrorsV2 = displayErrorsV2; 

}

// check to see if input is whitespace only or empty
function isEmpty(val)
{
	if (val.match(/^\s+$/) || val == "")
	{
		return true;
	}
	else
	{
		return false;
	} 
}

// check to see if input is number
function isNumber(val)
{
	if (this.isEmpty(val))
		return false;
	if (isNaN(val))
	{
		return false;
	}
	else
	{
		return true;
	} 
}

// check to see if input is alphabetic
function isAlphabetic(val)
{
	if (val.match(/^[a-zA-Z ]*$/))
	{
		return true;
	}
	else
	{
		return false;
	} 
}
function isInteger(val)
{
	if (val.match(/^([ ]*)([a-zA-Z0-9]+)([ ]*)$/))
	{
		return true;
	}
	else
	{
		return false;
	} 
}

// check to see if input is alphanumeric
function isAlphaNumeric(val)
{
	if (val.match(/^[a-zA-Z0-9]+$/))
	{
		return true;
	}
	else
	{
		return false;
	} 
}

// check to see if input is alphanumeric
function isPassword(val)
{
	if ( this.hasMinimumLength(val,  6) && this.isAlphaNumeric(val))
	{			
		return true;
	}
	else
	{
		return false;
	} 
}

function hasMinimumLength(val, min)
{
	if(val.length <min)
		return false;
	else
		return true;
}

function isZipCode(val)
{
	return 	(this.isNumber(val) && this.hasMinimumLength(val,5))	
}

function isCreditCard(val)
{
	if(this.isNumber(val) && (val.length==15 || val.length==16))
		return true;
	else
		return false;
}

function isPhone(val)
{
	if (this.isEmpty(val))
		return true;
	if(val.match(/^\s*\d{3}\s*-?\s*\d{3}\s*-?\s*\d{4}\s*$/) || val.match(/^\s\(*\d{3}\)\s*\d{3}\s*\d{4}\s*$/))
	{
		val = val.replace(/[^0-9]/gi, "");
		if(this.isNumber(val) && val.length==10)
			return true;
		else
			return false;
	}
	else
	return false;
	
}
function isCCV(val)
{
	if(this.isNumber(val) && (val.length==3 || val.length==4))
		return true;
	else
		return false;
	
}
// check to see if value is within range
function isWithinRange(val, min, max)
{
	if (val >= min && val <= max)
	{
		return true;
	}
	else
	{
		return false;
	} 
}

// check to see if value is within range
function hasMinValue(val, min)
{
	if (!isNaN(val) && val >= min )
	{
		return true;
	}
	else
	{
		return false;
	} 
}

// check to see if value is less than maximum
function hasMaxValue(val, max)
{
	if (!isNaN(val) && val <= max )
	{
		return true;
	}
	else
	{
		return false;
	} 
}
// check to see if input is a valid email address
function isEmailAddress(val)
{
	if (this.isEmpty(val))
		return true;	
	if (val.match(/^([a-zA-Z0-9])+([.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-]+)+/))
	{
		return true;
	}
	else
	{
		return false;
	} 
}
// check to see if form value is checked
function isChecked(obj)
{
	if (obj.checked)
	{
		return true;
	}
	else
	{
		return false;
	} 
}

//add the error texts
function raiseError(msg, element)
{
	n = this.errorList.length;
	this.errorList[n] = new Array(msg, element);
}

//return the number of errors
function numErrors()
{
	return this.errorList.length;
}
// display all errors
// iterate through error array and print each item
function displayErrors()
{
	errors = "Error: ";
	for (x=0; x<this.errorList.length; x++)
	{
		errors = errors + "\n" + this.errorList[x][0];	
	}
	alert("ttt"+errors);
}

function displayErrorsV2()
{
	errors = "Error: ";
	for (x=0; x<this.errorList.length; x++)
	{	
		if(document.getElementById(this.errorList[x][1]))
		{
			document.getElementById(this.errorList[x][1]).style.borderColor='#AC231A';
		}
		if(document.getElementById(this.errorList[x][1]+ '_error'))
			document.getElementById(this.errorList[x][1]+ '_error').innerHTML =this.errorList[x][0];
	}
}


// end object
