﻿// JScript File

var maxChar = 300;
function goalTxtChange(id,txtGoal,txtbox)
{
    var id2 = id.substring(0,id.indexOf(txtGoal));
    id2 = id2 + txtbox;
    var temp = maxChar - document.getElementById(id).value.length;
    if(temp >= 0)
        document.getElementById(id2).value = temp;
    else
    {
        document.getElementById(id).readOnly = true;
        document.getElementById(id).value = document.getElementById(id).value.substring(0,document.getElementById(id).value.length-2);
        document.getElementById(id2).value = 0;
    }
}
function copyName(loc1,loc2,name)
{
    if(loc1)
        document.getElementById(loc1).value=name;
    if(loc2)
        document.getElementById(loc2).value=name;
}
function calendarPicker(strField)
{
    window.open('calendar.aspx?field=' + strField, 'calendarPopup', 'width=250,height=250,resizable=yes');
}
function previewGoal()
{
    var begin;
    var chpt1;
    var chpt2;
    var finish;
    var goal;
    var the_inputs=document.getElementsByTagName("input");
    for(var n=0;n<the_inputs.length;n++)
    {
        if(the_inputs[n].type=="hidden")
        {
            if(the_inputs[n].id.indexOf('hidden_goal') > 0)
            {
                goal = the_inputs[n].value;
            }
        }
        if(the_inputs[n].type=="text")
        {
            if(the_inputs[n].id.indexOf('txt_begin') > 0)
            {
                begin = the_inputs[n].value;
            }
            if(the_inputs[n].id.indexOf('txt_checkpt1') > 0)
            {
                chpt1 = the_inputs[n].value;
            }
            if(the_inputs[n].id.indexOf('txt_checkpt2') > 0)
            {
                chpt2 = the_inputs[n].value;
            }
            if(the_inputs[n].id.indexOf('txt_finish') > 0)
            {
                finish = the_inputs[n].value;
            }
        }

    }
    var today = new Date();
    var yday = new Date(today - (24*60*60*1000));
    if(finish == '' || (isNaN(Date.parse(finish)) || Date.parse(finish) < yday))
    {
        if(isNaN(Date.parse(finish)) || Date.parse(finish) < yday)
            alert('Finish Date is invalid');
        else
            alert('Finish Date is required.');
    }
    else if(goal == '')
    {
        alert('Goal is required.');
    }
    else if(begin != '' && ((isNaN(Date.parse(begin))) || (Date.parse(begin) > Date.parse(finish))) || Date.parse(begin) < yday)
    {
        if(isNaN(Date.parse(begin)) || Date.parse(begin) < yday)
            alert("Begin Date is invalid");
        else
            alert("Begin Date cannot be after Finish Date!");
    }
    else if(chpt1 != '' && begin != '' && ((isNaN(Date.parse(chpt1))) || ((Date.parse(chpt1) > Date.parse(finish) || Date.parse(chpt1) < Date.parse(begin)))))
    {
        if(isNaN(Date.parse(chpt1)))
            alert('Checkpoint 1 is invalid');
        else
            alert("Checkpoint 1 must be between " + begin + " and " + finish);
    }
    else if(chpt2 != '' && begin != '' && ((isNaN(Date.parse(chpt2))) || ((Date.parse(chpt2) > Date.parse(finish) || Date.parse(chpt2) < Date.parse(begin)))))
    {
        if(isNaN(Date.parse(chpt2)))
            alert('Checkpoint 2 is invalid');
        else
            alert("Checkpoint 2 must be between " + begin + " and " + finish);
    }
    else if(chpt1 != '' && chpt2 != '' && (Date.parse(chpt1) > Date.parse(chpt2)))
    {
        alert("Checkpoint 1 must come before Checkpoint 2");
    }
    else
    {
        window.open('goalPreview.aspx?goal='+goal+'&begin='+begin+'&chpt1='+chpt1+'&chpt2='+chpt2+'&finish='+finish , 'goalPreview', 'width=550,height=550,resizable=yes');
    }
}

function showTool(toolID) 
{   
    if(document.getElementById(toolID).style.display == 'none') 
    {
        document.getElementById(toolID).style.display = 'block';
    }
    else
    {
        document.getElementById(toolID).style.display = 'none';
    }
}

function closeTool(toolID, toolMode)
{
    if(toolMode == 'my')
    {

    }


    document.getElementById(toolID).style.display = 'none';
}

function showToolPopup(toolID)
{
    switch (toolID)
    {
       //blood sugar demo
       case 15:
            window.open("../tools/blood_sugar_demo.html");
            break;
       // Google map and go
       case 16:
             window.open("../tools/gamp/index.htm");
             break; 
       // ADC monitor selection
       case 22:
             window.open("http://abbottdiabetescare.com/adc_dotcom/showProductSelectionForm.do");
             break;
    }

}