// emcutils.js
// G Macfarlane, Mar 2004
//
// General javascript utilities for EMC website pages, events diary and rotas system

// ------------------------------------------------------------ 
// isNumeric(): Check whether a field is numeric, or null.

function IsNumeric(sText)
{   var ValidChars = "0123456789";
    var Char;
  
    for (i = 0; i < sText.length; i++) 
    {   Char = sText.charAt(i); 
        if (ValidChars.indexOf(Char) == -1) 
        {   return false;
        }
    }
    
    return true;
}

// ------------------------------------------------------------ 
// eventButton(): Set the "event" and "key" parameters as indicated, then submit the event maintenance form

function eventButton(evAction,evKey,mustValidate,hashTo)
{   if (mustValidate > 0)
    {   if (window.document.emcForm.eventDate.value == '')
        {   alert('The event date cannot be left blank!');
            return;
        }

        if (window.document.emcForm.eventDescr.value == '')
        {   alert('You have to provide an event description!');
            return;
        }

        var stime = window.document.emcForm.eventStart.value;
        var etime = window.document.emcForm.eventEnd.value;
        
        if (stime != '' && etime != '' && etime < stime)
        {   alert('Event finishes before it starts!');
            return;
        }
    }

    window.document.emcForm.evAction.value = evAction;
    window.document.emcForm.editKey.value  = evKey;
    window.document.emcForm.hashTo.value   = hashTo;
    window.document.emcForm.submit();
}

// ------------------------------------------------------------ 
// secButton(): Set the "event" and "key" parameters for "section" maintenance form

function secButton(secAction,secKey,mustValidate,hashTo)
{   if (mustValidate > 0)
    {   if (window.document.emcForm.secName.value == '')
        {   alert('The section name cannot be left blank!');
            return;
        }
    }
    
    window.document.emcForm.secAction.value = secAction;
    window.document.emcForm.editKey.value   = secKey;
    window.document.emcForm.hashTo.value   = hashTo;
    window.document.emcForm.submit();
}

// ------------------------------------------------------------ 
// locnButton(): Set the "event" and "key" parameters for "location" maintenance form

function locnButton(locnAction,locnKey,mustValidate,hashTo)
{   if (mustValidate > 0)
    {   if (window.document.emcForm.locnName.value == '')
        {   alert('The location name cannot be left blank!');
            return;
        }
        if (window.document.emcForm.locnShortName.value == '')
        {   alert('The location "short" name cannot be left blank!');
            return;
        }
    }
    
    window.document.emcForm.locnAction.value = locnAction;
    window.document.emcForm.editKey.value    = locnKey;
    window.document.emcForm.hashTo.value   = hashTo;
    window.document.emcForm.submit();
}

// ------------------------------------------------------------ 
// evtypeButton(): Set the "event" and "key" parameters for "event type" maintenance form

function evtypeButton(evtypeAction,evtypeKey,mustValidate,hashTo)
{   if (mustValidate > 0)
    {   if (window.document.emcForm.evtypeName.value == '')
        {   alert('The event type name cannot be left blank!');
            return;
        }
    }
    
    window.document.emcForm.evtypeAction.value = evtypeAction;
    window.document.emcForm.editKey.value      = evtypeKey;
    window.document.emcForm.hashTo.value   = hashTo;
    window.document.emcForm.submit();
}

// ------------------------------------------------------------ 
// ppntButton(): Set the "event" and "key" parameters for "participant" maintenance form

function ppntButton(ppntAction,ppntKey,mustValidate,hashTo)
{   if (mustValidate > 0)
    {   if (window.document.emcForm.ppntSurname.value == '')
        {   alert('The Surname cannot be left blank!');
            return;
        }
        
        if (window.document.emcForm.ppntFirstName.value == '')
        {   alert('The first name cannot be left blank!');
            return;
        }

        if (window.document.emcForm.ppntNickname.value == '')
        {   alert('The Nickname (short name) cannot be left blank!');
            return;
        }
    }
    
    window.document.emcForm.ppntAction.value   = ppntAction;
    window.document.emcForm.editKey.value      = ppntKey;
    window.document.emcForm.hashTo.value       = hashTo;
    window.document.emcForm.submit();
}

// ------------------------------------------------------------ 
// regButton(): Set the "event" and "key" parameters for "regular events" maintenance form

function regButton(regAction,regKey,mustValidate,hashTo)
{   if (mustValidate > 0)
    {   if (!IsNumeric(window.document.emcForm.regInstances.value))
        {   alert('If supplied, the number of instances must be numeric!');
            return;
        }
        
        if (window.document.emcForm.regDescr.value == '')
        {   alert('The Description cannot be left blank!');
            return;
        }
        
        if (window.document.emcForm.regDays.value == '')
        {   alert('You must specify which weekdays the event repeats on!');
            return;
        }
        
        if (window.document.emcForm.regWeeks.value == '')
        {   alert('You must specify which monthly weeks the event repeats in!');
            return;
        }
        
        if (window.document.emcForm.regMonths.value == '')
        {   alert('You must specify which months the event repeats in!');
            return;
        }
    }
    
    window.document.emcForm.regAction.value = regAction;
    window.document.emcForm.editKey.value   = regKey;
    window.document.emcForm.hashTo.value    = hashTo;
    window.document.emcForm.submit();
}

// ------------------------------------------------------------ 
// rotaButton(): Set the "event" and "key" parameters for "rotas" maintenance form

function rotaButton(rotaAction,rotaKey,numPpts,mustValidate)
{   if (rotaAction == 'delete')
    {   var x = window.confirm("This is going to delete the whole rota.\nAre you quite sure you want to do this?")
        if (!x) 
            return;
    }
    
    if (mustValidate > 0)
    {   if (window.document.emcForm.rotaDescr.value == '')
        {   alert('The rota description cannot be left blank!');
            return;
        }
        
       if (window.document.emcForm.rotaShortName.value == '')
        {   alert('The rota short name cannot be left blank!');
            return;
        }
        
        if (window.document.emcForm.rotaDays.value == '')
        {   alert('You must specify which weekdays the rota applies to!');
            return;
        }
        
        if (window.document.emcForm.rotaWeeks.value == '')
        {   alert('You must specify which monthly weeks the rota takes place in!');
            return;
        }
        
        if (window.document.emcForm.rotaMonths.value == '')
        {   alert('You must specify which months the rota takes place in!');
            return;
        }

        var i=0;

        for (i=0; i<numPpts; i++)
        {   var participant = window.document.emcForm['rpPpt'+i].value;
            var sequenceno  = window.document.emcForm['rpSeq'+i].value;
    
            if (participant != '')
            {   if (sequenceno == '')
                {   alert('You must specify a sequence number for every member in the rota!');
                    return;
                }
                
                if (!IsNumeric(sequenceno))
                {   alert('All sequence numbers must be numeric!');
                    return;
                }
            }
        }
    }
    
    window.document.emcForm.rotaAction.value = rotaAction;
    window.document.emcForm.editKey.value   = rotaKey;
    window.document.emcForm.submit();
}

// ------------------------------------------------------------ 
// makeStyleList(): create a list of style elements 'bold,underline,italic'

function makeStyleList(lbl,l1,l2,l3)
{   var sl1 =window.document.emcForm[l1].value;
    var sl2 =window.document.emcForm[l2].value;
    var sl3 =window.document.emcForm[l3].value;
    
    var c1 =window.document.emcForm[l1].checked ? sl1+',' : '';
    var c2 =window.document.emcForm[l2].checked ? sl2+',' : '';
    var c3 =window.document.emcForm[l3].checked ? sl3+',' : '';
        
    window.document.emcForm[lbl].value = c1+c2+c3;
}

// ------------------------------------------------------------ 
// makeWeekdayList(): create a list of weekday elements

function makeWeekdayList(lbl)
{   var c1 =window.document.emcForm['Mon'+lbl].checked ? 'Mon,': '';
    var c2 =window.document.emcForm['Tue'+lbl].checked ? 'Tue,': '';
    var c3 =window.document.emcForm['Wed'+lbl].checked ? 'Wed,': '';  
    var c4 =window.document.emcForm['Thu'+lbl].checked ? 'Thu,': '';
    var c5 =window.document.emcForm['Fri'+lbl].checked ? 'Fri,': '';
    var c6 =window.document.emcForm['Sat'+lbl].checked ? 'Sat,': '';
    var c7 =window.document.emcForm['Sun'+lbl].checked ? 'Sun,': '';
    
    window.document.emcForm[lbl].value = c1+c2+c3+c4+c5+c6+c7;
}

// ------------------------------------------------------------ 
// makeMonthList(): create a list of month name elements

function makeMonthList(lbl)
{   var c1 =window.document.emcForm['Jan'+lbl].checked ? 'Jan,': '';
    var c2 =window.document.emcForm['Feb'+lbl].checked ? 'Feb,': '';
    var c3 =window.document.emcForm['Mar'+lbl].checked ? 'Mar,': '';
    var c4 =window.document.emcForm['Apr'+lbl].checked ? 'Apr,': '';
    var c5 =window.document.emcForm['May'+lbl].checked ? 'May,': '';
    var c6 =window.document.emcForm['Jun'+lbl].checked ? 'Jun,': '';
    var c7 =window.document.emcForm['Jul'+lbl].checked ? 'Jul,': '';
    var c8 =window.document.emcForm['Aug'+lbl].checked ? 'Aug,': '';
    var c9 =window.document.emcForm['Sep'+lbl].checked ? 'Sep,': '';
    var ca =window.document.emcForm['Oct'+lbl].checked ? 'Oct,': '';
    var cb =window.document.emcForm['Nov'+lbl].checked ? 'Nov,': '';
    var cc =window.document.emcForm['Dec'+lbl].checked ? 'Dec,': '';
    
    window.document.emcForm[lbl].value = c1+c2+c3+c4+c5+c6+c7+c8+c9+ca+cb+cc;
}

// ------------------------------------------------------------ 
// makeWeeksList(): create a list of week name elements

function makeWeeksList(lbl)
{   var c1 =window.document.emcForm['1st'+lbl].checked ? '1st,': '';
    var c2 =window.document.emcForm['2nd'+lbl].checked ? '2nd,': '';
    var c3 =window.document.emcForm['3rd'+lbl].checked ? '3rd,': '';
    var c4 =window.document.emcForm['4th'+lbl].checked ? '4th,': '';
    var c5 =window.document.emcForm['5th'+lbl].checked ? '5th,': '';
    var c6 =window.document.emcForm['dn2'+lbl].checked ? 'dn2,': '';
    var c7 =window.document.emcForm['ts1'+lbl].checked ? 'ts1,': '';
    
    window.document.emcForm[lbl].value = c1+c2+c3+c4+c5+c6+c7;
}

// ------------------------------------------------------------ 
// setWeekDays(): set/clear all weekday checkboxes

function setWeekDays(lbl,setclr)
{   window.document.emcForm['Mon'+lbl].checked = setclr;
    window.document.emcForm['Tue'+lbl].checked = setclr;
    window.document.emcForm['Wed'+lbl].checked = setclr;
    window.document.emcForm['Thu'+lbl].checked = setclr;
    window.document.emcForm['Fri'+lbl].checked = setclr;
    window.document.emcForm['Sat'+lbl].checked = setclr;
    window.document.emcForm['Sun'+lbl].checked = setclr;
    
    if (setclr)
        window.document.emcForm[lbl].value = 'Mon,Tue,Wed,Thu,Fri,Sat,Sun';
    else
        window.document.emcForm[lbl].value = '';
}

// ------------------------------------------------------------ 
// setWeeks(): set/clear all weekday checkboxes

function setWeeks(lbl,setclr)
{   window.document.emcForm['1st'+lbl].checked = setclr;
    window.document.emcForm['2nd'+lbl].checked = setclr;
    window.document.emcForm['3rd'+lbl].checked = setclr;
    window.document.emcForm['4th'+lbl].checked = setclr;
    window.document.emcForm['5th'+lbl].checked = setclr;
    window.document.emcForm['dn2'+lbl].checked = setclr;
    window.document.emcForm['ts1'+lbl].checked = setclr;
    
    if (setclr)
        window.document.emcForm[lbl].value = '1st,2nd,3rd,4th,5th,dn2,ts1';
    else
        window.document.emcForm[lbl].value = '';
}

// ------------------------------------------------------------ 
// setMonths(): set/clear all month checkboxes

function setMonths(lbl,setclr)
{   window.document.emcForm['Jan'+lbl].checked = setclr;
    window.document.emcForm['Feb'+lbl].checked = setclr;
    window.document.emcForm['Mar'+lbl].checked = setclr;
    window.document.emcForm['Apr'+lbl].checked = setclr;
    window.document.emcForm['May'+lbl].checked = setclr;
    window.document.emcForm['Jun'+lbl].checked = setclr;
    window.document.emcForm['Jul'+lbl].checked = setclr;
    window.document.emcForm['Aug'+lbl].checked = setclr;
    window.document.emcForm['Sep'+lbl].checked = setclr;
    window.document.emcForm['Oct'+lbl].checked = setclr;
    window.document.emcForm['Nov'+lbl].checked = setclr;
    window.document.emcForm['Dec'+lbl].checked = setclr;
    
    if (setclr)
        window.document.emcForm[lbl].value = 'Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec';
    else
        window.document.emcForm[lbl].value = '';
}

// ------------------------------------------------------------ 
// safeSubmit(): submit form (eg on change of selection criteria) safely, ie without deleting or inserting anything
//               (used on multiple forms, doesn't know which action/key fields to clear, so tries to clear them all)

function safeSubmit()
{   if (window.document.emcForm.evAction)       window.document.emcForm.evAction.value      ='';
    if (window.document.emcForm.secAction)      window.document.emcForm.secAction.value     ='';
    if (window.document.emcForm.locnAction)     window.document.emcForm.locnAction.value    ='';
    if (window.document.emcForm.evtypeAction)   window.document.emcForm.evtypeAction.value  ='';
    if (window.document.emcForm.ppntAction)     window.document.emcForm.ppntAction.value    ='';
    if (window.document.emcForm.regAction)      window.document.emcForm.regAction.value     ='';
    if (window.document.emcForm.rotaAction)     window.document.emcForm.rotaAction.value    ='';
    if (window.document.emcForm.swapAction)     window.document.emcForm.swapAction.value    ='';

    if (window.document.emcForm.editKey)
        if (!window.document.emcForm.rotaAction)
        // don't blank out the key when editing rotas
            window.document.emcForm.editKey.value ='';
    
    window.document.emcForm.submit();
}

// ------------------------------------------------------------ 
// yyyymmdd(): update a date field based on month and year, then submit

function yyyymmdd(m,y,dt)
{   var dtstr=window.document.emcForm[y].value + "-" + window.document.emcForm[m].value + "-01";
    window.document.emcForm[dt].value = dtstr;
    safeSubmit();
}

// ------------------------------------------------------------ 
// Setup for "calendar" popup(date picker)

self.name="BODY";

function calpopup(Ink)
{   window.open(Ink,"calendar","height=250,width=250,scrollbars=no");
}

// ------------------------------------------------------------ 
// iconPicker(): launch the icon selector

function iconPicker(label)
{   var targetUrl="/emcphplib/iconpicker.php?label="+label;
    var features="dependent=yes,height=600,width=400,scrollbars=yes,resizable=yes";
    window.open(targetUrl,"iconPicker",features);
}

// ------------------------------------------------------------ 
// chooseIcon(): choose an icon for display

function chooseIcon(label,icon)
{   opener.document.emcForm[label].value=icon;
    opener.document.images["display_"+label].src = "/emcphplib/images/icons/"+icon;
    window.close();
}

// ------------------------------------------------------------ 
// colourPicker(): launch the colour selector

function colourPicker(fgcolour,fglabel,bgcolour,bglabel)
{   var targetUrl="/emcphplib/colourpicker.php?fgcolour="+fgcolour+"&fglabel="+fglabel+"&bgcolour="+bgcolour+"&bglabel="+bglabel;
    var features="dependent=yes,height=600,width=250,scrollbars=yes,resizable=yes";
    window.open(targetUrl,"colourPicker",features);
}

// ------------------------------------------------------------ 
// Colour picker support functions

function setForeground(label,colour,disp1,disp2)
{   opener.document.emcForm[label].value=colour;

    window.document.getElementById(disp2).style.color = colour;
    opener.document.getElementById(disp1).style.color = colour;
}

function setBackground(label,colour,disp1,disp2)
{   opener.document.emcForm[label].value=colour;

    window.document.getElementById(disp2).style.backgroundColor = colour;
    opener.document.getElementById(disp1).style.backgroundColor = colour;
}

function colourOK()
{   window.close();
}

function colourReset(fglabel,bglabel,disp1,disp2)
{   setForeground(fglabel,'',disp1,disp2);   
    setBackground(bglabel,'',disp1,disp2);   
    window.close();
}

// ------------------------------------------------------------ 
// EventHelpButton(): Launch a help page for maintaining events in the diary

function eventHelpButton()
{   var targetUrl="/emcphplib/eventhelp.php";
    var features="dependent=yes,height=600,width=600,scrollbars=yes,resizable=yes";
    window.open(targetUrl,"eventHelp",features);
}

// ------------------------------------------------------------ 
// swapDuty(): set return valus for a duty swap

function swapDuty(evkey,curppt,orgppt)
{   window.document.emcForm.evkey.value  = evkey;
    window.document.emcForm.curppt.value = curppt;
    window.document.emcForm.orgppt.value = orgppt;
    window.document.emcForm.newppt.value = window.document.emcForm['Ppt'+evkey].value;
    
    window.document.emcForm.swapAction.value = 'update';
    
    window.document.emcForm.submit();
}

// ------------------------------------------------------------ 

function test2() { alert('Yup! Test number 2 works!'); }

function test4() { window.document.emcForm.TEST4.value = 'Good! Test 4 works as well.';}

function test6() 
{   window.document.emcForm.test6Source.value = 'Well, test 6 works too. I think everything is working.';
    window.document.emcForm.submit();
}

