/*
 * jQuery Easing Plugin
 *
 * website:			http://gsgd.co.uk/sandbox/jquery/easing/
*/
jQuery.easing['jswing']=jQuery.easing['swing'];jQuery.extend(jQuery.easing,{def:'easeOutQuad',swing:function(x,t,b,c,d){return jQuery.easing[jQuery.easing.def](x,t,b,c,d)},easeInQuad:function(x,t,b,c,d){return c*(t/=d)*t+b},easeOutQuad:function(x,t,b,c,d){return-c*(t/=d)*(t-2)+b},easeInOutQuad:function(x,t,b,c,d){if((t/=d/2)<1)return c/2*t*t+b;return-c/2*((--t)*(t-2)-1)+b},easeInCubic:function(x,t,b,c,d){return c*(t/=d)*t*t+b},easeOutCubic:function(x,t,b,c,d){return c*((t=t/d-1)*t*t+1)+b},easeInOutCubic:function(x,t,b,c,d){if((t/=d/2)<1)return c/2*t*t*t+b;return c/2*((t-=2)*t*t+2)+b},easeInQuart:function(x,t,b,c,d){return c*(t/=d)*t*t*t+b},easeOutQuart:function(x,t,b,c,d){return-c*((t=t/d-1)*t*t*t-1)+b},easeInOutQuart:function(x,t,b,c,d){if((t/=d/2)<1)return c/2*t*t*t*t+b;return-c/2*((t-=2)*t*t*t-2)+b},easeInQuint:function(x,t,b,c,d){return c*(t/=d)*t*t*t*t+b},easeOutQuint:function(x,t,b,c,d){return c*((t=t/d-1)*t*t*t*t+1)+b},easeInOutQuint:function(x,t,b,c,d){if((t/=d/2)<1)return c/2*t*t*t*t*t+b;return c/2*((t-=2)*t*t*t*t+2)+b},easeInSine:function(x,t,b,c,d){return-c*Math.cos(t/d*(Math.PI/2))+c+b},easeOutSine:function(x,t,b,c,d){return c*Math.sin(t/d*(Math.PI/2))+b},easeInOutSine:function(x,t,b,c,d){return-c/2*(Math.cos(Math.PI*t/d)-1)+b},easeInExpo:function(x,t,b,c,d){return(t==0)?b:c*Math.pow(2,10*(t/d-1))+b},easeOutExpo:function(x,t,b,c,d){return(t==d)?b+c:c*(-Math.pow(2,-10*t/d)+1)+b},easeInOutExpo:function(x,t,b,c,d){if(t==0)return b;if(t==d)return b+c;if((t/=d/2)<1)return c/2*Math.pow(2,10*(t-1))+b;return c/2*(-Math.pow(2,-10*--t)+2)+b},easeInCirc:function(x,t,b,c,d){return-c*(Math.sqrt(1-(t/=d)*t)-1)+b},easeOutCirc:function(x,t,b,c,d){return c*Math.sqrt(1-(t=t/d-1)*t)+b},easeInOutCirc:function(x,t,b,c,d){if((t/=d/2)<1)return-c/2*(Math.sqrt(1-t*t)-1)+b;return c/2*(Math.sqrt(1-(t-=2)*t)+1)+b},easeInElastic:function(x,t,b,c,d){var s=1.70158;var p=0;var a=c;if(t==0)return b;if((t/=d)==1)return b+c;if(!p)p=d*.3;if(a<Math.abs(c)){a=c;var s=p/4}else var s=p/(2*Math.PI)*Math.asin(c/a);return-(a*Math.pow(2,10*(t-=1))*Math.sin((t*d-s)*(2*Math.PI)/p))+b},easeOutElastic:function(x,t,b,c,d){var s=1.70158;var p=0;var a=c;if(t==0)return b;if((t/=d)==1)return b+c;if(!p)p=d*.3;if(a<Math.abs(c)){a=c;var s=p/4}else var s=p/(2*Math.PI)*Math.asin(c/a);return a*Math.pow(2,-10*t)*Math.sin((t*d-s)*(2*Math.PI)/p)+c+b},easeInOutElastic:function(x,t,b,c,d){var s=1.70158;var p=0;var a=c;if(t==0)return b;if((t/=d/2)==2)return b+c;if(!p)p=d*(.3*1.5);if(a<Math.abs(c)){a=c;var s=p/4}else var s=p/(2*Math.PI)*Math.asin(c/a);if(t<1)return-.5*(a*Math.pow(2,10*(t-=1))*Math.sin((t*d-s)*(2*Math.PI)/p))+b;return a*Math.pow(2,-10*(t-=1))*Math.sin((t*d-s)*(2*Math.PI)/p)*.5+c+b},easeInBack:function(x,t,b,c,d,s){if(s==undefined)s=1.70158;return c*(t/=d)*t*((s+1)*t-s)+b},easeOutBack:function(x,t,b,c,d,s){if(s==undefined)s=1.70158;return c*((t=t/d-1)*t*((s+1)*t+s)+1)+b},easeInOutBack:function(x,t,b,c,d,s){if(s==undefined)s=1.70158;if((t/=d/2)<1)return c/2*(t*t*(((s*=(1.525))+1)*t-s))+b;return c/2*((t-=2)*t*(((s*=(1.525))+1)*t+s)+2)+b},easeInBounce:function(x,t,b,c,d){return c-jQuery.easing.easeOutBounce(x,d-t,0,c,d)+b},easeOutBounce:function(x,t,b,c,d){if((t/=d)<(1/2.75)){return c*(7.5625*t*t)+b}else if(t<(2/2.75)){return c*(7.5625*(t-=(1.5/2.75))*t+.75)+b}else if(t<(2.5/2.75)){return c*(7.5625*(t-=(2.25/2.75))*t+.9375)+b}else{return c*(7.5625*(t-=(2.625/2.75))*t+.984375)+b}},easeInOutBounce:function(x,t,b,c,d){if(t<d/2)return jQuery.easing.easeInBounce(x,t*2,0,c,d)*.5+b;return jQuery.easing.easeOutBounce(x,t*2-d,0,c,d)*.5+c*.5+b}});

$(document).ready(function(){

	/* 
	 * JQUERY CSS3 EMULATOR 
	 * author: 	Chris Gonzales, Provenance Digital Media
	 * desc:	allow browsers to use css3 selectors that don't support it, jquery css3 not actual css3
	*/
	
		function jqueryCSS3(file){
			// css file that will be parsed, to make parse and cache css on server, 
			// remove all lines, spaces (except font names), comments and 
			// remove all styles without selectors listed below
			// and use just the parseAsCss3()
			var css_doc = file;
			//chars to check for that exist in css3, mush slashify all chars and separated by |
			var css_selectors = '\[|\\]|\=|\"|\~|\^|\$|\*|\||\:|\(|\)|\>|\+';
			//exceptions to css that already work in most browsers
			var css_exceptions = ':hover|:active|:link|:visited';
			
			//ajax call to load and parse css
			$.ajax({
				url		: css_doc,
				cache	: true,
				success	: parseCleanCss
			});
			
			//clean css then parse it
			function parseCleanCss(data){			
				var css_str = cleanCss(data);
				var css_ary = parseCss(css_str);			
				applyCss(css_ary);
			}
			
			//apply all css to document
			function applyCss(ary){
				//kinda a hack to get ie6 to update display
				$('#container').attr('style', '');
				for(style in ary){
					//alert(ary[style][0] + " --- " + ary[style][1]);
					$(ary[style][0]).attr('style', ary[style][1]);
				}
			}
			
			//parse css function
			function parseCss(str, checklines){			
				//if true will skip checking and just push styles into array
				var checklines = checklines ? true : false;
				
				//array that will hold all styles to be applied
				var css_ary = []
				
				tmp_ary = str.split('}');
				
				for(line in tmp_ary){
					var css_line = tmp_ary[line].split('{');
					var selector = css_line[0];
					
					if(checklines == false){
						if( selector.search(RegExp('[' + css_selectors + ']+', 'g')) > -1 && selector.search(RegExp('(' + css_exceptions + ')', 'g')) == -1){
							css_ary.push( css_line );
						}
					}else{
						css_ary.push( css_line );
					}
				}
				
				//$('#debug').text(css_ary.join("\r\n"));		
				return css_ary;
			}
			
			//clean css function
			function cleanCss(str){
				var css_str = str;
				
				//remove comments
				css_str = css_str.replace(/\/\*[\s\S]+?\*\//g, '');
				
				//remove newlines
				css_str = css_str.replace(/[\n]*/g, '');		
				
				//remove duplicate spaces
				css_str = css_str.replace(/[\s]+/g, ' ');
		
				return css_str;
			}
		}
	
		//initialize it
		//jqueryCSS3('/css/default.css');
	
	/* END OF EMULATOR */
	
	/* ----------------------------------------- */
	
	/*
	 * TEXTAREA AUTO GROW
	*/
		
		$('textarea').each(function() {		
			var $this       = $(this),
				minHeight   = $this.height(),
				lineHeight  = $this.css('lineHeight');
			
			var shadow = $('<div></div>').css({
				position:   'absolute',
				top:        -10000,
				left:       -10000,
				width:      $(this).width(),
				fontSize:   $this.css('fontSize'),
				fontFamily: $this.css('fontFamily'),
				lineHeight: $this.css('lineHeight'),
				resize:     'none'
			}).appendTo(document.body);
			shadow.hide();
			
			var update = function() {
				
				var val = this.value.replace(/</g, '&lt;')
									.replace(/>/g, '&gt;')
									.replace(/&/g, '&amp;')
									.replace(/\n/g, '<br/>');
				
				shadow.html(val);
				shadow.hide();
				$(this).css('height', Math.max(shadow.height() + 20, minHeight));
				$(this).css({overflow: 'hidden'});
			}
			
			$(this).change(update).keyup(update).keydown(update);
			
			update.apply(this);		
		});
		
	/* END OF AUTOGROW */
	
	/* ----------------------------------------- */
	
	/*
	 * SIMPLE FORM VALIDATION
	*/	
	
		$('form.valid').submit(function(){
						
			var msg = '';
			
			$(this).find('label.required').each(function(){
				$input = $('#' + $(this).attr('for')).eq(0);
				
				if($input.hasClass('default')){					
					if($input.val() == $input[0].defaultValue) $input.val('');
				}
				
				if($input.val() == ''){
					msg+= $(this).text().replace('*', '') + " is required\n";
				}else if($input.attr('name') == 'email'){
					if($input.val().search(/([^\/.]+)@([^\/.]+)\.([^\/.]+)/) == -1){
						if($input.attr('title')){
							msg+= $input.attr('title') + 'is invalid';	
						}else{
							msg+= $(this).text() + " is invalid";
						}
					}
				}
			});
			
			// register form only
			if($(this).attr('id') == 'register'){
				r = false;
				$(this).find('input[name^=num]').each(function(){
					if($(this).val() != '') r = true;
				});
				if(!r){
					alert("One of the following must be entered:\nASI#, PPAI#, UPIC#, SAGE#, Other Industry#, State Resale#");
					return false;
				}
			}
			
			if(msg){
				alert(msg);
				return false;	
			}
			
			return true;
		});
	
	/* END OF VALIDATION */
	
	/* ----------------------------------------- */
	
	/*
	 * CLEAR DEFAULT FROM FORM FIELD
	*/
		
		default_color = "#bbb";
		default_focus_color = "#000";
		
		$('.default').each(function(){
			$(this).css('color', default_color);
			var _default = $(this)[0].defaultValue;
			$(this).focus(function(){
				$(this).css({'color' : default_focus_color});
				if($(this).val() == _default){
					$(this).val('');	
				}
			}).blur(function(){
				if($(this).val() == ''){
					$(this).val(_default);	
					$(this).css({'color' : default_color});
				}
			});						
		});
		
	/* END OF DEFAULT */
	
	/* ----------------------------------------- */
	
	/*
	 * SIMPLE MODAL
	*/
		function centerModal(ele, animate){		
		
			var offset = $(window).scrollTop();
			var top = ($(window).height() / 2 - ele.height() / 2) + offset;
			var left = ($(window).width() / 2 - ele.width() / 2);
						
			if(animate){
				ele.animate({top: top, left: left/*, opacity: 1*/}, {queue: false, duration: 300, easing: 'easeOutQuad'});			
			}else{
				ele.css({'top': top, 'left': left});	
			}
		}
	
		function closeModal(){
			$('#modal').animate({opacity: 0}, {duration: 200, queue: false, easing: 'easeInQuad', complete: function(){
				$(this).remove();
				$('#modal_overlay').animate({opacity: 0}, {duration: 200, queue: false, complete: function(){
					$(this).remove();
				}});
			}});
		}
	
		function displayModal(data){			
			var overlay = $('<div id="modal_overlay"></div>')
			.css({
				position	: 'absolute',
				top			: 0,
				left		: 0,
				zIndex		: 1000,
				width		: '100%',//$(window).width(),
				height		: $(document).height(),
				background	: '#000',
				opacity		: 0
			})
			.appendTo($(document.body))
			.animate({opacity: .3}, {duration: 200, queue: false})
			.click(closeModal);
			
			
			var html 	= $('<div></div>').html(data).css({width: 600, background: '#fff', padding: '10px 0 0'});
			var html_bg = $('<div></div>').css({background: '#000', margin: -5, opacity:.3});
			
			var modal = $('<div id="modal"></div>')			
			.css({position: 'absolute', width: 600, top: 0, left: $(window).width() / 2 - 300, zIndex: 9999})
			.append(html_bg)
			.append(html)
			.appendTo($(document.body));	
			
			var height 	= modal.height();
			var width	= modal.width();
			
			html_bg.css({position: 'absolute'});
			html.css({position: 'absolute'});
			
			html_bg.css({width: width+10, height: height+15});
			
			modal.css({height: html_bg.height()-10});
			
			modal.css({top: ($(window).height() / 2 - modal.height() / 2) - 50 + $(window).scrollTop()});//, opacity: 0});
			
			var close_btn = $('<div><a href="#close">close</a></div>')
			.css({
				position	: 'absolute',	 
				right		: 0,
				top			: 0,
				padding		: '3px 5px',
				background	: '#1B88B1',
				fontSize	: '11px',
				color		: '#fff',
				textAlign	: 'right',
				width		: modal.width() - 10
			})
			.appendTo(html);
			
			close_btn.find('a')
			.css({
				color: '#fff',
				textDecoration: 'none'
			 })
			.click(function(){
				closeModal();					   
			});
			
			setTimeout(function(){centerModal(modal, true);}, 210);
		}
	
		
		$('.modal').click(function(){
			var url = $(this).attr('href') + '/js';
			var type = 'html';
								   
			$.get(url, null, displayModal, type);
			
			return false;
		});
		
		$(window).resize(function(){
			if($('#modal_overlay').length > 0){
				centerModal($('#modal'), true);	
			}
		});
		$(window).scroll(function(){
			if($('#modal_overlay').length > 0 && $('#modal').height() < $(window).height()){
				centerModal($('#modal'), true);	
			}
		});
	
	/* END OF MODAL */
	
	/* ----------------------------------------- */
	
	/*
	 * TEMPLATE PREVIEW CLICK 
	*/
	
		$('#template-preview>div>a').live('click', function(){
			$('.image-preview').css({opacity: 0, display: 'block'}).animate({opacity: 1}, {duration: 400});
			var img = new Image();
			$(img).attr('alt', 'click image to close');
			$(img).attr('title', 'click image to close');
			img.src = $(this).attr('href');
			$(img).load(function(){
				$(this).css({opacity: 0});
				$('.image-preview').append($(this));
				$(this).animate({opacity: 1},{duration: 500});
				$(this).click(function(){
					$('.image-preview').animate({opacity: 0}, {duration: 400, queue: false, complete: function(){
						$(img).remove(); 
						$(this).css({display: 'none'});
					}});
				});
			});
			return false;
		});
	
	/* END OF PREVIEW */
	
	/* ----------------------------------------- */
	
	/*
	 * SHIPPING SAME AS BILLING
	*/
	
	$('#order input[name=same_as_billing]').parent().css({display: 'block'}).end().click(function(){
		if($(this)[0].checked){
		   $('input[name=ship_company]').val($('input[name=bill_company]').val());
		   $('input[name=ship_address]').val($('input[name=bill_address]').val());
		   $('input[name=ship_address2]').val($('input[name=bill_address2]').val());
		   $('input[name=ship_city]').val($('input[name=bill_city]').val());
		   $('select[name=ship_state] option').eq($('select[name=bill_state]')[0].selectedIndex).attr('selected', 'selected');
		   $('input[name=ship_zip]').val($('input[name=bill_zip]').val());
		}
	});
	
	/* END OF SHIPPING */
	
	/* ----------------------------------------- */
	
	/*
	 * VIEW ORDER DETAILS
	*/
	
		$('#orders-view .view a').eq(0).toggle(
			function(){
				$(this).parent().parent().next().css({opacity: 0, display: 'table-row'}).animate({opacity: 1}, {duration: 300, queue: false});			
				return false;
			},
			function(){
				$(this).parent().parent().next().animate({opacity: 0}, {duration: 300, queue: false, complete: function(){$(this).css({display: 'none'})}});
				return false;	
			}
		);
	
	/* END OF VIEW ORDER */
	
	/* ----------------------------------------- */
	
	/*
	 * DISPLAY RUNNING TOTAL
	*/
	
		function displayEntry(data, details)
		{
			var entry = $('.items .entry' + details.idx);
			if(entry.length <= 0){
				entry = $('<li></li>');
				entry.addClass('entry' + details.idx);
				$('.items').append(entry);
			}
			var entry_total = data[0].price * details.quantity.val() + details.corners;
			var title = details.title.val() ? details.title.val() : 'Item ' + (details.idx+1);
			entry.html('<span>' + title + '<br/> <span>[' + details.dimension.find('option:selected').text() + '] x ' 
				+ details.quantity.val() + (details.corners > 0 ? ' - rounded corners' : '') + '<br/><em>' 
				+ entryFileName(details.file) + '</em></span></span> <em title="' + entry_total.toFixed(2) + '">$' + entry_total.toFixed(2) + '</em>');
		}
		
		function entryFileName(input)
		{
			var filename = input.val().split('\\');
			return filename.pop();
		}
		
		function displayTotal()
		{
			var sub_total = 0;
			// var sales_tax = 0; // Removed at request of client 06-11-10
			var shipping = 0;
			var total = 0;
			var items = $('.items li');
			
			if(items.length > 0){
				items.each(function(){
					sub_total+= parseFloat($(this).find('>em').attr('title'));
				});
				
				$('.sub-total em').text('$' + (sub_total * 1).toFixed(2));
				
				// Removed at request of client 06-11-10
				/* if($('#order select[name=bill_state]').val() == 'AL'){
					$('.sales-tax').show();	
					sales_tax = sub_total * .08;
					$('.sales-tax em').text('$' + sales_tax.toFixed(2));
				}*/
				
				shipping = parseFloat($('input[name=ship_price]').val());
			}
			// Removed at request of client 06-11-10
			// total = sub_total + sales_tax + shipping;
			total = sub_total + shipping;
			
			var num = total.toFixed(2);
			if(num == 'NaN'){
				num = 'calculating';
			}else{
				num = '$' + num;
			}
			
			$('.total .total em').text(num);
		}
	
		$('#order input[name=ship_address], #order input[name=ship_city], #order select[name=ship_state], #order input[name=ship_zip], #order select[name^=file_dimension], #order input[name^=file_quantity], #order input[name^=file_title], #order input[name^=userfile], #order input[name^=rounded_corners]')
		.change(function(){
						
			ship_fields = ['ship_address', 'ship_city', 'ship_state', 'ship_zip'];
			
			if($.inArray($(this).attr('name'), ship_fields) != -1){
				var field_name = $(this).attr('name');
				console.log(field_name, field_name.substr(5));
				if($(this).val() != $('#order input[name=bill_' + field_name.substr(5) + ']').val()){
					$('#order input[name=same_as_billing]').attr('checked', false);
				}
			}
						 
			
			if($('#order #shipping').val()){
				$('#order #shipping').trigger('change');	
			}else{							 
				var submit_btn = $('#order.update input[title=update]');			
				submit_btn.css({opacity: .25}).attr('disabled', 'disabled');
			}
			
			var dimensions 	= $('#order select[name^=file_dimension]');
			var quantities 	= $('#order input[name^=file_quantity]');
			var titles		= $('#order input[name^=file_title]');
			var files		= $('#order input[name^=userfile]');
			
			for(i=0; i<$('#order input[name^=file_title]').length; i++){
				var details = {idx: i, dimension: dimensions.eq(i), quantity: quantities.eq(i), title: titles.eq(i), file: files.eq(i), corners: 0};
				//check if quantiy and dimension are set
				if(dimensions.eq(i).val() && quantities.eq(i).val()){
					//$.getJSON('/pricing/calc/' + dimensions.eq(i).val() + '/' +  quantities.eq(i).val(), null, function(data){ console.log(i);displayEntry(data, i);});
										
					details.corners  = $('#order input[name^=rounded_corners]:checked').val() == 1 ? quantities.eq(i).val() * 0.03 : 0;					
					
					$.ajax({
						type	: 'GET',
						url		: '/pricing/getprice/' + quantities.eq(i).val(),
						data	: null,
						inc		: i,
						dataType: 'json',
						success	: function(json){
							continueCalc(json.id, this.inc);	
						}
					});
					
					function continueCalc(qty_id, i){
						$.ajax({
							type	: 'GET',
							url		: '/pricing/calc/' + dimensions.eq(i).val() + '/' +  qty_id,
							data	: null,
							details : details,
							dataType: 'json',
							success : function(json){
								displayEntry(json, this.details);			
								displayTotal();
								if(submit_btn) submit_btn.css({opacity: 1}).removeAttr('disabled');
							}
						});
					
					}
				}
			}
		});
	
	
	/* END OF TOTAL */	
	
	/* ----------------------------------------- */
	
	/*
	 * CALCULATE SHIPPING
	 */
		$('#order #shipping').change(function(){
											  
			var submit_btn = $('#order.update input[title=update]');			
			submit_btn.css({opacity: .25}).attr('disabled', 'disabled');
			
			var req_ary = ['ship_address', 'ship_city', 'ship_state', 'ship_zip', 'file_dimension', 'file_quantity'];
			
			for(field in req_ary){
				if(!$('[input,select][name^=' + req_ary[field] + ']').val()){
					return false;
					break;
				}
			}
			
			items = $('input[name^=file_title]');
			
			packages = '';
			
			for(i=0; i<items.length; i++){
				if($('select[name^=file_dimension]').eq(i).val() && $('input[name^=file_quantity]').eq(i).val()){
					packages += $('select[name^=file_dimension]').eq(i).val() + ',' + $('input[name^=file_quantity]').eq(i).val() + '|';
				}
			}
			
			$('.shipping em').text('calculating');
					
			$.ajax({
				type	: 'POST',
				url		: '/shipping',
				cache	: false,
				dataType: 'json',
				data	: ({
						   		packages	: packages, 
								shippingmethod		: $(this).val(),
						   		company		: $('input[name=company]').val(),
								name		: $('input[name=first_name]').val(),
								phone		: $('input[name=phone]').val(),
								address		: $('input[name=ship_address]').val(),
								address2	: $('input[name=ship_address2]').val(),
								city		: $('input[name=ship_city]').val(),
								state		: $('select[name=ship_state]').val(),
								zip			: $('input[name=ship_zip]').val(),
								action		: 'get-rate'
						   }),
				success: function(data){
					if(data == false){
						$('.shipping em').text('shipping option unavailble, reselect');
						$('input[name=ship_price]').val(0);
					}else{
						$('.shipping em').text("$" + data.RatedShipment.TotalCharges.MonetaryValue);
						$('input[name=ship_price]').val(data.RatedShipment.TotalCharges.MonetaryValue);
						$('input[name=ship_method]').val($('#order #shipping').val());
						displayTotal();
					}							
					submit_btn.css({opacity: 1}).removeAttr('disabled');
				}
			})
		});
	
	/* END OF SHIPPING */
	
	/* ----------------------------------------- */
	
	/*
	 * MISC
	 */
	
		$('#orders-view .process').each(function(){
			$(this).find('li').each(function(){
				if($(this).hasClass('active')) return false;
				$(this).addClass('past').css({color: 'rgba(255, 255, 255, .5)'});
			});			
		});
		
		$('.calctotal').each(function(){
			$('#order select[name^=file_dimension]').trigger('change');
		});
	
	/* END OF MISC */
	
	/* ----------------------------------------- */
	
	/*
	 * ORDER FORM VALIDATION
	 */

		$('.validate').submit(function(){
			
			var r = true;
			var msg = "";
			
			//special check for shipping
			
			if($(this).find('input[name=ship_price]').val() == 0){
				
				alert("Please verify all required fields* are complete and accurate.");
				
				return false;
			}
			
			if(parseInt($(this).find('input[name^=file_quantity]').val()) < 100){
				alert("Quanity must be more than 100");
				
				return false;
			}
			
			//end
			
			// check for inhand date and shipping
			
			var date = $('#order input[name=in_hand_date]').val().split('/');		
			if(date[2].length == 2) date[2] = '20' + date[2];		
			var today 	= new Date();
			var inhand 	= new Date(date[2], date[0]-1, date[1]);		
			var weekday = today.getDay();
			var offset 	= weekday > 2 ? 5 : 3;		
			var today_3days = new Date(today.getFullYear(), today.getMonth(), today.getDate() + offset);		
			if(today_3days >= inhand && $('#order select[name=shipping]').val() != '01'){
				alert('You must select Next Day Air shipping to meet the in-hand date.');	
				r = false;
			}
			
			$(this).find('.required').each(
				function(){
					
					if($(this).val() == ""){// || $(this).val() == $(this)[0].defaultValue){
						msg += $(this).attr('title') + " is required\n";
						r = false;	
					}
					if($(this).attr('name') == 'userfile[]' || $(this).attr('name') == 'userfile'){
						var approved_ext = ['zip', 'psd', 'pdf', 'ai', 'eps', 'sit', 'rar', 'jpg', 'gif', 'bmp', 'png'];
						var filename = $(this).val();
						var pieces = filename.split('.');
						var ext = pieces.reverse()[0];
						if(approved_ext.indexOf(ext) == -1 && ext != ""){
							msg += '.' + ext + " is an invalid file format\n";
							r = false;
						}
					}
				}
			);
		
			
			if(msg != "") alert(msg);
			
			return r;
		});
		
		$('input[name="userfile[]"]').change(function(){
			file = $(this).val();
			ext = file.split('.').pop();
			if(ext == 'ai'){
				alert('.ai file not supported please convert to .eps');		
				$(this).val('');	
			}
		});
	
	/* ORDER FORM VALIDATION END */
	
	/* ----------------------------------------- */
	
	/*
	 * ORDER FORM IN-HAND DATE
	 */
	$('#order input[name=in_hand_date]').change(function(){
		
		var date = $(this).val().split('/');
		
		if(date[2].length == 2) date[2] = '20' + date[2];
		
		var today 	= new Date();
		var inhand 	= new Date(date[2], date[0]-1, date[1]);	
		
		var weekday = today.getDay();
		var offset 	= weekday > 2 ? 5 : 3;
		
		var today_3days = new Date(today.getFullYear(), today.getMonth(), today.getDate() + offset);
		
		if(today_3days >= inhand){
			alert('You must select Next Day Air shipping to meet the in-hand date.');	
		}
		
	});
	 
	/* ORDER FORM IN-HAND DATE END */
	
	/* ----------------------------------------- */
	
});