﻿// JavaScript Document

/*********************by lj**********************/
/**
 * 获取验证码
 */
 function refreshCheckCode(aeP, axConf){
	var leCheckCode = document.getElementById("CAPTCHAImage");
	if(leCheckCode){
		leCheckCode.src = getLocalInterface("captcha.ashx")+'?SecurityToken=&dl=11&fresh='+Math.random();
	}else{
		alert("ErrorLine(105);ID为'CAPTCHAImage'的元素不存在");
	}
 }
/**
 * 获得本域接口URL
 * 参数:asInterfaceURL接口名XXX.php?XX
 */
 function getLocalInterface(asInterfaceURL){
	var lsLocation = location.href;
	var liFirstIndex = lsLocation.indexOf("//");
	var liLastIndex = lsLocation.indexOf("/", liFirstIndex+2);
	var lsForstLoc = lsLocation.substring(liFirstIndex+2, liLastIndex);
	var lsUrl = "http://"+lsForstLoc+"/"+asInterfaceURL;
	return lsUrl;
 }

function formSubmitInPreview_lj(aeP, axConf){
	aeP.action = axConf;
	aeP.submit();
}
 
/**
 * 发表留言和预览的和form提交跳转
 */
 function formSubmit_lj(aeP, axConf, lsIsCheck, lsFrdId){
	var lxNode = aeP.getElementsByTagName("textarea");
	var leHidden = document.getElementById("receipantId");
	if(leHidden){
		leHidden.value = MO_MySpaceContext.ReceipantId || -1;
	}
	
	if(lxNode.length >0){
		var lsVal = lxNode[0].value;
		if(!/\S/.test(lsVal)){
			alert("请输入留言内容！");
			return false;
		}		
	}
	
	aeP.action = axConf;
	aeP.submit();
 /*
 	if(aeP.tagName == "FORM"){
	 	//判断是否需要验证码
	    var lsUrl = "http://ajax.myspace.cn/_Common/AjaxService/ProfileService.asmx/GetSendCommentStatus";
		var loRequest={"webservices": lsUrl,
		"contentType":"application/json",
		"values":CNMS.obj2Json({"friendId":lsFrdId})
		};
		CNMS.loadAjax(loRequest, callBack, "ajax");
 	}
 	
	function callBack(aoJson){
		if(aoJson.Result == 2){
			//需要验证则不跳转
			var leCheckCode = document.getElementById("checkCodeShow");
			if(leCheckCode){
				leCheckCode.style.display = "block";
			}
		}else{
			//不需要验证则直接发送跳转
	 		aeP.action = axConf;
	 		aeP.submit();
		}
	}
*/
 }

 /**
  * 预览页面发表留言
  */
 function sendMessage_lj(aeP, axConf, lsIsCheck, lsFrdId){
 	//判断是否需要验证码
    var lsUrl = "http://ajax.myspace.cn/_Common/AjaxService/profileservice.asmx/GetSendCommentStatus";
	var loRequest={"webservices": lsUrl,
	"contentType":"application/json",
	"values":CNMS.obj2Json({"friendId":lsFrdId})
	};
	CNMS.loadAjax(loRequest, callBack, "ajax");
	function callBack(aoJson){
		if(aoJson.Result == 2){
			var leCheckCode = document.getElementById("checkCodeShow");
			if(leCheckCode){
				leCheckCode.style.display = "block";
			}
		}else{
			location.href = aoJson.url;
		}
	}
 }
 /**
  * 删除提示
  */
 function deleteMsg_lj(aeP, axConf){
 	var lbIsDel = confirm("确实要删除这条留言吗?");
 	if(lbIsDel){
 		return true;
 	}else{
 		return void(0);
 	}
 }
 
 /**
  * 回复留言
  */
 function replyMsg_lj(aeP, lsFrdId){
	MO_MySpaceContext.ReceipantId = lsFrdId;
 	var leArea = document.getElementById("t_area");
 	if(leArea){
 		var lsHostName = aeP.parentNode.parentNode.getElementsByTagName("a")[0].getAttribute("title");
 		var leIsReply = document.getElementsByName("isReply");
 		if(leIsReply){
 			leIsReply[0].value = "true";
 		}
 		leArea.focus();
 		leArea.value = "回复"+lsHostName+"：";
//	 	//判断是否需要验证码
//	    var lsUrl = "http://ajax.myspace.cn/_Common/AjaxService/ProfileService.asmx/GetSendCommentStatus";
//		var loRequest={"webservices": lsUrl,
//		"contentType":"application/json",
//		"values":CNMS.obj2Json({"friendId":lsFrdId})
//		};
//		CNMS.loadAjax(loRequest, callBack, "ajax");
 		return false;
 	}else{
 		alert("ErrorLine(153);ID为't_area'的元素不存在");
 		return false;
 	}
 	return true;
//	function callBack(aoJson){
//		if(aoJson.Result == 2){
//			//回复留言时需要验证码
//			var leCheckCode = document.getElementById("checkCodeShow");
//			if(leCheckCode){
//				leCheckCode.style.display = "block";
//			}
//		}
//	}
 }
 
 
 function valiateUserRelation(){
	var lsService = "profileservice.asmx/GetSendCommentStatusForReply";
	var loRelationReq = {
		"ajaxservice": lsService,
		"json": {
			"receipantId":MO_MySpaceContext.DisplayFriendId,
			"type":"comment"
		}
	}
	var loResp = _api(loRelationReq);
	if(loResp.Result == 0){
		//ContactBox.canPostComment = true;
	}else{
		//ContactBox.canPostComment = false;		
		if(loResp.Result == 2){
			location.href = loResp.RedirectUrl;
		}else if(loResp.Result == 1){
			alert(loResp.Message);
		}
	}
 }

function imgSize(axImg,axWidth,aiHeight){
	if(axImg==null||axWidth==null) return;
	var lxImg;
	if(axImg.nodeType){
		lxImg = [axImg];
	}else{
		lxImg = axImg;
	}
	for(var i=0;i<lxImg.length;i++){
		var leImage = lxImg[i];
		var loImage = new Image();
		loImage.onload = action(leImage);
		loImage.src = leImage.src;
	}
	function action(aeNode){
		return function(){
			if(axWidth.length==1){
				var liWidth=axWidth[0];
				if(aeNode.width>liWidth){
					aeNode.style.height=Math.ceil(aeNode.height * liWidth/aeNode.width)+"px";
					aeNode.style.width=liWidth+"px";
				}
			}else if(axWidth.length==2){
				var liWidth;
				var liWidth0=axWidth[0];
				var liWidth1=axWidth[1];
				if(aeNode.width>liWidth1){
					liWidth=liWidth1;
				}else if(aeNode.width<aeNode.height&&aeNode.width>liWidth0){
					liWidth=liWidth1;
				}
				aeNode.style.height=Math.ceil(aeNode.height * liWidth/aeNode.width)+"px";
				aeNode.style.width=liWidth+"px";
			}
			if(aiHeight!=null){
				if(aeNode.height>aiHeight){
					aeNode.style.width=Math.ceil(aeNode.width * aiHeight/aeNode.height)+"px";
					aeNode.style.height=aiHeight+"px";
				}
			}		
		}
	}
}

function captchaInputSubmit(event){
	event = event || window.event;
	if(event.keyCode == 13){
		ajaxPostComment('ctl00_cpMain_postComment_Pnl_Captcha');
	}
}

function disableForm(asName){
	document.forms[asName].onsubmit = function(){
		return false;
	}
}


function ajaxPostComment(asCaptchaId){
 	var lsVal = document.getElementById("t_area").value;
	if(!/\S/.test(lsVal)){
		alert("请输入留言内容！");
		return false;
	}	
	var lsService = "profileservice.asmx/ProfilePostComment";
	var loRelationReq = {
		"ajaxservice": lsService,
		"json": {
			"msg":encodeURIComponent(lsVal.split("\r\n").join("<br />").split("\n").join("<br />")),
			"picUrl":document.getElementById("picurl").value,
			"receipantId":MO_MySpaceContext.DisplayFriendId,
			"type":"comment",
			"captcha":encodeURIComponent(document.getElementById("captchaInput").value)
		}
	}
	var loResp = _api(loRelationReq);
	//loResp = {"__type":"MySpace.Web.Services.Profile.CommentWrapper","Result":1,"Message":"不允许给该用户发评论","UserId":0,"DisplayName":"MyTest","OnLineNow":true,"PostDate":"2008年8月25日 17:31:00","CommentBody":"HelloWorld","UserImage":"http://b2.ac-images.cdnmyspace.cn/cnimages01/1/s_c57b217291f83dc70f09462256644b4c.png","CommentId":5555};
		
	if(loResp.Result == 0){		
		addComment(loResp);
		document.getElementById("t_area").value = "";
		document.getElementById("picurl").value = "";
		document.getElementById("prePicTotal").style.display = "none";		
		document.getElementById("insertLink").style.display = "block";		
		document.getElementById("prePicList").style.display = "none";
		document.getElementById("captchaInput").value = "";
	}else if(loResp.Result == 3){
		var leCaptcha = document.getElementById(asCaptchaId);
		document.getElementById("captchaInput").value = "";
		leCaptcha.style.display = "block";
		alert(loResp.Message);
	}else if(loResp.Result == 2){
		location.href = loResp.RedirectUrl;
	}else{
		alert(loResp.Message);
	}
	refreshCheckCode();
}
 function hideInsertPic(asCaptchaId){
 	var leInsertPic = document.getElementById(asCaptchaId);
 	if(leInsertPic){
	 	leInsertPic.style.display = "none"; 		
 	}
 }

function addComment(aoUserData){
	var lsId = aoUserData.UserId;
	var lsImage = aoUserData.UserImage;
	var lsDate = aoUserData.PostDate;
	var lsContent = aoUserData.CommentBody;
	var lsName = aoUserData.DisplayName;
	var lsHTML = ['	<a href="http://www.myspace.cn/',lsId,'" class="padPic padPicReSize"><img onload="imgSize(this,[60],60)" src="',lsImage,'" style="height: 60px; width: 60px;"/></a>',
				'	<dl class="padDescList">',
				'		<dt class="padTitle"><a title="',lsName,'" href="">',lsName,'</a><cite>',lsDate,'</cite></dt>',
				'		<dd>',lsContent,'</dd>',
				'		<dd class="editMsgFun_lj clearfix"><a href="javascript:void(0)" onclick="deleteCommentInAll(this,',aoUserData.CommentId,')" class="dele_lj">删除</a></dd>',
				'	</dl>'].join("");
	var leNode = document.createElement("div");
	leNode.className = "picAndDesc clearfix";
	leNode.setAttribute("uid",lsId);
	leNode.setAttribute("cid",aoUserData.CommentId);
	leNode.setAttribute("id","comment_item_"+aoUserData.CommentId);
	leNode.innerHTML = lsHTML;
	
	var leMsgList = document.getElementById("msgShowList");
	if(leMsgList){
		leMsgList.insertBefore(leNode,leMsgList.firstChild);
	}
}

function deleteCommentInAll(aeNode,asCid){
	
	if(!confirm("您确定要删除该条留言吗？")){
		return false;
	}	
	var leParent = aeNode.parentNode;
	if(asCid){
		leParent=document.getElementById("comment_item_"+asCid);;
	}
 	var lsCid = leParent.getAttribute("cid");
 	var lsUid = leParent.getAttribute("uid");
 	var lsService = "profileservice.asmx/DeleteComment";
	var loRelationReq = {
		"ajaxservice": lsService,
		"json": {
			"commentId":parseInt(lsCid),
			"userId":parseInt(lsUid),
			"displayFriendId":MO_MySpaceContext.DisplayFriendId
		}
	}
	var loResp = _api(loRelationReq);
	if(loResp.Result == 0){
		leParent.style.display = "none";
	}else if(loResp.Result == 2){
		location.href = loResp.RedirectUrl;
	}else{
		alert(loResp.Message);
	}		
	
	
}
 
 function goBack(){
 	if(navigator.userAgent.indexOf("IE")!=-1){
 		history.back();
 	}else{
	 	history.go(-1); 		
 	}
 	return false;

 }
 
 function focusTextarea(){
 	document.getElementById("t_area").focus();
 }