function isEmpty(val){ if(val == null){return true;} for(var i=0; i < val.length; i++) { if ((val.charAt(i)!=' ')&&(val.charAt(i)!="\t")&&(val.charAt(i)!="\n")&&(val.charAt(i)!="\r")){ return false; } } return true; }
function isEmail(val){if (isEmpty(val)){ return false; }var i = 1,length = val.length;while ((i < length) && (val.charAt(i) != "@")){i++;}if ((i >= length) || (val.charAt(i) != "@")){ return false; }else { i += 2; }while ((i < length) && (val.charAt(i) != ".")){i++;}if ((i >= length - 1) || (val.charAt(i) != ".")){ return false; }else { return true; }}
function isPhone(val){ pattern = new RegExp(/^[0-9-\s+]{5,16}$/); if (!pattern.test(val)) { return false; } return true; }
function onSelect(what){ var destination = what.options[what.selectedIndex].value; if (destination) location.href = destination; }
function isInt(val){var y = parseInt(val);if(isNaN(y)) return false;return val == y && val.toString() == y.toString() && y > 0;} 
function isChecked(checkboxes,qty){var chks = checkboxes, checkCount = 0, checkValues = [];for (var i = 0; i < chks.length; i++){if (chks[i].checked){ checkCount++; checkValues.include(chks[i].value);}}if (checkCount < qty){ return false; }return checkValues;}
function printArea(id){var html = '<html>';html += document.getElementById(id).innerHTML;html += '</html>';var printWin = window.open('','','left=0,top=0,width=1,height=1,toolbar=0,scrollbars=0,status=0');printWin.document.write(html);printWin.document.close();printWin.focus();printWin.print();printWin.close();}

/*
authors: [atom smith]
requires: - core/1.3: [Core, Browser, Array, Function, Number, String, Hash, Event, Class.Extras, Element.Event, Element.Style, Element.Dimensions, Fx.CSS, FX.Tween, Fx.Morph]
provides: [Purr, Element.alert]
*/

var Purr = new Class({
	options: {
		mode: 'top',
		position: 'left',
		elementAlertClass: 'purr-element-alert',
		elements: {wrapper: 'div',alert: 'div',buttonWrapper: 'div',button: 'button'},
		elementOptions: {
			wrapper: {
				styles: {'position': 'fixed','z-index': '9999'},
				'class': 'purr-wrapper'
			},
			alert: {
				'class': 'purr-alert',
				styles: {'opacity': '.85','border-radius': '10px','box-shadow': '2px 2px 6px #666'}
			},
			buttonWrapper: {'class': 'purr-button-wrapper'},
			button: {'class': 'purr-button'}
		},
		alert: {
			buttons: [],
			clickDismiss: true,
			hoverWait: true,
			hideAfter: 5000,
			fx: {duration: 500},
			highlightRepeat: false,
			highlight: {start: '#FF0',end: false}
		}
	},
	Implements: [Options, Events, Chain],
	initialize: function(options){
		this.setOptions(options);
		this.createWrapper();
		return this;
	},
	bindAlert: function(){
		return this.alert.bind(this);
	},
	createWrapper: function(){
		this.wrapper = new Element(this.options.elements.wrapper, this.options.elementOptions.wrapper);
		if(this.options.mode == 'top'){ this.wrapper.setStyle('top', 0); }
		else{ this.wrapper.setStyle('bottom', 0); }
		document.id(document.body).grab(this.wrapper);
		this.positionWrapper(this.options.position);
	},
	positionWrapper: function(position){
		if(typeOf(position) == 'object'){
			var wrapperCoords = this.getWrapperCoords();
			this.wrapper.setStyles({'bottom': '','left': position.x,'top': position.y - wrapperCoords.height,'position': 'absolute'});
		}
		else if(position == 'left'){this.wrapper.setStyle('left', 0);}
		else if(position == 'right'){this.wrapper.setStyle('right', 0);}
		else{
			var winsize = window.getSize(), wrapperCoords = this.getWrapperCoords();
			if(position == 'centered'){
				this.wrapper.setStyles({
					'left': Math.floor((winsize.x-wrapperCoords.width)/2),
					'top': Math.floor((winsize.y-wrapperCoords.height)/2 - (wrapperCoords.height / 2))
				});
			}
			else{
				this.wrapper.setStyle('left', (winsize.x / 2) - (wrapperCoords.width / 2));
			}
		}
		return this;
	},
	getWrapperCoords: function(){
		this.wrapper.setStyle('visibility', 'hidden');
		var measurer = this.alert('need something in here to measure');
		var coords = this.wrapper.getCoordinates();
		Browser.ie ? measurer.empty().dispose() : measurer.destroy();
		this.wrapper.setStyle('visibility','');
		return coords;
		
	},
	alert: function(msg, options){
		options = Object.merge({}, this.options.alert, options || {});

		var alert = new Element(this.options.elements.alert, this.options.elementOptions.alert);

		if(typeOf(msg) == 'string'){alert.set('html', msg);}
		else if(typeOf(msg) == 'element'){alert.grab(msg);}
		else if(typeOf(msg) == 'array'){
			var alerts = [];
			msg.each(function(m){alerts.push(this.alert(m, options));}, this);
			return alerts;
		}

		alert.store('options', options);

		if(options.buttons.length > 0){
			options.clickDismiss = false;
			options.hideAfter = false;
			options.hoverWait = false;
			var buttonWrapper = new Element(this.options.elements.buttonWrapper, this.options.elementOptions.buttonWrapper);
			alert.grab(buttonWrapper);
			options.buttons.each(function(button){
				if(button.text !== undefined){
					var callbackButton = new Element(this.options.elements.button, this.options.elementOptions.button);
					callbackButton.set('html', button.text);
					if(button.callback !== undefined){callbackButton.addEvent('click', button.callback.pass(alert));}
					if(button.dismiss !== undefined && button.dismiss){callbackButton.addEvent('click', this.dismiss.pass(alert, this));}
					buttonWrapper.grab(callbackButton);
				}
			}, this);
		}
		if(options.className !== undefined){alert.addClass(options.className);}

		this.wrapper.grab(alert, (this.options.mode == 'top') ? 'bottom' : 'top');

		var fx = Object.merge(this.options.alert.fx, options.fx);
		var alertFx = new Fx.Morph(alert, fx);
		alert.store('fx', alertFx);
		this.fadeIn(alert);


		if(options.highlight){
			alertFx.addEvent('complete', function(){
				alert.highlight(options.highlight.start, options.highlight.end);
				if(options.highlightRepeat){alert.highlight.periodical(options.highlightRepeat, alert, [options.highlight.start, options.highlight.end]);}
			});
		}
		if(options.hideAfter){this.dismiss(alert);}
		if(options.clickDismiss){
			alert.addEvent('click', function(){
				this.holdUp = false;
				this.dismiss(alert, true);
			}.bind(this));
		}
		if(options.hoverWait){
			alert.addEvents({
				'mouseenter': function(){this.holdUp = true;}.bind(this),
				'mouseleave': function(){this.holdUp = false;}.bind(this)
			});
		}
		return alert;
	},
	fadeIn: function(alert){
		var alertFx = alert.retrieve('fx');
		alertFx.set({'opacity': 0});
		alertFx.start({'opacity': [this.options.elementOptions.alert.styles.opacity, '.9'].pick()});
	},
	dismiss: function(alert, now){
		now = now || false;
		var options = alert.retrieve('options');
		if(now){this.fadeOut(alert);}
		else{this.fadeOut.delay(options.hideAfter, this, alert);}
	},
	fadeOut: function(alert){
		if(this.holdUp){
			this.dismiss.delay(100, this, [alert, true]);
			return null;
		}
		var alertFx = alert.retrieve('fx');
		if(!alertFx){return null;}
		var to = {'opacity': 0};
		if(this.options.mode == 'top'){to['margin-top'] = '-'+alert.offsetHeight+'px';}
		else{to['margin-bottom'] = '-'+alert.offsetHeight+'px';}
		alertFx.start(to);
		alertFx.addEvent('complete', function(){Browser.ie ? alert.empty().dispose() : alert.destroy();});
	}
});

var jsbox = new Class({
	Implements:[Options,Events],
	options:{from:'.jsbox',showCount:false,autostart:[false,0,0],width:120,height:120,htmlWidth:600,htmlHeight:250,overlay:true,opacity:0.7,borderWidth:10,borderColor:'#fff'},
	initialize:function(options){
		this.setOptions(options);
		this.timer=0;
		this.index=0;
		this.opened=false;
		this.openClosePos={};
		this.attributes={};
		this.object={};
		this.build();
		switch(typeOf(this.options.from)){
			case'string':
				this.from=$$(this.options.from);
				if(this.options.autostart[0]){this.open(this.from[this.options.autostart[1]]);}
				this.from.each(function(el,i){
					el.addEvent('click',function(e){
						e.stop();
						this.open(el);
					}.bind(this));
				},this);
			break;
			case'element':
				var el=this.options.from;
				this.from=[el];
				this.options.autostart[0]?this.open(el):el.addEvent('click',function(e){
					e.stop();
					this.open(el);
				}.bind(this));
			break;
		}
	},
	build:function(){
		this.overlay=new Element('div',{id:'overlay','opacity': this.options.opacity,styles: {display:'none',position: 'fixed',background: '#000',left: 0,top: 0,'z-index': 99},events:{'click':this.close.bind(this)}}).inject(document.body,'top');
		
		var closeButton=new Element('div',{'class':'jswin-close'}).addEvent('click',this.close.bind(this));
		
		this.prevLink=new Element('div',{'class':'jswin-prevlink'}).addEvent('click',this.previous.bind(this));
		this.nextLink=new Element('div',{'class':'jswin-nextlink'}).addEvent('click',this.next.bind(this));
		
		this.captionInner=new Element('div',{'class':'jswin-captionInner'});
		
		var caption=new Element('div',{'class':'jswin-caption'}).adopt(this.captionInner);
		
		this.content=new Element('div',{'class':'jswin-content'});
		this.win=new Element('div',{'class':'jswin jswin-hide',styles:{display:'none','border-width':this.options.borderWidth,'border-color':this.options.borderColor}}).adopt(closeButton,this.content,caption).inject(document.body,'top');
		
		this.fxwin=new Fx.Morph(this.win,{duration:500,transition: Fx.Transitions.Cubic.easeOut});
		this.fxcontent=new Fx.Tween(this.content,{property:'opacity',duration:250}).set(0);
		this.fxcaption=new Fx.Tween(caption,{property:'height',duration:250});
	},
	showOverlay:function(){
		var ssize=document.getScrollSize();
		this.overlay.setStyles({display:'',width:ssize.x+'px',height:ssize.y+'px'});
	},
	getOpenClosePos:function(el){
		var cords=el.getFirst()?el.getFirst().getCoordinates():el.getCoordinates();
		this.openClosePos={
			width:cords.width<0||cords.width>400?32:cords.width-(this.options.borderWidth*2),
			height:cords.height<60?32:cords.height-(this.options.borderWidth*2),
			top:this.options.autostart[0]?(this.options.autostart[2]?-50:cords.top):cords.top,
			left:this.options.autostart[0]?(this.options.autostart[2]?window.getWidth()/2:cords.left):cords.left
		};
		return this.openClosePos;
	},
	open:function(el){
		this.index=this.from.indexOf(el);
		this.getOpenClosePos(this.from[this.index]);
		if(!this.opened){
			this.opened=true;
			if(this.options.overlay){
				this.showOverlay();
			}
			this.win.setStyles(Object.merge({display:'',opacity:0.6},this.openClosePos));
			this.win.addClass('jswin-loading');
			this.load(this.index);
		}
		else{
			this.hide(this.index);
		}
		return false;
	},
	load:function(index){
		var link=this.from[index];
		this.attributes={number:index+1};
		this.win.addClass('jswin-loading');
		
		if(link.rel){
			var array=link.rel.split('|');
			array.each(function(el){
				var arr=el.split(':');
				this.attributes[arr[0]]=arr[1];
			}.bind(this));
		}
		
		var extension = this.attributes.type != undefined ? this.attributes.type : link.href.split('?')[0].substr(link.href.lastIndexOf('.')+1).toLowerCase(),
			caption = this.attributes.caption != undefined ? this.attributes.caption : '';
		
		switch(extension){
			case'jpg':case'gif':case'png':this.type='image';break;
			default:this.type='html';break;
		}
		
		if(this.options.showCount){
			this.captionInner.set('html','<em>'+this.attributes.number+' of '+this.from.length+'</em>'+caption);
		}
		else{
			this.captionInner.set('html',caption);
		}
		
		if(this.type=='image'){
			this.object = new Image();
			this.object.onload = this.show.bind(this);
			this.object.src = link.href;
		}
		else{
			var html_height = this.attributes.height != undefined ? this.attributes.height.toInt() : this.options.htmlHeight,
			html_width = this.attributes.width != undefined ? this.attributes.width.toInt() : this.options.htmlWidth,
			url = this.attributes.url != undefined ? this.attributes.url : link.href;
			
			this.win.setStyle('border',0);
			
			this.object=new Element('div',{'class':'jswin-html',styles:{height:html_height}}).load(url);
			this.object.width=html_width;
			this.object.height=html_height;
			this.show();
		}
	},
	show:function(){
		var top=(window.getHeight()/2)-(((this.object.height.toInt()+24)/2))+window.getScrollTop(),left=(window.getWidth()/2)-(this.object.width/2);
		
		if(top<0){top=30;}
		if(left<0){left=0;}
		
		this.fxwin.cancel().start({width:this.object.width,height:this.object.height,top:top,left:left,opacity:1}).chain(function(){
			this.win.setStyle('height','').removeClass('jswin-hide').removeClass('jswin-loading');
			this.content.empty().adopt(this.object, this.prevLink, this.nextLink).fade('in');
			
			if(this.from.length>1){
				if(this.type != 'html'){
					$$(this.prevLink, this.nextLink).setStyle('height', this.object.height);
					this.attributes.number==1 ? this.prevLink.style.display = 'none' : this.prevLink.style.display = 'block';
					this.attributes.number==this.from.length ? this.nextLink.style.display = 'none' : this.nextLink.style.display = 'block';
					
					var ci = this.captionInner.getSize();
					if(ci.y != 5){
						this.fxcaption.cancel().start(ci.y+'px');
					}
				}
			}
		}.bind(this));
	},
	hide:function(index){
		this.fxcaption.cancel().start(0);
		this.fxcontent.cancel().start(0).chain(function(){
			this.load(index);
		}.bind(this));
	},
	next:function(){
		if(this.index<this.from.length-1){
			this.index++;
			this.getOpenClosePos(this.from[this.index]);
			this.hide(this.index);
		}
	},
	previous:function(){
		if(this.index>0){
			this.index--;
			this.getOpenClosePos(this.from[this.index]);
			this.hide(this.index);
		}
	},
	close:function(){
		this.fxcaption.start(0).chain(function(){
			this.win.addClass('jswin-hide').setStyle('height',this.object.height);
			this.content.empty();
			if(this.options.overlay){
				this.overlay.setStyles({display:'none',width:0,height:0});
			}
			this.fxwin.cancel().start({width:this.openClosePos.width,height:this.openClosePos.height,top:this.openClosePos.top,left:this.openClosePos.left,opacity:0.2}).chain(function(){
				this.opened=false;
				this.win.setStyle('display','none');
				this.openClosePos={};
			}.bind(this));
		}.bind(this));
	}
});

var imgtrail = {
	init:function(items){
		if($('imgtrail') == undefined){
			imgtrailObj=new Element('div',{'id':'imgtrailObj','styles':{'position':'absolute','z-index':'99999999','visibility':'hidden','left':'0','top':'-1000px','width':'1px','height':'1px','border':'1px solid #888888','background':'#dddddd'}}).inject(document.body,'top');
		}
		items.addEvents({
			'click':function(e){e.stop()},
			'mouseenter':function(){imgtrail.show(this)},
			'mouseleave':function(){imgtrail.hide()},
			'mousemove':function(event){imgtrail.position(event)}
		})
	},
	position:function(event){
		var size = window.getSize(), scroll = window.getScroll(), tip = {x:imgtrailObj.offsetWidth, y:imgtrailObj.offsetHeight}, props = {x:'left',y:'top'};
		for(var z in props){
			var pos = event.page[z] + 20;
			if((pos + tip[z] - scroll[z]) > size[z]){ pos = event.page[z] - 20 - tip[z]; }
			imgtrailObj.setStyle(props[z],pos);
		}
	},
	show:function(el){
		var splitstr = el.rel.split('|');
		imgtrailObj.setStyles({'visibility':'visible','width':splitstr[0]+'px','height':splitstr[1]+'px','background':'#dddddd url('+el.href+'#rand'+Math.floor(Math.random()*100)+') no-repeat center'});
	},
	hide:function(){
		imgtrailObj.setStyles({'visibility':'hidden','left':'-1000px','top':'0px','background':'#fff'});
	}
};

Element.implement({
	inputHint:function(val){
		switch(this.get('tag')){
			case'form':this.getElements('input[type="text"],textarea').inputHint(val);return this;
			case'input': case'textarea':
				this.store('default',(val || this.get('value')));
				this.addEvents({
					'focus':function(){
						if(this.get('value') == this.retrieve('default')){this.set('value','');}
					},
					'blur':function(){
						if(this.get('value').clean() == ''){ this.set('value',this.retrieve('default')); }
					}
				}).fireEvent('blur');
			default:return this;
		}
	},
	alert: function(msg, options){
		var alert = this.retrieve('alert');
		if(!alert){
			options = options || {'mode':'top'};
			alert = new Purr(options);
			this.store('alert', alert);
		}
		var coords = this.getCoordinates();
		alert.alert(msg, options);
		alert.wrapper.setStyles({'bottom': '','left': (coords.left - (alert.wrapper.getWidth() / 2)) + (this.getWidth() / 2),'top': coords.top - (alert.wrapper.getHeight()),'position': 'absolute'});
	}
});

