// ****************************************************************************// COPYRIGHT © 2002 Foxfire Printing & Packaging, Inc. All Rights Reserved.//// NAME:         f_StdValidation.js//// PURPOSE:      Browser side standard validation service.//// AUTHOR:       Zuber Jafri// ****************************************************************************var UNDEFINED_TYPE = "undefined";var HTML_TRUE = "true";var HTML_FALSE = "false";var HTML_ATTRNAME_REQUIRED = "myRequired";var HTML_ATTRNAME_DISPLAYNAME = "myDisplayName";var HTML_ATTRNAME_VALIDATIONTYPE = "myValidationType";var HTML_ATTRNAME_VALIDATION_MIN = "myMin";var HTML_ATTRNAME_VALIDATION_MAX = "myMax";var HTML_VALIDATIONTYPE_CHAR = "CHAR";var HTML_VALIDATIONTYPE_DATE = "DATE";var HTML_VALIDATIONTYPE_CURRENCY = "CURRENCY";var HTML_VALIDATIONTYPE_FLOAT = "FLOAT";var HTML_VALIDATIONTYPE_INTEGER = "INTEGER";var HTML_VALIDATIONTYPE_LONG = "LONG";// This is the allowed maximum in text area's.var TEXTAREA_MAXLEN = 100;// This is flag for whether date validation that results in an error// should set the date field to the current date.var m_blnSetDateToCurrent = true;// returns true if value is undefinedfunction isUndefined(pi_Value){	return typeof(pi_Value) == "undefined";}// checks to make sure that the text area doesn't exceed the// maximum length that can be supported by the serverfunction checkTextAreaMaxLen(pi_Control){	var l_lngMaxLen;	if (isUndefined(pi_Control.myMaxLen))		l_lngMaxLen = TEXTAREA_MAXLEN;	else		l_lngMaxLen = pi_Control.myMaxLen;	var s = pi_Control.innerText;	if (s.length > l_lngMaxLen) {		alert("You entered " + s.length + " characters in " + getDisplayName(pi_Control) + ". You can enter up to " + l_lngMaxLen + " characters. Please make the text shorter.");		return false;	}	return true;}// Gets the value of an attribute of the tag. If the attribute is missing,// this returns the empty string.function getAttrValue(pi_objTag, pi_strAttrName){	var attrValue;	attrValue = pi_objTag.getAttribute(pi_strAttrName);	if (typeof(attrValue) == UNDEFINED_TYPE)		return "";	else		return attrValue;}// Gets the display name of a control.// If the control has a display name attribute, the display name is that value.// Otherwise, a display name is constructed from the name of the control (which// is usually the metadata name of the data item displayed in the control).function getDisplayName(pi_objControl){	var strName;	strName = getAttrValue(pi_objControl, HTML_ATTRNAME_DISPLAYNAME);	if (strName == null || strName == "")	{		strName = pi_objControl.name;		// strip off the BO name (the part before the underscore), if any		var i = strName.indexOf("_");		if (i > -1) strName = strName.substr(i+1);	}	return strName;}// Returns a displayable string representation of the date.// e.g. 02-9-5 becomes "2002-9-5"function getDateString(pi_dtmDate){	var dateStr = "";	dateStr += (pi_dtmDate.getMonth()+1) + "/";	dateStr += (pi_dtmDate.getDate()) + "/";	dateStr += (pi_dtmDate.getFullYear());	return dateStr;}// Returns a string representation of the date that can be used// to compare two dates. This representation is not for display,// but renders the date in a format where string comparison will// be the same as if the dates were compared.// e.g. the date 2/9/98 becomes 19980209.function getDateCompareString(pi_dtmDate){	var dateStr;	dateStr = "" + pi_dtmDate.getFullYear();	if (pi_dtmDate.getMonth() < 9)		dateStr += "0";	dateStr +=  (pi_dtmDate.getMonth() + 1);	if (pi_dtmDate.getDate() < 10)      dateStr += "0";    dateStr += pi_dtmDate.getDate();    return dateStr;}// This function is used when you have a date// and want the compare string// NOTE: Assumes previous validationfunction getStringCompareForDate(pi_strDate){	var dtmVal = new Date(pi_strDate);	return (getDateCompareString(dtmVal));}function doOnBlurValidation(pi_objControl){	//var l_strReturn = doStandardValidation_Control_WithOptions(pi_objControl, "SkipRequiredValidation");	var l_strReturn = doStandardValidation_Control_WithOptions(pi_objControl, "");	if (l_strReturn.length > 0) {		l_strReturn = "Please enter:\n\n" + l_strReturn;		pi_objControl.focus();	}	return (l_strReturn);}function doOnBlurValidationWithMessage(pi_objControl){	var l_strMessage;	l_strMessage = doOnBlurValidation(pi_objControl);	if (l_strMessage.length > 0)		alert (l_strMessage);}function setDate(){	var l_Date;	var l_strDate = "";	l_Date = new Date();	l_strDate = l_Date.getFullYear() + "-" ;	l_strDate += (l_Date.getMonth() + 1) + "-";	l_strDate += l_Date.getDate();	return (l_strDate);}function setTime(){	var l_intHour;	var l_intMinutes;	var l_strCorrectTime;	var l_intMidHour = 12;	var l_strAMPM;	var l_Date;	l_Date = new Date();	l_intHour = l_Date.getHours();	if (l_intHour < l_intMidHour)	{		l_strAMPM = "AM";		if (l_intHour == 0)			l_intHour = l_intMidHour;	}	else	{		if (l_intHour != l_intMidHour)			l_intHour = l_intHour - l_intMidHour;		l_strAMPM = "PM";	}	if (l_intHour < 10)		l_intHour = "0" + l_intHour;	l_intMinutes = l_Date.getMinutes();	if (l_intMinutes < 10)		l_intMinutes = "0" + l_intMinutes;	l_strCorrectTime = l_intHour + ":" + l_intMinutes + " " + l_strAMPM;	return (l_strCorrectTime);}function doStandardValidation_Control(pi_objControl){	return doStandardValidation_Control_WithOptions(pi_objControl, "");}function doStandardValidation_Control_WithOptions(pi_objControl, pi_Options){	var strValidationErrors = "";	// get display name	var strControlName = getDisplayName(pi_objControl);	// any value in the control?	var val = "";	if ((pi_objControl.tagName == "INPUT" && pi_objControl.type == "text" ) || (pi_objControl.tagName == "TEXTAREA")) {		val = pi_objControl.value;		} else if (pi_objControl.tagName == "SELECT") {		if (pi_objControl.selectedIndex > -1) {			val = pi_objControl.options[pi_objControl.selectedIndex].value;		}	} else {		// Unknown type of control - just return an empty string		return "";	}	// trim it	val = Trim(val);	if (val == "")	{		// is this a required field?		if (getAttrValue(pi_objControl, HTML_ATTRNAME_REQUIRED) == HTML_TRUE)		{			// yes - are we supposed to skip required field validation?			if (pi_Options.indexOf("SkipRequiredValidation") < 0) {				// yes - we should do required field validation				strValidationErrors += "   - a " + strControlName + "\n";				return strValidationErrors;			}		}	} else {		// there is a value		var strValidationType = getAttrValue(pi_objControl, HTML_ATTRNAME_VALIDATIONTYPE);		switch (strValidationType)		{			case HTML_VALIDATIONTYPE_CHAR:				// nothing to do				break;			case HTML_VALIDATIONTYPE_FLOAT:			case HTML_VALIDATIONTYPE_CURRENCY: // for now, same as float			case HTML_VALIDATIONTYPE_INTEGER:  // for now, same as float			case HTML_VALIDATIONTYPE_LONG: // for now, same as float				var numVal = new Number(val);				// isFinite checks for NaN, +Infinity and -Infinity				// numVal is NaN if not a number; it is Inifinity if the number is too				// big to represent in javascript.				// JavaScript numbers have ranges equal to VB doubles, so if a number is too big				// in JavaScript, it will be too big in VB				if (isFinite(numVal))				{					// it's a valid number - check for min, max					var numMin = getAttrValue(pi_objControl, HTML_ATTRNAME_VALIDATION_MIN);					var numMax = getAttrValue(pi_objControl, HTML_ATTRNAME_VALIDATION_MAX);					if (numMin != null && numMin != "" && numVal < numMin)						strValidationErrors += "   - an " + strControlName + " greater than " + numMin.toString() + "\n";					else if (numMax != null && numMax != "" && numVal > numMax)						strValidationErrors += "   - an " + strControlName + " less than " + numMax.toString() + "\n";				}				else				{					// it's not a valid number					strValidationErrors += "   - a numeric " + strControlName + "\n";				}				break;			case HTML_VALIDATIONTYPE_DATE:				var dtmVal = new Date(val);				var l_strRet = validateDate(val);				if (g_blnValidDate)				{					// it's a valid date - check for min, max					var strMin = getAttrValue(pi_objControl, HTML_ATTRNAME_VALIDATION_MIN);					var strMax = getAttrValue(pi_objControl, HTML_ATTRNAME_VALIDATION_MAX);					var dtmMin, dtmMax;					if (strMin != null && strMin != "")						dtmMin = new Date(strMin);					else						dtmMin = null;					if (strMax != null && strMax != "")						dtmMax = new Date(strMax);					else						dtmMax = null;					if (dtmMin != null && !isNaN(dtmMin) && getDateCompareString(dtmVal) < getDateCompareString(dtmMin))						strValidationErrors += "   - a " + strControlName + " after " + getDateString(dtmMin) + "\n";					else {						if (dtmMax != null && !isNaN(dtmMax) && getDateCompareString(dtmVal) > getDateCompareString(dtmMax))							strValidationErrors += "   - a " + strControlName + " before " + getDateString(dtmMax) + "\n";						else							pi_objControl.value = l_strRet;					}				}				else				{					// it's not a valid date					strValidationErrors += "   - a valid " + strControlName + " - " + l_strRet;					if (m_blnSetDateToCurrent == true)						pi_objControl.value = setDate();				}				break;			}	}	return strValidationErrors;}function isInteger(p_str) {	var numVal = new Number(p_str);	// isFinite checks for NaN, +Infinity and -Infinity	// numVal is NaN if not a number; it is Inifinity if the number is too	// big to represent in javascript.	// JavaScript numbers have ranges equal to VB doubles, so if a number is too big	// in JavaScript, it will be too big in VB	return (isFinite(numVal));}var g_blnValidDate;function validateDate(p_str) {	g_blnValidDate = false;	var l_str = p_str;    if (l_str.length < 8)			return " Use the format: yyyy-mm-dd.\n" ;	if (l_str.substr(4,1)!="-") {		if (l_str.substr(2,1)!="-") {			return " Use the format: yyyy-mm-dd.\n";		} else {			l_str = "20" + l_str;		}	}	if (l_str.substr(7,1)!="-") {		if (l_str.substr(6,1)!="-") {			return (l_str.substr(5,2) + " is not a valid day.\n");		} else {			l_str = l_str.substr(0,5) + "0" + l_str.substr(5);		}	}    if (l_str.substr(7,1) == "-") {       var l_temp = l_str.substr(8) ;        if (l_temp.length < 2)        	l_str = l_str.substr(0,8) + "0" + l_str.substr(8) ;	}	var l_strYear = "";	if (l_str.length == 10) {		l_strYear = l_str.substr(0,4);	} else {		if (l_str.length == 8)			l_strYear = l_str.substr(0,2);		else			return "Use the format yyyy-mm-dd.\n" ;	}	var l_strMonth = l_str.substr(5,2);	var l_strDay = l_str.substr(8,2);	if (!isInteger(l_strYear))		return l_strYear + " is not a valid year.\n";	if (!isInteger(l_strMonth))		return l_strMonth + " is not valid month.\n";	if (!isInteger(l_strDay))			return l_strDay + " is not a valid day.\n";	var l_intDay = parseInt(l_strDay,10);	var l_intMonth = parseInt(l_strMonth,10);	var l_intYear = parseInt(l_strYear,10);	if (l_intYear <100) {		l_intYear = l_intYear + 1900;		if ((l_intYear >= 1900) && (l_intYear < 1930) )			l_intYear = l_intYear + 100;	}	if (l_intYear < MIN_YEAR )		return "Years less than " + MIN_YEAR + " are not supported.\n";	if (l_intYear > MAX_YEAR )		return "Years greater than " + MAX_YEAR + " are not supported.\n";	var l_intMaxDay = 0;	switch(l_intMonth) {		case 1:		case 3:		case 5:		case 7:		case 8:		case 10:		case 12:			l_intMaxDay = 31;			break;		case 4:		case 6:		case 9:		case 11:			l_intMaxDay = 30;			break;		case 2:			var l_blnLeap = false;			if (l_intYear  % 400 == 0)				l_blnLeap = true;			else {				if ((l_intYear % 4 == 0) && (l_intYear % 100 != 0))					l_blnLeap = true;				else					l_blnLeap = false;			}			if (l_blnLeap)				l_intMaxDay = 29;			else				l_intMaxDay = 28;			break;		default:			return "has an invalid month.\n";	}	if ((l_intDay > l_intMaxDay) || (l_intDay < 1))		return l_intDay + " is not a valid day.  Please enter a value equal to or less than " + l_intMaxDay + "\n";	var l_date = new Date(l_intMonth, l_intDay, l_intYear);	if (isNaN(l_date))		return "is an invalid date.\n";	else {		g_blnValidDate = true;		return(l_intYear + "-" + l_intMonth + "-" + l_intDay);	}}function isTime(p_str) {	var l_Hour;	var l_Minute;	var l_Second;	var l_str = p_str;	// Minimum format is H:MMAM	l_str = Trim(l_str);	if (l_str.length < 6)		return false;	// Get Hour	if (l_str.substr(2,1)!=":") {		if (l_str.substr(1,1)!=":") {			return false;		} else {			l_Hour = l_str.substr(0,1);			l_str = l_str.substr(2,l_str.length-2);		}	}	else {		l_Hour = l_str.substr(0,2);		l_str = l_str.substr(3,l_str.length-3);	}	if (l_Hour < 1 || l_Hour > 12)		return false;	// Get Minute	if (l_str.substr(2,1)!=":") {		if (l_str.substr(1,1)!=":") {			l_Minute = l_str.substr(0,2);			l_str = l_str.substr(2,l_str.length-2);		} else {			l_Minute = l_str.substr(0,1);			l_str = l_str.substr(1,l_str.length-1);		}	}	else {		l_Minute = l_str.substr(0,2);		l_str = l_str.substr(2,l_str.length-2);	}	if (l_Minute < 0 || l_Minute > 59 )		return false;	// Check to see if that is all they entered.	if (l_str.substr(0,1) == ":") {		l_Second = l_str.substr(1,2);		l_str = l_str.substr(3,l_str.length-3);		if (l_Second < 0 || l_Second > 59)			return false;	}	l_str = Trim(l_str);	if (l_str.length < 2)		return false;	// MI accept any case. Tracker 686	l_str = l_str.toUpperCase()	if (l_str.substr(0,2)!="AM" && l_str.substr(0,2)!="PM" )		return false;	return true;}// This function does standard validation (required, validation type, min, max) on// all the controls in the supplied collection.// The return value is a string listing all the validation errors, or an empty// string if no validation errors were there.function doStandardValidation_Collection(pi_colControls){	var strValidationErrors = "";	var iii;	for (iii=0; iii<pi_colControls.length; iii++)	{		var objControl = pi_colControls[iii];		strValidationErrors += doStandardValidation_Control(objControl);	}	return strValidationErrors;}function doStandardValidation_Collection_WithOptions(pi_colControls, pi_strOptions){	var strValidationErrors = "";	var iii;	for (iii=0; iii<pi_colControls.length; iii++)	{		var objControl = pi_colControls[iii];		strValidationErrors += doStandardValidation_Control_WithOptions(objControl, pi_strOptions);	}	return strValidationErrors;}function doStandardValidation_WithOptions(pi_frm, pi_strOptions){	var colControls;	var strValidationErrors = "";	// "INPUT" tags	colControls = document.all.frm.elements.tags("INPUT");	strValidationErrors += doStandardValidation_Collection_WithOptions(colControls, pi_strOptions);	// "SELECT" tags	colControls = document.all.frm.elements.tags("SELECT");	strValidationErrors += doStandardValidation_Collection_WithOptions(colControls, pi_strOptions);	// "TEXTAREA" tags	colControls = document.all.frm.elements.tags("TEXTAREA");	strValidationErrors += doStandardValidation_Collection_WithOptions(colControls, pi_strOptions);	// return the Errors	if (strValidationErrors != "")		strValidationErrors = "Please enter:\n\n" + strValidationErrors;	return strValidationErrors;}function doStandardValidation(pi_frm){	var colControls;	var strValidationErrors = "";	// "INPUT" tags	colControls = document.all.frm.elements.tags("INPUT");	strValidationErrors += doStandardValidation_Collection(colControls);	// "SELECT" tags	colControls = document.all.frm.elements.tags("SELECT");	strValidationErrors += doStandardValidation_Collection(colControls);	// "TEXTAREA" tags	colControls = document.all.frm.elements.tags("TEXTAREA");	strValidationErrors += doStandardValidation_Collection(colControls);	// return the Errors	if (strValidationErrors != "")		strValidationErrors = "Please enter:\n\n" + strValidationErrors;	return strValidationErrors;}
