Array.prototype.find = function(searchStr) {
  var returnArray = false;
  for (i=0; i<this.length; i++) {
    if (typeof(searchStr) == 'function') {
      if (searchStr.test(this[i])) {
        if (!returnArray) { returnArray = [] }
        returnArray.push(i);
      }
    } else {
      if (this[i]===searchStr) {
        if (!returnArray) { returnArray = [] }
        returnArray.push(i);
      }
    }
  }
  return returnArray;
}


function getObj(name)
{
  if (document.getElementById)
  {
    return document.getElementById(name);
  }
  else if (document.all)
  {
    return document.all[name];
  }
  else if (document.layers)
  {
    return getObjNN4(document,name);
  }
}

function getObjNN4(obj,name)
{
    var x = obj.layers;
    var foundLayer;
    for (var i=0;i<x.length;i++)
    {
        if (x[i].id == name)
            foundLayer = x[i];
        else if (x[i].layers.length)
            var tmp = getObjNN4(x[i],name);
        if (tmp) foundLayer = tmp;
    }
    return foundLayer;
}


function fixMenuLI(navRoot)
{
    for(i=0; i<navRoot.childNodes.length; i++)
    {
        node = navRoot.childNodes[i];
        if(node.nodeName=="LI")
        {
            node.onmouseover=function()
            {
                this.className+=" over";
            }
            node.onmouseout=function()
            {
                this.className=this.className.replace(" over", "");
            }
        }
        else if(node.nodeName=="UL")
        {
            fixMenuLI(node);
        }
    }
}

startList = function() {
    if(document.all && document.getElementById)
    {
        navRoot = document.getElementById("menu");

        if(navRoot && navRoot.childNodes)
        {
            for(i=0; i<navRoot.childNodes.length; i++)
            {

                node = navRoot.childNodes[i];
                fixMenuLI(node);
            }
        }

        navRoot = document.getElementById("usermenu");

        if(navRoot && navRoot.childNodes)
        {
            for(i=0; i < navRoot.childNodes.length; i++)
            {

                node = navRoot.childNodes[i];
                fixMenuLI(node);
            }
        }
        navRoot = document.getElementById("autotabs");

        if(navRoot && navRoot.childNodes)
        {
            for(i=0; i<navRoot.childNodes.length; i++)
            {

                node = navRoot.childNodes[i];
                fixMenuLI(node);
            }
        }
    }
}
window.onload=startList;

function popUp(URL) {
day = new Date();
id = day.getTime();
eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=1,menubar=0,resizable=0,width=730,height=400,left = 492,top = 325');");
}

function getRadioVal(rb)
{
    var L=rb.length;
    var ret="";
    if(!rb.length)
    {
        ret = rb.value;
        L = 0;
    }
    for(var i = 0 ; i< L ; i++)
    {
        if(rb[i].checked)
        {
            ret=rb[i].value;
            break;
        }
    }
    return(ret);
}


function showHiddenQuestion()
{
    if(showHiddenQuestion.arguments && showHiddenQuestion.arguments && showHiddenQuestion.arguments.length == 1)
    {
        if(showHiddenQuestion.arguments[0].split)
        {
            var questionIds = showHiddenQuestion.arguments[0].split(',');

            for(idx = 0; idx < questionIds.length; idx++)
            {
                obj = getObj(questionIds[idx]);
                if(obj)
                {
                    obj.style["display"] = '';
                }
            }
            if(obj)
                return true;
        }
    }
}

function hideQuestion()
{
    if(hideQuestion.arguments && hideQuestion.arguments && hideQuestion.arguments.length == 1)
    {
        if(hideQuestion.arguments[0].split)
        {
            var questionIds = hideQuestion.arguments[0].split(',');

            for(idx = 0; idx < questionIds.length; idx++)
            {
                obj = getObj(questionIds[idx]);
                if(obj)
                {
                    obj.style["display"] = 'none';
                }
            }
            if(obj)
                return true;
        }
    }
}

function checkQuestion(question)
{
    if(checkQuestion.arguments.length <= 1)
    {
        return false;
    }
    else
    {
        var qids = Array();
        for(pidx = 1; pidx < checkQuestion.arguments.length; pidx++)
        {
            qids[(pidx-1)] = checkQuestion.arguments[pidx];
            qidString = qids.join(',');
        }
        hideQuestion(qidString);
        switch(question.name)
        {
            case 'maritalStatus':
                if(question.value == "Married")
                {
                    return showHiddenQuestion(qidString);
                }
                break;
            case 'autoCount':
                hideQuestion(qidString+',211,212,213,214,215');
                if(question.value > 0)
                {
                    showHiddenQuestion(qidString);
                    for(qidx = 1; qidx <= question.value; qidx++)
                    {
                        qidNum = qidString+ '' +qidx;
                        showHiddenQuestion(qidNum);
                    }
                    return true;
                }
                break;
            default:
                if(question.type && question.type == "radio")
                {
                    var value = getRadioVal(question);
                    hideQuestion(qidString);
                    if(value == 1)
                    {
                        return showHiddenQuestion(qidString);
                    }
                }
                break;
        }
    }
    return false;
}


function questionValue(question)
{
    if(document.forms["calculate"] && document.forms["calculate"][question.name])
    {
        var option = document.forms["calculate"][question.name];
        if(option)
        {
            switch(question.nodeName)
            {
                case "SELECT":
                    option.selectedName = option.options[option.selectedIndex].text;
                    break;
                case "INPUT":
                    break;
                default:
                    return FALSE;
            }
            return option;
        }
    }


}

function resizeObject(name, width, height, delta)
{
    var obj = getObj(name);
    var increase = 0;
    var objWidth = parseInt(obj.offsetWidth);
    var newWidth = (obj.offsetWidth + delta);
    var increment = ( objWidth < width ) ? 1 : -1;
    var percentage = (increment < 0) ? 0.66 : 0.33;
    if(stretchObjectByWidth.arguments && stretchObjectByWidth.arguments.length == 4)
    {
        increase = parseInt(stretchObjectByWidth.arguments[3]);
    }
    if(obj && (typeof(obj) == "object"))
    {
        obj.style["width"] = newWidth;
        var threshold = ((width * increment) - (newWidth * increment));
        var newdelta = delta-increment;
        var newincrease = increase-increment;
        if(threshold < (width*percentage) && ( newdelta != 0 ))
        {
            delta = newdelta;
            if(newincrease != 0)
            {
                increase = newincrease;
            }
        }
        if((newWidth*increment) < (width*increment))
        {
            setTimeout("stretchObjectByWidth(\"" + name + "\", " + width + ", " + (delta+increase) + " , " + increase + " )", 10);
        }
        else
        {
            obj.style["width"] = width;
        }
    }
}

function stretchObjectByWidth(name, width, delta)
{
    var obj = getObj(name);
    var increase = 0;
    var objWidth = parseInt( obj.offsetWidth );
    var newWidth = ( obj.offsetWidth + delta );
    var increment = ( objWidth < width ) ? 1 : -1;
    var percentage = ( increment < 0 ) ? 0.66 : 0.33;

    if ( stretchObjectByWidth.arguments && stretchObjectByWidth.arguments.length == 4 )
    {
        increase = parseInt( stretchObjectByWidth.arguments[3] );
    }

    if ( obj && ( typeof( obj ) == "object" ) )
    {
        obj.style["width"] = newWidth;
        var threshold = ( ( width * increment ) - ( newWidth * increment ) );
        var newdelta = delta-increment;
        var newincrease = increase-increment;

        if ( threshold < ( width * percentage ) && ( newdelta != 0 ) )
        {
            delta = newdelta;
            if ( newincrease != 0 )
            {
                increase = newincrease;
            }
        }
        if ( ( newWidth * increment ) < ( width * increment ) )
        {
            setTimeout( "stretchObjectByWidth(\"" + name + "\", " + width + ", " + (delta+increase) + " , " + increase + " )", 10);
        }
        else
        {
            obj.style["width"] = width;
        }
    }
}


function hideLoadMask()
{
    if(!document.transparencyInterval)
    {
        document.transparencyInterval = setInterval("hideLoadMask()", 150);
    }


    var x = getObj("loading-mask");

    //var y = getObj("loading");

    var i = Math.round(10*Math.random())+20
    if(x && x.style)
    {
        var tmpOpacity = (x.style["opacity"] * 100) - i*i;


        x.style.opacity = tmpOpacity/100;
        x.style.filter = 'alpha(opacity=' + (tmpOpacity) + ')';

        if(tmpOpacity <= 0)
        {
            clearInterval(document.transparencyInterval);
            //x.style["display"] = "none";
            removeElementById(x.id);
            return;
        }
    }
}

function removeElementById(id)
{
    var el = getObj(id);
    if(el && el.parentNode && el.parentNode.removeChild)
    {
        el.parentNode.removeChild(el);
    }
}

function toggleShowHide(element_id)
{
    var element = getObj(element_id);
    
    if(element.style)
    {
        if(element.style["display"] && element.style["display"] == "none")
        {
            element.style["display"] = "block";
            return;
        }
        element.style["display"] = "none";
    }
    
}

function toggleClass(element, class1, class2)
{
   if(element)
   {
        if(element.className == class1)
        {
            element.className = class2;
            return;
        }        
        element.className = class1;      
       
    }
}
