// Copyright © 1998-2004 FireLogic, Inc. All Rights Reserved. For GSK Use Only.
// A component of the Lamictal Dosing Illustrator Application by HeathEngage implemented for GlaxoSmithKline 
// Last revised on October 20, 2004


var lastWeight;

    function validateForm() {
    	checkForIncrementValue('medication');

		checkForEnabling();

    	var notFilledFields = "Please enter required information into the following fields: \n";
    	var isFilled = true;
    	
    	if (document.forms['doseForm'].elements['age'].selectedIndex == 0) {
    		notFilledFields += "Age\n";
    		isFilled = false;
    	}


    	var string = new String("");
    	string = document.forms['doseForm'].elements['weight'].value;
 		for (var i = 0; i < string.length; i++ ) {
			if (string.charAt(i) == '.') {
				if ((string.length - (i + 1)) > 1) {
					alert("Please input weight as a whole number or a number with only one decimal.");
 					return false;
				} else {
					i++;
				}
			}
			if ((string.charAt(i) != '0') 
 				&& (string.charAt(i) != '1')
 				&& (string.charAt(i) != '2')
 				&& (string.charAt(i) != '3')
 				&& (string.charAt(i) != '4')
 				&& (string.charAt(i) != '5')
 				&& (string.charAt(i) != '6')
 				&& (string.charAt(i) != '7')
 				&& (string.charAt(i) != '8')
 				&& (string.charAt(i) != '9')) {
 					alert("Please input weight as a whole number or a number with only one decimal.");
 					return false;
 				}
			if (document.forms['doseForm'].elements['wUnits'][0].checked) {
				if (string < 15) {
					alert("The weight value you entered is too low - it should not be below 6.7 kg (15 lb).");
					return false;
				} else if (string > 220) {
					alert("The weight value you entered is too high - it should not exceed 100 kg (220 lb).");
					return false;
				}
			} else if (document.forms['doseForm'].elements['wUnits'][1].checked) {
				if (string < 6.7) {
					alert("The weight value you entered is too low - it should not be below 6.7 kg (15 lb).");
					return false;
				} else if (string > 100) {
					alert("The weight value you entered is too high - it should not exceed 100 kg (220 lb).");
					return false;
				}
			}
			
			if (string > 999) {
 					alert("Please enter a weight value no greater than 999");
 					return false;
			}
 		}

    	
        if 
 (((!document.forms['doseForm'].elements['wUnits'][0].checked)&&(!document.forms['doseForm'].elements['wUnits'][1].checked))||(document.forms['doseForm'].elements['weight'].value == "")) {
 			notFilledFields += "Weight\n";
 			isFilled = false;
 		}
 		
        if (document.forms['doseForm'].elements['startDateButton'].value == "") {
 			notFilledFields += "Start Date\n";
 			isFilled = false;
        }

		document.forms['doseForm'].elements['startDate'].value = document.forms['doseForm'].elements['startDateButton'].value;

       if (!document.forms['doseForm'].elements['medication'][0].checked
       		&& !document.forms['doseForm'].elements['medication'][1].checked

       		    // start of change, dated 2005-08-01
       		&& !document.forms['doseForm'].elements['medication'][2].checked)
       		    // end of change

       		  {
        	notFilledFields += "Therapy\n";
        	isFilled = false;
        }

    	if (!document.forms['doseForm'].elements['userType'][0].checked
		 && !document.forms['doseForm'].elements['userType'][1].checked 
		 && !document.forms['doseForm'].elements['userType'][2].checked
		 && !document.forms['doseForm'].elements['userType'][3].checked 
		 && !document.forms['doseForm'].elements['userType'][4].checked
		 && !document.forms['doseForm'].elements['userType'][5].checked ) {
    		notFilledFields += "Patient's or Physician's version\n";
    		isFilled = false;
    	}

        if (document.forms['doseForm'].elements['achievingDose'].value == "") {
        	notFilledFields += "Initial Target Maintenance Dose of LAMICTAL to be Achieved\n";
        	isFilled = false;
        }


 		var string = new String("");
 		string = document.forms['doseForm'].elements['achievingDose'].value;
 		for (var i = 0; i < string.length; i++ ) {
             if (document.forms['doseForm'].elements['medication'][2].checked) {

                 if ((string.charAt(i) != '0')
 				&& (string.charAt(i) != '1')
 				&& (string.charAt(i) != '2')
 				&& (string.charAt(i) != '3')
 				&& (string.charAt(i) != '4')
 				&& (string.charAt(i) != '5')
 				&& (string.charAt(i) != '6')
 				&& (string.charAt(i) != '7')
 				&& (string.charAt(i) != '8')
 				&& (string.charAt(i) != '9')
                && (string.charAt(i) != '.'))
             {

                 alert("Please enter a decimal or numeric value for the Initial Target Maintenance Dose of LAMICTAL.");
                 return false;

             }
             } else if ((string.charAt(i) != '0')
 				&& (string.charAt(i) != '1')
 				&& (string.charAt(i) != '2')
 				&& (string.charAt(i) != '3')
 				&& (string.charAt(i) != '4')
 				&& (string.charAt(i) != '5')
 				&& (string.charAt(i) != '6')
 				&& (string.charAt(i) != '7')
 				&& (string.charAt(i) != '8')
 				&& (string.charAt(i) != '9'))
                {
 					alert("Please enter a whole number for the Initial Target Maintenance Dose of LAMICTAL.");
 					return false;
 				}
			if (string == "0") {
 				alert("The initial target maintenance dose cannot be 0 mg per kg per day.  Please enter another value.");
 				return false;
			} 
			if (string > 99) {
 				alert("Please enter a whole number no greater than 99 mg per kg per day for the initial target maintenance dose of LAMICTAL.");
 				return false;
			}
 		}

        
        if (document.forms['doseForm'].elements['dosageFrequency'].selectedIndex == 0) {
        	notFilledFields += "Dosage Increase Frequency\n";
        	isFilled = false;
        }
        if (!isFilled) {
        	alert(notFilledFields);
        	return false;
        }
    	
        /* Validating AGE field */
        if (document.forms['doseForm'].elements['age'].selectedIndex == 0) {
            alert("LAMICTAL is indicated for patients 2 years old and older. Please make sure that you entered correct age information");
            return false;
        }
        /* Validating WEIGHT field */
        if ((!document.forms['doseForm'].elements['wUnits'][0].checked)&&(!document.forms['doseForm'].elements['wUnits'][1].checked)) {
            alert("Please make sure that you have correctly entered the patient's weight. Currently, this value seems to be outside the normal weight range for a patient of the entered age.")
            return false;
        }
        if (document.forms['doseForm'].elements['weight'].value == "") {
        	alert("Please enter a weight value.");
            //alert("The weight you entered seems to be outside the limits for the patient of the entered age.")
            return false;
        }
        
        /* CHECKING WEIGHT USING TABLE OF VALUES*/
        var weight = document.forms['doseForm'].elements['weight'].value;
        var error = "";
        if (document.forms['doseForm'].elements['wUnits'][1].checked) {
        	weight = weight * 2.2;
        }
      
        if (document.forms['doseForm'].elements['age'].selectedIndex == 1) {	// 2 years
        	if (weight < 24.7) error = "low";
        	else if (weight >31.9) error = "high";
        } else if (document.forms['doseForm'].elements['age'].selectedIndex == 2) {	// 3 years
        	if (weight < 28.7) error = "low";
        	else if (weight >36.8) error = "high";
        } else if (document.forms['doseForm'].elements['age'].selectedIndex == 3) {	// 4 years
        	if (weight < 32.1) error = "low";
        	else if (weight > 41.4) error = "high";
        } else if (document.forms['doseForm'].elements['age'].selectedIndex == 4) {	// 5 years
        	if (weight < 36.6) error = "low";
        	else if (weight > 49.7) error = "high";
        } else if (document.forms['doseForm'].elements['age'].selectedIndex == 5) {	// 6 years
        	if (weight < 40.9) error = "low";
        	else if (weight > 56.4) error = "high";
        } else if (document.forms['doseForm'].elements['age'].selectedIndex == 6) {	// 7 years
        	if (weight < 45.8) error = "low";
        	else if (weight > 64.4) error = "high";
        } else if (document.forms['doseForm'].elements['age'].selectedIndex == 7) {	// 8 years
        	if (weight < 51.2) error = "low";
        	else if (weight > 73) error = "high";
        } else if (document.forms['doseForm'].elements['age'].selectedIndex == 8) {	// 9 years
        	if (weight < 56.3) error = "low";
        	else if (weight > 81) error = "high";
        } else if (document.forms['doseForm'].elements['age'].selectedIndex == 9) {	// 10 years
        	if (weight < 61.1) error = "low";
        	else if (weight > 89.9) error = "high";
        } else if (document.forms['doseForm'].elements['age'].selectedIndex == 10) {	// 11 years
        	if (weight < 66.3) error = "low";
        	else if (weight > 99.3) error = "high";
        } else if (document.forms['doseForm'].elements['age'].selectedIndex == 11) {	// 12 years
        	if (weight < 72) error = "low";
        	else if (weight > 109.6) error = "high";
		}


		if (error == "high") {
				 if(!confirm("The weight you have entered appears to be above the average weight for a child of this age. Please double check the weight you have entered to ensure that it has been correctly entered. Click OK if you are sure that the weight is correct or Cancel to re-enter the weight value."))
				 	return false;
		} else if (error == "low") {
				if(!confirm("The weight you have entered appears to be below the average weight for a child of this age. Please double check the weight you have entered to ensure that it has been correctly entered. Click OK if you are sure that the weight is correct or Cancel to re-enter the weight value."))
				 	return false;
		}

        /* Validating START DATE field */
        if (document.forms['doseForm'].elements['startDateButton'].value != "") {
            var s = document.forms['doseForm'].elements['startDateButton'].value;
            var year = s.substring(6,10);
            var currYear = new Date().getYear();
            if (!document.all) {
            	currYear += 1900;
            }
            if (year < currYear) {
                alert("Please enter a value for the Start Date.");
                return false;
            }
			if (s == "Show Calendar") {
                alert("Please enter the Start Date in mm/dd/yyyy format.");
                return false;
			}
        } else {
                alert("Please enter the Start Date in mm/dd/yyyy format.");
                return false;
        }

        /* Validating THERAPY field */
       if ((!document.forms['doseForm'].elements['medication'][0].checked)
       		&& (!document.forms['doseForm'].elements['medication'][1].checked)
       		// start of change, dated 2995-08-01
       		    && (!document.forms['doseForm'].elements['medication'][2].checked))
       		// end of change
       		{
            alert("Please select the therapy with which LAMICTAL will be used.");

            return false;
        }

        /* Validating MAINTENANCE DOSE field */
        if (document.forms['doseForm'].elements['achievingDose'].value == "") {
        	alert("Please enter a whole number for the Initial Target Maintenance Dose of LAMICTAL.");
        	return false;
        }
        if ((document.forms['doseForm'].elements['age'].selectedIndex != 12)
        		&& (document.forms['doseForm'].elements['medication'][0].checked)
        		&& (document.forms['doseForm'].elements['achievingDose'].value > 400)) {
        	alert("The maintenance dose should not exceed 400 mg/day.");
        	return false;
        }
        if ((document.forms['doseForm'].elements['age'].selectedIndex != 12)
        		&& (document.forms['doseForm'].elements['medication'][1].checked)
        		&& (document.forms['doseForm'].elements['achievingDose'].value > 200)) {
        	alert("The maintenance dose should not exceed 200 mg/day.");
        	return false;
        }

        // start of change, dated 2005-08-01
        if ((document.forms['doseForm'].elements['age'].selectedIndex != 12)
        		&& (document.forms['doseForm'].elements['medication'][2].checked)
        		&& (document.forms['doseForm'].elements['achievingDose'].value > 300)) {
        	alert("The maintenance dose should not exceed 300 mg/day.");
        	return false;
        }
        // end of change



        /*if ((document.forms['doseForm'].elements['age'].selectedIndex == 12)
        		&& (document.forms['doseForm'].elements['medication'].value == "EIAED")
        		&& (document.forms['doseForm'].elements['achievingDose'].value > 500)) {
        	alert("The maintenance dose should not exceed 500 mg/day.");
        	return false;
        }
        if ((document.forms['doseForm'].elements['age'].selectedIndex == 12)
        		&& (document.forms['doseForm'].elements['medication'].value == "VPA")
        		&& (document.forms['doseForm'].elements['achievingDose'].value > 400)) {
        	alert("The maintenance dose should not exceed 400 mg/day.");
        	return false;
        }
        */

		/* Validating FREQUENCY field */
		if (document.forms['doseForm'].elements['dosageFrequency'].selectedIndex == 0) {
			alert("Please select the expected Dosage Increase Frequency (every one week or every two weeks).");
			return false;
		}

        
        message = "Confirm info you entered:\n\nTherapy = ";
        
        if (document.forms['doseForm'].elements['medication'][0].checked)
        	message += "LAMICTAL added to carbamazepine, phenytoin, phenobarbital or primidone (without VPA)\n";
        else if (document.forms['doseForm'].elements['medication'][1].checked)
        	message += "LAMICTAL added to any AED regimen containing VPA\n";
        else {
            message += "LAMICTAL added to other AED regimens NOT containing carbamazepine, phenytoin, phenobarbital or primidone and NOT containing VPA\n";        }
        	
        message += "Initial Target Maintenance Dose of LAMICTAL = " + document.forms['doseForm'].elements['achievingDose'].value + " mg per kg per day\n";
        

        if (document.forms['doseForm'].elements['medication'][0].checked)
        	message += "Dosage Increase Increment (after first 4 weeks) = Calculated automatically based on 1.2 mg/kg/day\n";
        else if (document.forms['doseForm'].elements['medication'][1].checked)
        	message += "Dosage Increase Increment (after first 4 weeks) = Calculated automatically based on 0.3 mg/kg/day\n";
        // start of change, dated 2005-08-01
        else if (document.forms['doseForm'].elements['medication'][2].checked)
            message += "Dosage Increase Increment (after first 4 weeks) = Calculated automatically based on 0.6 mg/kg/day\n";
        // end of change

        
        if (document.forms['doseForm'].elements['dosageFrequency'].selectedIndex == 1)
        	message += "Dosage Increase Frequency = Every 1 week";
        else if (document.forms['doseForm'].elements['dosageFrequency'].selectedIndex == 2)
        	message += "Dosage Increase Frequency = Every 2 weeks";


        if (!confirm(message))
        	return false;
        
        return true;
    }

	var whatTo, whatText;

	function checkForIncrementValue(element) {

		whatFrom = document.forms['doseForm'].elements[element];

		if (element == "age") {

			whatTo = document.forms['doseForm'].elements['dosageIncrement'];

			element = "check";

		} else if (element == "med1") {

			whatTo = document.forms['doseForm'].elements['dosageIncrement'];

			whatText = "med1";

			element = "end";

			for (var i = whatTo.length; i > 0; i--) {
				whatTo.options[0] = null;
			}

			checkForEnabling();

		} else if (element == "med2") {

			whatTo = document.forms['doseForm'].elements['dosageIncrement'];

			whatText = "med2";

			element = "end";

			for (var i = whatTo.length; i > 0; i--) {
				whatTo.options[0] = null;
			}

			checkForEnabling();

		// ---- start of change, dated 2005-08-01 8:11pm //
		} else if (element == "med3") {

		    whatTo = document.forms['doseForm'].elements['dosageIncrement'];

		    whatText = "med3";

		    element = "end";

            for (var i = whatTo.length; i > 0; i--) {
				whatTo.options[0] = null;
			}

			checkForEnabling();

		}
		// ---- end of change, dated 2005-08-01 8:11pm //

		///whatText = whatFrom.options[whatFrom.selectedIndex].text;
		//whatText = whatFrom.name;
		showLink(element);
		//whatTo.options[0].selected = true;
		//if (element == "medication")	checkForIncrementValue("age");
        return false;
	}

	function showLink(element) {
		if (element == "medication")
			ageUpdated();
		else if (element == "end")
			dosageIncrementUpdated();
		else if (element == "check")
			checkForEnabling();
	}

	function checkForEnabling() {
		var allChecked = true;

		if (
			((!document.forms['doseForm'].elements['wUnits'][0].checked)
				&&(!document.forms['doseForm'].elements['wUnits'][1].checked))
			||(document.forms['doseForm'].elements['weight'].value == "")
			
			) 
		{
			allChecked = false;
		} else if (document.forms['doseForm'].elements['age'].selectedIndex == 0) {
			allChecked = false;
		} else if ((!document.forms['doseForm'].elements['medication'][0].checked)
       		&& (!document.forms['doseForm'].elements['medication'][1].checked)
       		// start of change, dated 2005-08-01
       		&& (!document.forms['doseForm'].elements['medication'][2].checked)) {
       		// end of change
       		allChecked = false;
       	}

       	if (allChecked) {
       		document.forms['doseForm'].elements['startDateButton'].disabled = false;
       		document.forms['doseForm'].elements['achievingDose'].disabled = false;
       		document.forms['doseForm'].elements['dosageIncrement'].disabled = false;
       		document.forms['doseForm'].elements['dosageFrequency'].disabled = false;
       	}
	}

	
	function opt(key, value) {
    	var optionName = new Option(value, key, false, false)
    	var length = whatTo.length;
    	whatTo.options[length] = optionName;
	}
	
	function ageUpdated() {
	// if (whatText == '13 and older') {
    // 	opt('EIAED','LAMICTAL added to EIAEDs (without VPA)');
    //	    opt('VPA','LAMICTAL added to an AED regimen containing VPA');
	//    }
	//    else {
    //    	opt('EIAED','LAMICTAL added to EIAEDs (without VPA).');
    //	    opt('VPA','LAMICTAL added to an AED regimen containing VPA.');
	//    }
	}

	function dosageIncrementUpdated() {
	    //if (whatText == 'med1') {
    	//    opt('100','100 mg/day');
	    //}
    	//if (whatText == 'med2') {
	    //    opt('25','25 mg/day');
    	//    opt('50','50 mg/day');
	    //}
    	if (whatText == 'med1') {
        	opt('0','Calculated automatically based on 1.2 mg/kg/day');
	    }
	    if (whatText == 'med2') {
    	    opt('1','Calculated automatically based on 0.3 mg/kg/day');
	    }
	    // start of change, dated 2005-08-01
	    if (whatText == 'med3') {
	        opt('2', 'Calculated automatically based on 0.6 mg/kg/day');
	    }
	    // end of change
	}
	
	function updateHint() {
		if (document.forms['doseForm'].elements['medication'][0].checked) {
			document.forms['doseForm'].elements['hintText'].value = "5 mg/kg/day to 15 mg/kg/day";
		} else if (document.forms['doseForm'].elements['medication'][1].checked) {
			document.forms['doseForm'].elements['hintText'].value = "1 mg/kg/day to 5 mg/kg/day";

        // start of change, dated 2005-08-01
		} else if (document.forms['doseForm'].elements['medication'][2].checked) {
		    document.forms['doseForm'].elements['hintText'].value = "4.5 mg/kg/day to 7.5 mg/kg/day";
		}
		// end of change
	}
	
	function getCurrentDate() {
		d = new Date();
		
		date = d.getDate();
		if (date <= 9) date = "0"+date;
		
		month = d.getMonth();
		month++;
		if (month <= 9) month = "0"+month;
		
		year = d.getFullYear();
		
		return (""+month+"/"+date+"/"+year);
	}

	function resetCalendar() {
		document.forms['doseForm'].elements['startDateButton'].value = getCurrentDate();
	}