// JavaScript Document


$(document).ready(function(){
	var mainObj = $(".category");
	var offset = mainObj.offset();
	var mainHeight = mainObj.height();
	var mainWidth = mainObj.width();
	var wtop = offset.top - 20;
	var wleft = offset.left - 30;
	var u = 'actioncode.php';
	var errorMsgObj = $("#errorregmessage");
	var errorImg = '<img src="/html/template/images/exclamation.gif" align="absmiddle" /> ';
	var consultationData = {};
	var icate = 0;
	/*
	var bttabObj = $("#bttab");
	var zltabObj = $("#zltab");
	
	bttabObj.click(function(){
		$(this).removeClass();
		zltabObj.addClass("noselectlogintab boxlinebottom boxlineleft");		
		$("#consultationcate").removeClass("nodisplay");
		$("#specificcate").addClass("nodisplay");
		$("#cons_type").text('1');
	});
	
	zltabObj.click(function(){
		$(this).removeClass();
		bttabObj.addClass("noselectlogintab boxlinebottom boxlineright");
		$("#consultationcate").addClass("nodisplay");
		$("#specificcate").removeClass("nodisplay");
		$("#cons_type").text('2');
	});*/
	
	$("input[type=radio][name=rdo_consulttype]").click(function(){
		var curObj = $(this);
		var chk_rdo_constype_val = curObj.val();
		if (chk_rdo_constype_val == '1')
		{
			$("#consultationcate").removeClass("nodisplay");
			$("#specificcate").addClass("nodisplay");
			$("#cons_type").text('1');
		}
		else
		{
			$("#consultationcate").addClass("nodisplay");
			$("#specificcate").removeClass("nodisplay");
			$("#cons_type").text('2');			
		}
		
	});
	
	var su_click = function(){		
		var msg = '';
		
		var userconsulationMsgObj = $("#msg_consulationinput");
		
		var provinceid = $("#selProvinces").val();
		var cityid = $("#selCities").val();
		var title = $("#contitle").val();
		var content = $("#kw").val();
		var province = $("#selProvinces").find("option:selected").text();
		var city = $("#selCities").find("option:selected").text();
		var cons_type = $("#cons_type").text();
		if (cons_type == '1')
		{
			var catid = $("#consultationcategory").val();	
		}
		else
		{
			var catid = $("#specificcatgory").val();			
		}
		if (title.length == 0)
		{
			msg += '咨询主题 ';
		}		
		if (content.length == 0)
		{
			msg += '咨询内容 ';
		}		
		if (city.length == 0)
		{
			msg += '涉案区域 ';
		}	
		if (msg.length > 0)
		{
			msg += ' 不能为空!';
		}
		
		//var loadObj = $("#loadingid");
		//loadObj.css({"display": 'block', "top": wtop, "left": wleft});
		
		if (msg.length > 0)
		{
			msg += ' 不能为空!';
			userconsulationMsgObj.html(msg);
			userconsulationMsgObj.removeClass();
			userconsulationMsgObj.addClass("errorregmessage");
		}
		else
		{
			userconsulationMsgObj.html('');
			userconsulationMsgObj.removeClass();
		
			consultationData = {ac:'submitconsultation', catid: catid, provinceid: provinceid, cityid: cityid, province: province, city: city, title: title, content: content, cons_type: cons_type};
			$.ajax({
				 type: "POST",
				 url: u,
				 data:consultationData,
				 dataType: "json",
				 success: returncallback
			});
		
		}
		
	};
	
	$("#su").click(su_click);
	
		
	$("#loginlink").click(function(){
		var loginObj = $("#logindiv");
		loginObj.css({"display": 'block', "top": wtop, "left": wleft});
	});
	
	$("#loginclose").click(function(){
		var loginObj = $("#logindiv");
		loginObj.css({"display": "none"});						
	});
	
	$("#reglink").click(function(){
		var regObj = $("#reguserdiv");
		var loginObj = $("#logindiv");
		if (loginObj.css("display") != 'none')
			loginObj.css("display", "none");
		
		
		regObj.css({"display": 'block', "top": wtop, "left": wleft});					 
	});
	
	$("#regclose").click(function(){
		var loginObj = $("#reguserdiv");
		loginObj.css({"display": "none"});						
	});
	
	$("#regsuccesslink").click(function(){
		
		
		var regsuccessObj = $("#payinfodiv");
		regsuccessObj.css({"display": 'block', "top": wtop, "left": wleft});
	});
	$("#payinfoclose").click(function(){
		var loginObj = $("#payinfodiv");
		loginObj.css({"display": "none"});						
	});
	
	$("#nowloginingid").click(function(){
		var regObj = $("#reguserdiv");	
		if (regObj.css("display") != 'none')
			regObj.css("display", "none");
		
		var loginObj = $("#logindiv");
		loginObj.css({"display": 'block', "top": wtop, "left": wleft});
		
	});
	
	$("#btn_login").click(function(){
		var username = $("#lguserno").val();
		var password = $("#lguserpwd").val();
		var captcha = $("#login_captcha").val();
		var args = {ac:'signin', username: username, password: password, captcha: captcha};
		$.ajax({
				 type: "POST",
				 url: u,
				 data:args,
				 dataType: "json",
				 success: returncallback
			});
	});
	
	$("#btn_pay").click(function(){
		var consid = $("#consultationidval").text();
		var constypeval = $("#consultationtypeval").text();
		var paypointidval = parseInt($("#paypointid").text());
		var balancepointval = parseInt($("#balancepointid").text());
		var tourl = '';
		if (balancepointval >= paypointidval)
		{
			
			var args = {ac:'submitinsertservicepaylog', typ: constypeval, consulation_id: consid, paycode: 'balance', payname: '余额支付'};
			$.ajax({
				 type: "POST",
				 url: u,
				 data:args,
				 dataType: "json",
				 success: function(res){
				 	
					if (res.message == '1')
					{
						alert('未登录');
					}
					else
					{
						if (res.content == 'balance')
						{
							alert(res.message);
							if (constypeval == '1')
							{
								tourl = 'user.php?act=myconsultation&itm=detail&id=' + consid;
							}
							else
							{
								tourl = 'user.php?act=myservicespecific&itm=detail&id=' + consid;
							}
							
							window.location.href = tourl;
						}
					}
				 }
			});
			
			
		}
		else
		{
			if (confirm(notice_balancepoint_insufficiency))
			{
				// jump to pay page
				
				window.location.href = 'user.php?act=myconsultation&itm=detail&id=' + consid;
			}
			else
			{
				return ;
			}
		}
	});
	
	$("#btn_credit").click(function(){
		var cid = $("#consultationidval").text();
		var constypeval = $("#consultationtypeval").text();
		var tourl = '';
		if (constypeval == '1')
		{
			tourl = 'user.php?act=myconsultation&itm=detail&id=' + cid;
		}
		else
		{
			tourl = 'user.php?act=myservicespecific&itm=detail&id=' + cid;
		}
		window.location.href = tourl;
		//window.location.href = 'user.php?act=myfinances&itm=myrecharge';
	});
	
	$("#btn_reg").click(function(){
		var vfrm = register();
		if (vfrm)
		{
			var txtuserno = $("#txtuserno").val();
			var txtuserpwd = $("#txtuserpwd").val();
			//var txtemail = $("#txtemail").val();
			var txtmobile = $("#txtmobile").val();
			var args = {ac:'submituserreg', username: txtuserno, pwd: txtuserpwd, mobile: txtmobile};
			$.ajax({
				 type: "POST",
				 url: u,
				 data:args,
				 dataType: "json",
				 success: returncallback
			});
		}
	});
	
	var usernameMsgObj = $("#msg_username");
	$("#txtuserno").blur(function(){
		var username = $.trim($(this).val());	
		var msg = '';
		
		if (username.length == 0)
		{
			msg = username_empty + '\n';
		}
		else if (username.match(/^\s*$|^c:\\con\\con$|[%,\'\*\"\s\t\<\>\&\\]/))
		{
			msg = username_invalid + '\n';
		}
		else if (username.length < 3)
		{
			msg = username_shorter + '\n';
		}
		
		if (msg.length > 0)
		{
			usernameMsgObj.html(msg);
			usernameMsgObj.removeClass();
			usernameMsgObj.addClass("errorregmessage");
		}
		else
		{
			
			var args = {ac:'submitusername', username: username};
			$.ajax({
				 type: "POST",
				 url: u,
				 data:args,
				 dataType: "json",
				 success: returncallback
			});
		}
	});
	
	$("#txtuserpwd").blur(function(){
		var password  = $.trim($("#txtuserpwd").val());	
		var pwdMsgObj = $("#msg_password");
		var msg = '';
		
		if (password.length == 0)
		{
			msg = password_empty + '\n';
		}
		else if (password.length < 6)
		{
			msg = password_shorter + '\n';
		}
		
		if (msg.length > 0)
		{
			pwdMsgObj.html(msg);
			pwdMsgObj.removeClass();
			pwdMsgObj.addClass("errorregmessage");
		}
		else
		{
			pwdMsgObj.html('');
			pwdMsgObj.removeClass();
			pwdMsgObj.addClass("successregmessage");
		}
	});
	
	$("#txtuserconfirmpwd").blur(function(){
		var password  = $.trim($("#txtuserpwd").val());
		var confirm_password = $.trim($("#txtuserconfirmpwd").val());
		var pwdMsgObj = $("#msg_confirmpassword");
		var msg = '';
		
		if (confirm_password != password )
		{
			msg = confirm_password_invalid + '\n';
		}
		
		if (msg.length > 0)
		{
			pwdMsgObj.html(msg);
			pwdMsgObj.removeClass();
			pwdMsgObj.addClass("errorregmessage");
		}
		else
		{
			pwdMsgObj.html('');
			pwdMsgObj.removeClass();
			pwdMsgObj.addClass("successregmessage");
		}
		
	});
	
	var mobileMsgObj = $("#msg_mobile");
	$("#txtmobile").blur(function(){
		var mobile = $.trim($(this).val());
		
		var msg = '';
		if (mobile.length == 0)
		{
			msg += mobile_empty + '\n';
		}
		else
		{
			if ( ! (Utils.isMobile(mobile)))
			{
			  msg += mobile_invalid + '\n';
			}
			
		}
		
		if (msg.length > 0)
		{
			mobileMsgObj.html(msg);
			mobileMsgObj.removeClass();
			mobileMsgObj.addClass("errorregmessage");
		}
		else
		{
			var args = {ac:'submitregmobile', mobile: mobile};
			$.ajax({
				 type: "POST",
				 url: u,
				 data:args,
				 dataType: "json",
				 success: returncallback
			});
		}
		
	});
	
	/*var emailMsgObj = $("#msg_email");
	$("#txtemail").blur(function(){
		var email = $.trim($(this).val());
		
		var msg = '';
		if (email.length == 0)
		{
			msg += email_empty + '\n';
		}
		else
		{
			if ( ! (Utils.isEmail(email)))
			{
			  msg += email_invalid + '\n';
			}
		}	
		
		if (msg.length > 0)
		{
			emailMsgObj.html(msg);
			emailMsgObj.removeClass();
			emailMsgObj.addClass("errorregmessage");
		}
		else
		{
			var args = {ac:'submitregemail', email: email};
			$.ajax({
				 type: "POST",
				 url: u,
				 data:args,
				 dataType: "json",
				 success: returncallback
			});
		}
	});*/
	
	

	function treatmentpayinfo(result)
	{
		copytopayinfo(result.content);
		var payinfoObj = $("#payinfodiv");
		payinfoObj.css({"display": 'block', "top": wtop, "left": wleft});
		clear_consultation_form();
	}
	
	function copytopayinfo(itm)
	{
		var consultationareaid = '['+ consultationData.province +'-'+ consultationData.city +']';
		$("#consultationtitleid").html(consultationData.title);
		$("#consultationareaid").html(consultationareaid);
		$("#consultationcontentid").html(consultationData.content);
		$("#consultationcategoryid").html(itm.category_name);
		$("#paypointid").html(itm.pay_point);
		$("#consultationidval").text(itm.consultation_id);
		$("#balancepointid").html(itm.balance_point);
		$("#consultationtypeval").html(itm.cons_type);
	}
	
	function clear_consultation_form()
	{
		$("#contitle").val('');
		$("#kw").val('');
	}
	
	function register()
	{
	  var frm  = document.forms['formUser'];
	  var username  = Utils.trim(frm.elements['txtuserno'].value);
	  //var email  = frm.elements['txtemail'].value;
	  var mobile  = frm.elements['txtmobile'].value;
	  var password  = Utils.trim(frm.elements['txtuserpwd'].value);
	  var confirm_password = Utils.trim(frm.elements['txtuserconfirmpwd'].value);
	  var checked_agreement = frm.elements['cb_reg_agreement'].checked;
	
	
	  var msg = "";
	  // 检查输入
	  var msg = '';
	  if (username.length == 0)
	  {
		msg += username_empty + '\n';
	  }
	  else if (username.match(/^\s*$|^c:\\con\\con$|[%,\'\*\"\s\t\<\>\&\\]/))
	  {
		msg += username_invalid + '\n';
	  }
	  else if (username.length < 3)
	  {
		//msg += username_shorter + '\n';
	  }
	
	  /*if (email.length == 0)
	  {
		msg += email_empty + '\n';
	  }
	  else
	  {
		if ( ! (Utils.isEmail(email)))
		{
		  msg += email_invalid + '\n';
		}
	  }*/
	  
	  if (mobile.length == 0)
	  {
		msg += mobile_empty + '\n';
	  }
	  
	  if (password.length == 0)
	  {
		msg += password_empty + '\n';
	  }
	  else if (password.length < 6)
	  {
		msg += password_shorter + '\n';
	  }
	  if (confirm_password != password )
	  {
		msg += confirm_password_invalid + '\n';
	  }
	  if(checked_agreement != true)
	  {
		msg += agreement + '\n';
	  }
	  
	/*
	  if (msn.length > 0 && (!Utils.isEmail(msn)))
	  {
		msg += msn_invalid + '\n';
	  }
	
	  if (qq.length > 0 && (!Utils.isNumber(qq)))
	  {
		msg += qq_invalid + '\n';
	  }
	
	  if (office_phone.length>0)
	  {
		var reg = /^[\d|\-|\s]+$/;
		if (!reg.test(office_phone))
		{
		  msg += office_phone_invalid + '\n';
		}
	  }
	  if (home_phone.length>0)
	  {
		var reg = /^[\d|\-|\s]+$/;
	
		if (!reg.test(home_phone))
		{
		  msg += home_phone_invalid + '\n';
		}
	  }
	  if (mobile_phone.length>0)
	  {
		var reg = /^[\d|\-|\s]+$/;
		if (!reg.test(mobile_phone))
		{
		  msg += mobile_phone_invalid + '\n';
		}
	  }*/
	  
	  if (msg.length > 0)
	  {
		
		return false;
	  }
	  else
	  {
		return true;
	  }
	}
	
	returncallback = function(res)
	{
		var type = res.type;
		
		if (type == '1')	// 咨询回调
		{
			var ifca = res.message;
			
			if(ifca == '1')
			{
				//loadObj.css({"display": 'none'});
				var loginObj = $("#logindiv");
				icate = 1;
				loginObj.css({"display": 'block', "top": wtop, "left": wleft});
			}
			else if (ifca == '2')
			{				
				/*loadObj.animate({
					opacity: 0
				},500);
				loadObj.css({"display": 'none'});*/
				treatmentpayinfo(res);
				
			}
		}
		else if (type == '2')	// 登录回调
		{
			if (res.error == 1)
			 {
				var loginingMsgObj = $("#loginingmessage");
				loginingMsgObj.html(res.message); 
				loginingMsgObj.removeClass();
				loginingMsgObj.addClass("errorregmessage");
			 }
			 else
			 {
				if (icate == 1)
				{
					$.ajax({
						 type: "POST",
						 url: u,
						 data:consultationData,
						 dataType: "json",
						 success: function(rest){
							 treatmentpayinfo(rest);
						 }
					});
				}
				
				var mbinfoObj = $(".header_top_right"); 
				mbinfoObj.html(res.content);
				var loginObj = $("#logindiv");
				loginObj.css({"display": "none"});	
			 }
		}
		else if (type == '3')
		{
			if (res.error == 0)
			{
				msg = res.content;
				emailMsgObj.html(msg);
				emailMsgObj.removeClass();
				emailMsgObj.addClass("successregmessage");
			}else{
				msg = res.message;
				emailMsgObj.html(msg);
				emailMsgObj.removeClass();
				emailMsgObj.addClass("errorregmessage");
			}	
		}
		
		else if (type == '301')
		{
			if (res.error == 0)
			{
				msg = res.content;
				mobileMsgObj.html(msg);
				mobileMsgObj.removeClass();
				mobileMsgObj.addClass("successregmessage");
			}else{
				msg = res.message;
				mobileMsgObj.html(msg);
				mobileMsgObj.removeClass();
				mobileMsgObj.addClass("errorregmessage");
			}	
		}
		else if (type == '4')
		{
			
			if (res.error == 0)
			{
				msg = res.content;
				usernameMsgObj.html(msg);
				usernameMsgObj.removeClass();
				usernameMsgObj.addClass("successregmessage");
			}else{
				msg = res.message;
				usernameMsgObj.html(msg);
				usernameMsgObj.removeClass();
				usernameMsgObj.addClass("errorregmessage");
			}
		}
		else if (type == '5')
		{
			if (res.error == 1)
			{
				alert(res.message);
			}
			else{
				var retdata = res.content;
				var loginObj = $("#reguserdiv");
				loginObj.css({"display": "none"});
				
				var seconds = 3;
				var regsuccessObj = $("#regsuccessdiv");
				$("#regusernamesuccessMsg").html(retdata.reg_success);
				regsuccessObj.css({"display": 'block', "top": wtop, "left": wleft});
				window.setInterval(redirection, 1000);
				function redirection()
				{
					if (seconds <= 0)
					{
						window.clearInterval();
						return;
					}
					seconds --;
					$("#spanSeconds").html(seconds);
					if (seconds == 0)
					{
						window.clearInterval();
						regsuccessObj.css({"display": 'none'});
						var mbinfoObj = $(".header_top_right"); 
						mbinfoObj.html(retdata.member_info);
						su_click();
						//window.location.href = 'user.php';
					}
				}
				
			}
		}
		
		
	}
	
	
	
});



