var order = {
	x_request_id: "",
	x_grommets: 0,
	x_hems: 0,
	x_pockets: 0,
	x_slits: 0,
	x_comments: "",
	x_default_comment: "",
	x_quality: "",
	x_ppsf: 0,
	x_total_price: 0,
	x_price_per_item: 0,
	x_width: 1,
	x_height: 1,
	x_sides: 1,
	x_artwork: 0,
	x_quantity: 1,
	width_up: function(){
		$( "#width .arrup" ).click( function(){
			if( order.x_width == 100 ){
				return;
			}
			++order.x_width;
			$( "#width input" ).attr( "value", order.x_width );
			order.calc_price();
		});
	},
	width_down: function(){
		$( "#width .arrdown" ).click( function(){
			if( order.x_width == 1 ){
				return;
			}
			--order.x_width;
			$( "#width input" ).attr( "value", order.x_width );
			order.calc_price();
		});
	},
	width_change: function(){
		$( "#width input" ).change( function(){
			var width = parseInt( $( "#width input" ).attr( "value" ) );
			if ( width > 100 ){
				width = 100;
			}
			if ( width < 1 || isNaN( width ) ){
				width = 1;
			}
			order.x_width = width;
			$( "#width input" ).attr( "value", order.x_width );
			order.calc_price();
			
		});
	},
	height_up: function(){
		$( "#height .arrup" ).click( function(){
			if( order.x_height == 6 ){
				return;
			}
			++order.x_height;
			$( "#height input" ).attr( "value", order.x_height );
			order.calc_price();
		});
	},
	height_down: function(){
		$( "#height .arrdown" ).click( function(){
			if( order.x_height == 1 ){
				return;
			}
			--order.x_height;
			$( "#height input" ).attr( "value", order.x_height );
			order.calc_price();
		});
	},
	height_change: function(){
		$( "#height input" ).change( function(){
			var height = parseInt( $( "#height input" ).attr( "value" ) );
			if ( height > 6 ){
				height = 6;
			}
			if ( height < 1 || isNaN( height ) ){
				height = 1;
			}
			order.x_height = height;
			$( "#height input" ).attr( "value", order.x_height );
			order.calc_price();
			
		});
	},
	sides_change: function(){
		$( "#step2 input" ).change( function(){
			order.x_sides = this.value;
			order.calc_price();
		});
	}, 
	quantity_up: function(){
		$( "#quantity .arrup" ).click( function(){
			++order.x_quantity;
			$( "#quantity input" ).attr( "value", order.x_quantity );
			order.calc_price();
		});
	},
	quantity_down: function(){
		$( "#quantity .arrdown" ).click( function(){
			if( order.x_quantity == 1 ){
				return;
			}
			--order.x_quantity;
			$( "#quantity input" ).attr( "value", order.x_quantity );
			order.calc_price();
		});
	},
	quantity_change: function(){
		$( "#quantity input" ).change( function(){
			var quantity = parseInt( $( "#quantity input" ).attr( "value" ) );
			if ( quantity < 1 || isNaN( quantity ) ){
				quantity = 1;
			}
			order.x_quantity = quantity;
			$( "#quantity input" ).attr( "value", order.x_quantity );
			order.calc_price();
			
		});
	},
	calc_price: function( price ){
		if ( !isNaN( price ) ){
			this.x_total_price = price;
			this.x_price_per_item = price / this.x_quantity;
		}
		else{
			this.x_total_price = this.x_width * this.x_height * this.x_sides * this.x_quantity * this.x_ppsf;
			this.x_price_per_item = this.x_total_price / this.x_quantity;
		}
		this.x_total_price = parseInt( this.x_total_price * 100 ) / 100;
		var price = this.x_total_price + "";
		if ( this.x_total_price == parseInt( this.x_total_price ) ){
			price = this.x_total_price + ".00";
		}
		else if ( this.x_total_price == parseInt( this.x_total_price * 10 ) / 10 ){
			price = this.x_total_price + "0";
		}
		$( ".price span" ).html( "$" + price );
	},
	setAttr: function(){
		//request_id is set manually
		//set grommets, hems, pockets and slits
		var checkboxes = $( ".content #step3 form input" );
		this.x_grommets = checkboxes[ 0 ].value;
		this.x_hems = checkboxes[ 1 ].value;
		this.x_pockets = checkboxes[ 2 ].value;
		this.x_slits = checkboxes[ 3 ].value;
		//set x_comments
		this.x_comments = $( ".content .fileUpload textarea" ).attr( "value" );
		/*if ( this.x_comments == this.x_default_comment ){
			this.x_comments = "";
		}*/
		
		//set x_request_id
		this.x_request_id = $( "#uuid input" ).attr( "value" );
		
		//set x_quality
		this.x_quality = $( "#thetype input" ).attr( "value" );
		
		//set width
		this.width_change();
		var width = parseInt( $( "#width input" ).attr( "value" ) );
		if ( width > 100 || width < 1 || isNaN( width ) ){
			width = 1;
		}
		this.x_width = width;
		$( "#width input" ).attr( "value", width );
		//set height
		this.height_change();
		var height = parseInt( $( "#height input" ).attr( "value" ) );
		if ( height > 6 || height < 1 || isNaN( height ) ){
			height = 1;
		}
		this.x_height = height;
		$( "#height input" ).attr( "value", height );
		//set quantity
		this.quantity_change();
		var quantity = parseInt( $( "#quantity input" ).attr( "value" ) );
		if ( parseInt( quantity ) != quantity ){
			quantity = 1;
		}
		this.x_quantity = quantity;
		$( "#quantity input" ).attr( "value", quantity );
		//set sides
		this.x_sides = parseInt( $( "#step2 input:checked" ).attr( "value" ) );
		//
	},
	check: function(){
		$( ".content #step2 form span" )
			.click( function(){
				$( this ).children( "input" ).checked = true;
				$( this ).children( "input" )
				.attr( "checked", "checked" )
				.trigger( "change" );
			});
		
		$( ".content #step13 form span" )
			.toggle( function(){
				$( this ).children( "input" ).checked = true;
				$( this ).children( "input" )
				.attr({
					"checked": "checked",
					"value": 1
					})
				.trigger( "change" );
			},
			function(){
				$( this ).children( "input" ).checked = false;
				$( this ).children( "input" )
				.attr({
					"checked": "",
					"value": 0
					})
				.trigger( "change" );
			});
	},
	next: function(){
		$( ".content .next .next_step" ).click( function(){
            if( !order.checkprice() ){
                return;
            }
            $( ".content > div:not( .next )" ).hide();
            $( this ).hide();
            $( ".content > div.fileUpload" ).show();
            $( ".content .next span:not( .next_step )" ).show();
            
        } );
	},
    checkprice: function(){
        if ( order.x_sides == 1 && order.x_price_per_item < 15 ){
            if ( !confirm( "Minimum cost per banner is $" + 15 * order.x_quantity + ". Do you want to proceed?" ) ){
            return false;
            }
            order.calc_price( 15 * order.x_quantity );
            return true;
        } else if (order.x_sides == 2 && order.x_price_per_item < 15) {
            if ( !confirm( "Minimum cost per double sided banner is $" + 30 * order.x_quantity + ". Do you want to proceed?" ) ){
            return false;
            }
            order.calc_price( 30 * order.x_quantity );
            return true;
		}
        return true;
    },
	back: function(){
		$( ".content .next .back" ).click( function(){
			$( ".content > div" ).show();
			$( ".content > div.fileUpload" ).hide();
			$( this ).hide();
			$( ".content > div .add_cart" ).hide();
			$( ".content .next .next_step" ).show();
			
		});
	},
	init: function(){
		$( ".content #step3 form input" ).checked = false;
		$( ".content #step3 form input" ).attr({
					"checked": "",
					"value": 0
					});
        this.x_ppsf = ppsf;
		this.setAttr();
		this.calc_price();
		this.width_up();
		this.width_down();
		this.width_change();
		this.height_up();
		this.height_down();
		this.height_change();
		this.sides_change();
		this.quantity_up();
		this.quantity_down();
		this.quantity_change();
		this.check();
		this.next();
		this.back();
	},
	send_form: function( bool ){
        if( bool === true ){
            if( !order.checkprice() ){
                return false;
            }
        }
		if (CheckMe()){
			
		var form = $("#theForm");
		var input = document.createElement("input");
		// - - - - - - - - - - - - - - - - x_request_id - - - - - - - - - - - - - - - -
		$( input ).clone().attr({
			"type": "hidden",
			"name": "x_request_id",
			"value": order.x_request_id
			}).appendTo( form );
		// - - - - - - - - - - - - - - - - x_option_type - - - - - - - - - - - - - - - -
		$( input ).clone().attr({
			"type": "hidden",
			"name": "x_option_type",
			"value": 1
			}).appendTo( form );
		// - - - - - - - - - - - - - - - - folder uuid - - - - - - - - - - - - - - - -
		$( input ).clone().attr({
			"type": "hidden",
			"name": "x_folder_uuid",
			"value": $('#x_folder_uuid').val()
			}).appendTo( form );
		// - - - - - - - - - - - - - - - - x_grommets - - - - - - - - - - - - - - - -
		$( input ).clone().attr({
			"type": "hidden",
			"name": "x_grommets",
			"value": document.getElementById('x_grommets').checked
			}).appendTo( form );
		// - - - - - - - - - - - - - - - - x_hems - - - - - - - - - - - - - - - -
		$( input ).clone().attr({
			"type": "hidden",
			"name": "x_hems",
			"value": document.getElementById('x_hems').checked
			}).appendTo( form );
		// - - - - - - - - - - - - - - - - x_pockets - - - - - - - - - - - - - - - -
		$( input ).clone().attr({
			"type": "hidden",
			"name": "x_pockets",
			"value": document.getElementById('x_pockets').checked
			}).appendTo( form );
		// - - - - - - - - - - - - - - - - x_pockets_orientation - - - - - - - - - - - - - - - -
		$( input ).clone().attr({
			"type": "hidden",
			"name": "x_pockets_orientation",
			"value": $("input[name='x_pockets_orientation']:checked").val()
			}).appendTo( form );
		// - - - - - - - - - - - - - - - - x_pole_width - - - - - - - - - - - - - - - -
		$( input ).clone().attr({
			"type": "hidden",
			"name": "x_pole_width",
			"value": $('#x_pole_width :selected').val()
			}).appendTo( form );
		// - - - - - - - - - - - - - - - - x_slits - - - - - - - - - - - - - - - -
		$( input ).clone().attr({
			"type": "hidden",
			"name": "x_slits",
			"value": document.getElementById('x_slits').checked
			}).appendTo( form );
		// - - - - - - - - - - - - - - - - x_comments - - - - - - - - - - - - - - - -
		$( input ).clone().attr({
			"type": "hidden",
			"name": "x_comments",
			"value": $( ".content .fileUpload textarea" ).attr( "value" )
			}).appendTo( form );
		// - - - - - - - - - - - - - - - - x_artwork - - - - - - - - - - - - - - - -
		$( input ).clone().attr({
			"type": "hidden",
			"name": "x_artwork",
			"value": document.getElementById('x_artwork').value
			}).appendTo( form );
		// - - - - - - - - - - - - - - - - x_quality - - - - - - - - - - - - - - - -
		$( input ).clone().attr({
			"type": "hidden",
			"name": "x_quality",
			"value": order.x_quality
			}).appendTo( form );
		// - - - - - - - - - - - - - - - - x_ppsf - - - - - - - - - - - - - - - -
		$( input ).clone().attr({
			"type": "hidden",
			"name": "x_ppsf",
			"value": order.x_ppsf
			}).appendTo( form );
		// - - - - - - - - - - - - - - - - x_total_price - - - - - - - - - - - - - - - -
		$( input ).clone().attr({
			"type": "hidden",
			"name": "x_total_price",
			"value": order.x_total_price
			}).appendTo( form );
		// - - - - - - - - - - - - - - - - x_width - - - - - - - - - - - - - - - -
		$( input ).clone().attr({
			"type": "hidden",
			"name": "x_width",
			"value": order.x_width
			}).appendTo( form );
		// - - - - - - - - - - - - - - - - x_height - - - - - - - - - - - - - - - -
		$( input ).clone().attr({
			"type": "hidden",
			"name": "x_height",
			"value": order.x_height
			}).appendTo( form );
		// - - - - - - - - - - - - - - - - x_sides - - - - - - - - - - - - - - - -
		$( input ).clone().attr({
			"type": "hidden",
			"name": "x_sides",
			"value": order.x_sides
			}).appendTo( form );
		// - - - - - - - - - - - - - - - - x_quantity - - - - - - - - - - - - - - - -
		$( input ).clone().attr({
			"type": "hidden",
			"name": "x_quantity",
			"value": order.x_quantity
			}).appendTo( form );
		
		$( form ).prependTo( "body" );
		
		document.forms[ 0 ].submit();
		}
	}
};
$( function(){
	order.init();
	$( ".content .fileUpload textarea" ).focus( function(){
		if ( this.value == order.x_default_comment ){
			this.value = "";
		}
	});
	$( ".content .fileUpload textarea" ).blur( function(){
		if ( this.value == "" ){
			this.value = order.x_default_comment;
		}
	});


	var pockets = $('input#x_pockets');
	pockets.parent().click(function(event){
		if (pockets.is(':checked')) {
			$('#show_pockets_orientation').show();
			$('#orderSummary div#sum_pockets').show();
			$('#ppocketspan').css('background-color','#FFFFCC');	
		} else {
			$('#ppocketspan').css('background-color','#FFFFFF');
			$('#orderSummary div#sum_pockets').hide();
			$('#show_pockets_orientation').hide();
		};
	});


	/*$( "#fileInput" ).fileUpload({
		"uploader": 		"./Scripts/uploader.swf",
		"script": 			"./upload.cfm",
		"folder":			$( "#uuid input" ).attr( "value" ) + "&type=" + $( "#thetype input" ).attr( "value" ),
		"cancelImg": 		"./images/delete.png",
		"scriptAccess":		"always",
		"multi":			true,
		"simUploadLimit":	3,
		"buttonText": "SELECT FILES",
		"onSelect":			function(){
			$( "#fileInputQueue" ).insertAfter( ".fileUpload #x_artwork" ).show();
		},
		"onComplete":		function( e, queueID ){
			$( "#fileInput" + queueID + " .cancel" )
			.find( "img" )
			.insertBefore( "#fileInput" + queueID + " .cancel a" )
			.attr( "src", "./images/yes.gif" )
			.parent()
			.children( "a" )
			.remove();
			$( ".content .next .add_cart" ).css({ 
				"backgroundPosition": "",
				"cursor": "pointer"
				});
			return false;
		}
	});
	$( "#uploadFiles" ).click( function(){
		$( "#fileInput" ).fileUploadStart();
	});
	$( "#clearQueue" ).click(function(){
		$( "#fileInput" ).fileUploadClearQueue();
	});*/
	$( ".content .next .add_cart" )
	.css( "backgroundPosition", "" )
	.click( function(){
        if( $( this ).attr( 'id' ) == 'monolith'){
			//alert('0');
            if($("#x_artwork").val() == 1 && $( 'input:hidden[id^=x_file_]' ).length || $("#x_artwork").val() > 1){
				//alert('1');
				order.send_form( true );
			} else
			
			{
				alert('You must upload your artwork OR indicate otherwise from the options available.');
				//order.send_form();
			}
        }
        else{
			alert('3');
            //order.send_form();
        }
    });
	$( "#x_artwork" ).change( function(){
		if ( this.value == 1 ){
			$( '#uploadForm' ).show();
			$( ".add_cart" ).css({ 
				"backgroundPosition": "0 -120px",
				"cursor": "default"
				});
			
		}
		else {
			$( '#uploadForm' ).hide();
			$( ".add_cart" ).css({"backgroundPosition": "", "cursor": "pointer" });
		}
	});
});


function CheckMe() {
	var OK=true;

		while (OK==true) {

			if ( $( "#x_artwork" ).value==0)
			{	OK=false;
				$( "#x_artwork_error" ).attr('style','display:block;');
				break;
			}
			break;
		}
	if ( $( ".add_cart" ).css( "cursor" ) == "default" ){
		return false;
	}
	if (OK==true) return true;
	return false;
}