function forminit(theFormID){
	/*
	if ( document.getElementById(theFormID + '_control') ) {
		var theSpamControl = document.getElementById(theFormID + '_control');
		theSpamControl.parentNode.removeChild(theSpamControl);
	}*/
	
	if ( document.getElementById && document.getElementById(theFormID) ) {
		theForm = document.getElementById(theFormID);
		theForm.validate = function(){
			return domValidateForm(this,false);
		};
		theForm.oldsubmit = ( typeof theForm.onsubmit == 'function' ) ? theForm.onsubmit : function(){return true;};
		theForm.onsubmit = function() {
			if ( domValidateForm(this,true) ) {
				return this.oldsubmit();
			} else return false;
		}
	}
	
	/*
		Developed by Robert Nyman, http://www.robertnyman.com
		Code/licensing: http://code.google.com/p/getelementsbyclassname/
	*/	
	var getElementsByClassName = function (className, tag, elm){
		if (document.getElementsByClassName) {
			getElementsByClassName = function (className, tag, elm) {
				elm = elm || document;
				var elements = elm.getElementsByClassName(className),
					nodeName = (tag)? new RegExp("\\b" + tag + "\\b", "i") : null,
					returnElements = [],
					current;
				for(var i=0, il=elements.length; i<il; i+=1){
					current = elements[i];
					if(!nodeName || nodeName.test(current.nodeName)) {
						returnElements.push(current);
					}
				}
				return returnElements;
			};
		}
		else if (document.evaluate) {
			getElementsByClassName = function (className, tag, elm) {
				tag = tag || "*";
				elm = elm || document;
				var classes = className.split(" "),
					classesToCheck = "",
					xhtmlNamespace = "http://www.w3.org/1999/xhtml",
					namespaceResolver = (document.documentElement.namespaceURI === xhtmlNamespace)? xhtmlNamespace : null,
					returnElements = [],
					elements,
					node;
				for(var j=0, jl=classes.length; j<jl; j+=1){
					classesToCheck += "[contains(concat(' ', @class, ' '), ' " + classes[j] + " ')]";
				}
				try	{
					elements = document.evaluate(".//" + tag + classesToCheck, elm, namespaceResolver, 0, null);
				}
				catch (e) {
					elements = document.evaluate(".//" + tag + classesToCheck, elm, null, 0, null);
				}
				while ((node = elements.iterateNext())) {
					returnElements.push(node);
				}
				return returnElements;
			};
		}
		else {
			getElementsByClassName = function (className, tag, elm) {
				tag = tag || "*";
				elm = elm || document;
				var classes = className.split(" "),
					classesToCheck = [],
					elements = (tag === "*" && elm.all)? elm.all : elm.getElementsByTagName(tag),
					current,
					returnElements = [],
					match;
				for(var k=0, kl=classes.length; k<kl; k+=1){
					classesToCheck.push(new RegExp("(^|\\s)" + classes[k] + "(\\s|$)"));
				}
				for(var l=0, ll=elements.length; l<ll; l+=1){
					current = elements[l];
					match = false;
					for(var m=0, ml=classesToCheck.length; m<ml; m+=1){
						match = classesToCheck[m].test(current.className);
						if (!match) {
							break;
						}
					}
					if (match) {
						returnElements.push(current);
					}
				}
				return returnElements;
			};
		}
		return getElementsByClassName(className, tag, elm);
	};
	
	function domValidateForm(thisForm,showAlerts) {
		var theReturn = true;
		var thisClass = "";
		var thisName = "";
		var thisValue = "";
		var emailReg = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		var dateReg = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{2,4}$/;
		var phoneReg = /^([0-9+ ()-])+/;
		var intReg = /^\d+$/;
		var handled = {};
		var checked = false;
		for ( var r = 0 ; r < thisForm.elements.length ; r++ ) {
			thisClass = " " + thisForm.elements[r].className + " ";
			thisSpan = thisForm.elements[r].parentNode.getElementsByTagName('span');
			thisName = ( thisSpan.length > 0 ) ? 'The field labeled "' + thisSpan[0].innerHTML + '"' : thisForm.elements[r].name;
			thisAlert = ( thisForm.elements[r].title != '' ) ? thisForm.elements[r].title : '';
			if ( thisClass.indexOf(' required ') > -1 ) {
				if ( ( thisForm.elements[r].type == 'text' || thisForm.elements[r].type == 'password' || thisForm.elements[r].type == 'textarea' || thisForm.elements[r].type == 'file' ) && thisForm.elements[r].value.length == 0 ) {
					if ( showAlerts ) {
						alert(( thisAlert.length > 0 ) ? thisAlert : thisName + ' is required before submitting the form');
					}
					theReturn = false;
				} else if ( thisForm.elements[r].type == 'select-one' && thisForm.elements[r].options[thisForm.elements[r].selectedIndex].value == '' ) {
					if ( showAlerts ) {
						alert(( thisAlert.length > 0 ) ? thisAlert : thisName + ' is required before submitting the form');
					}
					theReturn = false;
				} else if ( thisForm.elements[r].type == 'select-multiple' && thisForm.elements[r].selectedIndex == -1 ) {
					if ( showAlerts ) {
						alert(( thisAlert.length > 0 ) ? thisAlert : thisName + ' is required before submitting the form');
					}
					theReturn = false;
				} else if ( thisForm.elements[r].type == 'radio' || thisForm.elements[r].type == 'checkbox' ) {
					if ( typeof handled[thisForm.elements[r].name] == 'undefined' ) {
						checked = false;
						if ( typeof thisForm[thisForm.elements[r].name].length == 'undefined' ) {
							checked = ( thisForm.elements[r].checked == true );
						} else {
							for ( var e = 0 ; e < thisForm[thisForm.elements[r].name].length ; e++ ) {
								if ( thisForm[thisForm.elements[r].name][e].checked == true ) {
									checked = true;
									break;
								}
							}
						}
						if ( !checked ) {
							if ( showAlerts ) {
								alert(( thisAlert.length > 0 ) ? thisAlert : thisName+' is required before submitting the form');
							}
							theReturn = false;
						}
						handled[thisForm.elements[r].name] = true;
					}
				}
			}
			if ( thisClass.indexOf(' required-one ') > -1 && !checked ) {
				var theFields = getElementsByClassName("required-one","",thisForm);
				var passed = false;
				for ( var f = 0 ; f < theFields.length ; f++ ) {
					if ( 
							( typeof theFields[f].checked != 'undefined' && theFields[f].checked == true ) ||
							( ( theFields[f].type == 'text' || theFields[f].type == 'password' || theFields[f].type == 'textarea' || theFields[f].type == 'file' ) && theFields[f].value.length > 0 ) ||
							( theFields[f].type == 'select-one' && theFields[f].options[theFields[f].selectedIndex].value != '' ) ||
							( theFields[f].type == 'select-multiple' && theFields[f].selectedIndex != -1 )
					) {
						passed = true;
						break;
					}
				}
				if ( !passed ) {
					if ( showAlerts ) {
						alert(( thisAlert.length > 0 ) ? thisAlert : thisName + ' or one of the other options must be chosen');
					}
					theReturn = false;
				}
				checked = true;
			}
			if ( thisClass.indexOf(' integer ') > -1 && ( thisForm.elements[r].type == 'text' || thisForm.elements[r].type == 'password' ) && thisForm.elements[r].value.length > 0 ) {
				if ( !intReg.test(thisForm.elements[r].value) ) {
					if ( showAlerts ) {
						alert(( thisAlert.length > 0 ) ? thisAlert : thisName + ' must be a proper number');
					}
					theReturn = false;
				}
			}
			if ( thisClass.indexOf(' numeric ') > -1 && ( thisForm.elements[r].type == 'text' || thisForm.elements[r].type == 'password' ) && thisForm.elements[r].value.length > 0 ) {
				if ( thisForm.elements[r].value.length > 0 && ( parseFloat(thisForm.elements[r].value) != thisForm.elements[r].value-0 ) ) {
					if ( showAlerts ) {
						alert(( thisAlert.length > 0 ) ? thisAlert : thisName + ' must be a proper number');
					}
					theReturn = false;
				}
			}
			if ( thisClass.indexOf(' email ') > -1 && ( thisForm.elements[r].type == 'text' || thisForm.elements[r].type == 'password' ) && thisForm.elements[r].value.length > 0 ) {
				var arEmails = thisForm.elements[r].value.replace(' ','').split(/[;,]/);
				var checked = true;
				for ( var f = 0 ; f < arEmails.length ; f++ ) {
					if ( !emailReg.test(arEmails[f]) ) {
						checked = false;
						break;
					}
				}
				if ( !checked ) {
					if ( showAlerts ) {
						alert(( thisAlert.length > 0 ) ? thisAlert : thisName + ' must be a valid email address to submit the form');
					}
					theReturn = false;
				}
			}
			if ( thisClass.indexOf(' phone ') > -1 && ( thisForm.elements[r].type == 'text' || thisForm.elements[r].type == 'password' ) && thisForm.elements[r].value.length > 0 ) {
				if ( !phoneReg.test(thisForm.elements[r].value) ) {
					if ( showAlerts ) {
						alert(( thisAlert.length > 0 ) ? thisAlert : thisName + ' must be a valid phone number like "(555) 555-5555"');
					}
					theReturn = false;
				}
			}
			if ( thisClass.indexOf(' date ') > -1 && ( thisForm.elements[r].type == 'text' || thisForm.elements[r].type == 'password' ) && thisForm.elements[r].value.length > 0 ) {
				if ( !dateReg.exec(thisForm.elements[r].value) ) {
					if ( showAlerts ) {
						alert(( thisAlert.length > 0 ) ? thisAlert : thisName + ' must be a valid date to submit the form, like "mm/dd/yy"');
					}
					theReturn = false;
				}
			}
			if ( thisClass.indexOf(' confirm ') > -1 && ( thisForm.elements[r].type == 'text' || thisForm.elements[r].type == 'password' ) && thisForm.elements[r].value.length > 0 ) {
				if ( r > 0 && thisForm.elements[r].value != thisForm.elements[r-1].value ) {
					if ( showAlerts ) {
						alert(( thisAlert.length > 0 ) ? thisAlert : thisName + ' and ' + prevName + ' must have the same values');
					}
					theReturn = false;
				}
			}
			prevName = thisName;
		}
		return theReturn;
	}
}
window.addEvent('domready',function(){
	var theForms = $$('form');
	theForms.each(function(thisForm){
		if ( thisForm.hasClass('ajax') ) {
			thisForm.onsubmit = function(){
				var theTime = new Date();
				thisForm.send({
					data: thisForm.toQueryString() + '&method=ajax&ts='+theTime.getTime(),
					method: 'post',
					evalScripts: true
				});
				return false;
			}
		}
		if ( thisForm.getProperty('id') ) {
			forminit(thisForm.getProperty('id'));
		}
	});
		
	
	
	var openWin = function ( windowURL, windowName, windowFeatures ) { 
		return window.open( windowURL, windowName, windowFeatures ) ; 
	} 
	
	$$('a[class^=popup_]').addEvent('click',function(e){
		var width = this.className.split('_')[1];
		var height = this.className.split('_')[2];
		newWindow = openWin( this.href+'&method=ajax', 'popup', 'width='+width+',height='+height+',toolbar=0,location=0,directories=0,status=1,menuBar=0,scrollBars=1,resizable=1' ); 
		newWindow.focus();
		e = new Event(e).stop();
	});
	
	var hasImagePopup = false;
	
	$$('option[class=cms_optionimage]').each(function(option){
		hasImagePopup = true;
		var theSelect = option.getParent();
		if ( !document.getElementById('image_'+theSelect.getProperty('id')) ) {
			var thePicker = document.createElement('ul'); thePicker = $(thePicker).set({
				id: 'image_'+theSelect.getProperty('id'),
				'class': 'cms_optionimagepicker hidden'
			}).injectBefore(theSelect);
			theSelect.addEvent('focus',function(e){
				var theCoords = theSelect.getCoordinates();
				var theHeight;
				var screenHeight = window.getHeight();
				$$('body')[0].fireEvent('click');
				theSelect.addClass('hidden').getParent().setStyles({'z-index':'40'});
				theHeight = screenHeight - theCoords.top.toInt() + window.getScrollTop() - 40;
				thePicker.setStyles({
					'max-height': theHeight + 'px'
				}).removeClass('hidden').getElement('a').focus();
			});
			theSelect.addClass('cms_optionpicker').getElements('option').each(function(_option){
				var theItem = document.createElement('li'); theItem = $(theItem).set({
					'class': thePicker.getElements('li').length,
					events: {
						click: function(e){
							theSelect.removeClass('hidden').getParent().setStyles({'z-index':'20'});
							document.getElementById(theSelect.getProperty('id')).options.selectedIndex = this.className;
							thePicker.addClass('hidden');
							var e = new Event(e).preventDefault().stopPropagation().stop();
						}
					}
				}).injectInside(thePicker);
				var theItemImage = document.createElement('a'); $(theItemImage).set({
					'class': 'cms_productoptionimage',
					title: _option.getProperty('title'),
					href: '#',
					name: 'imageoption'+thePicker.getElements('li').length+1,
					rel: 'cms_productoptionimage'
				}).setHTML((function(){
					return ( _option.getProperty('title').length > 0 ) ?
						'<img height="100" src="'+_option.getProperty('title')+'" alt="'+_option.value+'" />' :
						'<img height="100" src="/_engine/images/no-image.jpg" alt="'+_option.value+'" />';
				})());
				theItem.adopt(theItemImage);
				var theItemSpan = document.createElement('span'); theItemSpan = $(theItemSpan).setText(_option.text);
				theItem.adopt(theItemSpan);
			});
		}
	});
	
	if ( hasImagePopup ) {
		$$('body')[0].addEvent('click',function(e){
			$$('.cms_optionimagepicker').each(function(imgpicker){
				imgpicker.addClass('hidden').getParent().setStyles({'z-index':'20'});
			});
			$$('.cms_optionpicker').removeClass('hidden');
		});
	}
	
	$$('a.magnifier').each(function(magnifier){
		var theBody = $$('body')[0];
		var theCoords = magnifier.getElement('img').getCoordinates();
		var magCoords;
		var magImage = new Asset.image(magnifier.getProperty('href'),{
			onload: function(){
				var regionHeight = Math.max(theCoords.height.toInt(),magnifier.getElement('img').height.toInt());
				var regionWidth = Math.max(theCoords.width.toInt(),magnifier.getElement('img').width.toInt());
				if ( magImage.width.toInt() / regionWidth > 1.25 ) {
					var theRegion = document.createElement('div'); theRegion = $(theRegion).set({
						'class': 'cms_magnifierregion',
						title: 'Move your mouse over this region to "zoom in" on an area of the picture, or click to view the full-size image.',
						styles: 'width: ' + magnifier.getElement('img').width.toInt() + 'px; height: ' + magnifier.getElement('img').height.toInt() + 'px; left: ' + theCoords.left.toInt() + 'px; top: ' + theCoords.top.toInt() + 'px;',
						events: {
							'click': function(e){
								Slimbox.open(magnifier.getProperty('href'), magnifier.getProperty('title'));
							},
							'mouseover': function(){
								theMagnifier.setStyles({
									'display': 'block'
								});
							},
							'mouseout': function(){
								theMagnifier.setStyles({
									'display': 'none'
								});
							},
							'mousemove': function(e){
								var e = new Event(e);
								var bodyLeft = parseInt(theBody.getStyle('margin-left'), 10);
								var bodyTop = parseInt(theBody.getStyle('margin-top'), 10);
								var htmlTop = parseInt(theBody.getParent().getStyle('border-top-width'),10);
								var htmlLeft = parseInt(theBody.getParent().getStyle('border-left-width'),10);
								var magLeft = 0;
								var magTop = 0;
								htmlTop = ( !isNaN(htmlTop) ) ? htmlTop : 0;
								htmlLeft = ( !isNaN(htmlLeft) ) ? htmlLeft : 0;
								bodyLeft = ( !isNaN(bodyLeft) ) ? bodyLeft : 10;
								bodyTop = ( !isNaN(bodyTop) ) ? bodyTop : 10;
								magLeft = Math.max(1, Math.round((Math.max(1, e.page.x - theCoords.left.toInt() - bodyLeft - htmlLeft)) / regionWidth * magImage.width) - 50);
								magTop = Math.max(1, Math.round((Math.max(1, e.page.y - theCoords.top.toInt() - bodyTop - htmlTop)) / regionHeight * magImage.height) - 50);
								theMagnifier.setStyles({
									'left': (e.page.x - bodyLeft - htmlLeft - 50) + 'px',
									'top': (e.page.y - bodyTop - htmlTop - 50) + 'px',
									'background-position': '-' + magLeft + 'px -' + magTop + 'px'
								})
							}
						}
					}).injectInside(theBody);
				}
			}
		});
		var theMagnifier = document.createElement('div'); theMagnifier = $(theMagnifier).set({
			'class': 'cms_magnifier',
			styles: 'display: none; background: url('+magnifier.getProperty('href')+') no-repeat; left: '+theCoords.left.toInt()+'px; top: '+theCoords.top.toInt()+'px;'
		}).injectInside(theBody);
	});
	
});

