var plist = null;

jQuery( function()
{
	place_add_product_button();
	init_country_select();
	show_errors();
});

function place_add_product_button()
{
	$("#quote-products")
	.after(
		$("<input id=\"quote-add-product-button\" type=\"button\" />")
		.attr({
			"name":"action",
			"value":"Add Product"
		})
		.click( add_product )
	);
}

function add_product()
{
	$("#quote-products")
	.after(
		$("<div/>").attr("id","quote-plist")
	);
	if( plist == null )
	{
		$.ajax({
			type:"get",
			url:"tpl/plist.php",
			dataType:"html",
			success:function( response, status )
			{
				plist = response;
				init_plist();
			}
		});
	}
	else
	{
		init_plist();
	}
	$("#quote-add-product-button").val("Loading...").attr( "disabled", "disabled" );
}

function remove_product()
{
	$(this).parents("tr").remove();
}

function init_plist()
{
	$("#quote-plist")
	.html( plist );

	$("#plist")
	.css({ "height":"200px", "width":"300px" })
	.hover( function(){}, remove_plist );
	
	$(".plist-products li").click( function()
	{
		var product_id = ( ( $(this).attr("id") ).split("-") ).pop()
		var product_number = $(".plist-product-number",this).text();
		var product_title = $(".plist-product-title",this).text()
		insert_product( product_id, product_number, product_title );
		remove_plist();
		return false;
	});
}

function remove_plist()
{
	$("#quote-plist").remove();
	$("#quote-add-product-button").val("Add Product").removeAttr( "disabled" );
}

function insert_product( id, number, title )
{
	

	var qty = $("<select/>")
		.attr({
			"name":"quote[products]["+id+"][qty]"
		})
		.addClass( "quote-product-qty" );

	for( var i=1; i<11; i++ )
		qty.append( $("<option/>").val(i).text(i) );

	var product = $("<a/>")
						.append(
							$("<span/>")
							.addClass("product-number")
							.text(number+" ")
						)
						.append(
							$("<span/>")
							.addClass("product-title")
							.text(title)
						);
						
	var remove = $("<input type=\"button\"/>")
					.val("remove")
					.click( function(){ remove_product.call( this ); } );
	
	var input = $('<input type="hidden" />')
					.attr({
						"name":"quote[products]["+id+"][id]"
					})
					.addClass( "quote-product-id" )
					.val(id);
	try
	{
		$("#quote-products")
		.append(
			$("<tr/>")
				.append( $("<td/>").append( qty ).append( input ) )
				.append( $("<td/>").append( product ) )
				.append( $("<td/>").css( "text-align", "right" ).html( remove ) )
			);
	}
	catch( e )
	{
		alert( e );
	}
}

function init_country_select()
{
	$("#quote-country").change( init_country_select );
	if( $("#quote-country").val() == "USA" )
	{
		place_state_select();
	}
	else
	{
		place_state_input();
	}
}

function place_state_select()
{
	$("#quote-state-container")
	.empty()
	.append(
		$("<select/>")
		.attr({
			"id":"quote-state",
			"name":"quote[state]"
		})
		.append( $("<option/>").text( "Loading..." ) )
		.load( "tpl/stateoptions.php" )
	);
}

function place_state_input()
{
	$("#quote-state-container")
	.empty()
	.append(
		$("<input/>")
		.attr({
			"id":"quote-state",
			"name":"quote[state]"
		})
	);
}

function validate_form()
{
	var errors = [];
}

function show_errors()
{
	$("#errors li").each( function()
	{
		var field_id = "#"+$(this).attr("rel");
		$( field_id ).parents( "li" ).addClass( "error" );
	});
}

