﻿


window.onerror=function()
{
	return true;
}
function JSLoad(url, container, type, defer, language, title)
   {
     
      if(container == undefined || container == null) container = this;

      // setup container
      if(typeof container.write == "undefined")
         if(typeof container.document != "undefined")
            container = container.document;
         else throw "Invalid container. Unable to load [" + url + "]";

    
      if(type == undefined || type == null)
      {
         type = '';

       
         if(language == undefined || language == null)
         {
            language = undefined;
            type = "text/javascript";
         }
      }

    
      if(language == undefined || language == null) language = "JavaScript";

      // set title
      if(title == undefined || title == null) title = '';

      // set defer
      if(defer == undefined) defer = false;

     
      var script = container.createElement("script");
      script.defer = defer;
      script.language = language;
      script.title = title;
      script.type = type;
      script.src = url;

    
      var head = container.getElementsByTagName("head")[0];
      head.appendChild(script);
   }
   
function $(element) {
  if (typeof element == 'string')
    element = document.getElementById(element);
  return element;
}


var PosHelper = {
getPosition : function(o){
	
	var nLt=0;
	var nTp=0;
	var offsetParent = o;
	
	while (offsetParent!=null && offsetParent!=document.body) {
			
			nLt+=offsetParent.offsetLeft;
			nTp+=offsetParent.offsetTop;
	
	if(!this.IsNav()){		
		parseInt(offsetParent.currentStyle.borderLeftWidth)>0?nLt+=parseInt(offsetParent.currentStyle.borderLeftWidth):"";
		parseInt(offsetParent.currentStyle.borderTopWidth)>0?nTp+=parseInt(offsetParent.currentStyle.borderTopWidth):"";	
		}	
	offsetParent=offsetParent.offsetParent;	
	}
	
	return {x:nLt, y:nTp}; 
},


IsIE:function(){
		return ( navigator.appName=="Microsoft Internet Explorer" ); 
},

IsNav:function (){
	return ( navigator.appName=="Netscape" );
},

SetOffset:function(sobj,tobj,x,y )
{

    if (typeof sobj == 'string') sobj = document.getElementById(sobj);    
    if (typeof tobj == 'string') tobj = document.getElementById(tobj);
    
    var pos = this.getPosition(sobj);
    if(tobj)
    {
        tobj.style.left= pos.x + x + "px";
        tobj.style.top = pos.y + y +  "px";			
        tobj.style.display="block";	
    }
}

}

// JScript File
function Ajax(url)
{
    this.m_xmlReq=false;
    this.Url=url;
    
    if(window.XMLHttpRequest)
    {
        this.m_xmlReq = new XMLHttpRequest();
        if(this.m_xmlReq.overrideMimeType)  this.m_xmlReq.overrideMimeType('text/xml');
    }
    else if(window.ActiveXObject)
    { 
       try
       {
            this.m_xmlReq = new ActiveXObject('Msxml2.XMLHTTP');
       }
       catch(e)
       {
          try
          {
              this.m_xmlReq = new ActiveXObject('Microsoft.XMLHTTP');
          }
          catch(e){}
        }
    }
    
    this.invokeServer=function(send_data,method)
    {
        if(!this.m_xmlReq)  return;
                        
        this.m_xmlReq.open(method,this.Url,false);
                
        if(method=='POST') 
        //this.m_xmlReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');            
         this.m_xmlReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8"); 
         
        this.m_xmlReq.send(send_data.toString());             
        var result=null;
        if(this.m_xmlReq.status==200 && this.m_xmlReq.readyState == 4)
            eval("result="+this.m_xmlReq.responseText);  
        
        return result;     
                       
    }
}


var StringHelper = {
filter : function(str)
{
	var re1 = /(\<.[^\<]*\>)/g;
    return str.replace(/"/g,"").replace(/'/g,"").replace(re1,"");
}

}

String.prototype.lenB   =   function(){return   this.replace(/[^\x00-\xff]/g,"**").length;}   

var StringBuilder = function(){
    this.cache = [];
    if(arguments.length)this.append.apply(this,arguments);
}
StringBuilder.prototype = {
    prepend:function(){
        this.cache.splice.apply(this.cache,[].concat.apply([0,0],arguments));
        return this;
    },
    append:function(){
        this.cache = this.cache.concat.apply(this.cache,arguments);
        return this;
    },
    toString:function(){
        return this.getString();
    },
    getString:function(){
        return this.cache.join('');    
    }
}



var reInnerHTML = function(ele,innerHtml) {
			var nA=navigator.appVersion;
			if(nA.indexOf('MSIE')>=0)var curIE=nA.substr(nA.indexOf('MSIE')+5,3);
			if (curIE){
				innerHtml=innerHtml.replace(/<script([^>]*)>/gi,"<script$1 defer=\"true\">");
				var reStartScript=/^(\s*<script)/gi;
				if (reStartScript.test(innerHtml)){
					innerHtml="<span style=\"display:none;\">Hack IE</span>"+innerHtml;
					ele.innerHTML=innerHtml;
					ele.removeChild(ele.firstChild);
				}else{
					ele.innerHTML=innerHtml;
				}

			}else{
				var newObj=document.createElement(ele.tagName);
				newObj.id=ele.id;
				newObj.className=ele.className;
				newObj.innerHTML = innerHtml;
				ele.parentNode.replaceChild(newObj,ele);
			}
		}
	
	

var XMLHttp = {
_createXMLHttpRequest : function()
     {
         if (window.XMLHttpRequest)  {  var objXMLHttp = new XMLHttpRequest();  }
         else
         {
             var MSXML = ['MSXML2.XMLHTTP', 'Microsoft.XMLHTTP'];
             var Count = MSXML.length;
             for(var i = 0; i < Count; i ++)
             {
                 try
                 {
                     var objXMLHttp = new ActiveXObject(MSXML[i]);        
                     break;
                 }
                 catch(e)
                 {
                 }
             }
          }          
        
         // mozilla某些版本没有readyState属性
         if (objXMLHttp.readyState == null) {
             objXMLHttp.readyState = 0;
             objXMLHttp.addEventListener("load", function ()
                 {
                     objXMLHttp.readyState = 4;                    
                     if (typeof objXMLHttp.onreadystatechange == "function") { objXMLHttp.onreadystatechange();}
                 },   false);
         }

         return objXMLHttp;
     },
     
_sendRequest : function(method, url, data, sendtype, datatype, callback, backpar)
     {
         var objXMLHttp = this._createXMLHttpRequest();        
         with(objXMLHttp)
         {
             try
             {
                 // 加随机数防止缓存
                 if (url.indexOf("?") > 0){ url += "&randnum=" + Math.random(); }
                 else{ url += "?randnum=" + Math.random();}
                
                
                 open(method, url, sendtype);              
                 setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");                
                 send(data);
                 
                   if(callback == null)
                   {
                      if (objXMLHttp.readyState == 4 && (objXMLHttp.status == 200 || objXMLHttp.status == 304))
                         {
                         return objXMLHttp.responseText;
                         }
                   }
        
                 onreadystatechange = function ()
                 {                  
                     if (objXMLHttp.readyState == 4 && (objXMLHttp.status == 200 || objXMLHttp.status == 304))
                     {
                         if(callback != null){ 
                          if(backpar != null) callback(objXMLHttp,backpar); else callback(objXMLHttp); 
                         } else { return objXMLHttp.responseText; }
                     }
                 }
                
               
             }
             catch(e)
             {
                 alert(e+"/n"+'你的浏览器版本太低,建议你升级浏览器');
             }
         }
     }
    
    
}


var currTId = null;
var currDId = null;
function mouseOver(obj,tag0,tag1,id)
{

if( currTId!=null)
{
 if($(currTId)) $(currTId).style.display="block";
 if($(currDId)) $(currDId).style.display="none";	   
}	

var Tid = tag0 + id;
var Did = tag1 + id;

if($(Tid)) $(Tid).style.display="none";	   
if($(Did)) $(Did).style.display="block";	  

currTId = Tid;
currDId = Did;


}


function openWin(url,name,w,h)
 {
    var x=(window.screen.width-w)/2;
	var y=(window.screen.height-h)/2;
	var win=window.open(url,name,'toolbar=0,location=0,status=0,top='+y+',left='+x+',width='+w+',height='+h );
	win.focus();
	
	return win;
 }
function openSuitWin(url,name)
 {
	var sw=window.screen.width;
	var sh=window.screen.height;
	var w=sw-300;
	var h=sh-300;
	var x=(sw-w)/2;
	var y=0;
	var win=window.open(url,name,'toolbar=0,scrollbars=1,location=1,menubar=1,resizable=1,status=1,top='+y+',left='+x+',width='+w+',height='+h );
	win.focus();
 }
 
 var last_img_btn_chg_prd=null;
 
function ImgBtnChgPrd_Mouseover(obj,mainpictureurl,largepictureurl)
 {

    if(last_img_btn_chg_prd!=null) 
        last_img_btn_chg_prd.className='img_btn_chg_prd';
    last_img_btn_chg_prd=obj;
    obj.className='img_btn_chg_prd_active';
    
    

    document.getElementById('img_show_prd').src=mainpictureurl;
	document.getElementById('img_show_prd').onclick=function(){ImgBtnChgPrd_Click(this,largepictureurl);}
	document.getElementById('spn_tobig').onclick=function(){ImgBtnChgPrd_Click(this,largepictureurl);}
	
 }

function ImgBtnChgPrd_Click(obj,largepictureurl)
 {
 
  if(arguments.length>1)
     {  
       document.getElementById('hid_largepictureurl').value=largepictureurl;
    }
	var win=openWin('/picexp.aspx','preview',700,580);
	win.opener=this;
	   
 }

function suitImage(img,w,h)
{
		var image=new Image();
        image.src=img.src;

		var iw=image.width;
		var ih=image.height;
		var iratio=iw/ih;
        
        if(iw>w)
		{
                iw=w;
				ih=w/iratio;
        }
        if(ih>h)
		{
                ih=h;
                iw=h*iratio;
        }  
        
		if(iw > 0 && ih > 0)
        {    
		    img.width=iw;
		    img.height=ih;
	    }
}

function ImageOver(Mainimage,LargeImage)
{

var img = document.getElementById('largePic');
 document.getElementById('hid_largepictureurl').value=LargeImage;
img.src=Mainimage;

	
}
function AddToMallShoppingCart(product_id)
{
	var url=null;

	if(product_id==null || product_id<1)
		url="http://shopping.dangdang.com/shoppingcart/shopping_cart.aspx";
	else
		url="http://shopping.dangdang.com/shoppingcart/shopping_cart_add.aspx?product_ids="+product_id+"&reference_url="+escape(window.location.href);

	var popup=window.open(url,"shoppingcart");
	popup.focus()
}

function AddToWishList(product_id)
{  
	

	showMsgBox('favor',product_id,'http://customer.dangdang.com/wishlist/remote_addtofavorlist.aspx');

}

function addlike(product_id)
{
    if(confirm(unescape("%u786E%u5B9A%u5C06%u6B64%u4EA7%u54C1%u52A0%u5165%u60A8%u611F%u5174%u8DA3%u7684%u5546%u54C1%u5417%uFF1F")))
			document.getElementById("likepd").src="http://www.dangdang.com/usercut/savelikepd.asp?productid="+product_id+"&like=1";
}

function AddToOutOfStock(product_id)
{
    var url='http://www.dangdang.com/shoppingcart/pdnothing.aspx?pdname='+product_id;
    openWin(url,400,280);
}

function set_review_summary(product_type,total_review_count,total_question_count)
{
    //var str_total_count=product_type=='GM' || product_type=='UnKnow'?total_question_count:total_review_count;
      var str_total_count= product_type=='UnKnow'?total_question_count:total_review_count;
    if(str_total_count>'0')
    {
        var reviewTag = document.getElementById("review_summary");
        var revTag = document.getElementById("spn_rev_cnt");
        
        if(reviewTag)reviewTag.style.display='inline';
        if(revTag) revTag.innerHTML=str_total_count;
    }
    else
    {
     var respan = document.getElementById("reviewscount");
     if(respan) respan.style.display="none";    
    }
}


var red_array=["顾客评分：","<img src='images/star_gray.gif' />","<img src='images/star_gray.gif' />","<img src='images/star_gray.gif' />","<img src='images/star_gray.gif' />","<img src='images/star_gray.gif' />"];
function set_average_score(average_score)
{
    if(average_score=='' || average_score < 0.5)
        return;

    var int_average_score = parseInt(average_score);
    for(var i=0;i<int_average_score;i++)
        red_array[i+1]="<img src='images/star_red.gif' />";
        
    if(int_average_score<average_score)
        red_array[i+1]="<img src='images/star_red2.gif' />";
    
    if( document.getElementById('spn_average_score'))
    document.getElementById('spn_average_score').innerHTML=red_array.join('');
}


function AddToDonateShoppingCart(product_id)
{
	var url=null;

	if(product_id==null || product_id<1)
		url="http://shopping.dangdang.com/shoppingcart_donate/shopping_cart.aspx";
	else
		url="http://shopping.dangdang.com/shoppingcart_donate/shopping_cart_add.aspx?product_ids="+product_id+"&reference_url="+escape(window.location.href);

	var popup=window.open(url,"shoppingcart");
	popup.focus()
}


function getposOffset(what, offsettype)
{ 
    var totaloffset=(offsettype=="left")? what.offsetLeft : what.offsetTop; 
    var parentEl=what.offsetParent; 
    while (parentEl!=null)
    { 
        totaloffset=(offsettype=="left")? totaloffset+parentEl.offsetLeft : totaloffset+parentEl.offsetTop; 
         parentEl=parentEl.offsetParent; 
    } 
    return totaloffset; 
} 

function showLocSelect(objid,divId,x_offset,y_offset)
{
	
	var locdiv=document.getElementById(divId);
	var obj=document.getElementById(objid);
	
    locdiv.style.top=(getposOffset(obj,"top")-y_offset)+"px";
	locdiv.style.left=(getposOffset(obj,"left")-x_offset)+"px";
	locdiv.style.display="block";
}

function hideLocSelect(divId)
{	
	document.getElementById(divId).style.display="none";
}



var cat_link =
{		
		'作者':'http://search.book.dangdang.com/search.aspx?category=01&key2=',
		'出版社':'http://search.book.dangdang.com/search.aspx?category=01&key3=',
		'歌手':'http://search.music.dangdang.com/search.aspx?catalog=03&key2=',
		'演奏者':'http://search.music.dangdang.com/search.aspx?catalog=03&key2=',
		'导演':'http://search.movie.dangdang.com/search.aspx?catalog=05&key2=',
		'主演':'http://search.movie.dangdang.com/search.aspx?catalog=05&key4='
};



var li_ids=
{
        '作者':'author_',
		'出版社':'publisher_',
		'歌手':'singer_',
		'演奏者':'singer_',
		'导演':'director_',
		'主演':'star_'
};



var click_99_name=
{
        '作者':'author',
		'出版社':'publisher',
		'歌手':'singer',
		'演奏者':'singer',
		'导演':'director',
		'主演':'star'
};

function key_link(cat,key,mode)
{
    var click_name = null;       
    click_name = click_99_name;
   
    var normal_html= "";
	var link =cat_link[cat] + escape(key);
	var link_name=click_name[cat];
	var rep_id=li_ids[cat];
	var link_html="<a  class='blue12a'  href='"+link+"' name='"+link_name+"' target='_blank'>"+key+"</a>";
	if(document.getElementById(rep_id))normal_html = document.getElementById(rep_id).innerHTML;	
	if(document.getElementById(rep_id)) document.getElementById(rep_id).innerHTML=normal_html.replace(key,link_html);
}



//present

function hidden(id)
{      
   if($(id))$(id).style.display="none";
  
}

var result;
function OpenPresentWin(Id,parameters)
{ 
    var lin =  $("tag_box");               
    var sta = parameters.split(":");      
          
    if(result ==null)
    {
                     
    var aAjax= new Ajax("OutputDataMall.aspx");	    	
    var send_data = "Dtype=present" + "&Par="+parameters
    result = aAjax.invokeServer(send_data,'POST');
    
    }
    if(result.errorCode != 1)
    {                 
      lin.innerHTML = presentWin(result);         
      PosHelper.SetOffset(Id,lin,0,20);       
    }
}


function presentWin(preData){

    var sb = new StringBuilder('<div class="wind_present" onmouseover=javascript:showLocSelect("giftlink","tag_box",0,-20); onmouseout=javascript:hidden("tag_box");>');    
    sb.append('<div class="pres_c">');
    sb.append('<a  target="_blank" href="http://product.dangdang.com/product.aspx?product_id=',preData.pid,'"><img src="',preData.image,'" border="0" \/></a>');
    sb.append('<h3><a  target="_blank" class="blue_w" href="http://product.dangdang.com/product.aspx?product_id=',preData.pid,'">',preData.pname,'<\/a><\/h3>');
    sb.append('<p>市场价：',preData.price,'<\/p>');
    sb.append('<\/div>'); 
    sb.append('<\/div>');       
    return sb.getString();

}


