// JScript File


function ACDoesSelectionSpanUnavailableDay(rowIndex, colIndex) {
    var firstCheckedIndex = -1;
    var lastCheckedIndex  = -1;
    
    for (d=0;d<dayCount;d++) {
        if (chkRooms[rowIndex][d] != undefined) {
            //this is a checkbox
            if (document.getElementById(chkRooms[rowIndex][d]).checked) {
                if (firstCheckedIndex < 0) 
                    firstCheckedIndex = d;
                
                lastCheckedIndex = d;
            }            
        }
    }
    
    if (firstCheckedIndex > -1 && ACIsUnavailDayBetween(rowIndex, firstCheckedIndex, colIndex))
        return true;
    
    if (lastCheckedIndex > -1 && ACIsUnavailDayBetween(rowIndex, lastCheckedIndex, colIndex))
        return true;
    
    return false;
}

function ACIsUnavailDayBetween(rowIndex, colIndex1, colIndex2) {

    if (colIndex1 > colIndex2) {
        var temp = colIndex1;
        colIndex1 = colIndex2;
        colIndex2 = temp;
    }

    for (c=colIndex1;c<colIndex2;c++) 
        if (chkRooms[rowIndex][c] == undefined) 
            return true;
            
    return false;
}

//This is the standard entry point
function ACSelRowByCell(chkTrigger, rowIndex, colIndex) {
    
    var newSelectionWouldSpanUnavailableDays = ACDoesSelectionSpanUnavailableDay(rowIndex, colIndex);
    var shouldWeContinue = false;
    
    if (newSelectionWouldSpanUnavailableDays) {
        //we need to figure out which side of the selection to keep (orig or new)
        var affirm = confirm("Selecting this night will undo your previous selections. Do you want to do this?");
        if(affirm) {
            shouldWeContinue = true;
            //we need to unselect all of the current selections
            for (c=0;c<dayCount;c++) {
                UnhighlightCell(rowIndex, c);
            }
        } else {
            //user doesn't want to undo their selections
        }
    } else {
        shouldWeContinue = true;
    }
    
    if (shouldWeContinue) {
        chkTrigger.checked = !chkTrigger.checked;
        var selected = ACSelRow(chkTrigger, rowIndex);
        if (chkTrigger.checked)
            HighlightCell(rowIndex, colIndex);
        else
            UnhighlightCell(rowIndex, colIndex);
    }

    ACAutoSelectFillerDays(rowIndex, colIndex); 

}

function ACAutoSelectFillerDays(rowIndex, colIndex) {
    //we should make sure there aren't any gaps in our selection
    
    var firstCheckedIndex = -1;
    var lastCheckedIndex  = -1;
    
    for (var d=0;d<dayCount;d++) {
        if (chkRooms[rowIndex][d] != undefined) {
            //this is a checkbox
            if (document.getElementById(chkRooms[rowIndex][d]).checked) {
                if (firstCheckedIndex < 0) 
                    firstCheckedIndex = d;
                
                lastCheckedIndex = d;
            }            
        }
    }

    if (colIndex <= firstCheckedIndex) {
        colIndex = lastCheckedIndex;
    }

    for (var c = firstCheckedIndex; c <= lastCheckedIndex; c++) {
        if (chkRooms[rowIndex][c] != undefined) {
            if (c <= colIndex) {
                HighlightCell(rowIndex, c, true);
                document.getElementById(chkRooms[rowIndex][c]).checked = true;
            } else {
                UnhighlightCell(rowIndex, c);
            }
        }
    }
    
    if (lastCheckedIndex > firstCheckedIndex)
        ACAddBookRow(rowIndex);
}   


function ACSelRow(chkTrigger, rowIndex) {

    if (selectedRow != rowIndex) {
        //different row, so let's acutally do something
        if (selectedRow == undefined || selectedRow == -1) {
            //debug("previously selectedRow: " + selectedRow);
            selectedRow == -1;
        } else {
            //we've already selected a row...need to display a warning
            //debug("previously selectedRow: " + selectedRow);
            
            var affirm = confirm("Selecting this room will undo your previous selections. Do you want to do this?");
            if(!affirm) {
                chkTrigger.checked = false;
                return false; //oops, don't change anything
            } else {
                //we want to unselect our previous selections
            }
        }
    } else {
        //same row
        //debug("same row");
    }
    return true;
}

function HighlightCell(rowIndex, colIndex, pRowIsAlreadyEnabled) {
    document.getElementById("trRow" + rowIndex + "Col" + colIndex).className = "selected";
    if (!(pRowIsAlreadyEnabled == true)) {
        ACEnableRow(rowIndex);
        ACAddBookRow(rowIndex);
    }
        
    selectedRow = rowIndex; //we want to select a new row
    
    if (document.getElementById("img" + rowIndex + "." + colIndex) != null)
        document.getElementById("img" + rowIndex + "." + colIndex).src = "img/btnCBSelectNormal.gif";
}

function UnhighlightCell(rowIndex, colIndex) {
    ACAddBookRow(rowIndex);
    if (document.getElementById("trRow" + rowIndex + "Col" + colIndex).className == "selected")
        document.getElementById("trRow" + rowIndex + "Col" + colIndex).className = "";

    if (document.getElementById("img" + rowIndex + "." + colIndex) != null)
        document.getElementById("img" + rowIndex + "." + colIndex).src = "img/btnCBSelectDown.gif";
        
    if (chkRooms[rowIndex][colIndex] != undefined ) 
        if (document.getElementById(chkRooms[rowIndex][colIndex]) != null) 
            document.getElementById(chkRooms[rowIndex][colIndex]).checked = false;
}

function ACEnableRow(rowIndex) {
    //UNgray out the row
    document.getElementById("trRow" + rowIndex).className = "";

    for (var r=0; r<roomCount; r++)
        if (r != rowIndex) //disable the other rows
            ACDisableRow(r);
}

function ACDisableRow(rowIndex) {
    //gray out the row, deselect the checkboxes
    document.getElementById("trRow" + rowIndex).className = "disabled";
    for (c=0;c<dayCount;c++) {
        UnhighlightCell(rowIndex, c);
    }
}

function ACAddBookRow(rowIndex) {
    var table = document.getElementById("tblCalender");
    var tableBody = table.tBodies[0];

    if (bookRow >= 0) {
        ACRemoveBookRow();
    }

    if (rowIndex < 0)
        return;

    var firstCheckedIndex = -1;
    var lastCheckedIndex = -1;

    for (var d = 0; d < dayCount; d++) {
        if (chkRooms[rowIndex][d] != undefined) {
            //this is a checkbox
            if (document.getElementById(chkRooms[rowIndex][d]).checked) {
                if (firstCheckedIndex < 0)
                    firstCheckedIndex = d;

                lastCheckedIndex = d;
            }
        }
    }

    rowIndex += 2;
    if (firstCheckedIndex >= 0 && lastCheckedIndex >= 0) {
        var newRow = tableBody.insertRow(rowIndex)
        var newCell;

        newCell = newRow.insertCell(0);
        newCell.colSpan = firstCheckedIndex + 1;

        newCell = newRow.insertCell(1);
        newCell.colSpan = lastCheckedIndex - firstCheckedIndex + 1;
        newCell.innerHTML = "<a href='#' onclick=\"document.getElementById('ctl00_Main_btnSubmit').click(); return false;\">" +
"<table border='0' cellpadding='0' cellspacing='0' width='100%' id='tblAvailCalButtonHolder'>" +
"<tr>" +
"<td width='4'><img border='0' src='img/btnBookSpanL.png'></td>" +
"<td width='100%' id='tblAvailCalButtonRepeatBg'>Book</td>" +
"<td width='4'><img border='0' src='img/btnBookSpanR.png'></td>" +
"</tr>" +
"</table>" +
"</a>";

        if (lastCheckedIndex < dayCount - 1) {
            newCell = newRow.insertCell(2);
            newCell.colSpan = dayCount - lastCheckedIndex;
        }

        bookRow = rowIndex;
    }
}

function ACRemoveBookRow() {
    if (bookRow < 0)
        return;

    var table = document.getElementById("tblCalender");
    var tableBody = table.tBodies[0];
    tableBody.deleteRow(bookRow);
    bookRow = -1;
}

function debug(text) {
    window.status = text + " | " + window.status.substring(0, 200);
}


//not the most efficient way, but reliable and easy to understand. 
function GetFirstSelectedDate() {
    for (var r=0; r<roomCount; r++) //each row
        for (var d=0; d<dayCount; d++) //each day
            if (chkRooms[r][d] != undefined) //found a checkbox
                if (document.getElementById(chkRooms[r][d]).checked) //it is checked
                    return chkRooms[r][d].substring(chkRooms[r][d].indexOf(".", 0)+1);  //get the part after the period. done searching

    return "NODATESELECTED";                    
}

function GetLastSelectedDate() {
    var lastSeletedDate = "NODATESELECTED";
    
    for (var r=0; r<roomCount; r++) //each row
        for (var d=0; d<dayCount; d++)  //each day
            if (chkRooms[r][d] != undefined)  //found a checkbox
                if (document.getElementById(chkRooms[r][d]).checked)  //it is checked
                    lastSeletedDate = chkRooms[r][d].substring(chkRooms[r][d].indexOf(".", 0)+1);   //get the part after the period. save for later
                    
    return lastSeletedDate;                    
}


function GetSelectedRoomId() {
    for (var r=0; r<roomCount; r++) //each row
        for (var d=0; d<dayCount; d++) //each day
            if (chkRooms[r][d] != undefined) //found a checkbox
                if (document.getElementById(chkRooms[r][d]).checked) //it is checked
                    return roomIds[r]; 

    return -1;                    
}


function IsNightStayLimitationsHonored() {
    //we need to select more rooms if the minnight property is > 1. 
    //This might be tricky because we may have already selected enough rooms
    //we also may need to select a room ahead of this one and behind it as well

    var inarow = 0;
    var minNightReq;
    var largestMinNightReq = 0;
    for (var r=0; r<roomCount; r++) { //each row
        for (var d=0; d<dayCount; d++) { //each day
            if (chkRooms[r][d] != undefined) { //found a checkbox
                if (document.getElementById(chkRooms[r][d]).checked) { //it is checked
                    inarow++;
                    minNightReq = document.getElementById(chkRooms[r][d]).attributes.getNamedItem("minnight").value;
                    if (minNightReq > largestMinNightReq) {
                        largestMinNightReq = minNightReq;
                    }
                }
            }
        }
    }       
    
    if (largestMinNightReq > inarow) {
        //then we need to auto-select some days to match the largest min night stay
        alert("Your room selection requires a minimum stay of " + largestMinNightReq + " consecutive nights.");
        return false;
    } else if (maxNight < inarow) {
        alert("Your room selection requires a maximum stay of " + maxNight + " consecutive nights.");
        return false;
    }
    return true;
}

