	jQuery.fn.center = function(loaded) {
		var obj = this;
		body_width = parseInt($(window).width());
		body_height = parseInt($(window).height());
		block_width = parseInt(obj.width());
		block_height = parseInt(obj.height());
		
		left_position = parseInt((body_width/2) - (block_width/2)  + $(window).scrollLeft());
		if (body_width<block_width) { left_position = 0 + $(window).scrollLeft(); };
		
		top_position = parseInt((body_height/2) - (block_height/2) + $(window).scrollTop());
		if (body_height<block_height) { top_position = 0 + $(window).scrollTop(); };
		
		if(!loaded) {
			
			obj.css({'position': 'absolute'});
			obj.css({ 'top': top_position, 'left': left_position });
			$(window).bind('resize', function() { 
				obj.center(!loaded);
			});
			$(window).bind('scroll', function() { 
				obj.center(!loaded);
			});
			
		} else {
			obj.stop();
			obj.css({'position': 'absolute'});
			obj.animate({ 'top': top_position }, 200, 'linear');
		}
	}
	
	function tip_box_fadeOut(tip_box_class) {
		$('.'+tip_box_class).fadeOut(function(){ $('#screen').hide(); });
		return false;
	}
	
	function tip_box_fadeOut_screen(tip_box_class) {
		$('.'+tip_box_class).fadeOut(function(){  });
		return false;
	}
	
	function tip_box_fadeIn(tip_box_class) {
		var h = $(document).height();
		$('#screen').css({ 'height': h });	
		$('#screen').show();
		$('.'+tip_box_class).center();
		$('.'+tip_box_class).fadeIn();
		return false;
	}

	function checkRelease() {
		$('span.cw,span.zq').remove();
		var flag = true;
		$.each($('input:text[readonly!="readonly"]'), function(i,item){
			if ($.trim($(item).val()) != '' && $(item).attr('regexp') && $(item).attr('regexp_desc')) {
				var pattern = new RegExp($(item).attr('regexp'));
				if (!pattern.test($(item).val())) {
					$(item).parent().append('<span class="cw"> '+$(this).attr('regexp_desc')+'</span>');
					if (flag) {flag = false;}
					return true;
				}
			}
			if ($.trim($(item).val()) != '' && (!isNaN($(item).attr('minval')) || !isNaN($(item).attr('maxval')))) {
				var pernum = $('#pernum').val();
				var price = $('#price').val();
				if (pernum && price) {
					var minval = $(item).attr('minval');
					var maxval = $(item).attr('maxval');
					var unit = $(':radio[name="goodsReleaseBean.unit"]:checked').val();
					var per_price = Math.round((price * 100 / pernum)*10000) / 10000;
					if ((!isNaN(minval) && parseInt(minval, 10) > per_price) || (!isNaN(maxval) && parseInt(maxval, 10) < per_price)) {
						$(item).parent().append('<span class="cw"> 单价应在每'+unit+(minval/100)+'-'+(maxval/100)+'元之间</span>');
						if (flag) {flag = false;}
						return true;
					}
				}
			}
			if ($(item).attr('required')) {
				return true;
			}
			if ($(item).attr('readonly')) {
				return true;
			}
			if ($.trim($(item).val()) == '') {
				$(item).parent().append('<span class="cw"> 请填写'+$(item).attr('desc')+'</span>');
				if (flag) {flag = false;}
			} else {
				if ($(item).attr('id') == 'price' && parseFloat($('#lab_unitprice').attr('commission')) >= 0) {
					$(item).parent().append('<span class="zq">所需手续费<font color="red">'+$('#lab_unitprice').attr('commission')+'</font>元&nbsp;&nbsp;<a class="blue" href="/helpcenter/73790970494518017.html" target="_blank">[了解更多]</a></span>');
				} else if ($(item).attr('id') == 'price' && parseFloat($('#price').val()) <= 0) {
					$(item).parent().append('<span class="cw">价格必须大于0元</span>');
					if (flag) {flag = false;}
					return true;
				}else {
					$(item).parent().append('<span class="zq">&nbsp;</span>');
				}
			}
		});
		return flag;
	}
	// 加载价格区间
	function loadPriceRange() {
		var gameid = $('#gameid').val();
		$.ajax({
			type:'post',
			url:'/transaction/queryGoodsReleasePriceRange.action',
			data:{gameid:gameid},
			dataType:'json',
			success:function(json) {
				if (json && json.success == 'T') {
					$('#price').attr('minval', json.minval);
					$('#price').attr('maxval', json.maxval);
				}
			}
		});
	}
	function initForm() {
		if ('${empty lastBean.tradetime}' == 'false' && '${lastBean.tradetime}'.indexOf('-') != -1) {
			var times = '${lastBean.tradetime}'.split('-');
			var begin = times[0].split(':')[0];
			$.each($('#select_tradetime_begin option'), function(i, item){
				if ($(item).val == begin) {
					$(item).attr('selected', true);
					return false;
				}
			})
			var end = times[1].split(':')[0];
			$.each($('#select_tradetime_end option'), function(i, item){
				if ($(item).val == end) {
					$(item).attr('selected', true);
					return false;
				}
			})
		}
	}

	$(document).ready(function(){
	
		// 加载价格区间
		loadPriceRange();
		initForm();
		
		restrict_price_input($('#pernum'));
		restrict_price_input($('#price'));
		restrict_digit_input($('#stock'));
	
		$('#hlkBack').click(function(){
			$('#releaseForm').attr('action', '/transaction/backToRelease.action');
			$('#releaseForm').submit();
		});
		
		$('#hlkOK').click(function(){
			if ($('input:radio[name="goodsReleaseBean.autoextractcash"]:checked').val() == 1 && $('#p_account').attr('account_type') == 'noaccount') {
				alert('对不起，您尚未设置提现账户');
				return false;
			}
			if (checkRelease()) {
				if ($('input:radio[name="tradeplace"]').length > 0) {
					$('#titlesupplement option:selected').val($('#titlesupplement option:selected').val() + ' ' + $('input:radio[name="tradeplace"]:checked').val());
				}
				var tradetime = $('select[type="tradetime"]:eq(0)').find('option:selected').text() + '-' + $('select[type="tradetime"]:eq(1)').find('option:selected').text()
				$('#tradetime').val(tradetime);
				if ($('input:radio[name="goodsReleaseBean.deposit"]:checked').val() == 1) {
					if ($('#reimbursement').val() == 'yes') {
						if ($('#reimbursement_tip_show').val() == 'yes') {
							$.ajax({
								type:'post',
								url:'/commerical/queryMyBalanceAndReimbursement.action?rand='+ new Date().getTime(),
								cache:false,
								dataType:'text',
								success:function(data) {
									var balance = parseFloat(data.split('|')[0]).toFixed(2);
									var reimbursement = parseFloat(data.split('|')[1]).toFixed(2);
									var threshold = parseFloat($('#threshold').val()).toFixed(2);
									$('#tip_box_reimbursement').val(reimbursement);
									$('#tip_box_balance').val(balance);
									if (parseFloat(reimbursement) < parseFloat(threshold)) {
										tip_box_fadeIn('tip_box_3');
									} else {
										$('#releaseForm').attr('action', '/transaction/guarantyRelease.action');
										$('#releaseForm').submit();
									}
								}
							});
						} else {
							$('#releaseForm').attr('action', '/transaction/guarantyRelease.action');
							$('#releaseForm').submit();
						}
					} else {
						$.ajax({
							type:'post',
							url:'/commerical/queryMyBalance.action?rand='+ new Date().getTime(),
							cache:false,
							dataType:'text',
							success:function(data) {
								if (isNaN(data)) {
									$('#releaseForm').attr('action', '/transaction/guarantyRelease.action');
									$('#releaseForm').submit();
								} else {
									// 计算余额是否大于要扣除的保证金
									var stock = $('#stock').val();
									var balance = parseFloat(data).toFixed(2);
									var security = (parseInt(stock, 10) * 5).toFixed(2);
									if (parseFloat(balance) >= parseFloat(security)) {
										$('#releaseForm').attr('action', '/transaction/guarantyRelease.action');
										$('#releaseForm').submit();
									} else {
										$('#tip_box_stock').val(stock);
										$('#tip_box_security').val(security);
										$('#tip_box_balance').val(balance);
										var extract = ((parseInt(security.replace('.', ''), 10) - parseInt(balance.replace('.', ''), 10))/100).toFixed(2);
										$('#tip_box_extract').val(extract);
										$('#hlk_tip_box_extract').attr('href', '/financecharge/toAdd.action?amount='+extract);
										tip_box_fadeIn('tip_box_3');
									}
								};
							}
						});
					}
				} else {
					$('#releaseForm').attr('action', '/transaction/guarantyRelease.action');
					$('#releaseForm').submit();
				}
			}
		});
		
		$('#pernum,#price').blur(function(){
			$('#lab_unitprice').attr('commission', '');
			var pernum = $('#pernum').val();
			var price = $('#price').val();
			if (pernum && price && parseFloat(price) > 0) {
				var unit = $(':radio[name="goodsReleaseBean.unit"]:checked').val();
				$('#title').val(pernum + unit + '=' + price + '元');
				// Math.round(number*100000) / 100000 消除浮点数对运算结果的影响
				$('#lab_unitprice').text('1' + unit + '=' + Math.round((price / pernum)*10000) / 10000 + '元');
				if ($('#price').attr('commissionList') != '') {
					var commissionList = $('#price').attr('commissionList').split(';');
					var length = commissionList.length;
					var commission = [];
					if (length > 0) {
						for (var i = 0; i < length; i++) {
							commission = commissionList[i].split('|');
							// 手续费格式 minvalue|maxvalue|payway|payvalue|expayvalue;
							if (i == length - 1 || (parseFloat(price) > commission[0] && parseFloat(price) <= commission[1])) {
								if (commission[2] == '0') {
									$('#lab_unitprice').attr('commission', parseFloat(commission[3]).toFixed(2));
								} else if (commission[2] == '1') {
									$('#lab_unitprice').attr('commission', ((parseFloat(price) * commission[3] + parseFloat(commission[4]) * 100) / 100).toFixed(2));
								} else if (commission[2] == '2') {
									$('#lab_unitprice').attr('commission', (Math.ceil(parseFloat(price) / parseFloat(commission[4])) * parseFloat(commission[3])).toFixed(2));
								}
								break;
							}
						}
					}
				}
			}
		});
		
		$('input:radio[name="tradetype"]').change(function(){
			if ($(this).val() == 'js') {
				$('#releaseForm').attr('action', '/transaction/toConsignment.action');
				$('#releaseForm').submit();
			}
		});
		if ($('input:radio[name="tradeplace"]').length > 0) {
			$('input:radio[name="tradeplace"]').change(function(){
				$('#lab_tradeplace').text($('input:radio[name="tradeplace"]:checked').val());
			});
		}
		
		$(':text,:password').blur(function(){
			if ($.trim($(this).val()) != '' && $(this).attr('regexp') && $(this).attr('regexp_desc')) {
				var pattern = new RegExp($(this).attr('regexp'));
				if (!pattern.test($(this).val())) {
					$(this).parent().find('.cw,.zq').remove();
					$(this).parent().append('<span class="cw"> '+$(this).attr('regexp_desc')+'</span>');
					return false;
				}
			}
			if ($.trim($(this).val()) != '' && (!isNaN($(this).attr('minval')) || !isNaN($(this).attr('maxval')))) {
				var pernum = $('#pernum').val();
				var price = $('#price').val();
				if (pernum && price) {
					var minval = $(this).attr('minval');
					var maxval = $(this).attr('maxval');
					var unit = $(':radio[name="goodsReleaseBean.unit"]:checked').val();
					var per_price = Math.round((price * 100 / pernum)*10000) / 10000;
					if ((!isNaN(minval) && parseInt(minval, 10) > per_price) || (!isNaN(maxval) && parseInt(maxval, 10) < per_price)) {
						$(this).parent().find('.cw,.zq').remove();
						$(this).parent().append('<span class="cw"> 单价应在每'+unit+(minval/100)+'-'+(maxval/100)+'元之间</span>');
						return false;
					}
				}
			}
			if ($(this).attr('id') == 'price' && parseFloat($(this).val()) <= 0) {
				$(this).parent().find('.cw,.zq').remove();
				$(this).parent().append('<span class="cw">价格必须大于0元</span>');
				return false;
			}
			if ($(this).attr('required')) {
				return false;
			}
			if ($(this).attr('readonly')) {
				return false;
			}
			$(this).parent().find('.cw,.zq').remove();
			if ($.trim($(this).val()) == '') {
				$(this).parent().append('<span class="cw"> 请填写'+$(this).attr('desc')+'</span>');
			} else {
				if ($(this).attr('id') == 'price' && parseFloat($('#lab_unitprice').attr('commission')) >= 0) {
					$(this).parent().append('<span class="zq">所需手续费<font color="red">'+$('#lab_unitprice').attr('commission')+'</font>元&nbsp;&nbsp;<a class="blue" href="/helpcenter/73790970494518017.html" target="_blank">[了解更多]</a></span>');
				} else {
					$(this).parent().append('<span class="zq">&nbsp;</span>');
				}
			}
			return false;
		});
	});
