/*用户登陆*/
function loginCheck()
{
	if($v('try_username') == '')
	{
		alert('请输入登录用户名！');
		$('try_username').focus();
		return false;
	}
	if($v('try_password') == '')
	{
		alert('请输入密码！');
		$('try_password').focus();
		return false;
	}
}

/*邀请好友注册为试用俱乐部*/
function InviterCopy(try_host,guid,type,try_id,try_name)
{
	if(type == 1)//试用中心首页
	{
		text = "我邀请您加入YOKA试用中心，申请成试用会员后能免费试用各种名牌化妆品哦 "+try_host+"/join_club.php?guid="+guid+"&type=";
	}
	else if (type == 2)//单个试用页面
	{
		text = "“"+try_name+"”正在免费试用哦，我已经申请了，你也来吧 "+try_host+"/try.php?try_id="+try_id+"&guid="+guid+"&type=";
	}
	else if(type == 3)//试用报告的分享
	{
		text = "我刚刚在YOKA试用了“"+try_name+"”，正在免费试用哦，看看我写的试用报告吧，或许对你有用哦！ "+try_host+"/join_club.php?guid="+guid+"&try_id="+try_id+"&type=";
	}
	copy_text(text);
	alert("链接地址已经复制到剪贴板上，您可以粘贴到QQ,MSN等发送给您的好友。");
}

/****************试用页面相关函数**********************/
function get_category_list(brand_id,category_id)
{
	var url = '/admin/try_product_category.php';
	var param = 'action=getCategorySelList&brand_id='+brand_id+'&category_id='+category_id;
	var ajax = new myajax(url, param, getCategoryValue);
	ajax.get();
}

function getCategoryValue(text)
{
	$html('category_list',text);
}

function get_product_list(brand_id,catalog_id)
{
	
	var url = '/admin/try_product_category.php';
	var param = 'action=getProductList&brand_id='+brand_id+'&catalog_id='+catalog_id;
	var ajax = new myajax(url, param, getProductValue);
	ajax.get();
}

function getProductValue(text)
{
	$html('product_list',text);
}

function applyCheck()
{
	//if(!check_empty(apply.category_first_id,"请选择要点评的品牌分类！")) return false;
	//if(!check_empty(apply.category_id,"请选择要点评的产品分类！")) return false;
	if($v('prod_id') == '')
	{
		alert('请选择要点评的产品！');
		return false;
	}
	if($v('apply_title') == '')
	{
		alert('请填写评论标题！');
		return false;
	}
	if($v('apply_content') == '')
	{
		alert('请填写评论内容！');
		return false;
	}
}


/***********************试用报告相关函数*****************************/
function changeOpinion(try_id)
{
	if(try_id == 0) return;
	var url = '/try_ajax.php';
	var param = 'action=getOpinionList&try_id='+try_id;
	var ajax = new myajax(url, param, getOpinionValue);
	ajax.get();
}

function getOpinionValue(text)
{
	$html('opinion_list',text);
}

function setProdList(try_id)
{
	if(try_id == 0) return;
	var url = '/try_ajax.php';
	var param = 'action=getProdList&try_id='+try_id;
	var ajax = new myajax(url, param, function(text){$html('prod_list',text)});
	ajax.get();
}

function checkGetPrize(try_id,user_id)
{
	if(try_id == 0) return;
	var url = '/try_ajax.php';
	var param = 'action=checkGetPrize&try_id='+try_id+'&user_id='+user_id;
	var ajax = new myajax(url, param, prizeAlert);
	ajax.get();
}
function prizeAlert(text)
{
	if(text != '')
	{
		alert(text);
	}
}

/*设置选择的项*/
function setItemValue(element,val)
{
	$html('op_'+element,val);
	$v('h_'+element,val);
}
/*设置选择项的值*/
function setInputValue(element,val)
{
	opinion = $('prod_opinions').value;
	if(opinion != '')
	{
		var aa,bb,cc='',dd='';
		aa = opinion.split(',');
		for(i=0;i<aa.length-1;i++)
		{
			bb = aa[i].split(':');
			if(bb[0] == element)//有相同情况
			{
				cc = bb[0]+':'+val+',';
			}
			else//没有相同情况
			{
				dd += aa[i]+',';
			}
		}
		if(cc == '') cc = element+':'+val+',';
		$v('prod_opinions',cc+dd);
	}
	else
	{
		$v('prod_opinions',element+':'+val+',');
	}
}


function check_report(type)
{
	if (type == '')
	{
		if($('try_id').value == '')
		{
			alert("请选择试用名称！");
			$('try_id').focus();
			return false;
		}
		if($('prod_id').value == 0)
		{
			alert("请选择试用的产品！");
			$('prod_id').focus();
			return false;
		}
		opinion_count = $v('opinion_count','');
		var op = '';
		for(i=1;i<=opinion_count;i++)
		{
			if($v('h_'+i)== '')
			{
				alert("请选择每一项评价！");
				return false;
			}
		}
		report_word_num = $v('report_word_num');
	}
	else if(type == 'edit')
	{
		report_word_num = $v('edit_report_word_num');
	}
	if($('report_title').value == '')
	{
		alert("请填写试用报告标题！");
		$('report_title').focus();
		return false;
	}
	var oEditor = FCKeditorAPI.GetInstance("report_content");
	var TextContents = oEditor.EditorDocument.body.innerText;
	if(TextContents == '') {
		alert("请填写试用报告内容！");
		return false;
	}
	
	//Unicode长度为2，非Unicode长度为1
	var iLength = 0;
	for(var i = 0;i<TextContents.trim().length;i++)
	{
		/*
		if(TextContents.charCodeAt(i) >255)
		{
			iLength += 2;
		}
		else
		{
			iLength += 1;
		}
		*/
		iLength += 1;
	}

	if(iLength < report_word_num) {
		alert("试用报告字数不少于"+report_word_num+"字");
		return false;
	}
}

function check_join()
{
	val = $('true_name').value;
	for(i=0;i<val.length;i++)
	{
		n = val.charCodeAt(i);
		if(n < 19968 || n > 40869) // 不是汉字
		{
			alert('真实姓名中不能含有字母，数字和特殊字符！');
			$('true_name').focus();
			return false;
		}
	}
	len = val.replace(/[^\x00-\xff]/g,"**").length;
	if(len < 4 || len > 12)
	{
		alert('姓名不符合要求，2-6个汉字之间！');
		$('true_name').focus();
		return false;
	}
	
	if($('marriage').value == 0)
	{
		alert('请选择婚姻状态！');
		$('marriage').focus();
		return false;
	}
	if($('education').value == 0)
	{
		alert('请选择教育程度！');
		$('education').focus();
		return false;
	}

	if($('location').value == 0)
	{
		alert('请选择所在省份！');
		$('location').focus();
		return false;
	}

	if($('sublocation').value == 0)
	{
		alert('请选择所在地区！');
		$('sublocation').focus();
		return false;
	}

	if($('income').value == 0)
	{
		alert('请选择月薪！');
		$('income').focus();
		return false;
	}

	if($('address').value == '')
	{
		alert('请填写通信地址！');
		$('address').focus();
		return false;
	}

	if($('address').value.length < 5)
	{
		alert('请填写正确的通信地址！');
		$('address').focus();
		return false;
	}
	
	if($('postalcode').value == '')
	{
		alert('请填写邮编地址！');
		$('postalcode').focus();
		return false;
	}

	if($('postalcode').value != '')
	{
		_reg = /^\d{6}$/;
		result = _reg.test($('postalcode').value)
		if(result == false)
		{
			alert("邮编输入不正确，只能为6位数字！");
			$('postalcode').focus();
			return false;
		}
	}

	if($('phone').value == '' && $('mobile').value == '')
	{
		alert("联系电话和手机至少填写一项");
		$('phone').focus();
		return false;
	}

	if($('phone').value != '')
	{
		_reg = /^(\d{3,4}-)?\d{6,9}(-\d{1,5})?$/;
		result = _reg.test($('phone').value)
		if(result == false)
		{
			alert("联系电话输入不正确，请参照输入框后面的格式！");
			$('phone').focus();
			return false;
		}
	}

	if($('mobile').value != '')
	{
		_reg = /^\d{11}$/;
		result = _reg.test($('mobile').value)
		if(result == false)
		{
			alert("手机号码输入不正确，只能为11位数字！");
			$('mobile').focus();
			return false;
		}
	}

	if($('qq').value != '')
	{
		_reg = /^\d{2,12}$/;
		result = _reg.test($('qq').value)
		if(result == false)
		{
			alert("QQ输入不正确，只能为2-12位数字！");
			$('qq').focus();
			return false;
		}
	}

	if($('msn').value != '')
	{
		val = $('msn').value;
		var reg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,3}){1,2})$/;
		if(!reg.test(val))
		{
			alert('您输入的MSN格式不正确！');
			$('msn').focus();
			return false;
		}
	}

	if($('skin_type').value == 0)
	{
		alert("请选择您的肤质！");
		$('skin_type').focus();
		return false;
	}
	
	if($('hair_type').value == 0)
	{
		alert("请选择您的发质！");
		$('hair_type').focus();
		return false;
	}

	if($('prink_custom').value == 0)
	{
		alert("请选择您的妆容习惯！");
		$('prink_custom').focus();
		return false;
	}

	if(!check_rep('brand_loc','请选择您的品牌喜好！')) return false;

	if($('brand_prink_text').value == 0)
	{
		alert("请选择您的喜爱的化妆品品牌！");
		$('brand_prink_text').focus();
		return false;
	}
	
	if(!check_rep('product','请选择您的产品喜好！')) return false;

	if($('buy_mode').value == 0)
	{
		alert("请选择您的化妆品购买方式！");
		$('buy_mode').focus();
		return false;
	}

	if($('buy_mode').value == 0)
	{
		alert("请选择您的化妆品购买方式！");
		$('buy_mode').focus();
		return false;
	}

	if($('beauty_month_consume').value == 0)
	{
		alert("请选择您的美容月消费！");
		$('beauty_month_consume').focus();
		return false;
	}

	if($('nurse_mode').value == 0)
	{
		alert("请选择您的日常护理方式！");
		$('nurse_mode').focus();
		return false;
	}

	if($('newproduct_mode').value == 0)
	{
		alert("请选择您得知美妆新品的方式！");
		$('newproduct_mode').focus();
		return false;
	}
	return true;
}

function check_rep(type,msg)
{	
	var oEvent = $('ul_' + type);
	var chks = oEvent.getElementsByTagName("INPUT");
	var ch="";
	for(var i=0; i<chks.length;i++)
	{
		if(chks[i].type=="checkbox" && chks[i].checked == true)
		{
			ch=ch+chks[i].value;
		}
	}
	if(ch=="")
	{
		alert(msg);
		return false;
	}
	return true;
}
