var QiuGouForm = function (box){
	var radio = $('.radio', box),
	table = $('table.form', box)
	table_hy = $('.hy', table).add('.fhy', table);
	var ajaxloading = false;
	radio.delegate('li', 'click', function (){
		$(':radio', this).attr('checked', true).change();
	})
	.delegate(':radio', 'change', function (){
		var name = this.value == 1 ? 'hy' : 'fhy';
		//alert(this.value);
		table_hy.hide();
		$('.' + name, table).show();
	})
	.find(':radio').attr('checked', false);
	var txttitle = $(':text[name=qiugoutitle]', table),
	txtcont = $('textarea[name=qiugoucontent]', table),
	txtemail = $('.email_l', table).add('.email_r', table),
	txtqiugouemail = $('.qiugouemail_l', table).add('.qiugouemail_r', table);
	txtemail.bind('focusout', function (){
		$(':hidden[name=email]').val($.trim($('.email_l', table).val()) + '@' + $.trim($('.email_r', table).val()));
	});
	txtqiugouemail.bind('focusout', function (){
		$(':hidden[name=qiugouemail]').val($.trim($('.qiugouemail_l', table).val()) + '@' + $.trim($('.qiugouemail_r', table).val()));
	});
	txttitle.bind('focus', function (){
		if (this.value == '请输入您的标题...')
			this.value = '';
		$(this).addClass('luru');
	})
	.bind('focusout', function (){
		if ($.trim(this.value) == ''){
			$(this).removeClass('luru');
			this.value = '请输入您的标题...';
		}
	});
	txtcont.bind('focus', function (){
		if (this.value == '请输入求购详细内容...')
			this.value = '';
		$(this).addClass('luru');
	})
	.bind('focusout', function (){
		if ($.trim(this.value) == ''){
			$(this).removeClass('luru');
			this.value = '请输入求购详细内容...';
		}
	});
	
	//投资额度
	$.getScript(APP_WEB_API_URL + '/wanttobuy/getinvest', function (){
		if (json_invest == null || json_invest.code == 'empty' || json_invest.code == 'erorr'){
			return;
		}
		var dom = getSelDom(json_invest);
		if (dom.size() < 7){
			$('.qiugouinvest', box).css({'height' : 'auto', 'overflow' : 'visible'});
		}
		$('.qiugouinvest', box).html(dom)
		.find('dd:first').click();
	});
	
	//货源
	$.getScript(APP_WEB_API_URL + '/wanttobuy/getsource', function (){
		if (json_source == null || json_source.code == 'empty' || json_source.code == 'erorr'){
			return;
		}
		
		var dom = getSelDom(json_source);
		if (dom.size() < 7){
			$('.qiugousource', box).css({'height' : 'auto', 'overflow' : 'visible'});
		}
		$('.qiugousource', box).html(dom)
		.find('dd:first').click();
	});
	
	//行业
	$.getScript(APP_WEB_API_URL + '/wanttobuy/getindustry', function (){
		if (json_industry == null || json_industry.code == 'empty' || json_industry.code == 'erorr'){
			return;
		}
		var dom = getSelDom(json_industry);
		if (dom.size() < 7){
			$('.qiugouindustry', box).css({'height' : 'auto', 'overflow' : 'visible'});
		}
		$('.qiugouindustry', box).html(dom)
		.find('dd:first').click();
	});
	
	//二级地区
	$.getScript(APP_WEB_API_URL + '/wanttobuy/getregionsecond', function (){
		if (json_regionsecond == null || json_regionsecond.code == 'empty' || json_regionsecond.code == 'erorr'){
			return;
		}
		var dom = getSelDom(json_regionsecond);
		if (dom.size() < 7){
			$('.qiugouregion', box).css({'height' : 'auto', 'overflow' : 'visible'});
		}
		$('.qiugouregion', box).html(dom);
		//.find('dd:first').click();
	});
	
	$('.qiugouregion', box).delegate('dd', 'click', function (){
		var parentid = $(this).attr('rel');
		//三级地区
		$.getScript(APP_WEB_API_URL + '/wanttobuy/getregionthird?parentid=' + parentid, function (){
			if (json_regionthird == null || json_regionthird.code == 'empty' || json_regionthird.code == 'erorr'){
				return;
			}
			var dom = getSelDom(json_regionthird);
			if (dom.size() < 7){
				$('.parent_id', box).css({'height' : 'auto', 'overflow' : 'visible'});
			} else {
				$('.parent_id', box).css({'height' : '160px', 'overflow' : 'auto'});
			}
			$('.parent_id', box).html(dom).prev().html('地/州');
			$('[name=parent_id]', box).val(''); 
			//.find('dd:first').click();
		});
	});
	
	//生成select元素
	function getSelDom (data){
		var arr = [];
		$.each(data, function (k, v){
			arr.push('<dd rel="' + k + '">' + v + '</dd>');
		});
		return $(arr.join(''));
	}
	
	table.delegate('.divselect', 'mouseleave', function (){
		$('dl', this).hide();
	})
	.delegate('.divselect span', 'click', function (){
		$(this).siblings('dl').show();
	})
	.delegate('dl>dd', 'hover', function (){
		$(this).toggleClass('selected');
	})
	.delegate('dl>dd', 'click', function (){
		$(this).parent().hide().siblings('span').html($(this).html())
		.siblings(':hidden').val($(this).attr('rel'));
	})
	.delegate('.qiugousend', 'click', function (){
		if (ajaxloading) return;
		ajaxloading = true;
		$.getScript(APP_WEB_API_URL + '/ajax/checkislogin',function (){
			ajaxloading = false;
			if (json == null || json.code == 'empty' || json.code == 'erorr'){
				return;
			}
			sendcallback(json);
		});
	});
	var sendcallback = function (data){
		var parm = {
			qiyememberid : data.uid,
			qiugouinvest : $('[name=qiugouinvest]', table).val(),
			qiugouindustry : $('[name=qiugouindustry]', table).val(),
			qiugousource : $('[name=qiugousource]', table).val()
		},
		reg_email = /^\s*([A-Za-z0-9_-]+(\.\w+)*@(\w+\.)+\w{2,3})\s*$/,
		reg_call = /^13[0-9]{1}[0-9]{8}$|15[01389]{1}[0-9]{8}$|189[0-9]{8}|153[0-9]{8}$/;
		
		
		if (data.uid == 0){
			parm.hy = $(':radio[name=hy]:checked', box).val();
			switch(parm.hy){
				case '1':
					parm.email = $('[name=email]', table).val();
					if (parm.email == ''){
						alert('请输入邮箱');
						return;
					}
					if (!reg_email.test(parm.email)){
						alert('请输入正确格式的邮箱');
						return;
					}
					parm.password = $('[name=password]', table).val();
					if (parm.password == ''){
						alert('请输入登录密码');
						return;
					}
					break
				case '0':
					parm.qiugouemail = $('[name=qiugouemail]', table).val();
					if (parm.qiugouemail == ''){
						alert('请输入邮箱');
						return;
					}
					if (!reg_email.test(parm.qiugouemail)){
						alert('请输入正确格式的邮箱');
						return;
					}
					parm.qiugouphone = $('[name=qiugouphone]', table).val();
					if (parm.qiugouphone == ''){
						alert('请输入手机号码');
						return;
					}
					if (!reg_call.test(parm.qiugouphone)){
						alert('请输入正确格式的手机号码');
						return;
					}
					parm.qiugouusername = $('[name=qiugouusername]', table).val();
					if (parm.qiugouusername == ''){
						alert('请输入姓名');
						return;
					}
				break
				default:
					alert('请选择是否为渠道网会员');
					return;
			}
		} else {
			if (data.usertype == 1){
				alert('企业用户不能发布求购');
				return;
			}
			parm.uctype = data.usertype;
			parm.hy = 1;
			parm.email = data.username;
		}
		if ($('[name=qiugoutitle]', table).val() == '' 
			|| $('[name=qiugoutitle]', table).val() == '请输入您的标题...'){
			alert('请输入标题');
			return;
		}
		if ($('[name=qiugoutitle]', table).val().length < 4){
			alert('标题字数至少填写4个');
			return;
		}
		parm.qiugoutitle = $('[name=qiugoutitle]', table).val();
		if ($('[name=qiugoucontent]', table).val() == '' 
			|| $('[name=qiugoucontent]', table).val() == '请输入求购详细内容...'){
			alert('请输入内容');
			return;
		}
		
		if ($('[name=qiugoucontent]', table).val().length < 10){
			alert('内容字数至少填写10个');
			return;
		}
		parm.qiugoucontent = $('[name=qiugoucontent]', table).val();
		parm.qiugouregion = $('[name=qiugouregion]', table).val();
		if (parm.qiugouregion == '') {
			alert('请选择省/市');
			return;
		}
		parm.parent_id = $('[name=parent_id]', table).val();
		if (parm.parent_id == '') {
			alert('请选择地/州');
			return;
		}
		parm.qiugoustoptime = $('[name=qiugoustoptime]:checked').val();
		if (! window.confirm('如果发布过求购，本次求购信息将更新上一条信息！')){
			return ;
		}
		$('.ajaxsending').show();
		$.getScript(APP_WEB_API_URL + '/wanttobuy/submit?' + $.param(parm), function (){
			$('.ajaxsending').hide();
			if (json_submit == null || json_submit.code == 'empty' || json_submit.code == 'erorr'){
				return;
			}
			
			var d_qiugoufhy = new Diglog('.fg-qiugou-fhy');
			var d_qiugouhy = new Diglog('.fg-qiugou-hy');
			if (json_submit.code){
				$('.dialog_qiugou_from').hide();
				if (parm.hy == 1){//会员
					d_qiugouhy.show();
					closehy(5);
					$.getScript(APP_WEB_API_URL + '/ajax/checkislogin', function (){
						if (json == null || json.code == 'empty' || json.code == 'erorr'){
							return;
						}
						var hid = $('#USERID').val(json.uid);
						if (hid.val() != 0){
							$('.username').html('<a href="' + json.loginurl + '">' + json.username + '</a>');
							$('a.login_link').hide();
							$('a.register_link').hide();
							$('a.returnUser').show();
						}
					});
				} else {//非会员
					d_qiugoufhy.show();
					fhyFunction(json_submit);
				}
			} else {
				switch(json_submit.error){
					case 1: //用户名错误
						alert('用户名错误');
						break;
					case 2: //密码错误
						alert('密码错误');
						break;
					case 3: //内容保存失败
						alert('发布失败，请检索数据');
						break;
					case 4: //用户名已经存在
						alert('用户名已经存在');
						break;
					case 5: //登录邮箱格式有误
						alert('登录邮箱格式有误');
						break;
					case 6: //登录邮箱不允许注册
						alert('登录邮箱不允许注册');
						break;
					case 7: //登录邮箱已经注册
						alert('登录邮箱已经注册');
						break;
					case 8: //企业用户发布失败
						alert('企业用户不能发布求购');
						break;
					default:
						alert('发布失败，请检索数据');
						return;
				}
			}
		});
	};
	var fhyFunction = function (p){
		var qiugoufhy = new QiuGouFhy('.fg-qiugou-fhy');
		qiugoufhy.setUserMailPsw(p.email, p.password);
		qiugoufhy.setUserInfo(p)
	};
};
var hydialogclose;
function closehy(d){
	var t = {
		1 : '一',
		2 : '二',
		3 : '三',
		4 : '四',
		5 : '五'
	}
	$('.fg-qiugou-hy .timeend').html(t[d]);
	d--;
	if (d < 0){
		$('.fg-qiugou-hy .close').click();
	} else {
		hydialogclose= setTimeout("closehy('" + d + "')", 1000);
	}
}
$(function(){
	var qiugou = new QiuGouForm('.fg-qiugou-form');
	$.getScript(APP_WEB_API_URL + '/ajax/checkislogin', function (){
		if (json == null || json.code == 'empty' || json.code == 'erorr'){
			return;
		}
		var hid = json.uid;
		if (hid != 0){
			$('.radio li', '.fg-qiugou-form').hide();
		}
	});
	
	$('.fg-qiugou-hy .close').bind('click', function (){
		window.location.reload();
		clearTimeout(hydialogclose);
	});
	
	$('.returnUser').bind('click', function (e){
		e.preventDefault();
		$('.radio li', '.fg-qiugou-form').show();
	});
});
