function remove_root_url( url ) {
	return url.replace(root_url,'');
}

function submitForm( form_id, response_container ) {
	form_el = $j('#'+form_id);
	form_data = form_el.serialize();
	$j.ajax({
		type: "POST",
		url: remove_root_url(form_el.attr('action')),
		data: form_data,
		cache: false,
		success: function(html){
			$j('#'+response_container).html(html);
		}
	});
	return false;
}

function getBlock( block_url, response_container, a_sync ) {
	if ( null == a_sync ) {
		a_sync = true;
	}
	$j.ajax({
		type: "GET",
		async: a_sync,
		url: remove_root_url(block_url),
		cache: false,
		success: function(html){
			$j('#'+response_container).html(html);
		}
	});
	return false;
}

function ask_us_container( block_url ) {
	el = $j('#ask_us_container');
	
	if ( el.size() == 0 ) {
		
		var new_div = '<div id="ask_us_container" class="ask_us_container_pos" style="display:none;"></div>';
		$j( 'body' ).prepend( new_div );
		el = $j('#ask_us_container');
	}
	
	if ( el.css('display') == 'none' ) {
		getBlock( block_url, 'ask_us_container', false );
	}
	el.show();
	return false;
}

function ask_us() {
	
	return ask_us_container( root_url + '?act=js/ask_us' );
}

function pollSubmit( form ) {
	
	form = $j( form );
	$j( '#poll_block *' ).removeClass( 'error' );
	
	$j.ajax( {
		type : 'post',
		url: form.get(0).action,
		data : form.serializeArray(),
		dataType: 'json',
		success : function( response ) {
			
			if ( response.error ) {
				
				alert( response.error_msg );
				
				return false;
			}
			
			if ( response.answers ) {
				
				$j( '#poll_block .poll' ).addClass( 'error' );
				
				return false;
			}
			
			if ( response.custom ) {
				
				$j( '#poll_block textarea' ).addClass( 'error' );
				
				return false;
			}
			
			if ( response.success ) {
			
				$j( '#poll_block' ).replaceWith( response.success );
			}
		}
	} );
}

function checkPollItem( id ) {
	
	var element = $j( '#' + id );
	
	if ( element ) {
		
		element.attr( 'checked', true );
	}
}

function uselessData( is_good, item_id ) {
	
	$j.ajax({
		url: root_url + '?act=useless/send',
		data: { good: is_good, item_id: item_id },
		dataType: 'json',
		success: function( data ) {
			
			if ( data.step == 1 ) {
				
				$j( '#useless_info_block' ).slideUp( 500, function() {
					
					$j( '#useless_info_form' ).slideDown( 500 );
				});
			}
			
			if ( data.step == 2 ) {
				
				$j( '#useless_info_text' ).html( data.text );
				$j( '#useless_info_block' ).slideUp( 500, function() {
					
					$j( '#useless_info_text' ).slideDown( 500 );
				});
			}
		}
	});
}

function uselessExplain( item_id ) {
	
	var info = $j( '#useless_info_textarea' ).attr( 'value' );
	
	if ( info.length < 5 ) {
		
		$j( '#useless_info_textarea' ).css( 'border', '1px solid red' );
		return false;
	} else {
		
		$j.ajax({
			url: root_url + '?act=useless/explain',
			data: { text: info, item_id: item_id },
			dataType: 'json',
			success: function( data ) {
				
				$j( '#useless_info_text' ).html( data.text );
				$j( '#useless_info_form' ).slideUp( 500, function() {
					
					$j( '#useless_info_text' ).slideDown( 500 );
				});
			}
		});
	}
}