CRM-14798 - CiviCase - Fix max_instances check

This code was using the ambiguous generic variable names ("$key", "$val")
and confusing the content of "$val" (e.g.  confusing caseId with
caseTypeName).