﻿// 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).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;
            }
        }

    }
    if(finish == '')
    {
        alert('Finish Date is required.');
    }
    else if(goal == '')
    {
        alert('Goal is required.');
    }
    else if(begin != '' && (Date.parse(begin) > Date.parse(finish)))
    {
        alert("Begin Date cannot be after Finish Date!")
    }
    else if(chpt1 != '' && begin != '' && (Date.parse(chpt1) > Date.parse(finish) || Date.parse(chpt1) < Date.parse(begin)))
    {
        alert("Checkpoint 1 must be between " + begin + " and " + finish);
    }
    else if(chpt2 != '' && begin != '' && (Date.parse(chpt2) > Date.parse(finish) || Date.parse(chpt2) < Date.parse(begin)))
    {
        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 == '') 
    {
        document.getElementById(toolID).style.display = 'none';
    }    
    
    if(document.getElementById(toolID).style.display == 'none') 
    {
        document.getElementById(toolID).style.display = 'block';
    }
    else
    {
        document.getElementById(toolID).style.display = 'none';
    }
}
