//supporting form script for the application

function spinupImage(timeid) {
    timeobj=document.getElementById(timeid);
    timeobj.src='images/spincontrolup.gif';
}


function spindownImage(timeid) {
    timeobj=document.getElementById(timeid);
    timeobj.src='images/spincontroldown.gif';
}


function spinbasicImage(timeid) {
    timeobj=document.getElementById(timeid);
    timeobj.src='images/spincontrol.gif';
}


function spinup(timeid) {
    spintime(timeid,1);
}


function spindown(timeid) {
    spintime(timeid,-1);
}


//bump the time up in the given display by 30 minutes.  Assumes
//"E MMM d, yyyy h:mm a" format
function spintime(timeid,direction) {
    incrementMinutes=30;
    timeobj=document.getElementById(timeid);
    if(timeobj!=null) {
        timeval=timeobj.value;
        date=appParseDate(timeval);
        minutes=date.getMinutes();
        if(minutes>0) {
            date.setMinutes(0);
            if(direction>0) {
                incrementMinutes=60; }
            else {
                incrementMinutes=0; } }
        millis=date.getTime() + (incrementMinutes*60*1000*direction);
        timeobj.value=appDateStr(new Date(millis));
        reflectRange(timeobj); }
}


//Reflect the date value of the current object in the corresponding
//range date value.  Assumes "E MMM d, yyyy h:mm a" format, and ids
//with ".start." and ".end." in them.  A block may not be
//shorter than the meeting duration, and it may not be longer than 12
//hours.  The upper bound is to prevent people accidentally defining
//huge blocks of time and creating huge quantities of RSVP requests.
function reflectRange(srcobj) {
    //alert('reflectRange ' + srcobj.id);
    srcid=srcobj.id;
    updid=srcobj.id;
    workingFromStart=true;
    startidx=srcid.indexOf('.start.input');
    if(startidx>=0) {
        updid=srcid.substring(0,startidx) + '.end.input'; }
    else {
        workingFromStart=false;
        endidx=srcid.indexOf('.end.input');
        updid=srcid.substring(0,endidx) + '.start.input'; }
    updobj=document.getElementById(updid);
    if(updobj!=null) {
        if(workingFromStart) {
            reflectRangeB(srcobj,updobj,'modEnd'); }
        else {
            reflectRangeB(updobj,srcobj,'modStart'); } }
}


function reflectRangeB(startobj,endobj,change)
{
    var durh=1;
    var durm=0;
    var meetingform=document.getElementById('MeetingDisplay');
    var inputs=meetingform.getElementsByTagName('input');
    for(var i=0;i<inputs.length;i++) {
        if(inputs[i].getAttribute("name")==
                'MeetingDisplay.UIFormContext.ci.duration') {
            var duration=inputs[i].getAttribute("value");
            //alert('reflectRangeB duration ' + duration);
            if(duration.indexOf('30 minutes')>=0) {
                durh=0;
                durm=30; }
            else if(duration.indexOf('half')>=0) {
                durm=30; }
            else {
                durh=parseInt(duration.substring(0,1)); }
            break; } }
    reflectRangeC(startobj,endobj,change,durh,durm);
}


//figure the amount of time needed to add or subtract for the range to
//be appropriate, then add or subtract as needed to the mod obj.
function reflectRangeC(startobj,endobj,change,durh,durm) {
    minminutes=(durh*60)+durm;
    maxminutes=12*60;
    startdate=appParseDate(startobj.value);
    enddate=appParseDate(endobj.value);
    diffminutes=(enddate.getTime()-startdate.getTime())/(1000*60);
    changeminutes=0;
    if(diffminutes<minminutes) {
        changeminutes=minminutes-diffminutes; }
    else if(diffminutes>maxminutes) {
        changeminutes=-1*(diffminutes-maxminutes); }
    //alert('reflectRangeC diffminutes: ' + diffminutes + ', minminutes: ' + minminutes + ', maxminutes: ' + maxminutes + ', changeminutes: ' + changeminutes);
    if(changeminutes!=0) {
        if(change=='modEnd') {
            millis=enddate.getTime() + (changeminutes*60*1000);
            enddate.setTime(millis);
            endobj.value=appDateStr(enddate); }
        else {  //modStart
            millis=startdate.getTime() + (-1*changeminutes*60*1000);
            startdate.setTime(millis);
            startobj.value=appDateStr(startdate); } }
}


var monthnames=new Array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
//return a Date instance initialized from the given string
function appParseDate(datestr) {
    dateparts=datestr.split(' ');
    month=0;
    for(var i=0;i<monthnames.length;i++) {
        if(monthnames[i]==dateparts[1]) {
            month=i;
            break; } }
    day=parseInt(dateparts[2]);
    year=parseInt(dateparts[3]);
    timeparts=dateparts[4].split(':');
    hour=parseInt(timeparts[0]);
    if((hour==12)&&(dateparts[5]=='AM')) {
        hour=0; }
    else if((hour!=12)&&(dateparts[5]=='PM')) {
        hour+=12; }
    minute=parseInt(timeparts[1]);
    return new Date(year,month,day,hour,minute,0,0);
}

var daynames=new Array('Sun','Mon','Tue','Wed','Thu','Fri','Sat');
function appDateStr(date) {
    ampm='AM';
    hour=date.getHours();
    if(hour==0) {
        hour=12; }
    else if(hour==12) {
        ampm='PM'; }
    else if(hour>12) {
        hour-=12;
        ampm='PM'; }
    minutes="00";
    if(date.getMinutes()<=9) {
        minutes="0" + date.getMinutes(); }
    else {
        minutes="" + date.getMinutes(); }
    retval="" + 
        daynames[date.getDay()] + " " + 
        monthnames[date.getMonth()] + " " + date.getDate() + ", " + 
        date.getFullYear() + " " + hour + ":" + minutes + " " + ampm;
    return retval;
}


function focusLastTextEntryField(formID,fieldname) {
    var currform=document.getElementById(formID);
    if(currform != null) {
        var inputs = currform.getElementsByTagName('input');
        if(inputs.length > 0) {
            for(var i=inputs.length-1;i>=0;i--) {
                var input = inputs[i];
                var type = input.getAttribute("type");
                if(type == null || type != "text") {
                    continue; }
                var name = input.getAttribute("name");
                if(name == null || name.indexOf(fieldname) < 0) {
                    continue; }
                input.focus();
                break; } } }
}


function focusToNextButton() {
  if(document.getElementById('linkaction.MeetingDisplay.NextButton')) {
    document.getElementById('linkaction.MeetingDisplay.NextButton').focus(); }
}


function pageToID(formID,uniqueID) {
    var currform=document.getElementById(formID);
    if(currform != null) {
        var ekpid=formID + '.UIFormContext.enterKeyPressed';
        var ekp=document.getElementById(ekpid);
        if(ekp) {
            var pageto='page to:' + uniqueID;
            ekp.setAttribute('value',pageto); }
        document.forms[formID].submit(); }
    return true;
}

//function trapEnterKeyAction(e) {
//    if(!e) var e = window.event;
//    if(e.keyCode) code = e.keyCode;
//    if(e.which) code = e.which;
//    if(code==13) {
//        if(e.srcElement) sourceElem = e.srcElement;
//        if(e.target) sourceElem = e.target;
//        parentForm = sourceElem.parentNode;
//        while((parentForm != null)&&
//              (parentForm.nodeName.toLowerCase() != 'form')) {
//            parentForm = parentForm.parentNode; }
//        if(parentForm!=null) {
//            elemName=parentForm.getAttribute('id');
//            elemName=elemName + ".UIFormContext.enterKeyPressed";
//            var ekp=document.getElementById(elemName);
//            if(ekp) ekp.setAttribute('value','true');
//            };
//        }
//    return true;
//}
//
//window.onload=function() {
//    document.onkeypress = trapEnterKeyAction;
//}

