/**
 @file
 Form validation functions.
 Copyright(C) 2003, Arcle Technologies.
 @version $Id: form_validations.js,v 1.4 2003/06/11 11:25:27 adib Exp $
*/

/**
 Mappings between the validation types and the function names.
*/
var validationTypes = Array();
validationTypes["numeric"] = isNumeric;
validationTypes["integer"] = isInteger;
validationTypes["email"] = isEmail;
validationTypes["purchaseOrderID"] = isPurchaseOrderID;
validationTypes["any"] = new Function("dummy", "return true;");


/**
 Validate the fields in a form.
 @param formName    The name of the form.
 @param fieldDefs   Field validation definitions. It is a string-indexed
                    array where the indexes are the field names of the form
                    and the values ValidationEntry objects.
 @return true if all the fields in the form is valid, false if there is
         at least one field that failed validation.
*/
function validateFields(formName, fieldDefs)
{		
    for (fieldName in fieldDefs) {
        entry = fieldDefs[fieldName];
        validateFunc = validationTypes[entry.validationType];
        value = document.forms[formName].elements[fieldName].value;				        
        isOK = true;
        if (entry.isRequired && value.length == 0) {
            isOK = false;
        } else {
            isOK = validateFunc(value);
        }
        if (!isOK) {
            alert(entry.errorMessage);            
            document.forms[formName].elements[fieldName].focus();		
            return false;
        }
	}
	return true;
}

/**
 Validate two fields wheter their values are match or not.
 Usually used for password confirmation
 @param formName    The name of the form.
 @param fieldDefs   Field validation definitions. It is a string-indexed
                    array where the indexes are the field names of the form
                    and the values ValidationEntry objects.
 @param field1      The first field
 @param field2      The second field                    
 @return true if all the fields in the form is valid, false if there is
         at least one field that failed validation.
*/
function validateEquals(formName, fieldDefs, field1, field2)
{  
   entry  = fieldDefs[field2];
   
   value1 = document.forms[formName].elements[field1].value
   value2 = document.forms[formName].elements[field2].value 
   
   if (value1!=value2) {
          alert(entry.errorMessage);
          document.forms[formName].elements[field2].focus();
          return false;
   }
   else return true;      
}

/**
 Returns true if val is an numeric value.
 @param val the value to check.
 @return boolean
*/
function isNumeric(val)
{
    isOK = (parseFloat(val) + 0) == val;
    return isOK;
}

/**
 Returns true if val is an integer value.
 @param val the value to check.
 @return boolean
*/
function isInteger(val)
{
    isOK = (parseInt(val) + 0) == val;
    return isOK;
}

/**
 Returns true if val is a valid email format
 @param val the value to check.
 @return boolean
*/
function isEmail(val)
{
    atLocation   = val.lastIndexOf('@');
    dotLocation  = val.lastIndexOf('.');
    isOK = ( (atLocation>0) && (dotLocation>0) && (dotLocation > atLocation + 1) )
    return isOK;
}

/**
 Returns true if val is a properly-formatted purchase order ID
 @param val the value to check.
 @return boolean
*/
function isPurchaseOrderID(val) {
	var re  = /\d+\.[V-Zv-z]([A-Za-z0-9]{5})/;
	matches = val.match(re);
	isOk =  matches != null && matches[0] == val;
	return isOk;
}

/**
 Constructs a ValidationEntry object.
 @param isRequired        Whether the form field is required.
 @param validationType  The type of validation to perform
 @param errorMessage    The error message to display if the form is not valid.
*/
function ValidationEntry(isRequired, validationType, errorMessage)
{
    this.isRequired = isRequired;
    this.validationType = validationType;
    this.errorMessage = errorMessage;
}


