	function logIt(item){
	 if (typeof window.console != "undefined"){ 
	 		if (typeof item == "Object") { item = Object.inspect(item); } 
	 		console.log(item); }
	}
	
function checkForImage(input){
	input = $(input);
	var inputFile = $F(input).toLowerCase();
	if(!inputFile.endsWith('.jpg') && !inputFile.endsWith('.png') && !inputFile.endsWith('.gif')){
		alert('File must be a JPG, GIF, or PNG file!');
		clearItem(input);
	}
	else{
		var tmp = inputFile.split('.');
		Form.Element.setValue('imageFormat',tmp[tmp.length-1]);
	}
}

function validateForm(formID){
	
	var formFields = $(formID).select('.fRequired, .fEmail, .fDate, .fNumber, .fNumberPlain');
	var errorMsg = 'Correct the following errors:';
	var datePattern = /([1-9]|0[1-9]|1[012])[- /.]([1-9]|0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d$/;
	var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;

	var errorFields = []; 
	var fixFields = [];
	formFields.each(function(ff){
		var ffvalue = $F(ff);
		var fftitle = (ff.getAttribute('title') ? ff.getAttribute('title') : ff.getAttribute('name'));
		fftitle = fftitle ? fftitle : ff.identify();
		
		if(ffvalue == ''){
			if(ff.hasClassName('fRequired')){
				errorMsg += '\n  ' +fftitle + ' cannot be empty.';
				ff.addClassName('fError-req').removeClassName('fError');
				errorFields.push(ff);
			}
		}
		else{
			if(ff.hasClassName('fEmail')){
				if(!emailPattern.test(ffvalue)){
					errorMsg += '\n  ' + fftitle + ' is not a valid email address.';
					ff.addClassName('fError').removeClassName('fError-req');
					errorFields.push(ff);
				}
			}
			else if(ff.hasClassName('fDate')){
				if(!datePattern.test(ffvalue)){
					errorMsg += '\n  ' + fftitle + ' is invalid.  It must be written as \'mm/dd/yyyy\'.';
					ff.addClassName('fError').removeClassName('fError-req');
					errorFields.push(ff);
				}
			}
			else if(ff.hasClassName('fNumber')){
				if(isNaN(ffvalue.replace(/\$/g,'').replace(/,/g,'').replace(/#/g,''))){
					errorMsg += '\n  ' + fftitle + ' is invalid.  It must be a number.';
					ff.addClassName('fError').removeClassName('fError-req');
					errorFields.push(ff);
				}
				else{
					fixFields.push({'elem':ff,'newValue':ffvalue.replace(/\$/g,'').replace(/,/g,'').replace(/#/g,'')});
				}
			}
			else if(ff.hasClassName('fNumberPlain')){
				if(isNaN(ffvalue)){
					errorMsg += '\n  ' + fftitle + ' is invalid.  It must be a number.';
					ff.addClassName('fError').removeClassName('fError-req');
					errorFields.push(ff);
				}
			}
			else{ ff.removeClassName('fError').removeClassName('fError-req')};

		}
	});
	if(errorFields.length > 0){
			errorFields[0].activate();
			alert(errorMsg);
			return false;
	}else{
			fixFields.each(function(fixF){
				Form.Element.setValue(fixF.elem,fixF.newValue);
			});
			return true;
	}
}

	function watchSubmits(){
		$$('form.validateForm').each(function(f){
			var finputs = f.select('input[type="submit"]');
			if(finputs[0]){
				var finput = finputs[0];
				Event.observe(finput,'click',function(event){
					event.stop();
					Form.Element.setValue(finput,'Submitting...');
					if(cleanForm(f) && validateForm(f)){
						f.submit();
					}					
					else{Form.Element.setValue(finput,'Save');}
				})
			}
		});
	}

	function clearItem(input){
		Form.Element.setValue(input,'');
	}

	function cleanForm(formID){
		var inputs = $(formID).select('input[type="text"],textarea');
		inputs.each(function(inp){
			Form.Element.setValue(inp,$F(inp).gsub('--','&mdash;'));
		});
		if(typeof FCKeditorAPI !== 'undefined'){
			$H(FCKeditorAPI.Instances).each(function(fck){
			    fck[1].SetHTML(fck[1].GetHTML().gsub('--','&mdash;').gsub('&#45;&#45;','&mdash;'));
			})	
		}
		return true;
	}


	function newFCK(LinkID){
		var ta = $(LinkID).previous('textarea');
		//logIt(ta);
		if(ta){
			var fck = new FCKeditor(ta.identify());
			fck.BasePath = '/Shabbat/js/fckeditor/';
			fck.ToolbarSet = 'Basic'
			fck.Width = ta.getWidth() + 'px';
			fck.Height = ta.getHeight() + 'px';
			fck.Value = $F(ta);
			ta.replace(fck.CreateHtml());
			$(LinkID).remove();
		}
	}
	
	function watchFCKSwitches(){
		$$('.fckSwitch').each(function(f){
			Event.observe(f,'click',function(){
				newFCK(f);
			})
		});
	}
	
	document.observe("dom:loaded",function(){
		watchSubmits();
		$$('input[type="file"]').each(clearItem);
		watchFCKSwitches();
	});