$(document).ready(function() {
	
	// Website color pickers.
	$('TABLE.quote-request.step-two.website .colorSelector.one').ColorPicker({ color: '#0000ff', onShow: function (colpkr) { $(colpkr).fadeIn(500); return false; }, onHide: function (colpkr) { $(colpkr).fadeOut(500); return false; }, onChange: function (hsb, hex, rgb) { $('TABLE.quote-request.step-two.website .colorSelector.one div').css('backgroundColor', '#' + hex); } });
	$('TABLE.quote-request.step-two.website .colorSelector.two').ColorPicker({ color: '#0000ff', onShow: function (colpkr) { $(colpkr).fadeIn(500); return false; }, onHide: function (colpkr) { $(colpkr).fadeOut(500); return false; }, onChange: function (hsb, hex, rgb) { $('TABLE.quote-request.step-two.website .colorSelector.two div').css('backgroundColor', '#' + hex); } });
	$('TABLE.quote-request.step-two.website .colorSelector.three').ColorPicker({ color: '#0000ff', onShow: function (colpkr) { $(colpkr).fadeIn(500); return false; }, onHide: function (colpkr) { $(colpkr).fadeOut(500); return false; }, onChange: function (hsb, hex, rgb) { $('TABLE.quote-request.step-two.website .colorSelector.three div').css('backgroundColor', '#' + hex); } });
	// Logo color pickers.
	$('TABLE.quote-request.step-two.logo .colorSelector.one').ColorPicker({ color: '#0000ff', onShow: function (colpkr) { $(colpkr).fadeIn(500); return false; }, onHide: function (colpkr) { $(colpkr).fadeOut(500); return false; }, onChange: function (hsb, hex, rgb) { $('TABLE.quote-request.step-two.logo .colorSelector.one div').css('backgroundColor', '#' + hex); } });
	$('TABLE.quote-request.step-two.logo .colorSelector.two').ColorPicker({ color: '#0000ff', onShow: function (colpkr) { $(colpkr).fadeIn(500); return false; }, onHide: function (colpkr) { $(colpkr).fadeOut(500); return false; }, onChange: function (hsb, hex, rgb) { $('TABLE.quote-request.step-two.logo .colorSelector.two div').css('backgroundColor', '#' + hex); } });
	$('TABLE.quote-request.step-two.logo .colorSelector.three').ColorPicker({ color: '#0000ff', onShow: function (colpkr) { $(colpkr).fadeIn(500); return false; }, onHide: function (colpkr) { $(colpkr).fadeOut(500); return false; }, onChange: function (hsb, hex, rgb) { $('TABLE.quote-request.step-two.logo .colorSelector.three div').css('backgroundColor', '#' + hex); } });
	// Card color pickers.
	$('TABLE.quote-request.step-two.card .colorSelector.one').ColorPicker({ color: '#0000ff', onShow: function (colpkr) { $(colpkr).fadeIn(500); return false; }, onHide: function (colpkr) { $(colpkr).fadeOut(500); return false; }, onChange: function (hsb, hex, rgb) { $('TABLE.quote-request.step-two.card .colorSelector.one div').css('backgroundColor', '#' + hex); } });
	$('TABLE.quote-request.step-two.card .colorSelector.two').ColorPicker({ color: '#0000ff', onShow: function (colpkr) { $(colpkr).fadeIn(500); return false; }, onHide: function (colpkr) { $(colpkr).fadeOut(500); return false; }, onChange: function (hsb, hex, rgb) { $('TABLE.quote-request.step-two.card .colorSelector.two div').css('backgroundColor', '#' + hex); } });
	$('TABLE.quote-request.step-two.card .colorSelector.three').ColorPicker({ color: '#0000ff', onShow: function (colpkr) { $(colpkr).fadeIn(500); return false; }, onHide: function (colpkr) { $(colpkr).fadeOut(500); return false; }, onChange: function (hsb, hex, rgb) { $('TABLE.quote-request.step-two.card .colorSelector.three div').css('backgroundColor', '#' + hex); } });
	
	// Next step
	$("TABLE.quote-request.step-one P.next-step").click(function() {
		
		errors = false;
		
		$("TABLE.quote-request.step-one INPUT.required, TABLE.quote-request.step-one TEXTAREA.required").each(function() {
			if( $(this).val() == '') {
				errors = true;
				$(this).parent().prev(".title").addClass("required");
			}else{
				errors = false;
				$(this).parent().prev(".title").removeClass("required");
			}
		});
		
		if( !errors ) {
			$("DIV.header").removeClass("quoteStepOne").addClass("quoteStepTwo");
			$("DIV.quote-type-buttons").show();
			$("TABLE.quote-request.step-one").hide();
			if($("TABLE.quote-request.step-two.focus").length == 0) {
				$("TABLE.quote-request.step-two.website").removeClass("blur").addClass("focus").show();
			}else{
				$("TABLE.quote-request.step-two.focus").show();
			}
		}
		
		return false;
	});
	
	// Go back
	$("TABLE.quote-request.step-two P.go-back").click(function() {
		$("DIV.header").removeClass("quoteStepTwo").addClass("quoteStepOne");
		$("DIV.quote-type-buttons").hide();
		$("TABLE.quote-request.step-two.focus").hide();
		$("TABLE.quote-request.step-one").show();
		return false;
	});
	
	// Switch request forms.
	$("DIV.quote-type-buttons DIV.website").click(function() {
		// Toggle buttons
		$(this).removeClass("blur").addClass("focus");
		$("DIV.quote-type-buttons DIV.logo").removeClass("focus").addClass("blur");
		$("DIV.quote-type-buttons DIV.card").removeClass("focus").addClass("blur");
		// Toggle forms
		$("TABLE.quote-request.step-two.website").removeClass("blur").addClass("focus").show();
		$("TABLE.quote-request.step-two.logo").removeClass("focus").addClass("blur").hide();
		$("TABLE.quote-request.step-two.card").removeClass("focus").addClass("blur").hide();
		return false;
	});
	$("DIV.quote-type-buttons DIV.logo").click(function() {
		// Toggle buttons
		$(this).removeClass("blur").addClass("focus");
		$("DIV.quote-type-buttons DIV.website").removeClass("focus").addClass("blur");
		$("DIV.quote-type-buttons DIV.card").removeClass("focus").addClass("blur");
		// Toggle forms
		$("TABLE.quote-request.step-two.logo").removeClass("blur").addClass("focus").show();
		$("TABLE.quote-request.step-two.website").removeClass("focus").addClass("blur").hide();
		$("TABLE.quote-request.step-two.card").removeClass("focus").addClass("blur").hide();
		return false;
	});
	$("DIV.quote-type-buttons DIV.card").click(function() {
		// Toggle buttons
		$(this).removeClass("blur").addClass("focus");
		$("DIV.quote-type-buttons DIV.logo").removeClass("focus").addClass("blur");
		$("DIV.quote-type-buttons DIV.website").removeClass("focus").addClass("blur");
		// Toggle forms
		$("TABLE.quote-request.step-two.card").removeClass("blur").addClass("focus").show();
		$("TABLE.quote-request.step-two.logo").removeClass("focus").addClass("blur").hide();
		$("TABLE.quote-request.step-two.website").removeClass("focus").addClass("blur").hide();
		return false;
	});
	
	
	// Submit quote
	$("TABLE.quote-request.step-two.focus P.submit-quote").live('click', function() {
	
		$(this).text("Processing...");
		
		errors = false;
		
		$("TABLE.quote-request.step-two.focus INPUT.required, TABLE.quote-request.step-two.focus TEXTAREA.required").each(function() {
			if( $(this).val() == '') {
				errors = true;
				$(this).parent().prev(".title").addClass("required");
			}else{
				errors = false;
				$(this).parent().prev(".title").removeClass("required");
			}			
		});
		
		if( !errors ) {
		
			// Collected from step one.
			var params = {
				'cmd': 'add_client',
				'first_name': $("INPUT[name=first_name]").val(),
				'last_name': $("INPUT[name=last_name]").val(),
				'email': $("INPUT[name=email]").val(),
				'address': $("INPUT[name=address]").val(),
				'address_extended': $("INPUT[name=address_continued]").val(),
				'city': $("INPUT[name=city]").val(),
				'state': $("INPUT[name=state_region]").val(),
				'country': $("SELECT[name=country]").val(),
				'password': $("INPUT[name=password]").val()
			}
		
			// Website design information.
			if($("TABLE.quote-request.step-two.website.focus").length == 1) {
				params.quote_request_type = "Website";
				params.website_name =  $("TABLE.quote-request.step-two.focus INPUT[name=website_name]").val();
				params.type_of_website = $("TABLE.quote-request.step-two.focus SELECT[name=type_of_website]").val();
				params.number_of_pages = $("TABLE.quote-request.step-two.focus SELECT[name=number_of_pages]").val();
				params.software_integration = $("TABLE.quote-request.step-two.focus SELECT[name=software_integration]").val();
				params.color_one = $("TABLE.quote-request.step-two.focus DIV.colorSelector.one DIV").css("backgroundColor");
				params.color_two = $("TABLE.quote-request.step-two.focus DIV.colorSelector.two DIV").css("backgroundColor");
				params.color_three = $("TABLE.quote-request.step-two.focus DIV.colorSelector.three DIV").css("backgroundColor");
				params.comments = $("TABLE.quote-request.step-two.focus TEXTAREA[name=comments]").val();
			// Logo design information.
			}else if($("TABLE.quote-request.step-two.logo.focus").length == 1) {
				params.quote_request_type = "Logo";
				params.company_name =  $("TABLE.quote-request.step-two.focus INPUT[name=company_name]").val();
				params.logo_type = $("TABLE.quote-request.step-two.focus SELECT[name=logo_type]").val();
				params.number_of_concepts = $("TABLE.quote-request.step-two.focus SELECT[name=number_of_concepts]").val();
				params.color_one = $("TABLE.quote-request.step-two.focus DIV.colorSelector.one DIV").css("backgroundColor");
				params.color_two = $("TABLE.quote-request.step-two.focus DIV.colorSelector.two DIV").css("backgroundColor");
				params.color_three = $("TABLE.quote-request.step-two.focus DIV.colorSelector.three DIV").css("backgroundColor");
				params.comments = $("TABLE.quote-request.step-two.focus TEXTAREA[name=comments]").val();
			// Card design information.
			}else{
				params.quote_request_type = "Business Card";
				params.company_name =  $("TABLE.quote-request.step-two.focus INPUT[name=company_name]").val();
				params.number_of_sides = $("TABLE.quote-request.step-two.focus SELECT[name=number_of_sides]").val();
				params.number_of_concepts = $("TABLE.quote-request.step-two.focus SELECT[name=number_of_concepts]").val();
				params.card_information = $("TABLE.quote-request.step-two.focus SELECT[name=card_information]").val();
				params.color_one = $("TABLE.quote-request.step-two.focus DIV.colorSelector.one DIV").css("backgroundColor");
				params.color_two = $("TABLE.quote-request.step-two.focus DIV.colorSelector.two DIV").css("backgroundColor");
				params.color_three = $("TABLE.quote-request.step-two.focus DIV.colorSelector.three DIV").css("backgroundColor");
				params.comments = $("TABLE.quote-request.step-two.focus TEXTAREA[name=comments]").val();
			}
		
			$.post('/src/ajax/quote.ajax.php', params, function(r) {
				if( r.status == 'success' ) {
					document.location = '/thankyou.php';
				}else{
					$("TABLE.quote-request.step-two.focus P.submit-quote").text("Submit Request");
					alert( r.result.message );
				}
			}, 'json');

		}else{
		
			$("TABLE.quote-request.step-two.focus P.submit-quote").text("Submit Request");
		
		}
		
		return false;
	});

});
