/**
 * Preforms all of the calculations and populates the display table
 */
function doCalculation() {
    var tier = document.calc.tier.options[document.calc.tier.selectedIndex].value;
    var employment = document.calc.employment.options[document.calc.employment.selectedIndex].value;
    var hmo_rate    = 0;
    var hmo_coin    = 0;
    var hmo_ipf     = 0;
    var hdppo_rate  = 0;
    var hdppo_copay = 0;
    var hdppo_coin  = 0;
    var multiplier  = 12;

    //State ASE
    if (employment == "1") {
         switch (tier) {
             //Employee Only
             case "1":
                hmo_rate    = (95.78 * multiplier);
                hmo_coin_b    = 1500;
                hdppo_rate  = 7.16 * multiplier;
                hdppo_copay_b = 1500;
                hdppo_coin_b  = 2500;
                break;
            //Employee & Spouse
            case "2":
                hmo_rate    = 367.74 * multiplier;
                hmo_coin_b    = 3000;
                hdppo_rate  = 154.02 * multiplier;
                hdppo_copay_b = 3000;
                hdppo_coin_b  = 5000;
                break;
            //Employee & Children
            case "3":
                hmo_rate    = 193.14 * multiplier;
                hmo_coin_b    = 3000;
                hdppo_rate  = 60.32 * multiplier;
                hdppo_copay_b = 3000;
                hdppo_coin_b  = 5000;
                break;
            //Employee & Family
            case "4":
                hmo_rate    = 419.62 * multiplier;
                hmo_coin_b    = 3000;
                hdppo_rate  = 183.54 * multiplier;
                hdppo_copay_b = 3000;
                hdppo_coin_b  = 5000;
                break;
            default:
                setForm();
                break;
        }
    }
    //Schools PSE
    else if (employment == "2") {
        multiplier  = 15;
        switch (tier) {
            //Employee Only
            case "1":
                hmo_rate    = (194.10 * multiplier);
                hmo_coin_b    = 1500;
                hdppo_rate  = 15.20 * multiplier;
                hdppo_copay_b = 1500;
                hdppo_coin_b  = 2500;
                break;
            //Employee & Spouse
            case "2":
                hmo_rate    = 771.74 * multiplier;
                hmo_coin_b    = 3000;
                hdppo_rate  = 333.10 * multiplier;
                hdppo_copay_b = 3000;
                hdppo_coin_b  = 5000;
                break;
            //Employee & Children
            case "3":
                hmo_rate    = 436.88 * multiplier;
                hmo_coin_b    = 3000;
                hdppo_rate  = 155.36 * multiplier;
                hdppo_copay_b = 3000;
                hdppo_coin_b  = 5000;
                break;
            //Employee & Family
            case "4":
                hmo_rate    = 773.82 * multiplier;
                hmo_coin_b    = 3000;
                hdppo_rate  = 333.70 * multiplier;
                hdppo_copay_b = 3000;
                hdppo_coin_b  = 5000;
                break;
            default:
                setForm();
                break;
        }
    }
    else { 
        hmo_rate    = 0;
        hmo_coin_b    = 0;
        hdppo_rate  = 0;
        hdppo_copay_b = 0;
        hdppo_coin_b  = 0;
    }
    
    // all of the variables off of the form
    var persons = new Number(document.calc.persons.value);
    var visits_n = new Number(document.calc.visits_n.value);
    var visits_s = new Number(document.calc.visits_s.value);
    var visits_e = new Number(document.calc.visits_e.value);
    var visits_o = new Number(document.calc.visits_o.value);
    var visits_i = new Number(document.calc.visits_i.value);
    //  Radiology added
    var visits_r = new Number(document.calc.visits_r.value);
    
    var prescriptions = new Number(document.calc.prescriptions.value);
    var major = new Number(document.calc.major.value);
    var additional = new Number(document.calc.additional.value);
    
    // the displayed calculations
    var hmo_total = new Number();
    var hmo_copay = new Number();
    var hmo_coin = new Number();
    var hmo_pocket = new Number();
    var hmo_annual = new Number();
    var hdppo_total = new Number();
    var hdppo_copay = new Number();
    var hdppo_coin = new Number();
    var hdppo_pocket = new Number();
    var hdppo_annual = new Number();
    var savings = new Number();
    
    // temporary calculations
    var hmo_inpatient = new Number();
    var hmo_radiology = new Number();
    
    // calculate the hmo total
    hmo_total = (visits_n * 76) + (visits_s * 108) + (visits_e * 320) +
        (visits_o * 1432) + (visits_i * 5014) + (prescriptions * 47) +
        (major * 316) + (visits_r * 800) + additional;
    
    // calculate the hmo copay
    hmo_copay = (visits_n * 25) + (visits_s * 35) + (visits_e * 100) +
        (visits_o * 100) + (prescriptions * 16) + (major * 0);
    if ((visits_i * 250) > (persons * 3 * 250)) {
        hmo_inpatient = (persons * 3 * 250);
    }
    else {
        hmo_inpatient = (visits_i * 250);
    }
    hmo_copay += hmo_inpatient;
    //Radiology ADDED
    if ((visits_r * 250) > (persons * 250)) {
        hmo_radiology = (persons * 250);
    }
    else {
        hmo_radiology = (visits_r * 250);
    }
    hmo_copay += hmo_radiology;
    
    // calculate the hmo coin
    hmo_coin = (visits_n * 0) + (visits_s * 0) + 
        (((visits_e * 320) - (visits_e * 100)) * .20) +
        (((visits_o * 1432) - (visits_o * 0)) * .20) +
        (((visits_i * 5014) - hmo_inpatient) * .20) +
        (((major * 316) - (major * 0)) * .20) +
        (((visits_r * 800) - (hmo_radiology * 0)) * .20) +
        (additional * .20);
    if(hmo_coin > hmo_coin_b) {
    		hmo_coin = hmo_coin_b;
    }
        
    // calculate the hmo total
    hdppo_total = (visits_n * 76) + (visits_s * 108) + (visits_e * 320) +
        (visits_o * 1432) + (visits_i * 5014) + (prescriptions * 47) +
        (major * 316) + (visits_r * 800) + additional;
        
    // calculate the hdppo copay
    if (hdppo_total > hdppo_copay_b) {
        hdppo_copay = hdppo_copay_b;
    }
    else {
        hdppo_copay = hdppo_total;
    }
    
    // calculate the hdppo coin
    if (((hdppo_total - hdppo_copay) * .20) > hdppo_coin_b) {
        hdppo_coin = hdppo_coin_b;
    }
    else {
        hdppo_coin = (hdppo_total - hdppo_copay) * .20;
    }
    
    // calculate total out of pocket expenses
    hmo_pocket = hmo_copay + hmo_coin;
    hdppo_pocket = hdppo_copay + hdppo_coin;
    
    // calculate the total annual expenses
    hmo_annual = hmo_rate + hmo_pocket;
    hdppo_annual = hdppo_rate + hdppo_pocket;
    
    // calculate total savings
    savings = hmo_annual - hdppo_annual;
    
    document.getElementById("hmo_rate").firstChild.nodeValue = 
        formatCurrency(hmo_rate);
    document.getElementById("hdppo_rate").firstChild.nodeValue = 
       formatCurrency(hdppo_rate);  
    document.getElementById("hmo_total").firstChild.nodeValue = 
        formatCurrency(hmo_total);
    document.getElementById("hmo_copay").firstChild.nodeValue = 
        formatCurrency(hmo_copay);
    document.getElementById("hmo_coin").firstChild.nodeValue = 
        formatCurrency(hmo_coin);
    document.getElementById("hdppo_total").firstChild.nodeValue = 
        formatCurrency(hdppo_total);
    document.getElementById("hdppo_copay").firstChild.nodeValue = 
        formatCurrency(hdppo_copay);
    document.getElementById("hdppo_coin").firstChild.nodeValue = 
        formatCurrency(hdppo_coin);
    
    document.getElementById("hmo_pocket").firstChild.nodeValue = 
        formatCurrency(hmo_pocket);
    document.getElementById("hdppo_pocket").firstChild.nodeValue = 
        formatCurrency(hdppo_pocket);
    
    document.getElementById("hmo_annual").firstChild.nodeValue = 
        formatCurrency(hmo_annual);
    document.getElementById("hdppo_annual").firstChild.nodeValue = 
        formatCurrency(hdppo_annual);
    if (savings >= 0) {
    		document.getElementById("savings").firstChild.nodeValue = 
        		"You will save  " + formatCurrency(savings) +
        		" just by choosing the ARHealth HD PPO plan";
    }
  	else {
  	    document.getElementById("savings").firstChild.nodeValue = 
        		"Your best option is an ARHealth Plan.  You can save even more by selecting the ARHealth - NovaSys.";
    }
}

/**
 * Sets the form to its original state
 */
function setForm() {
    document.calc.persons.value = 1;
    document.calc.persons.disabled = true;
    
    document.calc.visits_w.value = 0;
    document.calc.visits_n.value = 0;
    document.calc.visits_s.value = 0;
    document.calc.visits_e.value = 0;
    document.calc.visits_o.value = 0;
    document.calc.visits_i.value = 0;
    document.calc.visits_r.value = 0;
    document.calc.prescriptions.value = 0;
    document.calc.major.value = 0;
    document.calc.additional.value = "0.00";
    
    document.getElementById("hmo_rate").firstChild.nodeValue = "0";
    document.getElementById("hdppo_rate").firstChild.nodeValue = "0";
}

/**
 * Checks the value of the tier select box. Then determines what to do 
 * with the "persons" box. It enables the box if the value is greater than 2,
 * disables it otherwise.
 *
 * After the interface is updated it calls the doCalculation method to make 
 * the calculations. 
 */
function checkTier() {
    var tier = document.calc.tier.options[document.calc.tier.selectedIndex].value;
    
    if (tier == 0) {
        alert("You must select a tier.");
        document.calc.tier.focus();
        setForm();
        return;
    }
    if (tier == 1) {
    	  document.calc.persons.value = 1;
        document.calc.persons.disabled = true; 
    }
    if (tier == 2) {
    	  document.calc.persons.value = 2;
        document.calc.persons.disabled = true; 
    }
    if (tier > 2) {
        document.calc.persons.disabled = false;
    }

    
    doCalculation();
}

/**
 * Checks the value of the employee box.
 *
 * After the interface is updated it calls the doCalculation method to make 
 * the calculations. 
 */
function checkEmployment() {
    /*
    var employment = document.calc.employment.options[document.calc.employment.selectedIndex].value;
    
    if (employment == 0) {
        alert("You must select a type of employment.");
        document.calc.employment.focus();
        setForm();
        return;
    }
    
    doCalculation();
    */
    var employment = 2;
}

/**
 * Checks a value to make sure it is a positive number with a 
 * presision of 1 or 2. If it is it calls the doCalculation method, otherwise
 * it throws an alert, sets the focus to the field in question then exits.
 */
function checkPositiveNumber(field) {
    var exp = /^[0-9]{1,3}$/;
    
    if (!exp.exec(field.value)) {
        alert("This field must be a positive number");
        field.focus();
        return;
    }
    
    doCalculation();
}

/**
 * Checks a value to make sure it is in U.S. currenct format. 
 * If it is it calls the doCalculation method, otherwise
 * it throws an alert, sets the focus to the field in question then exits.
 */
function checkCurrency(field) {
    var exp = /^[0-9]{1,}(\.[0-9]{2})?$/;
    
    if (!exp.exec(field.value)) {
        alert("This field must be in the U.S. currency format ex. 100.00");
        field.focus();
        return;
    }
    
    doCalculation();
}

/**
 * Formats a number with a $ and sets the decimal to two places. This method
 * assumes the user is using a modern browser.
 */
function formatCurrency(num) {
    num = num.toString().replace(/\$|\,/g,'');

    if(isNaN(num)) {
        num = "0";
    }

    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num*100+0.50000000001);
    cents = num%100;
    num = Math.floor(num/100).toString();

    if(cents < 10) {
        cents = "0" + cents;
    }

    for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++) {
        num = num.substring(0, num.length - (4 * i + 3)) + "," +
            num.substring(num.length - (4 * i + 3));
    }

    return (((sign)?"":"-") + "$" + num );
}

function openWindow(page, height, width) { 
    window.open(page, "window", config="height=" + height + ", width=" +
        width + ", toolbar=no, menubar=no, resizable=yes, location=no, " +
        "directories=no, status=no"); 
} 

