
53 Cal.App.3d 576 (1975)
126 Cal. Rptr. 267
VAL'S PAINTING AND DRYWALL, INC., Plaintiff and Respondent,
v.
ALLSTATE INSURANCE COMPANY, Defendant and Appellant.
Docket No. 43098.
Court of Appeals of California, Second District, Division Five.
December 9, 1975.
*579 COUNSEL
Zajic & Kremser and Paul E. Kremser, Jr., for Defendant and Appellant.
Eckert, O'Gorman & McFarland and Charles V. Eckert III for Plaintiff and Respondent.
OPINION
ASHBY, J.
Respondent Val's Painting and Drywall, Inc. (Val's), filed an action in municipal court against appellant Allstate Insurance Company (Allstate). Allstate filed an amended cross-complaint seeking relief beyond the jurisdiction of the municipal court and the matter was transferred to superior court. The superior court sustained Val's demurrer to the amended cross-complaint without leave to amend, entered an order of dismissal of the amended cross-complaint (Code Civ. Proc., § 581, subd. 3), and remanded the matter to the municipal court. Allstate appeals from the judgment of dismissal. (Code Civ. Proc., § 581d; Keenan v. Dean, 134 Cal. App.2d 189, 191-192 [285 P.2d 300].) The dispute concerns the duty of Allstate to defend Val's against a third party claim under the terms of an insurance policy and whether Allstate can assert noncoverage as a ground for reimbursement from Val's for settlement of that claim.
Based upon the allegations of Allstate's amended cross-complaint and court files of which the trial court apparently took judicial notice,[1] the facts may be stated as follows: Allstate issued Val's a comprehensive general liability insurance policy. In Hendrick v. Ray-Cron, Inc., municipal court number 29736, and Iwanek v. Ray-Cron, Inc., superior court number 89121, complaints for personal injuries were filed alleging that on September 2, 1969, Hendrick and Iwanek were employed by Val's as painters and were injured in a fall from a scaffolding furnished by Ray-Cron. The injuries were alleged to be due to an unsafe and defective condition of the scaffolding and the failure of Ray-Cron to furnish required safety equipment.
*580 Ray-Cron filed cross-complaints against Val's, alleging that Val's agent had signed an equipment rental agreement which stated that Val's acknowledged the equipment was in good working condition, that Val's would use the equipment in a proper manner, and that Val's agreed to exonerate, indemnify and save Ray-Cron harmless from all claims and liabilities to all parties for damage to person and property in any way arising out of the use of the equipment. Ray-Cron also alleged that the negligence and violation of safety orders of which the plaintiffs complained were in fact the negligence and violations of Val's; that if the equipment was in a dangerous or defective condition such condition arose after the rental, due to Val's negligence in failing to use the equipment in the proper manner or in failing to discover such defects. Ray-Cron further alleged that even if the equipment was in a dangerous or defective condition Val's was obligated pursuant to the rental agreement to indemnify Ray-Cron for any liability to plaintiffs which might be found against Ray-Cron.[2]
Val's requested Allstate to provide a defense of the Ray-Cron cross-complaints. By letter dated August 21, 1970, Allstate advised Val's that the policy did not cover liability based on a contractual indemnifying clause, and declined to defend. Subsequently Val's again "insisted and requested" that Allstate defend the suits. Allstate agreed to defend under a "reservation of rights" stated in a letter to Val's dated February 3, 1971, as follows:
"With respect to the occurrence of September 2, 1969 in which your company was involved you are hereby notified that Allstate Insurance Company in investigating the said accident or any claim arising therefrom, or negotiating for compromise settlement, or in making any settlement or defending any suit against you or others, or in any other way acting or failing to act, does not waive any of its rights or admit any obligations under the policy.
"We are making this reservation of rights because insurance policy 04 725 028 does not apply; `(a) To liability assumed by the insured under any contract or agreement except an incidental contract'; and, `(g) To any obligation for which the insured or any carrier as his insurer may be held liable under any Workmen's Compensation, Unemployment Compensation or Disability Benefits Law, or under any similar law;' and for other reasons.
*581 "The service of this notice upon you does not deprive you of any rights you may have against this company.
"The cross complaint for indemnification is being defended by Frederick W. Kosmo, Attorney at Law, 520 West Fifth Street, Oxnard, California."
Allstate thereafter settled the municipal court action for $700 and the superior court action for $2,500. Allstate expended $3,639.94 on attorneys' fees in defending those actions.
Val's filed the instant action against Allstate to recover Val's defense expenses for the initial period during which Allstate refused to defend. Val's alleged that Allstate's refusal to defend was wrongful. Allstate filed a cross-complaint alleging that it had no duty under the insurance policy to defend or indemnify Val's for the Ray-Cron claims, that it had reserved its rights to contest coverage and the duty to defend, and that Allstate should be reimbursed by Val's for all amounts it had spent on the actions, including both the attorney expenses and the settlements.
The trial court sustained, without leave to amend, Val's demurrer to the amended cross-complaint, ruling in part as follows:
"It is further the Court's opinion that if an insurance carrier in a comprehensive insurance coverage contract, as is involved in this situation, undertakes to provide a defense for the insured, it is also the insurance carrier's obligation to pay for said defense. The Court can find no provision in the insurance contract for reimbursement by the insured to the insurance carrier for any amount of settlement that might have been negotiated and paid by the carrier, nor any provision for reimbursement to the insurance carrier for attorney fees or costs for an attorney hired by and under the control of the carrier. It is also to be noted that the control of the negotiations and settlement, pursuant to the contract in question, were solely in the control of and done exclusively by the carrier."

CONTENTIONS
Allstate contends that the trial court erred in sustaining the demurrer without leave to amend because the cross-complaint stated, or could be amended to state, a cause of action for reimbursement of defense costs and the amounts paid in settlement. Val's contends that the order was correct because Allstate had a duty to defend, and, having elected to *582 defend and settle the claims, may not now seek reimbursement from its insured.
We conclude that a distinction must be drawn between Allstate's duty to defend the actions and Allstate's duty to indemnify Val's under the policy. As to the former, we find that under the circumstances of this case Allstate had the duty to defend because the Ray-Cron claims were potentially within policy coverage, and that Allstate is therefore not entitled to recover its attorney and legal expenses in defending those actions.
As to the duty to indemnify Val's under the policy, there is a possibility Allstate can amend its pleadings to show that the claim was not covered by the policy, that Allstate adequately reserved its right to contest the issue and that Allstate can now seek recovery of the amounts paid on Val's behalf to settle the claims. Since Allstate has been given no opportunity to amend, it is necessary to reverse the judgment for this purpose.

DUTY TO DEFEND
If an insurer wrongfully refuses to defend the insured, the insurer may be held liable for the judgment rendered against the insured in the third party action. (Gray v. Zurich Insurance Co., 65 Cal.2d 263, 279-281 [54 Cal. Rptr. 104, 419 P.2d 168].) Allstate argues that in view of the possibly disastrous consequences of a refusal to defend, the insurer has virtually no choice but to defend, even though policy coverage is disputed. Allstate argues that if the insurer adequately reserves its rights, it may later seek recovery from the insured of the costs of providing the defense, if it can show there was in fact no duty to defend.
(1a) We need not determine if Allstate's reservation of rights was sufficient for this purpose, because, based upon the cross-complaint and the court files judicially noticed, Allstate had the duty to defend the Ray-Cron claims, and therefore Allstate is not entitled in any event to recovery of the defense costs.
Gray v. Zurich Insurance Co., supra, 65 Cal.2d 263, 275, held that an insurer must defend a suit which potentially seeks damages within the coverage of the policy. In Gray, as in the instant case, the insurer relied on an exclusionary clause as negating its duty to defend. The court rejected this contention for three reasons: (1) the policy provisions were *583 ambiguous, and doubts as to the meaning of an insurance policy must be resolved against the insurer; (2) where the contract is one of adhesion, courts will look to the reasonable expectations of the insured, and (3) the policy contains its own seeds of uncertainty where the insurer's obligation to indemnify cannot be determined without adjudicating the very action the insurer is being called on to defend. (Id. at pp. 269-275.)
Reviewing the policy and the Ray-Cron claims we conclude Allstate had the duty to defend. The policy provides "comprehensive general liability insurance." Allstate promises to pay on behalf of the insured all sums which the insured shall become legally obligated to pay as damages because of bodily injury to which this insurance applies. Allstate has the right and duty to defend any suit against the insured seeking damages on account of such bodily injury or property damage, even if any of the allegations of the suit are groundless, false or fraudulent, and may make such investigation and settlement of any claim or suit as it deems expedient. The policy contains the following exclusions from coverage: (a) liability assumed by the insured under any contract or agreement except an incidental contract; (g) any obligation for which the insured or any carrier as his insurer may be held liable under any workmen's compensation, unemployment compensation or disability benefits law, or under any similar law; and (h) bodily injury to any employee of the insured arising out of and in the course of his employment by the insured, but this exclusion does not apply to liability assumed by the insured under an incidental contract.
The Ray-Cron cross-complaints against Val's arose out of a suit for damages for bodily injury, a risk which was of the nature and kind covered by the insurance policy. It is possible to detect two basic theories of the Ray-Cron cross-complaint. The first is implied indemnity, by which Ray-Cron alleged that if Ray-Cron were held liable for the injuries to Val's employees, Val's was obligated to indemnify Ray-Cron, on the ground that Ray-Cron's negligence was passive and secondary whereas Val's negligence was active and the primary cause of the accident. (See, e.g., Ferrel v. Vegetable Oil Products Co., 247 Cal. App.2d 117, 120 [55 Cal. Rptr. 589].) The other theory was contractual indemnity, based upon the ground that when Val's obtained the scaffold from Ray-Cron, Val's agent signed a document agreeing to indemnify and save Ray-Cron harmless from damage arising out of use of the equipment. The two theories were closely entwined, however, by virtue of Labor Code section 3864, which provides: "If an action as provided in this chapter prosecuted by the employee, the employer, or both jointly *584 against the third person results in judgment against such third person, or settlement by such third person, the employer shall have no liability to reimburse or hold such third person harmless on such judgment or settlement in absence of a written agreement so to do executed prior to the injury."
(2) The purpose of Labor Code section 3864, enacted in 1959, was to eliminate the employer's liability for implied indemnity in the absence of a written agreement, because implied indemnity imposed a greater burden on the employer than was contemplated under the workers' compensation system. (Pacific Gas & Elec. Co. v. Morse, 6 Cal. App.3d 707, 713 [86 Cal. Rptr. 7]; City of Sacramento v. Superior Court, 205 Cal. App.2d 398, 404 [23 Cal. Rptr. 43].) It had the economic objective of reducing the employer's insurance costs by eliminating the cost of public liability insurance against implied indemnification. (Pacific Gas & Elec. Co. v. Morse, supra, at pp. 712-713.)
(1b) Although the exclusion in Val's policy for "liability assumed by the insured under any contract or agreement except an incidental contract" might be intended to exclude from coverage a written agreement of the type mentioned in Labor Code section 3864, the fact that Allstate may not be liable to indemnify the insured does not necessarily mean there is no duty to defend. Under Gray, the duty to defend is broader than the duty to indemnify. (See Blackfield v. Underwriters at Lloyd's, London, 245 Cal. App.2d 271, 272, 276 [53 Cal. Rptr. 838]; Firco, Inc. v. Fireman's Fund Ins. Co., 173 Cal. App.2d 524, 527 [343 P.2d 311].)
If Ray-Cron had sued Val's for implied indemnity without alleging a written agreement, Allstate would have been obligated to defend because the case involved a risk the nature and kind of which was covered by the comprehensive policy. The defense would have been an easy one, namely, a demurrer for failure to state a cause of action by virtue of Labor Code section 3864. (E.g., Progressive Trans. Co. v. Southern California Gas Co., 241 Cal. App.2d 738, 742-743 [51 Cal. Rptr. 116]; S.F. Examiner Division v. Sweat, 248 Cal. App.2d 493, 497 [56 Cal. Rptr. 711].) The absence of liability could be established, but the costs of the defense would be Allstate's responsibility, since under the policy Allstate promised "to defend any suit against the Insured seeking damages on account of such bodily injury ... even if any of the allegations of the suit are groundless, ..."
*585 The fact that Ray-Cron specifically alleged a written agreement would not necessarily preclude Allstate's duty to defend. Val's might have a valid defense to the written agreement, for example, that Val's agent lacked authority to enter into a written indemnity agreement, that the agreement was a contract of adhesion, or that the terms of the written agreement narrowly construed, did not cover the facts in question or exculpate the indemnitee from his own negligence. (See Western Gulf Oil Co. v. Oilwell Service Co., 219 Cal. App.2d 235, 242-243 [33 Cal. Rptr. 20]; MacDonald & Kruse, Inc. v. San Jose Steel Co., 29 Cal. App.3d 413, 419-420 [105 Cal. Rptr. 725].) The invalidity of the written agreement would be an issue in the very action Allstate was called on to defend.
Thus, applying the test of Gray v. Zurich Insurance Co., supra, 65 Cal.2d 263, we conclude that Allstate had the duty to defend the Ray-Cron claims. The claims were for bodily injury, a risk of the nature and kind covered by the comprehensive policy. The exclusion for liability assumed by contract or agreement "except an incidental contract" is ambiguous, and its application cannot be determined without reference to the validity of the written agreement, itself an issue in the very action for which the defense was sought. Allstate is obligated to defend even groundless, false or fraudulent claims. Under all the circumstances, Gray required that Allstate provide a defense, and Allstate is not entitled to be reimbursed for its defense costs.

REIMBURSEMENT FOR SETTLEMENT COSTS
As we have seen, the duty of an insurer to defend the insured may be broader than its duty to indemnify the insured under the terms of the policy. The insurer's "dilemma"[3] is how to defend the action without being held to have waived its claim of noncoverage of the policy. (E.g., J. Frank & Co. v. New Amsterdam C. Co., 175 Cal. 293, 295 [165 P. 927].)
Gray v. Zurich Insurance Co., supra, 65 Cal.2d 263, 275, explicitly suggests that the insurer may avoid the dilemma by reserving its rights. The court stated: "In any event, if the insurer adequately reserves its right to assert the noncoverage defense later, it will not be bound by the judgment [against the insured]. If the injured party prevails, that party or the insured will assert his claim against the insurer. At this time the insurer can raise the noncoverage defense previously reserved. In this *586 manner the interests of insured and insurer in defending against the injured party's primary suit will be identical; the insurer will not face the suggested dilemma." (65 Cal.2d at p. 279; fn. omitted.) See Note (1967) 14 U.C.L.A. L.Rev. 1328, 1333, footnote 23.
(3) Thus the insurer can avoid being bound by the judgment against the insured if it secures a nonwaiver agreement from the insured (e.g., Coolidge v. Standard Acc. Ins. Co., 114 Cal. App. 716, 719-720 [300 P. 885]; Sears v. Illinois Indemnity Co., 121 Cal. App. 211, 212-213, 227 [9 P.2d 245]) or makes an adequate reservation of rights. (E.g., Bear Film Co. v. Indemnity Ins. Co., 22 Cal. App.2d 520, 523-524 [71 P.2d 603]; see McDanels v. General Ins. Co., 1 Cal. App.2d 454, 458-461 [35 P.2d 394, 36 P.2d 829].)
The distinction between a nonwaiver agreement and a reservation of rights has been stated as follows: "... A nonwaiver agreement is a bilateral contract, normally in writing, entered into by the assured and the insurer after the accident, providing that the insurer will defend the tort suit while reserving its right to assert nonliability under the policy at a later date.... [¶] A reservation of rights is very similar to a nonwaiver agreement, and it is subject to the same limitations and restrictions. It differs in being less formal than the nonwaiver and less tied to strict contract principles. The insurer need only notify, or attempt to notify, the assured that it is conducting the investigation and defense of the tort claim under a reservation of the right to assert policy defenses at a later time, and the assured's silence will usually be deemed acquiescence. Courts have in general been fairly liberal in implying reservations." (Note (1955) 68 Harv.L.Rev. 1436, 1446, 1447; fns. omitted.)
The authorities are split on the circumstances in which a unilateral reservation of rights will preserve the rights of the insurer. (38 A.L.R.2d 1175-1177; 44 Am.Jur.2d, Insurance, § 1556, p. 440; Note (1950) 2 Stan.L.Rev. 387-388.) Thus, it has been said that courts are liberal in implying a reservation of rights and deeming the insured's acceptance of the defense to be acquiescence. (Note, supra, 68 Harv.L.Rev. at p. 1447.) On the other hand, it has been said that if the insured expressly refuses to consent to a reservation of rights, a unilateral reservation is ineffective; the insurer must make an election whether to defend or refuse to defend. 7A Appleman, Insurance (1962) § 4694, pp. 546-547; Note, supra, 2 Stan.L.Rev. 387-388; Roos, supra, 13 Hastings L.J. at p. 207.)
*587 (4) In California, however, the theory that by defending the suit an insurer "waives" its right to claim noncoverage rests upon the doctrine of estoppel. There must be a showing that the insurer either intentionally relinquished a known right, or acted in such manner as to cause the insured reasonably to believe the insurer had relinquished such right, and that the insured relied upon such conduct to his detriment. (McDanels v. General Ins. Co., supra, 1 Cal. App.2d 454, 460; Mirich v. Underwriters at Lloyd's London, 64 Cal. App.2d 522, 530-531 [149 P.2d 19]; Gaunt v. Prudential Ins. Co., 255 Cal. App.2d 18, 23-24 [62 Cal. Rptr. 624]; Purefoy v. Pacific Automobile Indemn. Exch., 5 Cal.2d 81, 91 [53 P.2d 155]; Note (1935) 23 Cal.L.Rev. 443.)
At this early stage of the proceedings, Val's has not yet pleaded, and so there is no allegation that Val's expressly refused to enter into a nonwaiver agreement, that Val's notified Allstate that it did not acquiesce in the reservation of rights, that Allstate led Val's to believe Allstate had relinquished its rights, or that Val's detrimentally relied thereon.
Thus, were this a case where Allstate had reserved its rights under the circumstances alleged, then defended the action and refused to pay an ensuing judgment against Val's, it could not be said as a matter of law that Allstate's pleading was insufficient to raise the issue of a valid reservation of rights.
The additional problem raised by this case, however, is that Allstate did not defend the action to trial and judgment but instead settled it. Allstate did more than merely to undertake the defense. Allstate voluntarily paid money out in order to settle the Ray-Cron claims. Allstate contends that this money was in effect Val's money and that Allstate should be reimbursed for the amounts paid out on Val's behalf, assuming that the policy did not cover the claims.
The recent case of Johansen v. California State Auto. Assn. Inter-Ins. Bureau, 15 Cal.3d 9 [123 Cal. Rptr. 288, 538 P.2d 744], supports Allstate's position that under certain circumstances the insurer may enter into a settlement on the insured's behalf while also reserving its rights and may then seek reimbursement from the insured. In Johansen the insurer was held liable for wrongful failure to settle an action against the insured within policy limits. The insurer had refused settlement on the ground that the policy did not cover the incident, but coverage was subsequently held by the courts to apply. In rejecting the insurer's argument that a *588 holding of liability would in effect require an insurer to settle all cases irrespective of whether the policy provided coverage, the Supreme Court stated: "Moreover, contrary to defendant's assertion, an insurer in defendant's position retains the ability to enter an agreement with the insured reserving its right to assert a defense of noncoverage even if it accepts a settlement offer. If, having reserved such rights and having accepted a reasonable offer, the insurer subsequently establishes the noncoverage of its policy, it would be free to seek reimbursement of the settlement payment from its insured." (Id. at p. 19.)
However, in this case, the letter sent by Allstate to Val's is insufficient, without more, to constitute the agreement contemplated by Johansen. Nowhere does the letter expressly state that if Allstate settled the case it would look to Val's for reimbursement for any reasonable amounts paid. The statement in the letter that "in investigating the said accident ... or negotiating for compromise settlement, or in making any settlement ... [Allstate] does not waive any of its rights or admit any obligations under the policy" cannot, without more, reasonably be interpreted to mean that Allstate was given carte blanche to settle the case with what will turn out to be the insured's own money.
Furthermore, there is no allegation in Allstate's pleading that Val's was informed of the settlement offer, that Val's expressly or impliedly agreed that the claimants should be paid a total of $3,200 as a reasonable settlement, that the settlement was reasonable, that Val's was given an opportunity to assume the defense if Val's did not think the settlement reasonable, or that Val's expressly or impliedly agreed that if such sum was paid the question of policy coverage should be left to future determination.
(5) Absent an agreement by the insured  express or implied in fact  that the insurer may commit the insured's own fund toward any reasonable settlement, the insurer is not permitted to seek reimbursement for a particular settlement unless it has secured specific authority to make that settlement or has notified the insured of a reasonable offer by the claimant and given the insured an opportunity to assume the defense.
(6) We conclude that the allegations in Allstate's present cross-complaint are insufficient to entitle it to reimbursement from Val's of the amounts paid in the settlements. It is possible, however, that Allstate may be able to state a valid cause of action if given an opportunity to amend. Allstate has never had that opportunity, since the trial court *589 sustained the demurrer without leave to amend. Based on the record on appeal, we have no way of knowing whether Allstate's failure to include essential allegations as to the extent of Val's participation in the settlement was merely due to inartful pleading, needed clarification in the law, or because the facts will not support such allegations. Since pleadings should be liberally construed to attain substantial justice between the parties, and leave to amend should be permitted if there is a reasonable possibility a valid cause of action can be stated, the matter should be remanded and Allstate given another opportunity to plead. (Youngman v. Nevada Irrigation Dist., 70 Cal.2d 240, 244-245, 251 [74 Cal. Rptr. 398, 449 P.2d 462].)
The judgment is reversed and the cause remanded for further proceedings consistent with the views expressed in this opinion.
Kaus, P.J., and Hastings, J., concurred.
A petition for a rehearing was denied January 6, 1976, and appellant's petition for a hearing by the Supreme Court was denied March 3, 1976. Clark, J., and Richardson, J., were of the opinion that the petition should be granted.
NOTES
[1]  In ruling on a demurrer the court may consider not only the allegations of the complaint but also facts of which the court may or must take judicial notice, including the court files in other cases involving the parties. (Bill Loeper Ford v. Hites, 47 Cal. App.3d 828, 830, fn. 1 [121 Cal. Rptr. 131]; Saltares v. Kristovich, 6 Cal. App.3d 504, 510-511 [85 Cal. Rptr. 866]; Pietro v. Pietro, 147 Cal. App.2d 788, 790 [305 P.2d 916]: Code Civ. Proc., § 430.30.)
[2]  In June 1971, Ray-Cron filed an amended cross-complaint in which it was additionally alleged that Val's had the duty to provide the necessary safety equipment, that Ray-Cron was prepared to provide it at the time of the rental, but that Val's did not order such equipment despite awareness of its obligation to do so.
[3]  See, e.g., Note (1950) 2 Stan.L.Rev. 383, 384; Roos, The Obligation to Defend and Some Related Problems (1961) 13 Hastings L.J. 206, 207.
