// Provide a default path to dwr.engine
if (dwr == null) var dwr = {};
if (dwr.engine == null) dwr.engine = {};
if (DWREngine == null) var DWREngine = dwr.engine;

if (ChrismasBean == null) var ChrismasBean = {};
ChrismasBean._path = '/activities/dwr';
ChrismasBean.init = function(callback) {
  dwr.engine._execute(ChrismasBean._path, 'ChrismasBean', 'init', callback);
}
ChrismasBean.addHeartWordItem = function(p0, p1, callback) {
  dwr.engine._execute(ChrismasBean._path, 'ChrismasBean', 'addHeartWordItem', p0, p1, false, callback);
}
ChrismasBean.getPrize = function(p0, callback) {
  dwr.engine._execute(ChrismasBean._path, 'ChrismasBean', 'getPrize', p0, false, callback);
}
ChrismasBean.addPrizePrivacy = function(p0, callback) {
  dwr.engine._execute(ChrismasBean._path, 'ChrismasBean', 'addPrizePrivacy', p0, callback);
}


//AboutMeBean操作Bean的定义
if (AboutMeBean == null) var AboutMeBean = {};
AboutMeBean._path = 'http://blog.163.com/'+currentUser.userName+'/dwr';
AboutMeBean.init = function(callback) {
  dwr.engine._execute(AboutMeBean._path, 'AboutMeBean', 'init', callback);
}

AboutMeBean.updateAboutMe = function(p0, p1, callback) {
  dwr.engine._execute(AboutMeBean._path, 'AboutMeBean', 'updateAboutMe', p0, p1, callback);
}

/**
			resultCode如下 
			 1:一等奖
			 2:二等奖
			 3:三等奖
			 4:幸运奖
			 -1:没有奖
			 -2:今天已经超过次数了
			 -3：资料没有填
			 -4:系统抽奖配置信息有误
*/
function getResult(){
	if(currentUser.userId == 0 || currentUser.userName == null || currentUser.userName==""){
		login();
		return false;
	}else{
		ChrismasBean.getPrize(currentUser.userName,postGetPrize);
	}
}
var prizeLevel = -1;
/**中奖回调函数*/
function postGetPrize(result){
	if(result==-3){
		showAboutWindow();
	}else if(result==-4){
		alert("暂时不能咂蛋哦，请稍后再试");
	}else if(result>0 && result<6){		
		prizeLevel = result;
		showInfoWindow();
	}
	//填写中奖信息
	document.getElementById("eggFlash").showResult(result);
}
function getBoolAddToBlog(){
	return $("to_blog").checked;	
}
function quickShow(){
	
	$("divQuickShow").style.display='';
	if ($("lastWish")) $("lastWish").style.display = 'none';
	$("divQuickShow_message").innerHTML=getTrimValue("wish");
	if(currentUser.nickName == null || currentUser.nickName=="")
		$("divQuickShow_name").innerHTML=currentUser.userName;
	else
		$("divQuickShow_name").innerHTML=currentUser.nickName;
	$("divQuickShow_url").href="http://" + currentUser.userName + ".blog.163.com/";
	$("wish").value="";
}
/**添加圣诞心愿*/
function getHeartWord(){	
	var item ={
		"userId":currentUser.userId,//发送者的userId
		"userName":currentUser.userName,
		"nickName":currentUser.nickName,//发送者的昵称
		"message":getTrimValue("wish")//发送的心愿	
	};	
	
	if(item["userId"]==""){
		effectUtils.fadeTipInfo('msgInfo',"你未登陆！",10);
		return null; 
	}	
		
	if(item["message"]==""){
		effectUtils.fadeTipInfo('msgInfo',"请填写圣诞心愿！",10);
		return null; 
	} else if (/[<>]/.test(item["message"])) {
		effectUtils.fadeTipInfo('msgInfo',"心愿中不能包含<>字符！",10);
		return null; 
	}
	if(item["message"].length < 5){
		effectUtils.fadeTipInfo('msgInfo',"心愿内容不能小于5个字符！",10);
		return null; 
	}	
	
	if (isValidInput(item["message"],"心愿",0,200)==false) return null;		
		
	return item;
}

var $_dwrProcessing = false;
function addHeartWord() {	
	if ($_dwrProcessing) {
		effectUtils.fadeTipInfo('msgInfo',"请稍候....",10000);
		return;
	}
	var item = getHeartWord();
	//设置是否添加到博客
	if (item==null) return;
	var boolAddToBlog = getBoolAddToBlog();	
	effectUtils.fadeTipInfo('msgInfo',"正在提交，请稍候....",10000);
	$_dwrProcessing = true;
	ChrismasBean.addHeartWordItem(item,boolAddToBlog,postAddHeartWordItem);
}
//完成后的回调函数
function postAddHeartWordItem(returnData) {
	$_dwrProcessing = false;
	if (returnData == -9) {
		effectUtils.fadeTipInfo('msgInfo',"对不起，您的心愿中含有敏感词，我们无法为您发布！",10000);		
	}else if (returnData == 0) {
		effectUtils.fadeTipInfo('msgInfo',"服务器错误，请稍候再试！",10000);						
	}else if (returnData == -3) {
		effectUtils.fadeTipInfo('msgInfo',"不要太贪心哦！今天已经发了10条心愿了，明天再来许愿吧！",10000);						
	}else if (returnData == -1) {
		effectUtils.fadeTipInfo('msgInfo',"你的心愿填写不完整",10000);
	}else if (returnData == -2) {
		effectUtils.fadeTipInfo('msgInfo',"发送心愿成功，添加到博客失败",10000);
		quickShow();
	}else if (returnData == 1) {
		effectUtils.fadeTipInfo('msgInfo',"成功许下心愿",1000);
		quickShow();
	}else{
		effectUtils.fadeTipInfo('msgInfo',"成功许下心愿，并添加到您的博客",1000);	
		quickShow();
	}	
}

function ismaxlength(obj){
	var mlength=obj.getAttribute? parseInt(obj.getAttribute("maxlength")) : "";
	if (obj.getAttribute && obj.value.length>mlength){
		alert("最多输入"+mlength+"个字");
		obj.value=obj.value.substring(0,mlength);
	}
}

jsWindowManager = (jsWindowManager == undefined) ? new NetEase.JSWindowManager() : jsWindowManager;

// 领奖信息
var g_InfoWindow = jsWindowManager.createWindow("aboutWindow", {
	notKeepPos : true,
	noUD : true,
	width : 500,
	height : 280,
	title : '填写获奖联系方式',
	onTop : false,
	needCover : true,
	allowScroll : false,
	opacity : 0
});
g_InfoWindow.panel.innerHTML = (typeof g_InfoWindowHtml != "undefined") ? g_InfoWindowHtml : "";

function showInfoWindow() {
	if (g_InfoWindow) {
		g_InfoWindow.showWindow();
	}
}
function getPrivacy(){
	var _privacy = {
		"username":currentUser.userName,
		"prizeLevel":prizeLevel,
		"realname":getTrimValue("realname"),
		"contact":getTrimValue("contact"),
		"mail":getTrimValue("mail"),
		"address":getTrimValue("address"),
		"zip":getTrimValue("zip")
		};
	return _privacy;	
}
function submitPrivacy(){
	var privacy = getPrivacy();
	if(privacy == null)
		return ;
	ChrismasBean.addPrizePrivacy(privacy,postAddPrizePrivacy);	
}
function postAddPrizePrivacy(returnData){
	alert("谢谢您的参与，信息提交成功！");
	g_InfoWindow && g_InfoWindow.hiddenWindow();
}
// 个人信息
var g_AboutWindow = jsWindowManager.createWindow("aboutWindow", {
	notKeepPos : true,
	noUD : true,
	width : 700,
	height : 390,
	title : '填写个人信息',
	onTop : false,
	needCover : true,
	allowScroll : false,
	opacity : 0
});
g_AboutWindow.panel.innerHTML = (typeof g_AboutWindowHtml != "undefined") ? g_AboutWindowHtml : "";

function showAboutWindow() {
	if (g_AboutWindow) {
		$("submithint").innerHTML = ""; 
		$("submitbutton").disabled = false;
		g_AboutWindow.showWindow();
		if (showAboutWindow.scriptExecuted !== true) {
			g_AboutWindowCallBack && g_AboutWindowCallBack();
			showAboutWindow.scriptExecuted = true;
		}
	}
}

// wrapper for showInfo in editProfile.js:submitProfile
function showInfo(_id, _msg, _level) {
	effectUtils.fadeTipInfo(_id, _msg, 1000);	
}

function getAboutMe(){
	if($("gender1").checked==false && $("gender2").checked==false){
			showInfo("submithint", "性别没有设置", 1000);
			return ;
	}
	var birth = $("birthday");	
	if (birth != null) {
		var birthday = Trim(birth.value);
		if (!checkDateFormat(birthday)) {
			birthday='';
		}
		$("birthday").value = birthday;		
		if (birthday != '') {
			var res = checkBirthday(birthday);
			if (res == 1) {
				showInfo("submithint", "生日输入错误，不能迟于今天", 1000);
				return ;
			} else if (res == -1) {
				showInfo("submithint", "生日输入错误，不能早于1900年", 1000);
				return ;
			}
		}else{
			showInfo("submithint","请先填完“生日”信息，再提交！^_^",1000);
			return ;
		}
	}
	if($('eduType').value == 0){
		showInfo("submithint","请先填完“学历”信息，再提交！^_^",1000);
		return;	
	}
	if($('industry').value == 0){
		showInfo("submithint","请先填完“职业”信息，再提交！^_^",1000);
		return;
	}
	if($('marital').value == 'N'){
		showInfo("submithint","请先填完“婚姻状况”信息，再提交！^_^",1000);
		return;
	}
	$('hobbies').value = getCheckBoxsStr('hobbiesDisplay', hobbyArray); //兴趣爱好
	if($('hobbies').value == ''){
		showInfo("submithint","请先填完“兴趣爱好”信息，再提交！^_^",1000);	
		return;
	}
	var aboutMe = {
		"birthday":$("birthday").value, 
		"gender":$("gender1").checked?"M":"F",
		"education":getTrimValue("eduType"),
		"industry":getTrimValue("industry"),
		"occupation":getTrimValue("occupation"),
		"marital":getTrimValue("marital"),
		"favoriteThings":getTrimValue("hobbies")
	}
	return aboutMe;
}
// Simplified editProfile.js:submitProfile
var $_dwrProcessing_ = false;
function simpleSubmitProfile(){
	if($_dwrProcessing_ == true){
		$("submithint").innerHTML = "请稍候...";
		return;
	}	
	var aboutMe = getAboutMe();
	/*alert('\nbirth'+aboutMe['birthDate']+
		'\ngender'+aboutMe['gender']+
		'\neducation'+aboutMe['education']+
		'\nindustry'+aboutMe['industry']+
		'\noccupation'+aboutMe['occupation']+
		'\nmarital'+aboutMe['marital']+
		'\nfavoriteThings'+aboutMe['favoriteThings']);*/
	if(aboutMe == null)
		return;
	$_dwrProcessing_ = true;
	
  	$("submithint").innerHTML = "正在提交信息，请稍候..."; 
	$("submitbutton").disabled = true;
	AboutMeBean.updateAboutMe(currentUser.userId,aboutMe,utils.getCrossDomainCallBack(postUpdateAboutMe));
}

function postUpdateAboutMe(returnData){
	$_dwrProcessing_ = false;
	//alert("returnData:[" + returnData + "]");
	if(returnData==true){
		showInfo("submithint", "提交成功!", 1000);
		g_AboutWindow && g_AboutWindow.hiddenWindow();
		location.reload();
	}
	else{
		$("submithint").innerHTML = "提交失败!"; 
	}
}