    var activityList;
    var programActivity;
    var programId;
    var programIndex;
    var activityId;
    var activityIndex;
    var programSelect;
    var activitySelect;
    var resourceSelect;
    var durationSelect;
    singleSubmitCheck = true;
    function Program(programId, programName, resourceName, isGender, groupRule, schedType, maxAttd) {
            this.programId=programId;
            this.programName=programName;
            this.resourceName=resourceName;
            this.isGender=isGender;
            this.groupRule=groupRule;
            this.schedType=schedType;
            this.maxAttd=maxAttd;
    }
    function initProgramList(){
            var ii;
//
            programSelect=window.document.forms[0].program;
            var select=programSelect;
            var selected;
//
            if (programSelect.type=='select-one') {
               programIndex=programSelect.selectedIndex;
               programId=programSelect.options[programIndex].value;
               programSelect.options.length=0;
	            if(programSelectOptionFirst==true) {
	               ii=programSelect.options.length;
	               programSelect.options.length=ii+1;
	               programSelect.options[ii].value =selectOptionId;
	               programSelect.options[ii].text  =selectOption;
	               if(programId==selectOptionId) programSelect.selectedIndex=ii;
	            }
	            for (var i=0; i<programList.length; i++)  {
	               ii=programSelect.options.length;
	               programSelect.options.length=ii+1;
	               programSelect.options[ii].value =programList[i].programId;
	               programSelect.options[ii].text  =programList[i].programName;
	               if(programId==programList[i].programId) programSelect.selectedIndex=ii;
	            }
	            if(programSelectOptionFirst==false) {
	               ii=programSelect.options.length;
	               programSelect.options.length=ii+1;
	               programSelect.options[ii].value =selectOptionId;
	               programSelect.options[ii].text  =selectOption;
	               if(programId==selectOptionId) programSelect.selectedIndex=ii;
	            }
               if(programSelect.selectedIndex<0) programSelect.selectedIndex=0;
            } else {
               programIndex=0;
               programId=programSelect.value;
           		for (var i=0; i<programList.length; i++)  {
	               if(programId==programList[i].programId) {
	               	programIndex=i;
	               	break;
	               }
	            }
	            programSelect.value=programList[programIndex].programId;
               window.document.forms[0].selectedProgramName.value=programList[programIndex].programName;
            }
         onProgramChange();
    }
   function onProgramChange() {
      if (programSelect.type=='select-one') {
         programIndex=programSelect.selectedIndex;
         programId=programSelect.options[programIndex].value;
         if(programId!=selectOptionId) window.document.forms[0].selectedProgramName.value=programSelect.options[programIndex].text;
         else window.document.forms[0].selectedProgramName.value='';
         if(programIndex>0&&programSelect.options[0].value==selectOptionId)programIndex--;
      } else {
         programIndex=0;
         programId=programSelect.value;
      }
      activitySelect=window.document.forms[0].activity;
      if(activitySelect.type=='select-one'){
         if (programId==selectOptionId||programActivity==null) {
            activitySelect.options.length=0;
            activitySelect.options[0]=new Option(selectOption, selectOptionId, false, true);
         } else {
            activityId=activitySelect.options[activitySelect.selectedIndex].value;
            if(activityId==selectOptionId) activityId='';
            activitySelect.options.length=0;
            if(activitySelectOptionFirst==true) {
               activitySelect.options.length=1;
               activitySelect.options[0].value =selectOptionId;
               activitySelect.options[0].text  =selectOption;
            }
            if (programList[programIndex].schedType==2) {
               var length=activitySelect.options.length;
               activitySelect.options.length=length+1;
               activitySelect.options[length].value =-1;
               activitySelect.options[length].text  =allActivities;
               if(activityId==-1) activitySelect.selectedIndex=length;
            }
               filterOptions( programSelect, activitySelect, programActivity, activityList, activityId);
            if(activitySelectOptionFirst==false) {
               var ii=activitySelect.options.length;
               activitySelect.options.length=ii+1;
               activitySelect.options[ii].value =selectOptionId;
               activitySelect.options[ii].text  =selectOption;
            }
         }
      } else {
         if(programId>0) {
            if (programList[programIndex].schedType==2) {
               window.document.forms[0].activity.value=-1;
               window.document.forms[0].selectedActivityName.value=allActivities;
            } else {
               var index;
               var firstIndex=-1;
               activityId=window.document.forms[0].activity.value;
               for (index=0; index<programActivity[0].length; index++) {
                  if(programActivity[0][index]==programId) {
                  	if(firstIndex<0) firstIndex=index;
                  	if(activityId==programActivity[1][index]) {
                  		firstIndex=index;
                    		break;
                    	}
               	}
               }
         		if(firstIndex<0) firstIndex=0;
               activityId=programActivity[1][firstIndex];
               for (index=0; index<activityList[0].length; index++) {
                    if(activityList[0][index]==activityId) break;
               }
               window.document.forms[0].activity.value=activityId;
               window.document.forms[0].selectedActivityName.value=activityList[1][index];
            }
         } else {
            window.document.forms[0].activity.value=selectOptionId;
            window.document.forms[0].selectedActivityName.value='';
         }
      }
      onActivityChange();
   }
   function onActivityChange() {
      var maxAttendees=1;
      var select;
      var index=0;
		var apptGroupType=1;
      if(activitySelect.type=='select-one') {
         index=activitySelect.selectedIndex;
         if (index<0) index=0;
         activityId=activitySelect.options[index].value;
         if(activityId!=selectOptionId) window.document.forms[0].selectedActivityName.value=activitySelect.options[index].text;
         else window.document.forms[0].selectedActivityName.value='';
      } else activityId=window.document.forms[0].activity.value;

      if(activityId!=selectOptionId) {
         for (index=0; index<activityList[0].length; index++) {
              if(activityList[0][index]==activityId) break;
         }
         if(index>=activityList[0].length) {
            maxAttendees=programList[programIndex].maxAttd;
         } else {
				maxAttendees=activityList[2][index];
				apptGroupType=activityList[3][index];
         }
      }
      if(maxAttendees==null||maxAttendees<=0) maxAttendees=1;
      select=window.document.forms[0].attendees;
      if(select.type=='select-one') {
         var attendees=select.options[select.selectedIndex].value;
         select.options.length=maxAttendees;
         for (var i=1; i<=maxAttendees; i++) {
           select.options[i-1].text=i;
           select.options[i-1].value=i;
           if(attendees==i) select.selectedIndex=i-1;
         }
         if(select.selectedIndex<0) select.selectedIndex=0;
      }
      try {
			window.document.forms[0].apptGroupType.value=apptGroupType;
	      displaySetup();
			if(apptGroupType==1) {
	         displayDurations();
	         displayResource();
			} else {
				setSelectDisabled(window.document.forms[0].duration);
				setSelectDisabled(window.document.forms[0].resource);
	         window.document.forms[0].selectedResourceName.value=any+' '+programList[programIndex].resourceName;
			}
		} catch(everithing){}
    }
	function displaySetup(){
		var setupSelect=document.forms[0].setupTime;
		if(setupSelect!=null&&setupTimesList!=null){
			 if(setupSelect.type=='select-one') {
				 var options=setupSelect.options;
				 var value=options[setupSelect.selectedIndex].value;
				 options.length=0;
				 if(activityId<=0) {
					 options.length=1;
					 options[0].value=0;
					 options[0].text=none;
					 setupSelect.selectedIndex=0;
				 } else {
				 	 options.length=0;
					 for(var i=0; i<setupTimesList[0].length; i++) {
						  if(setupTimesList[0][i]==activityId) {
							  var ii=options.length;
							  options.length=ii+1;
							  options[ii].value =setupTimesList[1][i];
							  options[ii].text  =setupTimesList[2][i];
							  if(value==setupTimesList[1][i]) setupSelect.selectedIndex=ii;
						  }
					 }
				 }
				  if(setupSelect.selectedIndex<0) setupSelect.selectedIndex=0;
			 } else {
				 if(activityId<=0) setupSelect.value="0";
				 else {
					 var setup=setupSelect.value;
					 for(var i=0; i<setupTimesList[0].length; i++) {
						 if(setupTimesList[0][i]==activityId) {
							 if(setup!=null&&setup>0) {
								 if(setup==setupTimesList[1][i]) setupSelect.value=setupTimesList[1][i];
							 } else {
								 setupSelect.value=setupTimesList[1][i];
								 break;
							 }
						 }
					 }
					 if(setupSelect.value==null||setupSelect.value==0||setupSelect.value<0) {
						 for(var i=0; i<setupTimesList[0].length; i++) {
							 if(setupTimesList[0][i]==activityId) {
								 setupSelect.value=setupTimesList[1][i];
								 break;
							 }
						 }
					 }
				 }
			 }
		 }
	 }
    function displayDurations(){
		if(durationList!=null){
	      durationSelect=window.document.forms[0].duration;
	      if(durationSelect.type=='select-one') {
				durationSelect.disabled=false;
	         var options=durationSelect.options;
	         var durationId=-10;
				if(durationSelect.selectedIndex>=0) durationId=options[durationSelect.selectedIndex].value;
	         options.length=0;
	         var selected=(durationId==-1)? true : false;
	         if(activityId<=0) {
	            options.length=1;
	            options[0].value=-1;
	            options[0].text=allDurations;
	            durationSelect.selectedIndex=0;
	         } else {
	           for(var i=0; i<durationList[0].length; i++) {
	               if(durationList[0][i]==activityId) {
	                  var ii=options.length;
	                  options.length=ii+1;
	                  options[ii].value =durationList[1][i];
	                  options[ii].text  =durationList[2][i];
	                  if(durationId== durationList[1][i]) durationSelect.selectedIndex=ii;
	               }
	           }
	         }
	         if(durationSelect.selectedIndex<0) durationSelect.selectedIndex=0;
	      } else {
	         if(activityId<=0) {
	            durationSelect.value=-1;
	            window.document.forms[0].durationLabel.value=allDurations;
	         } else {
					var dur=durationSelect.value;
					var durSelected=false;
					for(var i=0; i<durationList[0].length; i++) {
	               if(durationList[0][i]==activityId) {
							if(dur!=null&&dur>0) {
								if(dur==durationList[1][i]) {
									window.document.forms[0].durationLabel.value=durationList[2][i];
									durSelected=true;
								}
							} else {
								durationSelect.value=durationList[1][i];
								window.document.forms[0].durationLabel.value=durationList[2][i];
	                     durSelected=true;
								break;
							}
	               }
					}
					if(!durSelected) {
						for(var i=0; i<durationList[0].length; i++) {
							if(durationList[0][i]==activityId) {
								durationSelect.value=durationList[1][i];
								window.document.forms[0].durationLabel.value=durationList[2][i];
								break;
							}
						}
					}
	         }
	      }
	      setDurationLabel();
	    }
    }
   function displayResource() {
      var ii;
      var selected;
      var resourceId;
      resourceSelect=window.document.forms[0].resource;
      if (resourceSelect.type=='select-one') {
      	resourceSelect.disabled=false;
         resourceId=resourceSelect.options[resourceSelect.selectedIndex].value;
         if(activityId==-1) {
            var length=0;
            if(resourceSelectOptionFirst==true) {
               resourceSelect.options.length=length+1;
               resourceSelect.options[length].value=selectOptionId;
               resourceSelect.options[length++].text=selectOption;
            }
            if(programList[programIndex].groupRule!=1){
               resourceSelect.options.length=length+1;
               resourceSelect.options[length].value=-1;
               resourceSelect.options[length++].text=any+' '+programList[programIndex].resourceName;
               if(resourceId==-1) resourceSelect.selectedIndex=length-1;
               if(programList[programIndex].isGender==1){
                  resourceSelect.options.length=length+2;
                  resourceSelect.options[length].value =-3;
                  if(resourceId==-3)resourceSelect.selectedIndex=length;
                  resourceSelect.options[length++].text  =anyMale+' '+programList[programIndex].resourceName;
                  resourceSelect.options[length].value =-2;
                  if(resourceId==-2)resourceSelect.selectedIndex=length;
                  resourceSelect.options[length++].text  =anyFemale+' '+programList[programIndex].resourceName;
               }
            }
				if(programList[programIndex].groupRule!=2){
					for(var i=0; i<resourceList[0].length; i++) {
						var rId=resourceList[0][i];
						var add=false;
						for(var j=0; j<activityResource[0].length; j++) {
							var aId=-1;
							if(activityResource[1][j]==rId) aId=activityResource[0][j];
							if(aId>0) {
								for(var k=0; k<programActivity[0].length; k++) {
									if(programId==programActivity[0][k]&&aId==programActivity[1][k]) {
										add=true;
										break;
									}
								}
							}
							if(add==true) {
								resourceSelect.options.length=length+1;
								resourceSelect.options[length].value=rId;
								if(resourceId==rId) resourceSelect.selectedIndex=length;
								resourceSelect.options[length++].text=resourceList[1][i];
								break;
							}
						}
					}
				}
            if(resourceSelectOptionFirst==false) {
               resourceSelect.options.length=length+1;
               resourceSelect.options[length].value=selectOptionId;
               resourceSelect.options[length++].text=selectOption;
            }
         } else {
            var index=resourceSelect.selectedIndex;
            if(index!=null) {
               if(activityId!=selectOptionId) {
                  if(index<0) index=0;
                  resourceId=resourceSelect.options[index].value;
                  resourceSelect.options.length=0;
                  if(resourceSelectOptionFirst==true) {
                     resourceSelect.options[resourceSelect.options.length]=
                        new Option(selectOption, selectOptionId, false, selected);
                  }
                  if(programList[programIndex].groupRule!=1){
                     ii=resourceSelect.options.length;
                     resourceSelect.options.length=ii+1;
                     resourceSelect.options[ii].value =-1;
                     resourceSelect.options[ii].text  =any+' '+programList[programIndex].resourceName;
                     if(resourceId==-1)resourceSelect.selectedIndex=ii;
                     if(programList[programIndex].isGender==1){
                        ii=resourceSelect.options.length;
                        resourceSelect.options.length=ii+2;
                        resourceSelect.options[ii].value =-3;
                        resourceSelect.options[ii].text  =anyMale+' '+programList[programIndex].resourceName;
                        if(resourceId==-3)resourceSelect.selectedIndex=ii;
                        ii++;
                        resourceSelect.options[ii].value =-2;
                        resourceSelect.options[ii].text  =anyFemale+' '+programList[programIndex].resourceName;
                        if(resourceId==-2)resourceSelect.selectedIndex=ii;
                     }
                  }
                  if(programList[programIndex].groupRule!=2){
                     filterOptions( activitySelect, resourceSelect, activityResource, resourceList, resourceId);
                  }
                  if(resourceSelectOptionFirst==false) {
                     ii=resourceSelect.options.length;
                     resourceSelect.options.length=ii+1;
                     resourceSelect.options[ii].value =selectOptionId;
                     resourceSelect.options[ii].text  =selectOption;
                  }
               } else {
                  resourceSelect.options.length=1;
                  resourceSelect.options[0]=new Option(selectOption, selectOptionId, false, true);
               }
               index=resourceSelect.selectedIndex;
               if(index<0) resourceSelect.selectedIndex=0;
            }
         }
      	if(resourceSelect.selectedIndex<0) resourceSelect.selectedIndex=0;
      }
      onResourceChange();
   }
function onResourceChange() {
   if (resourceSelect.type=='select-one') {
      var resourceId=resourceSelect.options[resourceSelect.selectedIndex].value;
      if(resourceId!=selectOptionId) window.document.forms[0].selectedResourceName.value=resourceSelect.options[resourceSelect.selectedIndex].text;
      else window.document.forms[0].selectedResourceName.value='';
   } else {
      if(activityId!=selectOptionId&&activityResource[0].length>0) {
			var resourceId=window.document.forms[0].resource.value;
         if(activityId==-1){
				if(programList[programIndex].groupRule==1) {
					var sResource=null;
					var i;
parentLoop:		for(i=0; i<activityResource[0].length; i++) {
                  if(resourceId<0||resourceId==activityResource[1][i]) {
                     var sActivity=activityResource[0][i];
                     for(var j=0; j<programActivity[0].length; j++) {
                        if(programActivity[0][j]==programId&&programActivity[1][j]==sActivity) {
                           sResource=resourceId;
                           break parentLoop;
                        }
							}
						}
					}
					resourceId=sResource;
               window.document.forms[0].resource.value=resourceId;
               window.document.forms[0].selectedResourceName.value=resourceList[1][i];
				} else {
               window.document.forms[0].resource.value=-1;
               window.document.forms[0].selectedResourceName.value=any+' '+programList[programIndex].resourceName;
				}
         } else {
         	resourceId=document.forms[0].resource.value;
            var validResource=false;
            var index=0;
           	for (var i=0; i<activityResource[0].length; i++) {
            	if(activityResource[0][i]==activityId&&activityResource[1][i]==resourceId) {
                	validResource=true;
                	break;
               }
           	}
           	if(validResource) {
               for (index=0; index<resourceList[0].length; index++) {
                    if(resourceList[0][index]==resourceId) break;
               }
               window.document.forms[0].resource.value=resourceId;
               window.document.forms[0].selectedResourceName.value=resourceList[1][index];
           	} else {
					if(programList[programIndex].groupRule==1) {
	               for (index=0; index<activityResource[0].length; index++) {
	                    if(activityResource[0][index]==activityId) break;
	               }
	               resourceId=activityResource[1][index];
	               for (index=0; index<resourceList[0].length; index++) {
	                    if(resourceList[0][index]==resourceId) break;
	               }
	               window.document.forms[0].resource.value=resourceId;
	               window.document.forms[0].selectedResourceName.value=resourceList[1][index];
					} else {
	               window.document.forms[0].resource.value=-1;
	               window.document.forms[0].selectedResourceName.value=any+' '+programList[programIndex].resourceName;
					}
				}
         }
      } else {
         window.document.forms[0].resource.value=selectOptionId;
         window.document.forms[0].selectedResourceName.value='';
      }
   }
}
function getProgramDescription(){
   displayDescription('?table=Program&id=', programSelect);
}
function getActivityDescription(name){
   displayDescription('?table=Activity&id=', activitySelect, name);
}
function getResourceDescription(){
   programSelect=window.document.forms[0].program;
   activitySelect=window.document.forms[0].activity;
   displayDescription('?table=Resource&id=', window.document.forms[0].resource, programList[programIndex].resourceName);
}
function displayDescription(query, select) {
	if(select!=null) { 
		var id;
		var title;
		var urlString;
		var display=true;
		if (select.type=='select-one') {
			display=!select.disabled;
			id=select.options[select.selectedIndex].value;
			title=encode(select.options[select.selectedIndex].text);
		} else {
			id=select.value;
			title=1;
		}
		if(display) {
			urlString='Description.do'+getJsession()+query+id+'&title='+encode(title)+"&method=load";
			if(id<0&&id!=selectOptionId) {
				var index;
				if(programSelect.type=='select-one') {
					index=programSelect.selectedIndex;
					if (index<0) index=0;
					programId=programSelect.options[index].value;
				} else programId=window.document.forms[0].program.value;
				if(activitySelect.type=='select-one') {
					index=activitySelect.selectedIndex;
					if (index<0) index=0;
					activityId=activitySelect.options[index].value;
				} else activityId=window.document.forms[0].activity.value;
				urlString+='&ProgramId='+programId+'&ActivityId='+activityId
			}
			if(arguments.length>2) urlString+='&name='+encode(arguments[2]);
			if(id!=selectOptionId) openPopUp(urlString,  640, 420);
		}
	}
}
function setDurationLabel(){
   durationSelect=window.document.forms[0].duration;
   if(durationSelect.type=='select-one') {
      var options=durationSelect.options;
      var index=durationSelect.selectedIndex;
      window.document.forms[0].durationLabel.value=options[index].text;
   }
}
function setSelectDisabled(select){
   if(select.type=='select-one') {
      select.disabled=true;
      select.options.length=1;
      select.options[0].value=-10;
      select.options[0].text='     ';
		select.selectedIndex=0;
   } else select.value=-10;
}
