// JHOMEPAGE.JS  Chawton Park Surgery homepage lists           [06mar2010]

// 01dec2010 automated Xmas (overrides centre box)
// 30sep2011 missed appointments
// 21dec2011 missed appts to be 10 mins each not 15 mins


// ----------------------------------------------------------------------------------------------
// Arrays:    webnew    newsbites    recruitment    missedappts/m
//
// Variables available for subsequent display: centrehtml, webnewbox, box1html, box2html, recruitbox
// ----------------------------------------------------------------------------------------------


// ---------------------------------------------------------------------------------\/[30sep2011]
// Missed appointments       month and number (replaces Recruitment if none available)
// ----------------------------------------------------------------------------------------------
var missedapptm = 'December';
var missedappts = 218;
//                                                                                  /\[30sep2011]

// ----------------------------------------------------------------------------------------------
// New on the website...
// ----------------------------------------------------------------------------------------------
// date | heading | link-url                 ..only use first 5
// ----------------------------------------------------------------------------------------------
var webnew = new Array(

"14feb2012 - Recruitment|docs/20120214.pdf",
"07feb2012 - Medicine waste|hserv.htm",
"01feb2012 - Feb newsletter|news/news201202.pdf",
"01jan2012 - Jan newsletter|news/news201201.pdf",
"01dec2011 - Dec newsletter|news/news201112.pdf",
"01nov2011 - Nov newsletter|news/news201111.pdf",
"01oct2011 - Oct newsletter|news/news201110.pdf",
"01sep2011 - Sep newsletter|news/news201109.pdf",
"03aug2011 - Patient Group page|hppg.htm",

"");


// ----------------------------------------------------------------------------------------------
// From the newsletter...
// ----------------------------------------------------------------------------------------------
// startdate|end-date|header|text...|newsletter-ref (if blank, do NOT show "click for more info")|priority|centre|img!
//  yyyymmdd|yyyymmdd|max 30|max 180|yymm                                                        |y       |y     |y
//  default dates are 00000000 and 99999999  Header beginning with * is a website item, else news.
//  link can be blank, or a refno for a pdf newsletter, or an htmlfile (without .htm), or ANY full link
//  anything other than an htmlfile will be targeted _blank.
// ----------------------------------------------------------------------------------------------
var newsbites = new Array(

"20120131|20120228|Sarah Marks|...vocation to be a trapeze artist, but...|1202",
"20120131|20120228|Patient Participation Group|During October and November last year the PPG carried out a practice survey ...|1202",
"20120131|20120228|New Doctor|Dr Dominita Neagu will be joining us on Monday 6th February as an ST2.|1202",
"20120131|20120228|The dragons...|...put forward their view|1202",

"20111231|20120131|Lynn Kelly|...joined us from Nationwide in January 2001 as a filing clerk at The Alton Health Centre...|1201",
"20111231|20120131|Blood pressure|One of the quickest ways to lower your blood pressure is to eat less salt...|1201",
"20111231|20120131|Poppy appeal|Thank you very much. We raised &pound;133.44 from the box at the reception desk.|1201",
"20111231|20120131|Dalek invasion|At the end of November, we were invaded by a rogue Dalek, now safely contained in the waiting room...|1201",
"20111231|20120131|Anthony's hirsute lip|Anthony sported a rather fetching moustache for Movember to raise money for Prostate and Testicular Cancer, raising &pound;68.00|1201",

"20111128|20111231|Angela Wimshurst|Angela joined the dispensary team here at Chawton Park five years ago from working in retail dispensing...|1112",
"20111128|20111231|Driving with diabetes|Important changes to driving regulations for people with diabetes...|1112",
"20111128|20111231|Talkhealth online clinics|Talkhealth and NHS Choices are running online clinics covering a different topic each month...|1112",
"20111128|20111231|Movember|Anthony's hairy lip - to raise awareness and money towards Testicular and Prostate Cancer...|1112",
"20111128|20111224|Christmas quiz 2011|Some of our doctors and staff have unusual or unlikely hobbies!...|1112",

"20111031|20111231|Terry Cubitt|Dr Cubitt arrived in Alton in 1981 to join the Tomlinson Practice as a partner. When Dr Tomlinson retired in 1997 Dr Cubitt took over as senior partner...|1111",
"20111031|20111231|Sarah Loch|We welcome Sarah Loch back to Chawton Park Surgery. She joined us here to complete her GP training, then she and her husband left to go to Australia for a year...|1111",
"20111031|20111231|Appointments system|Feedback from the new Patient Participation Group suggests that the appointment booking system is sometimes a little confusing, so I will try to de-mystify it...|1111",
"20111031|20111130|Chawton Park baby boom|First we welcomed the arrival of Tim Robinson's first born, then Dr Julian Barber's wife gave birth to a little girl|1111",

"20110929|20111130|Debbie Haden|...receptionist, and sometime Mahout - joined us as a receptionist four years ago.|1110",
"20110929|20111130|Patient Participation Group|...will be conducting a confidential survey.|1110",
"20110929|20111130|Terry's Mammoth Stomp|...completed the enormous walk of 130 miles from Winchester to Canterbury in an amazing 7 days.|1110",
"20110929|20111130|Telephone appointments|It would help us greatly if you could accept ANY of the doctors ringing you back.|1110",
"20110929|20111130|Missed appointments|August - 176 missed = 44 hours of wasted doctor or nurses time; please, please just ring if you are unable to make an appointment.|1110",
"20110929|20111130|Thamesdoc new number|<b>0300 130 1305</b>|1110",



// Elsewhere on the website...

"||*E-newsletter|Get your newsletter by email. Subscribe here.|henews",
"||*News archive|View the latest newsletter... or select from previous newsletters all the way back to 2000.|hnews",
"||*The Practice Team|Get the lowdown on the doctors and the rest of the staff at the surgery.|hteam",
"||*Our services|What the surgery can do for you - see the various facilities we offer, and the conditions we can help with.|hserv",
"||*Local chemists|Names, addresses and telephone numbers for all the local pharmacies, and a guide to where to get prescriptions outside of normal shopping hours.|hchemist",
"||*Hospitals|A list of local (and not so local) hospitals, with addresses and links to websites and maps.|hospital",
"||*Local information|A list of local services related to health matters - dentists, opticians, helplines etc etc.|hlocals",
"||*Document archive|Links to various documents, whether produced by the surgery or from other authorities.|hdocarch",
"||*Where we are|Printable map of the location of the surgery|hloc",
"||*Pictures|A selection of photos of the surgery, from before its construction to the present (more or less!)|hfotos00",
"||*Emergency|How to get help in an emergency - who to call - how to find the out-of-hours service etc|hemerg",

"");


// ----------------------------------------------------------------------------------------------
// Recruitment...
// startdate | enddate | heading | link-url.ext     If none available... "None at present"
// ----------------------------------------------------------------------------------------------

var recruitment = new Array(

"20120214|20120301|Part-time dispenser/receptionist|docs/20120214.pdf",
//"20101003|20101130|Delivery driver|docs/driver.pdf",

"");


// ----------------------------------------------------------------------------------------------
// Processing routines...
// ----------------------------------------------------------------------------------------------

// ----------------------------------------------------------------------------------------------
// Get today's date for checking against entries
// ----------------------------------------------------------------------------------------------
var mydate = new Date();
var myday = mydate.getDate();                     // myday = 17            (1-31)
var mymonth = mydate.getMonth();                  // mymonth = 2           (0-11  0=January)
mymonth++;
var myyear = mydate.getFullYear();                // myyear = 2003
var mytoday = (myyear * 10000) + (mymonth * 100) + myday;
var field = new Array();


// ----------------------------------------------------------------------------------------------
// Select and prepare recruitment entries
// ----------------------------------------------------------------------------------------------
var recruitbox = '';
var recruitmentx = new Array();
var ir = 0;
for (i=0;i<recruitment.length;i++) {
    if (recruitment[i] == '') continue;
    field = recruitment[i].split("|");
    if (field[0] == '') field[0] = '00000000';
    if (field[1] == '') field[1] = '99999999';
    var mystart = field[0] * 1;
    var myend = field[1] * 1;
    if ((mytoday >= mystart) && (mytoday <= myend)) {
        recruitmentx[ir] = '<p class="center"><a href="' + field[3] + '" target="_blank">' + field[2] + '</a></p>';
        ir++;
    }
}

//                                               \/[30sep2011]
if (recruitmentx.length > 0) {
    for (i=0;i<recruitmentx.length;i++) recruitbox += recruitmentx[i];
    recruitbox = '<td class="home11"><p class="center"><img src="pbrecr.gif" border="0" width="120" height="20"></p>' + recruitbox + '</td>';
}

// ----------------------------------------------------------------------------------------------
// If no recruitment, show Missed Appointments
// ----------------------------------------------------------------------------------------------

else {
    var missedappth = Math.ceil(missedappts / 6); // 10 mins per appt          [21dec2011]
    recruitbox = '<td class="home11"><p class="center"><span class="font14">' + missedapptm + '</span><br><span class="font14">' + missedappts + '</span><br><span class="font12">missed appointments</span><br>= ' + missedappth + ' hours lost<br><span class="chem5">PLEASE call and cancel<br>if you can\'t make it.</span></p></td>';
}
//                                               /\[30sep2011]

// ----------------------------------------------------------------------------------------------
// Display latest web changes
// ----------------------------------------------------------------------------------------------
var webnewbox = '';
for (i=0;i<5;i++) {
    field = webnew[i].split("|");
    var myext = field[1].slice(-3);    // check if pdf or htm to determine target
    if (myext == 'htm') var mytarget = '';
    else var mytarget = ' target="_blank"';
    webnewbox += '<br><a href="' + field[1] + '"' + mytarget + '>' + field[0] + '</a>'; 
}
webnewbox = '<td class="home11"><p><b>&nbsp;&nbsp;&nbsp;&nbsp;New on the website..</b><br>' + webnewbox + '</p></td>';


// ----------------------------------------------------------------------------------------------
// Display newsbites/website links
// ----------------------------------------------------------------------------------------------
// Look for items specified for centre box and/or items for other boxes (field[6]), and split into pty or not (field[5]).
// Set up centre box by selecting randomly from pty list and adding any unused items to other-box pty list. 
// If no centre ptys, look in ordinary centres list, and add unused ones to ordinary list.
// If still no centre found, look among ordinary ptys first, then ordinarys.
// For other two boxes, look in ptys first, then ordinarys.

var ptys = new Array();
var ords = new Array();
var ptysc = new Array();
var ordsc = new Array();
var ip = 0;
var ipc = 0;
var io = 0;
var ioc = 0;

for (i=0;i<newsbites.length;i++) {
    field = newsbites[i].split("|");
    if (field.length < 6) field[5] = '';
    if (field.length < 7) field[6] = '';
    if (field.length < 8) field[7] = '';
    if (field[0] == '') field[0] = '00000000';
    if (field[1] == '') field[1] = '99999999';
    var mystart = field[0] * 1;
    var myend = field[1] * 1;
    if ((mytoday >= mystart) && (mytoday <= myend)) {
        if ((field[6] == 'y') && (field[5] == 'y')) {
            ptysc[ipc] = i;
            ipc++;
        }
        if ((field[6] == 'y') && (field[5] == '')) {
            ordsc[ioc] = i;
            ioc++;
        }
        if ((field[6] == '') && (field[5] == 'y')) {
            ptys[ip] = i;
            ip++;
        }
        if ((field[6] == '') && (field[5] == '')) {
            ords[io] = i;
            io++;
        }
    }
}

// ----------------------------------------------------------------------------------------------
// Select centre item
// ----------------------------------------------------------------------------------------------

var centreboxn = 999;

if (ptysc.length > 0) {        // first look for priority centre items
    var myrandomno = parseInt(Math.random() * 1000);
    var myrand = myrandomno % ptysc.length;
    for (i=0;i<ptysc.length;i++) {
        if (i == myrand) centreboxn = ptysc[i];
        else {
            ptys[ip] = ptysc[i];
            ip++;
        }        
    }
}
if (centreboxn == 999) {      // if none found, try for non-pty centre items
    if (ordsc.length > 0) {
        var myrandomno = parseInt(Math.random() * 1000);
        var myrand = myrandomno % ordsc.length;
        for (i=0;i<ordsc.length;i++) {
            if (i == myrand) centreboxn = ordsc[i];
            else {
                ords[io] = ordsc[i];
                io++;
            }
        }
    }
}
if (centreboxn == 999) {      // if none found, try for pty non-centre items
    if (ptys.length > 0) {
        var myrandomno = parseInt(Math.random() * 1000);
        var myrand = myrandomno % ptys.length;
        for (i=0;i<ptys.length;i++) {
            if (i == myrand) {
                centreboxn = ptys[i];
                ptys[i] = 999; // mark as used
            }
        }
    }
}
if (centreboxn == 999) {      // if none found, try for non-pty non-centre items
    if (ords.length > 0) {
        var myrandomno = parseInt(Math.random() * 1000);
        var myrand = myrandomno % ords.length;
        for (i=0;i<ords.length;i++) {
            if (i == myrand) {
                centreboxn = ords[i];
                ords[i] = 999;  // mark as used
            }
        }
    }
}

// ----------------------------------------------------------------------------------------------
// Now look for further 2 box items
// ----------------------------------------------------------------------------------------------

var box1 = 999;
var box2 = 999;
ordsc.length = 0;
ptysc.length = 0;
for (i=0;i<ords.length;i++) {
    if (ords[i] != 999) {
        ordsc[ioc] = ords[i];
        ioc++;
    }
}
for (i=0;i<ptys.length;i++) {
    if (ptys[i] != 999) {
        ptysc[ipc] = ords[i];
        ipc++;
    }
}

// Box1

if (ptysc.length > 0) {        // first look for priority items
    var myrandomno = parseInt(Math.random() * 1000);
    var myrand = myrandomno % ptysc.length;
    box1 = ptysc[myrand];
}
if (box1 == 999) {      // if none found, try for non-pty items
    if (ordsc.length > 0) {
        var myrandomno = parseInt(Math.random() * 1000);
        var myrand = myrandomno % ordsc.length;
        box1 = ordsc[myrand];
    }
}

// Box2

if (ptysc.length > 1) {        // first look for priority items
    for (i=0;i<100;i++) {
        var myrandomno = parseInt(Math.random() * 1000);
        var myrand = myrandomno % ptysc.length;
        box2 = ptysc[myrand];
        if (box2 != box1) break;  // ensure no duplicate
    }    
}
if (box2 == 999) {      // if none found, try for non-pty items
    if (ordsc.length > 0) {
        for (i=0;i<100;i++) {
            var myrandomno = parseInt(Math.random() * 1000);
            var myrand = myrandomno % ordsc.length;
            box2 = ordsc[myrand];
            if (box2 != box1) break;  // ensure no duplicate
        }
    }
}

// ----------------------------------------------------------------------------------------------
// Now convert into HTML
// ----------------------------------------------------------------------------------------------

// Centre box

//                                         \/[01dec2010]

if (xmastime == 'yes') var centrehtml = '<td class="home00a" colspan="2" rowspan="2" valign="middle" align="center"><img src="xmashappy.png" border="0" width="350" height="250" title="Happy Christmas"></td>'; // Xmas image
else {

    field = newsbites[centreboxn].split("|");
    if (field.length < 6) field[5] = '';
    if (field.length < 7) field[6] = '';
    if (field.length < 8) field[7] = '';
    var newsor = field[2].substr(0,1);  // marker for news v web (* = web)
    if (newsor == '*') {
        field[2] = field[2].slice(1);
        var descc = '<i>Elsewhere on the website</i>...<br>';
    }
    else {
        var descc = '<i>From the news</i>...<br>';
        newsor = '';
    }
    if (field[6] == 'y') descc = '';  // no pre-heading for specified centre box items
    if (field[7] != 'y') field[7] = '';  // set up class for background image if reqd

    // Prepare the link if present

    var myhref = '';
    if (field[4] != '') {
        var mytarget = ' target="_blank"';
        if (field[4].substr(-4,1) != '.') {  // no dot in appropriate place so set default ext
            if (newsor == '') field[4] = 'news/news20' + field[4] + '.pdf';
            else {
                field[4] += '.htm';
                mytarget = '';
            }
        }
        myhref = '<br><a href="' + field[4] + '"' + mytarget + '>[CLICK FOR MORE INFO]</a>';
    }

    var centrehtml = '<td class="home00a' + field[7] + '" colspan="2" rowspan="2">' + descc + '<span class="font14">' + field[2] + '</span><br>' + field[3] + myhref + '</td>';
}
//                                         /\[01dec2010]

// Box 1

field = newsbites[box1].split("|");
if (field.length < 6) field[5] = '';
if (field.length < 7) field[6] = '';
if (field.length < 8) field[7] = '';var newsor = field[2].substr(0,1);  // marker for news v web (* = web)
if (newsor == '*') {
    field[2] = field[2].slice(1);
    var descc = '<i>Elsewhere on the website</i>...<br>';
}
else {
    var descc = '<i>From the news</i>...<br>';
    newsor = '';
}

// Prepare the link if present

var myhref = '';
if (field[4] != '') {
    var mytarget = ' target="_blank"';
    if (field[4].substr(-4,1) != '.') {  // no dot in appropriate place so set default ext
        if (newsor == '') field[4] = 'news/news20' + field[4] + '.pdf';
        else {
            field[4] += '.htm';
            mytarget = '';
        }
    }
    myhref = '<br><a href="' + field[4] + '"' + mytarget + '>[CLICK FOR MORE INFO]</a>';
}

var box1html = '<td class="home11"><p>' + descc + '<b>&nbsp;&nbsp;&nbsp;&nbsp;' + field[2] + '</b><br>' + field[3] + myhref + '</p></td>';

// Box 2

field = newsbites[box2].split("|");
if (field.length < 6) field[5] = '';
if (field.length < 7) field[6] = '';
if (field.length < 8) field[7] = '';
var newsor = field[2].substr(0,1);  // marker for news v web (* = web)
if (newsor == '*') {
    field[2] = field[2].slice(1);
    var descc = '<i>Elsewhere on the website</i>...<br>';
}
else {
    var descc = '<i>From the news</i>...<br>';
    newsor = '';
}

// Prepare the link if present

var myhref = '';
if (field[4] != '') {
    var mytarget = ' target="_blank"';
    if (field[4].substr(-4,1) != '.') {  // no dot in appropriate place so set default ext
        if (newsor == '') field[4] = 'news/news20' + field[4] + '.pdf';
        else {
            field[4] += '.htm';
            mytarget = '';
        }
    }
    myhref = '<br><a href="' + field[4] + '"' + mytarget + '>[CLICK FOR MORE INFO]</a>';
}

var box2html = '<td class="home11"><p>' + descc + '<b>&nbsp;&nbsp;&nbsp;&nbsp;' + field[2] + '</b><br>' + field[3] + myhref + '</p></td>';

// THE END --------------------------------------------------------------------------------------

