/**
 * FILE        : checkform.js
 * VERSION     : 1.2.2
 * DESCRIPTION : Includes various form validation methods.
 * COPYRIGHT   : 
 *	2003 © Akiman ICS, All Rights Reserved.
 *  	web    : http://www.akiman.com 
 * PLATFORMS   :
 *	The script is cross-browser compatible. 
 *	At least works on NN4up, IE4up and (hopefully) Opera.
 * HISTORY     
 * - 03.08.'03 : v.o.; File created.
 * - 29.08.'03 : v.o.; Functions are re-designed to give flexibility.
 *		Also utility functions are added to script. The code is now 
 *		so called "more" object oriented.
 */
 
/*
 * @import lib/stringmanipulation.js
 */
 
/** Caveat: strMessage is globally shared. */
var strMessage = "";

/**
 * Checks the validity of the date
 * @param strFormName       - The name of the form.
 * @param strEventAlias     - The name of the event.
 * @param strTimeObjectName - PreScript for the time object.
 * @param blRequired        - false means that the date value is not mandatory.
 * @param blnClear          - true resets the calendar when an error occurs.
 */
function checkform_date( strFormName, strEventAlias, strTimeObjectName, blnRequired, blnClear ) {
	var objYear  = eval("document."+strFormName+"."+strTimeObjectName+"Year" );
	var objMonth = eval("document."+strFormName+"."+strTimeObjectName+"Month");
	var objDay   = eval("document."+strFormName+"."+strTimeObjectName+"Day"  );
	if( objDay && objMonth && objYear ) {
		/*
		 * If at least one field is not empty then all fields are required.
		 * So override blnRequired.
		 */
		if( !frm_isBlank(objYear) || !frm_isBlank(objMonth) || !frm_isBlank(objDay)) { blnRequired = true; }
		/* First check for the required fields */
		checkform_required( strFormName, strTimeObjectName+"Day"  , strEventAlias + " day "   , blnRequired );
		checkform_required( strFormName, strTimeObjectName+"Year" , strEventAlias + " year "  , blnRequired );
		checkform_required( strFormName, strTimeObjectName+"Month", strEventAlias + " month " , blnRequired );
		/*
		 * Then, check for an invalid day value such as 31st Feb.
		 * All fields must have a non-empty value for checkform_month to evaluate.
		 */
		if( !frm_isBlank(objYear) && !frm_isBlank(objMonth) && !frm_isBlank(objDay) ) {
			/* clear if blnClear and day value is invalid. */
			checkform_month( strFormName, strEventAlias, strTimeObjectName, blnClear );
		}
		/*
		 * if at least one field is blank (including the above check)
		 * and blnClear is true then clear all.
		 */
		if( frm_isBlank( objDay ) || frm_isBlank( objMonth )|| frm_isBlank( objYear ) ) {
			frm_clear( objDay  , blnClear );
			frm_clear( objMonth, blnClear );
			frm_clear( objYear , blnClear );
		}
	} else {
		alert( "At least one datetime object reference ("+ strTimeObjectName +") is invalid [checkform-001]." );
	}
}

 /**
  * A helper method for checkform_date.
  * Checks whether day value is legal.
  * @param strFormName       - The name of the form.
  * @param strEventAlias     - The name of the event.
  * @param strTimeObjectName - PreScript of the time object.
  * @param blnClear          - true resets the day field value if it is invalid.
  */
function checkform_month( strFormName, strEventAlias, strTimeObjectName, blnClear ) {
	var months = new Array(12);
	months[ 0 ] = 31;months[ 1 ] = 28;months[ 2 ] = 31;months[ 3 ] = 30;months[ 4 ] = 31;months[ 5 ] = 30;
	months[ 6 ] = 31;months[ 7 ] = 31;months[ 8 ] = 30;months[ 9 ] = 31;months[ 10] = 30;months[ 11] = 31;
	var objYear  = eval("document." + strFormName + "." + strTimeObjectName + "Year"  );
	var objMonth = eval("document." + strFormName + "." + strTimeObjectName + "Month" );
	var objDay   = eval("document." + strFormName + "." + strTimeObjectName + "Day"   );
	if( objYear && objMonth && objDay ) {
		var blnRemainder = ( parseInt( objMonth.value ) % 4 ) == 0;
		if( blnRemainder )
			months[ 1 ] = 29;
		else
			months[ 1 ] = 28;
		var intDay = parseInt( objDay.value );
		var intMaxDay = months [ parseInt( objMonth.value ) - 1 ];
		if ( intDay > intMaxDay ) {
			strMessage = "- " + strEventAlias + " day cannot be greater than " + intMaxDay + ".\n";
			frm_focusToObject( objDay, blnClear );
		}
	} else {
		alert("At least one datetime object ("+strTimeObjectName+") is invalid [checkform-002].");
	}
}

 /**
  * Checks for password requirements.
  * The password form should have the following fields:
  *		* intPasswordChk (optional)
  *		* strUserName (required)
  *		* strPassword (required)
  *		* strPasswordConfirm (optional)
  * @param strFormName  - The name of the form.
  * @param intMinLength - Minimum character length for the password. (-1 to discard parameter)
  * @param intMaxLength - Maximum character length for the password. (-1 to discard parameter)
  * @param blnRequired  - false means that password can be empty.
  * @param blnClear     - clears fields if an error occurs.
  */
function checkform_password(strFormName, intMinLength, intMaxLength, blnRequired, blnClear ) {
	var checkboxis  = eval("document."+strFormName+".intPasswordChk" );
	var blnCheckBox = false;
	if(checkboxis) blnCheckBox = true;
	/* if there is no checkbox at all or a checkbox is checked */
	if( !blnCheckBox || checkboxis.checked ) {
		var objUserName        = eval("document." + strFormName + ".strUserName"       );
		var objPassword        = eval("document." + strFormName + ".strPassword"       );
		var objPasswordConfirm = eval("document." + strFormName + ".strPasswordConfirm");
		if( objUserName && objPassword ) {
			/* begin override*/
			if( !frm_isBlank( objPassword ) ) {
				blnRequired = true;
			}
			if( objPasswordConfirm ) {
				if( !frm_isBlank( objPasswordConfirm ) ) {
					blnRequired = true;
				}
			}
			/* end override*/
			checkform_required( strFormName, "strUserName" , "User name", blnRequired );
			checkform_required( strFormName, "strPassword" , "Password" , blnRequired );
			if( objPasswordConfirm ) {
				checkform_required( strFormName, "strPasswordConfirm", "Password confirmation", blnRequired );
			}
			/* check for field lengths */
			var blnBefore = (frm_isBlank( objPassword ));
			checkform_length( strFormName, "strPassword", 
				"Password field " , intMinLength, intMaxLength, false, blnClear );
			var blnAfter = (frm_isBlank( objPassword ));
			/* if there is a confirmation field and password has not been cleared */
			if( blnAfter == false || blnBefore == true ) {
				if( objPasswordConfirm ) {
					//not required.
					//checkform_length( strFormName, "strPasswordConfirm", 
					//	"Password confirmation" , intMinLength, intMaxLength, blnClear );
					/* check for match */
					checkform_match( strFormName, strFormName, "strPassword", 
						"strPasswordConfirm", "Password", "Password confirmation", blnClear );
				}
			}
			/*reassure*/
			if( objPasswordConfirm &&
				( frm_isBlank( objPassword ) || frm_isBlank( objPasswordConfirm ) )
			) {
				frm_clear( objPasswordConfirm, blnClear  );
				frm_clear( objPassword, blnClear );
			}
		} else {
			alert("Username or password object ("+strFormName+")cannot be found [checkform-003].");
		}
	}
}

/**
 * Verifies that a field is an e-mail field.
 * @param strFormName   - The name of the form.
 * @param strFieldName  - The name of the field.
 * @param strFieldAlias - An alias for the field.
 * @param blnClear      - true clears the field content if the mail is invalid.
 */
function checkform_mail( strFormName, strFieldName, strFieldAlias, blnRequired, blnClear ) {
	var objField = eval("document." + strFormName + "." + strFieldName );
	if( objField ) {
		checkform_required( strFormName, strFieldName, strFieldAlias, blnRequired );
		if( ! frm_isBlank( objField ) ) {
			if( ! isEmail(objField.value) ) {
				strMessage += "- " + strFieldAlias + " does not appear to be a valid e-mail.\n";
				frm_focusToObject( objField, blnClear );
			}
		}
	} else {
		alert("Object cannot be found ("+strFormName+","+strFieldName+") [checkform-004].");
	}
}

/**
 * Checks whether the field is numeric and in correct range.
 * @param strFormName   - The name of the form.
 * @param strFieldName  - The name of the form field.
 * @param strFieldAlias - An alias for the field.
 * @param lngLowerBound - Lower bound for the value (a non-numeric value disables it ).
 * @param lngUpperBound - Upper bound for the value (a non-numeric value disables it ).
 * @param blnInteger    - true means the value is integer, false means it is float.
 * @param blnClear      - true clears the field value if an error occurs.
 */
function checkform_numeric( strFormName, strFieldName, strFieldAlias, 
	lngLowerBound, lngUpperBound, blnInteger, blnRequired, blnClear ) {
	var objField = eval("document." + strFormName + "." + strFieldName );
	if( objField ) {
		var strValue  = objField.value;
		var intNumber = parseInt( strValue );/*parseInt('12.22') gives 12*/
		var lngNumber = parseFloat( strValue );
		if( blnInteger ) {
			if( ! isNaN( intNumber ) ) {
				if( ""+intNumber != strValue ) {
					strMessage += "- " + strFieldAlias + " must be an integer.\n";
					frm_focusToObject( objField, blnClear );
				}
				/* implicit conversion */
				objField.value = intNumber;
			} else {
				/*
				 * intNumber is NaN then it is either blank or non-numeric
				 * empty fields can be validated via checkform_required
				 */
				 checkform_required( strFormName, strFieldName, strFieldAlias, blnRequired );
				if( ! frm_isBlank( objField ) ) {
					strMessage += "- " + strFieldAlias + " must be an integer.\n";
					frm_focusToObject( objField, blnClear );
				}
			}
		} else {
			if( ! isNaN ( lngNumber ) ) {
				/* parseFloat("23aaa") give 23. */
				if( ""+lngNumber != strValue ) {
					strMessage += "- " + strFieldAlias + " must be a numeric value.\n";
					frm_focusToObject( objField, blnClear );
				}
				/* implicit conversion */
				objField.value = lngNumber;
			} else {
				checkform_required( strFormName, strFieldName, strFieldAlias, blnRequired );
				if( ! frm_isBlank( objField ) ) {
					strMessage += "- " + strFieldAlias + " must be a numeric value.\n";
					frm_focusToObject( objField, blnClear );				
				}
			}
		}
		/* begin range checking */
		if( ! isNaN( lngNumber ) ) {
			if ( ! isNaN( lngLowerBound ) && ! isNaN( lngUpperBound ) ) {
				if( lngNumber < lngLowerBound || lngNumber > lngUpperBound ) {
					strMessage += "- " + strFieldAlias + " must be greater than " + lngLowerBound + " and less than " + lngUpperBound + ".\n";
					frm_focusToObject( objField, blnClear );
				}
			} else if ( ! isNaN( lngLowerBound ) &&    isNaN( lngUpperBound ) ) {
				if( lngNumber < lngLowerBound ) {
					strMessage += "- " + strFieldAlias + " must be greater than " + lngLowerBound + ".\n";
					frm_focusToObject( objField, blnClear );
				}
			} else if (   isNaN( lngLowerBound ) && ! isNaN( lngUpperBound ) ) {
				if( lngNumber > lngUpperBound ) {
					strMessage += "- " + strFieldAlias + " must be less than " + lngUpperBound + ".\n";
					frm_focusToObject( objField, blnClear );
				}
			} else if (   isNaN( lngLowerBound ) &&   isNaN( lngUpperBound ) ) {
				;
			}
		}
		/* end range checking */
	} else {
		alert( "Field object cannot be found ("+strFormName+","+strFieldName+") [checkform-005]." );
	}
}

/**
 * Checks the length of the form field.
 * @param strFormName   - The name of the form.
 * @param strFieldName  - The name of the field.
 * @param strFieldAlias - An alias for the field name.
 * @param intMinLength  - Minimum field length in characters.
 * @param intMaxLength  - Maximum field length in characters.
 * @param blnClear      - true resets the field values if an error occurs.
 */
function checkform_length( strFormName, strFieldName, strFieldAlias, intMinLength, intMaxLength, blnRequired, blnClear ) {
	var objField = eval( "document." + strFormName + "." + strFieldName );
	if( objField ) {
		/*check whether the value is required or not.*/
		checkform_required( strFormName, strFieldName, strFieldAlias, blnRequired );
		if( !frm_isBlank( objField ) ) {
			var intLength= objField.value.length;
			if( intMinLength >= 0 && intMaxLength >= 0 ) {
				if( intLength < intMinLength || intLength > intMaxLength ) {
					strMessage += "- " + strFieldAlias + " must be at least " + intMinLength + " and at most " + intMaxLength + " characters.\n";
					frm_focusToObject( objField, blnClear );
				}
			} else if ( intMinLength >= 0 && intMaxLength < 0 ) {
				if( intLength < intMinLength ) {
					strMessage += "- " + strFieldAlias + " must be at least " + intMinLength + " characters.\n";
					frm_focusToObject( objField, blnClear );
				}
			} else if ( intMinLength < 0 && intMaxLength >= 0 ) {
				if( intLength > intMaxLength ) {
					strMessage += "- " + strFieldAlias + " can be at most " + intMaxLength + " characters.\n";
					frm_focusToObject( objField, blnClear );
				}
			} else {
				;
			}
		}
	} else {
		alert( "Field object cannot be found ("+strFormName+","+strFieldName+") [checkform-006]." );
	}
}

/** 
 * Verifies that a field is required.
 * @param strFormName   - The name of the form.
 * @param strFieldName  - The name of the field.
 * @param strFieldAlias - An alias for the field.
 * @param blnRequired   - false disables the requirement check.
 */
function checkform_required( strFormName, strFieldName, strFieldAlias, blnRequired ) {

	var objField = eval("document." + strFormName + "." + strFieldName );
	if( objField ) {
		if( frm_isBlank( objField ) && blnRequired ) {
				strMessage += "- " + strFieldAlias + " is required.\n";
				frm_focusToObject( objField, false ) ;
		}
	} else {
		alert("Field object cannot be found ("+strFormName+","+strFieldName+")[checkform-007]." );
	}
}

/**
 * Checks whether two field values are identical.
 * @param strFirstFormName    - The name of the form of the first element.
 * @param strSecondFormName   - The name of the form of the second element.
 * @param strFirstFieldName   - The name of the first field.
 * @param strSecondFieldName  - The name of the seconf field.
 * @param strFirstFieldAlias  - An alias for the first field.
 * @param strSecondFieldAlias - An alias for the second field.
 * @param blnClear            - true, clears all the field content when a mismatch occurs.
 */
function checkform_match( strFirstFormName, strSecondFormName, strFirstFieldName, strSecondFieldName, strFirstFieldAlias, strSecondFieldAlias, blnClear ) 	{
	var objFirst = eval("document."+strFirstFormName +"."+strFirstFieldName );
	var objSecond= eval("document."+strSecondFormName+"."+strSecondFieldName);
	if( objFirst && objSecond ) {
		if( objFirst.value != objSecond.value ) {
			strMessage += "- " + strFirstFieldAlias + " and " + strSecondFieldAlias +
				" do not have identical values.\n";
			frm_focusToObject( objFirst , blnClear  );
			frm_clear( objSecond, blnClear );
		}
	} else {
		alert("Missing objects ("+strFirstFormName+","+strSecondFormName+","+strFirstFieldName+","+strSecondFieldName+")[checkform-008].");
	}
}

/**
 * Checks whether the field is empty or not.
 * @param objField - The field object to be checked.
 * @return true if field is empty or only contains whitespace characters.
 */
function frm_isBlank( objField ) {
	if( objField ) {
		strValue = objField.value;
		strValue = strValue.trim();
		if( strValue == "" ) {
			objField.value = strValue;
			return true;
		} else return false;
	} else {
		alert("Requested object cannot be found! [checkform-009].");
	}
}

/**
 * Focuses to object and clears its contents if requested.
 * @param objField - The object to set focus to.
 * @param blnClear - true clears the content after focus.
 */
function frm_focusToObject( objField, blnClear ) {
	if( objField ) {
		objField.focus();
		frm_clear( objField, blnClear );
	} else {
		alert( "Field object cannot be found [checkform-010]." );
	}
}

/**
 * Sets the value of the object to "".
 * @param objField - The form object field to be set blank.
 * @param blnClear - false makes objField.value remain intact.
 */
function frm_clear( objField, blnClear ) {
	if( objField ) {
		if( blnClear ) {
			objField.value = "";
		}
	} else {
		alert("Requested object cannot be found! [checkform-011]");
	}
}

function checkform_confirm( strFormName, strFieldName, strFieldAlias ) {
	var objField = eval("document."+strFormName+"."+strFieldName);
	if( objField ) {
		if( frm_isBlank( objField ) ) {
			var blnResult;
			blnResult = confirm("Are you sure you want to leave " + strFieldAlias + " blank?" );
			if( !blnResult ) {
				strMessage += "- " + strFieldAlias + " is empty.\n";
				objField.focus();
			}
		}
	} else {
		alert("Object cannot be found ("+strFormName+"."+strFieldName+")[checkform-011].");
	}
	return false;		
}
