/*
 * Browser-safe get object. Tries all three approaches to get an object
 * by its element Id.
 *
 * Param:               objId - String - id of element to retrieve.
 * Param:               formId - String - id of form (optional).
 * Returns:             element or null if not found.
 */
function getObj(objId) {
    
    var elem = null;
    
    if (document.getElementById) {
        elem = document.getElementById(objId);
    } else if (document.all) {
        elem = document.all[objId];
    } else if (document.layers) {
        elem = document.layers[objId];
    }
    
    return elem;
}

function getFirstIndex(arrayId, index) {
    var firstIndex = index;
    index = index - 1;
    id = arrayId + '[' + (index) + ']';
    while(true) {
        if (getObj(id) != null) {
            firstIndex = index;
            index = index - 1;
            id = arrayId + '[' + (index) + ']';
        } else {
            break;
        }
    }
    
    return firstIndex;
}

function setElemChecked(chkAllElmId, state) {
    elem = getObj(chkAllElmId);
    if (elem != null) {
        elem.checked = state;
    }
}

function onChangeSelect(objsIdsArr, checkbox, checkBoxArrayId, chkAllElmId, index) {
    
    var currentSelectedIndexElm = getObj("currentSelectedIndex");
    var currentSelectedIndex = currentSelectedIndexElm.value;
    
    var state = (currentSelectedIndex.indexOf(index + ",") > -1);
    
    if (state) { // check box was selected before, remove it from array
        currentSelectedIndex = currentSelectedIndex.replace(index + ",", '');
        getObj("currentSelectedIndex").value = currentSelectedIndex;
    } else { // check box un selected before, add it to array
        currentSelectedIndex += index + ",";
        getObj("currentSelectedIndex").value = currentSelectedIndex;
    }
    
    var firstIndex = getFirstIndex(checkBoxArrayId, index);
    
    if (checkBoxArrayAllChecked(firstIndex, checkBoxArrayId)) {
        setElemChecked(chkAllElmId, true);
    } else {
        setElemChecked(chkAllElmId, false);
    }
    
    enableOrDisableObject(objsIdsArr, checkBoxArrayHasChecked(firstIndex, checkBoxArrayId));
}

/* 
 * Browser-safe. Checks to see if an array of check boxes has any which are 
 * checked. Boxes have ids like check[0], check[1], ... , check[n] where 'check' 
 * is the base Id that has been assigned to the group.
 *
 * Param:               arrayId - String - id of element group to change
 * Returns:             boolean (true one or more checked) false (else)
 */
function checkBoxArrayHasChecked(firstIndex, arrayId) {
    for (i = firstIndex; ; i++) {
        id = arrayId + '[' + i + ']';
        elem = getObj(id);
        if (elem == null) {
            break;
        } else if (elem.checked) {
           return true;
        }
    }
    return false;
}

function checkBoxArrayAllChecked(firstIndex, arrayId) {
    
    var state = true;
    for (i = firstIndex; ; i++) {
        id = arrayId + '[' + i + ']';
        elem = getObj(id);
        if (elem == null) {
            break;
        } else {
            state = state && elem.checked;
        }
    }
    
    return state;
}

function checkBoxArrayPageChecked(arrayId, chkAllElmId, pageSize) {
    alert(pageSize);
    var firstIndex = 0;
    for (i = 0; ; i++) {
        id = arrayId + '[' + i + ']';
        elem = getObj(id);
        if (elem != null) {
            firstIndex = i;
            break;
        }
    }
    firstIndex += pageSize;
    alert(firstIndex);
    var allPageChecked = checkBoxArrayAllChecked(firstIndex, arrayId);
    alert(allPageChecked);
    chkAllElem = getObj(chkAllElmId);
    if (chkAllElem != null) {
        chkAllElem.checked = allPageChecked;
    }
    
}

/*
 * Browser-safe. Sets the visibility of an element using CSS visibility
 *
 * Param:               objId - String - id of element to change
 * Param:               state - boolean - true (show element) false (hide element)
 * Returns:             nothing
 */                                                             
function enableOrDisableObject(objsIdsArr, state) {
    
    for (var i = 0; i < objsIdsArr.length; ++i) {
        
        var elem = getObj(objsIdsArr[i]);
        
        if (elem != null) {
            elem.disabled = !state;
        }
        
    }
}

/*
 * Browser-safe. Check or uncheck an array of checkboxes. Boxes have ids
 * like check[0], check[1], ... , check[n] where 'check' is the base Id that
 * has been assigned to the group.
 *
 * Param:               arrayId - String - id of element group to change
 * Param:               state - boolean - true (check all elements) false (uncheck all elements)
 * Returns:             nothing
 */
function checkBoxArraySet(arrayId, state) {
    for (i = 0; ; i++) {
        id = arrayId + '[' + i + ']';
        elem = getObj(id);
        if (elem == null) {
            break;
        } else {
            elem.checked = state;
        }
    }   
}

function setAll(checkBoxArrayId, chkAllElmId) {
    
    var state;
    elem = getObj(chkAllElmId);
    if (elem != null) {
        state = elem.checked;
        elem.checked = state;
    }
    
    // Set the checkBox array on or off
    checkBoxArraySet(checkBoxArrayId, state);
    // Render the transfer button if one or more checkboxes are selected
    //enableOrDisableObject(objsIdsArr, checkBoxArrayHasChecked(getFirstIndex(checkBoxArrayId, index), checkBoxArrayId));
}

// Set the cmd button off until a checkbox has been changed
/*function setInitialState(objsIdsArr) {
    enableOrDisableObject(objsIdsArr, false);
}*/

/*function setCurrentIndex(index) {
    var selectedIdElm = getObj("currentSelectedIndex");
    selectedIdElm.value = index;
}*/