Generalized theory for two-phase ion-pair and complexometric titrations