user.js 5.15 KB
/**
 * 
 */

QUser = {
	oldBoxId : 1,
	init:function(year, monthon, day)
	{
		this.crateDay(year, monthon, day);
		$("#box_2").hide();
		$("#box_3").hide();
		$("#box_4").hide();
	},
	changeMonth : function() {
		var year = $("#year").val();
		var month = $("#month").val();
		var day = $("#day").val();
		this.crateDay(year, month, day);
	},
	changeYear : function() {
		var year = $("#year").val();
		var month = $("#month").val();
		var day = $("#day").val();
		this.crateDay(year, month, day);
	},
	crateDay : function(year, month, day) {
		if(parseInt(year) == 0)
		{
			year = 0;
			
		}
		if(parseInt(month) == 0)
		{
			month = 0;
		}
		$("#year").val(year);
		$("#month").val(month);
		
		var html = '<select name="day" id="day">';
		html += '<option value="0">日</option>';
		if(year > 0 && month > 0){
			var monthArray = new Array(4, 6, 9, 11);
			var dayNum = 31;
			if (jQuery.inArray(month, monthArray) != -1) {
				dayNum = 30;
			} else if (month == 2 ) {
				dayNum = 28;
				if(0 == year % 4 && (year % 100 != 0 || year % 400 == 0))
				{
					dayNum = 29;
				}
			}
			
			for ( var i = 1; i <= dayNum; i++) {
				var select = '';
				if (parseInt(day) == i) {
					select = 'selected';
				}
				html += '<option value="' + i + '" ' + select + '>' + i	+ '日</option>';
			}
		}
		html += '</select>';
		$("#day").replaceWith(html);
	},
	showBox: function(id)
	{
		if(parseInt(id) == 0 || parseInt(this.oldBoxId) == 0)
		{
			return false;
		}
		$("#box_" + id).slideDown('slow');
		$("#box_" + this.oldBoxId).hide();
		this.oldBoxId = id;
		if(id == 2)
		{
			this.checkContactForm();
		}else if(id == 1)
		{
			this.checkBaseForm();
		}
	},
	save: function (postUrl,formId, box_id){
		if(typeof(postUrl) == 'undefined' || postUrl == '' || typeof(formId) == 'undefined' || formId == '')
		{
			alert('参数错误');
			return false;
		}
		$.post(postUrl,$('#'+formId).serialize(), 
			function(data){
				if(data.code == 200){
					alert(data.message);
					if(parseInt(box_id) != 0)
					{
						QUser.showBox(box_id);
					}
					return true;
				}
				alert(data.message);
				return false;
			}, 'json');
	},
	delBrand: function(brand_id, flag)
	{
		if(parseInt(brand_id) == 0)
		{
			return false;
		}
		var brand = $("#likebrand").val();
		$("#likebrand").val(brand.replace(','+brand_id+',', ','));
		$("#pp_" + brand_id).attr('checked', false);
		$("#brand_" + brand_id).remove();
	},
	addBrand: function(obj, brand_id, brand_name)
	{
		if(parseInt(brand_id) == 0)
		{
			return false;
		}
		var brandInfo = $('#brand_' + brand_id).html();
		if(typeof obj != 'undefined' && obj != '' && obj.checked == false)
		{
			this.delBrand(brand_id);
			return false;
		}
		if(brandInfo != null)
		{
			alert('您已经选择了该品牌.');
			return false;
		}
		var html = '<li id="brand_'+brand_id+'">'+ brand_name +' <a href="javascript:void(0);" onclick="QUser.delBrand('+brand_id+', this);" class="btn_del"></a></li>';
		$("#brandBox").append(html);
		var brandStr = $("#likebrand").val();
		brandStr += brand_id + ',';
		$("#likebrand").val(brandStr);
	},
	checkBrand: function()
	{
		$.post('/home/user/checkbrand',{keywords:$('#keywords').val()}, 
			function(data){
				if(data.code == 200){
					QUser.addBrand('',data.data.id, data.data.brand_name);
					$('#keywords').val('');
					return true;
				}
				alert(data.message);
				return false;
			}, 'json');
	},
	checkBaseForm: function()
	{
		return false;
		jQuery.formValidator.initConfig({
			formid : "userBaseInfo"
		});
		jQuery("#nickname").formValidator({
			onshow : "请填写昵称",
			onfocus : "昵称必须大于2而小于20个字符",
			oncorrect : "昵称输入正确"
		}).inputValidator({
			min : 2,
			max : 20,
			onerror : "您输入的昵称不正确"
		});
		jQuery("#user_name").formValidator({
			onshow : "请填写申请人的真实姓名",
			onfocus : "真实姓名只能为中文",
			oncorrect : "输入正确"
		}).regexValidator({
			regexp : "^[\u4e00-\u9fa5]{2,5}$",
			onerror : "真实姓名至少2个中文,最多5个中文"
		});
	},
	checkContactForm: function()
	{
		return false;
		jQuery.formValidator.initConfig({
			formid : "userContactInfo"
		});
		$('#mobile').formValidator({
			onshow : "填写手机号便于接收发货和收货通知",
			onfocus : "填写手机号便于接收发货和收货通知",
			oncorrect : "输入正确"
		}).regexValidator({
			regexp : regexEnum.mobile,
			onerror : "您输入的手机号码格式不正确"
		});	
		$('#full_address').formValidator({
			onshow : "请填写详细地址",
			onfocus : "请填写详细地址",
			oncorrect : "输入正确",
			onempty: "请填写详细地址",
			empty: false
		}).inputValidator({
			min : 10,
			onerror : "请填写详细地址"
		});
		//邮编
		$('#zip_code').formValidator({
			onshow : "请输入收货人所在地邮编号",
			onfocus : "请输入收货人所在地邮编号",
			oncorrect : "输入正确",
			onempty: "请输入收货人所在地邮编号",
			empty: false
		}).regexValidator({
			regexp : regexEnum.zipcode,
			onerror : "您输入的邮编式不正确"
		});
	},
	checkShoppingForm: function()
	{
		if($('#brandStr').val() == '')
		{
			alert('请添加您喜欢的品牌');
			return false;
		}
		return true;
	}
};