﻿function $(id)
{
	return document.getElementById(id);
};
function $normal(id)
{
	$(id).style.cursor='';
};
function $wait(id)
{
	$(id).style.cursor='wait';
};

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 getScrollTop()
{
    if (typeof(window.pageYOffset) != 'undefined') 
    { 
       return window.pageYOffset; 
    } 
    else if (typeof(document.compatMode)!= 'undefined' && document.compatMode != 'BackCompat') 
    { 
       return document.documentElement.scrollTop; 
    } 
    else if (typeof(document.body)!= 'undefined') 
    { 
       return document.body.scrollTop; 
    }
}

function Ajax(url)
{
	var m_xmlReq=false;
	var m_OnSucceed=function(){};
	var m_OnReading=function(){};
	var m_timer=null;

	this.Url=url;

	if(window.XMLHttpRequest)
	{
		m_xmlReq = new XMLHttpRequest();
	}
	else if(window.ActiveXObject)
	{ 
	   try {m_xmlReq = new ActiveXObject('Msxml2.XMLHTTP'); }catch(e)
	   {
		  try { m_xmlReq = new ActiveXObject('Microsoft.XMLHTTP');}catch(e){}
		}
	}

	this.OnReading=function(reading)
	{
		m_OnReading=reading;
	}

	this.OnSucceed=function(succeed)
	{
		m_OnSucceed=succeed;
	}

	this.OnTimeout=function(timeout)
	{
		m_timer=window.setTimeout(timeout,10000);
	}

	this.invokeServer=function(send_data,method)
	{
		if(!m_xmlReq)  return;
		m_xmlReq.open(method,this.Url,true);
		if(method=='POST') 
		m_xmlReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded;charset=utf-8');  
		m_xmlReq.onreadystatechange=function()
		{
					if(m_xmlReq.readyState == 1)
					{
						m_OnReading();
					}
					else if(m_xmlReq.readyState == 4 && m_xmlReq.status==200)
					{
						var result=null;
						eval("result="+m_xmlReq.responseText);
						m_OnSucceed(result);
						if(m_timer!=null) window.clearTimeout(m_timer);
					}    
		}
		m_xmlReq.send(send_data.toString());
	}
}

function showWindowLoading(divid)
 {
    var locdiv = $(divid);
    locdiv.style.top=getScrollTop()+(window.screen.height/2-200)+"px";
	locdiv.style.left=(window.screen.width-(window.screen.width/2+100))+"px";
	locdiv.style.display="block";
 }
 
function hideLocSelect(itemName)
{	
	$(itemName).style.display="none";
}
 
function hidelocListDivTips()
{
    var locListDivTips=$("locListDiv");
    locListDivTips.style.display = "none";   
}

function setCartCookies(o)
{
    var setCartCookie_Ajax=new Ajax("/ordercenter/ajaxurl/Shopping_CartCookies_Set.aspx");
    setCartCookie_Ajax.OnReading
    (
        function()
        {
            $wait("gm");
            showWindowLoading('window_loading');
        }
    );
    setCartCookie_Ajax.OnSucceed
    (
	    function(result)
	    {
		    hideLocSelect('window_loading');
		    $normal("gm");
		    if(result!=null && result["errorCode"]==0 )
            {  
                showLocSelect($("gm"),null,null,100,190,result["errorCode"],result["Msg"]);
                //把购物车中的商品数量更新
                if($("View_Right_Top1_lblProCount")!=null)
                {
                    $("View_Right_Top1_lblProCount").innerHTML=(parseInt($("View_Right_Top1_lblProCount").innerHTML)+1).toString();
                }
            }
            else
            {
                showLocSelect($("gm"),null,null,86,83,result["errorCode"],result["Msg"]);          
            }
	    }
    );
	setCartCookie_Ajax.OnTimeout
    (
	    function()
	    {
	        hideLocSelect('window_loading');
	        $normal("gm");
            showLocSelect($("gm"),null,null,90,90,"-1","<div class='content_3'><p><b>对不起，商品购买失败！</b></p><p>原因：网络链接超时！</p><p style='text-align:center;cursor:hand;'><a onclick='javascript:hidelocListDivTips();'>关闭</a></p></div><div class='c_b_3'></div>");          
	    }
    );
	setCartCookie_Ajax.invokeServer("proid="+o.toString(),'POST');	
}

function showLocSelect(sender,cfm_func,cancel_func,offsetX,offsetY,errorCode,Msg)
{
	var locListDivTips=$("locListDiv");
	switch(errorCode.toString())
	{
	    case "-1":
	    {
            locListDiv.innerHTML=Msg;
            locListDiv.className = "box_window_3";
	        break;
	    }
	    case "0":	    
	    {
            locListDivTips.innerHTML=Msg;
            locListDivTips.className = "box_window_3";
	        break;
	    }
	    default:
	    break;
	}
	locListDivTips.style.display="";
    locListDivTips.style.top=(getposOffset(sender,"top")-offsetX)+"px";
    locListDivTips.style.left=(getposOffset(sender,"left")-offsetY)+"px";
}
