//function for checking the validness of an entered string


function fieldValidate(valElement)
{	var valType = valElement.MASK; 
	var valState;  
	if((valType==null) || (valElement.value.length == 0))
		return true;
	
	switch(valType.toLowerCase())
	{	case 'email': valState = isEmail(valElement); break;
		case 'date': valState = isDate(valElement); break;
		case 'zipformat': valState = isZipFormat(valElement); break;
    case 'zip': valState = isZip(valElement); break;
    case 'zipext': valState = isZipExt(valElement); break;
		case 'ssn': valState = isSSN(valElement); break;
		case 'integer': valState = isInteger(valElement); break;
		case 'currency': valState = isCurrency(valElement); break;
		case 'string': valState = isString(valElement); break;
		case 'password': valState = isPassword(valElement); break; 
		case 'phone': valState = isPhone(valElement);break;	
    case 'phoneformat1': valState = isPhoneFormat1(valElement);break;
    case 'alpha': valState = isAlpha(valElement);break; 
		case 'alphanumeric': valState = isAlphaNumeric(valElement);break;
		default: valState = true; 
	}
	return valState;
}
 
 
//Make sure it's an email string
function isEmail(curElement)
{	var pattern = /^\S+@\S+\.\S+$/;
	var testValue = curElement.value;
	var lastChar = testValue.charAt(testValue.length-1);
	var firstChar = testValue.charAt(0);
	if (!(pattern.test(testValue))||(lastChar.match(/\w/)== null)||(firstChar.match(/\w/)== null))
	{	
		errorString = errorString + curElement.title + "(must be a valid email address)\n";
		return false;
	}  
	return true;
}

  //make sure it's a valid aplhabet
function isAlpha(curElement)
{ var testValue = curElement.value;
  var pattern = /^[a-zA-Z ]+$/;
  switch(pattern.test(testValue))
  { case false: errorString = errorString + curElement.title + "(must be a alphabets only)\n"; return false;
    default: return true;
  }
}
	   
function isAlphaNumeric(curElement)
{	//remove any extraneous symbols from the string
	var testValue = curElement.value;  
	curElement.value = testValue = stripCharsFromString(curElement.value,'^a-z A-Z\\d');  
	switch(checkLength(testValue,curElement.MIN,curElement.MAX))
	{	case -1:	errorString = errorString + curElement.title + " (must be more than " + curElement.MIN + " characters long)"; return false;
		case 1:		errorString = errorString + curElement.title + " (must be less than " + curElement.MAX + " characters long)"; return false;
		default: 	return true;
	}
}	 

//Make sure it's a password string
function isPassword(curElement)
{	var pattern = /^[a-zA-Z\d]+$/;
	var testValue = curElement.value;
	switch(pattern.test(testValue))
	{	case false: errorString = errorString + curElement.title + " (must be alpha numeric only)\n"; return false;
		default:
			switch(checkLength(testValue,curElement.MIN,curElement.MAX))
			{ 	case -1:	errorString = errorString + curElement.title + " (must be more than " + curElement.MIN + " characters long)\n"; return false;
				case 1:		errorString = errorString + curElement.title + " (must be less than " + curElement.MAX + " characters long)\n"; return false;
				default: 	return true;
			}
	}
}
	
//Make sure it's a string of a proper length
function isString(curElement)
{	var testValue = curElement.value;
	//if there were specified allowed characters then strip to only them
	if(curElement.ALLOWCHAR!=null)	
		curElement.value = testValue = stripCharsFromString(curElement.value,'^'+curElement.ALLOWCHAR);  
	switch(checkLength(testValue,curElement.MIN,curElement.MAX))
	{	case -1:	errorString = errorString + curElement.title + " (must be more than " + curElement.MIN + " characters long)\n"; return false;
		case 1:		errorString = errorString + curElement.title + " (must be less than " + curElement.MAX + " characters long)\n"; return false;
		default: 	return true;
	}
} 			 				 
		   
//make sure it's a valid SSN
function isSSN(curElement)
{	var testValue = curElement.value;
	var	pattern = /^\d{3}-?\d{2}-?\d{4}$/;
	switch(pattern.test(testValue))
	{	case false: errorString = errorString + curElement.title + "(must be a valid SSN (###-##-####))\n"; return false;
		default: return true;
	}
}

//make sure it's a valid date  	
function isDate(curElement)
{	
	var longMonths = "(01|03|05|07|08|10|12)\\/(0[1-9]|[12]\\d|3[01])";
	var shortMonths = "(04|06|09|11)\\/(0[1-9]|[12]\\d|30)";
	var febCase = "02\\/(0[1-9]|[12]\\d)";
	var years = "(((19)|(20))\\d\\d)";
	var pattern = new RegExp("^(("+longMonths+")|("+shortMonths+")|"+febCase+")\\/"+years+"$");
	var testValue = curElement.value;	
	
	curElement.value = testValue = testValue.replace(/-/g,"/");
		
	//See if it's needs a leading 0
	if(testValue.match("^\\d\\/"))
		curElement.value = testValue = "0"+testValue;
	//see if the month needs a leading 0
	if(testValue.match("^\\d\\d\\/\\d\\/"))
		curElement.value = testValue = testValue.substr(0,3) + "0" + testValue.substr(3,(testValue.length-1));

	if(testValue.match("^\\d\\d\\/\\d\\d\\/\\d\\d$"))
		if(parseInt(testValue.charAt(6))<5)	
			curElement.value = testValue = testValue.substr(0,6) + "20" + testValue.substr(6,2)
		else 
			curElement.value = testValue = testValue.substr(0,6) + "19" + testValue.substr(6,2);
	switch(pattern.test(testValue))
	{	case false: errorString = errorString + curElement.title +  "(must be a valid date in the format mm/dd/yyyy)\n"; return false;
		default:
	}
	if ((testValue.substr(0,2) == '02') && (testValue.substr(3,2)=='29'))
	{	var testYear = parseInt(testValue.substr(6,4));
		if(!(testYear%4==0)||((testYear%100==0)&&!(testYear%400==0)))
		{	errorString = errorString + curElement.title + "(must be a valid date; " + testYear + " is not a leap year)\n";	return false; }
	}
	switch(checkFloatRange(parseDate(testValue),parseDate(curElement.MIN),parseDate(curElement.MAX)))
	{	case -1:	errorString = errorString + curElement.title + "(must be after " + curElement.MIN + ")\n"; return false;
		case 1: 	errorString = errorString + curElement.title + "(must be before " + curElement.MAX + ")\n"; return false;
		default: 	return true;
	} 
}	

function isZipFormat(curElement)
{	var testValue = curElement.value;
	var pattern =  /^\d{5}(-?\d{4})?$/;
	if ((testValue.length == 9)&&(testValue.match("[/\\-/]") == -1))
	{testValue = testValue.substring(0,5) + "-" + testValue.substring(5,9);
	 curElement.value = testValue;
	}
	switch(pattern.test(testValue))
	{	case false: errorString = errorString + curElement.title + " (must be a valid zip code of 5 or 9 digits)\n"; return false;
		default: 	return true;
	}
}

 //make sure it's a valid zip ddddd
function isZip(curElement)
{ var testValue = curElement.value;
  var pattern = /^\d{5}$/;
  switch(pattern.test(testValue))
  { case false: errorString = errorString + curElement.title + "(must be a valid Zip (#####))\n"; return false;
    default: return true;
  }
}

 //make sure it's a valid zip dddd
function isZipExt(curElement)
{ var testValue = curElement.value;
  var pattern = /^\d{4}$/;
  switch(pattern.test(testValue))
  { case false: errorString = errorString + curElement.title + "(must be a valid Zip Ext (####))\n"; return false;
    default: return true;
  }
}

	   
function isCurrency(curElement)
{	var decimals = 2;											
	//remove any extraneous $ or , from the string before working on it
	var testValue = curElement.value;
	curElement.value = testValue = stripCharsFromString(curElement.value,'$,');  
	
	var pattern = new RegExp("^\\d+\\.\\d{1,"+decimals+"}$");
	(testValue.indexOf(".")==-1)?curElement.value = testValue = testValue + ".00":testValue = testValue;
	var decimalPlace = testValue.search(/\./);
	var decPoint = parseInt(decimalPlace + 1) + parseInt(decimals) - parseInt(testValue.length);
		if (decimalPlace != -1)
		{
			for (i = 0 ; i < decPoint ; i++)
			{testValue = testValue + "0";
			 curElement.value = testValue;
			}
		}
	switch(pattern.test(testValue))
	{	case false: errorString = errorString + curElement.title + " (must be a valid currency (example 9.45)\n";	return false;
		default:
			//if there is no MAX set it to 999999.99
			if((curElement.MAX == null)||(curElement.MAX==0))
				curElement.MAX=999999.99
 			switch(checkFloatRange(testValue,curElement.MIN,curElement.MAX))
			{	case -1:	errorString = errorString + curElement.title + " (must be greater than or equal to " + curElement.MIN + ")\n"; return false;
				case 1:		errorString = errorString + curElement.title + " (must be less than or equal to " + curElement.MAX + ")\n"; return false;
				default: return true;	
			}	
	}	
}				 

//Make sure it's a valid integer
function isInteger(curElement)
{	var testValue = curElement.value; 
	var pattern = /^\d+$/;
	switch(pattern.test(testValue))
	{	case false: errorString = errorString + curElement.title + " (must be an integer value)\n";	return false;
		default:
			var curMAX = 2147483647;
			if(curElement.MAX!=null) curMAX = curElement.MAX; 
			//parse off the leading 0's
			var leadingZero = /^0+/;
			if(leadingZero.test(testValue))						
			{
				testValue = testValue.replace(leadingZero,"");
			}		  
			switch(checkFloatRange(parseInt(testValue),curElement.MIN,curMAX))
			{	case 1: 	errorString = errorString + curElement.title + " (must be less than or equal to " + curMAX + ")\n"; return false;
				case -1: 	errorString = errorString + curElement.title + " (must be greater than or equal to " + curElement.MIN +")\n"; return false;
				default: 	return true;
			}
	}
}

//Make sure it contains only possible phone things
function isPhone(curElement)
{	var pattern = /^[\d\(\). -]+$/;
	var testValue = curElement.value;
	switch(pattern.test(testValue))
	{	case false: errorString = errorString + curElement.title + " (must be a valid phone number.)\n"; return false;
		default:
			switch(checkLength(testValue,curElement.MIN,curElement.MAX))
			{ 	case -1:	errorString = errorString + curElement.title + " (must be more than " + curElement.MIN + " characters long)\n"; return false;
				case 1:		errorString = errorString + curElement.title + " (must be less than " + curElement.MAX + " characters long)\n"; return false;
				default: 	return true;
			}
	}
}
  
  //make sure it's a valid phone format ddd-ddd-dddd
function isPhoneFormat1(curElement)
{ var testValue = curElement.value;
  var pattern = /^\d{3}-?\d{3}-?\d{4}$/;
  switch(pattern.test(testValue))
  { case false: errorString = errorString + curElement.title + "(must be a valid Phone (###-###-####))\n"; return false;
    default: return true;
  }
}

