//--********************************************************************-->
//--                                                                    -->
//--   Frontier Software Pty Ltd                                        -->
//--                                                                    -->
//--   File Name     : ftrtime.js                                       -->
//--                                                                    -->
//--   History       :                                                  -->
//--   012039 - 02.07.06 - Created.                                     -->
//--   013668 - 02.03.07 - All times in hh:mm format                    -->
//--   013901 - 10.05.07 - loadPageCurrent function created             -->
//--********************************************************************-->
//  Written by Tan Ling wee                                             -->
//  on 19 June 2005                                                     -->
//  email : info@sparrowscripts.com                                     -->
//  url : www.sparrowscripts.com                                        -->
//--********************************************************************-->
//-- Notes:                                                             -->
//-- ftrtime contains functions used to rework the basic timesheet page -->
//-- to allow for any number of KTC codes to appear on the              -->
//-- New Timesheet page                                                 -->
//--********************************************************************-->

// *********************************************************************
var oTimeField;

// *********************************************************************
function timeBuild() {
    var sHtml = "<table border='0' width='100%' cellpadding='0' cellspacing='0' bgcolor='" + hAlternate + "'>";
    sHtml += "<tr bgcolor='" + hVertBG + "'><td>";
    sHtml += "<table cellpadding=0 cellspacing=0 width='100%'><tr>";
    sHtml += "<td style='color:" + ((bHorizMenu) ? hHorizInactive : hVertInactive) + ";padding:3px' valign=center><B>&nbsp;&nbsp;Select a Time&nbsp;&nbsp;</B></td>";
    sHtml += "<td style='color:" + ((bHorizMenu) ? hHorizInactive : hVertInactive) + ";padding:3px' valign=center>AM&nbsp;<input type='radio' name='iconAMPM' id='iconAM' value='AM' checked onclick='oGlob.timeLoadField(false);' style='cursor:hand'></td>";
    sHtml += "<td style='color:" + ((bHorizMenu) ? hHorizInactive : hVertInactive) + ";padding:3px' valign=center>PM&nbsp;<input type='radio' name='iconAMPM' id='iconPM' value='PM' onclick='oGlob.timeLoadField(true);' style='cursor:hand'></td>";
    sHtml += "</tr></table>";
    sHtml += "</td></tr><tr><td colspan=2><span id='timePickerContent'>"
    sHtml += timeCreateContent(false);
    sHtml += "</span></td></tr>";
    if (aDictDetail["setimvew"]["back"]) {
        sHtml += "<tr><td height='2' style='padding:0'; class='horizRule'><img src='" + loadPageResource("menu", true) + "pixel.png' width='1' height='1' border='0' alt=''></td></tr>";
        sHtml += "<tr><td height='2'></td></tr>";
        sHtml += "<tr><td align='right'><input style='height:18px;font:9px;margin:3px;cursor:pointer;cursor:hand;' type='button' onClick='oGlob.oTimeWin.close(); return true;' value='" + aDictDetail["setimvew"]["back"].labeltext + "' title='" + aDictDetail["setimvew"]["back"].tooltip + "'></td></tr>";
    }
    sHtml += "</table>";
    return sHtml;
}

// *********************************************************************
function timeCreateContent(bPMMode) {
    var iRow, iHour, iMin, sSuffix;
    if (bPMMode) { 
        sSuffix = "pm"; 
    } else { 
        sSuffix = "am"; 
    }
    var sHtml = "<table><tr><td><table border='0' width='100%' cellpadding='2' cellspacing='0' bgcolor='" + hAlternate + "'>";
    for (iRow=0;iRow<=11;iRow++) {
        sHtml+="<tr align=right style='font-size:9px;'>";
        if (iRow == 0) {
            iHour = 12;
        } else {
            iHour = iRow;
        }
        for (iMin=0;iMin<4;iMin++) {
            sHtml += "<td width=57 style='cursor:hand' onmouseover='this.style.backgroundColor=\"" + hHighlight + "\";' onmouseout='this.style.backgroundColor=\"\";' onclick='oGlob.timeReturnValue(\""+ timePadHour(iHour,sSuffix) + ":" + timePadMin(iMin*15) + "\")'>";
            sHtml += "<a style='text-decoration:none;color:#000000' href='javascript:oGlob.timeReturnValue(\""+ timePadHour(iHour,sSuffix) + ":" + timePadMin(iMin*15) + "\")'>" + iHour + ":" + timePadMin(iMin*15) + sSuffix + "</a>";
            sHtml += "</td>";
        }
        sHtml+="</tr>";
    }
    sHtml += "</table></td></tr></table>";
    return sHtml;
}

// *********************************************************************
function timeSetField(oField) {
    // Assign the incoming field object to a global variable
    oTimeField = oField;
    return;
}

// *********************************************************************
function timeLoadField(bPMMode) {
    oTimeWin.document.getElementById("timePickerContent").innerHTML = timeCreateContent(bPMMode);
    return;
}

// *********************************************************************
function timeReturnValue(sTime) {
    // Set the value of the field that was passed in
    oTimeField.value = sTime;
    // Give focus back to the time field
    oTimeField.focus();
    // Form Field has been modified
    if ((bModifyAlert) && (oDataPage.document.getElementById("updat"))) {
        bModified = true;
    }
    // Close the Time Window
    oTimeWin.close();
    return;
}

// *********************************************************************
function timePadHour(iHour,sAMPM) {
    if (sAMPM == "am") {
        if (iHour == 12) {
            iHour = 0;
        }
    } else {
        if (iHour == 12) {
            iHour = 12;
        } else {
            iHour += 12;
        }
    }
    if (iHour < 10){ 
        return ('0' + iHour);
    } else {
        return iHour;
    }
}

// *********************************************************************
function timePadMin(iMin) {
    if (iMin < 10){ 
        return ('0' + iMin);
    } else {
        return iMin;
    }
}

// *********************************************************************
var sForm = "";
var aKtcCodes = new Array();
var iSequence = 1;

// *********************************************************************
function timeSetValues() {
    var sTempPage = oGlob.loadPageCurrent();
    sForm = sTempPage.substr(2,3);
    return;
}

// *********************************************************************
function timeInitTable() {
    var oTable = document.getElementById("ktcCodes");
    if (sForm == "kta") {
        var oDiv = document.getElementById("staffHeader");
        oDiv.innerHTML = oGlob.hdftStaffHeader(1);
    }
    var oHeaderRow = oTable.insertRow(0);
    var oCell = oHeaderRow.insertCell(oHeaderRow.cells.length);
    oCell.align = "left"; 
    oCell.className = "Prompt";
    oCell.style.color = oGlob.hPrompt;
    oCell.innerHTML = oDict[sForm + "date"].label + (oDict[sForm + "date"].mandatory ? "&nbsp;<font style='color:" + oGlob.hError + ";font:bold;'>*</font>" : "");
    oCell = oHeaderRow.insertCell(oHeaderRow.cells.length);
    oCell.align = "left";
    var sInput = "<input type='text' name='" + sForm + "date' maxlength='10' style='width:70px;' class='Key' style='background-color:" + oGlob.hKey + ";' onblur='if (this.value == \"\") this.value=oGlob.sToday; return true;'";
    if (oGlob.bDrillMode) sInput += " " + oGlob.sDisplayOnly;
    sInput += " >";
    if (sForm == "kts") sInput += oGlob.hdftCalendar("cal","document.detForm." + sForm + "date");
    oCell.innerHTML = sInput;
    oHeaderRow = oTable.insertRow(1);
    timeAddHeadings(oHeaderRow);
    if (oTable.rows[2] && oTable.rows[2].cells.length > 3) timeAddHeadings(oHeaderRow);
    return;
}

// *********************************************************************
function timeAddHeadings(oHeaderRow) {
    var oCell = oHeaderRow.insertCell(oHeaderRow.cells.length);
    oCell.innerHTML = "&nbsp;"
    oCell = oHeaderRow.insertCell(oHeaderRow.cells.length);
    oCell.className = "Prompt";
    oCell.style.color = oGlob.hPrompt;
    oCell.innerHTML = oDict["i_hours"].label;
    oCell = oHeaderRow.insertCell(oHeaderRow.cells.length);
    oCell.className = "Prompt";
    oCell.style.color = oGlob.hPrompt;
    oCell.innerHTML = oGlob.sLevelToCostFor;
    if ((oDict["i_comment"]) && (oDict["i_comment"]["comment"]) && (oDict["i_comment"]["comment"].toString().indexOf("*") != -1)) oCell.innerHTML += " *";
    return;
}

// *********************************************************************
function timeAddButtons() {
    var sTemp;
    switch (sForm) {
        case "kta":
            buttons = new Array("back","appr","decl","nte");
            break;
        default:
            buttons = new Array("cal","back","del","updat","nte");
            break;
    }
    // Preload all the buttons for the page
    locnImageCreate(buttons);
    // Process the buttons for the page
    oParent = document.getElementById("ktcTable");
    var oTempDiv = document.createElement("div");
    oTempDiv.innerHTML = oGlob.hdftButtonCreate(buttons);
    oParent.appendChild(oTempDiv);
    return;
}

// *********************************************************************
function timeAddComments() {
    var oDiv = document.createElement("div");
    var sTemp = "";
    sTemp += "<table width='100%' cellpadding=0 cellspacing=0 style='margin-left:5px;'><tr><td width='5px' rowspan='6'><img src='" + oGlob.loadPageResource("menu") + "pixel.png' width='5px' height='1' alt=''></td></tr>";
    if (oDict["i_comment"]) sTemp += "<tr><td class='Comment' style='color:" + oDict["i_comment"]["colour"] + ";' colspan='2'>" + oDict["i_comment"]["comment"] + "</td></tr>";
    //8366 - Add link to workflow notes page if required
    if (oDict["i_notes"]) sTemp += oGlob.hdftFieldCreate(((oGlob.hdftDisplayOnly(sForm == "kta")) ? "nte" : "textarea"),"i_notes");
    oTemp = eval("oDict['" + sForm + "status']"); 
    if (oTemp) {
        sTemp += "<tr><td class='Prompt' style='color:" + oGlob.hPrompt + ";' id='" + sForm + "status'>" + oTemp["label"];
        if (oTemp["mandatory"]) sTemp += "&nbsp;<font style='color:" + oGlob.hError + ";font:bold;'>*</font>";
    }
    sTemp += "</td><td><input type='text' name='" + sForm + "status' " + oGlob.hdftDisplayOnly(true) + "></td></tr></table>";
    oDiv.innerHTML = sTemp;
    oParent = document.getElementById("ktcTable");
    oParent.appendChild(oDiv);
    return;
}

// *********************************************************************
function timeAddCell(oTable, sText, sKtcCode, sIndex) {
    var aStyles;
    var oCurrentRow;
    if (oTable.rows) oCurrentRow = oTable.rows[oTable.rows.length-1];
    else oCurrentRow = oTable.insertRow(oTable.rows.length);
    if (oCurrentRow.cells && oCurrentRow.cells.length >= 4) {
        oCurrentRow = oTable.insertRow(oTable.rows.length);
    }
    var oCell = oCurrentRow.insertCell(oCurrentRow.cells.length);
    oCell.className = "Prompt";
    oCell.style.color = oGlob.hPrompt;
    oCell.innerHTML = sText;
    oCell.id = sKtcCode + "_" + sIndex;
    oCell = oCurrentRow.insertCell(oCurrentRow.cells.length);
    oCell.id = sForm + "allow_" + sKtcCode + "_" + sIndex;

    var sName = sForm + "allow_" + sKtcCode + "_" + iSequence + "_" + sIndex;
    aStyles = new Array();
    aStyles["width"] = "70px";
    var oInput = timeCreateInput(sName, sName, "text", null, "8", aStyles);
    oCell.appendChild(oInput);

    var sHTML = "";
    sHTML += "<input type='text' id='" + sForm + "costcd_" + sKtcCode + "_" + iSequence + "_" + sIndex + "'";
    sHTML += " maxlength='4' style='width:70px;'";
    sHTML += " " + oGlob.hdftDisplayOnly(sForm == "kta") + ">";
    if (sForm != "kta") sHTML += oGlob.hdftSelect("gna_1","esgnasel","document.detForm." + sForm + "costcd_" + sKtcCode + "_" + iSequence + "_" +sIndex);
    var oCell = oCurrentRow.insertCell(oCurrentRow.cells.length);
    oCell.innerHTML = sHTML;
    return;
}

// *********************************************************************
function timeCreateInput(name, id, type, className, maxLength, aStyles) {
    var oInput = document.createElement("input");
    if (type) oInput.type = type; 
    if (name) oInput.name = name;
    if (id) oInput.id = id;
    if (className) oInput.className = className;
    if (maxLength) oInput.maxLength = maxLength;
    if (aStyles) for (var style in aStyles) oInput.style[style] = aStyles[style];
    oInput.disabled = oGlob.hdftDisplayOnly(sForm == "kta");
    return oInput;
}

// *********************************************************************
function timeRenderTable() {
    iSequence = 1;
    timeInitTable();
    timeAddComments();
    timeAddButtons();
    clearClick();
    return;
}

// *********************************************************************
