function selectState(){

   if($('#countrySel').val()=="USA")
   {
       $('#stateInputbox').hide();
       $('#stateSelectbox').show();
        $('#stateSelectCanbox').hide();
   }
   else if($('#countrySel').val()=="Canada")
   {
       $('#stateInputbox').hide();
       $('#stateSelectbox').hide();
       $('#stateSelectCanbox').show();
   }
   else
   {
       $('#stateInputbox').show();
       $('#stateSelectbox').hide();
        $('#stateSelectCanbox').hide();
   }
}

$(document).ready(function(){
	$('#imageReplace a').live('click', function(){
		$('#uploadButton').click();
		return false;
	});

	$('#uploadForm').submit(function(){
		$('#TB_ajaxContent .error').remove();
		$('#uploadForm :submit')
		.attr('title', $('#uploadForm :submit').attr('value') )
		.attr('value', 'Uploading...');
	});

	$('#uploadIframe').load(function(){
		$('#uploadForm :submit').attr('value', $('#uploadForm :submit').attr('title') );

		if ( $('#uploadIframe').contents().find('#image').length == 1 )
		{
			$('#imageReplace').remove();
			var path = $('#uploadIframe').contents().find('#image').text();
			$( '<p id="imageReplace"><img src="' + path + '" /><input type="hidden" name="84ad04e1-a931-4938-aafe-474290f7b052" value="' + path + '" /><br><a href="#">Replace the image</a></p>' ).insertBefore('#uploadButton');
			$('#uploadButton').hide();
			$('#TB_ajaxContent .error').remove();
			tb_remove();
		}
		else
		{
//			$( '<p class="error">' + $('#uploadIframe').contents().find('div:first').html() + '</p>' ).insertBefore('#uploadForm');
			$( '<p class="error">Please upload only image files. (JPG, GIF or PNG format)</p>' ).insertBefore('#uploadForm');
		}
	});

	$('.etsy_shop_preview .listing-card a').click(function() {
		$(this).attr('target', '_blank');
	});

	$('.etsy_shop_preview .descWrapper p a').click(function() {
		$(this).attr('target', '_blank');
	});

	$('#joinForm').submit(function(){
		$('input.error, select.error', this).removeClass('error');
		$('span.error', this).remove();
		
		$('.required', this).each(function(){
			if ( $(this).is('input') )
			{
				if ( '' == $(this).val() )
				{
					$(this).addClass('error');
				}
			};
			if ( $(this).is('select') )
			{
				if ( '' == $(':selected', this).val() )
				{
					$(this).addClass('error');
				}
			};
		});
		
		if($('#Confirm_Contact_Email').val() != $('#Contact_Email').val()) {
		    $('#Confirm_Contact_Email').addClass('error');
		    $('#Contact_Email').addClass('error');
         	$('<span class="error">Your email addresses do not match.  Please confirm your email.</span>').insertBefore('#joinForm .fields');
			window.scrollTo(0, 600);
    		return false;
		}

		if ( $('.error', this).length > 0 ) {
			$('<span class="error">Please complete all fields marked below.</span>').insertBefore('#joinForm .fields');
			window.scrollTo(0, 600);
			return false;
		}

		return true;
	});

	$('#join_continue').click(function() {
		$('#joinForm').submit();
	});

	$('#regValidateForm').submit(function(){

	   $('input.error, select.error', this).removeClass('error');
		 $('span.error', this).remove();
     $('.required', this).each(function(){
			if ( $(this).is('input') )
			{
				if ( '' == $(this).val() )
				{
					$(this).addClass('error');
				}
			};
			if ( $(this).is('select') )
			{
				if ( '' == $(':selected', this).val() )
				{
					$(this).addClass('error');
				}
			};
		});
		
		if ( $('.error', this).length > 0 ) 
		{
			$('<span class="error">Please complete all fields marked below.</span>').insertBefore('#regValidateForm h4.errorspan');
			window.location = '#top'; 
			return false;
		}
  
  });
  
	$('#updateForm').submit(function(){
		$('input.error, select.error', this).removeClass('error');
		$('span.error', this).remove();
		
		$('.required', this).each(function(){
			if ( $(this).is('input') )
			{
				if ( '' == $(this).val() )
				{
					$(this).addClass('error');
				}
			};
			if ( $(this).is('select') )
			{
				if ( '' == $(':selected', this).val() )
				{
					$(this).addClass('error');
				}
			};
		});

		if ( $('.product :checked').length == 0 )
		{
			$('<br /><span class="error">Please Choose one.</span>').appendTo('.chooser');
		}
		
		if ( $('.error', this).length > 0 ) 
		{
			$('<span class="error">Please complete all fields marked below.</span>').insertBefore('#updateForm h4.errorspan');
			window.location = '#top'; 
			return false;
		}

		// show preview
		 if($('#countrySel').val()=="USA")
     {
         $('#stateInputbox').remove();        
          $('#stateSelectCanbox').remove();
     }
     else if($('#countrySel').val()=="Canada")
     {
         $('#stateInputbox').remove();
         $('#stateSelectbox').remove();       
     }
     else
     {
         $('#stateSelectbox').remove();
          $('#stateSelectCanbox').remove();
     }


	});
	
	$('#.product :checkbox').click(function(){
		$('#.product :checkbox').not(this).removeAttr('checked');
		$(this).attr('checked', 'checked');
		
		$(this).closest('div.product')
		.addClass('chosen');
	
	});
	
	$('.product .button')
	.prepend('<a href="#" class="checkbox"></a> Choose this one.')
	.find(':checkbox')
	.hide();
	
	$('.product .checkbox').click(function(){
		
		if($(this).closest('div.product').find('input').val() == "Free")
		{
		  $(".premium").hide();
		  $(".freeonly").show();    
		}
		else
		{
		  $(".premium").show();
		  $(".freeonly").hide();    
		  
    }
		  
		  
		var product = $(this).closest('div.product');
		$('.product :checkbox').removeAttr('checked');
		$('div.product').removeClass('chosen');
		$(product).addClass('chosen');
		$(':checkbox', product).attr('checked', 'checked');
		return false;
	});

	$('<b class="count">600 Characters</b>')
	.insertAfter('.textarea');

	$('.textarea')
	.keyup(function(){
		$(this).next('b').text( (600 - $(this).val().length) +  ' Characters');
		if ( $(this).val().length > 600 )
		{
			$(this).val( $(this).val().substring(0,599) );
		}
	});

	// populate form
	if ( $('#listing_Listing_Type').length == 1 ) 
	{
		$('#enhanced .checkbox')
		.click();
	}
	else
	{
		if ( $('#listing_Listing_Type').length == 1 ) 	
		{
			$('#basic .checkbox')
			.click();
		}
	}

	if ( $('#listing_Category_1').length == 1 ) $('#Category_1 option[value=' + $('#listing_Category_1').val()  + ']').attr('selected', 'selected');
	if ( $('#listing_Category_2').length == 1 ) $('#Category_2 option[value=' + $('#listing_Category_2').val()  + ']').attr('selected', 'selected');
	if ( $('#listing_Category_3').length == 1 ) $('#Category_3 option[value=' + $('#listing_Category_3').val()  + ']').attr('selected', 'selected');
	if ( $('#listing_Category_4').length == 1 ) $('#Category_4 option[value=' + $('#listing_Category_4').val()  + ']').attr('selected', 'selected');
	if ( $('#listing_Category_5').length == 1 ) $('#Category_5 option[value=' + $('#listing_Category_5').val()  + ']').attr('selected', 'selected');
	if ( $('#listing_Country').length == 1 ) $('#Country option[value=' + $('#listing_Country').val()  + ']').attr('selected', 'selected');
	if ( $('#listing_State').length == 1 ) $('#stateSelectbox option[value=' + $('#listing_State').val()  + ']').attr('selected', 'selected');
	if ( $('#listing_State').length == 1 ) $('#stateSelectCanbox option[value=' + $('#listing_State').val()  + ']').attr('selected', 'selected');
	if ( $('#listing_Source').length == 1 ) $('#Source option[value=' + $('#listing_Source').val()  + ']').attr('selected', 'selected');
  
});
