﻿String.prototype.trim=function() 
{ 
	return this.replace(/(\s*$)|(^\s*)/g, ''); 
} 

//去除首尾空格
function Trim(s) {return s.replace(/(^\s+)|(\s+$)/g, "");}
String.prototype.TrimAll=function() {return this.replace(/\s+/g, "");}String.prototype.format = function()
{
    var args = arguments;
    return this.replace(/\{(\d+)\}/g,                
        function(m,i){
            return args[i];
        });
}
var xmlReq = false;

function createXMLHttpRequest() 
{
     if(window.XMLHttpRequest)
    {
        xmlReq = new XMLHttpRequest();
        if(xmlReq.overrideMimeType)  xmlReq.overrideMimeType('text/xml');
    }
    else if(window.ActiveXObject)
    { 
       try
       {
            xmlReq = new ActiveXObject('Msxml2.XMLHTTP');
       }
       catch(e)
       {
          try
          {
              xmlReq = new ActiveXObject('Microsoft.XMLHTTP');
          }
          catch(e){}
        }
    }
}
//去除全部空格	
//String.prototype.TrimAll=function() {return this.replace(/\s+/g, "");}

//function SetImage(ImgD,w,h){ 
//var image=new Image(); 
//image.src=ImgD.src; 
//if(image.width > 0 && image.height > 0) 
//{ 
////flag=true; 
//if(image.width/image.height >= w/h) 
//{ 
//if(image.width > w) 
//{ 
//ImgD.width=w; 
//ImgD.height=(image.height*w)/image.width; 
//} 
//else 
//{ 
//ImgD.width=image.width; 
//ImgD.height=image.height; 
//} 

////ImgD.alt=image.width+"x"+image.height; 
//} 
//else 
//{ 
//if(image.height>h){ 
//ImgD.height=h; 
//ImgD.width=(image.width*h)/image.height; 
//} 
//else 
//{ 
//ImgD.width=image.width; 
//ImgD.height=image.height; 
//} 
////ImgD.alt=image.width+"x"+image.height; 
//} 
//} 
//} 

function changeImage()
{
    createXMLHttpRequest();

     num = Math.random();
     num = Math.round(num*10000000);
    var url = 'http://comm.dangdang.com/member/ShowRdmChange.aspx?t='+num;
    
    xmlReq.open("GET", url, false);   
    xmlReq.send(null);
    var img = document.getElementById("validateCode");
    img.src = 'http://comm.dangdang.com/member/ShowVCode.aspx?t='+num;

}

function validateCode()
{
    var code= document.getElementById("validNum").value;
    var url = 'http://comm.dangdang.com/member/ValidateCode.aspx?code='+escape(code);
    if(code.lenB()!=4)
    {
       showValidateInfo();
       return false;
    }
    createXMLHttpRequest();
    xmlReq.open("GET",url,false);
    
    xmlReq.send(null);
		
    var result=xmlReq.responseText;   
    
     if(result=="2")
     {        
         //alert("wrong code");    
         showValidateInfo();          
         changeImage();
         return false;
     }   
     else
     {
        return true;
     }
}
function showValidateInfo()
{
    document.getElementById("errorMessage").style.display = "block";
    document.getElementById("errorMessage").innerHTML="请输入正确的验证码。"; 
}
function clearvalidate()
{
    var errorMessage = document.getElementById("errorMessage");
    if(errorMessage){
        errorMessage.style.display = "none";
    }
}


function SetImage(img){ 
var width=70;
var height=110;
//width="50px" height="69px"
var image=new Image(); 
image.src=img.src;
	if(image.width>0 && image.height>0)
	{
	  if(image.width!=width)
	  {
//	  	rate=image.height/image.width;
//	  	newHeight=rate*width;
	  	img.width=width;
//	  	img.height=newHeight;
	  } 
	}
	if(img.height>height)
	{
//		rate=image.width/image.height;
//		newWidth=rate*height;
		img.height=height;
//		img.width=newWidth;
	}
}

function bookImage(img){ 
var width=72;
var height=112;
//width="50px" height="69px"
var image=new Image(); 
image.src=img.src; 
	if(image.width>0 && image.height>0)
	{
	  if(image.width!=width)
	  {
	  	rate=image.height/image.width;
	  	newHeight=rate*width;
	  	img.width=width;
	  	img.height=newHeight;
	  } 
	}
	if(img.height>height)
	{
		rate=image.width/image.height;
		newWidth=rate*height;
		img.height=height;
		img.width=newWidth;
	}
}

function CheckRad()
{
    var RadValid=requireRadFieldValid();
    
    if(RadValid)
    {
        $('btn_Report').disabled="";
    }
} 


function CheckReport()
{
    var RadValid=requireRadFieldValid();
    var CountValid=CheckCount();
    
//    var ContValid=requireBoby();
    
    if(RadValid&CountValid)
    {
        $('btn_ReportConfirm').click();
    }
}

function requireRadFieldValid()
{
    var isValid;
    var objReaction=$('rab_Reaction');
    var objAdvert=$('rab_Advert');
    var objCopyright=$('rab_Copyright');
    var objElse=$('rab_Else');
        
    isValid=objReaction.checked!=''|objAdvert.checked!=''|objCopyright.checked!=''|objElse.checked!='';   
    //objMsgSpan.style.color = "red"; 
    //objMsgSpan.innerHTML=isValid?'':errorMessage;
    return isValid;
}

var errorMessage="";

function requireBoby()
{
    var isValid;
    var objBoby=$('txt_Boby').Value;
        
    isValid=objBoby!='';   
    $('MsgSpan').style.color = "red"; 
    $('MsgSpan').innerHTML=isValid?'':errorMessage;
    return $('txt_Boby').Value!='';
}

//内容字数
function CheckCount()
{   
    var ret=false;
    var TotalLenght=250;    
    var contlen = $("txt_Body").value.length;
    var leave = TotalLenght-contlen;
    if(leave>=0)
    {
//       hide($("txt_Boby"));
       $("p_result").innerHTML = "您还可以输入"+leave+"字"; 
       ret = true;      
    }
    else
    {        
        $("p_result").innerHTML = "<span class='red_purple'>您输入超出"+Math.abs(leave)+"字。</span>";        
    }
    return ret;    
}function checkProductReview(  )
{       
    var controlB=document.getElementById("txtBody");
    if(controlB==null)
    {
      return false;
    }
    else
    {
      var controlBValue=controlB.value.trim();      
       
       if(controlBValue=="")
       {         
            alert("正文不能为空！"); 
            controlB.focus();
            return false;      
       }
        if(!CheckCount())
       {      
            alert("正文不能超过250字！");
            controlB.focus();
            return false;       
       }      
       if(!validateCode())
       {                  
           return false;
       }       
       document.getElementById("btnPostback").click();       
    }      
}

function copyurl()
{
    var txt=document.getElementById("txt_pageUrl").value;

    var copyf={};
    copyf.copyToClipboard=function(txt) { 
    //clipboardData.setData('text',viurl.value);
    //alert('复制成功，请粘贴到你的QQ/MSN上推荐给你的好友！');

    if(window.clipboardData) 
    { 
        window.clipboardData.clearData(); 
        window.clipboardData.setData("Text", txt); 
    } 
    else if(navigator.userAgent.indexOf("Opera") != -1) { 
        window.location = txt; 
    } 
    else if (window.netscape) { 
    try { netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect"); 
    } 
        catch (e) {alert("您的firefox安全限制限制您进行剪贴板操作，请打开’about:config’将signed.applets.codebase_principal_support’设置为true’之后重试"); 
        return false; 
    } 
    var clip = Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard); 
    if (!clip) 
    return; 
    var trans = Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable); 
    if (!trans) 
    return; 
    trans.addDataFlavor('text/unicode'); 
    var str = new Object(); 
    var len = new Object(); 
    var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString); 
    var copytext = txt; 
    str.data = copytext; 
    trans.setTransferData("text/unicode",str,copytext.length*2); 
    var clipid = Components.interfaces.nsIClipboard; 
    if (!clip) 
    return false; 
    clip.setData(trans,null,clipid.kGlobalClipboard); 
    } 
    }

    copyf.copyToClipboard(txt);
}
  
function Reset()
{
    $('rab_Reaction').checked="";
    $('rab_Advert').checked="";
    $('rab_Copyright').checked="";
    $('rab_Else').checked="";
    $('btn_Report').disabled="true";
    $('txt_Boby').value="";
    CheckCount();
}

///////////////////////////
//留言板///
//////////////////////////
function onTextClick(txt)
{
    if(!txt||txt.value!="写点什么吧...") 
        return;
    else
        txt.value="";
    
}

function hide(o)
{
    if(o)
        o.style.display="none";
}
function show(o)
{
    if(o)
        o.style.display="block";
}

//留言字数
function boardCheckCount()
{   
    var ret=false;
    var TotalLenght=200;    
    var contlen = Trim($("txt_boby").value).length;
    var leave = TotalLenght-contlen;
    if(leave>=0&leave<200)
    {
       hide($("btn_mesgray"));
       show($("btn_mes"));
       
       $("sp_result").style.visibility="hidden";        
    }
    else if(leave==200)
    {
       hide($("btn_mes"));
       show($("btn_mesgray"));
    }
    else
    {        
        $("sp_result").style.visibility="visible";         
        $("sp_result").innerHTML = "已超出"+Math.abs(leave)+"字。";        
        hide($("btn_mes"));
        show($("btn_mesgray"));
    }
    if(leave>=0&leave<200)
        ret = true;
    return ret;    
}

function requireBoardFieldValid()
{
    var ret=false;
    if(!$("txt_boby").value=="" && $("txt_boby").value!="写点什么吧..."&&Trim($("txt_boby").value)!="") ret=true;
    else 
        $("sp_result").innerHTML = "请您填写信息！";
    return ret;
}

function CheckBoard()
{  
    var CountValid=boardCheckCount();
    var boardValid=requireBoardFieldValid();
    var valid =validateCode();
    
    if(boardValid&CountValid&valid)
    {
        $('btn_boardSubmit').click();
    }
}

//显示head详细
function showHeadInfo(img_id, img_path,review_count,bookshelf_count,name,address,url_user,url_review,url_bookshelf,review_level, active_degree, review_honor)
{
    var mid = $(img_id);
    var tb =  $("tag_box");
    if(tb)
    {
        hookEvent( "tag_box","mouseout",function(event){currentHeadObj(event);});
        tb.innerHTML = userHeadWin(img_path,review_count,bookshelf_count,name,address,url_user,url_review,url_bookshelf, review_level, active_degree, review_honor);
        tb.style.display='block';                
        setHeadPosition(mid,tb);
    }
    
//    var dhi = $("div_headinfo");
//    var img = $("img_head");
//    var amh = $("aimg_head");
//        
//    var ahr = $("a_review");
//    var ari = $("a_reviewimg");
//    var ahb = $("a_bookshelf");
//    var abi = $("a_bookshelfimg");
//    
//    var ahn = $("a_name");    
//    var pha = $("p_headaddress");   
//  
//      
//    //img    
//    if(img)
//    {
//        if(img_path)
//            img.src = img_path;
//        else
//            img.src = "";
//        
//    }
//    //评论数量
//    if(ahr)
//    {
//        
//        if(review_count)
//            ahr.innerHTML = review_count;
//        else
//            ahr.innerHTML = "";
//        //评论地址    
//        if(url_review)
//        {   
//            ahr.href = url_review;
////            ahr.target= "_blank";
//            if(ari)
//                ari.href = url_review;
//            
//        }
//        else
//            ahr.href = "javascript:void(0)";
//        
//        
//    }
//    //书架数量
//    if(ahb)
//    {
//        if(bookshelf_count)
//            ahb.innerHTML =bookshelf_count;
//        else
//            ahb.innerHTML ="";
//        //书架地址    
//        if(url_bookshelf)
//        {           
//            ahb.href = url_bookshelf;
////            ahb.target= "_blank";
//            if(abi)
//                abi.href = url_bookshelf;
//        }
//        else
//            ahb.href = "javascript:void(0)";
//    }
//    //用户名
//    if(ahn)
//    {
//       if(url_user)
//       { 
//         ahn.href      = url_user;
//         if(amh)
//            amh.href      = url_user;         
//       }
//       ahn.alt       = name;
//       ahn.title     = name; 
//       ahn.innerHTML = name;      
//    }
//    //地址
//    if(pha)
//    {
//        if(address)
//            pha.innerHTML = address;
//        else
//            pha.innerHTML = "";
//        
//    }
//    if(dhi)
//    {
//        dhi.style.display='block';
//        setHeadPosition(mid,dhi);
//    }
    
}

function setHeadPosition(control,obj)
{
    var eT=0,eL=0,p=control;
	var sT=document.documentElement.scrollTop,sL=document.documentElement.scrollLeft;
	var eH=control.height,eW=control.width;
	
	while(p&&p.tagName!="BODY")
	{
		eT+=p.offsetTop;
		eL+=p.offsetLeft;
		p=p.offsetParent;
	}
	obj.style.top=(document.documentElement.clientHeight-(eT-sT)-eH>=obj.height)?eT+eH:eT-4 + "px";
		
	if(IsIE())
	{
	    var objwidth=0;
	    if(obj.width!=undefined)
	    {	 
	        objwidth=obj.width;	        
	    }
	    obj.style.left=(document.documentElement.clientWidth-(eL-sL)>=objwidth)?eL-3:eL  + "px";
	}
	else
	{
	    obj.style.left=eL-4 + "px";
	}
}

//隐藏书详细
function hideHeadInfo()
{
    var dhi = $("tag_box");    
    if(dhi)
    {
        dhi.style.display='none';        
    }    
}

function currentHeadObj(e)
{
    e=e||event;
    
    if(isIE()){
        var obj = e.toElement;
        while( obj!=null && obj.id!="tag_box" ){
            obj = obj.parentElement;
        }
        if( obj==null ){ hideHeadInfo(); }
    }
    else{
        if (!inElem($("tag_box"),e)){
            hideHeadInfo();
        }
    }
}

var messageBoardUrl="http://comm.dangdang.com/member/{0}/messageboard";
var valiEmailUrl = "https://login.dangdang.com/VerifyEmail.aspx";
var loginUrl = "https://login.dangdang.com/signin.aspx?returnURL=";

var messValiHtml = "(<span class=\"red_nn\">*</span>请先完成<a href=\"{0}\">邮箱验证</a>)";
var leftValiHtml = "(<span>*</span>请先完成<a href=\"{0}\">邮箱验证</a>)";

function validateUser(type,did,cid){

    if(type==3)
         addInterestWin(cid,did);
         
    createXMLHttpRequest();
    var url = "http://comm.dangdang.com/member/ValidateCheck.aspx"+ "?d=" + new Date();
    xmlReq.open("GET",url,false);
    
    xmlReq.send(null);
		
    var result=xmlReq.responseText;
    if(result=="0")
    {
        switch(type)
        {
            case 1:
            $("txt_boby").style.display="block";
            break;
            case 2:
            window.location.href=messageBoardUrl.format(did);
            break;            
        }
    }
    else
    {
        switch(result)
        {
            case "5":
            if(type==1)
                $("p_valiEmail").innerHTML=messValiHtml.format(valiEmailUrl);
            else if(type==2)   
                $("p_notice").innerHTML=leftValiHtml.format(valiEmailUrl);
            break;
        
            case "4":
            if(type==1)
                $("p_valiEmail").innerHTML=messValiHtml.format(valiEmailUrl);
            else if(type==2)   
                $("p_notice").innerHTML=leftValiHtml.format(valiEmailUrl);
            break;
            //未验证邮箱的帐户，点击“有用”“没用”，提示：请先完成邮箱验证。“邮箱验证”加链接，链接到相应的“被动验证”和“主动验证”页。 
            //5为被动验证  4为主动验证
            case "3":         
            if(type==1)
                window.location.href=loginUrl+escape(window.location.href);
            else if(type==2)
                window.location.href=loginUrl+escape(messageBoardUrl.format(did));
                break;
            case "2":
            alert("提交失败");
                break;          
            default:
                break;
        }
    }    
}

/////////////////////
/////////////////////////////
//////////////////////////////////////
