// add functionality to the window's onload event
// in script so the body tag in the header still
// belongs to the county.
function setFocus(controlName) {

    if(focuscontrol==null) {
        alert("setFocus() cannot be called until after Utils.js has been included.");
        return;
    }
    focuscontrol=controlName;
}
var focuscontrol="";
var el = window;
el.oldOnLoad = el.onload;
el.onload = function(evt) {
    try {
        // if it exists preserve the existing onload event
        if (el.oldOnLoad) {
            el.oldOnLoad(evt);
        }
        // set the focus where we want it
        document.getElementById(focuscontrol).focus();
        document.getElementById(focuscontrol).select();
    } catch(err){
        // this is just to make the page flow better
        // if there was a problem we do not want the
        // error to cause further problems so it is
        // best to throw the error away
    }
};

function performAction(action) {
    document.getElementById("action").value = action;
    document.getElementById("mainform").submit();
}

function isElementChecked(obj)
{
 /////////////////////////////////////////////////////////////
 // Check to see if a class has been selected from the list //
 /////////////////////////////////////////////////////////////
 
 var foundchecked = false;
 var rExp = "/" + obj + "/gi";
 rExp = eval(rExp);
 var arySelectors = new Array();
 
 var objElements = document.getElementsByTagName("input");
 
 var j = 0;
 for(i=0; i<objElements.length; i++)
 {
  if(objElements[i].name.search(rExp) >= 0)
  {
   arySelectors[j] = objElements[i];
   j++;
  }
 }
 
 for(i=0; i<arySelectors.length; i++)
 {
  if(arySelectors[i].checked)
   foundchecked = true;
 }
 
 return foundchecked;
}

function testSelect() {
    if ( isElementChecked("select") )
        return true;
    alert("Please select an item before performing this action.");
    return false;
}

function searchSearch() {
    performAction("search");
}

function searchAdd() {
    performAction("add");
}

function searchEdit() {
    if( testSelect() )
        performAction("edit");
}

function searchDelete(message) {
    if( testSelect() && confirm(message) ) {
        performAction("delete");
    }
}

function listAdd() {
    performAction("listadd");
}

function listEdit() {
    if( testSelect() )
        performAction("listedit");
}

function listDelete() {
    if( testSelect() )
        performAction("listdelete");
}



var lastRow = null;
var lastRow2 = null;
var lastClass = "grid";
function hilite(obj, obj2)
{
    ///////////////////////////////////////////////////////////
    // Hilight the selected row, browser dependent branching //
    ///////////////////////////////////////////////////////////

    if (navigator.appName == "Netscape") {
        /*
        for(j=1; j<obj.parentNode.childNodes.length; j++) {
            for(x=1; x<obj.parentNode.childNodes[j].childNodes.length; x=x+2)
                obj.parentNode.childNodes[j].childNodes[x].className="grid";
        }
        */

        if(lastRow) {
            for(i=1; i<lastRow.childNodes.length; i=i+2)
                lastRow.childNodes[i].className=lastClass;
        }
        if(lastRow2) {
            for(i=1; i<lastRow2.childNodes.length; i=i+2)
                lastRow2.childNodes[i].className=lastClass;
        }


        lastRow = obj;
        if(obj2)
            lastRow2 = obj2;
        if(obj.childNodes[0])
            lastClass = obj.childNodes[0].className;

        newClass = "grid-selected";

        for(i=1; i<obj.childNodes.length; i=i+2)
            obj.childNodes[i].className=newClass;

        if(obj2) {
            for(i=1; i<obj2.childNodes.length; i=i+2)
                obj2.childNodes[i].className=newClass;
        }

    } else {

        /*
        for(j=1; j<obj.parentNode.childNodes.length; j++){
            for(x=0; x<obj.parentNode.childNodes[j].childNodes.length; x++)
                obj.parentNode.childNodes[j].childNodes[x].className="grid";
        }
        */
        if(lastRow) {
            for(i=0; i<lastRow.childNodes.length; i++)
                lastRow.childNodes[i].className=lastClass;
        }
        if(lastRow2) {
            for(i=0; i<lastRow2.childNodes.length; i++)
                lastRow2.childNodes[i].className=lastClass;
        }


        lastRow = obj;
        if(obj2)
            lastRow2 = obj2;
        if(obj.childNodes[0])
            lastClass = obj.childNodes[0].className;

        newClass = "grid-selected";

        for(i=0; i<obj.childNodes.length; i++)
            obj.childNodes[i].className=newClass;

        if(obj2) {
            for(i=0; i<obj2.childNodes.length; i++)
                obj2.childNodes[i].className=newClass;
        }

    }
}

function select(rowId) {
    document.getElementById("selected").value = rowId;
}

function showHelp(helpId) {
    RH_ShowHelp(0, './help/Service_Request_County_Office.htm', HH_HELP_CONTEXT, helpId);
}

function showHelpContents() {
    RH_ShowHelp(0, './help/Service_Request_County_Office.htm' ,HH_DISPLAY_TOC, 0);
}

var confirmed = true;
function canContinue() {
    temp = confirmed;
    confirmed = true;
    return temp;
}
function canDelete(itemDescription) {
    confirmed = confirm('Are you sure you want to delete the selected '+itemDescription+'?');
}
function canView() {
    confirmed = testSelect();
}
