$(document).ready(function(){
	$(".infoicon").tooltip({delay: 400, fade: true, showURL: false});

	$("table.coverTypes input:radio").each(function () {
		if (this.checked == true) {
			if (this.name == "hct") {
				hospitalCoverTypesHandler(this);
			} else if (this.name = "ect") {
				extraCoverTypesHandler(this);
			}
		}
	});
});

function enable(section, enabled) {
	section = "table#" + section + "Cover";
	if (enabled == true) {
		$(section + " label").removeClass("greyed");
		$(section + " td.subHeading").removeClass("subHeadingGreyed");
		$(section + " td.featureCategory").removeClass("featureCategoryGreyed");
		$(section + " td.featureLabel").removeClass("featureLabelGreyed");
		$(section + " img.infoicon").attr("src", "../images-common/info_icon_white.gif");	
		$(section + " input:radio").removeAttr("disabled");	
		$(section + " input:checkbox").removeAttr("disabled");	
	} else {
		$(section + " label").addClass("greyed");
		$(section + " td.subHeading").addClass("subHeadingGreyed");
		$(section + " td.featureCategory").addClass("featureCategoryGreyed");
		$(section + " td.featureLabel").addClass("featureLabelGreyed");
		$(section + " img.infoicon").attr("src", "../images-common/info_icon_greyed_white.gif");	
		$(section + " input:radio").removeAttr("checked");	
		$(section + " input:radio").attr("disabled", "disabled");	
		$(section + " input:checkbox").removeAttr("checked");
		$(section + " input:checkbox").attr("disabled", "disabled");	
	}
}

function hospitalCoverTypesHandler(hospitalCoverType) {
	hospitalCoverType.checked = true;
	
	var coverType = hospitalCoverType.id.substr(5);
	if (coverType == "mostComprehensiveHospitalCoverType") {
		enable("hospital", false);	
	} else if (coverType == "chooseMyOwnHospitalCoverType") {
		enable("hospital", true);
	} else if (coverType == "noHospitalCoverType") {
		enable("hospital", false);		
	}
}

function extraCoverTypesHandler(extraCoverType) {
	extraCoverType.checked = true;
	
	var coverType = extraCoverType.id.substr(5);
	if (coverType == "mostComprehensiveExtraCoverType") {
		enable("extra", false);	
	} else if (coverType == "chooseMyOwnExtraCoverType") {
		enable("extra", true);	
	} else if (coverType == "noExtraCoverType") {
		enable("extra", false);	
	}
}

function hospitalCoverLevelsHandler() {
	var chooseMyOwnHospitalCoverType = $("table.coverTypes input:radio#hctc_chooseMyOwnHospitalCoverType").get(0);
	if (!chooseMyOwnHospitalCoverType.checked) {
		hospitalCoverTypesHandler(chooseMyOwnHospitalCoverType);
	}
}

function hospitalFeaturesHandler() {
	var chooseMyOwnHospitalCoverType = $("table.coverTypes input:radio#hctc_chooseMyOwnHospitalCoverType").get(0);
	if (!chooseMyOwnHospitalCoverType.checked) {
		hospitalCoverTypesHandler(chooseMyOwnHospitalCoverType);
	}

	var selection = false;
	$("table.coverLevels input:radio").each(function () {
		if ((this.name == "hcl") && (this.checked == true)) {
			selection = true;
		}
	});
	
	if (selection == false) {
		$("table#hospitalCover table.coverLevels input:radio").get(0).checked = true;
	}
}

function extraFeaturesHandler() {
	var chooseMyOwnExtraCoverType = $("table.coverTypes input:radio#ectc_chooseMyOwnExtraCoverType").get(0);
	if (!chooseMyOwnExtraCoverType.checked) {
		extraCoverTypesHandler(chooseMyOwnExtraCoverType);
	}
}

