﻿// JScript File
function rollon(source)
{
    var element = document.getElementById(source);
    var old = element.src;
    var newsrc = old.replace("_off", "_on");
    element.src = newsrc;    
}

function rolloff(source)
{
    var element = document.getElementById(source);
    var old = element.src;
    var newsrc = old.replace("_on", "_off");
    element.src = newsrc; 
}

function roll(source, type)
{
    var element = document.getElementById(source);
    var old = element.src;
    
    var newsrc = ""; 
    if (type == "on")
        newsrc = old.replace("_off", "_on");
    else 
        newsrc = old.replace("_on", "_off");
    
    element.src = newsrc;    
}

function OpenFlash(type, file, height, width)
{
    var win;
    switch(type)
    {
        case "FLASH":
            win = window.open("/flashholder.html?f=" + file + "&h=" + height + "&w=" + width, "flash_video", "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,left=150,top=150,alwaysRaised=1,height=" + Math.floor(height * 1.1) + ",width=" + Math.floor(width * 1.1));
            break;
    }
    win.focus();
}

function OpenWindow(type, arg)
{
    var win;
    switch(type)
    {
        case "DIRECTIONS":
            win = window.open(arg, type, "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0,left=150,top=150,alwaysRaised=1,height=500,width=300");
            break;
        case "ACCELERATE":
            win = window.open(arg, type, "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0,left=150,top=150,alwaysRaised=1,width=" + screen.width + ", height=" + screen.height);
            break;
    }
    win.focus();
}

function GetQs()
{
    var objURL = new Object();

    // Use the String::replace method to iterate over each
    // name-value pair in the query string. Location.search
    // gives us the query string (if it exists).
    window.location.search.replace(
        new RegExp( "([^?=&]+)(=([^&]*))?", "g" ),

        // For each matched query string pair, add that
        // pair to the URL struct using the pre-equals
        // value as the key.
        function( $0, $1, $2, $3 )
        {
            objURL[ $1 ] = $3;
        }
    );
    return objURL;
}

function pop_up(HiddenAddId, HiddenPkId)
{
    window.open("DivisionSearch.aspx?HiddenAddId=" + HiddenAddId + "&HiddenPkId=" + HiddenPkId, "DivisionPopup", "width=375, height=450, location=no, status=no, toolbar=0, menubar=0");
}

function select_organization(HiddenAddId, HiddenPkId, PkOrganization)
{
    dom = window.opener.document.getElementById(HiddenAddId);
    domPk = window.opener.document.getElementById(HiddenPkId);
    domPk.value = PkOrganization;
    dom.click();
    window.close();
}

/***********************
*   UNIVERSAL    *******
***********************/
function GetElement(id) {
    if (document.getElementById) {
        return document.getElementById(id);
    }
    else if (document.all) {
        return document.all[id];
    }
    else {
        return document.layers[id];
    }
}

function CheckAll(form)
{
    chks = document.form.chkOrgs;
    alert("test");
}

/* this roll function is for the directory */
function ImgRoll(img, state)
{
    if (state == 'on')
        img.src = img.src.replace('-off', '-on');        
    else if (state == 'off')
        img.src = img.src.replace('-on', '-off');
}

function ClientNodeClicked(sender, eventArgs) // used to hide/show the blue text on the user side category tree
{
    var node = eventArgs.get_node();
    node.toggle();

    HideShowCount(node);

    var tree = node.get_treeView();

    if (node.get_parent().get_parent() != null) {
        // current node is child. show loader
        ShowOverlay("browseable", 0.25, 90, "circle");
    }

    for (var i = 0; i < tree.get_nodes().get_count(); i++) {
        var node2 = tree.get_nodes().getNode(i);

        // search through the entire treeview for any expanded categories, collapse all
        // of them except for the category that was just clicked
        if (node2 != node) {
            if (node.get_parent().set_checked == null) {
                node2.collapse();
                HideShowCount(node2, 1);
            }
        }
    }
}

function onNodeExpanded(sender, args) {

    var node = args.get_node();
    node.expand();

    HideShowCount(node);
    
    var tree = node.get_treeView();

    for (var i = 0; i < tree.get_nodes().get_count(); i++) {
        var node2 = tree.get_nodes().getNode(i);

        // search through the entire treeview for any expanded categories, collapse all
        // of them except for the category that was just clicked
        if (node2 != node) {
            if (node.get_parent().set_checked == null) {
                node2.collapse();
                HideShowCount(node2, 1);
            }
        }
    }
}

function onNodeCollapsed(sender, args) {

    var node = args.get_node();
    node.collapse();
    

    HideShowCount(node);

    var tree = node.get_treeView();

    for (var i = 0; i < tree.get_nodes().get_count(); i++) {
        var node2 = tree.get_nodes().getNode(i);

        // search through the entire treeview for any expanded categories, collapse all
        // of them except for the category that was just clicked
        if (node2 != node) {
            if (node.get_parent().set_checked == null) {
                node2.collapse();
                HideShowCount(node2, 1);
            }
        }
    }
}

function HideShowCount(node, collapse) // used to hide/show the blue text on the user side category tree
{
   var span_tag = GetElement(node.get_value());
   
   if (span_tag != null)
   {    
       if (collapse == null) // it is null when the node that was clicked is passed
       {
           if (span_tag.style.display == "none")
           {
               span_tag.style.display = "inline";
           }
           else
           {
               span_tag.style.display = "none";
           }
       }
       else // this is when we are collapsing all nodes that were not clicked
       {
           span_tag.style.display = "none";
       }
   }
}

function UpdateAllChildren(nodes, checked) // used for the admin side category tree
{
   var i;
   for (i=0; i<nodes.get_count(); i++)
   {
       if (checked)
       {
           nodes.getNode(i).check();
       }
       else
       {
           nodes.getNode(i).set_checked(false);
       }
       
       if (nodes.getNode(i).get_nodes().get_count()> 0)
       {
           UpdateAllChildren(nodes.getNode(i).get_nodes(), checked);
       }
   }
}

function ClientNodeChecked(sender, eventArgs) // used for the admin side category tree
{
    // this function runs AFTER a node is checked / unchecked

    var node = eventArgs.get_node(); // node that was clicked
    var childNodes = node.get_nodes(); // children nodes
    var isChecked = node.get_checked();

    UpdateAllChildren(childNodes, isChecked); // if parent node, check / uncheck all children
    node.expand();

    if (!node.get_checked()) // if a node is not checked
    {
        var parent = node.get_parent();
        var children = parent.get_nodes();
        var test = false;

        for (i = 0; i < children.get_count(); i++) // loop through every "sibling" node
        {
            if (children.getNode(i).get_checked()) {
                test = true; // capture if any of the siblings are checked
            }
        }

        if (!test) // no sibling nodes are checked
        {
            while (node.get_parent().set_checked != null) // loop through every parent node
            {
                node.get_parent().set_checked(false); // uncheck the parent
                node = node.get_parent();
            }
        }
    }
    else // if a node IS checked
    {
        while (node.get_parent().set_checked != null) // loop through every parent node
        {
            node.get_parent().set_checked(true); // check the parent
            node = node.get_parent();
        }
    }
}

function ShowHideCities(display)
{
    var div = GetElement("divCities");
    
    if (display == "hide")
    {
        div.style.display = "none";
    }
    else
    {
        div.style.display = "block";
    }
}

/***********************
*   SEARCH QUERY  ******
***********************/
// sets the search box to default text when unfocused
function SetUsernameBoxTextFocus(tb) {
    if (tb.value == "username")
        tb.value = "";
}

function SetPasswordBoxTextFocus(tb) {
    if (tb.value == "password")
    {
        tb.value = "";
        tb.type = "password";
    }
}

function SetUsernameBoxTextUnFocus(tb) {
    if (tb.value == "")
        tb.value = "username";
}

function SetPasswordBoxTextUnFocus(tb) {
    if (tb.value == "")
    {
        tb.value = "password";
        tb.type = "text";
    }
}

function ShowOverlay(strElementID, dOpacity, iSize, strType) {

    //  strElementID: element id that needs overlay
    //  dOpacity: 0.00 to 1.00.
    //  iSize: 16 - 512
    //  strType: circle|oval|polygon|rectangle|tube.
    
    getBusyOverlay(GetElement(strElementID), { opacity: dOpacity }, { size: iSize, type: strType });
}
