Returns a valid index within the given maximum index. Out of bound index loops back to the first valid index.
The index to validate.
Optional
The maximum index. If not provided or 0, returns 0.
The valid index.