function startSteps()
{
	myCat = $('cat').get('value');
	var myStep = 0;
	var myVal = myCat;
	if($('javascript'))
	{
		$('javascript').dispose();
	}
	if($$('.step').length > 0)
	{
	 var lastStep = $$('.step').getLast();
	 myStep = lastStep.get('id').substring(4);
	 myVal = lastStep.getElement('select, input').get('value');
  }
	var jsonRequest = new Request.JSON({url: "/products_ajax.php", onSuccess: function(result){
		if(!result.error)
		{
			if($('step'+result.step))
			{
				var nextStep = $('step'+result.step);
			}
			else
			{
				var nextStep = new Element('div', {'id':'step'+result.step, 'class':'step'});
				nextStep.inject($('steps'));
			}
			nextStep.empty();
			if(result.template || result.templateJpeg)
			{
			  var tempSpan = new Element('span', {'html':'<b>Download template: </b>'});
			  tempSpan.inject(nextStep);
			  if(result.template)
			  {
				var tempLink = new Element('a', {'href':'/template.php?id='+result.template, 'html':'<span>EPS Format</span>'});
				tempLink.inject(nextStep);
				}
				var tempSpan2 = new Element('span', {'text':' | '});
				if(result.template && result.templateJpeg)
				{
				  tempSpan2.inject(nextStep);
        }
				if(result.templateJpeg)
			  {
				var tempLink = new Element('a', {'href':'/template.php?type=jpeg&id='+result.templateJpeg, 'html':'<span>JPEG Format</span>'});
				tempLink.inject(nextStep);
				}
			}
			if(result.values)
			{
				var nextSel = new Element('select', {'name':result.name});
				var nextOpt = new Element('option', {'value':'0', 'text':result.blank});
				nextOpt.inject(nextSel);
				result.values.each(function(myOpt){
					(new Element('option', {'value':myOpt.id, 'text':myOpt.value})).inject(nextSel);
				});
				nextSel.inject(nextStep);
				makeSelectable(nextSel);
			}
			else if(result.name == 'file')
			{
				var nextText = new Element('b', {'text':'File Upload:'});
				nextText.inject(nextStep);
				var nextFile = new Element('input', {'name':result.name, 'type':'file'});
				nextFile.inject(nextStep);
				makeSelectable(nextFile);
			}
			else if(result.name == 'fileSkip')
			{
				var nextText = new Element('b', {'text':'File Upload (2nd File):'});
				nextText.inject(nextStep);
				var nextFile = new Element('input', {'name':result.name, 'type':'file', 'class':'file'});
				nextFile.inject(nextStep);
				makeSelectable(nextFile);
				var nextSkip = new Element('input', {'name':result.name, 'type':'button', 'class':'button', 'value':'Skip'});
				nextSkip.inject(nextStep);
				nextSkip.addEvent('click', function(){
				  this.fireEvent('change');
        });
				makeSelectable(nextSkip);
			}
			else if(result.name == 'finish')
			{
				var nextSub = new Element('input', {'name':'submit', 'type':'submit', 'value':'Add to Cart'});
				nextSub.inject(nextStep);
			}
		}
		else
		{
			makeError(result.error);
		}
	}}).get({'step': myStep, 'value': myVal});
}

function makeSelectable(obj)
{
	obj.addEvent('change', function(){
	this.blur();
		myStep = obj.getParent('.step');
		myStep.getAllNext('.step').each(function(nextStep){
			nextStep.dispose();
		});
		myStepId = myStep.get('id').substr(4);
		var jsonRequest = new Request.JSON({url: "/products_ajax.php", onSuccess: function(result){
			if(!result.error)
			{
				if($('step'+result.step))
				{
					var nextStep = $('step'+result.step);
				}
				else
				{
					var nextStep = new Element('div', {'id':'step'+result.step, 'class':'step'});
					nextStep.inject(myStep, 'after');
				}
				nextStep.empty();
				if(result.template || result.templateJpeg)
  			{
  			  var tempSpan = new Element('span', {'html':'<b>Download template: </b>'});
  			  tempSpan.inject(nextStep);
  			  if(result.template)
  			  {
  				var tempLink = new Element('a', {'href':'/template.php?id='+result.template, 'html':'<span>EPS Format</span>'});
  				tempLink.inject(nextStep);
  				}
  				var tempSpan2 = new Element('span', {'text':' | '});
  				if(result.template && result.templateJpeg)
  				{
  				  tempSpan2.inject(nextStep);
          }
  				if(result.templateJpeg)
  			  {
  				var tempLink = new Element('a', {'href':'/template.php?type=jpeg&id='+result.templateJpeg, 'html':'<span>JPEG Format</span>'});
  				tempLink.inject(nextStep);
  				}
  			}
				if(result.values)
				{
					var nextSel = new Element('select', {'name':result.name});
					var nextOpt = new Element('option', {'value':'0', 'text':result.blank});
					nextOpt.inject(nextSel);
					result.values.each(function(myOpt){
						(new Element('option', {'value':myOpt.id, 'text':myOpt.value})).inject(nextSel);
					});
					nextSel.inject(nextStep);
					makeSelectable(nextSel);
				}
				else if(result.name == 'file')
				{
					var nextText = new Element('b', {'text':'File Upload:'});
					nextText.inject(nextStep);
					var nextFile = new Element('input', {'name':result.name, 'type':'file'});
					nextFile.inject(nextStep);
					makeSelectable(nextFile);
				}
				else if(result.name == 'fileSkip')
  			{
  				var nextText = new Element('b', {'text':'File Upload (2nd File):'});
  				nextText.inject(nextStep);
  				var nextFile = new Element('input', {'name':result.name, 'type':'file', 'class':'file'});
  				nextFile.inject(nextStep);
  				makeSelectable(nextFile);
  				var nextSkip = new Element('input', {'name':result.name, 'type':'button', 'class':'button', 'value':'Skip'});
  				nextSkip.inject(nextStep);
  				nextSkip.addEvent('click', function(){
  				  this.fireEvent('change');
          });
  				makeSelectable(nextSkip);
  			}
				else if(result.name == 'finish')
				{
					var nextSub = new Element('input', {'name':'submit', 'type':'submit', 'value':'Add to Cart'});
					nextSub.inject(nextStep);
				}
			}
			else
			{
				makeError(result.error);
			}
		}}).get({'step': myStepId, 'value': obj.get('value')});
	});
}

window.addEvent('domready', function(){
	
	if($('shipZip'))
	{
	 $('shipZip').addEvent('blur', function(){
	   var jsonRequest = new Request.JSON({url: "/shipping_ajax.php", onSuccess: function(result){
	     if(result)
	     {
	        if($('cart').getElement('.subtotal .price'))
  				{
  					$('cart').getElement('.subtotal .price').set('text', '$'+result.subtotal);
  				}
  				if($('cart').getElement('.discount .price'))
  				{
  				  $('cart').getElement('.discount .price').set('text', '$'+result.discount);
  				}
  				if($('cart').getElement('#shipping .price'))
  				{
  				 $('cart').getElement('#shipping .price').set('text', '$'+result.shipping);
          }
  				$('cart').getElement('.total .price').set('text', '$'+result.price);
       }
	   }});
   });
  }
	if($('cart'))
	{
		var myCart = $('cart');
		var myItems = myCart.getElements('.item');
		myItems.each(function(item, i){
			item.set('id', 'item'+i);
			var myInfo = item.getElement('.info');
			var myBr = new Element('br');
			var myRem = new Element('a', {'class':'remove', 'text':'Remove item', 'href':'#'});
			myBr.inject(myInfo);
			myRem.inject(myInfo);
			myRem.addEvent('click', function(E){
				E.stop();
				var jsonRequest = new Request.JSON({url: "/remove_ajax.php", onSuccess: function(result){
					if(!result.error)
					{
						var myId = 'item'+result.success;
						if($(myId))
						{
							var myItem = $(myId);
							var myFx = new Fx.Morph(myItem, {'duration':500, transition: Fx.Transitions.Quad.easeIn});
							myFx.start({
								'opacity':0
							}).chain(
								function(){
									myFx.start({
										'margin-bottom':0,
										'height':0
									}).chain(
										function(){
											myItem.dispose();
											if($('cart').getElement('.subtotal .price'))
              				{
              					$('cart').getElement('.subtotal .price').set('text', '$'+result.subtotal);
              				}
              				if($('cart').getElement('.discount .price'))
              				{
              				  $('cart').getElement('.discount .price').set('text', '$'+result.discount);
              				}
											if($('cart').getElement('#shipping .price'))
											{
											 $('cart').getElement('#shipping .price').set('text', '$'+result.shipping);
                      }
											$('cart').getElement('.total .price').set('text', '$'+result.price);
											if($$('#cart .items').length == 0)
											{
											 $(document.body).getElement('form').dispose();
											 var newText = new Element('div', {'class':'error', 'text':'There are no items in your cart!'});
											 newText.inject($('middle'));
                      }
										}
									);
								}
							);
						}
					}
					else
					{
						makeError(result.error);
					}
				}}).get({'item':this.getParent('.item').id.substr(4), 'zip':$('shipZip').get('value')});
			});
		});
	}
	if($('cat'))
	{
	 startSteps();
	}
	
	$$('.step').each(function(myStep){
		var selectables = myStep.getElements('select');
		selectables.combine(myStep.getElements('input'));
		selectables.each(function(myField){
			makeSelectable(myField);
		});
	});
	
});