/* 

8/18/2005 Updated
  Added checkTime function to check valid time values with
  or without the AM/PM

5/16/2004 Updated
  Enhanced currency checks to use RegExp as well as enhancing the 
  integer and currency checks to use the intMin and intMax
  values to test the values of the number as opposed to the length.

*/

function cleanInteger(strIn)
{
	var strOut;
	strOut = stripWhitespace(strIn);
	strOut = stripCharsNotInBag(strOut,"0123456789");
	return strOut;
}

// checkInteger (FIELD oFldObject, STRING strError, INTEGER intMin, INTEGER intMax [, BOOLEAN emptyOK])
function checkInteger(oFldObject,strError,intMin,intMax,emptyOK)
{
	var strValue = cleanInteger(oFldObject.value);
	var bolEmptyOk;
	
	if(emptyOK)
		bolEmptyOk=true;
	else
		bolEmptyOk=false;
			
	if(isInteger(strValue,bolEmptyOk) && strValue >= intMin && strValue <= intMax){
		return true;
	} else {
		alert(strError);
		oFldObject.focus();
		oFldObject.select();
		return false;
	}
}

function checkMoney(oFldObject,strError,intMin,intMax)
{
	var strValue = oFldObject.value;
	isPrice = new RegExp("^\\$?(\\d{1,3},?(\\d{3},?)*\\d{3}(\\.\\d{0,2})?|\\d{1,3}(\\.\\d{0,2})?|\\.\\d{1,2}?)$");
	var isValid = isPrice.test(strValue);
	
	if(intMin==0 && stripWhitespace(strValue)=="")
		return true;
	
	if(isValid && ((cleanMoney(strValue) >= intMin) && (cleanMoney(strValue) <= intMax)))
		return true;
	else {
		alert(strError);
		oFldObject.focus();
		oFldObject.select();
		return false;
	}
}

function cleanMoney(strIn)
{
	var strOut;
	strOut = stripWhitespace(strIn);
	strOut = stripCharsNotInBag(strOut,"0123456789.-");
	// look to see if we have a '-' sign in the wrong place if so get rid of it.
	if(isSignedFloat(strOut,true))
	{
		strOut = stripCharsNotInBag(strOut,"0123456789.-");
	}
	strOut = String(parseInt(strOut * 100,10)/100);
	return strOut;
}

function cleanFloat(strIn)
{
	var strOut;
	strOut = stripWhitespace(strIn);
	strOut = stripCharsNotInBag(strOut,"0123456789.");
	strOut = String(parseFloat(strOut));
	return strOut;
}

function checkFloat(oFldObject,strError,intMin,intMax)
{
	var strValue = oFldObject.value;
	
	if(intMin==0 && strValue.length==0)
		return true;
	if(isFloat(strValue) && strValue >= intMin && strValue <= intMax)
		return true;
	else {
		alert(strError);
		oFldObject.focus();
		oFldObject.select();
		return false;
	}
}

// overloaded checkString function..
function checkString(oFldObject,strError,intMin,intMax)
{
	var strValue = stripInitialWhitespace(oFldObject.value);
	
	if(strValue.length >= intMin && (strValue.length <= intMax || intMax == 0))
		return true;
	else {
		alert(strError);
		oFldObject.focus();
		oFldObject.select();
		return false;
	}
}

//Check date using a regular expression
function checkDate(oFldObject,strError,emptyOK)
{
	if ((emptyOK == true) && (isEmpty(oFldObject.value))) return true;
	if(!isDateEx(oFldObject)){
		alert(strError);
		oFldObject.focus();
		oFldObject.select();
		return false;
	}else{
		return true;
	}
}

function isDateEx(oFldObject){
	var re = /^(?:(?:(?:0?[13578]|1[02])(\/|-|\.)31)\1|(?:(?:0?[1,3-9]|1[0-2])(\/|-|\.)(?:29|30)\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:0?2(\/|-|\.)29\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:(?:0?[1-9])|(?:1[0-2]))(\/|-|\.)(?:0?[1-9]|1\d|2[0-8])\4(?:(?:1[6-9]|[2-9]\d)?\d{2})$/
	if(!re.test(oFldObject.value))
		return false;
	else
		return true;
}

//Funciton with check time to be in proper format, pass T/F for withAMPM
function checkTime(oFldObject,strError,withAMPM, emptyOK){

	var strValue = oFldObject.value;
	if(emptyOK==true && stripWhitespace(strValue)=="")
		return true;

	if(withAMPM==true){
		isTime = new RegExp("^([1-9]|1[0-2]|0[1-9]){1}(:[0-5][0-9][aApP][mM]){1}$");
	}else{
		var isTime = new RegExp("^(([0-1]?[0-9])|([2][0-3])):([0-5]?[0-9])(:([0-5]?[0-9]))?$");
	}
	var isValid = isTime.test(strValue);

	if(isValid)
		return true;
	else {
		alert(strError);
		oFldObject.focus();
		oFldObject.select();
		return false;
	}
}

// we will see if the clean function can clean up the phone number into a acceptable format
function checkUSPhone(oFldObject,strError, emptyOK)
{
	var normalizedPhone = stripCharsInBag(oFldObject.value, phoneNumberDelimiters);
    if (!isUSPhoneNumber(normalizedPhone, emptyOK)) {
		alert(strError);
		oFldObject.focus();
		oFldObject.select();
		return false;
	} else {  
		if (normalizedPhone.length > 7)
			oFldObject.value = reformatUSPhone(normalizedPhone)
		
		return true;
	}
}


// we will see if the clean function can clean up the phone number into a acceptable format
function checkUSSSN(oFldObject,strError, emptyOK)
{
	var normalizedSSN = stripCharsInBag(oFldObject.value, SSNDelimiters);
    if (!isSSN(normalizedSSN, emptyOK)) {
		alert(strError);
		oFldObject.focus();
		oFldObject.select();
		return false;
	} else {  
		if (normalizedSSN.length >= 9)
			oFldObject.value = reformatSSN(normalizedSSN)
		
		return true;
	}
}

function checkZipCode(oFldObject,strError, emptyOK) {
	if ((emptyOK == true) && (isEmpty(oFldObject.value))) return true;

	var re = /^[0-9]{5}-[0-9]{4}$|^[0-9]{5}$|^[A-Z][0-9][A-Z][ ]?[0-9][A-Z][0-9]$/
	if(!re.test(oFldObject.value)){
		alert(strError);
		oFldObject.focus();
		oFldObject.select();
		return false;
	}else{
		return true;
	}
}


//Function can check if a value is selected if returnVal==false,
//or it can return the value of the checked radio if returnVal==true.
function checkRadioGrp(oFldObject,strError,returnVal)
{
	if(!returnVal) {
		if(getRadioButtonValue(oFldObject)==null) {
			oFldObject[0].focus();
			alert(strError);
			return false;
		}else{
			return true;
		}
	}else{
		return getRadioButtonValue(oFldObject);
	}
}

function checkCheckboxGrp(oFldObject,strError)
{
	cntChecked=0;
	for (i=0; i<oFldObject.length; i++) {
		if(oFldObject[i].checked)
			cntChecked++;
	}
	if(cntChecked<=0){
		oFldObject[0].focus();
		alert(strError);
		return false;
	}
	return true;
}

function checkCheckBox(oFldObject,strError)
{
	if(!oFldObject.checked){
		oFldObject.focus();
		alert(strError);
		return false;
	}
	return true;
}

function checkSelect(oFldObject,strError)
{
	if(oFldObject.selectedIndex<0 || oFldObject[oFldObject.selectedIndex].value==''){
		oFldObject.focus();
		alert(strError);
		return false;
	} else {
		return true;
	}
}

function checkCardNum(oFldObject,strError, emptyOK) {
	if ((emptyOK == true) && (isEmpty(oFldObject.value))) return true;

	var re = /^(\d{4}-){3}\d{4}$|^(\d{4} ){3}\d{4}$|^\d{16}$/
	if(!re.test(oFldObject.value)){
		alert(strError);
		oFldObject.focus();
		oFldObject.select();
		return false;
	}else{
		return true;
	}
}

function checkEmailEx(oFldObject,strError){
	var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
	
	if(checkString(oFldObject,strError,5,512)){
		if(!re.test(oFldObject.value)){
			alert(strError);
			oFldObject.focus();
			oFldObject.select();
			return false;
		}else{
			return true;
		}
	}else{
		return false;
	}
}