Date.prototype.isLeapYear =
function(utc) {
	var y = utc ? this.getUTCFullYear() : this.getFullYear();
	return !(y % 4) && (y % 100) || !(y % 400) ? true : false;
};

var newReg = {

	zone: [],
	setHeaderImage: function(inVal, inObj) {
		if (inVal == "1") {
			inObj.css("backgroundImage", "url(\"/images/registration_header_p.gif\")");
		}
		else if (inVal == "3") {
			inObj.css("backgroundImage", "url(\"/images/registration_header_ff.gif\")");
		}
		else {
			inObj.css("backgroundImage", "url(\"/images/registration_header.gif\")");
		}
	},
	setPaging: function(inVal, inObj, curPage) {
		if (inVal == "0") {
			inObj.html("");
		}
		else if (inVal == "1") {
			inObj.html("Page " + curPage + " of 2 ");
		}
		else if (inVal == "3") {
			inObj.html("Page " + curPage + " of 2 ");
		}
		else if (inVal == "4") {
			inObj.html("Page " + curPage + " of 1 ");
		}
	},
	setNextBtn: function(inVal) {

		//alert(inVal);

		$("#newRegNext1").hide();
		$("#newRegNext2").hide();

		var strLoc;
		if (inVal != "4") {
			if (inVal == "1") strLoc = "#newRegNext" + inVal;
			if (inVal == "3") strLoc = "#newRegNext2";

			$(strLoc).show();
		}
		
		if(inVal=="4") {
			$("#newRegZone6").show();
		}
	},
	medicationsChecked: function() {

		if ($("input:radio[name=medication4_status]:checked").val() != undefined) {
			return true;
		}
		if ($("input:radio[name=medication14_status]:checked").val() != undefined) {
			return true;
		}
		if ($("input:radio[name=medication12_status]:checked").val() != undefined) {
			return true;
		}
		if ($("input:radio[name=medication8_status]:checked").val() != undefined) {
			return true;
		}
		if ($("input:radio[name=medication5_status]:checked").val() != undefined) {
			return true;
		}
		if ($("input:radio[name=medication9_status]:checked").val() != undefined) {
			return true;
		}
		if ($("input:radio[name=medication6_status]:checked").val() != undefined) {
			return true;
		}
		if ($("input:radio[name=medication10_status]:checked").val() != undefined) {
			return true;
		}
		if ($("input:radio[name=medication13_status]:checked").val() != undefined) {
			return true;
		}

		return false;
	},
	currentMedSelected: function() {

		var checkCurrentMeds = 0;
		if ($("input:radio[name=medication4_status]:checked").val() == '2') {
			checkCurrentMeds++;
		}
		if ($("input:radio[name=medication14_status]:checked").val() == '2') {
			checkCurrentMeds++;
		}
		if ($("input:radio[name=medication12_status]:checked").val() == '2') {
			checkCurrentMeds++;
		}
		if ($("input:radio[name=medication8_status]:checked").val() == '2') {
			checkCurrentMeds++;
		}
		if ($("input:radio[name=medication5_status]:checked").val() == '2') {
			checkCurrentMeds++;
		}
		if ($("input:radio[name=medication9_status]:checked").val() == '2') {
			checkCurrentMeds++;
		}
		if ($("input:radio[name=medication6_status]:checked").val() == '2') {
			checkCurrentMeds++;
		}
		if ($("input:radio[name=medication10_status]:checked").val() == '2') {
			checkCurrentMeds++;
		}
		if ($("input:radio[name=medication13_status]:checked").val() == '2') {
			checkCurrentMeds++;
		}

		if (checkCurrentMeds > 1) {
			return true;
		}
		else {
			return false;
		}
	},
	adjustDates: function() {

		if (($("#dobmonth").val() == "9") || ($("#dobmonth").val() == "4") || ($("#dobmonth").val() == "6") || ($("#dobmonth").val() == "11")) {
			// 30 days logic
			$("#dobdayopt31").remove();
			this.addDobDay("29");
			this.addDobDay("30");
		}
		else if ($("#dobmonth").val() == "2") {
			// February logic
			var leapYear;
			if ($("#dobyear").val() != "-Year-") {
				var d = new Date();
				d.setFullYear($("#dobyear").val());
				leapYear = d.isLeapYear();
			}
			else {
				leapYear = false;
			}

			$("#dobdayopt29").remove();
			$("#dobdayopt30").remove();
			$("#dobdayopt31").remove();

			if (leapYear) {
				this.addDobDay("29");
			}

		}
		else {
			// 31 days logic
			this.addDobDay("29");
			this.addDobDay("30");
			this.addDobDay("31");
		}

	},
	addDobDay: function(inNum) {

		if ($("#dobdayopt" + inNum).val() == undefined) {
			$("#dobday").append("<option value=\"" + inNum + "\" id=\"dobdayopt" + inNum + "\" >" + inNum + "</option>");
		}
	},
	toggleMeds: function() {

		var onVal = true;
		if ($("#medication4_status").attr('disabled') == true) {
			onVal = false;
		}

		$("#medication4_status").attr('disabled', onVal);
		$("#medication14_status").attr('disabled', onVal);
		$("#medication12_status").attr('disabled', onVal);
		$("#medication8_status").attr('disabled', onVal);
		$("#medication5_status").attr('disabled', onVal);
		$("#medication9_status").attr('disabled', onVal);
		$("#medication6_status").attr('disabled', onVal);
		$("#medication10_status").attr('disabled', onVal);
		$("#medication13_status").attr('disabled', onVal);

		$("#medication4a_status").attr('disabled', onVal);
		$("#medication14a_status").attr('disabled', onVal);
		$("#medication12a_status").attr('disabled', onVal);
		$("#medication8a_status").attr('disabled', onVal);
		$("#medication5a_status").attr('disabled', onVal);
		$("#medication9a_status").attr('disabled', onVal);
		$("#medication6a_status").attr('disabled', onVal);
		$("#medication10a_status").attr('disabled', onVal);
		$("#medication13a_status").attr('disabled', onVal);

	},
	clearMeds: function() {

		$("#medication4_status").attr('checked', false);
		$("#medication14_status").attr('checked', false);
		$("#medication12_status").attr('checked', false);
		$("#medication8_status").attr('checked', false);
		$("#medication5_status").attr('checked', false);
		$("#medication9_status").attr('checked', false);
		$("#medication6_status").attr('checked', false);
		$("#medication10_status").attr('checked', false);
		$("#medication13_status").attr('checked', false);

		$("#medication4a_status").attr('checked', false);
		$("#medication14a_status").attr('checked', false);
		$("#medication12a_status").attr('checked', false);
		$("#medication8a_status").attr('checked', false);
		$("#medication5a_status").attr('checked', false);
		$("#medication9a_status").attr('checked', false);
		$("#medication6a_status").attr('checked', false);
		$("#medication10a_status").attr('checked', false);
		$("#medication13a_status").attr('checked', false);

	},
	validateFirstPage: function() {

		var outVal = "";

		//alert("hello");	

		if ($("#iam").val() == "0") {
			return "Please choose a menu option.";
		}
		else if (($("#doctor_type").val() == "0") && ($("#iam").val() == "4")) {
			return "Answer all of the Required Questions.";
		}
		else if ($("#fname").val() == "") {
			return "Please enter your first name.";
		}
		else if ($("#lname").val() == "") {
			return "Please enter your last name.";
		}
		else if ($("#address").val() == "") {
			return "Please enter your address.";
		}
		else if ($("#city").val() == "") {
			return "Please enter your city.";
		}
		else if ($("#state").val() == "0") {
			return "Please choose your state.";
		}
		else if ($("#zip").val() == "") {
			return "Please enter your zip code.";
		}
		else if ($("#email").val() == "") {
			return "Please enter your email address.";
		}
		else if ($("#pass1").val() == "") {
			return "Please enter your password.";
		}
		else if ($("#pass2").val() == "") {
			return "Please confirm your password.";
		}
		else if ($("#pass1").val() != $("#pass2").val()) {
			return "Password fields don't match.";
		}
		else if (($("input:radio[name=gender]:checked").val() != "M") && ($("input:radio[name=gender]:checked").val() != "F")) {
			return "Please choose your gender.";
		}
		else if ($("#dobmonth").val() == "-Month-") {
			return "Please choose your Date of Birth month.";
		}
		else if ($("#dobday").val() == "-Day-") {
			return "Please choose your Date of Birth day.";
		}
		else if ($("#dobyear").val() == "-Year-") {
			return "Please choose your Date of Birth year.";
		}
		//"<p class=\"errorMessage\" style=\"margin-left: 15px;\">" +  + "</p>";

		return outVal;

	}


};



$(document).ready(function() {

	newReg.zone[0] = $("#newRegZone0");
	newReg.zone[1] = $("#newRegZone1");
	newReg.zone[2] = $("#newRegZone2");
	newReg.zone[3] = $("#newRegZone3");
	newReg.zone[4] = $("#newRegZone4");
	newReg.zone[5] = $("#newRegZone5");
	newReg.zone[6] = $("#newRegZone6");

	newReg.zone[2].hide();
	newReg.zone[3].hide();
	newReg.zone[4].hide();
	newReg.zone[5].hide();
	newReg.zone[6].hide();

	$("#newRegZone100").hide();

	newReg.setNextBtn($("#iam").val());
	newReg.adjustDates();
	

	$("#iam").change(function() {

		newReg.setHeaderImage($(this).val(), $("#regHeaderBkg"));
		newReg.setPaging($(this).val(), $(".newRegPaging"), "1");
		newReg.setNextBtn($(this).val());

		if ($(this).val() == "4") {
			newReg.zone[2].show();
			newReg.zone[6].show();
		}
		else {
			newReg.zone[2].hide();
			newReg.zone[6].hide();
		}

	});

	$("#dobmonth").change(function() {
		newReg.adjustDates();
	});
	$("#dobyear").change(function() {
		newReg.adjustDates();
	});

	$("#newRegNext1").click(function() {

		var outVal = newReg.validateFirstPage();
		if (outVal == "") {
			$("#newRegErrBox1").html("");
			$("#newRegNext1").hide();
			newReg.zone[0].hide();
			newReg.zone[1].hide();
			newReg.zone[3].show();
			newReg.zone[6].show();
		}
		else {

			
				$("#newRegErrBox1").html("<p class=\"errorMessage\" style=\"margin-left: 15px;\">" + outVal + "</p>");
		
		}
	});

	$("#newRegNext2").click(function() {
		var outVal = newReg.validateFirstPage();
		if (outVal == "") {
			$("#newRegErrBox1").html("");
			$("#newRegNext2").hide();
			newReg.zone[0].hide();
			newReg.zone[1].hide();
			newReg.zone[5].show();
			newReg.zone[6].show();
		}
		else {
			$("#newRegErrBox1").html("<p class=\"errorMessage\" style=\"margin-left: 15px;\">" + outVal + "</p>");
		}
	});



	$("#racurrently1").click(function() {
		newReg.zone[4].show();
	});

	$("#racurrently2").click(function() {
		newReg.zone[4].hide();
	});

	$("#ramednone").click(function() {
		if (newReg.medicationsChecked()) {
			alert("You can only select this option if you are not taking or have not taken any of these medications.");
			$(this).attr('checked', false);
		}
		else {
			newReg.toggleMeds();
		}
	});

	/*	$(".medRadio").click(function () {
	if($(this).val() == '2')
	{
	if(newReg.currentMedSelected())
	{
	alert("You can only name one medication as being currently taken.");
	$(this).attr('checked', false);			
	}
	}
	});*/

	$("#newRegClear").click(function() {
		newReg.clearMeds();
		return false;
	});

});

