﻿$(document).ready(function(){
				
       _init();
})


function _init(){
	
       $("a[@type]").click(function(){
									 
		       if(sdomain=="")
			   {
				  alert("此操作需要登录后执行，请先登录或注册。");
			   }
			   else
			   {
			      var type = $(this).attr("type");
				 
				  if(type=="friend")
				  {
					   addFriend();
				  }
				  else
				  {
					   window.location.href ='/'+sdomain+'/manage/scripsend/username/'+domain;
				  }
			   }
	   })	
}


//加为好友

function addFriend(){
	
															
       var height  = window.document.body.scrollHeight; 
	   
	   var top     = document.documentElement.scrollTop+200; 
	   
	   var left   = document.body.scrollWidth/2-250;
	   
	   var html;

	   if(sdomain=="")
	   {
		  alert("请你先登录，再发加为好友请求");
	   }
	   else if(sdomain==domain)
	   {
		  alert("自己不能加自己为好友");
	   }
	   else
	   {
	   html='<div id="scrip"></div><div class="scripcontent"><div><h3>加为好友</h3>';
	   
	   html+='<p>您要加『'+username+'』为好友吗？</p><h2><textarea>给对方留言...</textarea></h2>';
	   
	   html+='<h4><input type="button" value="加好友" /><input type="button" value="取消" /></h4></div></div>';
	   
	   $("body").append(html);
	   
	   $("#scrip").height(height);

	   $(".scripcontent").css("top",top);
	   
	   $(".scripcontent").css("left",left);

	   $(".scripcontent textarea").focus(function(){
	
	   $(this).val("");
	   
	   $(this).unbind("focus");
								 
        })

	    $(".scripcontent input:first").click(function(){
								
			 var content  =   $(".scripcontent textarea").val();	
			 
			 var len      =  calculate_byte(content);
			 
			 if(len>100)
			 {
				 alert("内容最多允许100个中文或 200个字符");
			 }
			 else
			 {
				 if(content=="给对方留言...")
				 {
					 content ="";
				 }					
		   <!-- start -->
				$.post(
			   "/login/friend",
			    {content:content,domain:domain},
				 function(msg)
					{
			
						if(msg=="refuse")
						{
							alert("自己不能加自己为好友");
					    }
						else if(msg=="login")
						{
						    alert("请你先登录，再发加为好友请求");
						}
						else if(msg==1)
						{
						    alert("好友邀请发送成功，请等待对方确认");
						   
						   $(".scripcontent input:last").click();
						}
						else if(msg=="wait")
						{
						    alert("您已经给TA发送过好友邀请啦，请等待对方确认");
						   
						    $(".scripcontent input:last").click();
						}
						else if(msg=="friend")
						{
						    alert("此用户已经是您的好友 ,无需再添加");
						   
						    $(".scripcontent input:last").click();
						}
					}
				);
	       <!-- end -->
			 }
	   }) 
	  
	   $(".scripcontent input:last").click(function(){
											  
		    $(".scripcontent").remove();
			
			$("#scrip").remove();
	   }) 
	  }
}




function calculate_byte( sTargetStr ) {
        var sTmpStr, sTmpChar;
        var nOriginLen = 0;
        var nStrLength = 0;
         
        sTmpStr = new String(sTargetStr);
        nOriginLen = sTmpStr.length;

        for ( var i=0 ; i < nOriginLen ; i++ ) {
                sTmpChar = sTmpStr.charAt(i);

                  if (escape(sTmpChar).length > 4) {
                        nStrLength += 2;
                } else if (sTmpChar!='\r') {
                        nStrLength ++;
                }
        }
                  return nStrLength;    
}



