﻿try  {  document.domain='dangdang.com';  }
catch(err) {} 

var chars = ['0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];

function generateMixed(n) {
    var res = "";
    for(var i = 0; i < n ; i ++) {
        var id = Math.ceil(Math.random()*35);
        res += chars[id];
    }
    return res;
} 
function BodyInit()
{    
    var dl = parent.document.getElementById("div_loading");    
    if(dl.style.display=="none") return;        
    dl.style.display="none";    
    parent.document.getElementById("MsgBoxFrame").style.display="block";    
    resizeMsgBox();
    
   var foc= document.getElementById('txtUsername');
   if(foc != null)
   {
    foc.focus();
    }
}

var loginPath="http://login.dangdang.com/"; 
//var loginPath="";

function showMsgBox(Id,parameters,TransferPath)
{	    
   	var cf=document.getElementById("MsgBoxFrame");
   	cf.style.display="none";
   	cf.src=null;
   	showLoadDiv(Id);
	cf.src="";
	var wcf=window.frames.MsgBoxFrame;
	
	var numargs = arguments.length;      
    if(numargs > 3)
    {
        cf.callback = arguments[3];
        cf.callbackid = arguments[4];
        cf.callbackparameters = arguments[5];        
    }
	
	var oImg=document.getElementById(Id);
	if(!oImg){alert("控制对象不存在！");return;}
	
	var eT=0,eL=0,p=oImg;
	var sT=document.documentElement.scrollTop,sL=document.documentElement.scrollLeft;
	var eH=oImg.height,eW=oImg.width;
	
	while(p&&p.tagName!="BODY")
	{
		eT+=p.offsetTop;
		eL+=p.offsetLeft;
		p=p.offsetParent;
	}
	 
	 var randxx = generateMixed(5);
	cf.src=loginPath+"SignInSimple.aspx?aa="+randxx+"&rurl="+escape(TransferPath)+"?par="+ escape(parameters);
	
	cf.style.top=(document.documentElement.clientHeight-(eT-sT)-eH>=cf.height)?eT+eH-20:eT+15 + "px";
		
	if(IsIframeIE())
	{
	    cf.style.left=(document.documentElement.clientWidth-(eL-sL)>=cf.width)?eL-280:eL  + "px";
	}
	else
	{
	    cf.style.left=eL-250 + "px";
	}
}

function position(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-20:eT+15 + "px";
		
	if(IsIframeIE())
	{
	    var objwidth=0;
	    if(obj.width!=undefined)
	    {	 
	        objwidth=obj.width;	        
	    }
	    obj.style.left=(document.documentElement.clientWidth-(eL-sL)>=objwidth)?eL-280:eL  + "px";
	}
	else
	{
	    obj.style.left=eL-250 + "px";
	}
	
}

function showLoadDiv(control)
{    
    var oImg = document.getElementById(control);
    var div = document.getElementById("div_loading");
    div.style.display='block';    
    position(oImg,div);    
}

function hideMsgBox()
{  
    var mb=document.getElementById("MsgBoxFrame");
    if(mb)
    {
        mb.style.display="none";
        mb.src="";   
        document.onclick=null;    	    
    }
}

function hideLoadDiv()
{
    var div=document.getElementById("div_loading");
    if(div)
    {
        div.style.display="none";
        document.onclick=null;	    
    }
}

function IsIframeIE()
{
    return ( navigator.appName=="Microsoft Internet Explorer" ); 
}
	
function IsNav()
{
	return ( navigator.appName=="Netscape" );
}

//用于登陆后显示调用窗口
function ShowFrame(url)
{
    var dl = parent.document.getElementById("div_loading");
    if(dl.style.display=="none") 
        parent.document.getElementById("div_loading").style.display="block";    ; 
    var cf=parent.document.getElementById('MsgBoxFrame');
        cf.style.display = "none";
        cf.src=url+"&t="+generateMixed(4);
}

function HideFramInLoginPage()
{
    var mb= parent.document.getElementById("MsgBoxFrame");
    if(mb)
    {
    
        mb.style.display="none";
        mb.src="";   
        document.onclick=null;    	
        
         if ('function' == typeof mb.callback)
          { 
          mb.callback(mb.callbackid,mb.callbackparameters);
           }
               
    }
    var div=parent.document.getElementById("div_loading");
    if(div)
    {
        div.style.display="none";
        document.onclick=null;	    
    }
   
   

        
}


//调整大小
function resizeMsgBox()
{ 
      var wp=window.parent;

      var cf=wp.document.getElementById("MsgBoxFrame");

      cf.style.height=null;

      cf.style.width=null;

      cf.style.width= document.documentElement.scrollWidth+20+"px"; 

      if(document.documentElement.scrollHeight==0)

          cf.style.height=document.documentElement.scrollWidth+20+"px";

      else  

          cf.style.height=document.documentElement.scrollHeight+20+"px";

      

      if(cf.offsetLeft+document.documentElement.scrollWidth+20> window.screen.width)

          cf.style.left=cf.offsetLeft-(cf.offsetLeft+document.documentElement.scrollWidth+20-window.screen.width)+"px";      

      if(cf.offsetLeft<0)

          cf.style.left=0;
}





var DIF_dragging=false;
var DIF_iframeBeingDragged="";
var DIF_iframeObjects=new Object();
var DIF_iframeWindows=new Object();
var DIF_iframeMouseDownLeft = new Object();
var DIF_iframeMouseDownTop = new Object();
var DIF_pageMouseDownLeft = new Object();
var DIF_pageMouseDownTop = new Object();
var DIF_handles = new Object();
var DIF_highestZIndex=99;
var DIF_raiseSelectedIframe=false;
var DIF_allowDragOffScreen=false;

// Set to true to always raise the dragged iframe to top zIndex
function bringSelectedIframeToTop(val) {
	DIF_raiseSelectedIframe = val;
	}
	
// Set to try to allow iframes to be dragged off the top/left of the document
function allowDragOffScreen(val) {
	DIF_allowDragOffScreen=val;
	}

// Method to be used by iframe content document to specify what object can be draggable in the window
function addHandle(o, win) {
	if (arguments.length==2 && win==window) {
		// JS is included in the iframe who has a handle, search up the chain to find a parent window that this one is dragged in
		var p = win;
		while (p=p.parent) {
			if (p.addHandle) { p.addHandle(o,win,true); return; }
			if (p==win.top) { return; } // Already reached the top, stop looking
			}
		return; // If it reaches here, there is no parent with the addHandle function defined, so this frame can't be dragged!
		}
	var topRef=win;
	var topRefStr = "window";
	while (topRef.parent && topRef.parent!=window) {
		topRef = topRef.parent;
		topRefStr = topRefStr + ".parent";
		}
	// Add handlers to child window
	if (typeof(win.DIF_mainHandlersAdded)=="undefined" || !win.DIF_mainHandlersAdded) {
		// This is done in a funky way to make Netscape happy
		with (win) { 
			eval("function OnMouseDownHandler(evt) { if(typeof(evt)=='undefined'){evt=event;}"+topRefStr+".parent.DIF_begindrag(evt, "+topRefStr+") }");
			eval("document.onmousedown = OnMouseDownHandler;");
			eval("function OnMouseUpHandler(evt) { if(typeof(evt)=='undefined'){evt=event;}"+topRefStr+".parent.DIF_enddrag(evt, "+topRefStr+") }");
			eval("document.onmouseup = OnMouseUpHandler;");
			eval("function OnMouseMoveHandler(evt) { if(typeof(evt)=='undefined'){evt=event;}"+topRefStr+".parent.DIF_iframemove(evt, "+topRefStr+") }");
			eval("document.onmousemove = OnMouseMoveHandler;");
			win.DIF_handlersAdded = true;
			win.DIF_mainHandlersAdded = true;
			}
		}
	// Add handler to this window
	if (typeof(window.DIF_handlersAdded)!="undefined" || !window.DIF_handlersAdded) {
		eval("function OnMouseMoveHandler(evt) { if(typeof(evt)=='undefined'){evt=event;}DIF_mouseMove(evt, window) }");
		eval("document.onmousemove = OnMouseMoveHandler;");
		window.DIF_handlersAdded=true;
		}
	o.style.cursor="move";
	var name = DIF_getIframeId(topRef);
	if (DIF_handles[name]==null) {
		// Initialize relative positions for mouse down events
		DIF_handles[name] = new Array();
		DIF_iframeMouseDownLeft[name] = 0;
		DIF_iframeMouseDownTop[name] = 0;
		DIF_pageMouseDownLeft[name] = 0;
		DIF_pageMouseDownTop[name] = 0;
		}
	DIF_handles[name][DIF_handles[name].length] = o;
	}

// Generalized function to get position of an event (like mousedown, mousemove, etc)
function DIF_getEventPosition(evt) {
	var pos=new Object();
	pos.x=0;
	pos.y=0;
	if (!evt) {
		evt = window.event;
		}
	if (typeof(evt.pageX) == 'number') {
		pos.x = evt.pageX;
		pos.y = evt.pageY;
	}
	else {
		pos.x = evt.clientX;
		pos.y = evt.clientY;
		if (!top.opera) {
			if ((!window.document.compatMode) || (window.document.compatMode == 'BackCompat')) {
				pos.x += window.document.body.scrollLeft;
				pos.y += window.document.body.scrollTop;
			}
			else {
				pos.x += window.document.documentElement.scrollLeft;
				pos.y += window.document.documentElement.scrollTop;
			}
		}
	}
	return pos;
}

// Gets the ID of a frame given a reference to a window object.
// Also stores a reference to the IFRAME object and it's window object
function DIF_getIframeId(win) {
	// Loop through the window's IFRAME objects looking for a matching window object
	var iframes = document.getElementsByTagName("IFRAME");
	for (var i=0; i<iframes.length; i++) {
		var o = iframes.item(i);
		var w = null;
		if (o.contentWindow) {
			// For IE5.5 and IE6
			w = o.contentWindow;
			}
		else if (window.frames && window.frames[o.id].window) {
			w = window.frames[o.id];
			}
		if (w == win) {
			DIF_iframeWindows[o.id] = win;
			DIF_iframeObjects[o.id] = o;
			return o.id; 
			}
		}
	return null;
	}

// Gets the page x, y coordinates of the iframe (or any object)
function DIF_getObjectXY(o) {
	var res = new Object();
	res.x=0; res.y=0;
	if (o != null) {
		res.x = o.style.left.substring(0,o.style.left.indexOf("px"));
		res.y = o.style.top.substring(0,o.style.top.indexOf("px"));
		}
	return res;
	}

// Function to get the src element clicked for non-IE browsers
function getSrcElement(e) {
	var tgt = e.target;
	while (tgt.nodeType != 1) { tgt = tgt.parentNode; }
	return tgt;
	}

// Check if object clicked is a 'handle' - walk up the node tree if required
function isHandleClicked(handle, objectClicked) {
	if (handle==objectClicked) { return true; }
	while (objectClicked.parentNode != null) {
		if (objectClicked==handle) {
			return true;
			}
		objectClicked = objectClicked.parentNode;
		}
	return false;
	}
	
// Called when user clicks an iframe that has a handle in it to begin dragging
function DIF_begindrag(e, win) {
	// Get the IFRAME ID that was clicked on
	var iframename = DIF_getIframeId(win);
	if (iframename==null) { return; }
	// Make sure that this IFRAME has a handle and that the handle was clicked
	if (DIF_handles[iframename]==null || DIF_handles[iframename].length<1) {
		return;
		}
	var isHandle = false;
	var t = e.srcElement || getSrcElement(e);
	for (var i=0; i<DIF_handles[iframename].length; i++) {
		if (isHandleClicked(DIF_handles[iframename][i],t)) {
			isHandle=true;
			break;
			}
		}
	if (!isHandle) { return false; }
	DIF_iframeBeingDragged = iframename;
	if (DIF_raiseSelectedIframe) {
		DIF_iframeObjects[DIF_iframeBeingDragged].style.zIndex=DIF_highestZIndex++;
		}
	DIF_dragging=true;
	var pos=DIF_getEventPosition(e);
	DIF_iframeMouseDownLeft[DIF_iframeBeingDragged] = pos.x;
	DIF_iframeMouseDownTop[DIF_iframeBeingDragged] = pos.y;
	var o = DIF_getObjectXY(DIF_iframeObjects[DIF_iframeBeingDragged]);
	DIF_pageMouseDownLeft[DIF_iframeBeingDragged] = o.x - 0 + pos.x;
	DIF_pageMouseDownTop[DIF_iframeBeingDragged] = o.y -0 + pos.y;
	}

// Called when mouse button is released after dragging an iframe
function DIF_enddrag(e) {
	DIF_dragging=false;
	DIF_iframeBeingDragged="";
	}

// Called when mouse moves in the main window
function DIF_mouseMove(e) {
	if (DIF_dragging) {
		var pos = DIF_getEventPosition(e);
		DIF_drag(pos.x - DIF_pageMouseDownLeft[DIF_iframeBeingDragged] , pos.y - DIF_pageMouseDownTop[DIF_iframeBeingDragged]);
		}
	}

// Called when mouse moves in the IFRAME window
function DIF_iframemove(e) {
	if (DIF_dragging) {
		var pos = DIF_getEventPosition(e);
		DIF_drag(pos.x - DIF_iframeMouseDownLeft[DIF_iframeBeingDragged] , pos.y - DIF_iframeMouseDownTop[DIF_iframeBeingDragged]);
		}
	}

// Function which actually moves of the iframe object on the screen
function DIF_drag(x,y) {
	var o = DIF_getObjectXY(DIF_iframeObjects[DIF_iframeBeingDragged]);
	// Don't drag it off the top or left of the screen?
	var newPositionX = o.x-0+x;
	var newPositionY = o.y-0+y;
	if (!DIF_allowDragOffScreen) {
		if (newPositionX < 0) { newPositionX=0; }
		if (newPositionY < 0) { newPositionY=0; }
		}
	DIF_iframeObjects[DIF_iframeBeingDragged].style.left = newPositionX + "px";
	DIF_iframeObjects[DIF_iframeBeingDragged].style.top  = newPositionY + "px";
	DIF_pageMouseDownLeft[DIF_iframeBeingDragged] += x;
	DIF_pageMouseDownTop[DIF_iframeBeingDragged] += y;
	}




/////////////////////////////////////////////////////////////////////////////////////////////////////////

function dragIframe(frameName)
{
var o=window.parent.frames[frameName];
var r=[1,1000,1,1000];
var bx=0;
var by=0;
var cx=0;
var cy=0;
window.parent.frames[frameName].document.onmousedown=function(a){
    
	var d=document;
        if(!a)a=window.event||window.parent.frames[frameName].event;
         bx=a.screenX;
         by=a.screenY;
         cx=parent.document.getElementById(frameName).offsetLeft;
         cy=parent.document.getElementById(frameName).offsetTop;
		var x=a.layerX?a.layerX:a.offsetX,y=a.layerY?a.layerY:a.offsetY;
                
		if(window.parent.frames[frameName].document.setCapture)
			window.parent.frames[frameName].document.setCapture();
		else if(window.parent.captureEvents)
			window.parent.captureEvents(Event.MOUSEMOVE|Event.MOUSEUP);


		window.parent.frames[frameName].document.onmousemove=function(a){
			if(!a)a=window.event||window.parent.frames[frameName].event;

            parent.document.getElementById(frameName).style.left=cx+a.screenX-bx;
            parent.document.getElementById(frameName).style.top=cy+a.screenY-by;
            
		};

		window.parent.frames[frameName].document.onmouseup=function(){
			if(window.parent.frames[frameName].document.releaseCapture)
				window.parent.frames[frameName].document.releaseCapture();
			else if(window.parent.captureEvents)
				window.parent.captureEvents(Event.MOUSEMOVE|Event.MOUSEUP);
			window.parent.frames[frameName].document.onmousemove=null;
			window.parent.frames[frameName].document.onmouseup=null;
		};
	};
}



////////////////////////////////////////////////////////////////////////////////////////////////



/////////////////////////////////////////////////////////////////////////////////////////////////////////




function GetPos(inputObj, divObj)
{
	
     
	var inputObj = getPosition(inputObj);		
	
	if(IsIframeIE())
	{
		divObj.style.left = inputObj.x -245 +"px";
		divObj.style.top = inputObj.y + 19+"px";		
	}
	else
	{
		divObj.style.left = inputObj.x-242+"px";
		divObj.style.top = inputObj.y + 20+"px";		
	}

}

function getPosition(o){
	var nLt=0;
	var nTp=0;
	var offsetParent = o;
	while (offsetParent!=null && offsetParent!=document.body) {
			nLt+=offsetParent.offsetLeft;
			nTp+=offsetParent.offsetTop;
 
	if(!IsNav()){
		parseInt(offsetParent.currentStyle.borderLeftWidth)>0?nLt+=parseInt(offsetParent.currentStyle.borderLeftWidth):"";
		parseInt(offsetParent.currentStyle.borderTopWidth)>0?nTp+=parseInt(offsetParent.currentStyle.borderTopWidth):"";
 
		}
 
	offsetParent=offsetParent.offsetParent;
	//alert(offsetParent.tagName);
 }
 
  return {x:nLt, y:nTp};
  
//alert("ID:"+o.id+"\n\nL:"+nLt+" T:"+nTp+"\nW:"+o.offsetWidth+" H:"+o.offsetHeight);
}