function ModalForm(name, containerId, zindex){
	
	this.name = name;
	this.container = $(containerId);
	/* Grab a reference to the actual form */
	this.form = this.container.select("form.modalMain")[0];
	
	/* And create an Event to handle post-submit
	   processing */
	this.complete = new YAHOO.util.CustomEvent("complete", this);
	/* Function to intercept form submission and issue
	   an AJAX call */
	this.handleSubmit = function(event,form){
		var container = this;
		form.request({
			parameters: {
				'submitmode':'ajax',
				'submit.x':'',
				'submit.y':'',
				'saveCourse.x':'',
				'saveCourse.y':'',
				'atg.formHandlerUseForwards':'true' },
			onSuccess: function(transport) {
				try{
					response = transport.responseText.evalJSON();
					container.complete.fire(response);
				}
				catch(e){
					if(console) console.log(e);
				}
			}
		});
	}
	
	this.prepareForm = function(){
		var panel = this.panel;
		var ref = this;this.container
		this.form.select(".formCancel").each(function(cancelLink){
			cancelLink.href = "#";
			Event.observe(cancelLink,"click",function(event){
				Event.stop(event);
				ref.hide();
			})
			
		});
		if(this.form.select(".fpURL")){
			this.form.select(".fpURL").each(function(urlInput){
				urlInput.value = "/renderJSON.data"
			});
		}
	}
	
	this.hide = function(){
		this.panel.hide();
		this.container.hide();
		if(this.parentForm){
			this.parentForm.show();
		}
	}
	
	this.show = function(){
		if(this.parentForm){
			this.parentForm.hide();
		}
		this.panel.show();
		this.container.show();
		this.redraw();
	}
	
	this.redraw = function(){
		this.panel.hide();
		this.panel.show();
	}
	
	/* initialzes form event handling */
	this.init = function(){
		var modalForm = this;
		Event.observe(this.form,'submit',function(event){
			Event.stop(event);
			modalForm.handleSubmit(event,modalForm.form);
		});
		this.prepareForm();
		
		/* Initialize the panel */
		var panel = new YAHOO.widget.Panel(name,{
			width:"500px",  
			fixedcenter: true,
			constraintoviewport: true,
			modal:true,
			visible:false,
			draggable:false,
			close:true,
			zindex: zindex,
			iframe: true,
			underlay: "none"
		});
		
		var container = this;
		
		panel.setBody(this.container);
		panel.render(document.body);
		$(name).select(".container-close").each(function(item){
			Element.observe($(item), "click", function(event){
				Event.stop(event);
				container.hide();
			});
		});
		
		this.panel = panel;
	}
	
	this.init();

}

function Consumer(name,form,callback){
	this.form = form;
	this.callback = callback;
	form.complete.subscribe(this.callback, this);
}


function AoiModal(name, containerId, zindex,site){
	this.name = name;
	this.container = $(containerId);
	/* And create an Event to handle post-submit
	   processing */
	this.complete = new YAHOO.util.CustomEvent("complete", this);
	this.prepare = function(){
		var panel = this.panel;
		var ref = this;
		this.container.select(".formCancel").each(function(cancelLink){
			cancelLink.href = "#";
			Event.observe(cancelLink,"click",function(event){
				Event.stop(event);
				ref.hide();
			})
			
		});
		if(this.container.select("modalAOIPanel")){
			Event.observe("modalAOISave","click",function(event){
				var aoiNames='';
				var aoiValues='';
				var aoiSelected;
				if(site!='corwin-press'){
					$("modalAOIPanel").select(".aoiItemButton").each(function(frmInput){
						if(frmInput.checked){
							aoiSelected = (frmInput.value).split("_");
							if(aoiNames!=''){
								aoiNames = aoiNames +', '+aoiSelected[2];
								aoiValues = aoiValues +'*'+aoiSelected[0]+'_'+aoiSelected[1];
							}else{
								aoiNames = aoiSelected[2];
								aoiValues = aoiSelected[0]+'_'+aoiSelected[1];
								if($("selectAOI")){
									$("selectAOI").style.display="none";
									$("editAOI").style.display="inline";
								}
							}
						}	
					});
					$("modalAOIPanel").select(".aoiCheckButton").each(function(frmInput){
						if(frmInput.checked){
							aoiSelected = (frmInput.value).split("_");
							var parentId =  aoiSelected[1];
							if(aoiValues!='' && aoiValues.indexOf(parentId+'_')>=0){ 
							}else{
								if(aoiNames!=''){
									aoiNames = aoiNames +', '+aoiSelected[2];
									aoiValues = aoiValues +'*'+aoiSelected[0]+'_'+aoiSelected[1];
								}else{
									aoiNames = aoiSelected[2];
									aoiValues = aoiSelected[0]+'_'+aoiSelected[1];
									if($("selectAOI")){
										$("selectAOI").style.display="none";
										$("editAOI").style.display="inline";
									}
								}
							}
						}
					});
					if($("selAoiNames")){
						$("selAoiNames").update(aoiNames);
						$("areaOfInterest").value=aoiValues.split('*');
					}
				}else{
					$("modalAOIPanel").select(".toggle").each(function(frmInput){
						if(frmInput.checked){
							if(aoiNames!=''){
								aoiNames = aoiNames +', '+frmInput.id;
								aoiValues = aoiValues +','+frmInput.value;
							}else{
								aoiNames = frmInput.id;
								aoiValues = frmInput.value;
								if($("selectAOI")){
									$("selectAOI").style.display="none";
									$("editAOI").style.display="inline";
								}
							}
						}	
					});
					if($("selAoiNames")){
						$("selAoiNames").update(aoiNames);
						$("areaOfInterest").value=aoiValues;
					}
				}
				Event.stop(event);
				ref.hide();
			})
		}
	}
	this.hide = function(){
		this.panel.hide();
		this.container.hide();
		if(this.parentForm){
			this.parentForm.show();
		}
	}
	
	this.show = function(){
		if(this.parentForm){
			this.parentForm.hide();
		}
		this.panel.show();
		this.container.show();
		this.redraw();
	}
	
	this.redraw = function(){
		this.panel.hide();
		this.panel.show();
	}
	
	/* initialzes form event handling */
	this.init = function(){
		var modalForm = this;
		this.prepare();
		/* Initialize the panel */
		var panel = new YAHOO.widget.Panel(name,{
			width:"615px",  
			fixedcenter: true,
			constraintoviewport: true,
			modal:true,
			visible:false,
			draggable:false,
			close:true,
			zindex: zindex,
			iframe: true,
			underlay: "none"
		});
		var container = this;
		
		panel.setBody(this.container);
		panel.render(document.body);
		$(name).select(".container-close").each(function(item){
			Element.observe($(item), "click", function(event){
				Event.stop(event);
				container.hide();
			});
		});
		this.panel = panel;
	}
	
	this.init();

}

function MapCourseModalForm(name, containerId, zindex){
	
	this.name = name;
	
	this.container = $(containerId);
	/* Grab a reference to the actual form */
	this.form = this.container.select("form.modalMain")[0];

	/* And create an Event to handle post-submit
	   processing */
	this.complete = new YAHOO.util.CustomEvent("complete", this);
	
	this.prepareForm = function(){
		var panel = this.panel;
		var ref = this;
		this.form.select(".formCancel").each(function(cancelLink){
			cancelLink.href = "#";
			Event.observe(cancelLink,"click",function(event){
				handleCancelMapBookToCourse();
				Event.stop(event);
				ref.hide();
			})
			
		});
		if(this.form.select(".fpURL")){
			this.form.select(".fpURL").each(function(urlInput){
				urlInput.value = "/renderJSON.data"
			});
		}
	}
	
	this.hide = function(){
		this.panel.hide();
		this.container.hide();
		if(this.parentForm){
			this.parentForm.show();
		}
	}
	
	this.show = function(){
		if(this.parentForm){
			this.parentForm.hide();
		}
		this.panel.show();
		this.container.show();
		this.redraw();
	}
	
	this.redraw = function(){
		this.panel.hide();
		this.panel.show();
	}
	
	/* initialzes form event handling */
	this.init = function(){
		var modalForm = this;
		this.prepareForm();
		
		/* Initialize the panel */
		var panel = new YAHOO.widget.Panel(name,{
			width:"500px",  
			fixedcenter: true,
			constraintoviewport: true,
			modal:true,
			visible:false,
			draggable:false,
			close:true,
			zindex: zindex,
			iframe: true,
			underlay: "none"
		});
		
		var container = this;
		
		panel.setBody(this.container);
		panel.render(document.body);
		$(name).select(".container-close").each(function(item){
			Element.observe($(item), "click", function(event){
				handleCancelMapBookToCourse();
				Event.stop(event);
				container.hide();
			});
		});
		
		this.panel = panel;
	}
	
	this.init();

}


function CourseModalForm(name, containerId, zindex){
	
	this.name = name;
	
	this.container = $(containerId);
	/* Grab a reference to the actual form */
	this.form = this.container.select("form.modalMain")[0];

	/* And create an Event to handle post-submit
	   processing */
	this.complete = new YAHOO.util.CustomEvent("complete", this);
	
	/* Function to intercept form submission and issue
	   an AJAX call */
	this.handleSubmit = function(event,form){
		
		var container = this;
		if (saveCourseDetails(event,form,'add')) {
			form.request({
				parameters: {
					'submitmode':'ajax',
					'submit.x':'',
					'submit.y':'',
					'saveCourse.x':'',
					'saveCourse.y':'',
					'atg.formHandlerUseForwards':'true' },
				onSuccess: function(transport) {
					try{
						response = transport.responseText.evalJSON();
						container.complete.fire(response);
					}
					catch(e){
						if(console) console.log(e);
					}
				}
			});
			Event.stop(event);
			container.hide();
		}
	}
	
	
	this.prepareForm = function(){
		var panel = this.panel;
		var ref = this;
		this.form.select(".formCancel").each(function(cancelLink){
			cancelLink.href = "#";
			Event.observe(cancelLink,"click",function(event){
				Event.stop(event);
				ref.hide();
			})
			
		});
		if(this.form.select(".fpURL")){	
			this.form.select(".fpURL").each(function(urlInput){
				urlInput.value = "/renderJSON.data"
			});
		}
	}
	
	this.hide = function(){
		this.panel.hide();
		this.container.hide();
		if(this.parentForm){
			this.parentForm.show();
		}
	}
	
	this.show = function(){
		if(this.parentForm){
			this.parentForm.hide();
		}
		this.panel.show();
		this.container.show();
		this.redraw();
	}
	
	this.redraw = function(){
		this.panel.hide();
		this.panel.show();
	}
	
	/* initialzes form event handling */
	this.init = function(){
		var modalForm = this;
		
		Event.observe(this.form,'submit',function(event){
			Event.stop(event);
			modalForm.handleSubmit(event,modalForm.form);
		});		
		
		this.prepareForm();
		
		/* Initialize the panel */
		var panel = new YAHOO.widget.Panel(name,{
			width:"500px",  
			fixedcenter: true,
			constraintoviewport: true,
			modal:true,
			visible:false,
			draggable:false,
			close:true,
			zindex: zindex,
			iframe: true,
			underlay: "none"
		});
		
		var container = this;
		
		panel.setBody(this.container);
		panel.render(document.body);
		$(name).select(".container-close").each(function(item){
			Element.observe($(item), "click", function(event){
				Event.stop(event);
				container.hide();
			});
		});
		
		this.panel = panel;
	}
	
	this.init();

}
