Any admission or commitment of a person under this act, shall be valid and authorize the detention of such person only for the period specified in the section under which he was admitted or committed, or, where authorized, for such period as a court may specify in its order of commitment. If no period is specified in a commitment, the person may be detained until care or treatment is no longer necessary, whereupon he shall be discharged, or otherwise dealt with in accordance with the provisions of this act.
50 P.S. § 4418