/*
 *
 * DOM HELPER FUNCTIONS
 *
 */

/////////// TO  SHOW/HIDE OPTIONS WE NEED TO MAKE AN ARRAY OF THEIR NODES
var aOptionsNodes = new Array(); //we will be building this array to hold the NODES
function buildOptionsNodes(sSelector,sArrayName) {
    $(sSelector+' option').each(function() {//for each of the option's NODES
        var sValue = $(this).attr('value'); //get the value of the current option
        aOptionsNodes[sArrayName+sValue] = $(this); //save each node in the array with the correct index
    });//we can now use this array to remove all nodes and replace with the visible ones
}


//used to show the correct options based on other selection and array
// sFiltereeSelect is the selector to get the select being filtered
// sFiltereeName is the prefix for your options nodes keys for the select being filtered
// aControllerArray is the array thats being used to filter
// and aControllerValue is the value that we are filtering by
function showHideOptions(sFiltereeSelect,sFiltereeName,aControllerArray,aControllerValue) {
    var iSelectedOption = $(sFiltereeSelect).val();
    $(sFiltereeSelect+' option').remove(); //remove all options nodes
    var oNonSelectedNode = '';
    if (aOptionsNodes[sFiltereeName]) oNonSelectedNode = aOptionsNodes[sFiltereeName];
    else if (aOptionsNodes[sFiltereeName+'0']) oNonSelectedNode = aOptionsNodes[sFiltereeName+'0'];
    if (oNonSelectedNode) {
        $(sFiltereeSelect).append(oNonSelectedNode);//put the PLEASE SELECT option back
        oNonSelectedNode.attr('selected','selected');
    }
    var bSelectedVisible = false;
    for (var i in aControllerArray[aControllerValue]) {  //loop through invoices for selected supplier
        var sValue = aControllerArray[aControllerValue][i]; //this is the current available invoice value
        if (iSelectedOption == sValue) bSelectedVisible = true;
        $(sFiltereeSelect).append(aOptionsNodes[sFiltereeName+sValue]);//put the node back into the select
    }
    if (bSelectedVisible) {
        $(sFiltereeSelect+' option[value='+iSelectedOption+']').attr('selected','selected');
    } else if (oNonSelectedNode) {
        oNonSelectedNode.attr('selected','selected');
    }
}


//function fixes the on/off styling on lines
//useful if rows are shown or hidden
//@tableSelector - the jquery selector for the row
//@startRow - the first row to start the styling on
function reassignOnLine(tableSelector,startRow) {
    $(tableSelector + ' tr').removeClass('onLine');
    $(tableSelector + ' tr:visible:even').addClass('onLine');
    $(tableSelector + ' tr.invalidRow').removeClass('onLine');
}


/* modifyChildren - used to affect children of selected element
 * sAction = 'hide' to set childnren to display: none
 * sAction = 'deactivate' to set children to neutral styling */
function modifyChildren(sElementId,sAction) {
    var oParent = document.getElementById(sElementId);
    var aChildren = oParent.childNodes;
    for (var iThisChild in aChildren) {
        if (sAction == 'hide' && aChildren[iThisChild] && aChildren[iThisChild].style) aChildren[iThisChild].style.display = 'none';
        if (sAction == 'deactivate' && aChildren[iThisChild]) {
            if (aChildren[iThisChild].nodeName && aChildren[iThisChild].nodeName.toLowerCase()=='div') modifyChildren(aChildren[iThisChild].getAttribute('id'),'deactivate');
            else if (aChildren[iThisChild].className) {
                if (aChildren[iThisChild].className==' activeLink') aChildren[iThisChild].className = '';
                else if (aChildren[iThisChild].className=='activePage activeLink') aChildren[iThisChild].className = 'activePage';
            }
        }
    }
}


/* showHide - used to show or hide selected element to display: sType */
function showHide(sElementId,sType) {
    var oElement = document.getElementById(sElementId);
    if (oElement && oElement.style) oElement.style.display = sType;
}


function showHideBlockSwitch(sElementId,blockType,bInstant) {
    if (!blockType) blockType = 'block';
    var sNewState = '';
    var oElement = document.getElementById(sElementId);
    if (oElement && oElement.style) {
        sNewState = oElement.style.display==blockType?'none':blockType;
        if (bInstant) $(oElement).toggle();
        else $(oElement).slideToggle('slow');
    }
    return sNewState;
}

function setBlockHeight(sElementId) {
    /*
    var oElement = document.getElementById(sElementId);
    var sState = oElement.style.display;
    if (sState=='none') oElement.style.display='block';

    var iNewHeight = $(oElement).height();
        //-$(oElement).css("padding-bottom").replace("px", "")
        //-$(oElement).css("margin-bottom").replace("px", "");
        //-$(oElement).css("border-bottom-width").replace("px", "");
    $(oElement).css("height", iNewHeight);
    if (sState=='none') oElement.style.display='none';*/
}


/* activateLink - used to activate the selected link in the styles */
function activateLink(sElementId) {
    var oElement = document.getElementById(sElementId);
    if (oElement && oElement) oElement.className = oElement.className + ' activeLink';
}




/* getClassNameElements - custom get class name function to deal with ie */
if (!document.getElementsByClassName) {//first, include the js file if necessary
    var head = document.getElementsByTagName("head")[0];
    var newScript = document.createElement('script');
    newScript.type = 'text/javascript';
    newScript.src = '/js/getElementsByClassName.js';
    head.appendChild(newScript);
}
function getClassNameElements(className) {
    var aElements;
    if (document.getElementsByClassName) {
        aElements = document.getElementsByClassName(className);
    } else {
        aElements = getElementsByClassName(className);
    }
    return aElements;
}



/* fixDropdown - used to fix dropdown width in case of ie */
function fixDropdown(sElement, bOn) {
    if(navigator.appName=='Microsoft Internet Explorer'){
        var oElement = document.getElementById(sElement);
        if (bOn) {
            oElement.style.position='absolute';
            oElement.style.width='auto';
        } else {
            oElement.style.position='';
            oElement.style.width='';
        }
    }
}

/* used to select all checkboxes of a given class */
function selectAllCheckboxes(sClassName,bSelect) {
    var aCheckboxes = getClassNameElements(sClassName);
    for (var i in aCheckboxes) {
        aCheckboxes[i].checked = bSelect;
    }
}

function showWaiting() {
    //document.body.style.cursor = 'wait';
    $('#waitLoading').show();
    $('#waitLoadingMessage').show();
}
function showNormal() {
    //document.body.style.cursor = 'auto';
    $('#waitLoading').hide();
    $('#waitLoadingMessage').hide();
}



/*
 *
 * ACTION SPECIFIC FUNCTIONS
 *
 */


/* Function used to show and hide navigation levels. */
function subNav(iTopLink,iSubLink) {
    //first hide all levels
    modifyChildren('level1Links','deactivate');
    modifyChildren('level2Links','hide');
    modifyChildren('level2Links','deactivate');
    modifyChildren('level3Links','hide');
    if (iTopLink) {
        showHide("subRow_2_"+iTopLink,'block');
        activateLink("topNav_"+iTopLink);
    }
    if (iSubLink) {
        showHide("subRow_3_"+iTopLink+"_"+iSubLink,'block');
        activateLink("subNav_"+iTopLink+"_"+iSubLink);
    }
}



function calendarClear(id) {
    $('#'+id).val('');
}



function closeWindow() {
	window.close();
	window.opener.focus();
}
