///Ajax Operations

var CHECKBOX_CONTROL_ID;
var ALL_CHECKBOX_CONTROL_ID;

var SELECT_AJAX_OPERATION; 
var SELECT_ALL_AJAX_OPERATION;

var RESPONSE_XSL;
var RESPONSE_TARGET;
var SHORT_AJAX_CALLBACK;

///Callback function when the checkbox is clicked/checked.
///
///     The select-all checkbox is automatically checked if all the checkboxes are checked, and 
///     unchecked otherwise.
///
///p_Node : html-input element that was checked.
///p_ControlID : id of the html element.
///
function OnItemClicked( p_Node, p_ControlID, p_SelectAllControlID ) {
  
    //If the node invoking this is a checkbox 
    if( p_Node.type != null && p_Node.type == 'checkbox' ) {

        //Build the ajax url to record the checkbox's checkstate
        var ajaxURL = "?ACID=" + p_ControlID + 
                      "&AOP=" + SELECT_AJAX_OPERATION + 
                      "&APARAMS=" + p_Node.id + "," + p_Node.checked + 
                      "&ResponseXsl=" + RESPONSE_XSL;
        //run a short ajax , with no callback
        RunShortAjax( ajaxURL,  SHORT_AJAX_CALLBACK );

    }

    //Get the select-all checkbox associated with the group of checkboxes.
    var chkAllElement = document.getElementById(p_SelectAllControlID);

    //If all the checkboxes are checked then check the "select-all" element.
    chkAllElement.checked = isAllChecked(".*" + p_ControlID + ".*");
}

function OnSideItemClicked(p_ControlID, p_ID, p_AOP, p_ResponseXsl, p_ResponseTarget, p_BaseURL) {

    var shortAjaxCB =  "WriteTextToID('" + p_ResponseTarget + "', objHttpRequest.responseText);";
    var ajaxURL = p_BaseURL +
                  "?ACID=" + p_ControlID + 
                  "&AOP=" + p_AOP + 
                  "&APARAMS=" + p_ID + ",false" +
                  "&ResponseXsl=" + p_ResponseXsl;
    var clickedElement = document.getElementById( p_ID );

    //If the clicked selection is on the same page as it's corresponding control then ...    
    if( clickedElement != null ) {
        if( clickedElement.name == p_ControlID + ':' + p_ID ) {
            //Uncheck the checbox too.
            clickedElement.checked = false;
        }
    }
    
    RunShortAjax( ajaxURL , shortAjaxCB );                       
}

function RemoveItem( p_ID ) {

    RunShortAjax('?ACID=' + CHECKBOX_CONTROL_ID + 
                 '&AOP=' + SELECT_AJAX_OPERATION + 
                 '&APARAMS=' + p_ID + ',false' +
                 '&ResponseXsl=' + RESPONSE_XSL ,
                  SHORT_AJAX_CALLBACK
                 )
}


///Callback function when the "select all" checkbox is clicked/checked.
///
///     The subnodes are automatically checked or unchecked to reflect the state of the select-all checkbox.
///
///p_Node : html-input element that is the select-all checkbox.
///p_ControlID : id of the select-all checkbox
function OnSelectAll( p_Node , p_ControlID ) {

    //Make sure the checkbox clicked is a checkbox.
    if( p_Node.type != null && p_Node.type == 'checkbox' ) {

        //Build the ajax url to select-all or unselect-all the selections
        var ajaxURL = "?ACID=" + p_ControlID + 
                      "&AOP=" + SELECT_ALL_AJAX_OPERATION + 
                      "&APARAMS=" + p_Node.checked + 
                      "&ResponseXsl=" + RESPONSE_XSL;

        //run a short ajax, with no callback
        RunShortAjax( ajaxURL, SHORT_AJAX_CALLBACK );

    }

    //check all the boxes based on the state of the select-all checkbox.
    checkAllCheckBoxes(".*" + p_ControlID + ".*", p_Node );
}

///Function that will set the state of all the checkboxes that have a similar name.  I.E. they are a group of
///checkboxes related together by their name.
///
///p_RegExSearchID : regex that will locate the checkboxes
///p_SelectAllElement : select-all checkbox element
///
function checkAllCheckBoxes(p_RegExSearchID, p_SelectAllElement) {
    //Build the regex    
    var re = new RegExp(p_RegExSearchID); 

    //Loop through all the elements in the document
    for(var i = 0; i < document.forms[0].elements.length; i++) {
        var elm = document.forms[0].elements[i];

        //If it is a checkbox AND the passes the regex test
        if( elm.type == "checkbox" && re.test( elm.name ) ) {
            //then set the state of the checkbox based on the "select-all" box.        
            elm.checked = p_SelectAllElement.checked;
        }
    }

    return true;
}

///Function that checks if a group of checkboxes are all selected.
///
///p_RegExSearchID : regex that will locate the checkboxes.
///
function isAllChecked(p_RegExSearchID) {
    var re = new RegExp(p_RegExSearchID); 
    var allChecked = true;

    for(var i = 0; i < document.forms[0].elements.length && allChecked; i++) {
        var elm = document.forms[0].elements[i];

        if( elm.type == "checkbox" && re.test( elm.name ) ) {
                allChecked = elm.checked & allChecked;
        }

    }

    return allChecked;

}

/// Our callback function
function WriteTextToID(p_TargetId, p_responseText) 
{ 
    var target = document.getElementById(p_TargetId);
    target.innerHTML = p_responseText;
    return true;
}
