function getCookieVal(offset){
    var endstr = document.cookie.indexOf(";", offset);
    if ( endstr == -1)    
        endstr = document.cookie.length;
    return unescape(document.cookie.substring(offset, endstr));       
}

function getCookie(name){
        
    var namelen = name.length;        
    var cookielen = document.cookie.length;    
    
    var i = 0;  
    while (i < cookielen) {    
        var j = i + namelen;    
        if (document.cookie.substring(i, j) == name)      
            return getCookieVal(j);    
        i = document.cookie.indexOf(" ", i) + 1;    
        if (i == 0) break;   
    }  
    return null;
}

function checkfund(){
    
    if( getCookie('funds=') != null ){
                
        var idstr = getCookie('funds=');
    
        var len = idstr.length;
        var size = 4;    
        var no = 0;

        if (idstr != null) {
        //get size for array
            var i = 0;
            while ( i < len ){            
                no ++ ;                      
                i  = idstr.indexOf(",", i) + 1;
                if (i == 0) break;
            }
            //save cookie ids in array
            var idlist = new Array(no);
            var j = 0;
            var k = 0;
        
            while (j < len) {
                idlist[k++] = idstr.substring(i, i+size);
                i  = idstr.indexOf(",", i)+1;
                if (i == 0) break;
            }
            //compare cookie id with form id
            for (var h=0; h<document.form1.elements.length; h++){            
                var id = document.form1.elements[h].name;

                for (var g=0; g<no; g++) {
                    if (id == idlist[g]) {
                        document.form1.elements[h].checked = true;
                    }
                }
            }
        }        
    }    
}

function countfund(){
    var count = 0;
    
    for (var h=0; h<document.form1.elements.length; h++){ 
        if( document.form1.elements[h].checked ) {
            count ++;            
        }
    }
    if( count > 15 ){
        alert("Please limit your selections to 15 items or less.");
        document.form1.elements[0].focus();
        return false;
    }
    return true;
}

function compute(){    
    if (countfund()) {
        document.form1.submit();
    }
}


