﻿function $(sId)
{
   return(document.getElementById(sId));
}

function S4()
{
   return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
}

function GUID()
{
   return (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4());
}

function scrollIntoViewIfOutOfView(el)
{
    var topOfPage = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop;
    var heightOfPage = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
    var elY = 0;
    var elH = 0;
    if (document.layers)
    { // NS4 
        elY = el.y;
        elH = el.height;
    }
    else
    {
        for (var p = el; p && p.tagName != 'BODY'; p = p.offsetParent)
        {
            elY += p.offsetTop;
        }
        elH = el.offsetHeight;
    }
    if ((topOfPage + heightOfPage) < (elY + elH))
    {
        el.scrollIntoView(false);
    }
    else if (elY < topOfPage)
    {
        el.scrollIntoView(true);
    }
}

function ParseKey(sTemp, sKey)
{
   if (sTemp.indexOf(sKey) < 0)
   {
      return('');
   }

   if (sTemp.substring(sTemp.indexOf(sKey) + sKey.length).indexOf('&') > -1)
   {
      sTemp = sTemp.substring(sTemp.indexOf(sKey) + sKey.length);
      sTemp = sTemp.substring(0, sTemp.indexOf('&'));
      return(sTemp);
   }
   else
   {
      return(sTemp.substring(sTemp.indexOf(sKey) + sKey.length));
   }
}

function CheckLoginResult(sResult, sCaller)
{
    if (sCaller == "SignInPage")
    {
        CheckLoginResult2(sResult);
        return;
    }
    
    if (sResult != "")
    {
        g_sUserFirstName = sResult.split('|')[0] + " " + sResult.split('|')[1];
        g_nRole = sResult.split('|')[2];
        g_nUserId = sResult.split('|')[3];
        g_sJobPagePermissions = sResult.split('|')[4];
        g_nUserType = sResult.split('|')[5];
        
        if ((location.href.indexOf('Home.aspx') < 0) && (location.href.indexOf('.aspx') < 0))
        {
            location.href = location.href;
        }
    }
    else
    {
        alert("Email address and/or password incorrect.\nPlease try again.");
        document.getElementById("SignInPassword").value = '';
        g_sUserFirstName = '';
        g_nRole = 0;
    }

    CheckIsLoggedIn();
    AddOperationalButtons();
}

function AfterSignOut(sResult)
{
    if (sResult == "Ok")
    {
        g_sUserFirstName = '';
        
        if ((location.href.indexOf('Home.aspx') < 0) && (location.href.indexOf('Default.aspx') < 0))
        {
            location.href = 'Home.aspx' + location.search;
        }
        
        if ($('OperationalButtonsSpan') != null)
        {
            $('OperationalButtonsSpan').innerHTML = '';
        }
    }
    
    CheckIsLoggedIn();
}

function TryLogin()
{
    GetFromServer("TryLogin", document.getElementById("SignInEmailAddress").value + "|" + document.getElementById("SignInPassword").value + "|" + document.getElementById("SignInRememberMe").checked, "");
}

function SignOut()
{
    try
    {
        document.getElementById("SignInEmailAddress").value = '';
        document.getElementById("SignInPassword").value = '';
        document.getElementById("SignInRememberMe").checked = false;
    }
    catch(e) { }
    
    GetFromServer("SignOut", "", "");
}

function IsLoggedIn()
{
    return (g_sUserFirstName != '');
}

function CheckIsLoggedIn()
{
    $('UserName').innerHTML = g_sUserFirstName;

    if (g_sUserFirstName != '')
    {
        $('SignOutTab').style.display = '';
        //$('MainTab1').style.display = '';
        
        if ($("LoggedInText") != null)
        {
            $("LoggedInText").style.display = '';
        }
        
        if ($('LoginTitle') != null)
        {
            $("LoginTitle").src = 'images/Title-Welcome1.png';
        }
        
        if ($("LogInForm") != null)
        {
            $("LogInForm").style.display = 'none';
        }
        
        if ($("LoggedInUserFirstName") != null)
        {
            $("LoggedInUserFirstName").innerHTML = g_sUserFirstName;
        }
    }
    else
    {
        try
        {
            $('MainTab1').style.display = 'none';
            $('SignOutTab').style.display = 'none';
            $("LogInForm").style.display = '';
            $("LoggedInText").style.display = 'none';
            $("LoginTitle").src = 'images/Title-MemberLogIn1.png';
            $('MainTab6').style.display = 'none';
        }
        catch(e) { }
    }
}

function ParsePrice(dPrice)
{
    var sTemp = '';
    dPrice = dPrice + '';

    if (dPrice.indexOf('.') < 0)
    {
        sTemp = dPrice + '.00';
    }
    else
    {
        sTemp += dPrice.substring(0, dPrice.indexOf('.'));
        sTemp += dPrice.substring(dPrice.indexOf('.'), dPrice.indexOf('.') + 3);
        
        for (var i = 0; i < 3 - dPrice.substring(dPrice.indexOf('.'), dPrice.indexOf('.') + 3).length; i++)
        {
            sTemp += '0';
        }
    }

    return(sTemp);
}

function GetClientSize()
{
    var myWidth = 0, myHeight = 0;
    
    if(typeof(window.innerWidth) == 'number')
    {
        //Non-IE
        myWidth = window.innerWidth;
        myHeight = window.innerHeight;
    }
    else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight))
         {
            //IE 6+ in 'standards compliant mode'
            myWidth = document.documentElement.clientWidth;
            myHeight = document.documentElement.clientHeight;
         }
         else if (document.body && (document.body.clientWidth || document.body.clientHeight))
              {
                  //IE 4 compatible
                  myWidth = document.body.clientWidth;
                  myHeight = document.body.clientHeight;
              }
              
    return [myWidth, myHeight];
}

var hD="0123456789ABCDEF";

function Dec2Hex(d) 
{
    var h = hD.substr(d&15,1);
    
    while (d>15)
    {
        d>>=4;
        h=hD.substr(d&15,1)+h;
    }
    
    return h;
}

function Hex2Dec(h)
{
    return parseInt(h,16);
} 

function SetProperty(Section, Keyword, Value, Caller)
{
    GetFromServer("SetProperty", g_nGroupId + "|" + Section + "|" + Keyword + "|" + Value, Caller);
}

function SetPropertyClient(Section, Keyword, Value)
{
    GetFromServer("SetPropertyClient", g_nGroupId + "|" + Section + "|" + Keyword + "|" + Value, "");
}

function AddBtn(sCaption, sCSSClass, sFunc, sButtonId, sCaptionId, bIsThereHoverState, sPaddingClass)
{
    if (sButtonId == null)
    {
        sButtonId = '';
    }

    if (sCaptionId == null)
    {
        sCaptionId = '';
    }
    
    if (bIsThereHoverState == null)
    {
        bIsThereHoverState = true;
    }

    if (sPaddingClass == null)
    {
        sPaddingClass = 'ButtonPadding';
    }

    if (navigator.userAgent.indexOf('MSIE') > 0)
    {
        document.write('<a href="javascript:' + sFunc + ';void(0);" id="' + sButtonId + '" class="Btn ' + sCSSClass + '"><span class="' + sPaddingClass + '" id="' + sCaptionId + '">' + sCaption + '</span></a>');
    }
    else
    {
        document.write('<table cellspacing="0" cellpadding="0" id="' + sButtonId + '" class="Btn ' + sCSSClass + '" ' + (bIsThereHoverState ? 'onmouseover="this.className=\'Btn ' + sCSSClass + 'Hover\';" onmouseout="this.className=\'Btn ' + sCSSClass + '\';"' : '') + ' onclick="' + sFunc + '"><tr><td valign="top" style="cursor:pointer;"><a href="javascript:void(0);" class="Btn"><span id="' + sCaptionId + '">' + sCaption + '</a></td></tr></table>');
    }
}

function getAbsX(elt)
{
    return (elt.x) ? elt.x : getAbsPos(elt,"Left");
}

function getAbsY(elt)
{
    return (elt.y) ? elt.y : getAbsPos(elt,"Top");
}

function getAbsPos(elt,which)
{
    iPos = 0;
    while (elt != null)
    {
        iPos += elt["offset" + which];
        elt = elt.offsetParent;
    }

    return iPos;
}

function getClientHeight()
{
    var myWidth;
    var myHeight;

    if( typeof( window.innerWidth ) == 'number' ) { 

    //Non-IE 

    myWidth = window.innerWidth;
    myHeight = window.innerHeight; 

    } else if( document.documentElement && 

    ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) { 

    //IE 6+ in 'standards compliant mode' 

    myWidth = document.documentElement.clientWidth; 
    myHeight = document.documentElement.clientHeight; 

    } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) { 

    //IE 4 compatible 

    myWidth = document.body.clientWidth; 
    myHeight = document.body.clientHeight; 

    } 
    
    return(myHeight);
}

function AddZero(n)
{
    n = '' + n;
    return ((n.length < 2) ? '0' + n : n);
}

function ReplaceAll(SourceString, OrigSubStr, NewSubStr)
{
    var sTemp = SourceString;
    
    while (sTemp.indexOf(OrigSubStr) > -1)
    {
        sTemp = sTemp.replace(OrigSubStr, NewSubStr);
    }
    
    return(sTemp);
}

function GetUsersListHTML(TeamIndex, UserPos, asUsers, sFilter, bShowClear, sFunc)
{
    if (sFunc == null)
    {
        sFunc = "AdminChangedTeamMember";
    }

    var bFound = false;
    var sTemp = '';
    sTemp += '<table cellspacing="0" cellpadding="2" style="width:100%;font-family:Tahoma;font-size:10px;font-weight:bold;background-color:#ffffff;">';    
    
    if (bShowClear)
    {
        sTemp += '<tr>';
        sTemp += '<td style="border-bottom:1px solid #9999cc;width:35px;background-color:#ffffff;" align="center"><img src="images/teamX.png" width="32" height="32" align="absmiddle"></td>';
        sTemp += '<td style="border-bottom:1px solid #9999cc;"><a href="javascript:' + sFunc + '(' + TeamIndex + ', \'\', ' + UserPos + ');if ($(\'UsersList\')) document.body.removeChild($(\'UsersList\'));$(\'PopupBackground\').style.display=\'none\';void(0);void(0);">(Clear Selection)</a></td>';
        sTemp += '</tr>';
            
        bFound = true;
    }
    
    var sTempMembersForToday = ',' + g_sMembersForToday + ',';
    
    for (var i = 0; i < asUsers.length; i++)
    {
        if (asUsers[i] == '')
        {
            continue;
        }

        if ((g_sDefaultWorkingMode != "A") && (sTempMembersForToday.indexOf(',' + asUsers[i].split('|')[0] + ',') > -1))
        {
            continue;
        }
        
        if ((sFilter == '') || (asUsers[i].split('|')[1].toUpperCase().indexOf(sFilter.toUpperCase()) == 0) || (asUsers[i].split('|')[2].toUpperCase().indexOf(sFilter.toUpperCase()) == 0))
        {
            sTemp += '<tr>';
            sTemp += '<td style="' + ((i != asUsers.length - 1) ? 'border-bottom:1px solid #9999cc;' : '') + 'width:35px;" align="center">';
            
            if (false) // was (asUsers[i].split('|')[8] == "1")
            {
                sTemp += '<img src="UserPhoto.aspx?id=' + asUsers[i].split('|')[0] + '&width=32&height=32&force=1" align="absmiddle">';
            }
            else
            {
                if (asUsers[i].split('|')[7] == "True")
                {
                    sTemp += '<img src="images/teamMan.png" width="32" height="32" align="absmiddle">';
                }
                else
                {
                    sTemp += '<img src="images/teamWoman.png" width="32" height="32" align="absmiddle">';
                }
            }
            
            sTemp += '</td>';
            sTemp += '<td' + ((i != asUsers.length - 1) ? ' style="border-bottom:1px solid #9999cc;"' : '') + '><a href="javascript:' + sFunc + '(' + TeamIndex + ', ' + asUsers[i].split('|')[0] + ', ' + UserPos + ');if ($(\'UsersList\')) document.body.removeChild($(\'UsersList\'));$(\'PopupBackground\').style.display=\'none\';void(0);void(0);">' + asUsers[i].split('|')[1] + ' ' + asUsers[i].split('|')[2] + '</a>' + ((asUsers[i].split('|')[9] != "") ? '<br>Cell : ' + FormatPhoneNumber(asUsers[i].split('|')[9]) : ((asUsers[i].split('|')[6] != "" ) ? '<br>Home : ' + FormatPhoneNumber(asUsers[i].split('|')[6]) : '') ) + '</td>';
            sTemp += '</tr>';
            
            bFound = true;
        }
    }
    
    if (!bFound)
    {
        sTemp += '<tr>';
        sTemp += '<td>No matches found.</td>';
        sTemp += '</tr>';
    }
    
    sTemp += '</table>';
    
    return(sTemp);
}

var g_sFilteredUserList = '';

function ShowUsersList(TeamIndex, UserPos, srcElement, asUsers, UserId, bShowClear, sFunc)
{
    if ($('UsersList'))
    {
        document.body.removeChild($('UsersList'));
    }
    
    g_sFilteredUserList = asUsers;
    var oUsersList = document.createElement('div');
    oUsersList.id = 'UsersList';
    oUsersList.style.position = 'absolute';
    oUsersList.style.border = '1px solid #9999cc';
    oUsersList.style.display = '';
    oUsersList.style.width = '200px';
    oUsersList.style.height = '300px';
    oUsersList.style.zIndex = '99999';
    oUsersList.style.left = getAbsX(srcElement);
    oUsersList.style.top = Math.max(getAbsY(srcElement) - 250 - $('MainDiv').scrollTop, 0);
    
    var sTemp = '';
    sTemp += '<table cellspacing="0" cellpadding="0" width="100%" height="100%" style="background-color:#ffffff;z-index:99999;">';
    sTemp += '<tr><td style="height:25px;background-color:#003366;color:#ffffff;font-family:Tahoma;font-size:10px;font-weight:bold;padding-left:5px;border-bottom:1px solid #9999cc;" valign="middle">Search: <input type="textbox" style="width:135px;border:1px solid #000099;font-family:Tahoma;font-size:10px;font-weight:bold;" onkeyup="$(\'UsersListDiv\').innerHTML=GetUsersListHTML(' + TeamIndex + ', ' + UserPos + ', g_sFilteredUserList, this.value, ' + bShowClear + ');">&nbsp;&nbsp;<a href="javascript:if ($(\'UsersList\')) document.body.removeChild($(\'UsersList\'));$(\'PopupBackground\').style.display=\'none\';void(0);" style="color:#ffffff;text-decoration:none;">X</a></td></tr>';
    sTemp += '<tr><td valign="top"><div id="UsersListDiv" style="width:100%;height:100%;overflow-y:auto;">';
    sTemp += GetUsersListHTML(TeamIndex, UserPos, asUsers, '', bShowClear, sFunc);
    sTemp += '</div></td></tr>';
    sTemp += '</table>';

    oUsersList.innerHTML = sTemp;
    
    document.body.appendChild(oUsersList);
}

function AddOperationalButtons()
{
    var sTemp = "";

    if ($('OperationalButtonsSpan') == null)
    {
        sTemp += "<span id=\"OperationalButtonsSpan\">";
    }
    
    if (g_sUserFirstName == "")
    {
        sTemp += '<span style="visibility:hidden;">';
    }

    if (g_sJobPagePermissions == 1)
    {
        sTemp += '<a href="javascript:location.href=\'Jobs.aspx?group=' + ParseKey(location.search, 'group=') + '\';void(0);"><img src="images/BtnDispatch.png" border="0" title="Dispatch" onmouseover="this.src=this.src.replace(\'.png\', \'2.png\');" onmouseout="this.src=this.src.replace(\'2.png\', \'.png\');" /></a>&nbsp;';
    }

    if (g_nUserType > 0)
    {
        sTemp += '<a href="javascript:location.href=\'Emails.aspx?group=' + ParseKey(location.search, 'group=') + '\';void(0);"><img src="images/BtnEmail.png" border="0" title="Email Management" onmouseover="this.src=this.src.replace(\'.png\', \'2.png\');" onmouseout="this.src=this.src.replace(\'2.png\', \'.png\');" /></a>&nbsp;';
    }

    if (g_nRole == 1)
    {
        sTemp += '<a href="javascript:location.href=\'AdminGroupProfile.aspx?group=' + ParseKey(location.search, 'group=') + '\';void(0);"><img src="images/BtnParm.png" border="0" title="Group Settings" onmouseover="this.src=this.src.replace(\'.png\', \'2.png\');" onmouseout="this.src=this.src.replace(\'2.png\', \'.png\');" /></a>&nbsp;';
    }

    if (g_nUserType > 0)
    {
        sTemp += '<a href="javascript:location.href=\'AdminUsers.aspx?group=' + ParseKey(location.search, 'group=') + '\';void(0);"><img src="images/BtnMembers.png" border="0" title="Member Management" onmouseover="this.src=this.src.replace(\'.png\', \'2.png\');" onmouseout="this.src=this.src.replace(\'2.png\', \'.png\');" /></a> ';
    }

    if (g_nUserType > 0)
    {
        if ((location.href.toLowerCase().indexOf('schedule') > -1) && (location.href.toLowerCase().indexOf('schedule.aspx') == -1)) // second part after && should be removed
        {
            sTemp += '<a href="javascript:location.href=\'ScheduleUser.aspx?group=' + ParseKey(location.search, 'group=') + '&startdate=' + sStartDate + '&enddate=' + sEndDate + '\';void(0);"><img src="images/BtnSchedUser.png" border="0" title="Schedule Report" onmouseover="this.src=this.src.replace(\'.png\', \'2.png\');" onmouseout="this.src=this.src.replace(\'2.png\', \'.png\');" /></a> ';
        }
        else
        {
            sTemp += '<a href="javascript:location.href=\'ScheduleUser.aspx?group=' + ParseKey(location.search, 'group=') + '\';void(0);"><img src="images/BtnSchedUser.png" border="0" title="Schedule Report" onmouseover="this.src=this.src.replace(\'.png\', \'2.png\');" onmouseout="this.src=this.src.replace(\'2.png\', \'.png\');" /></a> ';
        }
    }
       
    sTemp += '<a href="javascript:location.href=\'EditProfile.aspx?group=' + ParseKey(location.search, 'group=') + '\';void(0);"><img src="images/BtnUser.png" border="0" title="Edit Your Profile" onmouseover="this.src=this.src.replace(\'.png\', \'2.png\');" onmouseout="this.src=this.src.replace(\'2.png\', \'.png\');" /></a>';
    
    if (g_sHelpFileName  != "")
    {
        sTemp += '<a href="javascript:window.open(\'help/' + g_sHelpFileName + '\', \'help\', \'width=620,height=500,top=0,left=0,resizable=1,scrollbars=yes,menubar=0,toolbar=0,status=0,location=0\');void(0);"><img src="images/BtnHelp.png" border="0" title="Help" onmouseover="this.src=this.src.replace(\'.png\', \'2.png\');" onmouseout="this.src=this.src.replace(\'2.png\', \'.png\');" /></a>';
    }

    if (g_sUserFirstName == "")
    {
        sTemp += '</span>';
    }
    
    if ($('OperationalButtonsSpan') == null)
    {
        sTemp += "</span>";
        document.write(sTemp);
    }
    else
    {
        $('OperationalButtonsSpan').innerHTML = sTemp;
    }

    try
    {
        if (!g_bVirtualGroup)
        {
            $('MainTab1').style.display = ((g_nUserType > 0) ? '' : 'none');
        }
    }
    catch (e) { }
}

function FormatPhoneNumber(Phone)
{
    var sTemp = '';
    
    for (var i = 0; i < Phone.length; i++)
    {
        if ((Phone.charAt(i) >= '0') && (Phone.charAt(i) <= '9'))
        {
            sTemp += Phone.charAt(i);
        }
    }
    
    if (sTemp.length < 7)
    {
        return(Phone);
    }
    
    var sResult = '(';
    sResult += sTemp.substring(0, 3) + ') ';
    sResult += sTemp.substring(3, 6) + '-';
    sResult += sTemp.substring(6);
    
    return(sResult);
}

function GetUserType(UserTypeId)
{
    switch (UserTypeId)
    {
        case '1': return('Volunteer'); break;
        case '2': return('Board Member'); break;
        case '3': return('Supervisor'); break;
        case '4': return('Head Coordinator'); break;
        case '5': return('Team Leader'); break;
        default: return('Supporter'); break;
    }
}

function WriteSchedule(bShowPhoto, nGroupId, sData, nUserId, bByRole, nRoleCodeIndex, bShowPartners)
{
    var asDays = new Array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
    var asMonths = new Array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
    var asData = sData.split('^');
    var sTemp = "";
    
    sTemp += '<table cellpadding="0" cellspacing="0" style="border:1px solid #999999;width:100%;font-size:11px;font-family:Tahoma;font-weight:bold;">';
    sTemp += '<tr>';
    sTemp += '<td style="font-family:Tahoma;font-weight:bold;background-image:url(images/HeaderX3.png);color:#4c4c4c;padding:3px;width:120px;border-right:1px solid #999999;">Date</td>';
    sTemp += '<td style="font-family:Tahoma;font-weight:bold;background-image:url(images/HeaderX3.png);color:#4c4c4c;padding:3px;width:100px;border-right:1px solid #999999;">Hours</td>';

    if (g_sDefaultWorkingMode != "C")
    {
        sTemp += '<td style="font-family:Tahoma;font-weight:bold;background-image:url(images/HeaderX3.png);color:#4c4c4c;padding:3px;width:100px;border-right:1px solid #999999;">Team</td>';
    }

    if (g_sDefaultWorkingMode == "A")
    {
        if ((!bByRole) && (nUserId > -1))
        {
            sTemp += '<td style="font-family:Tahoma;font-weight:bold;background-image:url(images/HeaderX3.png);color:#4c4c4c;padding:3px;width:100px;' + ((bShowPartners) ? 'border-right:1px solid #999999;' : '') + '">Position</td>';
            
            if (bShowPartners)
            {
                sTemp += '<td style="font-family:Tahoma;font-weight:bold;background-image:url(images/HeaderX3.png);color:#4c4c4c;padding:3px;"' + ((bShowPhoto) ? ' colspan="2"' : '') + '>Partner(s)</td>';
            }
        }
        else
        {
            sTemp += '<td style="font-family:Tahoma;font-weight:bold;background-image:url(images/HeaderX3.png);color:#4c4c4c;padding:3px;"' + ((bShowPhoto) ? ' colspan="2"' : '') + '>Member(s)</td>';
        }    
    }
    else
    {
        sTemp += '<td style="font-family:Tahoma;font-weight:bold;background-image:url(images/HeaderX3.png);color:#4c4c4c;padding:3px;">Team Member(s)</td>';
    }
    
    var nCounter = 0;
    var sLastDateString = "";
    
    var sTempSummary = "";
    
    for (var i = 0; i < asData.length; i++)
    {
        if (asData[i].split('|')[0] == "Summary")
        {
            sTempSummary += '<input type="hidden" id="ReportMembersIds" value="' + asData[i].split('|')[1] + '" /><input type="hidden" id="ReportMembersNames" value="' + asData[i].split('|')[2] + '" />';
            continue;
        }
    
        if (asData[i] != "")
        {
            var sDateString = asData[i].split('|')[1].split('_')[1];
            var nDay = sDateString.substring(6);
            var nMonth = sDateString.substring(4, 6);
            var nYear = sDateString.substring(0, 4);
            var dtDate = new Date();
            dtDate.setFullYear(nYear);
            dtDate.setMonth(nMonth - 1, nDay);
            dtDate.setDate(nDay);
            
            var dtDate2 = dtDate;

            nCounter++;
            
            var sHourStart = asData[i].split('|')[4] + ":0";
            var sHourEnd = asData[i].split('|')[5] + ":0";
            var sCSSClass = "";
            
            if ((!bByRole) && (nUserId > -1))
            {
                var sCSSClass = ((nCounter % 2 == 0) ? '' : ' class="GridEvenRow"');
            }
            else
            {
                if ((bByRole) && (nRoleCodeIndex < 2))
                {
                    var sCSSClass = ((sLastDateString == sDateString) ? '' : ' class="GridEvenRow"');
                }
                else
                {
                    var sCSSClass = ((nCounter % 2 == 0) ? '' : ' class="GridEvenRow"');
                }
            }                    
            
            var sTemp2 = '<tr>';
            sTemp2 += '<td style="border-top:1px solid #999999;border-right:1px solid #999999;color:#0f0078;padding:3px;"' + sCSSClass + ' valign="top">';
            
            if (sLastDateString != sDateString)
            {
                sTemp2 += '<a href="ScheduleDay.aspx?group=' + nGroupId + '&year=' + nYear + '&month=' + nMonth + '&day=' + nDay + '">'; // style="color:#0f0078;text-decoration:none;" onmouseover="this.style.textDecoration=\'underline\';" onmouseout="this.style.textDecoration=\'none\';"
                sTemp2 += asDays[dtDate.getDay()].substring(0,3) + ', ' + asMonths[nMonth - 1] + ' ' + nDay + ', ' + nYear;
                sTemp2 += '</a></td>';
            }
            else
            {
                sTemp2 += '&nbsp;'
            }
            
            sLastDateString = sDateString;
            
            sTemp2 += '<td style="border-top:1px solid #999999;border-right:1px solid #999999;color:#0f0078;padding:3px;"' + sCSSClass + ' valign="top"><nobr>' + AddZero(sHourStart.split(':')[0]) + ":" + AddZero(sHourStart.split(':')[1]) + ' - ' + AddZero(sHourEnd.split(':')[0]) + ":" + AddZero(sHourEnd.split(':')[1]) + '</nobr></td>';

            if (g_sDefaultWorkingMode == "A")
            {
                sTemp2 += '<td style="border-top:1px solid #999999;border-right:1px solid #999999;color:#0f0078;padding:3px;"' + sCSSClass + '>' + ((asData[i].split('|')[6] != "Car 0") ? asData[i].split('|')[6] : "Base") + '&nbsp;</td>';
                if ((!bByRole) && (nUserId > -1))
                {
                    sTemp2 += '<td style="border-top:1px solid #999999;' + ((bShowPartners) ? 'border-right:1px solid #999999;' : '') + 'color:#0f0078;padding:3px;"' + sCSSClass + '>' + asData[i].split('|')[9] + '&nbsp;</td>';
                }

                if (bShowPhoto)
                {            
                    if (asData[i].split('|')[7] > 0)
                    {
                        sTemp2 += '<td style="border-top:1px solid #999999;color:#0f0078;width:48px;" align="center"><img src="UserPhoto.aspx?id=' + asData[i].split('|')[7] + '&width=48&height=48" align="absmiddle"></td>';
                    }
                    else
                    {
                        sTemp2 += '<td style="border-top:1px solid #999999;color:#0f0078;width:48px;" align="center">&nbsp;</td>';
                    }
                }
                
                if (bShowPartners)
                {
                    sTemp2 += '<td style="border-top:1px solid #999999;color:#0f0078;padding:3px;"' + sCSSClass + '>' + asData[i].split('|')[8] + '&nbsp;</td>';
                }
            }
            else
            {
                if (g_sDefaultWorkingMode != "C")
                {
                    sTemp2 += '<td style="border-top:1px solid #999999;border-right:1px solid #999999;color:#0f0078;padding:3px;"' + sCSSClass + ' valign="top">' + asData[i].split('|')[2] + '&nbsp;</td>';
                }
                
                sTemp2 += '<td style="border-top:1px solid #999999;border-right:1px solid #999999;color:#0f0078;padding:3px;"' + sCSSClass + ' valign="top">' + asData[i].split('|')[3] + '&nbsp;</td>';
            }
            
            
            sTemp2 += '</tr>';
            
            var sCurrentDateString = new Date().getFullYear() + AddZero(new Date().getMonth() + 1) + AddZero(new Date().getDate());
            
            if (sDateString < sCurrentDateString)
            {
                sTemp2 = ReplaceAll(sTemp2, "#0f0078", "#999999");
                sTemp2 = ReplaceAll(sTemp2, "#cc0000", "#999999");
                sTemp2 = ReplaceAll(sTemp2, "#990000", "#999999");
            }
            
            sTemp += sTemp2;
        }
    }
    
    sTemp += '</tr>';
    sTemp += '</table>';
    sTemp += sTempSummary;

    return(sTemp);
}

function GetUserAdjustmentsTable(nGroupId, nUserId, sData, sStartDate, sEndDate)
{
    if (sStartDate.split('/').length >= 3)
    {
        sStartDate = sStartDate.split('/')[2] + sStartDate.split('/')[0] + sStartDate.split('/')[1];
    }
    else
    {
        sStartDate = "";
    }

    if (sEndDate.split('/').length >= 3)
    {
        sEndDate = sEndDate.split('/')[2] + sEndDate.split('/')[0] + sEndDate.split('/')[1];
    }
    else
    {
        sEndDate = "";
    }
    
    var asDays = new Array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
    var asMonths = new Array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
    var asData = sData.split('\n');
    var sTemp = "";
    
    sTemp += '<table cellpadding="0" cellspacing="0" style="width:100%;font-size:11px;font-family:Tahoma;font-weight:bold;">';
    sTemp += '<tr>';
    sTemp += '<td style="font-family:Tahoma;font-weight:bold;background-image:url(images/HeaderX3.png);color:#4c4c4c;padding:3px;width:120px;border-right:1px solid #999999;">Date</td>';
    sTemp += '<td style="font-family:Tahoma;font-weight:bold;background-image:url(images/HeaderX3.png);color:#4c4c4c;padding:3px;width:60px;border-right:1px solid #999999;">Credit</td>';
    sTemp += '<td style="font-family:Tahoma;font-weight:bold;background-image:url(images/HeaderX3.png);color:#4c4c4c;padding:3px;width:120px;border-right:1px solid #999999;">By</td>';
    sTemp += '<td style="font-family:Tahoma;font-weight:bold;background-image:url(images/HeaderX3.png);color:#4c4c4c;padding:3px;">Comment</td>';
    sTemp += '</tr>';

    var nCounter = 0;
    var sLastDateString = "";
    var nAdjustments = 0;
    var sTempSummary = "";
    
    for (var i = 0; i < asData.length; i++)
    {
        if (asData[i] != "")
        {
            var sDateString = asData[i].split('|')[0].split('~')[0];

            if ((sStartDate != "") && (sStartDate > sDateString))
            {
                continue;
            }

            if ((sEndDate != "") && (sEndDate < sDateString))
            {
                continue;
            }
            
            var nDay = sDateString.substring(6);
            var nMonth = sDateString.substring(4, 6);
            var nYear = sDateString.substring(0, 4);
            var dtDate = new Date();
            dtDate.setFullYear(nYear);
            dtDate.setMonth(nMonth - 1, nDay);
            dtDate.setDate(nDay);
            
            nCounter++;

            var sCSSClass = ((nCounter % 2 == 0) ? ' class="GridOddRow"' : ' class="GridEvenRow"');
            
            var sCredit = asData[i].split('|')[1].split('~')[0];
            var sBy = asData[i].split('|')[1].split('~')[1];
            var sComment = asData[i].split('|')[1].split('~')[2];
            
            var sTemp2 = '<tr>';
            sTemp2 += '<td style="border-top:1px solid #999999;border-right:1px solid #999999;color:#0f0078;padding:3px;"' + sCSSClass + ' valign="top">';
            sTemp2 += asDays[dtDate.getDay()].substring(0,3) + ', ' + asMonths[nMonth - 1] + ' ' + nDay + ', ' + nYear;
            sTemp2 += '</td>';

            sTemp2 += '<td style="border-top:1px solid #999999;border-right:1px solid #999999;color:#0f0078;padding:3px;"' + sCSSClass + ' valign="top"><nobr>' + sCredit + '</nobr></td>';
            sTemp2 += '<td style="border-top:1px solid #999999;border-right:1px solid #999999;color:#0f0078;padding:3px;"' + sCSSClass + ' valign="top"><nobr>' + sBy + '</nobr></td>';
            sTemp2 += '<td style="border-top:1px solid #999999;color:#0f0078;padding:3px;"' + sCSSClass + ' valign="top">' + sComment + '&nbsp;</td>';
            sTemp2 += '</tr>';
            
            sTemp += sTemp2;
            
            if (!isNaN(sCredit))
            {
                nAdjustments += (sCredit * 1);
            }
        }
    }
    
    var dtDateNow = new Date();
    
    sTemp += '<tr id="AddNewRow"><td align="middle" colspan="4" style="border-top:1px solid #999999;padding:3px;"><a href="javascript:$(\'NewRow\').style.display=\'\';javascript:$(\'AddNewRow\').style.display=\'none\';void(0);"></a>&nbsp;</td></tr>';
    sTemp += '<tr id="NewRow" style="display:none;"><td style="border-top:1px solid #999999;border-right:1px solid #999999;border-bottom:1px solid #999999;color:#0f0078;padding:3px;"' + sCSSClass + ' valign="top">';
    sTemp += asDays[dtDateNow.getDay()].substring(0,3) + ', ' + asMonths[dtDateNow.getMonth()] + ' ' + dtDateNow.getDate() + ', ' + dtDateNow.getFullYear();
    sTemp += '</td>';
    sTemp += '<td style="border-top:1px solid #999999;border-right:1px solid #999999;border-bottom:1px solid #999999;color:#0f0078;"' + sCSSClass + ' valign="top"><nobr><input id="AddNewAdjustment_Credit" type="text" style="width:60px;height:18px;font-family:Tahoma;font-weight:bold;color:#0f0078;padding:2px;padding-left:3px;font-size:11px;"></nobr></td>';
    sTemp += '<td style="border-top:1px solid #999999;border-right:1px solid #999999;border-bottom:1px solid #999999;color:#0f0078;"' + sCSSClass + '><nobr>' + parent.g_sUserFirstName + '</nobr></td>';
    sTemp += '<td style="border-top:1px solid #999999;border-bottom:1px solid #999999;color:#0f0078;"' + sCSSClass + ' valign="top"><nobr><input type="text" id="AddNewAdjustment_Comment" maxlength="100" style="width:100%;height:18px;font-family:Tahoma;font-weight:bold;color:#0f0078;padding:2px;padding-left:3px;font-size:11px;"></nobr></td>';
    sTemp += '</tr>';
    
    sTemp += '</table>';

    $('Adjustment').value = nAdjustments;
    
    if (nAdjustments != 0)
    {
        $('Fulfilled').value = $('Fulfilled').value.split(' ')[0] + ' (' + ((nAdjustments > 0) ? '+' + nAdjustments : nAdjustments) + ')';
    }
    
    UpdateBalance();

    return(sTemp);
}

function GetLineFromArrayById(StrArray, Key)
{
    for (var i = 0; i < StrArray.split('^').length; i++)
    {
        if (StrArray.split('^')[i].split('|')[0] == Key)
        {
            return(StrArray.split('^')[i]);
        }
    }
    
    return("");
}

function GetLineIndexFromArrayById(StrArray, Key)
{
    for (var i = 0; i < StrArray.split('^').length; i++)
    {
        if (StrArray.split('^')[i].split('|')[0] == Key)
        {
            return(i);
        }
    }
    
    return(-1);
}

function trim(str)
{
    var sTemp = str;
    
    while (sTemp.indexOf(' ') == 0)
    {
        sTemp = sTemp.substring(1);
    }

    while (sTemp.charAt(sTemp.length - 1) == ' ')
    {
        sTemp = sTemp.substring(0, sTemp.length - 1);
    }
    
    return(sTemp);
}

function TrimChar(str, ch)
{
    while ((str.length > 0) && (str.charAt(0) == ch))
    {
        str = str.substring(1);
    }
    
    while ((str.length > 0) && (str.charAt(str.length - 1) == ch))
    {
        str = str.substring(0, str.length - 1);
    }
    
    return(str);
}

